|
Captain von Trapp posted:What's the Pythonic way to do this? At first I tried: You can use sorted which creates a generator that yields elements in sorted order, so your one liner becomes: code:
|
# ? Aug 12, 2012 21:39 |
|
|
# ? May 9, 2024 16:46 |
|
Thanks both of you, that works beautifully.
|
# ? Aug 12, 2012 21:41 |
|
Suspicious Dish posted:That would make bar(None) behave wrong. I'm not sure what Bodhi wants, though. How so? Seems like bar(None) would behave just like foo(None) would.
|
# ? Aug 12, 2012 22:08 |
|
leterip posted:How so? Seems like bar(None) would behave just like foo(None) would. bar uses None as default argument, which is supposed to correspond to calling foo with a defaulted argument. But foo doesn't use None for that, so now semantics have changed.
|
# ? Aug 12, 2012 22:41 |
|
PiotrLegnica posted:bar uses None as default argument, which is supposed to correspond to calling foo with a defaulted argument. But foo doesn't use None for that, so now semantics have changed.
|
# ? Aug 12, 2012 22:56 |
|
leterip posted:Uh, in the code he was responding to, bar was defined as def bar(*args, **kwargs). I don't see a default argument there. That code is a response to this: Bodhi Tea posted:Say I have a function with an optional argument which I don't know the default value of:
|
# ? Aug 12, 2012 23:00 |
|
PiotrLegnica posted:That code is a response to this: Yeah but that code was a completely different definition. I guess I just got confused because I thought the code he quoted didn't have the problem he was describing.
|
# ? Aug 12, 2012 23:25 |
|
I don't see the point inPython code:
As far as I understood, the point of: Python code:
I'm not sure why Bodhi wants this, and if he gives his overall goal, I'm sure we'll find a more elegant solution. XY problem and all that.
|
# ? Aug 13, 2012 00:20 |
|
Suspicious Dish posted:I don't see the point in Yup I think you got my intention here. I'm using the Twitter api with Twython. There is a "max_id" argument that you can pass to getHomeTimeline() in order to properly parse a Twitter timeline. However, this argument should not be passed on the first call. code:
I'm using Flask and I know I'm probably doing a million things wrong, but one thing at a time and all that
|
# ? Aug 13, 2012 01:13 |
|
Bodhi Tea posted:Yup I think you got my intention here. I'm using the Twitter api with Twython. There is a "max_id" argument that you can pass to getHomeTimeline() in order to properly parse a Twitter timeline. However, this argument should not be passed on the first call. This should work: code:
Scaevolus fucked around with this message at 01:20 on Aug 13, 2012 |
# ? Aug 13, 2012 01:18 |
|
Scaevolus posted:This should work: I thought of that, but as best I can tell, when I pass max_id=None, no tweets are returned. I just double checked by replacing my code with yours and it seems tweetq is empty.
|
# ? Aug 13, 2012 01:28 |
|
Python code:
|
# ? Aug 13, 2012 01:38 |
|
Suspicious Dish posted:
That works, thanks!
|
# ? Aug 13, 2012 02:44 |
|
As a long-time (and happy) C and Objective-C programmer, I just recently had to learn Python so I could use the panda3d engine. I will give Python this: it isn't C++ (which is quite possibly the worst language ever designed), but Python does some really stupid stuff that I need help figuring out. Tabs: Spaces are for weirdos who like to wear out their space and delete keys particularly quickly, but Python seems to choke on tabs used as the leading whitespace. Is there a solution for this, or will I just have to deal? Empty Methods: The first thing I do when I create a new class in Objective-C is to design the API and put in all the methods, so that I have a shell that can be later fleshed out. This allows me to design other parts of the code that utilizes the class before fully fleshing it out. Python has a fit over empty methods though; is there a solution other than throwing in garbage code just to appease the parser? Serialization: I realize that Python has the pickling functionality, but it's inherently unsafe and apparently does way more than I actually want it to (just save the values of the instance variables; I don't care about restoring the object itself). In the end I've resorted to writing my own serialization code utilizing the struct class--unfortunately the lack of fine-grained data control and the beauty that are void pointers actually makes this much more complicated than the equivalent C code. I realize that some of this is probably because I'm coming from a C background, and that the overwhelming response will be "well that's just not how we do it in Python", but despite being an overall useful and nice language, it makes some really poor design decisions (the only other language I actively use that cares anything about white-space is Fortran 77, which should tell you something about the decision to use it in Python).
|
# ? Aug 13, 2012 07:43 |
|
1. Python only cares if you mix tabs and spaces. If you don't want to force yourself to adhere to one or the other, probably use a text editor that can autoreplace tabs to spaces. I mostly use Python on Windows, and I have Notepad++ set up to automatically type 4 spaces whenever I press tab 2. Use pass: Python code:
|
# ? Aug 13, 2012 08:04 |
|
MeramJert posted:1. Python only cares if you mix tabs and spaces. If you don't want to force yourself to adhere to one or the other, probably use a text editor that can autoreplace tabs to spaces. I mostly use Python on Windows, and I have Notepad++ set up to automatically type 4 spaces whenever I press tab Thanks for the help; that makes at least the first two problems a littler simpler.
|
# ? Aug 13, 2012 08:18 |
|
Not to get in a holy war, but your rationale for effective whitespace as bad design is somewhat silly. It produces very readable code, making it very easy to discern the structure and flow, whether you wrote it or not. Just because it isn't the norm doesn't make it inherently bad, and using tools such as the standard library itertools, list comprehensions and the like will remove the need for heavily nested structures.
|
# ? Aug 13, 2012 08:44 |
|
Maluco Marinero posted:Not to get in a holy war, but your rationale for effective whitespace as bad design is somewhat silly. It produces very readable code, making it very easy to discern the structure and flow, whether you wrote it or not. Actually I'll say that neither C nor Python is right in this situation; braces should be optional but completely valid code structures. Reliance on whitespace to properly parse code, by necessity limits your options on code style. Worst of all, I've never seen a white-space related error thrown that actually relates it to white-space at all, leading to sometimes ridiculous amounts of time spent trying to track a bug that shouldn't be a bug to begin with. In short, I agree with you: whitespace can be a very effective tool to structure code and define flow, but a fundamental reliance on it to function is ridiculous.
|
# ? Aug 13, 2012 09:09 |
|
The King of Swag posted:Worst of all, I've never seen a white-space related error thrown that actually relates it to white-space at all, leading to sometimes ridiculous amounts of time spent trying to track a bug that shouldn't be a bug to begin with. What do you mean by this? Python code:
Python code:
code:
|
# ? Aug 13, 2012 09:59 |
|
I am admittedly no expert but...The King of Swag posted:Reliance on whitespace to properly parse code, by necessity limits your options on code style. quote:Worst of all, I've never seen a white-space related error thrown that actually relates it to white-space at all, leading to sometimes ridiculous amounts of time spent trying to track a bug that shouldn't be a bug to begin with. quote:In short, I agree with you: whitespace can be a very effective tool to structure code and define flow, but a fundamental reliance on it to function is ridiculous.
|
# ? Aug 13, 2012 10:04 |
|
Maluco Marinero posted:Is entirely the point in the design of Python, as is the introduction of PEP8. You can always backslash a line end to carry a statement over to the next line if you need to as well. This is true: Python code:
|
# ? Aug 13, 2012 10:12 |
|
The King of Swag posted:Serialization: I realize that Python has the pickling functionality, but it's inherently unsafe and apparently does way more than I actually want it to (just save the values of the instance variables; I don't care about restoring the object itself). In the end I've resorted to writing my own serialization code utilizing the struct class--unfortunately the lack of fine-grained data control and the beauty that are void pointers actually makes this much more complicated than the equivalent C code. This should do the trick. Python code:
|
# ? Aug 13, 2012 11:21 |
|
Python will always expand tab characters to 8 spaces, because tab characters are always 8 spaces (if you set your editor to anything else source will look funny) That competes with PEP8's style of 4 spaces. Just configure your editor to emit 4 spaces when you hit the tab key. Every programmer editor can do it.
|
# ? Aug 13, 2012 15:45 |
|
Also echoing the make tab = 4 spaces method in your editor. It makes life a million times easier.
|
# ? Aug 13, 2012 17:36 |
|
My personal favourite is tab = 2 spaces, but I may go up to 4; I'm used to coding with Perl, where curlies are rife, and code blocks can be huge, so indentation with 2 spaces isn't too bad, but 4 spaces with Python would probably be better to maintain code readability.
|
# ? Aug 13, 2012 18:49 |
|
I think the only time I've ever seen Python that wasn't 4-space indented was in terrible ActiveState code snippets.
|
# ? Aug 13, 2012 19:10 |
|
evilentity posted:This should do the trick. When I was looking around for a good explanation on why pickle is insecure, I found a module called Cerealizer, which was written as a safe replacement. I figured a link to it might be useful here.
|
# ? Aug 13, 2012 19:14 |
|
MeramJert posted:Indentation Error examples I just tried this and I only get the carat; no notice about the indentation error. What it looks like may be happening is the output window in Komodo is dropping the last line or so of output, as I've noticed some other errors have strangely abrupt descriptions. Thanks for pointing it out though, I would have never known otherwise. evilentity posted:This should do the trick. I've seen that before and the problem is that I need to save my data out as binary and be able to easily read it all back. Using that trick, saving would be easy but loading would require a lot of parsing. My current method of an explicit file format with run lengths embedded for blocks of data is a lot more succinct with no need for parsing of data beyond reading the length of a block, loading that block into memory and reading the next. Although I'm all ears if there is an easy Pythonesque way to load your example. Suspicious Dish posted:Python will always expand tab characters to 8 spaces, because tab characters are always 8 spaces Since when? Tab on every computer I've ever used has been the equivalent of 4 spaces.
|
# ? Aug 13, 2012 22:50 |
|
The King of Swag posted:I've seen that before and the problem is that I need to save my data out as binary and be able to easily read it all back. Using that trick, saving would be easy but loading would require a lot of parsing. My current method of an explicit file format with run lengths embedded for blocks of data is a lot more succinct with no need for parsing of data beyond reading the length of a block, loading that block into memory and reading the next. Although I'm all ears if there is an easy Pythonesque way to load your example. Parsing bad? Json to the rescue! Python code:
|
# ? Aug 14, 2012 00:12 |
|
Please don't use vars like this. It will break in the unintentional case that you get cyclic values or anything like this. The correct thing to do is to define a list of variables that can be exported/imported as JSON, or to define a toJSON method that returns something that can be validly passed to json.dumps.
|
# ? Aug 14, 2012 00:20 |
|
The King of Swag posted:Serialization: I realize that Python has the pickling functionality, but it's inherently unsafe and apparently does way more than I actually want it to (just save the values of the instance variables; I don't care about restoring the object itself). In the end I've resorted to writing my own serialization code utilizing the struct class--unfortunately the lack of fine-grained data control and the beauty that are void pointers actually makes this much more complicated than the equivalent C code. A labmate of mine uses Panda3D extensively, and wrote some sort of methods for his classes that make them pickle in a way that he specifies. I know it's pretty useless of me to say "hey you can make your classes behave how you want with respect to pickling" without saying how, but at least you know it's possible so you can look up how? edit: looks like you want to write your own __getstate__() and __setstate__(), though I have no idea what side-effects this might have Emacs Headroom fucked around with this message at 02:11 on Aug 14, 2012 |
# ? Aug 14, 2012 02:07 |
|
Or you could use Protobuf.
|
# ? Aug 14, 2012 02:11 |
|
Emacs Headroom posted:A labmate of mine uses Panda3D extensively, and wrote some sort of methods for his classes that make them pickle in a way that he specifies. Pickle breaks when you sneeze on it. I'd highly suggest having a better serialization method.
|
# ? Aug 14, 2012 02:24 |
|
The King of Swag posted:I've seen that before and the problem is that I need to save my data out as binary and be able to easily read it all back. Using that trick, saving would be easy but loading would require a lot of parsing. My current method of an explicit file format with run lengths embedded for blocks of data is a lot more succinct with no need for parsing of data beyond reading the length of a block, loading that block into memory and reading the next. Although I'm all ears if there is an easy Pythonesque way to load your example. Firstlies, have you tried cjson for your dataset? I'm not saying fo-shizie it will be fast enough because there are indeed some very large datasets out there but unless your dataset is remarkably large cjson is very likely to be fast 'nuff. If not, you definitely want protobuffs. Unless your dataset is extremely specialized as well as large you are extremely unlikely to come up with something better loving around struct.pack. Anyway, json is amazing and I love it to pieces - it just really hits the sweet spot in terms of what a serialization format should do.
|
# ? Aug 14, 2012 02:36 |
|
Mr. Wynand posted:Anyway, json is amazing and I love it to pieces - it just really hits the sweet spot in terms of what a serialization format should do. Just as long as you use unicode within the BOM, floating point, and no binary data, integers, or dates.
|
# ? Aug 14, 2012 12:40 |
|
within the BOM? did you mean the BMP?
|
# ? Aug 14, 2012 17:27 |
|
tef posted:Just as long as you use unicode within the BOM, floating point, and no binary data, integers, or dates. UTF-8 until death base64 for occasional binary derp are numbers not good enough for you dates are just strings with app specific meaning! :p Ok of course you shouldn't actually use json for literally everything. As I said it's just a "sweet spot" for a huge number of application, primarily because it supports what are probably the most useful data structures across just about any language or platform while still remaining really really really simple. (TBH, I agree with you about dates. Fancy date formats can get complicated sure, but we could probably just have picked one and stuck with it. I'd nominate RFC3339 since that's what <time datetime> uses, and json is all web-y and poo poo anyway. OTOH I can also empathize with the choice against it since now you need a way to delimit string representations of non-string objects and then people will be tempted to allow arbitrary class markers and suddenly we are in YAML clusterfuckland)
|
# ? Aug 14, 2012 19:12 |
|
tef posted:Just as long as you use unicode within the BOM, floating point, and no binary data, integers, or dates. Why not integers?
|
# ? Aug 14, 2012 20:13 |
|
Suspicious Dish posted:within the BOM? Yes. I'm losing my mind
|
# ? Aug 14, 2012 21:42 |
|
|
# ? May 9, 2024 16:46 |
|
accipter posted:Why not integers? javascript/json only has double precision floating point
|
# ? Aug 14, 2012 21:51 |