|
Haystack posted:Yeah, this one gets a lot of people. Basically, when you assign to a variable anywhere within a scope (such as the one a function creates), it overwrites any outer variable, regardless of whether or not the logical flow assigns anything to the variable. It's generally considered a flaw in the language, and python 3 added a keyword that lets you work around it. Who considers this a flaw? The alternative is that whether or not a statement references a local or global reference depends on whether or not some condition happens to succeed or fail. That would be pretty impossible to reason about in any sensible way and would lead to all manner of subtle bugs. I'm also wondering what keyword you are referring to. There was nonlocal that was added but that's really only useful in closures.
|
# ? Jul 21, 2013 17:10 |
|
|
# ? May 24, 2024 02:24 |
|
BigRedDot posted:Who considers this a flaw? The alternative is that whether or not a statement references a local or global reference depends on whether or not some condition happens to succeed or fail. That would be pretty impossible to reason about in any sensible way and would lead to all manner of subtle bugs. I think what most people expect, thinking that python is an interpreted language, is that resolution would occur line-by-line. If the local has been defined yet, then it's local. If it's not, then you start going up and looking for a global. If you can't find it anywhere, then you throw an error. By the way, guess what this produces. Python code:
|
# ? Jul 21, 2013 18:02 |
|
evensevenone posted:I think what most people expect, thinking that python is an interpreted language, is that resolution would occur line-by-line. If the local has been defined yet, then it's local. If it's not, then you start going up and looking for a global. If you can't find it anywhere, then you throw an error. By the way, guess what this produces.
|
# ? Jul 21, 2013 18:12 |
|
The best solution is no more globals! :p
|
# ? Jul 21, 2013 18:14 |
|
Thermopyle posted:The best solution is no more globals! :p Would this stuff also apply to nested scopes that weren't global (e.g. closures)? edit: yeah looks like Python code:
Python code:
Emacs Headroom fucked around with this message at 18:45 on Jul 21, 2013 |
# ? Jul 21, 2013 18:41 |
|
Mr. Wynand posted:That's kind of weird for HTTP. Dominoes fucked around with this message at 19:04 on Jul 21, 2013 |
# ? Jul 21, 2013 18:59 |
|
Thermopyle posted:The best solution is no more globals! :p Well, the same thing happens with all names. The issue that brought this up was actually a module name that had the same name as a local variable in a later part of the function.
|
# ? Jul 21, 2013 19:34 |
|
evensevenone posted:Well, the same thing happens with all names. The issue that brought this up was actually a module name that had the same name as a local variable in a later part of the function. Yeah, I got you. In other news, PyCharm 3.0 EAP is out and a notable feature is more extensive SQLAlchemy support!
|
# ? Jul 21, 2013 19:41 |
|
evensevenone posted:I think what most people expect, thinking that python is an interpreted language, is that resolution would occur line-by-line. If the local has been defined yet, then it's local. If it's not, then you start going up and looking for a global. If you can't find it anywhere, then you throw an error. I agree that's what a person might expect at first blush. I think after actually giving it any amount of thought whatsoever they would conclude that that would be a terrible idea in practice. BigRedDot fucked around with this message at 00:59 on Jul 22, 2013 |
# ? Jul 22, 2013 00:56 |
|
Moddington posted:Plus, you taught me you can use tuples in function argument definitions. Don't know how I missed that handy little bit of syntax. Python code:
|
# ? Jul 22, 2013 01:23 |
|
Lysidas posted:Do not use this syntax. It is a language mistake that was removed in current versions of Python. Yeah, I found that out today when I tried to use it. Guess that's why I missed it before.
|
# ? Jul 22, 2013 02:23 |
|
Thermopyle posted:The best solution is no more globals! :p I agree with this. Whenever you think you need a global, there's actually a solution that doesn't use globals and that is also probably easier to understand
|
# ? Jul 22, 2013 03:59 |
|
QuarkJets posted:I agree with this. Whenever you think you need a global, there's actually a solution that doesn't use globals and that is also probably easier to understand The same resolution scheme applies to everything: imported modules, classes, bare functions, etc. Something has to go in the global namespace and it has to have a name.
|
# ? Jul 22, 2013 07:49 |
|
Is there a reason why this isn't allowed? (Two sequences to unpack in a call) I mean, it's clear why it isn't allowed in the def statement argument list. It wouldn't be meaningful to have two starred arguments there. But I think it's obvious what the below "should" do. code:
code:
Hammerite fucked around with this message at 09:41 on Jul 22, 2013 |
# ? Jul 22, 2013 09:38 |
|
http://www.python.org/dev/peps/pep-0448/
|
# ? Jul 22, 2013 11:48 |
|
Dominoes posted:Oops - was using the wrong endpoint. There is indeed a "Transfer-Encoding: Chunked" header in the streaming API. I'm still a bit unclear how to use it - I'm trying r.iter_lines() per the documents. There's also a section on chunked data, referring passing a generator function as the "data" property. I can't figure out how to get data from the open HTTP socket, but it seems like the program can't continue with the request open, so perhaps streaming requires a separate thread. So is it a blob of coherent json in each chunk? Does it just keep going until you forcefully close the connection?
|
# ? Jul 22, 2013 19:28 |
|
Mr. Wynand posted:So is it a blob of coherent json in each chunk? Does it just keep going until you forcefully close the connection? 2nd question - apparently yes. From the TradeKing website: quote:The socket must stay open! I'm still trying to figure out how to make it work; when using the correct (streaming) endpoint, I can no longer receive any useful data. Python code:
If I only run that, the code stops after printing the headers. When i run 'print(r.json())', I don't get any output (except for output before that line), and the console window stays up with a flashing cursor, indicating it's doing something, indefinitely. The Requests docs suggest running this for streaming: Python code:
Unrelated: Is there an easy way to play MIDI files in Python? PyAudio and PyGame are good for playing WAVs or other sampled input, and PyGame has many tools for creating MIDI or sending it to/receiving it from external devices, but has no obvious way to play midi files. Dominoes fucked around with this message at 22:36 on Jul 22, 2013 |
# ? Jul 22, 2013 21:53 |
|
Can you post your latest code in a gist? Or PM me an API key I can test with?
|
# ? Jul 22, 2013 23:08 |
|
Houston Rockets posted:Can you post your latest code in a gist? Or PM me an API key I can test with? I'm not comfortable giving out my current keys, since they're linked to my brokerage account (it buys/sells stocks, performs requests with the account balances etc), but I may be able to get user keys. Dominoes fucked around with this message at 23:35 on Jul 22, 2013 |
# ? Jul 22, 2013 23:32 |
|
QuarkJets posted:I would like to create a GUI with PyQt that displays a 2D numpy array. Is this relatively straightforward? Is it better to convert the image to a QPixMap or a QImage? Or is it better to try to embed matplotlib into my PyQt GUI? I'm creating a slider so that the displayed image can be switched rapidly, so I was imagining that refreshing matplotlib over and over might be suboptimal, but maybe not? I'm still fond of Kivy for doing GUIs. Its all bleeding edge stuff, but its cross plaform as hell, and very pretty. Personally I'd be trying to prerender those images as much as possible,, and just load up a background buffer with the next (and previous if the slider goes both ways) images so you can just blit them across. edit: If these datasets are really big, you might even want to consider setting up your bitmaps using something like cython with static typing which can win you some pretty huge speed-ups if done right.
|
# ? Jul 23, 2013 00:40 |
|
Hammerite posted:Is there a reason why this isn't allowed? (Two sequences to unpack in a call) I was just wondering the other day if something like this would be too crazy: Python code:
|
# ? Jul 23, 2013 03:50 |
|
What is the difference between dict.values() and dict.itervalues()? For example in the following code snippets as far as I can tell they produce the same output. Python code:
Python code:
|
# ? Jul 23, 2013 19:09 |
|
Jose Cuervo posted:What is the difference between dict.values() and dict.itervalues()? values() returns a list, itervalues() returns an iterator. If after reading that, you have more questions...ask away!
|
# ? Jul 23, 2013 19:14 |
|
Use itervalues(). It uses less memory and it is generally quicker.
|
# ? Jul 23, 2013 19:16 |
|
If you ever get to use Python 3.x, the normal keys/values/items methods return iterators rather than lists.
|
# ? Jul 23, 2013 19:27 |
|
In Py3, they're called views. http://www.python.org/dev/peps/pep-3106/quote:There are (at least) two ways to accomplish this. The original plan was to simply let .keys(), .values() and .items() return an iterator, i.e. exactly what iterkeys(), itervalues() and iteritems() return in Python 2.x. However, the Java Collections Framework [1] suggests that a better solution is possible: the methods return objects with set behavior (for .keys() and .items()) or multiset (== bag) behavior (for .values()) that do not contain copies of the keys, values or items, but rather reference the underlying dict and pull their values out of the dict as needed.
|
# ? Jul 23, 2013 19:38 |
|
I should have remembered the distinction between views and iterators because I literally used the set operations to find the intersection of two dicts yesterday. Thank you for the excellent link as usual.
|
# ? Jul 23, 2013 19:56 |
|
2.7 also has viewkeys() etc. for if you want the python 3 semantics.
|
# ? Jul 23, 2013 22:31 |
|
I'm working on a Curses-based program, which is my first GUI-based Python application. There are elements on the screen that I want to update in real-time (by means of a function that updates the .addstr()'s and .refresh()'es the screen/window). The problem is, I want to be able to update in real-time, but still have the option of key entry (using [screen].getch() ). When I do that, obviously it stops at the .getch(), and I have to press a key in order to refresh the info on the screen. How can I get around that? edit-- this is my main loop as it stands. I don't have hardly any functionality built in yet as I want to get over this one hurdle first. Python code:
Count Thrashula fucked around with this message at 22:42 on Jul 23, 2013 |
# ? Jul 23, 2013 22:38 |
|
Figure I'll ask in here to see if this is possible in Python. Is there anything that will allow me to make a really simple tray icon + menu for a small python script on osx?
|
# ? Jul 24, 2013 02:15 |
|
The March Hare posted:Is there anything that will allow me to make a really simple tray icon + menu for a small python script on osx? Yes. In wxPython 2.9.2 or higher, there's a class called TaskBarIcon. More: http://wxpython.org/docs/api/wx.TaskBarIcon-class.html
|
# ? Jul 24, 2013 04:37 |
|
QPZIL posted:I'm working on a Curses-based program, which is my first GUI-based Python application.
|
# ? Jul 24, 2013 10:10 |
|
How bad/good/incomprehensible is this, as a practice?Python code:
(NB. The collections output by the functions are without duplicates irrespective of whether sets are involved at any point)
|
# ? Jul 24, 2013 12:29 |
|
Hammerite posted:How bad/good/incomprehensible is this, as a practice? Why is list(set(thing)) silly? It is simple, short, and only applies in the one place where a list is needed. By contrast this solution mucks up the interface to a function with parameters that have nothing to with the function or its usual use. And worse now you are doing set(list(thing)) all the time, presumably making your common case slower. If you were trying to do this for performance reasons to avoid the copy, you've made it worse. Better to create a set, and only convert it to a list when you need a list if that is not the common case. Or else start off by creating an empty return_type at the start and filling it up instead of doing wholesale conversions, or better just have different functions that return different things. That's actually my biggest objection: that the return type can vary. This isn't as terrible as the awful "return a collection except if there is only one thing return the thing" transgression because it's explicitly switching on a parameter and can be documented, but I would not personally write code like this normally.
|
# ? Jul 24, 2013 13:27 |
|
Hammerite posted:How bad/good/incomprehensible is this, as a practice? My preference would be to see if you can turn do_a_thing into a generator. Then your code or user code can easily make lists or sets or just iterate over the things.
|
# ? Jul 24, 2013 17:30 |
|
edit: Here's the lesson kids - never forget to .grid() your widgets!
FoiledAgain fucked around with this message at 22:03 on Jul 24, 2013 |
# ? Jul 24, 2013 21:32 |
|
General advice in the thread has been to abandon TKinter and go with wxPython or PyQt. I'm not really familiar with TKinter or how it would interact with queues, so I don't have any more suggestions
|
# ? Jul 24, 2013 21:44 |
|
QuarkJets posted:General advice in the thread has been to abandon TKinter and go with wxPython or PyQt. I'm not really familiar with TKinter or how it would interact with queues, so I don't have any more suggestions The problem I don't think is necessarily related to Tkinter. Mainly my question was "how do I get the value of a variable from a process that is running". If I can just get the value of .current_generation, I'm sure I can find a way to feed it properly into Tkinter. edit: ugh, this is embarrassing. The whole problem was that I didn't grid the frame that the progress bar was in. Wow... Thanks for reading! FoiledAgain fucked around with this message at 22:02 on Jul 24, 2013 |
# ? Jul 24, 2013 21:55 |
|
QuarkJets posted:General advice in the thread has been to abandon TKinter and go with wxPython or PyQt. I'm not really familiar with TKinter or how it would interact with queues, so I don't have any more suggestions I don't use any of them very often, but I think PySide is supposed to be better in some sort of way than PyQt...
|
# ? Jul 24, 2013 22:13 |
|
|
# ? May 24, 2024 02:24 |
|
The main problem with PyQt is the GPL. PySide is still on 4.8 while PyQt has support for QT5. Both support Python3.
Posting Principle fucked around with this message at 22:50 on Jul 24, 2013 |
# ? Jul 24, 2013 22:45 |