Re: Comparison with False - something I don't understand
Howdy! When I run pychecker through my modules I get the message that comparisons with False is not necessary and that it might yield unexpected results. Comparisons against False -are- dangerous, demonstrated below. Yet in some situations I need to specifically check whether False was returned or None was returned. Why is comparison with False so bad? (False == 0) is True (True == 1) is True The bool type is a subclass of int! (Run those lines in a Python interpreter to see. ;) if var == False: if var is False: … So how do you get around this? My functions return False and None under different circumstances. Should I raise exceptions instead? I feel it's unnecessary clutter to use exceptions unless absolutely no other solution is available and yet I have doubts about the False value. If you want to check not just for value equivelance (False == 0) but literal type, use the is comparator. is checks, and others correct me if I'm wrong, the literal memory address of an object against another. E.g. False, being a singleton, will always have the same memory address. (This is true of CPython, possibly not of Python implementations like Jython or IronPython.) Using is will be effective for checking for literal None as well. When ever I need to test for None, I always use the is comparator. It's also more English-like. (None, evaluating to False when using '==', is useful when all you care about is having a blank default value, for example.) — Alice. -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorate un Frame with window managers title bar, etc en Tkinter 8.5
In article mailman.101.1291218554.2649.python-l...@python.org, craf p...@vtr.net wrote: Hi. I use python 3.1 and Tkinter 8.5 in Ubuntu 9.10 I would like to turn a frame into a toolbox, ,and for that I read that you can use the command wm manage (window) The information can be found at: http://www.tcl.tk/man/tcl8.5/TkCmd/wm.htm#M39 the explanation says: wm manage widget: The widget specified will become a stand alone top-level window. The window will be decorated with the window managers title bar, etc. Only frame, labelframe and toplevel widgets can be used with this command. Attempting to pass any other widget type will raise an error. Attempting to manage a toplevel widget is benign and achieves nothing. See also GEOMETRY MANAGEMENT. I have tried to use it in Tkinter but I can not know how is its structure. In Tkinter should be: ---TEST CODE--- from Tkinter import master = Tk() frame = Frame(master) wm_manager(Frame) master.mainloop() But this does not work. If your version of Tkinter supports it, then the correct syntax is: frame.wm_manage() Please note you have to call it on the Frame instance (the one you named frame), and not on Frame with a big F which is the class. If it says the method doesn't exist (AttributeError raised on the line frame.wm_manage()), you can also try to do it at tcl/tk level with the line: master.tk.call('wm', 'manage', frame) I appreciate any of this item HTH - Eric - -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 07:28:30 +, Harishankar wrote: When I run pychecker through my modules I get the message that comparisons with False is not necessary and that it might yield unexpected results. Yet in some situations I need to specifically check whether False was returned or None was returned. Why is comparison with False so bad? The behaviour may be counterintuitive. One might expect that x == False is equivalent to not x. Sometimes it is, sometimes it isn't. E.g. 0 and 0.0 are equal to False and are equivalent to False when converted to booleans: 0 == False True not 0 True 0.0 == False True not 0.0 True [], and None aren't equal to False but are equivalent to False when converted to booleans: [] == False False not [] True == False False not True None == False False not None True The boolean conversions are what's relevant for if x ..., while x ..., etc. If you want to test specifically for True, False or None, use is rather than an equality check. This eliminates the warning and doesn't risk misleading someone reading the code. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 00:15:42 -0800, Alice Bevan–McGregor wrote: Howdy! Good day to you! (False == 0) is True (True == 1) is True I see. Thanks for this. I suspected this, but wasn't sure. The bool type is a subclass of int! (Run those lines in a Python interpreter to see. ;) if var == False: if var is False: … So var is False is safer to use when I want to specifically check whether var is set to False and not 0 or None? If you want to check not just for value equivelance (False == 0) but literal type, use the is comparator. is checks, and others correct me if I'm wrong, the literal memory address of an object against another. E.g. False, being a singleton, will always have the same memory address. (This is true of CPython, possibly not of Python implementations like Jython or IronPython.) Using is will be effective for checking for literal None as well. Thanks, it makes sense to me now. Literal equivalence is what I was looking for. I didn't quite understand whether == achieved this or not. Now I guess I know. When ever I need to test for None, I always use the is comparator. It's also more English-like. (None, evaluating to False when using '==', is useful when all you care about is having a blank default value, for example.) Yes, but in my function I don't want to confuse False with 0 or anything else except False. Thanks again for explaining this clearly. -- Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 09:58:18 +, Nobody wrote: If you want to test specifically for True, False or None, use is rather than an equality check. This eliminates the warning and doesn't risk misleading someone reading the code. Thanks so much for this very specific answer. I guess is is what I am looking for. :-) -- Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 12/2/10 2:02 AM, Harishankar wrote: On Thu, 02 Dec 2010 00:15:42 -0800, Alice Bevan–McGregor wrote: The bool type is a subclass of int! (Run those lines in a Python interpreter to see. ;) if var == False: if var is False: … So var is False is safer to use when I want to specifically check whether var is set to False and not 0 or None? Equality is a somewhat fuzzy concept. By convention and habit, its usually fine and clear: but its still fuzzy and up to each individual object involved to answer the question of equality. Now, its generally suggested in Python to do fairly vague truth testing: if x and if not x as opposed to concepts like if x == True and if x == False because the former is broad but usually more correct, and the latter can lead to some unexpected semantics. But that doesn't mean you must never check if something is False or True: there are times when you really do want or need to see if _False_ is what's being returned, or _True_, or _None_. In this case, use is, yes, indeed. The is operator checks absolute object identity, and so is how you should do that check in cases where you want to test the distinction between Is it /False/, or just something false-ish or not-true or nothing-ish? Generally speaking in Python, you usually want to do tests as if x or if not x. But sometimes you need to know if x is a specific singleton value: True, False or None. In that case, its okay to do if x is True, if x is False or if x is None. But you should only do that after the simple test is deemed inappropriate in your API or situation. And-- here's the rub-- while is is absolutely OK and right for you to use to test if an object is one of those singletons, its *probably* NOT what you want to do in any other situation*. Outside of the True/False/None singletons, and places where you're doing some special OOP-stuff, you almost certainly don't want to use 'is', but use equality checking (even if its fuzzy, because its fuzzy) instead. This demonstrates why is should be avoided except when in those singleton situations (except when you need to, of course): a = 2 b = 2 a is b True a == b True a = 2 b = 2 a is b False a == b True (If you're wondering why that's happening: Python makes very little in the way of promises with regard to object identity. It may choose to make a whole new int object of value 2 every time you type 2, or use the same old int object each time: sure, presently it tends to only share small integers for re-use, but that's not a promise, not a documented feature, but a function of the current implementation. It could happen tomorrow, in theory, that where a = 1; b = 1; become the same object as far as is is concerned even though today they are different... is should only be used in situations where you care about absolute object identity, not *value*.) -- Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/ * P.S. I'm not saying its never right to use is outside of The Singletons. Just that its probably not, for most people, what they actually should do in most code. There are numerous counter-examples, of course. Its just a general guideline to follow. Until a need arises that demonstrates otherwise. signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
How to send an IP packet in Python?
Hello. I have sniffed some packet and now I would like to send it with the help of python. It's some simple IGMP packet with VLAN tag. (01 00 5E 00 43 67 00 02 B3 C8 7F 44 81 00 00 DE 08 00 46 00 00 20 00 01 00 00 01 02 36 4C C0 A8 00 7B EA 00 43 67 94 04 00 00 16 00 BC 97 EA 00 43 67) At first I wrote that: dst = '234.0.67.103' # Open a raw socket. s = socket.socket(socket.AF_INET, socket.SOCK_RAW,2) res='' temp='01 00 5E 00 43 67 00 02 B3 C8 7F 44 81 00 00 DE 08 00 46 00 00 20 00 01 00 00 01 02 36 4C C0 A8 00 7B EA 00 43 67 94 04 00 00 16 00 BC 97 EA 00 43 67' for i in temp.split(' '): res+=chr(int(i, 16)) print res s.sendto(res, (dst, 0)) But for some reason python send that: 0x 01 00 5E 00 43 67 00 02-B3 C8 7F 44 08 00 45 00 ..^.Cg..іИD..E. 0x0010 00 46 07 06 00 00 01 02-C4 25 C0 A8 00 7B EA 00 .F..Д %АЁ.{к. 0x0020 43 67 01 00 5E 00 43 67-00 02 B3 C8 7F 44 81 00 Cg..^.Cg..іИDЃ. 0x0030 00 DE 08 00 46 00 00 20-00 01 00 00 01 02 36 4C .Ю..F.. ..6L 0x0040 C0 A8 00 7B EA 00 43 67-94 04 00 00 16 00 BC 97 АЁ. {к.Cg”.ј— 0x0050 EA 00 43 67 к.Cg As you can see, python ignores my headers and creates its own. I discussed that problem on stackoverflow.com and one user told me to try that: import socket s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, 0x8100) s.bind(('eth0', 0x8100)) res='' temp='01 00 5E 00 43 67 00 02 B3 C8 7F 44 81 00 00 DE 08 00 46 00 00 20 00 01 00 00 01 02 36 4C C0 A8 00 7B EA 00 43 67 94 04 00 00 16 00 BC 97 EA 00 43 67' for i in temp.split(' '): res+=chr(int(i, 16)) s.send(res) But under Windows, AF_PACKET is said to be undefined. =( I tried to replace it with AF_INET, but now python tells me that that protocol (0x8100) is not supported. 0x8100 is IEEE 802.1Q and I just don't understand, why it isn't supported. I can generate packet with VLAN tag inside and send it with the help of pierf, for example. In fact, I can generate and send the packet from my example with pierf and it runs just fine. =) I've found a topic with the same problem (http:// www.computing.net/answers/programming/python-windows-sockets/11884.html) So, is there any way to somehow send a packet with ready headers and data under Windows with the help of python? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 3 encoding question: Read a filename from stdin, subsequently open that filename
Nobody wrote: This was actually a critical flaw in Python 3.0, as it meant that filenames which weren't valid in the locale's encoding simply couldn't be passed via argv or environ. 3.1 fixed this using the surrogateescape encoding, so now it's only an annoyance (i.e. you can recover the original bytes once you've spent enough time digging through the documentation). Is it just that you need to harden your scripts against these byte sequences or do you actually encounter them? If the latter, can you give some examples? -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
Harishankar v.harishan...@gmail.com writes: When I run pychecker through my modules I get the message that comparisons with False is not necessary and that it might yield unexpected results. Good advice. Yet in some situations I need to specifically check whether False was returned or None was returned. Why is comparison with False so bad? Because it's almost always unnecessary and can yield unexpected results :-) In other words, the only purpose of the ‘bool’ type is to have values explicitly designed for testing in Boolean conditions. Comparing them to the literals is a code smell — indicating a poor design. # example code which matches both False and None if not var: # do something Can you give a specific real-world example of why this is not good enough? It's likely we can suggest better ways of achieving the broader purpose. So how do you get around this? My functions return False and None under different circumstances. Usually it is None that will be the special case, so it's usually better to write something like:: result = foo() if result is None: # do special things But that's just one possibility, and might not apply in your use case. Should I raise exceptions instead? I feel it's unnecessary clutter to use exceptions unless absolutely no other solution is available and yet I have doubts about the False value. More details of the problem you're trying to solve would help with giving specific advice. -- \ “I was sad because I had no shoes, until I met a man who had no | `\ feet. So I said, ‘Got any shoes you're not using?’” —Steven | _o__) Wright | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
[Fwd: Re: Decorate un Frame with window managers title bar, etc en Tkinter 8.5]
- Mensaje reenviado De: Eric Brunel eric.bru...@pragmadev.nospam.com Para: python-list@python.org Asunto: Re: Decorate un Frame with window managers title bar, etc en Tkinter 8.5 Fecha: Thu, 02 Dec 2010 10:21:49 +0100 Grupos de noticias: comp.lang.python In article mailman.101.1291218554.2649.python-l...@python.org, craf p...@vtr.net wrote: Hi. I use python 3.1 and Tkinter 8.5 in Ubuntu 9.10 I would like to turn a frame into a toolbox, ,and for that I read that you can use the command wm manage (window) The information can be found at: http://www.tcl.tk/man/tcl8.5/TkCmd/wm.htm#M39 the explanation says: wm manage widget: The widget specified will become a stand alone top-level window. The window will be decorated with the window managers title bar, etc. Only frame, labelframe and toplevel widgets can be used with this command. Attempting to pass any other widget type will raise an error. Attempting to manage a toplevel widget is benign and achieves nothing. See also GEOMETRY MANAGEMENT. I have tried to use it in Tkinter but I can not know how is its structure. In Tkinter should be: ---TEST CODE--- from Tkinter import master = Tk() frame = Frame(master) wm_manager(Frame) master.mainloop() But this does not work. If your version of Tkinter supports it, then the correct syntax is: frame.wm_manage() Please note you have to call it on the Frame instance (the one you named frame), and not on Frame with a big F which is the class. If it says the method doesn't exist (AttributeError raised on the line frame.wm_manage()), you can also try to do it at tcl/tk level with the line: master.tk.call('wm', 'manage', frame) I appreciate any of this item HTH - Eric - Thank you very much Eric! Regards. Cristian. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 22:19:25 +1100, Ben Finney wrote: More details of the problem you're trying to solve would help with giving specific advice. I'm writing functions with multiple points of failure exits. I use return False as a way to flag the error condition rather than raising exceptions. But under certain circumstances, the function can also return empty lists which equate to false when using the condition like: # myfunction () can return a list of tuples, but can also return an empty # list under certain conditions since it's query a database and the result # can be empty also result = myfunction (vars) if not result: # error condition Now above I first realized that the function can also return an empty list under some conditions and so changed it to if result == False: # error condition But now I realize that it's better to use is if result is False: # error condition That is how my problem arose. - Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 02:49:50 -0800, Stephen Hansen wrote: ... ... ... * P.S. I'm not saying its never right to use is outside of The Singletons. Just that its probably not, for most people, what they actually should do in most code. There are numerous counter-examples, of course. Its just a general guideline to follow. Until a need arises that demonstrates otherwise. Here I'm using it to compare the result of a function where I specifically return False on error condition, so I think it's better I check it against the literal False rather than the fuzzy False produced by the boolean operation. I wouldn't do this in most situations though, but I did need to distinguish between the the empty list and False and using the broader form as in if not a did not work as expected. -- Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 12/02/2010 08:18 AM, Harishankar wrote: Here I'm using it to compare the result of a function where I specifically return False on error condition, This sounds exactly like the reason to use exceptions...you have an exceptional error condition. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: How to initialize each multithreading Pool worker with an individual value?
On Dec 1, 3:24 am, James Mills prolo...@shortcircuit.net.au wrote: I assume you are talking about multiprocessing despite you mentioning multithreading in the mix. yes, sorry. Have a look at the source code for multiprocessing.pool and how the Pool object works and what it does with the initializer argument. I'm not entirely sure it does what you expect and yes documentation on this is lacking... I see I found my way to seed each member of Pool with own data. I do it right after after initialization: port = None def port_seeder(port_val) from time import sleep sleep(1) # or less... global port port = port_val if __name__ == '__main__': pool = Pool(3) pool.map(port_seeder, range(3), chunksize=1) # now child processes are initialized with individual values. Another (a bit more heavier) approach would be via shared resource. P.S. sorry, I found your answer only now. reagrds -- Valery -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I define class methods outside of the class?
On Dec 1, 10:47 pm, James Mills prolo...@shortcircuit.net.au wrote: On Thu, Dec 2, 2010 at 3:36 PM, Jeremy jlcon...@gmail.com wrote: I have some methods that I need (would like) to define outside of the class. I know this can be done by defining the function and then setting it equal to some member of an instance of the class. But, because of the complexity of what I'm doing (I have to set many functions as class methods) I would rather not do this. Can someone show me how to do this? Is it even possible? Can decorators be used here? Do you mean something like this ? @classmethod def foo(cls): print I am the foo classmethod on %r % cls class Foo(object): pass Foo.foo = foo cheers James Thanks, James. That is almost exactly what I want. However, I want to avoid doing Foo.foo = foo Is this going to be possible? I'm trying to understand how decorators are used. Are they really necessary in this example? Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 08:44:11 -0600, Tim Chase wrote: On 12/02/2010 08:18 AM, Harishankar wrote: Here I'm using it to compare the result of a function where I specifically return False on error condition, This sounds exactly like the reason to use exceptions...you have an exceptional error condition. -tkc There are some reasons why I hate exceptions but that is a different topic. However, in short I can say that personally: 1. I hate try blocks which add complexity to the code when none is needed. Try blocks make code much more unreadable in my view and I use it only for the built-in exceptions when absolutely needed. 2. I prefer the less irksome True or False to do error checking. Exceptions seem too heavyweight for simple problems. 3. Philosophically I think exception handling is the wrong approach to error management. I have never grown up programming with exceptions in C and I couldn't pick up the habit with python either. Did I mention that I detest try blocks? try blocks seem ugly and destroy code clarity at least in my view. And enclosing single statements under separate try blocks seem to add a lot of clutter. -- Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 12/2/2010 9:13 AM, Harishankar wrote: On Thu, 02 Dec 2010 22:19:25 +1100, Ben Finney wrote: More details of the problem you're trying to solve would help with giving specific advice. I'm writing functions with multiple points of failure exits. I use return False as a way to flag the error condition rather than raising exceptions. But under certain circumstances, the function can also return empty lists which equate to false when using the condition like: # myfunction () can return a list of tuples, but can also return an empty # list under certain conditions since it's query a database and the result # can be empty also result = myfunction (vars) if not result: # error condition Now above I first realized that the function can also return an empty list under some conditions and so changed it to if result == False: # error condition But now I realize that it's better to use is if result is False: # error condition That is how my problem arose. Did you think about using exceptions to handle exceptional conditions? If you are new to Python it may not be the obvious soltuion, but it can greatly simplify program logic. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon 2011 Atlanta March 9-17 http://us.pycon.org/ See Python Video! http://python.mirocommunity.org/ Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 12/2/2010 9:56 AM, Harishankar wrote: 3. Philosophically I think exception handling is the wrong approach to error management. I have never grown up programming with exceptions in C and I couldn't pick up the habit with python either. Did I mention that I detest try blocks? try blocks seem ugly and destroy code clarity at least in my view. And enclosing single statements under separate try blocks seem to add a lot of clutter. Whereas lots of nested if statements to test that multiple errors have all not occurred is a model of clarity? This sounds to me like a prejudice that will harm your Python development. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon 2011 Atlanta March 9-17 http://us.pycon.org/ See Python Video! http://python.mirocommunity.org/ Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, Harishankar v.harishan...@gmail.com wrote: There are some reasons why I hate exceptions but that is a different topic. However, in short I can say that personally: 1. I hate try blocks which add complexity to the code when none is needed. Try blocks make code much more unreadable in my view and I use it only for the built-in exceptions when absolutely needed. Actually, exceptions remove a ton of complexity and almost universally remove a ton of redundant error checking code. Second, they aleviate a ton of design complexity about designing a clean and unified method of error handling throughout the program. Using exceptions, the only real questions are where to handle various errors. It is a godsend for having to clean up intermediary results when an error occurs as part of a complex set of dependant sequential operations. 2. I prefer the less irksome True or False to do error checking. Exceptions seem too heavyweight for simple problems. Error handling in C huge source of bugs. First, error handling code is scattered throughout the codebase and second it is hard to test the error handling code for a number of failures. Being able to raise exceptions within your test code makes it much easier to write tests capable of detecting error handling bugs. 3. Philosophically I think exception handling is the wrong approach to error management. I have never grown up programming with exceptions in C and I couldn't pick up the habit with python either. Did I mention that I detest try blocks? try blocks seem ugly and destroy code clarity at least in my view. And enclosing single statements under separate try blocks seem to add a lot of clutter. Perhaps you should take a look at how Erlang appoaches exception handling. Being message passing and concurrency oriented, Erlang encourages ignoring error conditions within worker processes. Errors instead cause the worker processes to be killed and a supervisory process is notified, by message, so that it can handle the error and respawn the worker process. Since it doesn't use try/exept blocks, maybe that will be more to your liking. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 10:19:35 -0500, Steve Holden wrote: On 12/2/2010 9:13 AM, Harishankar wrote: On Thu, 02 Dec 2010 22:19:25 +1100, Ben Finney wrote: More details of the problem you're trying to solve would help with giving specific advice. I'm writing functions with multiple points of failure exits. I use return False as a way to flag the error condition rather than raising exceptions. But under certain circumstances, the function can also return empty lists which equate to false when using the condition like: # myfunction () can return a list of tuples, but can also return an empty # list under certain conditions since it's query a database and the result # can be empty also result = myfunction (vars) if not result: # error condition Now above I first realized that the function can also return an empty list under some conditions and so changed it to if result == False: # error condition But now I realize that it's better to use is if result is False: # error condition That is how my problem arose. Did you think about using exceptions to handle exceptional conditions? If you are new to Python it may not be the obvious soltuion, but it can greatly simplify program logic. regards Steve I am not new to Python but I am not a fan of exceptions either. I prefer to avoid writing my own exceptions because it feels too heavy and clunky for simple error checking. Most times I find simple error checking ample for my purposes. Of course, I use the built-in exception objects when I have no choice, but I hate try blocks. They add clunkiness to code and besides exception objects seem to be fairly heavy-duty for simple error conditions where a true/false flag would probably suffice. I am also wary of using larger catch-all try blocks or try blocks with multiple exception exits (which seem to make tracking subtle bugs harder). I prefer the philosophy of dealing with errors immediately as they arise, rather than delegate them to exception mechanism. Of course, I could wrap single statements in try blocks, but that makes the code even messier without any significant benefits. -- Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, Steve Holden st...@holdenweb.com wrote: On 12/2/2010 9:13 AM, Harishankar wrote: if not result: # error condition Now above I first realized that the function can also return an empty list under some conditions and so changed it to if result == False: # error condition But now I realize that it's better to use is if result is False: # error condition That is how my problem arose. Did you think about using exceptions to handle exceptional conditions? If you are new to Python it may not be the obvious soltuion, but it can greatly simplify program logic. If you're not used to using exceptions it may at first seem like they take extra effort to use. But usually, in the end, they end up being less work (and more importantly easier to read and less bugs). -- Grant Edwards grant.b.edwardsYow! He is the MELBA-BEING at ... the ANGEL CAKE gmail.com... XEROX him ... XEROX him -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 12/2/10 6:56 AM, Harishankar wrote: On Thu, 02 Dec 2010 08:44:11 -0600, Tim Chase wrote: On 12/02/2010 08:18 AM, Harishankar wrote: Here I'm using it to compare the result of a function where I specifically return False on error condition, This sounds exactly like the reason to use exceptions...you have an exceptional error condition. -tkc There are some reasons why I hate exceptions but that is a different topic. However, in short I can say that personally: To each his/her own, of course; but -- 3. Philosophically I think exception handling is the wrong approach to error management. Exceptions aren't about error management; they are about exceptional conditions: some are errors, others are entirely normal situations you know are going to happen (such as reaching the end of a sequence as you iterate over it: that's not an error, but it is special). To be philosophically opposed to them seems to me to be philosophically in favor of race conditions. I have never grown up programming with exceptions in C and I couldn't pick up the habit with python either. Did I mention that I detest try blocks? try blocks seem ugly and destroy code clarity at least in my view. And enclosing single statements under separate try blocks seem to add a lot of clutter. ? How do they destroy clarity or add clutter, since presumably you have to deal with that False in some way with logical structure -- which always does whitespace in Python. If not immediately, then up the call stack (which seems to imply you should just not use a try/except and let the exception unwind the stack to wherever in your code someone wants to deal with it). if is_correct(): result = do_thing() else: do_error_handling() try: result = do_thing() except KeyError: do_error_handling() And as an aside, the more statements one puts into a try/except block: and the more complicated a statement at that-- the more likely it is they are going to mess up and do something Wrong. Now, all that said: sure, in some situations I do prefer the check first style of programming where exceptions don't end up being used. Its not *wrong* to return False on an error condition: its going against the grain, though, and makes your code harder to deal with long-term if only because now there's two separate mechanisms that errors happen in it. You can't totally do away with exceptions in Python, even if you try very hard. So with that in mind, IMHO, the best approach is to just... get over it, and learn to appreciate them :) But, to each his or her own. :) -- Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/ signature.asc Description: OpenPGP digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 15:25:55 +, Tim Harig wrote: ... ... Perhaps you should take a look at how Erlang appoaches exception handling. Being message passing and concurrency oriented, Erlang encourages ignoring error conditions within worker processes. Errors instead cause the worker processes to be killed and a supervisory process is notified, by message, so that it can handle the error and respawn the worker process. Since it doesn't use try/exept blocks, maybe that will be more to your liking. Thanks for the reply. I understand that the error vs exception debate is quite a big one in the programming community as a whole and I don't consider myself very knowledgeable in these issues. However, I will try to approach this with an open mind and see whether I can work with exceptions comfortably in Python. I do understand both sides of the issue. Exceptions seem to be generally more reliable but I feel they add a lot of complexity particular when a lot of code is placed in a try block. -- Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, Harishankar v.harishan...@gmail.com wrote: I am also wary of using larger catch-all try blocks or try blocks with multiple exception exits (which seem to make tracking subtle bugs harder). I prefer the philosophy of dealing with errors immediately as If you are using exceptions to try to catch bug then you are using them improperly. Exceptions (with the exception (no pun intended) of AssertionError) are designed to catch error conditions, not bugs. harder). I prefer the philosophy of dealing with errors immediately as they arise, rather than delegate them to exception mechanism. Of course, I could wrap single statements in try blocks, but that makes the code even messier without any significant benefits. Actually, finer grained error handling commonly covers up bugs. If you want to find bugs, you want to make the program prone to crashing if a bug is present. It is all too easy to accidently mistake the return value of a function as error condition and handle it rather the letting the program crash. By separating the results from the transmission of error conditions (in effect taking error conditions out of band) then you make it much harder to make such a mistake because you have to explicity indicate which error conditions your code is capable of generating. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 07:35:18 -0800, Stephen Hansen wrote: Exceptions aren't about error management; they are about exceptional conditions: some are errors, others are entirely normal situations you know are going to happen (such as reaching the end of a sequence as you iterate over it: that's not an error, but it is special). To be philosophically opposed to them seems to me to be philosophically in favor of race conditions. Maybe I worded that part wrongly. Of course I get that error handling is only a part of exception mechanism. I agree that the built-in exceptions are useful but I prefer most times not to catch them. :-) ? How do they destroy clarity or add clutter, since presumably you have to deal with that False in some way with logical structure -- which always does whitespace in Python. If not immediately, then up the call stack (which seems to imply you should just not use a try/except and let the exception unwind the stack to wherever in your code someone wants to deal with it). The reason why I said they remove clarity is because it's not always obvious at which point the exception may be raised. In other words, within a try block there may be multiple statements that generate the exception. Of course, as I said before, one way would be to wrap single statements with the try block and avoid this issue. if is_correct(): result = do_thing() else: do_error_handling() try: result = do_thing() except KeyError: do_error_handling() Of course, to me the if statement would make more sense because I immediately figure out the exact condition being tested against while the exception object is not always so clear and maybe ambiguous in some cases. Also if that same type of exception is raised by another statement within a function that is called within the try block then it would be handled by the same except block right? This is where it gets a bit confusing to me and the flow of code is not always obvious. That's why I prefer atomic error handling where I know exactly which part of the code led to the result. And as an aside, the more statements one puts into a try/except block: and the more complicated a statement at that-- the more likely it is they are going to mess up and do something Wrong. Now, all that said: sure, in some situations I do prefer the check first style of programming where exceptions don't end up being used. Its not *wrong* to return False on an error condition: its going against the grain, though, and makes your code harder to deal with long-term if only because now there's two separate mechanisms that errors happen in it. I realize that it is impossible for me to avoid exception mechanism myself. So I restrict it only to situations where I cannot avoid it (e.g. inbuilt exceptions in some cases where I want to handle it myself) You can't totally do away with exceptions in Python, even if you try very hard. So with that in mind, IMHO, the best approach is to just... get over it, and learn to appreciate them :) Finding it hard to appreciate exceptions myself. But I am used to thinking linearly. A piece of code which does not explain itself in the most obvious way even if I wrote it always worries me. -- Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: strange TypeError exception in function compiled from a string
nelson nelson1...@gmail.com writes: Hi all, I have this function, defined in a string and ecetuted through ad exec call def cell1(d): x=d.get('x') print x import y return y.add(y.add(self.adf0(x),self.adf0(x)),self.adf0(x)) What is self in line 7? -- Piet van Oostrum p...@vanoostrum.org WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] Nu Fair Trade woonartikelen op http://www.zylja.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, Harishankar v.harishan...@gmail.com wrote: I understand that the error vs exception debate is quite a big one in the programming community as a whole and I don't consider myself very Actually, I thought that debate was resolved years ago. I cannot think of a single recently developed programming language that does not provide exception handling mechanisms because they have been proven more reliable. Python. I do understand both sides of the issue. Exceptions seem to be generally more reliable but I feel they add a lot of complexity particular when a lot of code is placed in a try block. Lines of code is one measure of complexity. Each line of code has a small chance of containing a bug. The more lines of code that you have, then the more likely that one of them contains a bug. Exceptions, by placing error handling code in fewer places, requires much fewer lines of code then requiring error handling code after each call that might produce an error condition. The operations of propogating an error up to the higher level logic of the program is another measure. In languages without exception handling, careful planning is needed to pass error conditions up through the call stack until they reach a high enough level in the logic that decisions can be made about how to handle them. Even further planning must be taken so that when the error condition reaches level where it needs to be handled, that enough information about the error is present to know exactly what went wrong so that it can figure out what to do about it. This usually involves using globals like errorno to pass out of band information about the error. Sometimes you even need to know about how the error affect intermediate levels, did the intermediate code attempt to handle the condtion and fail? The Openssl error handling system, that creates an error logging chain is an example of just how complex this can become. You gain all of this functionality automatically through exception mechanisms; without all of the complexity. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 15:53:49 +, Tim Harig wrote: If you are using exceptions to try to catch bug then you are using them improperly. Exceptions (with the exception (no pun intended) of AssertionError) are designed to catch error conditions, not bugs. I agree. But more specifically some exceptions seem too broad to catch specific errors that occur in MY code rather than in a system call or a library call. Also writing my own exception objects and raising exceptions seem to add too much of complexity to what is essentially a simple problem. This is what I was trying to explain. Actually, finer grained error handling commonly covers up bugs. If you want to find bugs, you want to make the program prone to crashing if a bug is present. It is all too easy to accidently mistake the return value of a function as error condition and handle it rather the letting the program crash. By separating the results from the transmission of error conditions (in effect taking error conditions out of band) then you make it much harder to make such a mistake because you have to explicity indicate which error conditions your code is capable of generating. Doesn't the same finer grained exception mechanism make it prone to the same problems? Actually return values of functions which I write myself can be as specific and to the point. I could make it as fuzzy or as precise as I like. This is no doubt, something that I could emulate with an exception as well, but only make it slightly more complex with no obvious benefit. As I said before, the way exceptions are caught seem to me to be the most confusing bit. Non-atomic operations always worry me. What if my function which is wrapped inside a try block has two different statements that raised the same exception but for different reasons? With error handling I could probably handle it right below the statement which was called and thus reduce the problem??? -- Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: string find/replace
Carlo ca...@somewhere.com writes: On 2010-12-01, Peter Otten __pete...@web.de wrote: import re re.compile(([a-z])([A-Z])).sub(r\1 \2, camelCase) 'camel Case' Very simple if you know it. Thank you! And almost as cryptic as Perl!! -- Piet van Oostrum p...@vanoostrum.org WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] Nu Fair Trade woonartikelen op http://www.zylja.com -- http://mail.python.org/mailman/listinfo/python-list
Re: DBF (VFP) to XLS (Excel) in pure Python
On Dec 1, 10:28 pm, kirby.ur...@gmail.com kirby.ur...@gmail.com wrote: Playing around with arcane tools to read those pesky DBF files (with memo fields), like floating wine barrels cast off the sinking VFP ship. Although it's true I don't know that I'm getting in memory DBF reads, the bulk of the time in the code I just shared is in the writing to XLS inner loop. Looking to a different solution for writing, perhaps a DLL available for $ (one time). Been reading more about VFP, Silverlight and HTML5. Python seems in a good position. All caught up on IronPython now too. Kirby -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple modules from single c extension
Can you explain how to do this with distutils then? Would I need a separate setup.py for SpamABC and SpamXYZ? How would I get them included in the parent module Spam? Could you explain what you mean when you say The Python import mechanism will be looking for an appropriately-named .pyd file for each module? Are you saying that in python when I say from Spam.ABC import * I need a file called Spam.ABC.[so|pyd]? On Wed, Dec 1, 2010 at 8:39 PM, Robert Kern robert.k...@gmail.com wrote: On 12/1/10 4:12 PM, Eric Frederich wrote: I have an extension to some C library that I created using the guide found here... http://docs.python.org/extending/extending.html I am starting to have A LOT of functions being wrapped. The library that I'm creating bindings for is organized into modules. In fact, all of their function calls start with a prefix like ABC_do_something, XYZ_something_else. I'd like to start putting the bindings for each module into a separate C file and have each set of bindings end up in its own Python module as well. Is this possible to do using a single .dll / .pyd file so that I can use a single Visual Studio project for these bindings? No, I don't think so. The Python import mechanism will be looking for an appropriately-named .pyd file for each module. In any case, you shouldn't be using Visual Studio directly to build the .pyd. Instead, use distutils. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Some syntactic sugar proposals
On Nov 15, 12:46 pm, Tim Chase python.l...@tim.thechases.com wrote: On 11/15/2010 12:39 AM, Dmitry Groshev wrote: x in range optimisation I've often thought this would make a nice O(1)-test lookup on an xrange() generator. An O(1) test for 'x in range_object' is implemented in Python 3.2, at least provided that x has type 'int', 'long' or 'bool'. (If x is an instance of a subclass of int or long, then there's a risk that the semantics of the membership test have been changed by an explicitly overridden __eq__, so Python wimps out and falls back to the O(n) algorithm in that case.) Python 3.2a4+ (py3k:86635:86636M, Nov 21 2010, 19:22:18) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin Type help, copyright, credits or license for more information. -1 in range(10**9) False 5 in range(0, 10**100, 2) False 10**99 in range(0, 10**100, 2) True IIRC, there wasn't sufficient interest to get it backported to Python 2.7 in time for its release. Though as a pure optimization, one could argue that it would still be possible to get this into Python 2.7.2. Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple modules from single c extension
On 12/2/10 10:39 AM, Eric Frederich wrote: Can you explain how to do this with distutils then? Would I need a separate setup.py for SpamABC and SpamXYZ? How would I get them included in the parent module Spam? Please consult the distutils documentation. http://docs.python.org/distutils/index.html In short, you only need one setup.py file. from distutils.core import setup, Extension setup( ... ext_modules=[ Extension('Spam.ABC', sources=['src/spam_abc.c']), Extension('Spam.XYZ', sources=['src/spam_xyz.c']), ], packages=['Spam'], ... ) Could you explain what you mean when you say The Python import mechanism will be looking for an appropriately-named .pyd file for each module? Are you saying that in python when I say from Spam.ABC import * I need a file called Spam.ABC.[so|pyd]? No, it will be looking for Spam/ABC.pyd where Spam/ is your package directory (i.e. it should have an __init__.py file, like any other Python package). -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
Harishankar v.harishan...@gmail.com writes: There are some reasons why I hate exceptions but that is a different topic. However, in short I can say that personally: 1. I hate try blocks which add complexity to the code when none is needed. Try blocks make code much more unreadable in my view and I use it only for the built-in exceptions when absolutely needed. Very little code actually needs `try' blocks. 2. I prefer the less irksome True or False to do error checking. Exceptions seem too heavyweight for simple problems. Just write simple programs as if errors never happen; if an exception is raised, the program prints a vaguely useful message and exits. Besides, this complaint is incoherent. One needs at least as much structure to check an error code as to catch an exception. 3. Philosophically I think exception handling is the wrong approach to error management. There are better ways to handle errors than Python's exception system. Passing error codes around manually is most definitely not one of them. (One of the main reasons I ditched Perl in favour of Python is the former's insistence on returning error codes for I/O and system calls.) -- [mdw] -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, Harishankar v.harishan...@gmail.com wrote: Actually, finer grained error handling commonly covers up bugs. If you want to find bugs, you want to make the program prone to crashing if a bug is present. It is all too easy to accidently mistake the return value of a function as error condition and handle it rather the letting the program crash. By separating the results from the transmission of error conditions (in effect taking error conditions out of band) then you make it much harder to make such a mistake because you have to explicity indicate which error conditions your code is capable of generating. Doesn't the same finer grained exception mechanism make it prone to the same problems? Exception handling is a move away from fine grained handling. If in IOError is raised by a parser, I don't need to know at exactly which line the error occured. All I need to know is that the file is somehow unreadable; and that is why the parser failed. Therefore, none of the parser code should be handling IOError because it is being handled higher up in the stack. Since I expected that the parser might have problemes reading files, for problems that have nothing to do with my code, I explicity catch that error if it occurs. I am not expecting an IndexError from the parser and I don't bother to catch it. If the parser code does raise an IndexError, then my program crashes and I know that I have a bug in the parsing code. The call trace will tell me where that error occurs. I can watch that section of code in debugger to find out exactly what went wrong. Actually return values of functions which I write myself can be as specific and to the point. I could make it as fuzzy or as precise as I like. This is no doubt, something that I could emulate with an exception as well, but only make it slightly more complex with no obvious benefit. You seem to be making it complex because you are still trying to be too fine grained in handling each exception where it occurs as opposed to handing where the logic makes sense that it should be handled and because you are trying to code too defensively against your own code. Exception handling does require a different focus from handling errors from return values alone. As I said before, the way exceptions are caught seem to me to be the most confusing bit. Non-atomic operations always worry me. What if my function which is wrapped inside a try block has two different statements that raised the same exception but for different reasons? With error handling I could probably handle it right below the statement which was called and thus reduce the problem??? If you are having that issue, then you are likely placing the try blocks at too low of a level in your code. In general you will find that most systems have a gateway function as an entry point to the system. If there is not one already, then create such a function in you code. The parse function in my code above would be an example of such a gateway function. Beneath that function, you don't need to know exactly where the error occured, you just need to know the nature of the error and have general error handling procedures for each kind of error that you expect might occur. -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode thing that causes a traceback in 2.6 and 2.7 but not in 2.5, and only when writing to a pipe, not to the terminal
Dan Stromberg drsali...@gmail.com writes: What is this about? It's another n~ thing, but this time in 2.x. All I'm doing is printing a str containing a character 127. It works fine in 2.5, to a terminal or to a pipe. In 2.6 and 2.7, it fails when writing to a pipe but works fine writing to my terminal - an mrxvt.\ I really kind of want octets to just be octets, since I'm on a Linux system - it probably should be up to the user and their related software to decide how those octets are interpreted. I'm assuming that the bytes() workarounds I'm using in 3.x aren't going to work in 2.x - it looks like bytes() is just an alias for str() in 2.6 and 2.7. Strings are indeed byte strings (or octet strings if you prefer) in Python 2.x. Traceback (most recent call last): File ./test-readline0, line 22, in module print(line) UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 20: ordinal not in range(128) This message shows that line is not a byte string but a unicode string. If you want to output a unicode string it has to be converted to bytes first, and Python must know which encoding to use. For output to the terminal it can usually get that information from the system, but not for a pipe. You will have to supply it. By the way, if you just want to work with octets, why is the variable line in unicode? If you keep everything in byte strings your problem may disappear. In Python 3 you have to do this explicitely as the default is unicode. -- Piet van Oostrum p...@vanoostrum.org WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] Nu Fair Trade woonartikelen op http://www.zylja.com -- http://mail.python.org/mailman/listinfo/python-list
Google AI challenge: planet war. Lisp won.
discovered this rather late. Google has a AI Challenge: planet wars. http://ai-contest.com/index.php it started sometimes 2 months ago and ended first this month. the winner is Gábor Melis, with his code written in lisp. Congrats lispers! Gábor wrote a blog about it here http://quotenil.com/Planet-Wars-Post-Mortem.html (not sure if this has been mentioned here but quick search didn't find it) Xah ∑ http://xahlee.org/ ☄ -- http://mail.python.org/mailman/listinfo/python-list
pyOpenGL Error unable to detect undefined names
Hello, I am trying to use pyOpenGL and I keep getting the following errors: Traceback (most recent call last): File C:\Temp\Python\OpenGL_Test.py, line 10, in module from OpenGL.GLU import * File C:\Python26\lib\site-packages\OpenGL\GLU\__init__.py, line 4, in module from OpenGL.raw.GLU import * File C:\Python26\lib\site-packages\OpenGL\raw\GLU\__init__.py, line 6, in module from OpenGL.raw.GLU.constants import * File C:\Python26\lib\site-packages\OpenGL\raw\GLU\constants.py, line 7, in module from OpenGL import platform, arrays File C:\Python26\lib\site-packages\OpenGL\arrays\__init__.py, line 22, in module formathandler.FormatHandler.loadAll() File C:\Python26\lib\site-packages\OpenGL\arrays\formathandler.py, line 37, in loadAll cls.loadPlugin( entrypoint ) File C:\Python26\lib\site-packages\OpenGL\arrays\formathandler.py, line 44, in loadPlugin plugin_class = entrypoint.load() File C:\Python26\lib\site-packages\OpenGL\plugins.py, line 14, in load return importByName( self.import_path ) File C:\Python26\lib\site-packages\OpenGL\plugins.py, line 28, in importByName module = __import__( ..join(moduleName), {}, {}, moduleName) File C:\Python26\lib\site-packages\OpenGL\arrays\numpymodule.py, line 25, in module from OpenGL_accelerate.numpy_formathandler import NumpyHandler File numpy.pxd, line 30, in OpenGL_accelerate.numpy_formathandler (src\numpy_formathandler.c:3543) ValueError: numpy.dtype does not appear to be the correct type object The code is very simple all I have is two import statements: from OpenGL.GLU import * from OpenGL.GL import * The code analysis says that it is unable to detect undefined names. What does that mean and how do I fix it? Is it an installation error? Thanks for the help, Ron -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I define class methods outside of the class?
On 2 déc, 06:36, Jeremy jlcon...@gmail.com wrote: I have some methods that I need (would like) to define outside of the class. I know this can be done by defining the function and then setting it equal to some member OT assignement or binding might be the terms you were looking for here ;) Also in Python we talk about attributes, not members /OT of an instance of the class. What you describe here will not define class methods, nor even instance methods FWIW - it will only make the function an attribute of the instance, but won't turn the function into a method (IOW: the function won't get the instance as first argument). Also and while we're at it, a Python classmethod is something special - it's a method that can be called on either an instance or the class itself, and takes the class - not the instance - as first argument. But, because of the complexity of what I'm doing (I have to set many functions as class methods) I would rather not do this. Can someone show me how to do this? Is it even possible? To no do this ? Yes, certainly g More seriously: if your problem is to dynamically add a bunch of methods to an existing *class*, it's quite easy - just import the class and assign your functions to it, ie: from otherlib import OtherClass def foo(self): print %s.foo % self OtherClass.foo = foo And voila, The foo method is now available to all (even already existing) instances of OtherClass. If this doesn't answer your question, please provide more context. Can decorators be used here? What for ? -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I define class methods outside of the class?
On 2 déc, 15:45, Jeremy jlcon...@gmail.com wrote: On Dec 1, 10:47 pm, James Mills prolo...@shortcircuit.net.au wrote: On Thu, Dec 2, 2010 at 3:36 PM, Jeremy jlcon...@gmail.com wrote: I have some methods that I need (would like) to define outside of the class. I know this can be done by defining the function and then setting it equal to some member of an instance of the class. But, because of the complexity of what I'm doing (I have to set many functions as class methods) I would rather not do this. Can someone show me how to do this? Is it even possible? Can decorators be used here? Do you mean something like this ? @classmethod def foo(cls): print I am the foo classmethod on %r % cls class Foo(object): pass Foo.foo = foo cheers James Thanks, James. That is almost exactly what I want. However, I want to avoid doing Foo.foo = foo Is this going to be possible? def patch(cls): def _patch(func): setattr(cls, func.__name__, func) return func return _patch class Foo(object): pass @patch(Foo) def bar(self): print self f = Foo() f.bar() I'm trying to understand how decorators are used. Are they really necessary in this example? In the above example, the classmethod type was used as a decorator to turn the function into, well, a classmethod (read my other answer in this thread if you don't know what a classmethod is). -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
Harishankar wrote: As I said before, the way exceptions are caught seem to me to be the most confusing bit. Non-atomic operations always worry me. What if my function which is wrapped inside a try block has two different statements that raised the same exception but for different reasons? With error handling I could probably handle it right below the statement which was called and thus reduce the problem??? Exception actually are the solution as you can give an unlimitted quantity of information: def myfunction(self): error = myfuncException('a meaninful message') # error 1 error.blame = whateverobjectresponsibleoftheerror # error 2 error.blame = anotherobject_anothercause raise error try: myfunction() except myfuncException, exception: cause = exception.blame # you can inspect the 'cause' for specific handling In a more general and simple manner, you can tune the error feedback of exceptions by changing the message of the exception. Using different exception classes is also an obvious way to do it. But my point was that you can also set attributes to the exception you're raising with reference to almost anything at the time the exception occurs. And that is a very cool way to give precise feedback to exception handlers. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Get frame object of last called function
In article fe48f5b8-36b4-433d-84f7-e7d749485...@j2g2000yqf.googlegroups.com, moerchendiser2k3 googler.1.webmas...@spamgourmet.com wrote: Hi, is there any chance to get the frame object of the previous called function? sys._current_frames(), sys._getframe() -- Aahz (a...@pythoncraft.com) * http://www.pythoncraft.com/ Think of it as evolution in action. --Tony Rand -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 02/12/2010 16:12, Tim Harig wrote: On 2010-12-02, Harishankarv.harishan...@gmail.com wrote: I understand that the error vs exception debate is quite a big one in the programming community as a whole and I don't consider myself very Actually, I thought that debate was resolved years ago. I cannot think of a single recently developed programming language that does not provide exception handling mechanisms because they have been proven more reliable. Python. I do understand both sides of the issue. Exceptions seem to be generally more reliable but I feel they add a lot of complexity particular when a lot of code is placed in a try block. Lines of code is one measure of complexity. Each line of code has a small chance of containing a bug. The more lines of code that you have, then the more likely that one of them contains a bug. Exceptions, by placing error handling code in fewer places, requires much fewer lines of code then requiring error handling code after each call that might produce an error condition. The operations of propogating an error up to the higher level logic of the program is another measure. In languages without exception handling, careful planning is needed to pass error conditions up through the call stack until they reach a high enough level in the logic that decisions can be made about how to handle them. Even further planning must be taken so that when the error condition reaches level where it needs to be handled, that enough information about the error is present to know exactly what went wrong so that it can figure out what to do about it. This usually involves using globals like errorno to pass out of band information about the error. Sometimes you even need to know about how the error affect intermediate levels, did the intermediate code attempt to handle the condtion and fail? The Openssl error handling system, that creates an error logging chain is an example of just how complex this can become. You gain all of this functionality automatically through exception mechanisms; without all of the complexity. When writing the C code for the new regex module I thought that it would've been easier if I could've used exceptions to propagate errors and unwind the stack, instead of having to return an error code which had to be checked by the caller, and then have the caller explicitly return an error code to /its/ caller. Automatic garbage collection would also have been nice. You don't realise how nice it is to have such things until you have to go without them. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
MRAB pyt...@mrabarnett.plus.com writes: When writing the C code for the new regex module I thought that it would've been easier if I could've used exceptions to propagate errors and unwind the stack, instead of having to return an error code which had to be checked by the caller, and then have the caller explicitly return an error code to /its/ caller. That's called longjmp. Automatic garbage collection would also have been nice. alloca might help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
Aside from the other issues raised, I will just note that is more common to return None when there is no answer (for whatever reason) rather than False and explicitly compare 'is None' than 'is False'. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Some syntactic sugar proposals
On 12/02/2010 10:39 AM, Mark Dickinson wrote: On Nov 15, 12:46 pm, Tim Chasepython.l...@tim.thechases.com wrote: On 11/15/2010 12:39 AM, Dmitry Groshev wrote: x in range optimisation I've often thought this would make a nice O(1)-test lookup on an xrange() generator. An O(1) test for 'x inrange_object' is implemented in Python 3.2, at least provided that x has type 'int', 'long' or 'bool'. (If x is an instance of a subclass of int or long, then there's a risk that the semantics of the membership test have been changed by an explicitly overridden __eq__, so Python wimps out and falls back to the O(n) algorithm in that case.) Drat, bested again by the python time-machine. Thanks for bringing that to my attention. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, Paul Rubin no.em...@nospam.invalid wrote: MRAB pyt...@mrabarnett.plus.com writes: When writing the C code for the new regex module I thought that it would've been easier if I could've used exceptions to propagate errors and unwind the stack, instead of having to return an error code which had to be checked by the caller, and then have the caller explicitly return an error code to /its/ caller. That's called longjmp. The problem is that you might have partially allocated data structures that you need to free before you can go anywhere. Jumping up several levels, without letting the intermediate levels do their cleanup could easily lead to a memory leak or worse. -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I define class methods outside of the class?
On Dec 2, 10:26 am, bruno.desthuilli...@gmail.com bruno.desthuilli...@gmail.com wrote: On 2 déc, 15:45, Jeremy jlcon...@gmail.com wrote: On Dec 1, 10:47 pm, James Mills prolo...@shortcircuit.net.au wrote: On Thu, Dec 2, 2010 at 3:36 PM, Jeremy jlcon...@gmail.com wrote: I have some methods that I need (would like) to define outside of the class. I know this can be done by defining the function and then setting it equal to some member of an instance of the class. But, because of the complexity of what I'm doing (I have to set many functions as class methods) I would rather not do this. Can someone show me how to do this? Is it even possible? Can decorators be used here? Do you mean something like this ? @classmethod def foo(cls): print I am the foo classmethod on %r % cls class Foo(object): pass Foo.foo = foo cheers James Thanks, James. That is almost exactly what I want. However, I want to avoid doing Foo.foo = foo Is this going to be possible? def patch(cls): def _patch(func): setattr(cls, func.__name__, func) return func return _patch class Foo(object): pass @patch(Foo) def bar(self): print self f = Foo() f.bar() Yes! This is what I was looking for. Thanks! Jeremy -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 12/2/2010 9:56 AM, Harishankar wrote: There are some reasons why I hate exceptions but that is a different topic. However, in short I can say that personally: 1. I hate try blocks which add complexity to the code when none is needed. Try blocks make code much more unreadable in my view and I use it only for the built-in exceptions when absolutely needed. 2. I prefer the less irksome True or False to do error checking. Exceptions seem too heavyweight for simple problems. It turns out that try block are computationally lighter weight (faster) for normal execution ;-) 3. Philosophically I think exception handling is the wrong approach to error management. I have never grown up programming with exceptions in C and I couldn't pick up the habit with python either. Did I mention that I detest try blocks? try blocks seem ugly and destroy code clarity at least in my view. And enclosing single statements under separate try blocks seem to add a lot of clutter. Having also come to Python directly from C, I can sympathize. It took me a while to adjust. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
Tim Harig user...@ilthio.net writes: That's called longjmp. The problem is that you might have partially allocated data structures that you need to free before you can go anywhere. Alloca can help with that since the stack stuff gets released by the longjmp. Alternatively you can have an auxiliary stack of cleanup records that the longjmp handler walks through. Of course if you do that, you're halfway towards reinventing exceptions. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, Paul Rubin no.em...@nospam.invalid wrote: MRAB pyt...@mrabarnett.plus.com writes: When writing the C code for the new regex module I thought that it would've been easier if I could've used exceptions to propagate errors and unwind the stack, instead of having to return an error code which had to be checked by the caller, and then have the caller explicitly return an error code to /its/ caller. That's called longjmp. In theory. In practice, using longjump for that without blowing your foot off isn't easy. Automatic garbage collection would also have been nice. alloca might help. And that's almost as easy to screw up. :) -- Grant Edwards grant.b.edwardsYow! I want EARS! I want at two ROUND BLACK EARS gmail.comto make me feel warm 'n secure!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 02/12/2010 18:09, Paul Rubin wrote: MRABpyt...@mrabarnett.plus.com writes: When writing the C code for the new regex module I thought that it would've been easier if I could've used exceptions to propagate errors and unwind the stack, instead of having to return an error code which had to be checked by the caller, and then have the caller explicitly return an error code to /its/ caller. That's called longjmp. The problem with that is that the caller might have to do some tidying up, such as deallocation. Exceptions give the caller the chance of catching it (ideally in a 'finally' block), tidying up, and then propagating. Automatic garbage collection would also have been nice. alloca might help. I didn't know about that. It looks like that's allocated on the stack, and the allocation I'm talking must be on the heap, so it's not suitable anyway. -- http://mail.python.org/mailman/listinfo/python-list
hai this is kate, im staing near u, date with me for free... girls and boyz...
hai this is kate, im staing near u, date with me for free... girls and boyz... http://x2c.eu/5i http://x2c.eu/5i http://x2c.eu/5i http://x2c.eu/5i -- http://mail.python.org/mailman/listinfo/python-list
Re: DBF (VFP) to XLS (Excel) in pure Python
kirby.ur...@gmail.com wrote: Some ideas: for (i, name) in enumerate(thedbf.field_names): sheet1.write(0, i, name, header_style) thetype = thedbf.type(name) thelen, thedec = thedbf.size(name) if thetype == M: thelen = 100 elif thelen == 0: thelen = 1 colwidth = max(len(name), int(thelen)) sheet1.col(i).width = colwidth * 310 messages = [] for row, record in enumerate(thedbf): . . . Same error with: thetable = dbf.VfpTable(thefile) thetable.close(keep_memos=True) Does this still happen with the latest code? (Not yet on PyPI for those following along -- hope to get a new package released this week.) ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, Paul Rubin no.em...@nospam.invalid wrote: Tim Harig user...@ilthio.net writes: That's called longjmp. The problem is that you might have partially allocated data structures that you need to free before you can go anywhere. Alloca can help with that since the stack stuff gets released by the longjmp. Alternatively you can have an auxiliary stack of cleanup alloca() only helps if you actually *want* the data stored on the stack. There are many reasons one might prefer or need that the data in the heap. longjmp. Alternatively you can have an auxiliary stack of cleanup records that the longjmp handler walks through. Of course if you do Only if you already have pointers to *all* of the data structures at the point where you put your setjmp(). This approach is error prone. records that the longjmp handler walks through. Of course if you do that, you're halfway towards reinventing exceptions. Exactly. -- http://mail.python.org/mailman/listinfo/python-list
aggregation for a nested dict
Hi, i would like to parse many thousand files and aggregate the counts for the field entries related to every id. extract_field grep the identifier for the fields with regex. result = [ { extract_field(id, line) : [extract_field(field1, line),extract_field(field2, line)]} for line in FILE ] result gives me. {'a: ['0', '84']}, {'a': ['0', '84']}, {'b': ['1000', '83']}, {'b': ['0', '84']}, i like to aggregate them for every line or maybe file and get after the complete parsing procedure the possibility to count the amount of ids having 0 entries in '83'. {'a: {'0':2, '84':2}} {'b': {'1000':1,'83':1,'84':1} } My current solution with mysql is really slow. Many thanks for advance. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
Tim Harig user...@ilthio.net writes: longjmp. Alternatively you can have an auxiliary stack of cleanup records that the longjmp handler walks through. Of course if you do Only if you already have pointers to *all* of the data structures at the point where you put your setjmp(). The setjmp point only has to know where the aux stack is and its depth when the longjmp happens. The cleanup records contain any necessary pointers to data structures that need freeing. That is basically how try/finally would do it too. This is pretty standard stuff. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, Paul Rubin no.em...@nospam.invalid wrote: Tim Harig user...@ilthio.net writes: longjmp. Alternatively you can have an auxiliary stack of cleanup records that the longjmp handler walks through. Of course if you do Only if you already have pointers to *all* of the data structures at the point where you put your setjmp(). The setjmp point only has to know where the aux stack is and its depth when the longjmp happens. The cleanup records contain any necessary pointers to data structures that need freeing. That is basically how try/finally would do it too. This is pretty standard stuff. I am not talking about what setjmp() has to do, I am talking about what *you* have to do after setjmp() returns. If you have allocated memory in intermediate functions and you don't have a reference to them outside of the functions that longjmp() bypasses from returning properly (and thus either not clearning data structures or returning a reference to those data structures as it normally would) then you have potential memory leaks, dangling pointers, etc. I am not saying that this cannot be done. What I am saying is that it is inherently error prone. -- http://mail.python.org/mailman/listinfo/python-list
Re: aggregation for a nested dict
On 02/12/2010 19:01, chris wrote: Hi, i would like to parse many thousand files and aggregate the counts for the field entries related to every id. extract_field grep the identifier for the fields with regex. result = [ { extract_field(id, line) : [extract_field(field1, line),extract_field(field2, line)]} for line in FILE ] result gives me. {'a: ['0', '84']}, {'a': ['0', '84']}, {'b': ['1000', '83']}, {'b': ['0', '84']}, i like to aggregate them for every line or maybe file and get after the complete parsing procedure the possibility to count the amount of ids having 0 entries in '83'. {'a: {'0':2, '84':2}} {'b': {'1000':1,'83':1,'84':1} } My current solution with mysql is really slow. result = [ {'a': ['0', '84']}, {'a': ['0', '84']}, {'b': ['1000', '83']}, {'b': ['0', '84']}, ] from collections import defaultdict aggregates = defaultdict(lambda: defaultdict(int)) for entry in result: for key, values in entry.items(): for v in values: aggregates[key][v] += 1 print(aggregates) -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 12/2/2010 1:31 PM, Terry Reedy wrote: It turns out that try block are computationally lighter weight (faster) for normal execution ;-) Though that alone would hardly be sufficient reason to use them. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon 2011 Atlanta March 9-17 http://us.pycon.org/ See Python Video! http://python.mirocommunity.org/ Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 02/12/2010 19:15, Tim Harig wrote: On 2010-12-02, Paul Rubinno.em...@nospam.invalid wrote: Tim Hariguser...@ilthio.net writes: longjmp. Alternatively you can have an auxiliary stack of cleanup records that the longjmp handler walks through. Of course if you do Only if you already have pointers to *all* of the data structures at the point where you put your setjmp(). The setjmp point only has to know where the aux stack is and its depth when the longjmp happens. The cleanup records contain any necessary pointers to data structures that need freeing. That is basically how try/finally would do it too. This is pretty standard stuff. I am not talking about what setjmp() has to do, I am talking about what *you* have to do after setjmp() returns. If you have allocated memory in intermediate functions and you don't have a reference to them outside of the functions that longjmp() bypasses from returning properly (and thus either not clearning data structures or returning a reference to those data structures as it normally would) then you have potential memory leaks, dangling pointers, etc. I am not saying that this cannot be done. What I am saying is that it is inherently error prone. Automatic garbage collection is nice to have when using exceptions precisely because it's automatic, so unwinding the stack is much safer. -- http://mail.python.org/mailman/listinfo/python-list
Re: aggregation for a nested dict
On Thu, Dec 2, 2010 at 11:01 AM, chris oz...@web.de wrote: Hi, i would like to parse many thousand files and aggregate the counts for the field entries related to every id. extract_field grep the identifier for the fields with regex. result = [ { extract_field(id, line) : [extract_field(field1, line),extract_field(field2, line)]} for line in FILE ] result gives me. {'a: ['0', '84']}, {'a': ['0', '84']}, {'b': ['1000', '83']}, {'b': ['0', '84']}, i like to aggregate them for every line or maybe file and get after the complete parsing procedure the possibility to count the amount of ids having 0 entries in '83'. {'a: {'0':2, '84':2}} {'b': {'1000':1,'83':1,'84':1} } Er, what happened to the '0' for 'b'? My current solution with mysql is really slow. Untested: # requires Python 2.7+ due to Counter from collections import defaultdict, Counter FIELDS = [field1, field2] id2counter = defaultdict(Counter) for line in FILE: identifier = extract_field(id, line) counter = id2counter[identifier] for field_name in FIELDS: field_val = int(extract_field(field_name, line)) counter[field_val] += 1 print(id2counter) print(sum(1 for counter in id2counter.values() if counter[83])) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: aggregation for a nested dict
chris wrote: Hi, i would like to parse many thousand files and aggregate the counts for the field entries related to every id. extract_field grep the identifier for the fields with regex. result = [ { extract_field(id, line) : [extract_field(field1, line),extract_field(field2, line)]} for line in FILE ] result gives me. {'a: ['0', '84']}, {'a': ['0', '84']}, {'b': ['1000', '83']}, {'b': ['0', '84']}, i like to aggregate them for every line or maybe file and get after the complete parsing procedure the possibility to count the amount of ids having 0 entries in '83'. {'a: {'0':2, '84':2}} {'b': {'1000':1,'83':1,'84':1} } My current solution with mysql is really slow. def rows(lines): ... for line in lines: ... yield extract_field(id, line), [extract_field(name, line) for name in field1, field2] ... for row in rows(lines): ... print row ... ('a', ['0', '84']) ('b', ['1000', '83']) ('a', ['0', '84']) ('b', ['0', '84']) from collections import defaultdict class defaultdict(defaultdict): # omit that in your real code ... def __repr__(self): return repr(dict(self)) ... outer = defaultdict(lambda: defaultdict(int)) for key, values in rows(lines): ... inner = outer[key] ... for v in values: ... inner[v] += 1 ... outer {'a': {'0': 2, '84': 2}, 'b': {'83': 1, '1000': 1, '84': 1, '0': 1}} -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
Tim Harig user...@ilthio.net writes: I am not talking about what setjmp() has to do, I am talking about what *you* have to do after setjmp() returns. If you have allocated memory in intermediate functions and you don't have a reference to them outside of the functions that longjmp() bypasses from returning properly (and thus either not clearning data structures or returning a reference to those data structures as it normally would) then you have potential memory leaks, dangling pointers, etc. Sure, that's what the aux stack is for--you put any such references into it, for the setjmp handler to find later. You do that BEFORE setjmp returns, of course. I am not saying that this cannot be done. What I am saying is that it is inherently error prone. I suppose so, but so is everything else in C. On the overall scale of C-related hazards, this particular one isn't so bad if you code in a consistent style and are disciplined about recording the cleanups. You could also use something like an obstack, which is a stack allocated on the heap, so it persists after the control stack returns, but you can release the whole thing in one operation. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, MRAB pyt...@mrabarnett.plus.com wrote: On 02/12/2010 19:15, Tim Harig wrote: On 2010-12-02, Paul Rubinno.em...@nospam.invalid wrote: Tim Hariguser...@ilthio.net writes: longjmp. Alternatively you can have an auxiliary stack of cleanup records that the longjmp handler walks through. Of course if you do Only if you already have pointers to *all* of the data structures at the point where you put your setjmp(). The setjmp point only has to know where the aux stack is and its depth when the longjmp happens. The cleanup records contain any necessary pointers to data structures that need freeing. That is basically how try/finally would do it too. This is pretty standard stuff. I am not talking about what setjmp() has to do, I am talking about what *you* have to do after setjmp() returns. If you have allocated memory in intermediate functions and you don't have a reference to them outside of the functions that longjmp() bypasses from returning properly (and thus either not clearning data structures or returning a reference to those data structures as it normally would) then you have potential memory leaks, dangling pointers, etc. I am not saying that this cannot be done. What I am saying is that it is inherently error prone. Automatic garbage collection is nice to have when using exceptions precisely because it's automatic, so unwinding the stack is much safer. Indeed. -- http://mail.python.org/mailman/listinfo/python-list
Re: aggregation for a nested dict
On 12/02/2010 01:49 PM, MRAB wrote: On 02/12/2010 19:01, chris wrote: i would like to parse many thousand files and aggregate the counts for the field entries related to every id. extract_field grep the identifier for the fields with regex. result = [ { extract_field(id, line) : [extract_field(field1, line),extract_field(field2, line)]} for line in FILE ] i like to aggregate them for every line or maybe file and get after the complete parsing procedure {'a: {'0':2, '84':2}} {'b': {'1000':1,'83':1,'84':1} } I'm not sure what happened to b['0'] based on your initial data, but assuming that was an oversight... from collections import defaultdict aggregates = defaultdict(lambda: defaultdict(int)) for entry in result: for key, values in entry.items(): for v in values: aggregates[key][v] += 1 Or, if you don't need the intermediate result, you can tweak MRAB's solution and just iterate over the file(s): aggregates = defaultdict(lambda: defaultdict(int)) for line in FILE: key = extract_field(id, line) aggregates[key][extract_field(field1, line)] += 1 aggregates[key][extract_field(field2, line)] += 1 or, if you're using an older version (2.5) that doesn't provide defaultdict, you could do something like aggregates = {} for line in FILE: key = extract_field(id, line) d = aggregates.setdefault(key, {}) for fieldname in ('field1', 'field2'): value = extract_field(fieldname, line) d[value] = d.get(value, 0) + 1 -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-02, Paul Rubin no.em...@nospam.invalid wrote: Tim Harig user...@ilthio.net writes: I am not talking about what setjmp() has to do, I am talking about what *you* have to do after setjmp() returns. If you have allocated memory in intermediate functions and you don't have a reference to them outside of the functions that longjmp() bypasses from returning properly (and thus either not clearning data structures or returning a reference to those data structures as it normally would) then you have potential memory leaks, dangling pointers, etc. Sure, that's what the aux stack is for--you put any such references into it, for the setjmp handler to find later. You do that BEFORE setjmp returns, of course. If you miss something, you are in trouble. There is a concept of variable life that is measured by how many lines separate the use of variable from its first use to its last. By using setjmp/longjmp, you effectively extend the life of these variables, potentially through several files, to at least as long as the jump. If there are several function calls in depth, there may be quite a lot of space that you have to check to make sure that you have not missed anything. I am not saying that this cannot be done. What I am saying is that it is inherently error prone. I suppose so, but so is everything else in C. On the overall scale of C-related hazards, this particular one isn't so bad if you code in a consistent style and are disciplined about recording the cleanups. You could also use something like an obstack, which is a stack allocated on the heap, so it persists after the control stack returns, but you can release the whole thing in one operation. By working the error back up through the call stack, you can keep track of the variables and allocations in each function isolated to that function. The smaller each function is, the easier and less error prone it will be to theck it is to check. That makes it much easier to make sure that you have not missed anything. Essentially, you can validate that each function correctly handles is allocations rather then having to validate the setjmp/longjmp structure as a whole. To use Joe Armstrong's phrase, it makes the impossible merely difficult. Back to the topic, by using Python with its exceptions and garbage collection, all of this is a moot point. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
Harishankar v.harishan...@gmail.com writes: On Thu, 02 Dec 2010 22:19:25 +1100, Ben Finney wrote: More details of the problem you're trying to solve would help with giving specific advice. I'm writing functions with multiple points of failure exits. I use return False as a way to flag the error condition rather than raising exceptions. That's not much detail. Based only on that, I would say you should be raising an exception at each “point of failure”, preferably of a type defined for the purpose, instead of returning False. But under certain circumstances, the function can also return empty lists The function returns boolean, list, and None types? That's very much a case where the function is trying to do too many things with the return value. Raise exceptions for exceptional cases, and define the function interface so that it's doing one clear job only. Often that involves breaking a complicated function into several collaborating functions with simpler interfaces. -- \“Pinky, are you pondering what I'm pondering?” “Umm, I think | `\ so, Brain, but three men in a tub? Ooh, that's unsanitary!” | _o__) —_Pinky and The Brain_ | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Uso de variable Global
Hola. Estoy probando Tkinter y escribí este pequeño código el cual crea un formulario con un textbox y un botón. Al ingresar un dato en el textbox y presionar el botón, se imprime en la consola el valor. ---CODE from Tkinter import * def muestra(): print(valor.get()) class App: def __init__(self,master): global valor valor = StringVar() e = Entry(master,textvariable=valor).pack() b = Button(master,text='Mostrar',command=muestra).pack() master = Tk() app = App(master) master.mainloop() - Funciona, pero tuve que hacer uso de una variable Global. Pregunta: ¿Es valida esta forma?, ¿Se puede hacer de otra forma, sin ocuparla?. Saludos. Cristian -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python-es] Uso de variable Global
Ni idea de Tkinter, pero ¿no puedes almacenar *valor* en una variable de instancia de App y convertir la función *muestra* en un método de la classe App que teng aceso a las variables de instancia de App? - Pau Python..., what else? 2010/12/2 craf p...@vtr.net Hola. Estoy probando Tkinter y escribí este pequeño código el cual crea un formulario con un textbox y un botón. Al ingresar un dato en el textbox y presionar el botón, se imprime en la consola el valor. ---CODE from Tkinter import * def muestra(): print(valor.get()) class App: def __init__(self,master): global valor valor = StringVar() e = Entry(master,textvariable=valor).pack() b = Button(master,text='Mostrar',command=muestra).pack() master = Tk() app = App(master) master.mainloop() - Funciona, pero tuve que hacer uso de una variable Global. Pregunta: ¿Es valida esta forma?, ¿Se puede hacer de otra forma, sin ocuparla?. Saludos. Cristian ___ Python-es mailing list python...@python.org http://mail.python.org/mailman/listinfo/python-es FAQ: http://python-es-faq.wikidot.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Uso de variable Global
craf wrote: Hola. Estoy probando Tkinter y escribí este pequeño código el cual crea un formulario con un textbox y un botón. Al ingresar un dato en el textbox y presionar el botón, se imprime en la consola el valor. ---CODE from Tkinter import * def muestra(): print(valor.get()) class App: def __init__(self,master): global valor valor = StringVar() e = Entry(master,textvariable=valor).pack() b = Button(master,text='Mostrar',command=muestra).pack() pack() returns None so both e and b set to None here. In this case it doesn't matter because you don't do anything with e and b. master = Tk() app = App(master) master.mainloop() - Funciona, pero tuve que hacer uso de una variable Global. Pregunta: ¿Es valida esta forma?, ¿Se puede hacer de otra forma, sin ocuparla?. I'd prefer to make valor an attribute and muestra() a method: from Tkinter import * class App: def __init__(self, master): self.valor = StringVar() Entry(master, textvariable=self.valor).pack() Button(master, text='Mostrar', command=self.muestra).pack() def muestra(self): print self.valor.get() master = Tk() app = App(master) master.mainloop() -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 12/2/2010 10:13 AM, Terry Reedy wrote: Aside from the other issues raised, I will just note that is more common to return None when there is no answer (for whatever reason) rather than False and explicitly compare 'is None' than 'is False'. The basic problem is that the original design of Python lacked a bool type. Classic language design error. It seems reasonable to people not familiar with programming language history to let True be equivalent to 1 and False be equivalent to 0, but it doesn't work out well. Retrofitting a bool type never quite works right. C/C++ went through this decades ago. The semantics of integers are clear, and the semantics of booleans are clear, but the semantics of mixed booleans and integers are not. You get questions like the one in this thread. Related questions include the semantics of x = True + True What's the value of x? True? 2? Is + between two Bool items addition, logical OR, or an error? The same problem applies to *. The arguments on either side can be seen in PEP 285, but they gloss over the fact that the original design was botched. Similar design errors show up in other places in Python. Using + for concatenation seemed reasonable, but didn't scale out well, especially after NumPy's array type was introduced. [1,2,3] + [4,5,6] and array([1,2,3]) + array([4,5,6]) produce quite different results. Worse, what should array([1,2,3]) + [4,5,6] do? It doesn't raise an exception. I went to a talk by Alexander Stepanov at Stanford recently, where he talked about problems in the fundamentals of programming. This is one of the issues that came up. Defining addition in a way that is not associative and commutative leads to problems, and breaks generic algorithms. If the basic operators follow the expected rules, generic algorithms will work on them. That was botched in Python. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
three column dataset - additions and deletions
I'm going to be writing a utility that will be pulling three fields from a MySQL table. I've already got a sample dataset - there's a long int (which is a db key), a short string, and a looong string. Many rows. As it is, receive this data from the DB interface as a rather large tuple of tuples. I plan on hashing the long string field (both for convenience and security) and storing the set in a pickle. The idea is that this script will run periodically, pulling the table, and comparing the data gathered at that run to that stored by the previous, acting on changes made, and storing the current data back (to be referenced against in the next invocation) I figure it will be easy enough to determine changed hashes for a given key. What I'm unclear on is what the best type of structure to keep this data in, given that I need to modify the data after it comes in (replacing that long string with, say, an MD5 from hashlib) and both need to act on new rows (rows that don't exist in the 'old' data) and deleted rows (rows that only exist in the 'old' data). Keeping in mind that I'm a newbie here, and I'm probably not aware of most of the different ways to store such things. I shouldn't have any problems with the logic itself - I just know enough to know I don't know the best ways of doing things :) Any suggestions? I'm not asking for code or handholding, but some objects or datatypes to look into would be very helpful at this early stage. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: three column dataset - additions and deletions
On 2010-12-02, draeath draeath.spamt...@gmail.com wrote: The idea is that this script will run periodically, pulling the table, and comparing the data gathered at that run to that stored by the previous, acting on changes made, and storing the current data back (to be referenced against in the next invocation) So, basically, you want to store a local copy of the data and sync it to the original. I figure it will be easy enough to determine changed hashes for a given key. What I'm unclear on is what the best type of structure to keep this data in, given that I need to modify the data after it comes in (replacing that long string with, say, an MD5 from hashlib) and both need to act on new rows (rows that don't exist in the 'old' data) and deleted rows (rows that only exist in the 'old' data). You need to differentiate between the in memory data model and the storage model. Since this data comes from a database in the first place, I would dump it to an sqlite3 database from the beginning. You can use this to store, modify, and change the values as you receive them from the database. If you are looking for in-memory structures, then you haven't really provided us with enough information on the significance and organization of the data. -- http://mail.python.org/mailman/listinfo/python-list
[Fwd: Re: Uso de variable Global]
- Mensaje reenviado De: Peter Otten __pete...@web.de Para: python-list@python.org Asunto: Re: Uso de variable Global Fecha: Thu, 02 Dec 2010 23:06:25 +0100 Grupos de noticias: comp.lang.python craf wrote: Hola. Estoy probando Tkinter y escribí este pequeño código el cual crea un formulario con un textbox y un botón. Al ingresar un dato en el textbox y presionar el botón, se imprime en la consola el valor. ---CODE from Tkinter import * def muestra(): print(valor.get()) class App: def __init__(self,master): global valor valor = StringVar() e = Entry(master,textvariable=valor).pack() b = Button(master,text='Mostrar',command=muestra).pack() pack() returns None so both e and b set to None here. In this case it doesn't matter because you don't do anything with e and b. master = Tk() app = App(master) master.mainloop() - Funciona, pero tuve que hacer uso de una variable Global. Pregunta: ¿Es valida esta forma?, ¿Se puede hacer de otra forma, sin ocuparla?. I'd prefer to make valor an attribute and muestra() a method: from Tkinter import * class App: def __init__(self, master): self.valor = StringVar() Entry(master, textvariable=self.valor).pack() Button(master, text='Mostrar', command=self.muestra).pack() def muestra(self): print self.valor.get() master = Tk() app = App(master) master.mainloop() Thanks!, Sorry for the Spanish mail. Regards Cristian -- http://mail.python.org/mailman/listinfo/python-list
Changing ' to in printed representation of dictionaries
Hello, I was wondering if there was a way to change the quote character for keys in string representation of dictionaries, so that they will be JSON equivalent. For example: x = { 'x': 1, 'y': 2 } { 'x': 1, 'y': 2 } `x` { 'x': 1, 'y': 2 } # close but not quite a JSON string `x`.replace(', '') '{ x: 1, y: 2 }' # JSON and python compatible So the question is, is there an automatic way to tell python to use instead of ' when doing a repr of lists? Thanks. -- Burton Samograd -- http://mail.python.org/mailman/listinfo/python-list
[Fwd: Re: Uso de variable Global]
- Mensaje reenviado De: Peter Otten __pete...@web.de Para: python-list@python.org Asunto: Re: Uso de variable Global Fecha: Thu, 02 Dec 2010 23:06:25 +0100 Grupos de noticias: comp.lang.python craf wrote: Hola. Estoy probando Tkinter y escribí este pequeño código el cual crea un formulario con un textbox y un botón. Al ingresar un dato en el textbox y presionar el botón, se imprime en la consola el valor. ---CODE from Tkinter import * def muestra(): print(valor.get()) class App: def __init__(self,master): global valor valor = StringVar() e = Entry(master,textvariable=valor).pack() b = Button(master,text='Mostrar',command=muestra).pack() pack() returns None so both e and b set to None here. In this case it doesn't matter because you don't do anything with e and b. master = Tk() app = App(master) master.mainloop() - Funciona, pero tuve que hacer uso de una variable Global. Pregunta: ¿Es valida esta forma?, ¿Se puede hacer de otra forma, sin ocuparla?. I'd prefer to make valor an attribute and muestra() a method: from Tkinter import * class App: def __init__(self, master): self.valor = StringVar() Entry(master, textvariable=self.valor).pack() Button(master, text='Mostrar', command=self.muestra).pack() def muestra(self): print self.valor.get() master = Tk() app = App(master) master.mainloop() Thanks!, Sorry for the Spanish mail. Regards Cristian -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 3 encoding question: Read a filename from stdin, subsequently open that filename
On Thu, 02 Dec 2010 12:17:53 +0100, Peter Otten wrote: This was actually a critical flaw in Python 3.0, as it meant that filenames which weren't valid in the locale's encoding simply couldn't be passed via argv or environ. 3.1 fixed this using the surrogateescape encoding, so now it's only an annoyance (i.e. you can recover the original bytes once you've spent enough time digging through the documentation). Is it just that you need to harden your scripts against these byte sequences or do you actually encounter them? If the latter, can you give some examples? Assume that you have a Python3 script which takes filenames on the command-line. If any of the filenames contain byte sequences which aren't valid in the locale's encoding, the bytes will be decoded to characters in the range U+DC00 to U+DCFF. To recover the original bytes, you need to use 'surrogateescape' as the error handling method when decoding, e.g.: enc = sys.getfilesystemencoding() argv_bytes = [arg.encode(enc, 'surrogateescape') for arg in sys.argv] Otherwise, it will complain about not being able to encode the surrogate characters. Similarly for os.environ. For anything else, you can just use sys.setfilesystemencoding('iso-8859-1') at the beginning of the script. Decoding as ISO-8859-1 will never fail, and encoding as ISO-8859-1 will give you the original bytes. But argv and environ are decoded before your script can change the encoding, so you need to know the trick to undo them if you want to write a robust Python 3 script which works with byte strings in an encoding-agnostic manner (i.e. a traditional Unix script). -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing ' to in printed representation of dictionaries
On 12/2/2010 3:06 PM Burton Samograd said... Hello, I was wondering if there was a way to change the quote character for keys in string representation of dictionaries, so that they will be JSON equivalent. For example: x = { 'x': 1, 'y': 2 } { 'x': 1, 'y': 2 } `x` { 'x': 1, 'y': 2 } # close but not quite a JSON string `x`.replace(', '') '{ x: 1, y: 2 }' # JSON and python compatible So the question is, is there an automatic way to tell python to use instead of ' when doing a repr of lists? import json json.dumps(x) Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: How to send an IP packet in Python?
On Thu, 02 Dec 2010 03:12:42 -0800, yegorov-p wrote: I have sniffed some packet and now I would like to send it with the help of python. But for some reason python send that: As you can see, python ignores my headers and creates its own. It isn't Python doing that, but the OS. At least on Unix, you need setsockopt(IP_HDRINCL) to tell the OS that the packet includes a header. It appears that Windows supports the same option: http://msdn.microsoft.com/en-us/library/ms738586%28v=VS.85%29.aspx -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing ' to in printed representation of dictionaries
On 12/2/2010 6:06 PM, Burton Samograd wrote: Hello, I was wondering if there was a way to change the quote character for keys in string representation of dictionaries, so that they will be JSON equivalent. For example: x = { 'x': 1, 'y': 2 } { 'x': 1, 'y': 2 } `x` { 'x': 1, 'y': 2 } # close but not quite a JSON string `x`.replace(', '') '{ x: 1, y: 2 }' # JSON and python compatible So the question is, is there an automatic way to tell python to use instead of ' when doing a repr of lists? You meant dicts rather than lists, but the issue is repr of strings. And no, there is no control of that (without subclassing strings). Note that dicts are not limited to string keys, or even keys prepresentable in json. If you want the json representation, when you know it is possible, use the json module! -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
m2crypto, base64, quoted-printable, xml
Hi, I have encrypted signed smime message with xml file. Messages are constructed: 1) xml file is embedded to MIME message as attacment (Content-Disposition: attachment;). 2) over whole content MIME message is signed by PKCS#7 and encoded Base64. 3) This message is encrypted by public key. I use M2crypto to decrypt: from M2Crypto import BIO, SMIME, X509 import os output = open(c://decrypted.txt, 'w') s = SMIME.SMIME() s.load_key('test.pem', 'test.pem') p7, data = SMIME.smime_load_pkcs7('input.txt') out = s.decrypt(p7) output.write(out) output.close() this part works properly. I try to verify the signed data, but without success. File decrypted.txt contains base64 encoded data. ( I tried to decode through from M2Crypto import BIO, SMIME, X509 import base64 import os input = open(C://decrypted.txt, 'r') output= open(C://decoded.txt, 'w') base64.decode(input,output) output.close input.close Decoded.txt contains quoted printable data, but if I try decode these data: import os import quopri input = open(c://decoded.txt, 'r') output= open(c://output.xml, 'w') quopri.decode(input, output) output.close input.close I obtain invalid and incomplete data. Data contains for example s ? čplitting instead of splitting. Do you have any idea, what do I do wrong? Thanks you very much Best regards Milan 0€ *†H†÷ €0€10 + 0€*†H†÷ €$€‚èContent-Type: application/octet-stream; name=6000809820.XML Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=6000809820.XML ?xml version=3D1.0 encoding=3DUTF-8? ISOTEDATA xmlns=3Dhttp://www.ote-cr.cz/schema/market/data; answer-require= d=3D0 date-time=3D2010-11-20T17:28:00 dtd-release=3D1 dtd-version=3D= 1 id=3D6000809820 message-code=3D933SenderIdentification coding-= scheme=3D14 id=3D859182407/SenderIdentificationReceiverIdentifi= cation coding-scheme=3D14 id=3D8591824345009/ReceiverIdentification= Trade acceptance=3DN id=3D84953 market=3DVDT trade-day=3D2010-11-20= trade-order=3D1 trade-type=3DP version=3D0TimeData datetime=3D201= 0-11-19T15:18:40 datetime-type=3DDTC/TimeDataProfileData profile-rol= e=3DP72Data period=3D20 unit=3DCZK value=3D1290.00/DataData p= eriod=3D21 unit=3DCZK value=3D1190.00/DataData period=3D23 unit= =3‚èDCZK value=3D1090.00/DataData period=3D24 unit=3DCZK value=3D= 790.00/Data/ProfileDataProfileData profile-role=3DC72Data perio= d=3D20 splitting=3DA unit=3DMWH value=3D10.00/DataData period= =3D21 splitting=3DA unit=3DMWH value=3D30.00/DataData period=3D= 23 splitting=3DA unit=3DMWH value=3D20.00/DataData period=3D24= splitting=3DA unit=3DMWH value=3D25.00/Data/ProfileDataCommen= t/Comment/TradeTrade acceptance=3DN id=3D85018 market=3DVDT tra= de-day=3D2010-11-20 trade-order=3D16 trade-type=3DP version=3D0Ti= meData datetime=3D2010-11-20T18:28:00 datetime-type=3DDTR/TimeDataT= imeData datetime=3D2010-11-20T10:38:22 datetime-type=3DDTC/TimeData= ProfileData profile-role=3DP72Data period=3D21 unit=3DCZK value=3D= 1367.00/DataData period=3D22 unit=3DCZK value=3D1262.00/Data= /ProfileDataProfileData profile-role=3DC72Data period=3D21 s‚èplittin= g=3DA unit=3DMWH value=3D10.00/DataData period=3D22 splitting= =3DA unit=3DMWH value=3D10.00/Data/ProfileDataComment/Comment= /TradeTrade acceptance=3DN id=3D85048 market=3DVDT trade-day=3D2= 010-11-20 trade-order=3D16 trade-type=3DN version=3D0TimeData date= time=3D2010-11-20T17:29:00 datetime-type=3DDTR/TimeDataTimeData dat= etime=3D2010-11-20T16:39:21 datetime-type=3DDTC/TimeDataProfileData= profile-role=3DP71Data period=3D20 unit=3DCZK value=3D510.00/D= ata/ProfileDataProfileData profile-role=3DC71Data period=3D20 spl= itting=3DA unit=3DMWH value=3D10.00/Data/ProfileDataComment/C= omment/TradeTrade acceptance=3DN id=3D85049 market=3DVDT trade-da= y=3D2010-11-20 trade-order=3D17 trade-type=3DN version=3D0TimeDat= a datetime=3D2010-11-20T18:29:00 datetime-type=3DDTR/TimeDataTimeDa= ta datetime=3D2010-11-20T16:39:43 datetime-type=3DDTC‚è/TimeDataProfi= leData profile-role=3DP71Data period=3D21 unit=3DCZK value=3D480.0= 0/DataData period=3D22 unit=3DCZK value=3D470.00/Data/Profil= eDataProfileData profile-role=3DC71Data period=3D21 splitting=3DA= unit=3DMWH value=3D10.00/DataData period=3D22 splitting=3DA un= it=3DMWH value=3D10.00/Data/ProfileDataComment/Comment/Trade= Trade acceptance=3DN id=3D85050 market=3DVDT trade-day=3D2010-11-20= trade-order=3D18 trade-type=3DN version=3D0TimeData datetime=3D2= 010-11-20T20:29:00 datetime-type=3DDTR/TimeDataTimeData datetime=3D= 2010-11-20T16:40:26 datetime-type=3DDTC/TimeDataProfileData profile-= role=3DP71Data period=3D23 unit=3DCZK value=3D450.00/DataData= period=3D24 unit=3DCZK value=3D420.00/Data/ProfileDataProfileD= ata profile-role=3DC71Data period=3D23 splitting=3DA unit=3DMWH v= alue=3D10.00/DataData period=3D24 ‚èsplitting=3DA unit=3DMWH valu= e=3D10.00/Data/ProfileDataComment/Comment/TradeTrade acceptan= ce=3DN
Re: Changing ' to in printed representation of dictionaries
On 02/12/2010 23:06, Burton Samograd wrote: Hello, I was wondering if there was a way to change the quote character for keys in string representation of dictionaries, so that they will be JSON equivalent. For example: x = { 'x': 1, 'y': 2 } { 'x': 1, 'y': 2 } `x` { 'x': 1, 'y': 2 } # close but not quite a JSON string `x`.replace(', '') '{ x: 1, y: 2 }' # JSON and python compatible So the question is, is there an automatic way to tell python to use instead of ' when doing a repr of lists? Try the json module. -- http://mail.python.org/mailman/listinfo/python-list
Re: aggregation for a nested dict
I very appreciate all responses. It's incredible how fast it is! Cheers Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: three column dataset - additions and deletions
On Thu, 02 Dec 2010 22:55:53 +, Tim Harig wrote: Thanks for taking the time to check in on this, Tim! So, basically, you want to store a local copy of the data and sync it to the original. In a way. I only need to store one copy of the data, and make note of changes between it and the current data. You need to differentiate between the in memory data model and the storage model. Since this data comes from a database in the first place, I would dump it to an sqlite3 database from the beginning. You can use this to store, modify, and change the values as you receive them from the database. I thought of doing that, but given that I only need to store a single instance of the data, a simple pickle will do the job nicely (am I correct in reading that it can save/load any python object?) If you are looking for in-memory structures, then you haven't really provided us with enough information on the significance and organization of the data. The data columns: Long Int, String (under 30 chars), String (over 100 chars) The rows can scale up to hundreds, perhaps thousands. The integer is the database key, the shorter string is a user name, and the longer string is an access control definition. The whole idea of this script is to check, daily, for any added or removed users - or any altered access control definition. I realize this could likely all be done from inside the database itself - but altering the DB itself is not an option (as the product vendor is very touchy about that, and altering it can null our support agreement) -- http://mail.python.org/mailman/listinfo/python-list
Re: Google AI challenge: planet war. Lisp won.
On Dec 2, 5:59 pm, tivrfoa lescoutinh...@gmail.com wrote: On Dec 2, 3:06 pm, Xah Lee xah...@gmail.com wrote: discovered this rather late. Google has a AI Challenge: planet wars.http://ai-contest.com/index.php it started sometimes 2 months ago and ended first this month. the winner is Gábor Melis, with his code written in lisp. Congrats lispers! Gábor wrote a blog about it herehttp://quotenil.com/Planet-Wars-Post-Mortem.html (not sure if this has been mentioned here but quick search didn't find it) Xah ∑http://xahlee.org/☄ this game is very cool and it's hard .. arghhttp://www.galcon.com/flash/play.php For what it's worth, I recollect that the original GalCon was written in Python as part of one of the PyWeek competitions. ;-) Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
In article mailman.143.1291301807.2649.python-l...@python.org, Harishankar v.harishan...@gmail.com wrote: There are some reasons why I hate exceptions but that is a different topic. However, in short I can say that personally: 1. I hate try blocks which add complexity to the code when none is needed. Try blocks make code much more unreadable in my view and I use it only for the built-in exceptions when absolutely needed. 2. I prefer the less irksome True or False to do error checking. Exceptions seem too heavyweight for simple problems. Please demonstrate that using ``if`` blocks for True/False is impler and cleaner than using ``try`` blocks to handle exceptions. -- Aahz (a...@pythoncraft.com) * http://www.pythoncraft.com/ Think of it as evolution in action. --Tony Rand -- http://mail.python.org/mailman/listinfo/python-list
Re: three column dataset - additions and deletions
On 2010-12-03, draeath draeath.spamt...@gmail.com wrote: On Thu, 02 Dec 2010 22:55:53 +, Tim Harig wrote: Thanks for taking the time to check in on this, Tim! So, basically, you want to store a local copy of the data and sync it to the original. In a way. I only need to store one copy of the data, and make note of changes between it and the current data. Perhaps I am missing a sublty that makes those statements different. You need to differentiate between the in memory data model and the storage model. Since this data comes from a database in the first place, I would dump it to an sqlite3 database from the beginning. You can use this to store, modify, and change the values as you receive them from the database. I thought of doing that, but given that I only need to store a single instance of the data, a simple pickle will do the job nicely (am I correct in reading that it can save/load any python object?) So, you start by dumping the data from the remote server into an sqlite3 database table. What you end up with is a record=record copy of the original query (plus any other meta data that you want to add). Then, when the data changes, you apply those same changes to your local table (or just regenerate it since you seem to be pulling all of the information from the server anyway. The result is a *single instance of the data*. Why you would want to replace this with a pickle of a nested set up tuples or a homebrew on disk data structure is beyond me. Using sqlite3 is almost certainly faster and more functional then anything you are going to create without some serious work. If you are looking for in-memory structures, then you haven't really provided us with enough information on the significance and organization of the data. The data columns: Long Int, String (under 30 chars), String (over 100 chars) The rows can scale up to hundreds, perhaps thousands. Then those are the columns that you create for your local table. The integer is the database key, the shorter string is a user name, and the longer string is an access control definition. The whole idea of this script is to check, daily, for any added or removed users - or any altered access control definition. The question is how are you going to use this information once you have mirroed it locally. Most likely, from you description, you just need to access it as a local read only data store. Now compare the differences in how you would acces the data: pickle method: 1. You have to load the entire pickle into memory. 2. Unless you add some kind of tree or indexing mechanism, you will have to walk through an average of 1/2 of the records to find the matching id. If you do use an advanced mechanism you have to create the code that inserts and locates the data. sqlite3 method: 1. You open the file using the sqlite3 connector which does not have to read all of the data into memory. 2. You use a select query to get just the record for the id that you are looking for. sqlite3 has already provided you with optimized lookup and indexing capability, as well as modification operations, etc (most likely written in C). As an added bonus, you don't have to worry about locking issues, to keep the clients from accesses the datastore and receiving an inconsistant copy, while you are making your periodic updates to the database Summary: the pickle method is reinventing the wheel. You can do what has already been done for you with the sqlite3 module (and library) that has already been written for you; but, getting anything near the same functionality is going to require considerable effort on your part; and you are probably going to have to write C to get the equivilant performance. Which seems like a better option to you? I realize this could likely all be done from inside the database itself - but altering the DB itself is not an option (as the product vendor is very touchy about that, and altering it can null our support agreement) Altering the remote database is not an option; but, I am talking about modifying only your local copy. If you can rewrite your pickle file, then you can modify the sqlite3 file. -- http://mail.python.org/mailman/listinfo/python-list
Re: three column dataset - additions and deletions
On 03/12/2010 01:42, Tim Harig wrote: On 2010-12-03, draeathdraeath.spamt...@gmail.com wrote: On Thu, 02 Dec 2010 22:55:53 +, Tim Harig wrote: Thanks for taking the time to check in on this, Tim! So, basically, you want to store a local copy of the data and sync it to the original. In a way. I only need to store one copy of the data, and make note of changes between it and the current data. Perhaps I am missing a sublty that makes those statements different. [snip] I take the difference to be this: The first statement says there are 2 copies, the local one and the original one, with the local one kept in synch with the original one. The second statement says there's 1 copy, plus the changes which have been made to it. -- http://mail.python.org/mailman/listinfo/python-list
Re: three column dataset - additions and deletions
On 2010-12-03, MRAB pyt...@mrabarnett.plus.com wrote: On 03/12/2010 01:42, Tim Harig wrote: On 2010-12-03, draeathdraeath.spamt...@gmail.com wrote: On Thu, 02 Dec 2010 22:55:53 +, Tim Harig wrote: Thanks for taking the time to check in on this, Tim! So, basically, you want to store a local copy of the data and sync it to the original. In a way. I only need to store one copy of the data, and make note of changes between it and the current data. Perhaps I am missing a sublty that makes those statements different. [snip] I take the difference to be this: The first statement says there are 2 copies, the local one and the original one, with the local one kept in synch with the original one. The second statement says there's 1 copy, plus the changes which have been made to it. Okay, so you keep one local copy of the data pristine with what is on the server, then you want to keep what is effectively a set of local changes from the what is on the server like a patch so that when you want to access the data locally, you can generate return a local copy by applying the patch to the pristine server data. Then you want to be able to pull down and incorporate changes from the server, something like what you would do with update on an SCM. The informated is still changed on a record by record basis. Does that sound correct? In that case, I would use the same basic scheme that I suggested before but adding a row in the table to track the changes for each field that might be locally modified. Null would of course mark fields that have not changed. You could store the changes literaly or using some kind of diff style delta that could be applied against the pristine copy. Updating from the server works as before; but, you need to decide what happens if there is a conflict between what changes on the server and local changes. -- http://mail.python.org/mailman/listinfo/python-list
Re: three column dataset - additions and deletions
On Fri, 03 Dec 2010 02:19:54 +, Tim Harig wrote: a whole bunch of useful stuff Certainly some good points for me to chew on... thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Fri, 03 Dec 2010 08:06:35 +1100, Ben Finney wrote: Raise exceptions for exceptional cases, and define the function interface so that it's doing one clear job only. Often that involves breaking a complicated function into several collaborating functions with simpler interfaces. This is probably what I should try to do. Of course my function returns only a list in most circumstances. Only in error does it return False. I mis-represented the None type for the empty list in my previous post, my apologies. -- Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 16:52:57 +, Tim Harig wrote: If you are having that issue, then you are likely placing the try blocks at too low of a level in your code. In general you will find that most systems have a gateway function as an entry point to the system. If there is not one already, then create such a function in you code. The parse function in my code above would be an example of such a gateway function. Beneath that function, you don't need to know exactly where the error occured, you just need to know the nature of the error and have general error handling procedures for each kind of error that you expect might occur. I think I might very well by using try blocks rather defensively rather than letting the code reach its logical conclusion in normal circumstances. This is why I think I find it a bit clunky. I think I understand the general trend of what you're saying. It definitely requires a mindset change. I still feel that user-defined exception classes might not be the way, but maybe I should allow the built-in exceptions which are thrown by library functions to follow its natural path upwards till it reaches the top level where it could be handled. Maybe I should handle the error only at the highest level (UI level) rather than returning False to flag errors. One of the reasons why I feared to do this is because I need to know each and every exception that might be thrown by the function and litter my top-level code with too many exception handlers. -- Harishankar (http://harishankar.org http://lawstudentscommunity.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: To Thread or not to Thread....?
On 12/1/2010 1:24 AM, Antoine Pitrou wrote: On Wed, 1 Dec 2010 02:45:50 + Jack Keeganwhatsjacksem...@gmail.com wrote: Hi there, I'm currently writing an application to control and take measurements during an experiments. This is to be done on an embedded computer running XPe so I am happy to have python available, although I am pretty new to it. The application basically runs as a state machine, which transitions through it's states based on inputs read in from a set of general purpose input/output (GPIO) lines. So when a certain line is pulled low/high, do something and move to another state. All good so far and since I get through main loop pretty quickly, I can just do a read of the GPIO lines on each pass through the loop and respond accordingly. However, in one of the states I have to start reading in, and storing frames from a camera. In another, I have to start reading accelerometer data from an I2C bus (which operates at 400kHz). I haven't implemented either yet but I would imagine that, in the case of the camera data, reading a frame would take a large amount of time as compared to other operations. Therefore, if I just tried to read one (or one set of) frames on each pass through the loop then I would hold up the rest of the application. Conversely, as the I2C bus will need to be read at such a high rate, I may not be able to get the required data rate I need even without the camera data. This naturally leads me to think I need to use threads. As I am no expert in either I2C, cameras, python or threading I thought I would chance asking for some advice on the subject. Do you think I need threads here or would I be better off using some other method. I was previously toying with the idea of using generators to create weightless threads (as detailed in http://www.ibm.com/developerworks/library/l-pythrd.html) for reading the GPIOs. Do you think this would work in this situation? The main question IMO: the I2C bus operates at 400kHz, but how much received data can it buffer? That will give you a hint as to how much latency you can tolerate. Right. Neither Windows XPe nor Python is designed for hard real time work. Unless your peripheral devices have considerable buffering, don't expect this to work without missing a measurement once in a while. If you have to talk to the I2C interface for each bus transaction before you get another reading, so that you have to respond to the device within 2ms every time, this probably won't work. If you were writing this in C++ on QNX, which is intended for hard real-time work, you could easily meet those time requirements. I've done that, and just reading the devices took maybe 3% of a Pentium 4. Python on Windows XPe, maybe not. You'll need to test. XPe is just XP with some stuff taken out; it's not a real-time OS. A good test is to hook up a square wave generator to some I2C device you can read, and try to read it in real time, measuring the time in microseconds between each pulse. Turn up the frequency until you start missing events. That will give you a handle on how good your real time capabilities are. Note that the machine you're using matters. Some computers have stuff going on in system management mode that results in long interrupt lockout latencies. One option may be to put something like an Arduno on a USB port, and let it talk to the I2C device. See http://www.arduino.cc/playground/Learning/I2C Let it queue up and timestamp the accelerometer data, which can then be read in blocks from the USB port. If both the accelerometer data and video frames are buffered, then you can do your processing in Python without trying to meet hard real-time constraints. This is the usual approach in Windows land. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: three column dataset - additions and deletions
On 12/2/2010 5:06 PM, draeath wrote: On Thu, 02 Dec 2010 22:55:53 +, Tim Harig wrote: Thanks for taking the time to check in on this, Tim! I realize this could likely all be done from inside the database itself - but altering the DB itself is not an option (as the product vendor is very touchy about that, and altering it can null our support agreement) A local database is probably the way to go. You're already using MySQL, so you know how to do that. You can use MySQL or SQlite on a local machine machine for your local database, while also talking to the remote MySQL database. Locally, you probably want to store the key, the short string, the MD5 of the long string, and the long string. When you get an update, put it in a temporary table, then compare that table with your permanent table. (The comparison is one line of SQL.) What you do with the differences is your problem. I have a system running which does something like this. Every three hours, it fetches PhishTank's database of a few hundred thousand phishign sites, and compares it to my local copy. Another system of mine reads the daily updates to SEC filings and updates my local database. This is all routine database stuff. If you have to work with big, persistent data sets, use a real database. That's what they are for, and they already have good algorithms for the hard stuff. Storing some local data structure with pickle is probably not the right approach. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On 2010-12-03, Harishankar v.harishan...@gmail.com wrote: On Thu, 02 Dec 2010 16:52:57 +, Tim Harig wrote: If you are having that issue, then you are likely placing the try blocks at too low of a level in your code. In general you will find that most systems have a gateway function as an entry point to the system. If there is not one already, then create such a function in you code. The parse function in my code above would be an example of such a gateway function. Beneath that function, you don't need to know exactly where the error occured, you just need to know the nature of the error and have general error handling procedures for each kind of error that you expect might occur. I think I might very well by using try blocks rather defensively rather than letting the code reach its logical conclusion in normal circumstances. This is why I think I find it a bit clunky. That was the conclusion I was coming to. I think I understand the general trend of what you're saying. It definitely requires a mindset change. I still feel that user-defined exception classes might not be the way, but maybe I should allow the built-in exceptions which are thrown by library functions to follow its natural path upwards till it reaches the top level where it could be handled. Look at it this way, in C you were constrained to place your error handling code around ever function that might fail. Now you are free to place the error handling code wherever it makes sense to do so. As a general rule, if, in C, your function would handle the error by passing an error return value to the calling function, then the error handling code should be higher up. Maybe I should handle the error only at the highest level (UI level) rather than returning False to flag errors. I don't write many UIs; but, I normally consider the UI code to be yet another subsystem. In general, I find the best place to place error handling code in the high level business logic of your application (which might be what you actually meant by UI, to me the UI code is the code that actually draws the interface), in the high level logic of the systems, and in the bounderies between subsystems. The exceptions caught at each level depend on where the logic to handle the error is best applied. One of the reasons why I feared to do this is because I need to know each and every exception that might be thrown by the function and litter my top-level code with too many exception handlers. Each exception has a place where it is better handled. Wherever you find boundaries between subsystems, think about what error conditions that subsystem might encounter. Subsystems dealing with are likely to encounter io related errors, network subsystems network errors, parsers validation errors etc. Logic exceptions that indicate errors in your code should be left alone entirely so that they may be easily found. Look at the exceptions pertaining to these subsystems. For each error reaching the boundery, think about whether you have enough information within the module to handle the error in a constructive manner or whether the error handling would benefit from information further up in the program. If you have all of the information that you need then handle it in the main logic of that subsystem. If not, pass it up to the error handlers on top of the boundry. When you get there, make the same decision. In general you only need to catch a handful of exceptions at each level. The easy excpetions will be handled at lower levels. The most difficult exceptions will rise towards the top of the program until only the terminal exceptions, that cannot be resolved are left with the inevitable result that you should notify the user and exit, will remain. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
On Thu, 02 Dec 2010 16:35:08 +, Mark Wooding wrote: 3. Philosophically I think exception handling is the wrong approach to error management. There are better ways to handle errors than Python's exception system. I'm curious -- what ways would they be? I'm aware of three general exception handling techniques: 1. return a sentinel value or error code to indicate an exceptional case (e.g. str.find returns -1); 2. raise an exception (e.g. nearly everything else in Python); 3. set an error code somewhere (often a global variable) and hope the caller remembers to check it; plus some de facto techniques sadly in common use: 4. dump core; 5. do nothing and produce garbage output. What else is there? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: three column dataset - additions and deletions
The only reason I want the hash is that I don't want a copy of this string laying around. I also don't need to know what it is, I just need to know if it's different. Think of this as a tripwire - if someone's user access level is changed, we find out. I still think using a separate database (sqlite or not) is entirely unnecessary. The task simply isn't large enough of a scale to justify going to that extreme. That's like cutting a string with a chainsaw. Granted, the next time I'm at my workstation I'll just give it a go and just stick with whatever turns out best The alternatives are what I was looking for, and you all did provide them :) So here's the goal, since it seems it's still unclear: Cron will call my script on an interval (probably once a day... so yea). The script will determine if users were added, removed, or changed. If so, an email will be constructed with generic details users bjoe, jdoe created; user mwallace deleted; user psouter access level changed and sent to the MUA/MTA/whatever for delivery to myself and the other admins. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparison with False - something I don't understand
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info writes: There are better ways to handle errors than Python's exception system. I'm curious -- what ways would they be? I'm aware of three general exception handling techniques: ... What else is there? The Erlang approach is to chop the application into a lot of very lightweight processes, and let any process that encounters an error simply crash. A monitoring process notices the crashed process and restarts it. There is a supervision tree of uber-monitor processes that restart crashed monitor proceses. I haven't programmed in that style myself and I'm not persuaded that it's better than what Python does, but I think it's different from the stuff on your list, which is an answer to your what else is there. I do know that they write some complex, very high reliability systems (phone switches) in Erlang. -- http://mail.python.org/mailman/listinfo/python-list
[issue8194] Incompatible API change in xmlrpclib.Transport.parse_response() of Python 2.7 and 3.2
Changes by Joshua Lock incandesc...@gmail.com: -- nosy: +joshual ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue8194 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com