|
I disagree, the right way to do it is explicitly checking against None. '' is false, 0 is false, [] is false, lots of things that you could potentially want to accept, if not in this specific situation then in other similar ones, are false. self.x = x or y will bite you in the rear end sooner or later, self.x = x if x is not None else y is safe.
|
# ? Nov 28, 2013 20:54 |
|
|
# ? May 30, 2024 14:20 |
|
Haystack posted:.sort() sorts the list in place and returns None. You want: I'm actually realizing that I want something else. Sorry Now that I look at it, what I want to do is flip the associations between date and values, so that: code:
code:
|
# ? Nov 28, 2013 21:58 |
|
breaks posted:I disagree, the right way to do it is explicitly checking against None. '' is false, 0 is false, [] is false, lots of things that you could potentially want to accept, if not in this specific situation then in other similar ones, are false. self.x = x or y will bite you in the rear end sooner or later, self.x = x if x is not None else y is safe. Yeah, I agree with this and I'm almost sure no one calls this pythonic and the only place I remember the guy was recommending using this "or pattern" was in Dive into Python which is a book full of weird advice.
|
# ? Nov 28, 2013 22:12 |
|
A Violence Gang posted:Thanks, that all makes a lot of sense! As for I forgot to mention in my last post that Dive into Python is pretty much anti-recommended in this thread. For text-based learning these are probably the best recommendations (copy-pasted from the new OP I'm working on): Think Python: How To Think Like A Computer Scientist [http://www.greenteapress.com/thinkpython/] There’s a link there to read it online without paying anything. Learn Python The Hard Way [http://learnpythonthehardway.org/] There’s a link there to read it online without paying anything.
|
# ? Nov 28, 2013 22:20 |
|
A Violence Gang posted:Thanks, that's much easier. One more question -- there's a more idiomatic way to do this, isn't there? My answer: don't do this. Randomly choosing from a bunch of names should not be something your constructor does if you don't pass in anything. Make a factory method: Python code:
|
# ? Nov 28, 2013 23:08 |
|
Symbolic Butt posted:Yeah, I agree with this and I'm almost sure no one calls this pythonic and the only place I remember the guy was recommending using this "or pattern" was in Dive into Python which is a book full of weird advice. There's a lot of code-reviewed production code on my team that makes use of the Python code:
If the answer is no, we've found the above code is simpler and less likely to trick your brain into having to actually analyze the whole line in case it's more than an assignment with a mutable default value. if the answer is yes, then sure, go with your choice of: Python code:
Python code:
|
# ? Nov 29, 2013 04:25 |
|
OK, I can see how this does cool stuff in the case where I'm subclassing the thing we're talking about (which I am). The subclasses all inherit the factory method, you can define names in the parent class, override it in the subclass if one needs a unique list of random choices and that'll all work since cls will refer to the subclass if you call it that way. That's cool, it factors out the common stuff that all the subclasses need to do at initialization but means you can still do unique stuff in each subclass's constructor (or, I suppose, they can also have their own factory methods). But aside from the benefits a factory method provides when used in a parent class, I'm a little unclear on: Suspicious Dish posted:Randomly choosing from a bunch of names should not be something your constructor does if you don't pass in anything.
|
# ? Nov 29, 2013 05:13 |
|
I think it's a bad API in general. Simple defaults so that callers can omit things in the common case are OK, but having a sophisticated fallback with logic seems wrong. Explicit is better than implicit.
|
# ? Nov 29, 2013 06:01 |
|
A Violence Gang posted:Thanks, that all makes a lot of sense! As for They'd be very easy to edit and share between users. Here's an example: Python code:
|
# ? Nov 29, 2013 06:38 |
|
Pollyanna posted:I'm actually realizing that I want something else. Sorry Now that I look at it, what I want to do is flip the associations between date and values, so that: Python code:
|
# ? Nov 29, 2013 06:53 |
|
QuarkJets posted:They'd be very easy to edit and share between users. Here's an example: Yeah but hardcoding poo poo is dumb and you should basically never do it??? What if you want to load the names from a webserver or don't want to have to type "<name>", every time you want to add a new name. Or have a list of names you got from somewhere that you just want to copypaste in (I know you can easily write a script to add the commas and quotes but that's not the point).
|
# ? Nov 29, 2013 06:58 |
|
Hardcoding stuff is fine for testing. You can easily read names from a file at runtime, too. Also, Quark, you didn't show off the best feature: Python code:
Python code:
|
# ? Nov 29, 2013 07:18 |
|
Jewel posted:Yeah but hardcoding poo poo is dumb and you should basically never do it??? What if you want to load the names from a webserver or don't want to have to type "<name>", every time you want to add a new name. Or have a list of names you got from somewhere that you just want to copypaste in (I know you can easily write a script to add the commas and quotes but that's not the point). Then you replace the code in "all_names.py" with the code to load the names from a webserver, or the code to read from a text file if that's what you really want to do. My suggestion was really more related to code management. Besides, the guy was already using a "hardcoded" list of names since with an interpreted language there isn't really much difference between a list of names in a .py file that you read with an import and a list of names in a .txt file that you read with open(). QuarkJets fucked around with this message at 11:00 on Nov 29, 2013 |
# ? Nov 29, 2013 08:00 |
|
Jewel posted:Yeah but hardcoding poo poo is dumb and you should basically never do it??? What if you want to load the names from a webserver or don't want to have to type "<name>", every time you want to add a new name. Or have a list of names you got from somewhere that you just want to copypaste in (I know you can easily write a script to add the commas and quotes but that's not the point). Yeah! And what if you need to decode the list from a stegonographic image payload hosted on an eBay auction? Or pull it ou of the FFT of a music file? </sarcasm> Hard coding stuff is fine if that's all you need.
|
# ? Nov 29, 2013 17:47 |
|
Dren posted:Yeah! And what if you need to decode the list from a stegonographic image payload hosted on an eBay auction? Or pull it ou of the FFT of a music file? </sarcasm> I just don't see why adding a single line of code to read a txt file is a "worse solution" than putting " ", on every name
|
# ? Nov 29, 2013 17:56 |
|
Reading a list from a text file is fine. But depending on the use-case so is editing the python file directly.
|
# ? Nov 29, 2013 19:06 |
|
Jewel posted:I just don't see why adding a single line of code to read a txt file is a "worse solution" than putting " ", on every name Neither one is worse than the other, I was just serving it up as a suggestion to try as an alternative. I like mine better because I personally dislike using open() and mixing .txt and .py files in a project, but it doesn't really matter. What I really wanted was to see that line of code moved to its own module, since the impression that I got was that the poster was going to be using a lot of those kinds of lines in a lot of places, so putting them in all in one place and just using an import makes sense. I don't really care what the underlying code is
|
# ? Nov 30, 2013 07:22 |
|
Anyone know how to get matplotlib on ubuntu 13.10 to properly display unicode characters? It works on my laptop but not on my desktop. I have 13.10 installed on both.
|
# ? Dec 1, 2013 02:29 |
|
Dren posted:Anyone know how to get matplotlib on ubuntu 13.10 to properly display unicode characters? Just a guess, missing fonts?
|
# ? Dec 1, 2013 04:53 |
|
BigRedDot posted:Just a guess, missing fonts? That's what I thought but I'm having a hard time telling for sure. It looks like both installs end up pointed at the same font file. Next I need to open the ttf in something and verify that the code points I need are actually in the font. Surprisingly there doesn't seem to be a *nix utility for doing that. Charmap has been in windows for years.
|
# ? Dec 1, 2013 06:40 |
|
Semi-crossposting from the Ruby on Rails thread: I know drat near nothing about programming, but want to make a web app. A programmer friend suggested Ruby on Rails as a good starter language/framework/whatever exactly the word I ought to be using is. However, the FAQ suggests learning Python first. Basically what I want to make is a web app that will accept forms from users with some basic info, and then spit out an automated email based on it. Should I stick to Python for this, or would it be better to go straight to Ruby on Rails? If this is the wrong question to ask, or if a web app is the wrong thing to do for this, please let me know. Eventually I'd like to have an iOS and Android app that have similar functionality, but I figure that's a ways out.
|
# ? Dec 2, 2013 01:13 |
|
I'd say give Flask a shot if you want to try python. It's easy enough to get the form submission working, and then you will possibly want to rely on some kind of email module (smtplib for example) and some kind of queue to process the outgoing emails.
|
# ? Dec 2, 2013 01:29 |
|
Hey guys, I am having some trouble with a GUI I am creating for a school project. Basically i want to load an image into a tkinter GUI, it seems pretty simple. The image loads fine when it is loaded directly via the _ini_ function. But once inside another function it refuses to load, just a blank screen, sized to the size of the image. Heres some code where i can reproduce the problem: import tkinter from tkinter import * class ApocGUI(): def __init__(self): self.main_window = tkinter.Tk() self.cheat_button = tkinter.Button(self.main_window, \ #command = lambda D = PhotoImage(file = 'americasoldier.gif') : self.cheat_button.config(image = D)) command = self.clicked) A = PhotoImage(file = 'tank.gif') self.cheat_button['image'] = A self.cheat_button.grid (row = 0, column = 0) tkinter.mainloop() def clicked(self) : D = PhotoImage(file = 'americasoldier.gif') self.cheat_button.config(image = D) apoc = ApocGUI() any help would be appreciated!
|
# ? Dec 2, 2013 02:00 |
|
See this: http://stackoverflow.com/questions/4297949/image-on-a-button In the future, please use the code tags so your code is more readable.
|
# ? Dec 2, 2013 02:39 |
|
foutre posted:Semi-crossposting from the Ruby on Rails thread: I know drat near nothing about programming, but want to make a web app. A programmer friend suggested Ruby on Rails as a good starter language/framework/whatever exactly the word I ought to be using is. However, the FAQ suggests learning Python first. If it's simply a web form that will post data to a server which will handle the mailing, Python + Flask or Django would be a good choice. If it's more complicated than that or you want to make it do pretty things, you'll probably have to dive into an additional language for that functionality. iOS/Android development are different ecosystems in terms of languages and frameworks, but if you have a web app it's already cross platform * Codeacademy isn't the best resource, but this might help conceptualize what is going on and some of what you'll need to be aware of when doing Web Stuff. Relevant info for Django and relevant info for Flask. The latter is most relevant for your what you want to do, though I'm not going to pretend I read it. edit: Flask might be a pain to set up as the docs are written for someone who is already familiar with web development in mind, but I'm sure if you ask questions in this thread you'll get pointed in the right direction. * If you are interested, there is Xamarin that uses C# and Kivy that uses Python. The latter also suffers from that lack of good documentation, but it looks pretty. salisbury shake fucked around with this message at 14:19 on Dec 2, 2013 |
# ? Dec 2, 2013 13:12 |
|
plz help http://stackoverflow.com/questions/20331189/matplotlib-unicode-not-rendering-in-ubuntu-13-10 quote:I have an issue where the following snippet will render the unicode character as the square block you get when stuff can't render. This happens in matplotlib 1.3.1 on one of my ubuntu 13.10 systems (that was freshly installed) but the glyph will render properly on a different ubuntu 13.10 system (that was upgraded from 13.04). If you know of a better place to ask for help with matplotlib than stack or on here I'd like to know about that too.
|
# ? Dec 2, 2013 16:00 |
|
Posting again about SQlite and databases: How would I go about setting up something to replace a 3-axis nested Python data structure in the format: {symbols: {dates: {attributes: values}}}? Create a table of the main symbols, then have a table for each symbol, with the symbol as the primary key, and attributes as columns. Each entry would be a date?
|
# ? Dec 3, 2013 00:28 |
Python code:
|
|
# ? Dec 3, 2013 01:50 |
|
fletcher posted:
You could put this in its own module and then do from thingy import cached_expensive_stuff.
|
# ? Dec 3, 2013 01:53 |
FoiledAgain posted:You could put this in its own module and then do from thingy import cached_expensive_stuff. My example might be a little too simplified. There's other attributes in Thingy() that are used in get_expensive_stuff, so I thought it made sense to make it a method on Thingy()
|
|
# ? Dec 3, 2013 01:58 |
|
fletcher posted:
Do you mean automating caching? That's more of a ruby thing to do (), but there is actually a memoization decorator in Python 3's functools module (lru_cache). What you have looks fine. Like I'd be more likely to look at it closer during code review if you had used a fancy decorator than if you just did it explicitly like you did.
|
# ? Dec 3, 2013 02:10 |
|
PS: make sure it's "class Thing(object):" - when you don't inherit from "object" you are defining "old style classes" which is a deprecation measure you should steer clear of.
|
# ? Dec 3, 2013 02:12 |
Mr. Wynand posted:Do you mean automating caching? That's more of a ruby thing to do (), but there is actually a memoization decorator in Python 3's functools module (lru_cache). Yup, I had a feeling Python might have something fancy like that lru_cache. I'm still on 2.7 so I won't go down that route just yet. Thanks for the feedback!
|
|
# ? Dec 3, 2013 02:13 |
Mr. Wynand posted:PS: make sure it's "class Thing(object):" - when you don't inherit from "object" you are defining "old style classes" which is a deprecation measure you should steer clear of. Ah good to know. It's actually a Django model so it inherits from models.Model. I haven't actually declared any of my own classes yet outside of those models, but I will keep that in mind.
|
|
# ? Dec 3, 2013 02:15 |
|
fletcher posted:
Lifted more or less unmolested from Pyramid's source: Python code:
Python code:
|
# ? Dec 3, 2013 02:15 |
|
Dominoes posted:Posting again about SQlite and databases: How would I go about setting up something to replace a 3-axis nested Python data structure in the format: {symbols: {dates: {attributes: values}}}? Stepping up one level in complexity would be one table to hold symbols, one table to hold attribute names, and a third table would hold a foreign key to symbol, a foreign key to attribute name, have a column for the value of the attribute, and one column for the date.
|
# ? Dec 3, 2013 02:33 |
Haystack posted:Lifted more or less unmolested from Pyramid's source Well that's pretty drat slick, I don't quite understand everything going on in there though. What's it doing with __doc__?
|
|
# ? Dec 3, 2013 02:34 |
|
Yeah, if you are comfortable with decorators this is definitely a good time to use one. Since I was obviously late with that, edit to add: the __doc__ stuff is so that the wrapped function's docstring is retained (so you can still see it in IDEs, help(), ...). In general you should use functools.wraps to make your replacement function look like the wrapped function; they don't do so there since they are taking the pretty funky approach of replacing the wrapped function with a descriptor instead of another function. breaks fucked around with this message at 02:48 on Dec 3, 2013 |
# ? Dec 3, 2013 02:36 |
|
fletcher posted:Well that's pretty drat slick, I don't quite understand everything going on in there though. What's it doing with __doc__? breaks posted:Yeah, if you are comfortable with decorators this is definitely a good time to use one. I really would discourage this. Using decorators just because you can (and ostensibly understand them) will very often lead to regrettable decisions. Some parts of python need to be used more judiciously than others. Decorators are definitely towards the "more judicious" end. Descriptors even more so. I'm not saying you shouldn't use a memoizer here, but it is far from "definitely a good idea", and I personally would always err on the side of more explicit and obvious.
|
# ? Dec 3, 2013 02:57 |
|
|
# ? May 30, 2024 14:20 |
|
Mr. Wynand posted:I really would discourage this. Using decorators just because you can (and ostensibly understand them) will very often lead to regrettable decisions. Some parts of python need to be used more judiciously than others. Decorators are definitely towards the "more judicious" end. Descriptors even more so. Every feature of a language should be used judiciously. Python dictionaries are wonderful, but their over-allocation growth is extremely aggressive. If you don't understand this you can easily write extremely suboptimal code. Can you expand or provide some examples for why decorators in particular are different? Right now this admonishment sounds like a "just-so" story. BigRedDot fucked around with this message at 05:39 on Dec 3, 2013 |
# ? Dec 3, 2013 05:37 |