|
If I want my class to be modular what would be the best way to go about appending more variables to it? I was thinking about keeping a dict of all the custom variables but that seems like a bad idea.
|
# ? Jul 19, 2015 00:27 |
|
|
# ? May 31, 2024 18:02 |
|
Cingulate posted:I actually do that - not much, but sometimes.
|
# ? Jul 19, 2015 00:33 |
|
Cingulate posted:Things I know I don't do: Learn how to make effective use of classes and objects. There's a reason they're so ubiquitous in modern computing.
|
# ? Jul 19, 2015 01:36 |
|
superstepa posted:If I want my class to be modular what would be the best way to go about appending more variables to it? I was thinking about keeping a dict of all the custom variables but that seems like a bad idea. If you want to add more variables to a class, you can create a new class that inherits from it or just edit the original class. Maybe I'm missing what your goal is
|
# ? Jul 19, 2015 02:30 |
|
QuarkJets posted:If you want to add more variables to a class, you can create a new class that inherits from it or just edit the original class. Maybe I'm missing what your goal is It sounds like he wants to pass a number of variables that might be unwieldly, and is worried about just having tons of them. (Rather than just passing kwargs or somehing, maybe?) EAT THE EGGS RICOLA fucked around with this message at 02:37 on Jul 19, 2015 |
# ? Jul 19, 2015 02:32 |
|
Dominoes posted:Learn the functools, itertools, and collections modules. Nippashish posted:Learn how to make effective use of classes and objects. There's a reason they're so ubiquitous in modern computing.
|
# ? Jul 19, 2015 03:08 |
|
Cingulate posted:Things I do: I'm just now getting into the whole programming game, but since you're also a linguist: did you learn Python using the NLTK book? If so, would you recommend it?
|
# ? Jul 19, 2015 09:45 |
|
Badly Jester posted:I'm just now getting into the whole programming game, but since you're also a linguist: did you learn Python using the NLTK book? If so, would you recommend it? I know a bunch of other peopel in the linguistics thread are also pretty good at Python, I think at least foiled is much better than I am actually.
|
# ? Jul 19, 2015 10:28 |
|
superstepa posted:If I want my class to be modular what would be the best way to go about appending more variables to it? I was thinking about keeping a dict of all the custom variables but that seems like a bad idea. If you want multiple "versions" of a class that do the same "sort of thing" but do it slightly differently, there are two main ways to achieve that: inheritance (creating a subclass that overrides aspects of the base class's behaviour or makes the base class's behaviour more specific to a particular use case) and composition (attaching sub-objects to your class that implement specific functionality, letting you customise your class's behaviour by choosing what you attach to it) Like QuarkJets I'm not sure what you're aiming to do.
|
# ? Jul 19, 2015 14:17 |
|
QuarkJets posted:If you want to add more variables to a class, you can create a new class that inherits from it or just edit the original class. Maybe I'm missing what your goal is Hammerite posted:If you want multiple "versions" of a class that do the same "sort of thing" but do it slightly differently, there are two main ways to achieve that: inheritance (creating a subclass that overrides aspects of the base class's behaviour or makes the base class's behaviour more specific to a particular use case) and composition (attaching sub-objects to your class that implement specific functionality, letting you customise your class's behaviour by choosing what you attach to it) What I want is to have one main class that only defines a few very basic methods and variables and then have a large bulk of extra methods/variables appended to an instance of that class post initialization. I know you can just use setattr but I was wondering if there is an "official" way to do that. Sorry, I think I'm just making a really simple question sound really convoluted.
|
# ? Jul 19, 2015 21:47 |
|
superstepa posted:What I want is to have one main class that only defines a few very basic methods and variables and then have a large bulk of extra methods/variables appended to an instance of that class post initialization. I know you can just use setattr but I was wondering if there is an "official" way to do that. You effectively want to transform one type of object into another type of object? I'm not sure I've ever seen that done, and I'm not aware of any dedicated language feature to do that. If you are set on transforming the object itself for some reason then create some sort of method that does all your setattr calls for you in a standard way. Some sort of composition approach is probably what you want to do though.
|
# ? Jul 19, 2015 22:38 |
|
superstepa posted:What I want is to have one main class that only defines a few very basic methods and variables and then have a large bulk of extra methods/variables appended to an instance of that class post initialization. I know you can just use setattr but I was wondering if there is an "official" way to do that. On the face of it there is nothing to stop you from achieving this by just assigning to the objects. Can't really give you any advice about what you should really do, though, because I have no idea what you want to achieve. code:
|
# ? Jul 19, 2015 22:42 |
|
superstepa posted:What I want is to have one main class that only defines a few very basic methods and variables and then have a large bulk of extra methods/variables appended to an instance of that class post initialization. I know you can just use setattr but I was wondering if there is an "official" way to do that. This sounds a little bit like you want to create an abstract base class and then create subclasses from that. What's the underlying problem you're trying to solve?
|
# ? Jul 19, 2015 23:41 |
|
superstepa posted:What I want is to have one main class that only defines a few very basic methods and variables and then have a large bulk of extra methods/variables appended to an instance of that class post initialization. I know you can just use setattr but I was wondering if there is an "official" way to do that. This indeed sounds like class inheritance. If your objects have completely different sets of properties (which would make them structurally unique when using classes and would have you create subclass for every object), you could store properties in dict or something in a class that describes the way they are the same. Otherwise, if you can group them based on sets of properties, sub classing is the way to go, as it was designed for that. (New to python, so I might be overlooking something) Basically, (to use the common example) you can create class Vehicle that is a parent class of Car, Bicycle, Skateboard, Boat, Airplane, and so on. Vehicle would contain properties like speed and mass, while subclasses would contain specific stuff, like MPG for Car, or cruising altitude for Airplane. You could make intermediary classes for wheeled vehicles that share property numberOfWheels, etc...
|
# ? Jul 20, 2015 14:55 |
|
Well he's wanting to do this post-initialization so that's not really subclassing. He wants to add properties and methods to object instances, not to classes. You do this by just assigning to instance attributes. Also read this about methods: http://stackoverflow.com/a/2982/23972
|
# ? Jul 20, 2015 18:02 |
|
I am thinking about moving from Python 2 to 3 and to change the way I work as I am starting a new job with a lot more collaboration in September. I am also moving from PC to Mac in about two weeks. What I do now: I use Python 2.7 and lots of data packages like scipy, numpy, pandas, statsmodels, scikitlearn, nltk, networkx. I use Ipython notebooks to mess around and generally move cleaned up chunks of code into sublime for reuse. I like to explore things in R as well, but never got Rpy2 to work. I have been really bad about learning classes and objects and just write messy functions. I'm also terrible about version control (the version1.py, version2.py approach discussed above rings true). What I would like to do better, informed by the advice of future colleagues: 1. Learn useful Unix commands 2. Use Git 3. Write better code - more comments, start incorporating classes and objects Questions I have for the thread: - I've looked up the documentation for the packages I use and they all have versions for Python 3. This seems like a good a time as any to make the switch, as I will be starting a lot of new projects and leaving most of my old work behind. Are there any drawbacks? Things I should look out for? - I will continue to do exploration and experimenting in Ipython notebook, but I would like to do more involved coding in an editor with nice features (works with git, for example?). Is there a clear winner? -General advice for this kind of transition. ie how would you set up your system for writing code differently if you had to start over? hope its not too vague
|
# ? Jul 20, 2015 18:24 |
|
You can version control ipynbs. And I use all these packages minus networkx, exclusively under 3. You probably want to add seaborn btw.
|
# ? Jul 20, 2015 18:46 |
|
quote:What I would like to do better, informed by the advice of future colleagues:
quote:Questions I have for the thread: quote:- I will continue to do exploration and experimenting in Ipython notebook, but I would like to do more involved coding in an editor with nice features (works with git, for example?). Is there a clear winner? Note about Git: Its basic use is straightforward, but the CLI is a mess. Take advantage of PyCharm's integration, and look things up when needed. Dominoes fucked around with this message at 19:04 on Jul 20, 2015 |
# ? Jul 20, 2015 18:48 |
|
I have dictionary whose values are lists of lists. I want to write these lists to one csv file per key. According to this Stack Overflow question, this was straight-forward in Python 2. However, I get an error about not being able to write the value to the file at writer.writerow(value) in this code:Python code:
|
# ? Jul 20, 2015 19:00 |
|
Your code works for me, after making two modifications: setting the open file as 'w' to write instead of the default of read, and iterating over the list of lists so it splits each file into multiple rows.Python code:
|
# ? Jul 20, 2015 19:10 |
|
Dominoes posted:Your code works for me, after making two modifications: setting the open file as 'w' to write instead of the default of read, and iterating over the list of lists so it splits each file into multiple rows. Holy poo poo Ok, how can I write to the file without the brackets for each list? I saw this post, but I got a "float not iterable" message, even though the example has floats. I've also tried using itertools.chain to flatten my lists of lists, but that results in the same type of error. hooah fucked around with this message at 19:56 on Jul 20, 2015 |
# ? Jul 20, 2015 19:35 |
|
hooah posted:Ok, how can I write to the file without the brackets for each list?
|
# ? Jul 20, 2015 20:03 |
|
Great. Didn't realize you'd done a ninja edit.
|
# ? Jul 20, 2015 20:17 |
|
You should also add code:
quote:If csvfile is a file object, it should be opened with newline=''. [1]
|
# ? Jul 20, 2015 22:00 |
|
Yeah, I figured that out after it printed list, blank line, list, blank line...
|
# ? Jul 20, 2015 22:15 |
|
Is there a simple way to do a string comparison that is case insensitive? I'm doing the MIME comparison game thing from codingame.com. Python code:
|
# ? Jul 21, 2015 14:34 |
|
Gothmog1065 posted:Is there a simple way to do a string comparison that is case insensitive? I'm doing the MIME comparison game thing from codingame.com. if ext.lower() in (t.lower() for t in types): If you have a lot of places where you need to compare strings case-insensitively you might create a simple class that implements a notion of case-insensitive string.
|
# ? Jul 21, 2015 14:42 |
|
Hammerite posted:if ext.lower() in (t.lower() for t in types): Bleh, that works but that's not the solution I need, now I have to pull the keys from the dictionary with this with these case insensitivities. I could force all the keys to be lowercase, but I don't think that would work for the particular solution. Going to have to think on this.
|
# ? Jul 21, 2015 15:02 |
|
Yeah, that one ended up being trickier than I initially thought. I did end up lower-casing the keys, if that helps.
|
# ? Jul 21, 2015 15:07 |
|
hooah posted:Yeah, that one ended up being trickier than I initially thought. I did end up lower-casing the keys, if that helps. Actually that does work, it's not the key, it's the value that's important for the result. Thanks guys! e: thanks again! I ended up changing around some bits and bobs due to the way they were playing with filenames: Python code:
Gothmog1065 fucked around with this message at 15:50 on Jul 21, 2015 |
# ? Jul 21, 2015 15:19 |
|
NumPy question. Can I somehow do something like this:code:
code:
|
# ? Jul 21, 2015 21:17 |
|
Something likecode:
|
# ? Jul 21, 2015 21:37 |
|
Oh thanks.
|
# ? Jul 21, 2015 21:47 |
|
Suppose I wanted to run a function for a specified time period (it gathers data from the Internet and prints it to a file). I can doPython code:
|
# ? Jul 21, 2015 23:32 |
|
What's the easiest way to share some simple games I wrote in Python with non-tech savvy people? If the answer is freeze it in an .exe, what's the best current utility for doing so?
|
# ? Jul 22, 2015 00:03 |
|
dantheman650 posted:What's the easiest way to share some simple games I wrote in Python with non-tech savvy people? If the answer is freeze it in an .exe, what's the best current utility for doing so? Freeze 'em, but it is going to suck unless something drastic changed in the last 6 months and no one told me. You'll want to use py2exe which, I think, works on python 2 or 3 now.
|
# ? Jul 22, 2015 00:43 |
|
This is one of Python's biggest weaknesses; there's no clean way.
|
# ? Jul 22, 2015 01:03 |
|
Ghost of Reagan Past posted:Suppose I wanted to run a function for a specified time period (it gathers data from the Internet and prints it to a file). I can do Python code:
|
# ? Jul 22, 2015 15:32 |
|
It's been 6 years since I read this thread(!). Well, I'm still a lovely programmer, but am looking to fix that. In short, I'm looking for information on running loops over lists containing lists. I built this 7 segment LED display using shift registers to interface with my raspberry pi: http://forums.somethingawful.com/showthread.php?threadid=2734977&pagenumber=311#post447919014 I need assistance building the "rendering engine" because the existing libraries I found, listed below, are designed as demonstrations or work with only a single digit. Is there a library out there that already works with multiple 7 segment displays? My final device will be between 9-12 digits. https://github.com/mignev/shiftpi https://github.com/shrikantpatnaik/Pi7SegPy https://github.com/m01/sevensegment Addressing the segments looks like this: 10 millions place far left, highest outputs(56-63), ones-place (0-7). Here's what 8 digits looks like, even though I only have 5 digits built at the moment. 10,000,000 1,000,000 100,000 10,000 1,000 100 10 1 [55-63] [48-55] [40-47] [32-39] [24-31] [16-23] [8-15] [0-7] These are the pin outs from the 595 to the segments. Technically the 595 has these alphabetically labeled. A = 1 B = 0 C = 4 D = 5 E = 6 F = 3 G = 2 DP = 7 #decimal point Here are some examples - to get these familiar outputs bring these pins high: patterns = "0": [A, B, C, D, E, F], "1": [B, C], "2": [A, B, D, E, G], "3": [A, B, C, D, G], "4": [B, C, F, G], "5": [A, C, D, F, G], "6": [A, C, D, E, F, G], "7": [A, B, C], "8": [A, B, C, D, E, F, G], "9": [A, B, C, D, F, G], ".": [DP] } Okay so here's my idea, I wonder if there's a better way: Let's say I have an IP address which I want to display: "192.168.0.1" - a string with 8 digits and 3 decimal points. Let's assume I have 8 shift registers/digits so I don't have overflow/or scroll the device. First, my rendering engine is going to split that string into a list [1,9,2,.,1,6,8,.,0,.,1]. #Note that this has 11 elements Next, each element of the list is looked up in the patterns dictionary/truth table to find which pins to bring high. The result being : [[B, C],[A, B, C, D, F, G],[A, B, D, E, G],[DP],[B, C],[A, C, D, E, F, G],[A, B, C, D, E, F, G],[DP],[A, B, C, D, E, F],[DP],[B, C]]. Now run a loop to combine the decimal point, [DP] with the preceding list element. This is just the lazy way I see of doing it. I could eventually make a truth table with 255 patterns. [[B, C],[A, B, C, D, F, G],[A, B, D, E, G, DP],[B, C],[A, C, D, E, F, G],[A, B, C, D, E, F, G, DP],[A, B, C, D, E, F],[DP, B, C]]. # back to 8 elements The numerical values of this list are: [[0, 4],[1, 0, 4, 5, 3, 2],[1, 0, 5, 6, 2, 7],[0, 4],[1, 4, 5, 6, 3, 2],[1, 0, 4, 5, 6, 3, 2, 7],[1, 0, 4, 5, 6, 3],[7, 0, 4]] Next, flip the list so that "1" or [0,4] or [B,C] segments are in the left side's 10 millions place. [[7, 0, 4],[1, 0, 4, 5, 6, 3],[1, 0, 4, 5, 6, 3, 2, 7],[1, 4, 5, 6, 3, 2],[0, 4],[1, 0, 5, 6, 2, 7],[1, 0, 4, 5, 3, 2],[0, 4]] # probably good to copy this variable before going further; I guess for strings larger than 8 digits, I can pop values out and do a marquee scrolling effect If I write this to the shift register, it will only address the "ones" place. I need to add multiples of 8 to the values of each element in the list: [0]+0, [1]+8, [2]+16, [3]+24, [4]+32, [5]+40, [6]+48, 7[+56] So on so that I end up with: [[7, 0, 4],[9, 8, 12, 13, 14, 11],[17, 16, 20, 21, 22, 19, 18, 24],[25, 28, 29, 30, 27, 26],[32, 36],[41, 40, 45, 46, 42, 47],[49, 48, 52, 53, 51, 50],[56, 60]] At this point, I need to write a loop that goes over every element in the list and sublist and turns it "high" or "on". I suppose this just turns into a string of 64 rising and falling voltages. I then latch the values, display it, and then repeat with new information. Does this make sense? There is a lot more I want to do: like setting PWM for individual segments. Centering/right justifying/left justifying. Building digits/characters segment by segment, have digits "fly" in from the left and right. But for now, I just need a way to use the entire display, and not just the first digit. Does this sound like a smart way to go about it?
|
# ? Jul 22, 2015 16:33 |
|
|
# ? May 31, 2024 18:02 |
|
Munkeymon posted:
E: I was just hoping there was a fancy way to do it with a generator or something (you know, reflecting on it, I was just hoping for some cool new way to do this task beyond a while loop). Whatever, this way is transparent and probably better Python than anything fancy. Ghost of Reagan Past fucked around with this message at 17:59 on Jul 22, 2015 |
# ? Jul 22, 2015 17:29 |