From roy at panix.com Thu Dec 1 00:00:52 2011 From: roy at panix.com (Roy Smith) Date: Thu, 01 Dec 2011 00:00:52 -0500 Subject: Disable readline References: <4ed6ffed$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4ed6ffed$0$29986$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Is there a way to disable readline support in the interactive interpreter > at runtime? Either from within an existing session, or when the session > starts up will do. I'm assuming Python uses the standard GNU readline(). If that's correct, then you can configure its behavior by editing your ~/.inputrc file. Try "man readline". I don't see any global "disable readline" flag, but you may be able to get that effect by deleting all the key mappings, or some such silliness. Another possibility is setting your TERM environment variable to something that readline can't support: ~$ TERM=asr33 ~$ python Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. Cannot read termcap database; using dumb terminal settings. Cannot read termcap database; using dumb terminal settings. Cannot read termcap database; using dumb terminal settings. >>> > If all else fails, are there any traps or pitfalls in installing a second > Python installation with readline support disabled? None at all, as long as you keep them from stomping on each other. The easiest method is probably to use virtualenv. BTW, readline is the coolest, awesomist, most frabjulously gnarly thing to be invented since the pointed stick. The idea that somebody would want to turn it off (even for testing) disturbs me deeply. From roy at panix.com Thu Dec 1 00:02:35 2011 From: roy at panix.com (Roy Smith) Date: Thu, 01 Dec 2011 00:02:35 -0500 Subject: Need some IPC pointers References: <4ED69A1A.3080609@gmail.com> Message-ID: In article , Alec Taylor wrote: > Sure, I'll give you some pointers: > > 0x3A28213A > 0x6339392C > 0x7363682E What, no 0xDEADBEEF ??? From alec.taylor6 at gmail.com Thu Dec 1 00:21:53 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Thu, 1 Dec 2011 16:21:53 +1100 Subject: Need some IPC pointers In-Reply-To: References: <4ED69A1A.3080609@gmail.com> Message-ID: :P On Thu, Dec 1, 2011 at 4:02 PM, Roy Smith wrote: > In article , > ?Alec Taylor wrote: > >> Sure, I'll give you some pointers: >> >> 0x3A28213A >> 0x6339392C >> 0x7363682E > > What, no 0xDEADBEEF ??? > -- > http://mail.python.org/mailman/listinfo/python-list From rustompmody at gmail.com Thu Dec 1 00:32:30 2011 From: rustompmody at gmail.com (rusi) Date: Wed, 30 Nov 2011 21:32:30 -0800 (PST) Subject: Python crashes on segmentation error References: <8bffed0f-1050-418a-834d-f7060a899ab7@h5g2000yqk.googlegroups.com> Message-ID: <53778994-faa7-4b48-a568-85dd3daed693@n6g2000vbg.googlegroups.com> On Dec 1, 3:29?am, Terry Reedy wrote: > On 11/30/2011 7:58 AM, Christian Heimes wrote: > > > > > > > > > > > Am 30.11.2011 11:42, schrieb Ben Richardson: > >> Python crashes every time i run the following command? > > >>>>> import cv > >> Segmentation fault: 11 > > >> Python quit unexpectedly - any help would be greatly appreciated - > >> thankyou in advance :) > > >> Here is crash report? > > [...] > > >> Thread 0 Crashed:: Dispatch queue: com.apple.main-thread > >> 0 ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?000000000000000000 0 + 0 > >> 1 ? org.python.python ? ? ? ? ? ? ? ? ?0x00000001006c7885 PyImport_Import > >> + 121 > >> 2 ? org.python.python ? ? ? ? ? ? ? ? ?0x00000001006c7a1e > >> PyImport_ImportModule + 32 > >> 3 ? cv.so ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0x00000001004f5082 initcv + 18 > >> 4 ? org.python.python ? ? ? ? ? ? ? ? ?0x00000001000dc071 > >> _PyImport_LoadDynamicModule + 177 > > > It looks like you have a faulty 3rd party library with an extension > > module called cv.so that is causing the segfault. The segfault occurs > > either inside or during the import of the cv module. initcv() is the > > init function of the cv module that is called automatically during the > > import. > > In other words, contact the authors of the cv module. Or perhaps first > check to make sure that the cv.so build you are running is intended to > be compatible with the Python build you are running. > > -- > Terry Jan Reedy IOW python is a very safe language when python means python It can be very unsafe when it includes and tries to link carelessly written C. A safer intermediate option may be to try via ctypes [Of course assuming that clarifying from the authors of cv what is their intended use scenario is somehow a non-option] From bahamutzero8825 at gmail.com Thu Dec 1 00:48:54 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 30 Nov 2011 23:48:54 -0600 Subject: Need some IPC pointers In-Reply-To: References: <4ED69A1A.3080609@gmail.com> Message-ID: <4ED71546.3090909@gmail.com> On 11/30/2011 10:35 PM, Alec Taylor wrote: > Sure, I'll give you some pointers: I almost rephrased the subject line because I knew someone would make that joke. :P -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From steve+comp.lang.python at pearwood.info Thu Dec 1 01:42:19 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 01 Dec 2011 06:42:19 GMT Subject: Disable readline References: <4ed6ffed$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ed721cb$0$29986$c3e8da3$5496439d@news.astraweb.com> On Thu, 01 Dec 2011 00:00:52 -0500, Roy Smith wrote: > Another possibility is setting your TERM environment variable to > something that readline can't support: > > ~$ TERM=asr33 > ~$ python > Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. > build 5646)] on darwin Type "help", "copyright", "credits" or "license" > for more information. Cannot read termcap database; > using dumb terminal settings. > Cannot read termcap database; > using dumb terminal settings. > Cannot read termcap database; > using dumb terminal settings. Damn, my python is smarter than your python. steve at runes:~$ TERM=asr33 steve at runes:~$ python Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> And readline continues to work :/ I think I'll install from source a build with readline disabled. > BTW, readline is the coolest, awesomist, most frabjulously gnarly thing > to be invented since the pointed stick. The idea that somebody would > want to turn it off (even for testing) disturbs me deeply. I know! I don't use more than about 1% of what readline offers, but I can't imagine not using it. -- Steven From nmb.ten at gmail.com Thu Dec 1 02:03:26 2011 From: nmb.ten at gmail.com (=?UTF-8?B?0JrQsNC30LHQtdC6?=) Date: Wed, 30 Nov 2011 23:03:26 -0800 (PST) Subject: Python, django datetime to string conversation helper Message-ID: <098fc72f-90c0-4726-8575-7317a0c07ab2@v29g2000yqv.googlegroups.com> Online tool allows to select datetime to string format directives quickly and to check result. From nmb.ten at gmail.com Thu Dec 1 02:07:55 2011 From: nmb.ten at gmail.com (=?UTF-8?B?0JrQsNC30LHQtdC6?=) Date: Wed, 30 Nov 2011 23:07:55 -0800 (PST) Subject: Python, django datetime to string conversation helper References: <098fc72f-90c0-4726-8575-7317a0c07ab2@v29g2000yqv.googlegroups.com> Message-ID: <953a5e7d-772e-486b-851d-922480e4e838@w15g2000yqc.googlegroups.com> On 1 ???, 11:03, ?????? wrote: > Online tool allows to select datetime to string format directives > quickly and to check result. Sorry, here is the link. http://datetostr.org From rosuav at gmail.com Thu Dec 1 03:21:10 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 1 Dec 2011 19:21:10 +1100 Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: On Thu, Dec 1, 2011 at 2:15 PM, Roy Smith wrote: > for suffix in [''] + [str(i) for i in xrange(-1, -20, -1)]: > > It generates the right sequence of strings. ?But, if you came upon that > code, would it make sense to you, or would you spend half the afternoon > trying to figure out what it did and the other half of the afternoon > ranting about the deranged lunatic who wrote it? That's a self-contained piece of code.If I came upon it, I'd probably copy and paste it to IDLE, see what it comes up with, and proceed from there. Deranged you may be, but so long as code can be dropped into an interactive interpreter, it's fine. ChrisA From arnodel at gmail.com Thu Dec 1 03:35:15 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Thu, 1 Dec 2011 08:35:15 +0000 Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: On 1 December 2011 03:15, Roy Smith wrote: > I need to try a bunch of names in sequence until I find one that works > (definition of "works" is unimportant). ?The algorithm is: > > 1) Given a base name, "foo", first see if just plain "foo" works. > > 2) If not, try "foo-1", "foo-2", and so on > > 3) If you reach "foo-20", give up. > > What would you say if you saw this: > > for suffix in [''] + [str(i) for i in xrange(-1, -20, -1)]: > It's a little obfuscated ;) I would go for the simple: for i in xrange(21): suffix = "-%s" % i if i else "" .... -- Arnaud From janedenone at googlemail.com Thu Dec 1 05:01:33 2011 From: janedenone at googlemail.com (janedenone) Date: Thu, 1 Dec 2011 02:01:33 -0800 (PST) Subject: Reading twice from STDIN Message-ID: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> Hi, I would like to read from a pipe, parse the input and ask the user what to do next: message = sys.stdin.read() # message is parsed and URLs are printed as a list to choose from... selected_index = raw_input('Which URL to open?') Calling raw_input() always raises in an EOFError. I tried reopening and resetting sys.stdin, but nothing worked (at least on OX X 10.7, see http://stackoverflow.com/questions/8034595/python-raw-input-following-sys-stdin-read-throws-eoferror). I am surprised to find that a seemingly trivial task cannot be accomplished with Python 2.7 on a current Mac. Or am I missing something simple? - Jan From hniksic at xemacs.org Thu Dec 1 05:21:13 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 01 Dec 2011 11:21:13 +0100 Subject: unpack('>f', b'\x00\x01\x00\x00') References: <6b7251ef-3479-412f-8acb-882be1e25633@n35g2000yqf.googlegroups.com> Message-ID: <87obvsbnja.fsf@xemacs.org> Chris Rebert writes: > C does not have a built-in fixed-point datatype, so the `struct` > module doesn't handle fixed-point numbers directly. The built-in decimal module supports fixed-point arithmetic, but the struct module doesn't know about it. A bug report (or patch) by someone who works with binary representations of fixed-point would be a good start to improve it. From hv at tbz-pariv.de Thu Dec 1 05:25:02 2011 From: hv at tbz-pariv.de (Thomas Guettler) Date: Thu, 01 Dec 2011 11:25:02 +0100 Subject: get date from email Message-ID: <9jp2vuFrg4U1@mid.individual.net> Hi, up to now I use this code to parse date values from emails: msg=email.message_from_file(open(file_name)) date=None date_str=msg.get('date') if date_str: date_tuple=email.utils.parsedate_tz(date_str) if date_tuple: date=datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple)) if date: ... # valid date found Somehow this looks too complicated. Any chance to integrate the datetime module into the email module? related: http://stackoverflow.com/questions/1790795/python-parsing-date-with-timezone-from-an-email -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From gelonida at gmail.com Thu Dec 1 05:37:39 2011 From: gelonida at gmail.com (Gelonida N) Date: Thu, 01 Dec 2011 11:37:39 +0100 Subject: order independent hash? In-Reply-To: References: Message-ID: On 11/30/2011 01:32 PM, Neal Becker wrote: > I like to hash a list of words (actually, the command line args of my program) > in such a way that different words will create different hash, but not sensitive > to the order of the words. Any ideas? > Do youmean hash like digest like md5sum / sha1 ? You should sort the words alphabetically, concatenate them with a space or any character, that will NEVER be part of a word and calulate the hash. If words can exist multiple times, then youhad tu uniqufy them (u using a python dict / set) first. From gelonida at gmail.com Thu Dec 1 05:41:53 2011 From: gelonida at gmail.com (Gelonida N) Date: Thu, 01 Dec 2011 11:41:53 +0100 Subject: Reading twice from STDIN In-Reply-To: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> Message-ID: On 12/01/2011 11:01 AM, janedenone wrote: Hi, > > I would like to read from a pipe, parse the input and ask the user > what to do next: > > message = sys.stdin.read() With above line you said, that you want to read ALL data from stdin, so it's obvious that any following command will be unable to reda anything from standartd in Thus the EOF error. If you want to get input you had to read directly from the console (tty) and NOT from stdin. Stdin has already been consumed. It is possible in python to get the tty related to your console window and read from it. Unfortunately I don't kno whte commands by heart and I don't have time now to look it up. Perhaps somebody else can point you in the right direction. If not I'll it up till tomorrow. From markpackt at gmail.com Thu Dec 1 05:53:51 2011 From: markpackt at gmail.com (Mark) Date: Thu, 1 Dec 2011 02:53:51 -0800 (PST) Subject: Complete beginner, any help appreciated :) - For Loops Message-ID: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Hi there, I'm a complete beginner to Python and, aside from HTML and CSS, to coding in general. I've spent a few hours on it and think I understand most of the syntax. However, I'm wondering a bit about For Loops. I know that the basic syntax for them is to define a list, and then to use something like: for x in y However, what does "for" and "in" mean in this context? Can anyone help me to understand this? I know it's a really basic question, but hopefully it will see me on my way to coding properly :) Thanks a lot. From moky.math at gmail.com Thu Dec 1 06:14:01 2011 From: moky.math at gmail.com (Laurent Claessens) Date: Thu, 01 Dec 2011 12:14:01 +0100 Subject: Complete beginner, any help appreciated :) - For Loops In-Reply-To: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Message-ID: > for x in y > > However, what does "for" and "in" mean in this context? It means basically the same as in Englsish Does the following links answer the question ? http://www.ibiblio.org/g2swap/byteofpython/read/for-loop.html http://dsnra.jpl.nasa.gov/software/Python/diveintopython.pdf (page 58) Have a nice day Laurent From pedro.h.souto at gmail.com Thu Dec 1 06:32:11 2011 From: pedro.h.souto at gmail.com (Pedro Henrique G. Souto) Date: Thu, 01 Dec 2011 09:32:11 -0200 Subject: Complete beginner, any help appreciated :) - For Loops In-Reply-To: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Message-ID: <4ED765BB.7050305@gmail.com> On 01/12/2011 08:53, Mark wrote: > Hi there, > > I'm a complete beginner to Python and, aside from HTML and CSS, to coding in general. I've spent a few hours on it and think I understand most of the syntax. > > However, I'm wondering a bit about For Loops. I know that the basic syntax for them is to define a list, and then to use something like: > > for x in y > > However, what does "for" and "in" mean in this context? Can anyone help me to understand this? I know it's a really basic question, but hopefully it will see me on my way to coding properly :) > > Thanks a lot. That means (in a free translation) "For each one of 'x' in 'y', do this" 'y' is a list, for example, then it means: "For each one of the elements of the list 'y' (the element on the current iteration is named 'x'), do this" Good Luck! Att; Pedro Henrique G. Souto ??????????????? ? ???d?_?b??? ? ??????????????? From fred.sells at adventistcare.org Thu Dec 1 06:51:10 2011 From: fred.sells at adventistcare.org (Sells, Fred) Date: Thu, 1 Dec 2011 06:51:10 -0500 Subject: Py and SQL In-Reply-To: References: Message-ID: I find it easier to code like this Sql = ???select yadda, yadda, yadda FROM a,b,c Where this=that ORDER BY deudderting??? With the appropriate %s(varname) and % against a dictionary rather than positional args, but that?s just me. From: python-list-bounces+frsells=adventistcare.org at python.org [mailto:python-list-bounces+frsells=adventistcare.org at python.org] On Behalf Of Jerry Hill Sent: Wednesday, November 30, 2011 5:15 PM To: Verde Denim Cc: Python list Subject: Re: Py and SQL On Wed, Nov 30, 2011 at 3:30 PM, Verde Denim wrote: dbCursor1.execute('select lpad(' ', 2*level) || c "Privilege, Roles and Users" from ( select null p, name c from system_privilege_map where name like upper(\'%&enter_privliege%\') union select granted_role p, grantee c from dba_role_privs union select privilege p, grantee c from dba_sys_privs) start with p is null connect by p = prior c') I think this is your problem. Your string is delimited with single quotes on the outside ('), but you also have a mix of single and double quotes inside your string. If you were to assign this to a variable and print it out, you would probably see the problem right away. You have two options. First, you could flip the outer quotes to double quotes, then switch all of the quotes inside the string to single quotes (I think that will work fine in SQL). Second, you could use a triple-quoted string by switching the outer quotes to ''' or """. Doing that would let you mix whatever kinds of quotes you like inside your string, like this (untested): sql = '''select lpad(' ', 2*level) || c "Privilege, Roles and Users" from ( select null p, name c from system_privilege_map where name like upper(\'%&enter_privliege%\') union select granted_role p, grantee c from dba_role_privs union select privilege p, grantee c from dba_sys_privs) start with p is null connect by p = prior c''' dbCursor1.execute(sql) Once you do that, I think you will find that the "&enter_priviliege" bit in your SQL isn't going to do what you want. I assume you're expecting that to automatically pop up some sort of dialog box asking the user to enter a value for that variable? That isn't going to happen in python. That's a function of the database IDE you use. You'll need to use python to ask the user for the privilege level, then substitute it into the sql yourself. -- Jerry -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Dec 1 06:56:44 2011 From: d at davea.name (Dave Angel) Date: Thu, 01 Dec 2011 06:56:44 -0500 Subject: Complete beginner, any help appreciated :) - For Loops In-Reply-To: <4ED765BB.7050305@gmail.com> References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> <4ED765BB.7050305@gmail.com> Message-ID: <4ED76B7C.9020005@davea.name> On 12/01/2011 06:32 AM, Pedro Henrique G. Souto wrote: > > On 01/12/2011 08:53, Mark wrote: >> Hi there, >> >> I'm a complete beginner to Python and, aside from HTML and CSS, to >> coding in general. I've spent a few hours on it and think I >> understand most of the syntax. >> >> However, I'm wondering a bit about For Loops. I know that the basic >> syntax for them is to define a list, and then to use something like: >> >> for x in y >> >> However, what does "for" and "in" mean in this context? Can anyone >> help me to understand this? I know it's a really basic question, but >> hopefully it will see me on my way to coding properly :) >> >> Thanks a lot. > > That means (in a free translation) > > "For each one of 'x' in 'y', do this" > > 'y' is a list, for example, then it means: "For each one of the > elements of the list 'y' (the element on the current iteration is > named 'x'), do this" > > And if y is a string, it means for each character in y, do this x = the character (then do the indented part) And if y is an arbitrary iterable, it means for each thing the iterable produces x = the thing (then do the indented part) Each time you go through the loop, x will be equal to the next item in the sequence. So you can systematically process the items in the list (or characters in the string, or values from an iterable), one at a time. The only real caveat is to not do something that would change the list (etc.) while you're looping through it. What are some other examples of iterables? infile = open("myfile.txt", "r") #infile is an iterable for line in infile: print "**", line, "**" for val in (1, 4, 9, 2007) for x in xrange(50000000): #this does the same as range(), but doesn't actually build the list. #this way, we don't run out of memory You can also code your own generator function, which is an iterable, and may be used like the above. Some things in this message assume Python 2.x. in Python 3, range works differently, as does print. -- DaveA From markpackt at gmail.com Thu Dec 1 07:15:50 2011 From: markpackt at gmail.com (Mark) Date: Thu, 1 Dec 2011 04:15:50 -0800 (PST) Subject: Complete beginner, any help appreciated :) - For Loops In-Reply-To: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Message-ID: <28925560.347.1322741750955.JavaMail.geo-discussion-forums@prij11> Thanks a lot for the answers everyone, I really appreciate you getting back to me so quickly. I think that I understand where I am with this now :) From ben+python at benfinney.id.au Thu Dec 1 08:36:37 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 02 Dec 2011 00:36:37 +1100 Subject: Complete beginner, any help appreciated :) - For Loops References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Message-ID: <87aa7c4dne.fsf@benfinney.id.au> Mark writes: > I'm a complete beginner to Python and, aside from HTML and CSS, to > coding in general. I've spent a few hours on it and think I understand > most of the syntax. Welcome! You should work your way through the Python tutorial, from beginning to end . Actually do each exercise, experiment if you have questions, and then continue. Repeat until done. -- \ ?Ours is a world where people don't know what they want and are | `\ willing to go through hell to get it.? ?Donald Robert Perry | _o__) Marquis | Ben Finney From ndbecker2 at gmail.com Thu Dec 1 08:55:38 2011 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 01 Dec 2011 08:55:38 -0500 Subject: order independent hash? References: Message-ID: Gelonida N wrote: > On 11/30/2011 01:32 PM, Neal Becker wrote: >> I like to hash a list of words (actually, the command line args of my >> program) in such a way that different words will create different hash, but >> not sensitive >> to the order of the words. Any ideas? >> > Do youmean hash like digest like md5sum / sha1 ? > > > You should sort the words alphabetically, concatenate them with a space > or any character, that will NEVER be part of a word and calulate the hash. > > If words can exist multiple times, then youhad tu uniqufy them (u using > a python dict / set) first. Yes that sounds just like what I wanted - thanks! From nicholas.dokos at hp.com Thu Dec 1 09:30:11 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Thu, 01 Dec 2011 09:30:11 -0500 Subject: Disable readline In-Reply-To: Message from Steven D'Aprano of "01 Dec 2011 06:42:19 GMT." <4ed721cb$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <4ed6ffed$0$29986$c3e8da3$5496439d@news.astraweb.com> <4ed721cb$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8137.1322749811@alphaville.dokosmarshall.org> Steven D'Aprano wrote: > On Thu, 01 Dec 2011 00:00:52 -0500, Roy Smith wrote: > > > Another possibility is setting your TERM environment variable to > > something that readline can't support: > > > > ~$ TERM=asr33 > > ~$ python > > Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. > > build 5646)] on darwin Type "help", "copyright", "credits" or "license" > > for more information. Cannot read termcap database; > > using dumb terminal settings. > > Cannot read termcap database; > > using dumb terminal settings. > > Cannot read termcap database; > > using dumb terminal settings. > > > Damn, my python is smarter than your python. > > steve at runes:~$ TERM=asr33 > steve at runes:~$ python > Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) > [GCC 4.4.5] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> > > And readline continues to work :/ > Two things: o Is TERM exported? Maybe Roy's is and yours isn't. o Is asr33 in your termcap database? Try export TERM=dumb perhaps? Nick > > I think I'll install from source a build with readline disabled. > > > > BTW, readline is the coolest, awesomist, most frabjulously gnarly thing > > to be invented since the pointed stick. The idea that somebody would > > want to turn it off (even for testing) disturbs me deeply. > > I know! I don't use more than about 1% of what readline offers, but I > can't imagine not using it. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > From dihedral88888 at googlemail.com Thu Dec 1 10:35:32 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 07:35:32 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> On Wednesday, November 30, 2011 8:47:13 PM UTC+8, Peter Otten wrote: > Neal Becker wrote: > > > I like to hash a list of words (actually, the command line args of my > > program) in such a way that different words will create different hash, > > but not sensitive to the order of the words. Any ideas? > > You mean a typical python hash value which would be /likely/ to differ for > different lists and /guaranteed/ to be equal for equal lists is not good > enough? Because that would be easy: > > args = sys.argv[1:] > hash(tuple(sorted(args))) # consider duplicate args I knew a hash can replace a bi-directional linked list. The value can be a multi-field string to be parsed for further actions. Is this what you are asking? From dihedral88888 at googlemail.com Thu Dec 1 10:35:32 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 07:35:32 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> On Wednesday, November 30, 2011 8:47:13 PM UTC+8, Peter Otten wrote: > Neal Becker wrote: > > > I like to hash a list of words (actually, the command line args of my > > program) in such a way that different words will create different hash, > > but not sensitive to the order of the words. Any ideas? > > You mean a typical python hash value which would be /likely/ to differ for > different lists and /guaranteed/ to be equal for equal lists is not good > enough? Because that would be easy: > > args = sys.argv[1:] > hash(tuple(sorted(args))) # consider duplicate args I knew a hash can replace a bi-directional linked list. The value can be a multi-field string to be parsed for further actions. Is this what you are asking? From d at davea.name Thu Dec 1 10:52:46 2011 From: d at davea.name (Dave Angel) Date: Thu, 01 Dec 2011 10:52:46 -0500 Subject: order independent hash? In-Reply-To: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> Message-ID: <4ED7A2CE.6070306@davea.name> On 12/01/2011 10:35 AM, 88888 Dihedral wrote: > On Wednesday, November 30, 2011 8:47:13 PM UTC+8, Peter Otten wrote: >> Neal Becker wrote: >> >>> I like to hash a list of words (actually, the command line args of my >>> program) in such a way that different words will create different hash, >>> but not sensitive to the order of the words. Any ideas? >> You mean a typical python hash value which would be /likely/ to differ for >> different lists and /guaranteed/ to be equal for equal lists is not good >> enough? Because that would be easy: >> >> args = sys.argv[1:] >> hash(tuple(sorted(args))) # consider duplicate args > I knew a hash can replace a bi-directional linked list. > The value can be a multi-field string to be parsed for further actions. > Is this what you are asking? A hash is a number, so I don't see how it can replace any kind of linked list. Perhaps you're thinking of some other language. -- DaveA From dihedral88888 at googlemail.com Thu Dec 1 11:17:08 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 08:17:08 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> Message-ID: <31562283.427.1322756228499.JavaMail.geo-discussion-forums@pret21> On Thursday, December 1, 2011 11:52:46 PM UTC+8, Dave Angel wrote: > On 12/01/2011 10:35 AM, 88888 Dihedral wrote: > > On Wednesday, November 30, 2011 8:47:13 PM UTC+8, Peter Otten wrote: > >> Neal Becker wrote: > >> > >>> I like to hash a list of words (actually, the command line args of my > >>> program) in such a way that different words will create different hash, > >>> but not sensitive to the order of the words. Any ideas? > >> You mean a typical python hash value which would be /likely/ to differ for > >> different lists and /guaranteed/ to be equal for equal lists is not good > >> enough? Because that would be easy: > >> > >> args = sys.argv[1:] > >> hash(tuple(sorted(args))) # consider duplicate args > > I knew a hash can replace a bi-directional linked list. > > The value can be a multi-field string to be parsed for further actions. > > Is this what you are asking? > A hash is a number, so I don't see how it can replace any kind of linked > list. Perhaps you're thinking of some other language. > > -- > > DaveA A long number of a varied length allowed but can interpreted by the programmer. From dihedral88888 at googlemail.com Thu Dec 1 11:17:08 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 08:17:08 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> Message-ID: <31562283.427.1322756228499.JavaMail.geo-discussion-forums@pret21> On Thursday, December 1, 2011 11:52:46 PM UTC+8, Dave Angel wrote: > On 12/01/2011 10:35 AM, 88888 Dihedral wrote: > > On Wednesday, November 30, 2011 8:47:13 PM UTC+8, Peter Otten wrote: > >> Neal Becker wrote: > >> > >>> I like to hash a list of words (actually, the command line args of my > >>> program) in such a way that different words will create different hash, > >>> but not sensitive to the order of the words. Any ideas? > >> You mean a typical python hash value which would be /likely/ to differ for > >> different lists and /guaranteed/ to be equal for equal lists is not good > >> enough? Because that would be easy: > >> > >> args = sys.argv[1:] > >> hash(tuple(sorted(args))) # consider duplicate args > > I knew a hash can replace a bi-directional linked list. > > The value can be a multi-field string to be parsed for further actions. > > Is this what you are asking? > A hash is a number, so I don't see how it can replace any kind of linked > list. Perhaps you're thinking of some other language. > > -- > > DaveA A long number of a varied length allowed but can interpreted by the programmer. From rosuav at gmail.com Thu Dec 1 11:18:29 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 03:18:29 +1100 Subject: order independent hash? In-Reply-To: <4ED7A2CE.6070306@davea.name> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> Message-ID: On Fri, Dec 2, 2011 at 2:52 AM, Dave Angel wrote: > On 12/01/2011 10:35 AM, 88888 Dihedral wrote: >> I knew a hash can replace a bi-directional linked list. >> The value ?can be a multi-field string ?to be parsed for further actions. >> Is this what you are asking? > > A hash is a number, so I don't see how it can replace any kind of linked > list. ?Perhaps you're thinking of some other language. A hashtable is a form of data structure that involves hashing values (ie calculating hashes for them) and storing them for easier retrieval than a simple linked list offers. That may be what you're thinking of. ChrisA From adrianp at bringtechnology.com Thu Dec 1 12:41:22 2011 From: adrianp at bringtechnology.com (Adrian Powell) Date: Thu, 01 Dec 2011 12:41:22 -0500 Subject: platform issues? Message-ID: <20111201124122.8818@web003.roc2.bluetie.com> I'm new to python and I'm trying to get a twitter client running on a new machine but it keeps on failing. I tracked the problem down to an issue opening URLs and wrote this little test case: import urllib2 url = 'http://www.google.com/' opener = urllib2.build_opener() url_data = opener.open(url).read url_data When I run that on a dev machine it works fine, but when it's on one of our servers it crashes: >>> import urllib2 >>> url = 'http://www.google.com/' >>> opener = urllib2.build_opener() >>> url_data = opener.open(url).read Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/urllib2.py", line 391, in open response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py", line 409, in _open '_open', req) File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 1173, in http_open return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/python2.7/urllib2.py", line 1148, in do_open raise URLError(err) urllib2.URLError: Our servers have the same version of python and we're running the same OS (Fedora 14). Can anyone think of what might be causing this problem, or why it works on most (but not all) machines? Thanks From redcat at catfolks.net Thu Dec 1 13:00:41 2011 From: redcat at catfolks.net (Redcat) Date: 1 Dec 2011 18:00:41 GMT Subject: platform issues? References: Message-ID: <9jptm8Fug2U9@mid.individual.net> > Our servers have the same version of python and we're running the same > OS (Fedora 14). Can anyone think of what might be causing this problem, > or why it works on most (but not all) machines? > > Thanks Is the server able to resolve www.google.com properly? If so, are you POSITIVE that the "url = " line on the server has no typos? From devplayer at gmail.com Thu Dec 1 13:03:53 2011 From: devplayer at gmail.com (DevPlayer) Date: Thu, 1 Dec 2011 10:03:53 -0800 (PST) Subject: Using the Python Interpreter as a Reference References: <79379487-0081-4067-92ed-c6717652e1ff@y7g2000vbe.googlegroups.com> <4eb0af60-26b3-45d5-8aff-566505003d6a@m10g2000vbc.googlegroups.com> <4ed2cddc$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed491fe$0$14018$c3e8da3$76491128@news.astraweb.com> Message-ID: On Nov 29, 3:04?am, Steven D'Aprano wrote: > On Tue, 29 Nov 2011 12:49:49 +1100, Chris Angelico wrote: > > On Tue, Nov 29, 2011 at 11:54 AM, DevPlayer wrote: > >> To me, I would think the interpreter finding the coder's intended > >> indent wouldn't be that hard. And just make the need for consistant > >> spaces or tabs irrevelent simply by reformatting the indent as > >> expected. Pretty much all my text editors can. > > > The trouble with having a language declaration that "a tab is equivalent > > to X spaces" is that there's no consensus as to what X should be. > > Historically X has always been 8, and quite a few programs still assume > > this. I personally like 4. Some keep things narrow with 2. You can even > > go 1 - a strict substitution of \t with \x20. Once you declare it in > > your language, you immediately break everyone who uses anything > > different. > > Why should we enforce how many spaces a tab is set to? That is literally > only visible to the editor, not the compiler. (Unless the parser is > particularly stupid and merely substitutes X spaces for a tab every time > it sees one.) > > Mixed spaces and tabs in an indent are ambiguous, and so raises > SyntaxError (or at least it *should* raise SyntaxError). But separate > code blocks can use different absolute indent levels without ambiguity, > so long as the relative indentation is consistent: > > def ham(): # tab stops at 4 and 8 > ? ? do(a) > ? ? do(b) > ? ? if c: > ? ? ? ? do(c) > ? ? else: > ? ? ? ? do(d) > > def spam(): # tab stops at 8 and 12 > ? ? ? ? do(a) > ? ? ? ? do(b) > ? ? ? ? if c: > ? ? ? ? ? ? do(c) > ? ? ? ? else: > ? ? ? ? ? ? do(d) > > There is no meaningful difference in indentation between ham and spam > above. In either case, I could replace spaces with tabs to get the same > relative indents regardless of the absolute indentation. > > I can appreciate the aesthetic argument that *within a single file*, > indentation should be consistent. But it's not logically necessary for > the parser: it need only be consistent within a single code unit > (function or class usually). In other words: syntax should only care > about relative indentation, absolute indentation belongs as a coding > standard. > > -- > Steven Great point. Your point is why I started writting my own little Python parser/scanner (as an pet project/lesson project) and is in part, why I wrote my little reindent() in the first place. I am/was trying to figure out the algorithim for how Python's indent/dedents made logical "code blocks" or "closures" or whatever they are called. That and see if there was a way to interpret docstrings in various formats. I often get indentation syntax errors when I cut and paste stackflow.com, devprogrammer.com, daniweb.com, etc. code snippets into my various IDEs/ editors and I wanted to make a little tabnanny of my own. Some of the IDEs and editors (Editra) allow plugins. So I wanted to get a baseline for the various plugins. So I call the reindent() after I call the blocker(), which determines a block by -releative- indent/dedent as +1 or, 0 or -1 whether that be +4 spaces, +2 spaces, +1 tab or -3 spaces, whatever...; as you, Steve, mentioned indent is not fixed but relative. (btw I'v posted this in this topic because I thought it could contribute to how Unit's choice of how indents are handled my benefit from this discussion). My thoughts on tab verse space chars;, for most old and even current commandline interfaces, text editors, and historically line printers, tabs simply acted like a macro to move x number of fixed spaces. It wasn't until varible width fonts, kerning, and other advanced printing techniques became mainstream<- did space chars differ in meaning from tab chars, exception perhaps being file storage space (way back in the day). I only say this because I can't stand hitting the right and left arrow keys or using multiple fingers to move left and right 3 more times then I have to while editing my text. I -feel- I move faster around my text files with tabs then I do with 4 spaces. They only annoyance I've had with Python's flexiblity with indent using tab and or spaces is the docstrings. \t = tab def myfunc(): \t"""here is my docstring that bla bla bla's \t\tand indented do da days... \t""" verse (s = space) def myfunc(): ssss"""here is my docstring that bla bla bla's ssssssssand indented do da days... ssss""" verse space = tab or space def myfunc(): "here is my docstring that bla bla bla's"\ " and indented do da days..."\ "\n" verse the legal and sometimes seen form: def myfunc(): \tmyval = somefunc( arg0, arg1, arg2, lotsofargs, \t\tsssspaces_to_align_arg_to_arg0) If you strip the text and display it using basic reformatting in fixed font, like in the python interpreter, no biggy. But when you want to display the docstrings in an advanced font, like that of an ide with truetype fonts, formatting gets lost without having the app account for all the various ways Python coders do their docstrings. So if you wanted a consistant flexible (really) long term way to autogenerate and publish your and others code/packages/libraries, it gets tricky. Ask big-python-library maintainers know, many people go to chat and forums simply because autogenerated documention wasn't clear to the non-expert, thus repeating the same questions over and over. AND perhaps in the beginning it was well documented but since new constructs and refactoring of code, often auto-generated documentation can get "reduced" to something unuseful to those not-in- the-know. If whitespace indentation is used to define some construct of a programming language, then how people like to document their code (by using various forms of whitespace) could impact how well -some- people learn the new language. Auto-generated documention could very well be influenced by that decision. What form of whitespace indentation could effect readability/ understandable code. For example reducing indentation to 2 spaces instead of 4 might encourage some coders to use even deeper indentation because the code could fix more stuff (it's not being used up by whitespace indentation). Many feel that's a bad thing (I do too). (not knowing Unit code; I'll use Python) class Wow(object): def myfunc(self): a = func() if a: small_indent = something() if small_indent: getting_silly = godeeper() while getting_silly > a: bet_you_get_the_point += getting_silly - code_makes_no_sense() for i in range(100: if bet_you_get_the_point in SOMEGLOBAL['part1'][0]: display(a, small_indent, getting_silly, bet_you_get_the_point) Well, that may be a little hyperbolic. But with 2 spaces you can encourage coders to get very deep, indentially, and still fit 80 chars. (newly invented word: indentially. But I don't care, you get the point.) To me, each viewer of that code should be able to put the indent to something that fits their taste. And lets face it. most source code today is viewed in some gui text editor that can reinterpret that indent to whatever, without generating interperter indentation errors later. The only exception to consistancy would again be docstrings or more precisely string blocks, or block strings, or tripe quoted, whatever you want to call them. My words are not meant to garner flames or anything, just to cause thought; From jeanmichel at sequans.com Thu Dec 1 13:41:59 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 01 Dec 2011 19:41:59 +0100 Subject: platform issues? In-Reply-To: <20111201124122.8818@web003.roc2.bluetie.com> References: <20111201124122.8818@web003.roc2.bluetie.com> Message-ID: <4ED7CA77.6020405@sequans.com> Adrian Powell wrote: > I'm new to python and I'm trying to get a twitter client running on a new machine but it keeps on failing. I tracked the problem down to an issue opening URLs and wrote this little test case: > > import urllib2 > url = 'http://www.google.com/' > opener = urllib2.build_opener() > url_data = opener.open(url).read > url_data > > > When I run that on a dev machine it works fine, but when it's on one of our servers it crashes: > > >>>> import urllib2 >>>> url = 'http://www.google.com/' >>>> opener = urllib2.build_opener() >>>> url_data = opener.open(url).read >>>> > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.7/urllib2.py", line 391, in open > response = self._open(req, data) > File "/usr/lib/python2.7/urllib2.py", line 409, in _open > '_open', req) > File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain > result = func(*args) > File "/usr/lib/python2.7/urllib2.py", line 1173, in http_open > return self.do_open(httplib.HTTPConnection, req) > File "/usr/lib/python2.7/urllib2.py", line 1148, in do_open > raise URLError(err) > urllib2.URLError: > > > Our servers have the same version of python and we're running the same OS (Fedora 14). Can anyone think of what might be causing this problem, or why it works on most (but not all) machines? > > Thanks > Actually, Python did not crashed, some module raised an exception and nothing has been written to handle that exception, hence python stopping. But as you can see, python gave you a lot of meaningful informations about why it was not able to continue execution. urllib2.URLError: Your server may not be able to resolve that URL while your local dev machine can. JM From adrianp at bringtechnology.com Thu Dec 1 13:43:38 2011 From: adrianp at bringtechnology.com (Adrian Powell) Date: Thu, 01 Dec 2011 13:43:38 -0500 Subject: platform issues? Message-ID: <20111201134338.8582@web001.roc2.bluetie.com> to run the tests, I sit at my dev machine and SSH to the server where I can copy & paste the test case directly into the python interpreter. If there's a typo, it's going to both of them equally. I am concerned about the name lookup since our servers have had some network glitches in the past and I'm not confident in our DNS. We can browse the net on the server without a problem but to be sure, I tried this: On the server: >>> socket.gethostbyaddr(socket.gethostbyname('www.google.com')) ('yyz06s07-in-f20.1e100.net', [], ['74.125.226.84']) >>> socket.gethostbyaddr(socket.gethostbyname('www.twitter.com')) ('www2.twitter.com', [], ['199.59.149.198']) On the dev environment: >>> socket.gethostbyaddr(socket.gethostbyname('www.google.com')) ('sea09s02-in-f16.1e100.net', [], ['173.194.33.48']) >>> socket.gethostbyaddr(socket.gethostbyname('www.twitter.com')) ('www4.twitter.com', [], ['199.59.149.230']) Since I'm actually trying to write a twitter client, I was more focused on the results from twitter. Since google and twitter are so huge and so distributed, I'd bet neither are good tests for this, but they look approximately right. Are there better ways to test if our network setup is good? At least that would be a relatively easy thing to fix. Adrian. -----Original Message----- From: "Redcat" [redcat at catfolks.net] Date: 12/01/2011 01:05 PM To: python-list at python.org Subject: Re: platform issues? > Our servers have the same version of python and we're running the same > OS (Fedora 14). Can anyone think of what might be causing this problem, > or why it works on most (but not all) machines? > > Thanks Is the server able to resolve www.google.com properly? If so, are you POSITIVE that the "url = " line on the server has no typos? -- http://mail.python.org/mailman/listinfo/python-list From redcat at catfolks.net Thu Dec 1 14:03:45 2011 From: redcat at catfolks.net (Redcat) Date: 1 Dec 2011 19:03:45 GMT Subject: platform issues? References: Message-ID: <9jq1chFug2U10@mid.individual.net> On Thu, 01 Dec 2011 13:43:38 -0500, Adrian Powell wrote: > Since I'm actually trying to write a twitter client, I was more focused > on the results from twitter. Since google and twitter are so huge and so > distributed, I'd bet neither are good tests for this, but they look > approximately right. > > Are there better ways to test if our network setup is good? At least > that would be a relatively easy thing to fix. I think I'd first try "dig www.google.com" to be sure that it is resolving, then I believe I'd try to fetch the URL from your code sample using cUrl. If both of those work then I'm not sure what I'd look at next. From ian at excess.org Thu Dec 1 14:05:14 2011 From: ian at excess.org (Ian Ward) Date: Thu, 01 Dec 2011 14:05:14 -0500 Subject: ANN: Urwid 1.0.1, Urwid 0.9.9.3 - Console UI Library Message-ID: <4ED7CFEA.7070006@excess.org> Announcing Urwid 1.0.1 and Urwid 0.9.9.3 ---------------------------------------- Urwid home page: http://excess.org/urwid/ Manual: http://excess.org/urwid/wiki/UrwidManual Tarballs: http://excess.org/urwid/urwid-1.0.1.tar.gz http://excess.org/urwid/urwid-0.9.9.3.tar.gz About these releases: ===================== These are bug-fix releases for Urwid 1.0.0 and Urwid 0.9.9.2. Release 0.9.9.3 may be the last in the 0.9.9 series. Users are strongly encouraged to upgrade to the 1.0 series. New in 1.0.1: ============= * Fix for Terminal widget in BSD/OSX * Fix for a Filler mouse_event() position bug * Fix support for mouse positions up to x=255, y=255 * Fixes for a number of string encoding issues under Python 3 * Fix for a LineBox border __init__() parameters * Fix input input of UTF-8 in tour.py example by converting captions to unicode * Fix tutorial examples' use of TextCanvas and switch to using unicode literals * Prevent raw_display from calling tcseattr() or tcgetattr() on non-ttys * Disable curses_display external event loop support: screen resizing and gpm events are not properly supported * Mark PollingListWalker as deprecated New in 0.9.9.3: =============== * ListBox now includes a get_cursor_coords() method, allowing nested ListBox widgets * Fix for a Filler mouse_event() position bug * Fix support for mouse positions up to x=255, y=255 * Fix for leaks of None object in str_util extension * Fix for WidgetWrap and AttrMap not working with fixed widgets About Urwid =========== Urwid is a console UI library for Python. It features fluid interface resizing, Unicode support, multiple text layouts, simple attribute markup, powerful scrolling list boxes and flexible interface design. Urwid is released under the GNU LGPL. From zak.mc.kraken at libero.it Thu Dec 1 15:35:02 2011 From: zak.mc.kraken at libero.it (Vito 'ZeD' De Tullio) Date: Thu, 01 Dec 2011 21:35:02 +0100 Subject: Clever hack or code abomination? References: Message-ID: Arnaud Delobelle wrote: > On 1 December 2011 03:15, Roy Smith wrote: >> I need to try a bunch of names in sequence until I find one that works >> (definition of "works" is unimportant). The algorithm is: >> >> 1) Given a base name, "foo", first see if just plain "foo" works. >> >> 2) If not, try "foo-1", "foo-2", and so on >> >> 3) If you reach "foo-20", give up. >> >> What would you say if you saw this: >> >> for suffix in [''] + [str(i) for i in xrange(-1, -20, -1)]: >> > > It's a little obfuscated ;) I would go for the simple: > > for i in xrange(21): > suffix = "-%s" % i if i else "" > .... > obfuscated for obfuscated, you can merge the two ideas: for suffix in ('-%s' % i if i else '' for i in xrange(21)): ... -- By ZeD From adrianp at bringtechnology.com Thu Dec 1 15:35:17 2011 From: adrianp at bringtechnology.com (Adrian Powell) Date: Thu, 01 Dec 2011 15:35:17 -0500 Subject: platform issues? Message-ID: <20111201153517.4824@web002.roc2.bluetie.com> The curl & dig ideas were great, thanks. Turns out the machine's network was badly configured and that's probably what was causing this problem. Curiously firefox could browse these sites without any problem despite not having any proxy defined and it took the command line to reveal the issue. Thank you for your help, this had been bugging us for days! Adrian. -----Original Message----- From: "Redcat" [redcat at catfolks.net] Date: 12/01/2011 02:05 PM To: python-list at python.org Subject: Re: platform issues? On Thu, 01 Dec 2011 13:43:38 -0500, Adrian Powell wrote: > Since I'm actually trying to write a twitter client, I was more focused > on the results from twitter. Since google and twitter are so huge and so > distributed, I'd bet neither are good tests for this, but they look > approximately right. > > Are there better ways to test if our network setup is good? At least > that would be a relatively easy thing to fix. I think I'd first try "dig www.google.com" to be sure that it is resolving, then I believe I'd try to fetch the URL from your code sample using cUrl. If both of those work then I'm not sure what I'd look at next. -- http://mail.python.org/mailman/listinfo/python-list From martin.hellwig at gmail.com Thu Dec 1 16:13:51 2011 From: martin.hellwig at gmail.com (Martin P. Hellwig) Date: Thu, 01 Dec 2011 21:13:51 +0000 Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: On 01/12/2011 03:15, Roy Smith wrote: Well, I have seen much worse, so the WTFs/minute(*) count won't be too bad. However, as general rule for readability; If you think you have to ask, don't bother asking, spend that time rethinking and write a more readable solution. *) http://www.osnews.com/story/19266/WTFs_m -- mph From steve+comp.lang.python at pearwood.info Thu Dec 1 19:15:55 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2011 00:15:55 GMT Subject: Clever hack or code abomination? References: Message-ID: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> On Thu, 01 Dec 2011 21:13:51 +0000, Martin P. Hellwig wrote: > On 01/12/2011 03:15, Roy Smith wrote: > Well, I have seen much worse, so the WTFs/minute(*) count won't be too > bad. > > However, as general rule for readability; If you think you have to ask, > don't bother asking, spend that time rethinking and write a more > readable solution. That's generally good advice, but in this case, I don't know why Roy Smith thought he had to ask. His initial snippet was pretty much standard vanilla Python: a for loop over a list. The list was made by adding a list containing a single element '', and a second list made from a list comprehension that converted a bunch of numbers into strings. It uses no advanced features like generators. A newbie to Python could work it out. In my opinion, if anyone thinks that's "obfuscated" (as opposed to merely "not quite idiomatic"), either their standards for readability are impossibly high, or they just can't read Python code. Try this on for size. def obfuscated_prefixes(q=20): c = str() while isinstance(type(c), type(type)): yield None or c while isinstance(c, object): try: f = type(q)(c[c.index(chr(45))+1:])+type(q)(1) except ValueError: c = str.join('\n', list(map(chr, (45, 48))) + [c])[::2] else: break if f <= q: pass else: break try: c = (lambda a,b: a+b)(c[:c.index(chr(45))+1], type(c)(f)) except IndexError: c = c[1::] -- Steven From onexpadREMOVE at EVOMERyahoodotyouknow.com Thu Dec 1 19:23:22 2011 From: onexpadREMOVE at EVOMERyahoodotyouknow.com (Kyle T. Jones) Date: Thu, 01 Dec 2011 18:23:22 -0600 Subject: Complete beginner, any help appreciated :) - For Loops In-Reply-To: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Message-ID: On 12/1/11 4:53 AM, Mark wrote: > Hi there, > > I'm a complete beginner to Python and, aside from HTML and CSS, to coding in general. I've spent a few hours on it and think I understand most of the syntax. > > However, I'm wondering a bit about For Loops. I know that the basic syntax for them is to define a list, and then to use something like: > > for x in y > > However, what does "for" and "in" mean in this context? Can anyone help me to understand this? I know it's a really basic question, but hopefully it will see me on my way to coding properly :) > > Thanks a lot. y is some list ["help","beginner","question"] for x in y: if x=="question": print "FOUND" Clear? Cheers. From steve+comp.lang.python at pearwood.info Thu Dec 1 19:43:02 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2011 00:43:02 GMT Subject: Using the Python Interpreter as a Reference References: <79379487-0081-4067-92ed-c6717652e1ff@y7g2000vbe.googlegroups.com> <4eb0af60-26b3-45d5-8aff-566505003d6a@m10g2000vbc.googlegroups.com> <4ed2cddc$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed491fe$0$14018$c3e8da3$76491128@news.astraweb.com> Message-ID: <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> On Thu, 01 Dec 2011 10:03:53 -0800, DevPlayer wrote: [...] > Well, that may be a little hyperbolic. But with 2 spaces you can > encourage coders to get very deep, indentially, and still fit 80 chars. Why would you want to encourage coders to write deeply indented code? In my opinion, if your code is indented four or more levels, you should start to think about refactorising your code; if you reach six levels, your code is probably a mess. class K: def spam(): if x: for a in b: # This is about as deep as comfortable while y: # Code is starting to smell try: # Code smell is now beginning to reek with z as c: # And now more of a stench try: # A burning, painful stench if d: # Help! I can't breathe!!! for e in f: # WTF are you thinking? try: # DIE YOU M***********ER!!! while g: # gibbers quietly ... The beauty of languages like Python where indentation is significant is that you can't hide from the ugliness of this code. class K: { # Code looks okay to a casual glance. def spam():{ if x: { for a in b:{ while y:{ try:{ with z as c:{ try:{ if d:{ for e in f:{ try:{ while g:{ ... }}}} }}}} }}}} Deeply indented code *is* painful, it should *look* painful. -- Steven From rosuav at gmail.com Thu Dec 1 21:02:31 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 13:02:31 +1100 Subject: Using the Python Interpreter as a Reference In-Reply-To: <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <79379487-0081-4067-92ed-c6717652e1ff@y7g2000vbe.googlegroups.com> <4eb0af60-26b3-45d5-8aff-566505003d6a@m10g2000vbc.googlegroups.com> <4ed2cddc$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed491fe$0$14018$c3e8da3$76491128@news.astraweb.com> <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 2, 2011 at 11:43 AM, Steven D'Aprano wrote: > Why would you want to encourage coders to write deeply indented code? > > In my opinion, if your code is indented four or more levels, you should > start to think about refactorising your code; if you reach six levels, > your code is probably a mess. So... would it be a bad thing to wrap up all my code into a single massive expression that returns a single integer for success/failure? Oh wait, the only time I ever saw code do that was in the IOCCC. Still, it WAS pretty awesome code! IOCCC is a code perfume factory. Cloying smell. ChrisA From rosuav at gmail.com Thu Dec 1 21:07:57 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 13:07:57 +1100 Subject: Clever hack or code abomination? In-Reply-To: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 2, 2011 at 11:15 AM, Steven D'Aprano wrote: > Try this on for size. > > > ? ? ? ? ? ? ? ?f = type(q)(c[c.index(chr(45))+1:])+type(q)(1) > ? ? ? ? ? ? ? ?c = str.join('\n', list(map(chr, (45, 48))) + [c])[::2] > ? ? ? ? ? ?c = (lambda a,b: a+b)(c[:c.index(chr(45))+1], type(c)(f)) I would consider integer representations of ASCII to be code smell. It's not instantly obvious that 45 means '-', even if you happen to know the ASCII table by heart (which most people won't). This is one thing that I like about C's quote handling; double quotes for a string, or single quotes for an integer with that character's value. It's clearer than the Python (and other) requirement to have an actual function call: for (int i=0;i<10;++i) { digit[i]='0'+i; letter[i]='A'+i; } versus for i in range(10): digit[i]=chr(ord('0')+i) letter[i]=chr(ord('A')+i) Ignoring the fact that you'd probably use a list comp in Python, this is imho a win for C. ChrisA From drsalists at gmail.com Thu Dec 1 21:46:10 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Thu, 1 Dec 2011 18:46:10 -0800 Subject: Reading twice from STDIN In-Reply-To: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> Message-ID: On 12/1/11, janedenone wrote: > Hi, > > I would like to read from a pipe, parse the input and ask the user > what to do next: > > message = sys.stdin.read() > # message is parsed and URLs are printed as a list to choose from... > selected_index = raw_input('Which URL to open?') > > Calling raw_input() always raises in an EOFError. I tried reopening > and resetting sys.stdin, but nothing worked (at least on OX X 10.7, > see > You can read piped data from sys.stdin normally. Then if you want something from the user, at least on most *ix's, you would open /dev/tty and get user input from there. 'Not sure about OS/X. http://stackoverflow.com/questions/8034595/python-raw-input-following-sys-stdin-read-throws-eoferror). > > I am surprised to find that a seemingly trivial task cannot be > accomplished with Python 2.7 on a current Mac. Or am I missing > something simple? > > - Jan > -- > http://mail.python.org/mailman/listinfo/python-list > From tjreedy at udel.edu Thu Dec 1 21:49:07 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 01 Dec 2011 21:49:07 -0500 Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: On 11/30/2011 10:49 PM, Matt Joiner wrote: > def possible_names(): > yield "foo" > for i in range(20): > yield "foo-" + str(i) This is my favorite -- crystal clear with only the variable part variable. And it works in both 2.x and 3.x. -- Terry Jan Reedy From python.list at tim.thechases.com Thu Dec 1 23:02:18 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 01 Dec 2011 22:02:18 -0600 Subject: Passing along cmd.Cmd completion to contained class Message-ID: <4ED84DCA.2040802@tim.thechases.com> I have sub-classes of cmd.Cmd in an arrangement somewhat like class Config(cmd.Cmd): def do_foo(self, line): print "Fooing %r" % line def complete_foo(self, text, line, begidx, endidx): ... class MyTUI(cmd.Cmd): def __init__(self, configger=Config, *args, **kwargs): cmd.Cmd.__init__(self, *args, **kwargs) self.config = configger() def complete_config(self, text, line, begidx, endidx): magic_here(self.config, text, line, begidx, endidx) I've been sparring with getting MyTUI.complete_config() to reach into self.config for completions so I can type things like (cmd) config (cmd) config foo and have it suggest from Config as if I had done something like def do_config(self, line) self.config.cmdloop() and then asked for completions. That would mean that the first one would act like Config.completenames() and the second one would act like Config.complete_foo(...) Is there a best way to get this pass-along behavior? Thanks, -tkc From dihedral88888 at googlemail.com Thu Dec 1 23:29:40 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 20:29:40 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> Message-ID: <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> On Friday, December 2, 2011 12:18:29 AM UTC+8, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 2:52 AM, Dave Angel wrote: > > On 12/01/2011 10:35 AM, 88888 Dihedral wrote: > >> I knew a hash can replace a bi-directional linked list. > >> The value ?can be a multi-field string ?to be parsed for further actions. > >> Is this what you are asking? > > > > A hash is a number, so I don't see how it can replace any kind of linked > > list. ?Perhaps you're thinking of some other language. > > A hashtable is a form of data structure that involves hashing values > (ie calculating hashes for them) and storing them for easier retrieval > than a simple linked list offers. That may be what you're thinking of. > > ChrisA I clear my point a hash is a collection of (key, value) pairs that have well defined methods and behavior to be used in programming. The basic operations of a hash normally includes the following: 1. insertion of a (key, value) pair into the hash 2. deletion of a (key, value) from the hash 3. inquiring a hash by a key to retrieve the value if the (key, value) pair available in the hash. If no key matched, the hash will return a not found result. The hash can grow with (k,v) pairs accumulated in the run time. An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. Some implementations of a hash might pose some restrictions of k and v for some reasons. But in object programming k and v can be objects to be manipulated by the programmer. From dihedral88888 at googlemail.com Thu Dec 1 23:29:40 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 20:29:40 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> Message-ID: <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> On Friday, December 2, 2011 12:18:29 AM UTC+8, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 2:52 AM, Dave Angel wrote: > > On 12/01/2011 10:35 AM, 88888 Dihedral wrote: > >> I knew a hash can replace a bi-directional linked list. > >> The value ?can be a multi-field string ?to be parsed for further actions. > >> Is this what you are asking? > > > > A hash is a number, so I don't see how it can replace any kind of linked > > list. ?Perhaps you're thinking of some other language. > > A hashtable is a form of data structure that involves hashing values > (ie calculating hashes for them) and storing them for easier retrieval > than a simple linked list offers. That may be what you're thinking of. > > ChrisA I clear my point a hash is a collection of (key, value) pairs that have well defined methods and behavior to be used in programming. The basic operations of a hash normally includes the following: 1. insertion of a (key, value) pair into the hash 2. deletion of a (key, value) from the hash 3. inquiring a hash by a key to retrieve the value if the (key, value) pair available in the hash. If no key matched, the hash will return a not found result. The hash can grow with (k,v) pairs accumulated in the run time. An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. Some implementations of a hash might pose some restrictions of k and v for some reasons. But in object programming k and v can be objects to be manipulated by the programmer. From steve+comp.lang.python at pearwood.info Thu Dec 1 23:33:57 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2011 04:33:57 GMT Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> Message-ID: <4ed85535$0$29988$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Dec 2011 03:18:29 +1100, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 2:52 AM, Dave Angel wrote: >> On 12/01/2011 10:35 AM, 88888 Dihedral wrote: >>> I knew a hash can replace a bi-directional linked list. The value ?can >>> be a multi-field string ?to be parsed for further actions. Is this >>> what you are asking? >> >> A hash is a number, so I don't see how it can replace any kind of >> linked list. ?Perhaps you're thinking of some other language. > > A hashtable is a form of data structure that involves hashing values (ie > calculating hashes for them) and storing them for easier retrieval than > a simple linked list offers. That may be what you're thinking of. Python dicts are hash tables. -- Steven From rosuav at gmail.com Fri Dec 2 00:00:10 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 16:00:10 +1100 Subject: order independent hash? In-Reply-To: <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: On Fri, Dec 2, 2011 at 3:29 PM, 88888 Dihedral wrote: > I clear my point a hash is a collection of (key, value) pairs that have > well defined methods and behavior to be used in programming. > > The basic operations of a hash normally includes the following: > > 1. insertion of a (key, value) pair ?into the hash > 2. deletion of a (key, value) from the hash > 3. inquiring ?a hash by a key to retrieve the value if the (key, value) > pair available in the hash. If no key matched, the hash will return > a not found result. > > The hash can grow with (k,v) pairs accumulated in the run time. > An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. That's a hash table - think of a Python dictionary: On Fri, Dec 2, 2011 at 3:33 PM, Steven D'Aprano wrote: > Python dicts are hash tables. Although strictly speaking, isn't that "Python dicts are implemented as hash tables in CPython"? Or is the hashtable implementation mandated? Anyway, near enough. Cryptography and data verification use hashing too (look at the various historic hashing algorithms - CRC, MD5, SHA, etc). The concept of a hash is a number (usually of a fixed size) that is calculated from a string or other large data type, such that hashing the same input will always give the same output, but hashing different input will usually give different output. It's then possible to identify a large object solely by its hash, as is done in git, for instance; or to transmit both the data and the hash, as is done in message protection schemes (many archiving programs/formats include a hash of the uncompressed data). These have nothing to do with (key,value) pairs, but are important uses of hashes. ChrisA From anacrolix at gmail.com Fri Dec 2 00:11:06 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Fri, 2 Dec 2011 16:11:06 +1100 Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: Thank you. ?_? On Fri, Dec 2, 2011 at 1:49 PM, Terry Reedy wrote: > On 11/30/2011 10:49 PM, Matt Joiner wrote: >> >> def possible_names(): >> ? ? yield "foo" >> ? ? for i in range(20): >> ? ? ? ? yield "foo-" + str(i) > > > This is my favorite -- crystal clear with only the variable part variable. > And it works in both 2.x and 3.x. > > -- > Terry Jan Reedy > > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Fri Dec 2 00:34:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2011 05:34:16 GMT Subject: Clever hack or code abomination? References: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ed86358$0$29988$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Dec 2011 13:07:57 +1100, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 11:15 AM, Steven D'Aprano > wrote: >> Try this on for size. >> >> >> ? ? ? ? ? ? ? ?f = type(q)(c[c.index(chr(45))+1:])+type(q)(1) c >> ? ? ? ? ? ? ? ?= str.join('\n', list(map(chr, (45, 48))) + >> ? ? ? ? ? ? ? ?[c])[::2] >> ? ? ? ? ? ?c = (lambda a,b: a+b)(c[:c.index(chr(45))+1], >> ? ? ? ? ? ?type(c)(f)) > > I would consider integer representations of ASCII to be code smell. It's > not instantly obvious that 45 means '-', even if you happen to know the > ASCII table by heart (which most people won't). This is one thing that I > like about C's quote handling; double quotes for a string, or single > quotes for an integer with that character's value. It's clearer than the > Python (and other) requirement to have an actual function call: > > for (int i=0;i<10;++i) { > digit[i]='0'+i; > letter[i]='A'+i; > } I would disagree that this is clear at all. You're adding what looks like a character, but is actually an integer, with an integer. And then just to add insult to injury, you're storing integers into arrays that are named as if they were characters. In what mad universe would you describe 65 as a letter? To say nothing of the fact that C's trick only works (for some definition of works) for ASCII. Take for example one of the many EBCDIC encodings, cp500. If you expect 'I' + 1 to equal 'J', you will be sorely disappointed: py> u'I'.encode('cp500') '\xc9' py> u'J'.encode('cp500') '\xd1' Characters are not integers, and C conflates them, to the disservice of all. If fewer people learned C, fewer people would have such trouble understanding Unicode. Anyone unfamiliar with C's model would have trouble guessing what 'A' + 1 should mean. Should it be? - an error - 'B' - 'A1' - the numeric value of variable A plus 1 - 66 (assuming ascii encoding) - 194 (assuming cp500 encoding) - some other number - something else? How about 1000 + 'A'? > versus > > for i in range(10): > digit[i]=chr(ord('0')+i) > letter[i]=chr(ord('A')+i) It's a tad more verbose, but it's explicit about what is being done. Take the character '0', find out what ordinal value it encodes to, add 1 to that value, re-encode back to a character. That's exactly what C does, only it does it explicitly. Note that this still doesn't work the way we might like in EBCDIC, but the very fact that you are forced to think about explicit conversion steps means you are less likely to make unwarranted assumptions about what characters convert to. Better than both, I would say, would be for string objects to have successor and predecessor methods, that skip ahead (or back) the specified number of code points (defaulting to 1): 'A'.succ() => 'B' 'A'.succ(5) => 'F' with appropriate exceptions if you try to go below 0 or above the largest code point. -- Steven From dihedral88888 at googlemail.com Fri Dec 2 00:48:27 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 21:48:27 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: <1576053.251.1322804908048.JavaMail.geo-discussion-forums@pruu5> On Friday, December 2, 2011 1:00:10 PM UTC+8, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 3:29 PM, 88888 Dihedral > wrote: > > I clear my point a hash is a collection of (key, value) pairs that have > > well defined methods and behavior to be used in programming. > > > > The basic operations of a hash normally includes the following: > > > > 1. insertion of a (key, value) pair ?into the hash > > 2. deletion of a (key, value) from the hash > > 3. inquiring ?a hash by a key to retrieve the value if the (key, value) > > pair available in the hash. If no key matched, the hash will return > > a not found result. > > > > The hash can grow with (k,v) pairs accumulated in the run time. > > An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > That's a hash table - think of a Python dictionary: > > On Fri, Dec 2, 2011 at 3:33 PM, Steven D'Aprano > wrote: > > Python dicts are hash tables. > > Although strictly speaking, isn't that "Python dicts are implemented > as hash tables in CPython"? Or is the hashtable implementation > mandated? Anyway, near enough. > > Cryptography and data verification use hashing too (look at the > various historic hashing algorithms - CRC, MD5, SHA, etc). The concept > of a hash is a number (usually of a fixed size) that is calculated > from a string or other large data type, such that hashing the same > input will always give the same output, but hashing different input > will usually give different output. It's then possible to identify a > large object solely by its hash, as is done in git, for instance; or > to transmit both the data and the hash, as is done in message > protection schemes (many archiving programs/formats include a hash of > the uncompressed data). These have nothing to do with (key,value) > pairs, but are important uses of hashes. > > ChrisA If one tries to insert a (k,v1) and then a (k,v2) pair into a hash with v1 not equals V2, what could happen in your understanding of a hash? A hash function is different from a hash or so called a hash table in my post. If the hash collision rate is not specified, then it is trivial to write a hash function with the conditions you specified. A hash function applied to a set of data items only is of very limited use at all. A hash stores (k,v) pairs specified in the run time with auto memory management build in is not a simple hash function to produce data signatures only clearly in my post. What I said a hash which is lifted as a basic type in python is called a dictionary in python. It is called a map in c++'s generics library. From dihedral88888 at googlemail.com Fri Dec 2 00:48:27 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 21:48:27 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: <1576053.251.1322804908048.JavaMail.geo-discussion-forums@pruu5> On Friday, December 2, 2011 1:00:10 PM UTC+8, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 3:29 PM, 88888 Dihedral > wrote: > > I clear my point a hash is a collection of (key, value) pairs that have > > well defined methods and behavior to be used in programming. > > > > The basic operations of a hash normally includes the following: > > > > 1. insertion of a (key, value) pair ?into the hash > > 2. deletion of a (key, value) from the hash > > 3. inquiring ?a hash by a key to retrieve the value if the (key, value) > > pair available in the hash. If no key matched, the hash will return > > a not found result. > > > > The hash can grow with (k,v) pairs accumulated in the run time. > > An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > That's a hash table - think of a Python dictionary: > > On Fri, Dec 2, 2011 at 3:33 PM, Steven D'Aprano > wrote: > > Python dicts are hash tables. > > Although strictly speaking, isn't that "Python dicts are implemented > as hash tables in CPython"? Or is the hashtable implementation > mandated? Anyway, near enough. > > Cryptography and data verification use hashing too (look at the > various historic hashing algorithms - CRC, MD5, SHA, etc). The concept > of a hash is a number (usually of a fixed size) that is calculated > from a string or other large data type, such that hashing the same > input will always give the same output, but hashing different input > will usually give different output. It's then possible to identify a > large object solely by its hash, as is done in git, for instance; or > to transmit both the data and the hash, as is done in message > protection schemes (many archiving programs/formats include a hash of > the uncompressed data). These have nothing to do with (key,value) > pairs, but are important uses of hashes. > > ChrisA If one tries to insert a (k,v1) and then a (k,v2) pair into a hash with v1 not equals V2, what could happen in your understanding of a hash? A hash function is different from a hash or so called a hash table in my post. If the hash collision rate is not specified, then it is trivial to write a hash function with the conditions you specified. A hash function applied to a set of data items only is of very limited use at all. A hash stores (k,v) pairs specified in the run time with auto memory management build in is not a simple hash function to produce data signatures only clearly in my post. What I said a hash which is lifted as a basic type in python is called a dictionary in python. It is called a map in c++'s generics library. From rosuav at gmail.com Fri Dec 2 01:02:01 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 17:02:01 +1100 Subject: Clever hack or code abomination? In-Reply-To: <4ed86358$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed86358$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 2, 2011 at 4:34 PM, Steven D'Aprano wrote: > On Fri, 02 Dec 2011 13:07:57 +1100, Chris Angelico wrote: >> I would consider integer representations of ASCII to be code smell. It's >> not instantly obvious that 45 means '-', even if you happen to know the >> ASCII table by heart (which most people won't). Note, I'm not saying that C's way is perfect; merely that using the integer 45 to represent a hyphen is worse. > In what mad universe would you describe > 65 as a letter? I dunno, this universe looks pretty mad from where I am... wait, where am I? Oh, right. Rutledge's Private Clinic... nice soft walls they have here... > To say nothing of the fact that C's trick only works (for some definition > of works) for ASCII. Take for example one of the many EBCDIC encodings, > cp500. If you expect 'I' + 1 to equal 'J', you will be sorely > disappointed: > > py> u'I'.encode('cp500') > '\xc9' > py> u'J'.encode('cp500') > '\xd1' Nothing to do with C, this is a feature of ASCII. Anything involving arithmetic on ordinals depends on your encoding, regardless of conflation of int and char. > Anyone unfamiliar with C's model would have trouble guessing what 'A' + 1 > should mean. Should it be? > > - ?an error > - ?'B' > - ?'A1' > - ?the numeric value of variable A plus 1 > - ?66 ?(assuming ascii encoding) > - ?194 ?(assuming cp500 encoding) > - ?some other number > - ?something else? Agreed. But implicit casting is both a minefield and a source of immense amounts of clarity. Imagine if you couldn't implicitly cast int to float - it'd stop you from losing precision on large integers, but it would get in the way any time you want to work with integers and floating point together. > Note that this still doesn't work the way we might like in EBCDIC, but > the very fact that you are forced to think about explicit conversion > steps means you are less likely to make unwarranted assumptions about > what characters convert to. I don't know about that. Anyone brought up on ASCII and moving to EBCDIC will likely have trouble with this, no matter how many function calls it takes. > Better than both, I would say, would be for string objects to have > successor and predecessor methods, that skip ahead (or back) the > specified number of code points (defaulting to 1): > > 'A'.succ() ?=> 'B' > 'A'.succ(5) ?=> 'F' > > with appropriate exceptions if you try to go below 0 or above the largest > code point. ... and this still has that same issue. Arithmetic on codepoints depends on that. I'd be fine with a simple syntax that gives a Unicode codepoint, rather than an ASCII one; at least that's standardized. Being able to work with characters as though they're integers is a huge advantage in low-level code, but not so vital in Python. But if you're going to do it, you may as well do it without all the syntactic salt of explicit conversion functions. ChrisA From hansmu at xs4all.nl Fri Dec 2 02:53:08 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 02 Dec 2011 08:53:08 +0100 Subject: Reading twice from STDIN In-Reply-To: References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> Message-ID: <4ed883e4$0$6920$e4fe514c@news2.news.xs4all.nl> On 2/12/11 03:46:10, Dan Stromberg wrote: > You can read piped data from sys.stdin normally. Then if you want > something from the user, at least on most *ix's, you would open > /dev/tty and get user input from there. 'Not sure about OS/X. Reading from /dev/tty works fine on OS/X. -- HansM From durumdara at gmail.com Fri Dec 2 03:13:43 2011 From: durumdara at gmail.com (durumdara) Date: Fri, 2 Dec 2011 00:13:43 -0800 (PST) Subject: Python 3 - xml - crlf handling problem References: <3aae0b18-a194-444f-a2fc-da156204bd95@20g2000yqa.googlegroups.com> Message-ID: <6cc21b82-aa31-47d2-8510-5d629b6c12f2@t16g2000vba.googlegroups.com> Dear Stefan! So: may I don't understand the things well, but I thought that parser drop the "nondata" CRLF-s + other characters (not preserve them). Then don't matters that I read the XML from a file, or I create it from code, because all of them generating SAME RESULT. But Python don't do that. If I make xml from code, the code is without plus characters. But Python preserves parsed CRLF characters somewhere, and they are also flushing into the result. Example: original=''' AnyText ''' If I parse this, and write with toxml, the CRLF-s remaining in the code, but if I create this document line by line, there is no CRLF, the toxml write "only lined" xml. This also meaning that if I use prettyxml call, to prettying the xml, the file size is growing. If there is a multiple processing queue - if two pythons communicating in xml files, the size can growing every time. Py1 - read the Py2's file, process it, and write to a result file Py2 - read the Py1's result file, process it, and pass back to Py1 this can grow the file with each call, because "pretty" CRLF-s not normalized out from the code. original=''' AnyText ''' def main(): f = open('test.0.xml','w') f.write(original.strip()) f.close() for i in range(1, 10 + 1): xo = parse('test.%d.xml' % (i - 1)) de = xo.documentElement de.setAttribute('c', str(i)) t = de.getElementsByTagName('element')[0] tn = t.childNodes[0] print (dir(t)) print (tn) print (tn.nodeValue) tn.nodeValue = str(i) + '\t' + '\n' #s = xo.toxml() s = xo.toprettyxml() f = open('test.%d.xml' % i,'w') f.write(s) f.close() sys.exit() And: because Python is not converting CRLF to &013; I cannot make different from "prettied source's CRLF" (loaded from template file), "my own pretty's CRLF" (my own topretty), and really contained CRLF (for example a memo field's value). My case is that the processor application (for whom I pass the XML from Python) is sensitive to "plus CRLF"-s in text nodes, I must do something these "plus" items to avoid external's program errors. I got these templates and input files from prettied format (with CRLFS), but I must "eat" them to make an XML that one lined if possible. I hope you understand my problem with it. Thanks: dd From steve+comp.lang.python at pearwood.info Fri Dec 2 03:54:24 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2011 08:54:24 GMT Subject: Clever hack or code abomination? References: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed86358$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ed8923f$0$29988$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Dec 2011 17:02:01 +1100, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 4:34 PM, Steven D'Aprano > wrote: >> On Fri, 02 Dec 2011 13:07:57 +1100, Chris Angelico wrote: >>> I would consider integer representations of ASCII to be code smell. >>> It's not instantly obvious that 45 means '-', even if you happen to >>> know the ASCII table by heart (which most people won't). > > Note, I'm not saying that C's way is perfect; merely that using the > integer 45 to represent a hyphen is worse. Dude, it was deliberately obfuscated code. I even named the function "obfuscated_prefixes". I thought that would have been a hint It's kinda scary that of all the sins against readability committed in my function, including isinstance(type(c), type(type)) which I was particularly proud of, the only criticism you came up with was that chr(45) is hard to read. I'm impressed [...] >> Note that this still doesn't work the way we might like in EBCDIC, but >> the very fact that you are forced to think about explicit conversion >> steps means you are less likely to make unwarranted assumptions about >> what characters convert to. > > I don't know about that. Anyone brought up on ASCII and moving to EBCDIC > will likely have trouble with this, no matter how many function calls it > takes. Of course you will, because EBCDIC is a pile of festering garbage :) But IMAO you're less likely to have trouble with with Unicode if you haven't been trained to treat characters as synonymous with integers. And besides, given how rare such byte-manipulations on ASCII characters are in Python, it would be a shame to lose the ability to use '' and "" for strings just to avoid calling ord and chr functions. >> Better than both, I would say, would be for string objects to have >> successor and predecessor methods, that skip ahead (or back) the >> specified number of code points (defaulting to 1): >> >> 'A'.succ() ?=> 'B' >> 'A'.succ(5) ?=> 'F' >> >> with appropriate exceptions if you try to go below 0 or above the >> largest code point. > > ... and this still has that same issue. Arithmetic on codepoints depends > on that. We shouldn't be doing arithmetic on code points. Or at least we shouldn't unless we are writing a Unicode library that *needs* to care about the implementation. We should only care about the interface, that the character after 'A' is 'B'. Implementation-wise, we shouldn't care whether A and B are represented in memory by 0x0041 and 0x0042, or by 0x14AF and 0x9B30. All we really need to know is that B comes immediately after A. Everything else is implementation. But I fear that the idea of working with chr and ord is far to ingrained now to get rid of it. -- Steven From janedenone at googlemail.com Fri Dec 2 04:09:17 2011 From: janedenone at googlemail.com (janedenone) Date: Fri, 2 Dec 2011 01:09:17 -0800 (PST) Subject: Reading twice from STDIN References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> <4ed883e4$0$6920$e4fe514c@news2.news.xs4all.nl> Message-ID: <321b6b8b-16b2-433e-9bfc-b5ec22e77ff6@i6g2000vbe.googlegroups.com> On Dec 2, 8:53?am, Hans Mulder wrote: > On 2/12/11 03:46:10, Dan Stromberg wrote: > > > You can read piped data from sys.stdin normally. ?Then if you want > > something from the user, at least on most *ix's, you would open > > /dev/tty and get user input from there. ?'Not sure about OS/X. > > Reading from /dev/tty works fine on OS/X. > > -- HansM Many thanks for the pointers ? I had tried sys.stdin = open('/dev/tty', 'r') but the actual solution is slightly more complicated. With your help (and another round of Google searches), I found exactly what I was looking for: http://turambar.org/jarkko/stdin_input.php Because raw_input() (input() in Python 3) reads from file descriptor 0, the solution is to copy this file descriptor elsewhere, and use a file descriptor pointing to /dev/tty for the user input. Thanks again - Jan From rosuav at gmail.com Fri Dec 2 04:16:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 20:16:55 +1100 Subject: Clever hack or code abomination? In-Reply-To: <4ed8923f$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed86358$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed8923f$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 2, 2011 at 7:54 PM, Steven D'Aprano wrote: > Dude, it was deliberately obfuscated code. I even named the function > "obfuscated_prefixes". I thought that would have been a hint > > It's kinda scary that of all the sins against readability committed in my > function, including isinstance(type(c), type(type)) which I was > particularly proud of, the only criticism you came up with was that > chr(45) is hard to read. I'm impressed Heh! I know it was obfuscated, and that's why I didn't bother mentioning the obvious. > And besides, given how rare such byte-manipulations on ASCII characters > are in Python, it would be a shame to lose the ability to use '' and "" > for strings just to avoid calling ord and chr functions. Agreed. That flexibility is a _huge_ advantage. Definitely not worth changing anything. ChrisA From hniksic at xemacs.org Fri Dec 2 04:53:47 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Fri, 02 Dec 2011 10:53:47 +0100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: <87k46fb8pg.fsf@xemacs.org> Chris Angelico writes: >> The hash can grow with (k,v) pairs accumulated in the run time. >> An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > That's a hash table In many contexts "hash table" is shortened to "hash" when there is no ambiguity. This is especially popular among Perl programmers where the equivalent of dict is called a hash. > Although strictly speaking, isn't that "Python dicts are implemented > as hash tables in CPython"? Or is the hashtable implementation > mandated? It's pretty much mandated because of the __hash__ protocol. From a24061 at ducksburg.com Fri Dec 2 05:30:13 2011 From: a24061 at ducksburg.com (Adam Funk) Date: Fri, 02 Dec 2011 10:30:13 +0000 Subject: suppressing bad characters in output PCDATA (converting JSON to XML) References: <91j4q8xgv9.ln2@news.ducksburg.com> Message-ID: On 2011-11-29, Stefan Behnel wrote: > Adam Funk, 29.11.2011 13:57: >> On 2011-11-28, Stefan Behnel wrote: >>> If the name "big_json" is supposed to hint at a large set of data, you may >>> want to use something other than minidom. Take a look at the >>> xml.etree.cElementTree module instead, which is substantially more memory >>> efficient. >> >> Well, the input file in this case contains one big JSON list of >> reasonably sized elements, each of which I'm turning into a separate >> XML file. The output files range from 600 to 6000 bytes. > > It's also substantially easier to use, but if your XML writing code works > already, why change it. That module looks useful --- thanks for the tip. (TBH, I'm using minidom mainly because I've used it before and the API is similar to the DOM APIs I've used in other languages.) > You should read up on Unicode a bit. It wouldn't do me any harm. :-) >>>> I thought this would force all the output to be valid, but xmlstarlet >>>> gives some errors like these on a few documents: >>>> >>>> PCDATA invalid Char value 7 >>>> PCDATA invalid Char value 31 >>> >>> This strongly hints at a broken encoding, which can easily be triggered by >>> your erroneous encode-and-encode cycles above. >> >> No, I've checked the JSON input and those exact control characters are >> there too. > > Ah, right, I didn't look closely enough. Those are forbidden in XML: > > http://www.w3.org/TR/REC-xml/#charsets > > It's sad that minidom (apparently) lets them pass through without even a > warning. Yes, it is! I've now found this, which seems to fix the problem: http://bitkickers.blogspot.com/2011/05/stripping-control-characters-in-python.html -- The internet is quite simply a glorious place. Where else can you find bootlegged music and films, questionable women, deep seated xenophobia and amusing cats all together in the same place? [Tom Belshaw] From stefan_ml at behnel.de Fri Dec 2 06:23:54 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 02 Dec 2011 12:23:54 +0100 Subject: Python 3 - xml - crlf handling problem In-Reply-To: <6cc21b82-aa31-47d2-8510-5d629b6c12f2@t16g2000vba.googlegroups.com> References: <3aae0b18-a194-444f-a2fc-da156204bd95@20g2000yqa.googlegroups.com> <6cc21b82-aa31-47d2-8510-5d629b6c12f2@t16g2000vba.googlegroups.com> Message-ID: durumdara, 02.12.2011 09:13: > So: may I don't understand the things well, but I thought that parser > drop the "nondata" CRLF-s + other characters (not preserve them). Well, it does that, at least on my side (which is not under Windows): =================== original=''' AnyText ''' from xml.dom.minidom import parse def main(): f = open('test.0.xml', 'wb') f.write(original.strip().replace('\n', '\r\n').encode('utf8')) f.close() xo = parse('test.0.xml') de = xo.documentElement print(repr(de.childNodes[0].nodeValue)) print(repr(de.childNodes[1].childNodes[0].nodeValue)) if __name__ == '__main__': main() =================== This prints '\n ' and '\n AnyText\n ' on my side, so the whitespace normalisation in the parser properly did its work. > Then don't matters that I read the XML from a file, or I create it > from code, because all of them generating SAME RESULT. > But Python don't do that. > If I make xml from code, the code is without plus characters. What do you mean by "plus characters"? It's not the "+" character that you are referring to, right? Do you mean additional characters? Such as the additional '\r'? > But Python preserves parsed CRLF characters somewhere, and they are > also flushing into the result. > > Example: > > original=''' > > > > AnyText > > > ''' > > If I parse this, and write with toxml, the CRLF-s remaining in the > code, but if I create this document line by line, there is no CRLF, > the toxml write "only lined" xml. > > This also meaning that if I use prettyxml call, to prettying the xml, > the file size is growing. > > If there is a multiple processing queue - if two pythons communicating > in xml files, the size can growing every time. > > Py1 - read the Py2's file, process it, and write to a result file > Py2 - read the Py1's result file, process it, and pass back to Py1 > this can grow the file with each call, because "pretty" CRLF-s not > normalized out from the code. > > original=''' > > > > AnyText > > > ''' > > def main(): > f = open('test.0.xml','w') > f.write(original.strip()) > f.close() > > for i in range(1, 10 + 1): > xo = parse('test.%d.xml' % (i - 1)) > de = xo.documentElement > de.setAttribute('c', str(i)) > t = de.getElementsByTagName('element')[0] > tn = t.childNodes[0] > print (dir(t)) > print (tn) > print (tn.nodeValue) > tn.nodeValue = str(i) + '\t' + '\n' > #s = xo.toxml() > s = xo.toprettyxml() > f = open('test.%d.xml' % i,'w') > f.write(s) > f.close() > > sys.exit() > > And: because Python is not converting CRLF to&013; I cannot make > different from "prettied source's CRLF" (loaded from template file), > "my own pretty's CRLF" (my own topretty), and really contained CRLF > (for example a memo field's value). > > My case is that the processor application (for whom I pass the XML > from Python) is sensitive to "plus CRLF"-s in text nodes, I must do > something these "plus" items to avoid external's program errors. > > I got these templates and input files from prettied format (with > CRLFS), but I must "eat" them to make an XML that one lined if > possible. > > I hope you understand my problem with it. Still not quite, but never mind. May or may not be a problem in minidom or your code. For example, you shouldn't open the output file in text mode but in binary mode (i.e. "wb") because you are writing bytes into it. Here's what I tried with ElementTree, and it seems to do what your code above wants. The indent() function is taken from Fredrik's element lib page: http://effbot.org/zone/element-lib.htm ======================== original=''' AnyText ''' def indent(elem, level=0): i = "\n" + level*" " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: indent(elem, level+1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i def main(): f = open('test.0.xml','w', encoding='utf8') f.write(original.strip()) f.close() from xml.etree.cElementTree import parse for i in range(10): tree = parse('test.%d.xml' % i) root = tree.getroot() root.set('c', str(i+1)) t = root.find('.//element') t.text = '%d\t\n' % (i+1) indent(root) tree.write('test.%d.xml' % (i+1), encoding='utf8') if __name__ == '__main__': main() ======================== Hope that helps, Stefan From dihedral88888 at googlemail.com Fri Dec 2 06:48:17 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 03:48:17 -0800 (PST) Subject: order independent hash? In-Reply-To: <87k46fb8pg.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> Message-ID: <24346295.348.1322826497244.JavaMail.geo-discussion-forums@prfi36> On Friday, December 2, 2011 5:53:47 PM UTC+8, Hrvoje Niksic wrote: > Chris Angelico writes: > > >> The hash can grow with (k,v) pairs accumulated in the run time. > >> An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > > > That's a hash table > > In many contexts "hash table" is shortened to "hash" when there is no > ambiguity. This is especially popular among Perl programmers where the > equivalent of dict is called a hash. > > > Although strictly speaking, isn't that "Python dicts are implemented > > as hash tables in CPython"? Or is the hashtable implementation > > mandated? > > It's pretty much mandated because of the __hash__ protocol. A hash table definitely can replace a bi-directional list. But this is for applications or languages like C. In python hash is a build in type. Perl's lazy way of programming is famous. From hansmu at xs4all.nl Fri Dec 2 08:04:42 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 02 Dec 2011 14:04:42 +0100 Subject: Reading twice from STDIN In-Reply-To: <321b6b8b-16b2-433e-9bfc-b5ec22e77ff6@i6g2000vbe.googlegroups.com> References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> <4ed883e4$0$6920$e4fe514c@news2.news.xs4all.nl> <321b6b8b-16b2-433e-9bfc-b5ec22e77ff6@i6g2000vbe.googlegroups.com> Message-ID: <4ed8cceb$0$6931$e4fe514c@news2.news.xs4all.nl> On 2/12/11 10:09:17, janedenone wrote: > I had tried > > sys.stdin = open('/dev/tty', 'r') That seems to work for me. This code: import sys if sys.version_info.major == 2: input = raw_input for tp in enumerate(sys.stdin): print("%d: %s" % tp) sys.stdin = open('/dev/tty', 'r') answer = input('What is the carrying capacity of a swallow? ') print("You answered: %s" % answer) print("File descriptor is %d" % sys.stdin.fileno()) ... does what I expect it to do in both Python 2.7 and Python 3.2. > but the actual solution is slightly more complicated. With your help > (and another round of Google searches), I found exactly what I was > looking for: > > http://turambar.org/jarkko/stdin_input.php > > Because raw_input() (input() in Python 3) reads from file descriptor > 0, the solution is to copy this file descriptor elsewhere, and use a > file descriptor pointing to /dev/tty for the user input. That's odd. For some reason, I can get away with a simple sys.stdin = open('/dev/tty') and raw_input will merrily read from file descriptor 3. I'm using Pyhton 2.7.1 and 3.2 on MacOS/X 10.5.0. What version are you using? -- HansM From miki.tebeka at gmail.com Fri Dec 2 10:01:40 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Fri, 2 Dec 2011 07:01:40 -0800 (PST) Subject: Passing along cmd.Cmd completion to contained class In-Reply-To: References: Message-ID: <1593725.176.1322838100582.JavaMail.geo-discussion-forums@yqmd7> This is not tested, but maybe it'll work :) def complete(self, text, state): if text.startswith('config): return self.config.complete(text, state) return Cmd.complete(self, text, state) From miki.tebeka at gmail.com Fri Dec 2 10:01:40 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Fri, 2 Dec 2011 07:01:40 -0800 (PST) Subject: Passing along cmd.Cmd completion to contained class In-Reply-To: References: Message-ID: <1593725.176.1322838100582.JavaMail.geo-discussion-forums@yqmd7> This is not tested, but maybe it'll work :) def complete(self, text, state): if text.startswith('config): return self.config.complete(text, state) return Cmd.complete(self, text, state) From mbadoiu at gmail.com Fri Dec 2 10:05:49 2011 From: mbadoiu at gmail.com (Mihai Badoiu) Date: Fri, 2 Dec 2011 10:05:49 -0500 Subject: Multiprocessing: killing children when parent dies Message-ID: In the multiprocessing module, on a Process p, by just doing p.daemon=1 before p.start(), we can make the child die when the parent exits. However, the child does not die if the parent gets killed. How can I make sure the child die when the parent gets killed? thanks, --mihai -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Fri Dec 2 10:13:34 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Dec 2011 02:13:34 +1100 Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: Message-ID: On Sat, Dec 3, 2011 at 2:05 AM, Mihai Badoiu wrote: > In the multiprocessing module, on a Process p, by just doing p.daemon=1 > before p.start(), we can make the child die when the parent exits. ?However, > the child does not die if the parent gets killed. > How can I make sure the child die when the parent gets killed? Are you in control of the killing of the parent? One easy way would be to catch the signal (say SIGINT) and initiate an orderly shutdown, signalling the children first. ChrisA From devplayer at gmail.com Fri Dec 2 10:18:35 2011 From: devplayer at gmail.com (DevPlayer) Date: Fri, 2 Dec 2011 07:18:35 -0800 (PST) Subject: python 2.5 and ast References: <4ED37475.3050709@gmail.com> <4ED457C5.2020407@davea.name> <4ed4c2ce$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ED4E35E.6090405@gmail.com> Message-ID: <4bbaa89c-55d2-40d1-91fb-cb00c3f2239a@s4g2000yqk.googlegroups.com> On Nov 30, 1:03?pm, Andrea Crotti wrote: > Another thing about the AST, I am having fun trying to for example list > out all > the unused imports. > > I have already a visitor which works quite nicely I think, but now I > would like > to get a way to find all the unused imports, so I need more visitors that > find out all the used names. > > I didn't find too much documentation about these kind of things, so any > suggestions on how to approach? > > Pylint and snakefood have similar code, but they use the old "compiler" > module, > so it's not really helpful. There was another topic in these forums recently about "un-importing" modules (and how you can not do that reliably without restarting python). There was various ways mentioned of keeping track of what was imported. And there was mentioned reasonable ways of finding all possible imports on a computer. By "unused imports" I assume you mean "all unused imports in the application's source code" as opposed to meaning "all unused modules/ packages/libraries on that computer." Personally I'd love to see more tutorials on the AST module; An AST for Dummies. Pretty much the tutorials talk about parsing an expression like "1+2=3". But I'd like to see how blocks are compiled/ managed by the indent/dedent tokens and how the complete algorithm for finding qouted strings is implimented (using really simple explanations). Some google buzzwords to help with your search for your question: sys import cache, import hook, pydoc walkpackages(). And I just found this little tool; never knew about it: C:\PythonXX \Tools\Scripts\pydocui.pyw From kmichael.aye at gmail.com Fri Dec 2 10:27:04 2011 From: kmichael.aye at gmail.com (K.-Michael Aye) Date: Fri, 2 Dec 2011 16:27:04 +0100 Subject: Why are Sphinx docs incompatible with Safari Reader? Message-ID: Anybody knows? Have a nice weekend! Michael From janedenone at googlemail.com Fri Dec 2 10:27:43 2011 From: janedenone at googlemail.com (janedenone) Date: Fri, 2 Dec 2011 07:27:43 -0800 (PST) Subject: Reading twice from STDIN References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> <4ed883e4$0$6920$e4fe514c@news2.news.xs4all.nl> <321b6b8b-16b2-433e-9bfc-b5ec22e77ff6@i6g2000vbe.googlegroups.com> <4ed8cceb$0$6931$e4fe514c@news2.news.xs4all.nl> Message-ID: On Dec 2, 2:04?pm, Hans Mulder wrote: > That's odd. ?For some reason, I can get away with a simple > > ? ? ?sys.stdin = open('/dev/tty') > > and raw_input will merrily read from file descriptor 3. > > I'm using Pyhton 2.7.1 and 3.2 on MacOS/X 10.5.0. > > What version are you using? > > -- HansM Python 2.7.1 and 3.2.2 on Mac OS X 10.7.2. But what's really strange ??you're right. It works now. I have no idea what I did before, but I can confirm that the following code executes flawlessly: message = sys.stdin.read() urls = re.findall(r'(?:http|www)\S+', message) sys.stdin = open('/dev/tty') # Asks user to select a URL only if more than one URL is present if len(urls) > 1: for index, url in enumerate(urls): print '{0}: {1}\n'.format(index, url) selected_index = raw_input('Which URL to open? ') selected_index = int(selected_index) else: selected_index = 0 selected_url = urls[selected_index] Quite embarrassing. In any case, I am happy to have found the simplest solution. Thanks again, Jan From alec.taylor6 at gmail.com Fri Dec 2 10:57:08 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Sat, 3 Dec 2011 02:57:08 +1100 Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: Message-ID: I've been in philosophical discussions all day. This topic title makes me cringe :P From dihedral88888 at googlemail.com Fri Dec 2 10:57:56 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 07:57:56 -0800 (PST) Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: Message-ID: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> On Friday, December 2, 2011 11:13:34 PM UTC+8, Chris Angelico wrote: > On Sat, Dec 3, 2011 at 2:05 AM, Mihai Badoiu wrote: > > In the multiprocessing module, on a Process p, by just doing p.daemon=1 > > before p.start(), we can make the child die when the parent exits. ?However, > > the child does not die if the parent gets killed. > > How can I make sure the child die when the parent gets killed? > > Are you in control of the killing of the parent? One easy way would be > to catch the signal (say SIGINT) and initiate an orderly shutdown, > signalling the children first. > > ChrisA Multiple thread supporting programming languages in true OOP as Erlang and Python do not talk about POSIX signals. From dihedral88888 at googlemail.com Fri Dec 2 10:57:56 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 07:57:56 -0800 (PST) Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: Message-ID: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> On Friday, December 2, 2011 11:13:34 PM UTC+8, Chris Angelico wrote: > On Sat, Dec 3, 2011 at 2:05 AM, Mihai Badoiu wrote: > > In the multiprocessing module, on a Process p, by just doing p.daemon=1 > > before p.start(), we can make the child die when the parent exits. ?However, > > the child does not die if the parent gets killed. > > How can I make sure the child die when the parent gets killed? > > Are you in control of the killing of the parent? One easy way would be > to catch the signal (say SIGINT) and initiate an orderly shutdown, > signalling the children first. > > ChrisA Multiple thread supporting programming languages in true OOP as Erlang and Python do not talk about POSIX signals. From rosuav at gmail.com Fri Dec 2 11:14:15 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Dec 2011 03:14:15 +1100 Subject: Multiprocessing: killing children when parent dies In-Reply-To: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> Message-ID: On Sat, Dec 3, 2011 at 2:57 AM, 88888 Dihedral wrote: > Multiple thread supporting programming languages ?in true OOP as Erlang and Python do not talk about POSIX signals. The OP talked about multiprocessing. Each thread of execution is a separate context, and can receive signals. ChrisA From mdickinson at enthought.com Fri Dec 2 11:25:25 2011 From: mdickinson at enthought.com (Mark Dickinson) Date: Fri, 2 Dec 2011 08:25:25 -0800 (PST) Subject: unpack('>f', b'\x00\x01\x00\x00') References: <6b7251ef-3479-412f-8acb-882be1e25633@n35g2000yqf.googlegroups.com> <87obvsbnja.fsf@xemacs.org> Message-ID: <1bf12228-a5bc-4f1f-8928-3bc2274fdb34@gl2g2000vbb.googlegroups.com> On Dec 1, 10:21?am, Hrvoje Niksic wrote: > Chris Rebert writes: > > C does not have a built-in fixed-point datatype, so the `struct` > > module doesn't handle fixed-point numbers directly. > > The built-in decimal module supports fixed-point arithmetic, but the > struct module doesn't know about it. ?A bug report (or patch) by someone > who works with binary representations of fixed-point would be a good > start to improve it. Not really: the decimal module is for floating-point, not fixed- point, though its semantics for significant trailing zeros can help give the appearance of fixed-point arithmetic for simple operations (addition, subtraction) that stay within suitable bounds. And decimal fixed-point isn't so much use for a binary fixed-point format, anyway. -- Mark From dihedral88888 at googlemail.com Fri Dec 2 11:27:17 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 08:27:17 -0800 (PST) Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> Message-ID: <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Please check Erlang that spawn so easily. And there are Python packages can do the same task. From dihedral88888 at googlemail.com Fri Dec 2 11:27:17 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 08:27:17 -0800 (PST) Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> Message-ID: <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Please check Erlang that spawn so easily. And there are Python packages can do the same task. From andrea.crotti.0 at gmail.com Fri Dec 2 11:54:34 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Fri, 02 Dec 2011 16:54:34 +0000 Subject: python 2.5 and ast In-Reply-To: <4bbaa89c-55d2-40d1-91fb-cb00c3f2239a@s4g2000yqk.googlegroups.com> References: <4ED37475.3050709@gmail.com> <4ED457C5.2020407@davea.name> <4ed4c2ce$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ED4E35E.6090405@gmail.com> <4bbaa89c-55d2-40d1-91fb-cb00c3f2239a@s4g2000yqk.googlegroups.com> Message-ID: <4ED902CA.7090404@gmail.com> On 12/02/2011 03:18 PM, DevPlayer wrote: > There was another topic in these forums recently about "un-importing" > modules (and how you can not do that reliably without restarting > python). There was various ways mentioned of keeping track of what was > imported. And there was mentioned reasonable ways of finding all > possible imports on a computer. > > By "unused imports" I assume you mean "all unused imports in the > application's source code" as opposed to meaning "all unused modules/ > packages/libraries on that computer." > > Personally I'd love to see more tutorials on the AST module; An AST > for Dummies. Pretty much the tutorials talk about parsing an > expression like "1+2=3". But I'd like to see how blocks are compiled/ > managed by the indent/dedent tokens and how the complete algorithm for > finding qouted strings is implimented (using really simple > explanations). > > Some google buzzwords to help with your search for your question: sys > import cache, import hook, pydoc walkpackages(). > > And I just found this little tool; never knew about it: C:\PythonXX > \Tools\Scripts\pydocui.pyw > Yes I meant unused imports in each of the modules actually... The problem for me is to actually understand what are all the possible AST node that can involve the use of an actual import, and I didn't find anything helpful aboupt that. The ASDL definition says everything in theory but without some examples is a bit hard. I got something nice however: http://stackoverflow.com/questions/8340567/python-ast-to-dot-graph/8342383#8342383 But on windows and Python 2.5 nothing is working :/, even if I blandly copied the ast.py from the python 2.7 code file.. File "h:\long\path\devsonly\ast.py", line 166, in iter_fields for field in node._fields: TypeError: 'NoneType' object is not iterable From dihedral88888 at googlemail.com Fri Dec 2 12:00:40 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 09:00:40 -0800 (PST) Subject: order independent hash? In-Reply-To: <87k46fb8pg.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> Message-ID: <23692480.457.1322845240706.JavaMail.geo-discussion-forums@prfi36> On Friday, December 2, 2011 5:53:47 PM UTC+8, Hrvoje Niksic wrote: > Chris Angelico writes: > > >> The hash can grow with (k,v) pairs accumulated in the run time. > >> An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > > > That's a hash table > > In many contexts "hash table" is shortened to "hash" when there is no > ambiguity. This is especially popular among Perl programmers where the > equivalent of dict is called a hash. > > > Although strictly speaking, isn't that "Python dicts are implemented > > as hash tables in CPython"? Or is the hashtable implementation > > mandated? > > It's pretty much mandated because of the __hash__ protocol. I agree with you. When a hash is inserted with (k,v1) and then (k,v2) such that v1!=v2, there are several possibilities to handle this situation in programming: 1. v2 just replaces v1 with or without a message 2. the programmer can delete the (k,v1) entry and then insert a (k, (v1,v2)) entry. 3. the programmer initializes a tuple of (v1,v2), and then delete the (k,v1) entry to be replaced by a (k,(v1,v2)) entry. Hashes of hashes are really powerful in Python to be used to build a relational DB of thousands of thousands of objects or records in a piece of cake. A similar hash library in C is to be used everywhere is not difficult at all. From andrea.crotti.0 at gmail.com Fri Dec 2 12:01:47 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Fri, 02 Dec 2011 17:01:47 +0000 Subject: python 2.5 and ast In-Reply-To: <4ED902CA.7090404@gmail.com> References: <4ED37475.3050709@gmail.com> <4ED457C5.2020407@davea.name> <4ed4c2ce$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ED4E35E.6090405@gmail.com> <4bbaa89c-55d2-40d1-91fb-cb00c3f2239a@s4g2000yqk.googlegroups.com> <4ED902CA.7090404@gmail.com> Message-ID: <4ED9047B.2010603@gmail.com> And on a related topic, how can I actually detect other types of imports, for example __import__ Doing a dump I get this: In [113]: ast.dump(ast.parse('__import__("module")')) Out[113]: "Module(body=[Expr(value=Call(func=Name(id='__import__', ctx=Load()), args=[Str(s='module')], keywords=[], starargs=None, kwargs=None))])" So the visitor should be quite smart, and the string passed can't be always be done. I think the easiest way is just to use regexp and look for them warning the user that there might be other stupid imports, the harder way is to try to detect it and if the string passed is actually known at compile-time use it, otherwise warn the user. From chris.hulan at gmail.com Fri Dec 2 12:20:38 2011 From: chris.hulan at gmail.com (Chris Hulan) Date: Fri, 2 Dec 2011 09:20:38 -0800 (PST) Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: <31721129.458.1322846439035.JavaMail.geo-discussion-forums@prjr26> I'd just look a the unit tests for clarification/examples From gnarlodious at gmail.com Fri Dec 2 12:49:25 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 2 Dec 2011 09:49:25 -0800 (PST) Subject: Referencing module.instance Message-ID: What I am doing is importing modules that have an identical instance name. So I say: import Grid Grid has its instance: Grid.Grid() and this is the same for all modules of my webapp. allowedPages is a list of modules to import, so they are quoted strings: for page in self.allowedPages: setattr(self, page, __import__(page)) The problem is that the attribute name needs to reference the Grid.Grid instance and not the Grid module. How would I do this? I can do it literally: setattr(self, 'Grid', Grid.Grid) however doing it as a reference eludes me. Or is there some nifty Pythonic way of bulk importing modules? -- Gnarlie From dihedral88888 at googlemail.com Fri Dec 2 13:18:12 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 10:18:12 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> On Wednesday, November 30, 2011 8:32:39 PM UTC+8, Neal Becker wrote: > I like to hash a list of words (actually, the command line args of my program) > in such a way that different words will create different hash, but not sensitive > to the order of the words. Any ideas? For each word of your list as the key, an object of a hash can be used further is stored as the value in the hash of the list of words. Assume that word 1 is the key and the hash stored is a sentence that uses the key word 1. This is very useful to learn vocabularies and writing sentences. From dihedral88888 at googlemail.com Fri Dec 2 13:18:12 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 10:18:12 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> On Wednesday, November 30, 2011 8:32:39 PM UTC+8, Neal Becker wrote: > I like to hash a list of words (actually, the command line args of my program) > in such a way that different words will create different hash, but not sensitive > to the order of the words. Any ideas? For each word of your list as the key, an object of a hash can be used further is stored as the value in the hash of the list of words. Assume that word 1 is the key and the hash stored is a sentence that uses the key word 1. This is very useful to learn vocabularies and writing sentences. From dihedral88888 at googlemail.com Fri Dec 2 13:28:10 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 10:28:10 -0800 (PST) Subject: python 2.5 and ast In-Reply-To: References: Message-ID: <6866657.516.1322850490609.JavaMail.geo-discussion-forums@prjr26> On Monday, November 28, 2011 7:45:57 PM UTC+8, Andrea Crotti wrote: > I'm happily using the ast module to analyze some code, > but my scripts need also to run unfortunately on python 2.5 > > The _ast was there already, but the ast helpers not yet. > Is it ok if I just copy over the source from the ast helpers in my code base > or is there a smarter way? > (I don't even need all of them, just "parse" and NodeVisitor at the moment) Sounds like a hash for nodes and a hash for a tree. A hash can replace a tree of finite number of nodes. An array or list is enough to replace a tree of finite number of nodes. The heap sort ordering is simple but illustrative . After all the tasks of the tree are done, the hash of the tree will be deleted by Python's garbage collection mechanism. A hash can replace an indexed array with (k,v) pairs for k=0,1,2,3...n in a trivial way. From dihedral88888 at googlemail.com Fri Dec 2 13:28:10 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 10:28:10 -0800 (PST) Subject: python 2.5 and ast In-Reply-To: References: Message-ID: <6866657.516.1322850490609.JavaMail.geo-discussion-forums@prjr26> On Monday, November 28, 2011 7:45:57 PM UTC+8, Andrea Crotti wrote: > I'm happily using the ast module to analyze some code, > but my scripts need also to run unfortunately on python 2.5 > > The _ast was there already, but the ast helpers not yet. > Is it ok if I just copy over the source from the ast helpers in my code base > or is there a smarter way? > (I don't even need all of them, just "parse" and NodeVisitor at the moment) Sounds like a hash for nodes and a hash for a tree. A hash can replace a tree of finite number of nodes. An array or list is enough to replace a tree of finite number of nodes. The heap sort ordering is simple but illustrative . After all the tasks of the tree are done, the hash of the tree will be deleted by Python's garbage collection mechanism. A hash can replace an indexed array with (k,v) pairs for k=0,1,2,3...n in a trivial way. From snorble at hotmail.com Fri Dec 2 13:34:24 2011 From: snorble at hotmail.com (snorble) Date: Fri, 2 Dec 2011 10:34:24 -0800 (PST) Subject: Install Python on Windows without Start Menu icons? Message-ID: Is it possible to automate the Python installation on Windows using the MSI file so it does not add a Start Menu folder? I would like to push out Python to all of my office workstations, but I'd like for it to be relatively silent from the user's point of view. From ian.g.kelly at gmail.com Fri Dec 2 13:37:26 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 2 Dec 2011 11:37:26 -0700 Subject: order independent hash? In-Reply-To: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> References: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> Message-ID: On Fri, Dec 2, 2011 at 11:18 AM, 88888 Dihedral wrote: > On Wednesday, November 30, 2011 8:32:39 PM UTC+8, Neal Becker wrote: >> I like to hash a list of words (actually, the command line args of my program) >> in such a way that different words will create different hash, but not sensitive >> to the order of the words. ?Any ideas? > > For each word of your list as the key, an object of a hash can be used further > is stored as the value in the hash of the list of words. > > Assume that word 1 is the key and the hash stored is a sentence that uses > the key word 1. > > This is very useful to learn vocabularies and writing sentences. The OP is talking about hash functions, not hash tables, and furthermore he is talking about hashing the entire unordered collection of words, not hashing individual words or ordered sentences. From d at davea.name Fri Dec 2 13:39:44 2011 From: d at davea.name (Dave Angel) Date: Fri, 02 Dec 2011 13:39:44 -0500 Subject: order independent hash? In-Reply-To: References: Message-ID: <4ED91B70.1080804@davea.name> On 12/01/2011 08:55 AM, Neal Becker wrote: > Gelonida N wrote: > >> On 11/30/2011 01:32 PM, Neal Becker wrote: >>> I like to hash a list of words (actually, the command line args of my >>> program) in such a way that different words will create different hash, but >>> not sensitive >>> to the order of the words. Any ideas? >>> >> Do youmean hash like digest like md5sum / sha1 ? >> >> >> You should sort the words alphabetically, concatenate them with a space >> or any character, that will NEVER be part of a word and calulate the hash. >> >> If words can exist multiple times, then youhad tu uniqufy them (u using >> a python dict / set) first. > Yes that sounds just like what I wanted - thanks! > "different words will create different hash, but not sensitive to the order of the words" Just note that you can only say that "probably create" a different hash. If the hash number is constrained in size (eg. 32 bits), then a collision is possible. (Unless of course you also constrain the alphabet and/or length of the words involved) -- DaveA From ian.g.kelly at gmail.com Fri Dec 2 13:43:56 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 2 Dec 2011 11:43:56 -0700 Subject: python 2.5 and ast In-Reply-To: <6866657.516.1322850490609.JavaMail.geo-discussion-forums@prjr26> References: <6866657.516.1322850490609.JavaMail.geo-discussion-forums@prjr26> Message-ID: On Fri, Dec 2, 2011 at 11:28 AM, 88888 Dihedral wrote: > On Monday, November 28, 2011 7:45:57 PM UTC+8, Andrea Crotti wrote: >> I'm happily using the ast module to analyze some code, >> but my scripts need also to run unfortunately on python 2.5 >> >> The _ast was there already, but the ast helpers not yet. >> Is it ok if I just copy over the source from the ast helpers in my code base >> or is there a smarter way? >> (I don't even need all of them, just "parse" and NodeVisitor at the moment) > > Sounds like a hash for nodes and a hash for a tree. > A hash can replace a tree of finite number of nodes. > An array or list is enough to replace a tree of finite number of nodes. > > The heap sort ordering is simple but illustrative . > > After all the tasks ?of the tree are done, the hash of the tree > will be deleted by Python's garbage collection mechanism. > > A hash can replace an indexed array with (k,v) pairs for k=0,1,2,3...n in > a trivial way. What in the world does any of this have to do with using the ast module in Python 2.5? I am starting to suspect that "88888 Dihedral" may be a bot. From icanbob at gmail.com Fri Dec 2 14:06:27 2011 From: icanbob at gmail.com (bobicanprogram) Date: Fri, 2 Dec 2011 11:06:27 -0800 (PST) Subject: Need some IPC pointers References: Message-ID: <904c723e-ef2b-49ae-87a1-cc287a69192b@u6g2000vbg.googlegroups.com> On Nov 30, 4:03 pm, Andrew Berg wrote: > I've done some research, but I'm not sure what's most appropriate for my > situation. What I want to do is have a long running process that spawns > processes (that aren't necessarily written in Python) and communicates > with them. The children can be spawned at any time and communicate at > any time. Being able to communicate with non-local processes would be > nice, but is not necessary. The implementation needs to be > cross-platform, but child processes will use the same OS as the parent > during runtime. > I don't think I'll ever need to transfer anything complicated or large - > just strings or possibly tuples/lists. I'd rather not go outside the > standard library (but I'd consider it). I don't need to worry about > compatibility with older Python versions; if it only works with Python > 3.2, that's not a problem. > I'm thinking sockets, but perhaps there's something simpler/easier. > > -- > CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 You might want to take a look at the SIMPL toolkit. http://www.icanprogram.com/06py/lesson1/lesson1.html SIMPL modules can be written (and mixed) in any number of supported languages including Python, C, C++, JAVA, Tcl/Tk or PHP. Non local communication is handled by generic surrogates. Because of this surrogate architecture SIMPL modules can often be tested locally and then deployed into the cloud without any changes. bob From fred.sells at adventistcare.org Fri Dec 2 15:29:22 2011 From: fred.sells at adventistcare.org (Sells, Fred) Date: Fri, 2 Dec 2011 15:29:22 -0500 Subject: Using the Python Interpreter as a Reference In-Reply-To: <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <79379487-0081-4067-92ed-c6717652e1ff@y7g2000vbe.googlegroups.com><4eb0af60-26b3-45d5-8aff-566505003d6a@m10g2000vbc.googlegroups.com><4ed2cddc$0$29988$c3e8da3$5496439d@news.astraweb.com><4ed491fe$0$14018$c3e8da3$76491128@news.astraweb.com> <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven, that's probably the most elegant explanation of the "pythonic" way I've ever seen. I'm saving it for the next time upper management want to use Java again. -----Original Message----- From: python-list-bounces+frsells=adventistcare.org at python.org [mailto:python-list-bounces+frsells=adventistcare.org at python.org] On Behalf Of Steven D'Aprano Sent: Thursday, December 01, 2011 7:43 PM To: python-list at python.org Subject: Re: Using the Python Interpreter as a Reference On Thu, 01 Dec 2011 10:03:53 -0800, DevPlayer wrote: [...] > Well, that may be a little hyperbolic. But with 2 spaces you can > encourage coders to get very deep, indentially, and still fit 80 chars. Why would you want to encourage coders to write deeply indented code? In my opinion, if your code is indented four or more levels, you should start to think about refactorising your code; if you reach six levels, your code is probably a mess. class K: def spam(): if x: for a in b: # This is about as deep as comfortable while y: # Code is starting to smell try: # Code smell is now beginning to reek with z as c: # And now more of a stench try: # A burning, painful stench if d: # Help! I can't breathe!!! for e in f: # WTF are you thinking? try: # DIE YOU M***********ER!!! while g: # gibbers quietly ... The beauty of languages like Python where indentation is significant is that you can't hide from the ugliness of this code. class K: { # Code looks okay to a casual glance. def spam():{ if x: { for a in b:{ while y:{ try:{ with z as c:{ try:{ if d:{ for e in f:{ try:{ while g:{ ... }}}} }}}} }}}} Deeply indented code *is* painful, it should *look* painful. -- Steven -- http://mail.python.org/mailman/listinfo/python-list From jeanpierreda at gmail.com Fri Dec 2 15:58:23 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 2 Dec 2011 15:58:23 -0500 Subject: Using the Python Interpreter as a Reference In-Reply-To: <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <79379487-0081-4067-92ed-c6717652e1ff@y7g2000vbe.googlegroups.com> <4eb0af60-26b3-45d5-8aff-566505003d6a@m10g2000vbc.googlegroups.com> <4ed2cddc$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed491fe$0$14018$c3e8da3$76491128@news.astraweb.com> <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: > In my opinion, if your code is indented four or more levels, you should > start to think about refactorising your code; if you reach six levels, > your code is probably a mess. Here's some code I encountered while grading assignments from first-year CS students: if 'not' in temp_holder: for item in (range(len(ial))): for key in (range(len(ial[item]))): if type(ial[item][key]) == str: if temp[term].find(ial[item][key]) > 0: for value in range(len(ial[item][1])): if ial[item][1][value] in images: while ial[item][1][value] in images: images.remove(ial[item][1][value]) I think after some point of nesting, not only can we conclude that the code is difficult to read, we can probably conclude the author wasn't thinking very clearly about what he or she was doing. Devin On Thu, Dec 1, 2011 at 7:43 PM, Steven D'Aprano wrote: > On Thu, 01 Dec 2011 10:03:53 -0800, DevPlayer wrote: > > [...] >> Well, that may be a little hyperbolic. But with 2 spaces you can >> encourage coders to get very deep, indentially, and still fit 80 chars. > > Why would you want to encourage coders to write deeply indented code? > > In my opinion, if your code is indented four or more levels, you should > start to think about refactorising your code; if you reach six levels, > your code is probably a mess. > > class K: > ? ?def spam(): > ? ? ? ?if x: > ? ? ? ? ? ?for a in b: > ? ? ? ? ? ? ? ?# This is about as deep as comfortable > ? ? ? ? ? ? ? ?while y: > ? ? ? ? ? ? ? ? ? ?# Code is starting to smell > ? ? ? ? ? ? ? ? ? ?try: > ? ? ? ? ? ? ? ? ? ? ? ?# Code smell is now beginning to reek > ? ? ? ? ? ? ? ? ? ? ? ?with z as c: > ? ? ? ? ? ? ? ? ? ? ? ? ? ?# And now more of a stench > ? ? ? ? ? ? ? ? ? ? ? ? ? ?try: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# A burning, painful stench > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if d: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# Help! I can't breathe!!! > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?for e in f: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# WTF are you thinking? > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?try: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# DIE YOU M***********ER!!! > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?while g: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# gibbers quietly > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?... > > > The beauty of languages like Python where indentation is significant is > that you can't hide from the ugliness of this code. > > class K: { > ?# Code looks okay to a casual glance. > ?def spam():{ > ? if x: { for a in b:{ > ? ? while y:{ try:{ with z as c:{ > ? ? ? try:{ if d:{ for e in f:{ try:{ > ? ? ? ? while g:{ ... }}}} > ? ? ? }}}} > ? ? }}}} > > Deeply indented code *is* painful, it should *look* painful. > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From ben+python at benfinney.id.au Fri Dec 2 16:31:18 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 03 Dec 2011 08:31:18 +1100 Subject: Referencing module.instance References: Message-ID: <87sjl23bkp.fsf@benfinney.id.au> Gnarlodious writes: > What I am doing is importing modules that have an identical instance > name. Best to fix that, then. > import Grid That's a poorly-named module. PEP 8 recommends module names be all lowercase. > Grid has its instance: > > Grid.Grid() And this is the reason: PEP 8 recommends class names be named with TitleCase, so they're distinguishable from module names. So the above should be: import grid grid.Grid() > for page in self.allowedPages: > setattr(self, page, __import__(page)) For completeness, I'll note that the function name would be PEP 8 compatible as ?allowed_pages?. > The problem is that the attribute name needs to reference the > Grid.Grid instance and not the Grid module. How would I do this? > I can do it literally: > setattr(self, 'Grid', Grid.Grid) Why do you shy away from this? You're already using ?__import__?, and I don't know why that is since you already showed that you're importing the module explicitly anyway. -- \ ?Don't be afraid of missing opportunities. Behind every failure | `\ is an opportunity somebody wishes they had missed.? ?Jane | _o__) Wagner, via Lily Tomlin | Ben Finney From tjreedy at udel.edu Fri Dec 2 16:40:24 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 02 Dec 2011 16:40:24 -0500 Subject: order independent hash? In-Reply-To: <87k46fb8pg.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> Message-ID: On 12/2/2011 4:53 AM, Hrvoje Niksic wrote: > Chris Angelico writes: > >>> The hash can grow with (k,v) pairs accumulated in the run time. >>> An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. >> >> That's a hash table > > In many contexts "hash table" is shortened to "hash" when there is no > ambiguity. This is especially popular among Perl programmers where the > equivalent of dict is called a hash. > >> Although strictly speaking, isn't that "Python dicts are implemented >> as hash tables in CPython"? Or is the hashtable implementation >> mandated? > > It's pretty much mandated because of the __hash__ protocol. Lib Ref 4.8. Mapping Types ? dict "A mapping object maps hashable values to arbitrary objects." This does not say that the mapping has to *use* the hash value ;-). Even if it does, it could use a tree structure instead of a hash table. But hash tables seem to work well for general purposes, "Mappings are mutable objects." (This would change if a frozendict were added.) especially when additions and deletions are needed. -- Terry Jan Reedy From ursusmaximus at gmail.com Fri Dec 2 16:47:11 2011 From: ursusmaximus at gmail.com (Ron) Date: Fri, 2 Dec 2011 13:47:11 -0800 (PST) Subject: Django ported to Python3! Message-ID: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> It looks like Vinay Sajip has succeeded in porting Django to Python3 (in a shared code base for Python 3.2 and Python 2.7). This is an astoundingly good job, done very fast and is big news. See https://groups.google.com/forum/#!topic/django-developers/XjrX3FIPT-U and the actual code is at Bitbucket https://bitbucket.org/vinay.sajip/django With NumPy and SciPy already ported, and with Matplotlib almost there, maybe PIL and others will follow shortly. This could be a turning point, or a milestone, or whatever you want to call it. Vinay is a hero who should be thanked and congratulated! In an infinitely less important note, Python411 podcasts are finally back online after a three month occultation due to a prolonged and ugly billing dispute with Libsyn. Maybe I can interview Vinay and have him tell us about the porting effort! From yobu18.m at gmail.com Fri Dec 2 19:45:25 2011 From: yobu18.m at gmail.com (kutty) Date: Fri, 2 Dec 2011 16:45:25 -0800 (PST) Subject: hiiiiiiiiiiiiiiiii man makes a money, money makes whommmmmmmmmmmmm Message-ID: <4e5e8990-da41-4810-b6f6-15b97530c062@da3g2000vbb.googlegroups.com> just logon to thzzzzzzzz websites From steve+comp.lang.python at pearwood.info Fri Dec 2 20:04:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Dec 2011 01:04:49 GMT Subject: order independent hash? References: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> Message-ID: <4ed975b1$0$29988$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Dec 2011 10:18:12 -0800, 88888 Dihedral wrote: [...] Dihedral, EVERY SINGLE ONE of your messages is double posted. You are sending to the newsgroup and the mailing list, but they are aliases for each other. Please stop double posting. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 2 20:24:26 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Dec 2011 01:24:26 GMT Subject: Referencing module.instance References: Message-ID: <4ed97a4a$0$29988$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Dec 2011 09:49:25 -0800, Gnarlodious wrote: > What I am doing is importing modules that have an identical instance > name. So I say: > > import Grid > > Grid has its instance: > > Grid.Grid() > > and this is the same for all modules of my webapp. allowedPages is a > list of modules to import, so they are quoted strings: > > for page in self.allowedPages: > setattr(self, page, __import__(page)) So if allowedPages looks like this: ['Grid', 'Spam', 'Ham', 'Cheese'] you expect your instance to look like this: self.Grid # has the value Grid.Grid self.Spam # has the value Spam.Grid etc. self.Ham self.Cheese Correct? I'm not sure I like that design, it feels wrong, but perhaps I don't understand the reason for it. Moving along... > The problem is that the attribute name needs to reference the Grid.Grid > instance and not the Grid module. How would I do this? I can do it > literally: > setattr(self, 'Grid', Grid.Grid) > > however doing it as a reference eludes me. __import__(page) returns a module, so just grab that attribute from the module. for page in self.allowedPages: setattr(self, page, __import__(page).Grid) But note that this is an binding operation, like an assignment. If you rebind module.Grid, your instance self will not see the change: Spam.Grid = 42 # some value instance.read_pages() # whatever you call the method assert instance.Spam == 42 Spam.Grid = 23 # rebind the original assert instance.Spam == 23 # THIS WILL FAIL!!! If you need these attributes to be aliases to the module attribute, rather than independent bindings, you need to use computed properties rather than plain attributes. I leave that as an exercise, mainly because I suspect it will be very difficult. Wait... no, I think it's easy! (Famous last words...) Try this untested code: class MyClass(object): allowedPages = ('Grid', 'Spam', 'Ham', 'Cheese') def __init__(self): self.modules = {} for name in allowedPages: self.modules[name] = __import__(name) def __getattribute__(self, name): if name in self.allowedPages: return self.modules[name].Grid return super(MyClass, self).__getattribute__(name) -- Steven From gnarlodious at gmail.com Fri Dec 2 22:49:22 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 2 Dec 2011 19:49:22 -0800 (PST) Subject: Referencing module.instance In-Reply-To: References: Message-ID: <4121414.852.1322884162706.JavaMail.geo-discussion-forums@prnu18> To explain my reasoning, this scheme will allow me to run the script three ways, as shell, as one-shot CGI or as a persistent mod_wsgi module. So to be more exhaustive: In __init__ I can say: import Grid self.Grid = Grid.Grid self.Grid is now the instance of Grid inside the module Grid. then later on I __call__ with the input data: html=self.Grid(queryString, environ) and this works great for one module, a default output which is now running on my server. But now I want to use the query string to load the instance, which means it comes in as text. Say I load a list of modules: allowedPages=['Gnomon', 'Grid', 'Lexicon', 'Harmonics'] I need to import them all at __init__, and set the instance for each like this: self.Gnomon=Gnomon.Gnomon self.Grid=Grid.Grid self.Lexicon=Lexicon.Lexicon self.Harmonics=Harmonics.Harmonics Then dynamically, in __call_, I just use the CGI string to invoke my target object. What I have now is: self.modules = {} page = 'Gnomon' self.modules[page] = __import__(page) which gives me A solution is to set self.modules[page] to the Gnomon instance of the Gnomon module. How to? -- Gnarlie http://Sectrum.com From antti.ylikoski at tkk.fi Fri Dec 2 22:54:19 2011 From: antti.ylikoski at tkk.fi (Antti J Ylikoski) Date: Sat, 03 Dec 2011 05:54:19 +0200 Subject: Python 2 or 3 Message-ID: I'm in the process of learning Python. I already can code objet-oriented programs with the language. I have in my hands the O'Reilly book by Mark Lutz, Programming Python, in two versions: the 2nd Edition, which covers Python 2, and the 4th edition, which covers Python 3. In the "official Python site" so to speak, http://www.python.org, it is mentioned that the authors recommend the visitor, who is a novice, to learn Python 2 rather than Python 3, because most of existing software has been writen with Python 2. The O'Reilly book has some 1200 pages. I would not want to invest such an amount of work and time to an obsolete language (i. e. Python 2). What is the opinion of the wizards here, shall I learm Python 2 or Python 3? I'm posting this here because I feel that this point is interesting to other students of Python. Cheers, Antti "Andy" Ylikoski Helsinki, Finland, the EU From gnarlodious at gmail.com Fri Dec 2 23:10:24 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 2 Dec 2011 20:10:24 -0800 (PST) Subject: Python 2 or 3 In-Reply-To: References: Message-ID: <33402104.714.1322885424968.JavaMail.geo-discussion-forums@prgi20> If you are writing your own scripts, I would recommend Py3 for learning. But if you are studying existing scripts to learn, Py2 might be better. I have been doing Python for about 2 years and started learning Py3 with no regrets. Py2 is not going to be "obsolete" for quite a while. It is almost all the same as Py3, just a few updates mostly of concern to those who maintain existing code. And actually, Python has been incrementally improved upon all along, just that Py3 was the cutoff point for stuff that HAD to change. -- Gnarlie From bahamutzero8825 at gmail.com Fri Dec 2 23:20:19 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 02 Dec 2011 22:20:19 -0600 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: <4ED9A383.1090103@gmail.com> On 12/2/2011 9:54 PM, Antti J Ylikoski wrote: > What is the opinion of the wizards here, shall I learm Python 2 or > Python 3? I'm posting this here because I feel that this point is > interesting to other students of Python. Unless you are tied to Python 2 in some way, go for Python 3 and don't look back. There are a few major software projects that still do not support Python 3 (more and more are getting ported or replaced as time goes on), and many production systems are still using Python 2, but don't let /potential/ roadblocks keep you away from Python 3. Another thing to note is that, at least AFAICT, Jython, IronPython and PyPy are not going to support Python 3 any time soon, so if you need to use one of them, Python 2 is the way to go. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From roy at panix.com Fri Dec 2 23:27:19 2011 From: roy at panix.com (Roy Smith) Date: Fri, 02 Dec 2011 23:27:19 -0500 Subject: Python 2 or 3 References: Message-ID: In article , Antti J Ylikoski wrote: > I have in my hands the O'Reilly book by Mark Lutz, Programming > Python, in two versions: the 2nd Edition, which covers Python 2, and > the 4th edition, which covers Python 3. The engineer in me really has to wonder what the 3rd edition might have covered :-) > I would not want to invest such an amount of work and time to an > obsolete language (i. e. Python 2). I think the best that can be said for Python 2 is, "It's not dead yet!". The vast majority of production Python code written today is for 2.x, for x in {5, 6, 7}. The biggest thing that's holding back adoption of 3 is that most of the major packages don't support 3 yet (but I saw an announcement just this morning that django has been ported to 3). I predict that 2012 will be the year of Python-3. I expect we're at the point now that all major packages will either get ported to 3 in the next year or two, or become abandonware. Also, people building new packages will come out with versions for both 2 and 3 if they want their stuff to get widely adopted. > What is the opinion of the wizards here, shall I learm Python 2 or > Python 3? I'm posting this here because I feel that this point is > interesting to other students of Python. The difficult thing here is that you are living on the cusp. If you came back and asked that question in a couple of years, I strongly suspect the answer would be, "Don't bother with 2; 3 is what everybody uses today". But, we're not there quite yet. Learn 2. From drsalists at gmail.com Fri Dec 2 23:42:06 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Fri, 2 Dec 2011 20:42:06 -0800 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On 12/2/11, Antti J Ylikoski wrote: > > I'm in the process of learning Python. I already can code > objet-oriented programs with the language. I have in my hands the > O'Reilly book by Mark Lutz, Programming Python, in two versions: the > 2nd Edition, which covers Python 2, and the 4th edition, which covers > Python 3. > > In the "official Python site" so to speak, http://www.python.org, it > is mentioned that the authors recommend the visitor, who is a novice, > to learn Python 2 rather than Python 3, because most of existing > software has been writen with Python 2. > > The O'Reilly book has some 1200 pages. I would not want to invest > such an amount of work and time to an obsolete language (i. e. Python > 2). It mostly depends on what sort of thing you're learning Python for. If you have a project in mind that has large dependencies that still require Python 2.x, then you're probably best off with 2.x for now. Otherwise, go with 3.x. Or do what I've been enjoying: Install 2.x and 3.x, and test with both, each step of the way. In this manner, you can pretty easily write new code that runs on either equally well. But again, there's the matter of dependencies. > What is the opinion of the wizards here, shall I learm Python 2 or > Python 3? I'm posting this here because I feel that this point is > interesting to other students of Python. > > > Cheers, Antti "Andy" Ylikoski > Helsinki, Finland, the EU > -- > http://mail.python.org/mailman/listinfo/python-list > From tjreedy at udel.edu Sat Dec 3 01:23:33 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 03 Dec 2011 01:23:33 -0500 Subject: Python 2 or 3 In-Reply-To: <4ED9A383.1090103@gmail.com> References: <4ED9A383.1090103@gmail.com> Message-ID: On 12/2/2011 11:20 PM, Andrew Berg wrote: > thing to note is that, at least AFAICT, Jython, IronPython and PyPy are > not going to support Python 3 any time soon, PyPy has a roadmap for 3.2 http://pypy.org/py3donate.html They definitely plan to do it one way or another. -- Terry Jan Reedy From bahamutzero8825 at gmail.com Sat Dec 3 01:37:39 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 03 Dec 2011 00:37:39 -0600 Subject: Python 2 or 3 In-Reply-To: References: <4ED9A383.1090103@gmail.com> Message-ID: <4ED9C3B3.8050609@gmail.com> On 12/3/2011 12:23 AM, Terry Reedy wrote: > PyPy has a roadmap for 3.2 > http://pypy.org/py3donate.html > They definitely plan to do it one way or another. I never said there were no plans, but at $2567 out of $60k, I don't see it happening soon. Unless someone decides to donate a huge sum of money or a large amount of code in the next couple months, I don't see a release happening until at least the middle of next year. It would definitely not be practical to learn Python 3 at this point if your primary implementation is PyPy. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From bahamutzero8825 at gmail.com Sat Dec 3 01:41:43 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 03 Dec 2011 00:41:43 -0600 Subject: Python 2 or 3 In-Reply-To: References: <4ED9A383.1090103@gmail.com> Message-ID: <4ED9C4A7.40206@gmail.com> I accidentally quoted the wrong figure. I meant $4369 of $105000. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From stefan_ml at behnel.de Sat Dec 3 01:48:25 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 03 Dec 2011 07:48:25 +0100 Subject: Django ported to Python3! In-Reply-To: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> References: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> Message-ID: Ron, 02.12.2011 22:47: > It looks like Vinay Sajip has succeeded in porting Django to Python3 > (in a shared code base for Python 3.2 and Python 2.7). This is an > astoundingly good job, done very fast and is big news. > See https://groups.google.com/forum/#!topic/django-developers/XjrX3FIPT-U > and the actual code is at Bitbucket https://bitbucket.org/vinay.sajip/django > > With NumPy and SciPy already ported, and with Matplotlib almost there, > maybe PIL and others will follow shortly. This could be a turning > point, or a milestone, or whatever you want to call it. Vinay is a > hero who should be thanked and congratulated! Note that most of the work was done by Martin von L?wis, quite a while back in the early days of Python 3.x. http://wiki.python.org/moin/PortingDjangoTo3k He also did a huge amount of lobbying to get the changes accepted before the time that the project originally envisioned. The original plans of the Django project were to only *start* the porting after dropping support for Python 2.5 somewhere in the future. Martin made it rather clear with his patch (and keeps reiterating it wherever he can) that you can support both in one code base, even in a project as large as Django. Stefan From anacrolix at gmail.com Sat Dec 3 01:53:14 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Sat, 3 Dec 2011 17:53:14 +1100 Subject: Django ported to Python3! In-Reply-To: References: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> Message-ID: As long as we can dump python 2, a big congrats to anyone who makes this possible. Thanks martin On Dec 3, 2011 5:51 PM, "Stefan Behnel" wrote: > Ron, 02.12.2011 22:47: > >> It looks like Vinay Sajip has succeeded in porting Django to Python3 >> (in a shared code base for Python 3.2 and Python 2.7). This is an >> astoundingly good job, done very fast and is big news. >> See https://groups.google.com/**forum/#!topic/django-** >> developers/XjrX3FIPT-U >> and the actual code is at Bitbucket https://bitbucket.org/vinay.** >> sajip/django >> >> With NumPy and SciPy already ported, and with Matplotlib almost there, >> maybe PIL and others will follow shortly. This could be a turning >> point, or a milestone, or whatever you want to call it. Vinay is a >> hero who should be thanked and congratulated! >> > > Note that most of the work was done by Martin von L?wis, quite a while > back in the early days of Python 3.x. > > http://wiki.python.org/moin/**PortingDjangoTo3k > > He also did a huge amount of lobbying to get the changes accepted before > the time that the project originally envisioned. The original plans of the > Django project were to only *start* the porting after dropping support for > Python 2.5 somewhere in the future. Martin made it rather clear with his > patch (and keeps reiterating it wherever he can) that you can support both > in one code base, even in a project as large as Django. > > Stefan > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From anacrolix at gmail.com Sat Dec 3 01:54:28 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Sat, 3 Dec 2011 17:54:28 +1100 Subject: Python 2 or 3 In-Reply-To: <4ED9C3B3.8050609@gmail.com> References: <4ED9A383.1090103@gmail.com> <4ED9C3B3.8050609@gmail.com> Message-ID: 2 without a doubt. On Dec 3, 2011 5:40 PM, "Andrew Berg" wrote: > On 12/3/2011 12:23 AM, Terry Reedy wrote: > > PyPy has a roadmap for 3.2 > > http://pypy.org/py3donate.html > > They definitely plan to do it one way or another. > I never said there were no plans, but at $2567 out of $60k, I don't see > it happening soon. Unless someone decides to donate a huge sum of money > or a large amount of code in the next couple months, I don't see a > release happening until at least the middle of next year. It would > definitely not be practical to learn Python 3 at this point if your > primary implementation is PyPy. > > -- > CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wxjmfauth at gmail.com Sat Dec 3 03:59:12 2011 From: wxjmfauth at gmail.com (jmfauth) Date: Sat, 3 Dec 2011 00:59:12 -0800 (PST) Subject: Python 2 or 3 References: Message-ID: <834e5eac-4b53-44c1-bfad-e54bba60a06d@i8g2000vbh.googlegroups.com> On 3 d?c, 04:54, Antti J Ylikoski wrote: > Helsinki, Finland, the EU <<<<<<<<<<< >>> sys.version '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]' >>> '?l?phant' '\xe9l\xe9phant' >>> >>> sys.version '3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)]' >>> '?l?phant' '?l?phant' >>> jmf From whatsjacksemail at gmail.com Sat Dec 3 04:01:41 2011 From: whatsjacksemail at gmail.com (Jack Keegan) Date: Sat, 3 Dec 2011 09:01:41 +0000 Subject: Multiprocessing: killing children when parent dies In-Reply-To: <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Message-ID: I think the OP meant when the parent gets killed (by ctrl+c or similar), not deleted. At least that's what I think when I think of a program being killed. Is it even possible to send a signal in such a case? Cheers, Jack On Fri, Dec 2, 2011 at 4:27 PM, 88888 Dihedral wrote: > Please check Erlang that spawn so easily. And there are Python packages > can do the same task. > -- > http://mail.python.org/mailman/listinfo/python-list > -- The earth is a very small stage in a vast cosmic arena. Think of the rivers of blood spilled by all those generals and emperors so that in glory and in triumph they could become the momentary masters of a fraction of a dot. - Carl Sagan [Pale Blue Dot] -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sat Dec 3 05:04:07 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Dec 2011 10:04:07 GMT Subject: Python 2 or 3 References: Message-ID: <4ed9f416$0$29988$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Dec 2011 05:54:19 +0200, Antti J Ylikoski wrote: > The O'Reilly book has some 1200 pages. I would not want to invest such > an amount of work and time to an obsolete language (i. e. Python 2). Python 2 is not an obsolete language. The differences between Python 2 and Python 3 are minor, more like different dialects of one language than two languages. I learned Python 1.5 and watched the extended transition between 1.5 -> 2.2, and in my opinion, the 2.5 -> 3.2 transition is not that much more difficult. An experienced programmer won't have any difficulty learning both, and swapping between them. Newbies who have never programmed before often have trouble with the differences between 2.x and 3.x, but if you are an experienced programmer, you shouldn't have any trouble at all. The biggest difference to the language is that strings are now Unicode instead of byte strings; this has forced a lot of English speakers to learn about Unicode and bytes instead of assuming that the universe is ASCII. Apart from that, the differences are, in my opinion, trivial. However, there are a lot of them: http://docs.python.org/release/3.0.1/whatsnew/3.0.html But the most important ones in my opinion are: * unicode text, as mentioned above, and the consequences of this; * some of the modules in the standard library have been renamed; * various new modules have been added; * some built-in functions and methods which used to return lists now return lazy iterators or views; * print is now a function instead of a statement; * a small number of syntax changes, some of which are also supported by Python 2.6 and 2.7; * "classic classes" no longer exist, so there is now only one class mechanism, not two. If you can deal with the difference between these two lines without getting confused: print md5.md5("spam").hexdigest() # Python 2.x print(hashlib.md5("spam").hexdigest()) # Python 3.x you're half way there. -- Steven From gnarlodious at gmail.com Sat Dec 3 05:37:14 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Sat, 3 Dec 2011 02:37:14 -0800 (PST) Subject: Referencing module.instance In-Reply-To: References: Message-ID: <9331481.63.1322908634300.JavaMail.geo-discussion-forums@prij11> HA! After much experimenting I hit upon getattr(__import__(page), page): for page in self.allowedPages: scriptPath = '{}/{}.py'.format(os.path.dirname(__file__), page) if os.path.exists(scriptPath): self.modules[page] = getattr(__import__(page), page) Then in __call_ I just say: target = incoming CGI variable self.modules[target](qList, environ) and it works! -- Gnarlie From rosuav at gmail.com Sat Dec 3 05:44:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Dec 2011 21:44:22 +1100 Subject: Python 2 or 3 In-Reply-To: <4ed9f416$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4ed9f416$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Dec 3, 2011 at 9:04 PM, Steven D'Aprano wrote: > If you can deal with the difference between these two lines without > getting confused: > > print md5.md5("spam").hexdigest() ?# Python 2.x > print(hashlib.md5("spam").hexdigest()) ?# Python 3.x The second line needs to be: print(hashlib.md5("spam".encode()).hexdigest()) Relatively insignificant differences, since Python 2 and Python 3 both support both bytes and unicode strings. The only difference is that Py3 makes Unicode the default, forcing you to be explicit when you want bytes. ChrisA From arnodel at gmail.com Sat Dec 3 07:03:14 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sat, 3 Dec 2011 12:03:14 +0000 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On 3 December 2011 03:54, Antti J Ylikoski wrote: > > I'm in the process of learning Python. ?I already can code > objet-oriented programs with the language. ?I have in my hands the > O'Reilly book by Mark Lutz, Programming Python, in two versions: the > 2nd Edition, which covers Python 2, and the 4th edition, which covers > Python 3. > > In the "official Python site" so to speak, http://www.python.org, it > is mentioned that the authors recommend the visitor, who is a novice, > to learn Python 2 rather than Python 3, because most of existing > software has been writen with Python 2. > > The O'Reilly book has some 1200 pages. ?I would not want to invest > such an amount of work and time to an obsolete language (i. e. Python > 2). Python 2 and Python 3 are mostly the same language. Learning either will be equally valuable IMHO. There are some significant differences but if you have a good understanding of one, you will have no problem adapting very quickly to the other. And Python 2 is definitely not obsolete :) -- Arnaud From pedro.h.souto at gmail.com Sat Dec 3 09:01:45 2011 From: pedro.h.souto at gmail.com (Pedro Henrique G. Souto) Date: Sat, 03 Dec 2011 12:01:45 -0200 Subject: Install Python on Windows without Start Menu icons? In-Reply-To: References: Message-ID: <4EDA2BC9.2030607@gmail.com> On 02/12/2011 16:34, snorble wrote: > Is it possible to automate the Python installation on Windows using > the MSI file so it does not add a Start Menu folder? I would like to > push out Python to all of my office workstations, but I'd like for it > to be relatively silent from the user's point of view. If you just want to run python scripts in those machines (not developing in it), you can use something like py2exe [http://www.py2exe.org/]. It converts a python script to a standalone executable. Good luck! -- Att; Pedro Henrique G. Souto ??????????????? ? ???d?_?b??? ? ??????????????? From ursusmaximus at gmail.com Sat Dec 3 11:03:18 2011 From: ursusmaximus at gmail.com (Ron) Date: Sat, 3 Dec 2011 08:03:18 -0800 (PST) Subject: Django ported to Python3! References: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> Message-ID: Thanks Stefan for clarifying that. I guess Martin deserves most of the credit. But I still admire how Sajip jumped in, and I especially admire how the core team accepted his work without taking a "Not Invented Here" attitude. I sure hope the port is accepted into the main trunk soon. There is just such a huge difference bewtween "90% done" and actually released. Often code is 90% done but is never finished. And Django has such enormous psychological significance for Python 3. Many important projects will never begin serious porting until after Django officially supports Python 3. And many Python folks will finally start to take Python 3 seriously only when Django does announce official support. Ron From dihedral88888 at googlemail.com Sat Dec 3 11:36:09 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 3 Dec 2011 08:36:09 -0800 (PST) Subject: order independent hash? In-Reply-To: <4ed975b1$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> <4ed975b1$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <33348771.70.1322930169970.JavaMail.geo-discussion-forums@prgi20> On Saturday, December 3, 2011 9:04:49 AM UTC+8, Steven D'Aprano wrote: > On Fri, 02 Dec 2011 10:18:12 -0800, 88888 Dihedral wrote: > [...] > > > Dihedral, EVERY SINGLE ONE of your messages is double posted. You are > sending to the newsgroup and the mailing list, but they are aliases for > each other. Please stop double posting. > > > -- > Steven A list can replace a hash. I knew that in lisp. From gelonida at gmail.com Sat Dec 3 15:26:25 2011 From: gelonida at gmail.com (Gelonida N) Date: Sat, 03 Dec 2011 21:26:25 +0100 Subject: order independent hash? In-Reply-To: <4ED91B70.1080804@davea.name> References: <4ED91B70.1080804@davea.name> Message-ID: On 12/02/2011 07:39 PM, Dave Angel wrote: > On 12/01/2011 08:55 AM, Neal Becker wrote: >> Gelonida N wrote: >> >>> On 11/30/2011 01:32 PM, Neal Becker wrote: >>>> I like to hash a list of words (actually, the command line args of my >>>> program) in such a way that different words will create different >>>> hash, but >>>> not sensitive >>>> to the order of the words. Any ideas? >>>> >>> Do youmean hash like digest like md5sum / sha1 ? >>> >>> >>> You should sort the words alphabetically, concatenate them with a space >>> or any character, that will NEVER be part of a word and calulate the >>> hash. >>> >>> If words can exist multiple times, then youhad tu uniqufy them (u using >>> a python dict / set) first. >> Yes that sounds just like what I wanted - thanks! >> You're welcome. is it working as expected? From gelonida at gmail.com Sat Dec 3 15:59:40 2011 From: gelonida at gmail.com (Gelonida N) Date: Sat, 03 Dec 2011 21:59:40 +0100 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On 12/03/2011 04:54 AM, Antti J Ylikoski wrote: > > I'm in the process of learning Python. I already can code > objet-oriented programs with the language. I have in my hands the > O'Reilly book by Mark Lutz, Programming Python, in two versions: the > 2nd Edition, which covers Python 2, and the 4th edition, which covers > Python 3. > > In the "official Python site" so to speak, http://www.python.org, it > is mentioned that the authors recommend the visitor, who is a novice, > to learn Python 2 rather than Python 3, because most of existing > software has been writen with Python 2. > > The O'Reilly book has some 1200 pages. I would not want to invest > such an amount of work and time to an obsolete language (i. e. Python > 2). > > What is the opinion of the wizards here, shall I learm Python 2 or > Python 3? I'm posting this here because I feel that this point is > interesting to other students of Python. > > > Cheers, Antti "Andy" Ylikoski > Helsinki, Finland, the EU I would still stick with python 2. In my opinion there is no reason to rush to the most recent version. Especially when working in a corporate environment or when being obliged to use certain web servers / servers you will notice, that you will still encounter quite some hosts with python 2.5 and even some with python2.4. Most machines, that I have to use, run python 2.6 (and some python 2.5) I personally try to write most of my code such that it could still run with python 2.5 I decided that python2.4 is my cut-off point, though I still have one machines and some embedded devices, which just run 2.4 Other considerations - there are still more libraries / packages available for python 2 than for python 3, though this is changing. - if you write code nicely enough in python 2, then you can translate it to python 3. autmatically. However up to my knowledge you cannot automatically translate python 3 code to python 2 code. With that strategy your code will be able to run on most web servers and your own machines. If you want to write your own code which does not have to run on other machines and / or if you want to heavily use unicode, then it might be better to start with Python 3. From arnodel at gmail.com Sat Dec 3 16:34:08 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sat, 3 Dec 2011 21:34:08 +0000 Subject: How to generate java .properties files in python Message-ID: Hi all, I need to generate some java .properties files in Python (2.6 / 2.7). It's a simple format to store key/value pairs e.g. blue=bleu green=vert red=rouge The key/value are unicode strings. The annoying thing is that the file is encoded in ISO 8859-1, with all non Latin1 characters escaped in the form \uHHHH (same as how unicode characters are escaped in Python). I thought I could use the "unicode_escape" codec. But it doesn't work because it escapes Latin1 characters with escape sequences of the form \xHH, which is not valid in a java .properties file. Is there a simple way to achieve this? I could do something like this: def encode(u): """encode a unicode string in .properties format""" return u"".join(u"\\u%04x" % ord(c) if ord(c) > 0xFF else c for c in u).encode("latin_1") but it would be quite inefficient as I have many to generate. -- Arnaud From roy at panix.com Sat Dec 3 17:29:07 2011 From: roy at panix.com (Roy Smith) Date: Sat, 03 Dec 2011 17:29:07 -0500 Subject: Django ported to Python3! References: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> Message-ID: In article , Ron wrote: > Django has such > enormous psychological significance for Python 3. Many important > projects will never begin serious porting until after Django > officially supports Python 3. And many Python folks will finally start > to take Python 3 seriously only when Django does announce official > support. In a somewhat related topic, it looks like Mongodb will also be supporting Python 3 soon. It's on the roadmap for their 2.2 release (https://jira.mongodb.org/browse/PYTHON-84). There's no date announced yet, but extrapolating from past release schedules, I'd guess mid 2012. From __peter__ at web.de Sat Dec 3 18:51:16 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 04 Dec 2011 00:51:16 +0100 Subject: How to generate java .properties files in python References: Message-ID: Arnaud Delobelle wrote: > I need to generate some java .properties files in Python (2.6 / 2.7). > It's a simple format to store key/value pairs e.g. > > blue=bleu > green=vert > red=rouge > > The key/value are unicode strings. The annoying thing is that the > file is encoded in ISO 8859-1, with all non Latin1 characters escaped > in the form \uHHHH (same as how unicode characters are escaped in > Python). > > I thought I could use the "unicode_escape" codec. But it doesn't work > because it escapes Latin1 characters with escape sequences of the form > \xHH, which is not valid in a java .properties file. > > Is there a simple way to achieve this? I could do something like this: > > def encode(u): > """encode a unicode string in .properties format""" > return u"".join(u"\\u%04x" % ord(c) if ord(c) > 0xFF else c for c > in u).encode("latin_1") > > but it would be quite inefficient as I have many to generate. >>> class D(dict): ... def __missing__(self, key): ... result = self[key] = u"\\u%04x" % key ... return result ... >>> d = D(enumerate(map(unichr, range(256)))) >>> u"?hnlich ?blich n?tig ???" u'\xe4hnlich \xfcblich n\xf6tig \u03a6\u03a7\u03a8' >>> u"?hnlich ?blich n?tig ???".translate(d) u'\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' >>> u"?hnlich ?blich n?tig ???".translate(d).encode("latin1") '\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' From rosuav at gmail.com Sat Dec 3 21:52:23 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Dec 2011 13:52:23 +1100 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On Sun, Dec 4, 2011 at 7:59 AM, Gelonida N wrote: > if you write code nicely enough in python 2, then you can translate it > ?to python 3. autmatically. It's entirely possible to write code that can run on both Python 2 and Python 3 - at least, if you can target 2.6/2.7 and get the appropriate future directives. Add in a few exception-guarded imports for the renamed modules, and then all you're left with is a few things where you can take a little care while coding, run it through both versions to test, and have it all work. ChrisA From tjreedy at udel.edu Sat Dec 3 21:52:32 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 03 Dec 2011 21:52:32 -0500 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On 12/3/2011 3:59 PM, Gelonida N wrote: > I would still stick with python 2. > > In my opinion there is no reason to rush to the most recent version. Python 3 is 3 years old. Starting with it now is hardly rushing. There are several reasons someone 'in the process of learning Python' might want to start with it. The removal of old stuff makes it more steamlined and easier to learn. There is only one class system instead of two, one meaning of '/' instead of two, and one version of the input, range, map, and filter functions instead of two. For anyone working with unicode instead of ascii, Python 3 is much better, and 3.3 will be better yet. There are numerous improvements to the standard library. Most but not all bugfixes have been backported; changes and new features have not. What's New in 3.0/1/2 is a long list of possible reasons. -- Terry Jan Reedy From rosuav at gmail.com Sat Dec 3 22:02:15 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Dec 2011 14:02:15 +1100 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On Sun, Dec 4, 2011 at 1:52 PM, Terry Reedy wrote: > For anyone working with unicode instead of ascii... Which, frankly, should be everyone. You can't get away with assuming that a character is a byte any more; even if you stick to the US, you're going to run into some non-ASCII symbols sooner or later. Of course, you can work with UTF-8, which means that anything that fits into 7-bit ASCII will be represented as itself; but you still need to be aware of the difference between 'bytes' and 'str' (or between 'str' and 'unicode'). ChrisA From arnodel at gmail.com Sun Dec 4 04:32:23 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sun, 4 Dec 2011 09:32:23 +0000 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: On 3 December 2011 23:51, Peter Otten <__peter__ at web.de> wrote: > Arnaud Delobelle wrote: > >> I need to generate some java .properties files in Python (2.6 / 2.7). >> It's a simple format to store key/value pairs e.g. >> >> blue=bleu >> green=vert >> red=rouge >> >> The key/value are unicode strings. ?The annoying thing is that the >> file is encoded in ISO 8859-1, with all non Latin1 characters escaped >> in the form \uHHHH (same as how unicode characters are escaped in >> Python). >> >> I thought I could use the "unicode_escape" codec. ?But it doesn't work >> because it escapes Latin1 characters with escape sequences of the form >> \xHH, which is not valid in a java .properties file. >> >> Is there a simple way to achieve this? I could do something like this: >> >> def encode(u): >> ? ? """encode a unicode string in .properties format""" >> ? ? return u"".join(u"\\u%04x" % ord(c) if ord(c) > 0xFF else c for c >> in u).encode("latin_1") >> >> but it would be quite inefficient as I have many to generate. > >>>> class D(dict): > ... ? ? def __missing__(self, key): > ... ? ? ? ? ? ? result = self[key] = u"\\u%04x" % key > ... ? ? ? ? ? ? return result > ... >>>> d = D(enumerate(map(unichr, range(256)))) >>>> u"?hnlich ?blich n?tig ???" > u'\xe4hnlich \xfcblich n\xf6tig \u03a6\u03a7\u03a8' >>>> u"?hnlich ?blich n?tig ???".translate(d) > u'\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' >>>> u"?hnlich ?blich n?tig ???".translate(d).encode("latin1") > '\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' A very nice solution - thanks, Peter. -- Arnaud From __peter__ at web.de Sun Dec 4 05:22:10 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 04 Dec 2011 11:22:10 +0100 Subject: How to generate java .properties files in python References: Message-ID: Arnaud Delobelle wrote: > On 3 December 2011 23:51, Peter Otten <__peter__ at web.de> wrote: >> Arnaud Delobelle wrote: >> >>> I need to generate some java .properties files in Python (2.6 / 2.7). >>> It's a simple format to store key/value pairs e.g. >>> >>> blue=bleu >>> green=vert >>> red=rouge >>> >>> The key/value are unicode strings. The annoying thing is that the >>> file is encoded in ISO 8859-1, with all non Latin1 characters escaped >>> in the form \uHHHH (same as how unicode characters are escaped in >>> Python). >>> >>> I thought I could use the "unicode_escape" codec. But it doesn't work >>> because it escapes Latin1 characters with escape sequences of the form >>> \xHH, which is not valid in a java .properties file. >>> >>> Is there a simple way to achieve this? I could do something like this: >>> >>> def encode(u): >>> """encode a unicode string in .properties format""" >>> return u"".join(u"\\u%04x" % ord(c) if ord(c) > 0xFF else c for c >>> in u).encode("latin_1") >>> >>> but it would be quite inefficient as I have many to generate. >> >>>>> class D(dict): >> ... def __missing__(self, key): >> ... result = self[key] = u"\\u%04x" % key >> ... return result >> ... >>>>> d = D(enumerate(map(unichr, range(256)))) >>>>> u"?hnlich ?blich n?tig ???" >> u'\xe4hnlich \xfcblich n\xf6tig \u03a6\u03a7\u03a8' >>>>> u"?hnlich ?blich n?tig ???".translate(d) >> u'\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' >>>>> u"?hnlich ?blich n?tig ???".translate(d).encode("latin1") >> '\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' > > A very nice solution - thanks, Peter. I found another one: >>> u"??? ???".encode("latin1", "backslashreplace") '\xe4\xf6\xfc \\u03a6\\u03a7\\u03a8' From lie.1296 at gmail.com Sun Dec 4 08:29:15 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 00:29:15 +1100 Subject: order independent hash? In-Reply-To: <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: On 12/02/2011 03:29 PM, 88888 Dihedral wrote: > > I clear my point a hash is a collection of (key, value) pairs that have > well defined methods and behavior to be used in programming. > > The basic operations of a hash normally includes the following: > > 1. insertion of a (key, value) pair into the hash > 2. deletion of a (key, value) from the hash > 3. inquiring a hash by a key to retrieve the value if the (key, value) > pair available in the hash. If no key matched, the hash will return > a not found result. > > The hash can grow with (k,v) pairs accumulated in the run time. > An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > Some implementations of a hash might pose some restrictions of k and v > for some reasons. But in object programming k and v can be objects > to be manipulated by the programmer. Strictly speaking, what you're describing is just a dictionary/mapping abstract data type (ADT), not a hashtable. Hashtable is a particular way to implement the dictionary/mapping ADT. Python's dictionary is implemented as hashtable, but there are other ways to implement a dictionary/mapping, such as using a sorted tree. For a data structure to be considered a Hashtable, in addition to having the properties of a dictionary that you described, the data structure must also uses a "hashing function" to encode the dictionary's keys into integer that will be used to calculate the index for the corresponding value in its internal array. A hashtable also must provide mechanism to deal with hash collisions to maintains its invariants as a dictionary. From lie.1296 at gmail.com Sun Dec 4 08:40:20 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 00:40:20 +1100 Subject: order independent hash? In-Reply-To: <1576053.251.1322804908048.JavaMail.geo-discussion-forums@pruu5> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <1576053.251.1322804908048.JavaMail.geo-discussion-forums@pruu5> Message-ID: On 12/02/2011 04:48 PM, 88888 Dihedral wrote: > On Friday, December 2, 2011 1:00:10 PM UTC+8, Chris Angelico wrote: >> On Fri, Dec 2, 2011 at 3:29 PM, 88888 Dihedral >> wrote: >>> I clear my point a hash is a collection of (key, value) pairs that have >>> well defined methods and behavior to be used in programming. >>> >>> The basic operations of a hash normally includes the following: >>> >>> 1. insertion of a (key, value) pair into the hash >>> 2. deletion of a (key, value) from the hash >>> 3. inquiring a hash by a key to retrieve the value if the (key, value) >>> pair available in the hash. If no key matched, the hash will return >>> a not found result. >>> >>> The hash can grow with (k,v) pairs accumulated in the run time. >>> An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. >> >> That's a hash table - think of a Python dictionary: >> >> On Fri, Dec 2, 2011 at 3:33 PM, Steven D'Aprano >> wrote: >>> Python dicts are hash tables. >> >> Although strictly speaking, isn't that "Python dicts are implemented >> as hash tables in CPython"? Or is the hashtable implementation >> mandated? Anyway, near enough. >> > > >> Cryptography and data verification use hashing too (look at the >> various historic hashing algorithms - CRC, MD5, SHA, etc). The concept >> of a hash is a number (usually of a fixed size) that is calculated >> from a string or other large data type, such that hashing the same >> input will always give the same output, but hashing different input >> will usually give different output. It's then possible to identify a >> large object solely by its hash, as is done in git, for instance; or >> to transmit both the data and the hash, as is done in message >> protection schemes (many archiving programs/formats include a hash of >> the uncompressed data). These have nothing to do with (key,value) >> pairs, but are important uses of hashes. >> >> ChrisA > > If one tries to insert a (k,v1) and then a (k,v2) pair into a > hash with v1 not equals V2, what could happen in your understanding of > a hash? Don't try to argue, in English, `hash != hash` is true; it's just a typical occurence of homonyms. Just because they have the same name doesn't mean hash (function) has to have somewhat similar properties to hash (table). > A hash function is different from a hash or so called a hash table in > my post. Indeed. > If the hash collision rate is not specified, then it is trivial to write a hash function with the conditions you specified. A hash function applied to a set of data items only is of very limited use at all. It's trivial indeed, but a hashtable couldn't exist without hash function. And without a good hash function, a hash table's performance may degrade into O(n) access/insertion/deletion. > A hash stores (k,v) pairs specified in the run time with auto memory > management build in is not a simple hash function to produce data signatures only clearly in my post. > > What I said a hash which is lifted as a basic type in python is called a dictionary in python. > > It is called a map in c++'s generics library. Putting aside all these, it's pretty obvious from the beginning that OP was referring to hash functions, not hash tables. From rschroev_nospam_ml at fastmail.fm Sun Dec 4 08:41:19 2011 From: rschroev_nospam_ml at fastmail.fm (Roel Schroeven) Date: Sun, 04 Dec 2011 14:41:19 +0100 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: <3MKCq.194015$th7.47228@newsfe07.ams2> Op 2011-12-02 6:48, 88888 Dihedral schreef: > A hash stores (k,v) pairs specified in the run time with auto memory > management build in is not a simple hash function to produce data > signatures only clearly in my post. > > What I said a hash which is lifted as a basic type in python is > called a dictionary in python. > > It is called a map in c++'s generics library. Not exactly: a C++ std::map uses a tree structure (which is why it keeps the keys sorted). C++ STL also has std::hash_map which, as the name implies, does use a hash table implementation. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven From hniksic at xemacs.org Sun Dec 4 08:55:32 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Sun, 04 Dec 2011 14:55:32 +0100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> Message-ID: <87aa78bfvv.fsf@xemacs.org> Terry Reedy writes: >> [Hashing is] pretty much mandated because of the __hash__ protocol. > > Lib Ref 4.8. Mapping Types ? dict > "A mapping object maps hashable values to arbitrary objects." > > This does not say that the mapping has to *use* the hash value ;-). > Even if it does, it could use a tree structure instead of a hash > table. An arbitrary mapping doesn't, but reference to the hash protocol was in the context of implementation constraints for dicts themselves (my response quotes the relevant part of Chris's message). If a Python implementation tried to implement dict as a tree, instances of classes that define only __eq__ and __hash__ would not be correctly inserted in such a dict. This would be a major source of incompatibility with Python code, both in the standard library and at large. From rosuav at gmail.com Sun Dec 4 09:08:53 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 01:08:53 +1100 Subject: order independent hash? In-Reply-To: <87aa78bfvv.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> Message-ID: 2011/12/5 Hrvoje Niksic : > If a Python > implementation tried to implement dict as a tree, instances of classes > that define only __eq__ and __hash__ would not be correctly inserted in > such a dict. Couldn't you just make a tree of hash values? Okay, that's probably not the most useful way to do things, but technically it'd comply with the spec. ChrisA From glen.rice.noaa at gmail.com Sun Dec 4 09:25:16 2011 From: glen.rice.noaa at gmail.com (Glen Rice) Date: Sun, 4 Dec 2011 06:25:16 -0800 (PST) Subject: struct calcsize discrepency? Message-ID: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> In IPython: >import struct >struct.calcsize('4s') 4 >struct.calcsize('Q') 8 >struct.calcsize('4sQ') 16 This doesn't make sense to me. Can anyone explain? From arnodel at gmail.com Sun Dec 4 09:33:34 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sun, 4 Dec 2011 14:33:34 +0000 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: On 4 December 2011 10:22, Peter Otten <__peter__ at web.de> wrote: > I found another one: > >>>> u"??? ???".encode("latin1", "backslashreplace") > '\xe4\xf6\xfc \\u03a6\\u03a7\\u03a8' That's it! I was hoping for a built-in solution and this is it. FTR, the 'backslashreplace' argument tells the encoder to replace any character that it can't encode with a backslash escape sequence. Which is exactly what I needed, only I hadn't realised it. Thanks! -- Arnaud From rosuav at gmail.com Sun Dec 4 09:35:28 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 01:35:28 +1100 Subject: struct calcsize discrepency? In-Reply-To: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> Message-ID: On Mon, Dec 5, 2011 at 1:25 AM, Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me. ?Can anyone explain? Same thing happens in CPython, and it looks to be the result of alignment. >>> struct.calcsize("4sQ") 16 >>> struct.calcsize("Q4s") 12 The eight-byte integer is aligned on an eight-byte boundary, so when it follows a four-byte string, you get four padding bytes inserted. Put them in the other order, and the padding disappears. (Caveat: I don't use the struct module much, this is based on conjecture.) ChrisA From duncan.booth at invalid.invalid Sun Dec 4 09:38:06 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 4 Dec 2011 14:38:06 GMT Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> Message-ID: Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me. Can anyone explain? > When you mix different types in a struct there can be padding inserted between the items. In this case the 8 byte unsigned long long must always start on an 8 byte boundary so 4 padding bytes are inserted. See http://docs.python.org/library/struct.html?highlight=struct#byte-order- size-and-alignment in particular the first sentence: "By default, C types are represented in the machine?s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler)." -- Duncan Booth http://kupuguy.blogspot.com From glen.rice.noaa at gmail.com Sun Dec 4 09:40:53 2011 From: glen.rice.noaa at gmail.com (Glen Rice) Date: Sun, 4 Dec 2011 06:40:53 -0800 (PST) Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> Message-ID: <59cea0e3-2f09-4f47-a0fc-c970037dc8e2@p14g2000yqp.googlegroups.com> On Dec 4, 9:38?am, Duncan Booth wrote: > Glen Rice wrote: > > In IPython: > >>import struct > >>struct.calcsize('4s') > > 4 > >>struct.calcsize('Q') > > 8 > >>struct.calcsize('4sQ') > > 16 > > > This doesn't make sense to me. ?Can anyone explain? > > When you mix different types in a struct there can be padding inserted > between the items. In this case the 8 byte unsigned long long must always > start on an 8 byte boundary so 4 padding bytes are inserted. > > Seehttp://docs.python.org/library/struct.html?highlight=struct#byte-order- > size-and-alignment in particular the first sentence: > > "By default, C types are represented in the machine s native format and > byte order, and properly aligned by skipping pad bytes if necessary > (according to the rules used by the C compiler)." > > -- > Duncan Boothhttp://kupuguy.blogspot.com Chris / Duncan, Thanks. I missed that in the docs. From __peter__ at web.de Sun Dec 4 09:49:36 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 04 Dec 2011 15:49:36 +0100 Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> Message-ID: Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me. Can anyone explain? A C compiler can insert padding bytes into a struct: """By default, the result of packing a given C struct includes pad bytes in order to maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. This behavior is chosen so that the bytes of a packed struct correspond exactly to the layout in memory of the corresponding C struct. To handle platform-independent data formats or omit implicit pad bytes, use standard size and alignment instead of native size and alignment: see Byte Order, Size, and Alignment for details. """ http://docs.python.org/library/struct.html#struct-alignment You can avoid this by specifying a non-native byte order (little endian, big endian, or "network"): >>> struct.calcsize("4sQ") 16 >>> struct.calcsize("!4sQ") 12 From d at davea.name Sun Dec 4 09:51:41 2011 From: d at davea.name (Dave Angel) Date: Sun, 04 Dec 2011 09:51:41 -0500 Subject: struct calcsize discrepency? In-Reply-To: References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> Message-ID: <4EDB88FD.9000108@davea.name> On 12/04/2011 09:35 AM, Chris Angelico wrote: > On Mon, Dec 5, 2011 at 1:25 AM, Glen Rice wrote: >> In IPython: >>> import struct >>> struct.calcsize('4s') >> 4 >>> struct.calcsize('Q') >> 8 >>> struct.calcsize('4sQ') >> 16 >> >> This doesn't make sense to me. Can anyone explain? > Same thing happens in CPython, and it looks to be the result of alignment. > >>>> struct.calcsize("4sQ") > 16 >>>> struct.calcsize("Q4s") > 12 > > The eight-byte integer is aligned on an eight-byte boundary, so when > it follows a four-byte string, you get four padding bytes inserted. > Put them in the other order, and the padding disappears. > NOT disappears. In C, the padding to the largest alignment occurs at the end of a structure as well as between items. Otherwise, an array of the struct would not be safely aligned. if you have an 8byte item followed by a 4 byte item, the total size is 16. > (Caveat: I don't use the struct module much, this is based on conjecture.) > > ChrisA -- DaveA From rosuav at gmail.com Sun Dec 4 10:17:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 02:17:41 +1100 Subject: struct calcsize discrepency? In-Reply-To: <4EDB88FD.9000108@davea.name> References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> <4EDB88FD.9000108@davea.name> Message-ID: On Mon, Dec 5, 2011 at 1:51 AM, Dave Angel wrote: > On 12/04/2011 09:35 AM, Chris Angelico wrote: >> >>>>> struct.calcsize("4sQ") >> >> 16 >>>>> >>>>> struct.calcsize("Q4s") >> >> 12 >> >> The eight-byte integer is aligned on an eight-byte boundary, so when >> it follows a four-byte string, you get four padding bytes inserted. >> Put them in the other order, and the padding disappears. >> > NOT disappears. ?In C, the padding to the largest alignment occurs at the > end of a structure as well as between items. ?Otherwise, an array of the > struct would not be safely aligned. ?if you have an 8byte item followed by a > 4 byte item, the total size is 16. That's padding of the array, not of the structure. But you're right in that removing padding from inside the structure will in this case result in padding outside the structure. However, in more realistic scenarios, it's often possible to truly eliminate padding by ordering members appropriately. ChrisA From dihedral88888 at googlemail.com Sun Dec 4 10:39:03 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 07:39:03 -0800 (PST) Subject: order independent hash? In-Reply-To: <3MKCq.194015$th7.47228@newsfe07.ams2> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> Message-ID: <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> On Sunday, December 4, 2011 9:41:19 PM UTC+8, Roel Schroeven wrote: > Op 2011-12-02 6:48, 88888 Dihedral schreef: > > A hash stores (k,v) pairs specified in the run time with auto memory > > management build in is not a simple hash function to produce data > > signatures only clearly in my post. > > > > What I said a hash which is lifted as a basic type in python is > > called a dictionary in python. > > > > It is called a map in c++'s generics library. > > Not exactly: a C++ std::map uses a tree structure (which is why it keeps > the keys sorted). C++ STL also has std::hash_map which, as the name > implies, does use a hash table implementation. > Thanks for your comments. Are we gonna talk about the way to implement a hash table or the use of a hash table in programming? From phd at phdru.name Sun Dec 4 12:24:59 2011 From: phd at phdru.name (Oleg Broytman) Date: Sun, 4 Dec 2011 21:24:59 +0400 Subject: SQLObject 1.2.1 Message-ID: <20111204172459.GC1538@iskra.aviel.ru> Hello! I'm pleased to announce version 1.2.1, the first stable release of branch 1.2 of SQLObject. What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as classes, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with. SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite, Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB). Where is SQLObject ================== Site: http://sqlobject.org Development: http://sqlobject.org/devel/ Mailing list: https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss Archives: http://news.gmane.org/gmane.comp.python.sqlobject Download: http://pypi.python.org/pypi/SQLObject/1.2.1 News and changes: http://sqlobject.org/News.html What's New ========== * A bug was fixed in handling ``modulo`` operator - SQLite implements only ``%``, MySQL - only ``MOD()``, PostgreSQL implements both. For a more complete list, please see the news: http://sqlobject.org/News.html Oleg. -- Oleg Broytman http://phdru.name/ phd at phdru.name Programmers don't die, they just GOSUB without RETURN. From ian.g.kelly at gmail.com Sun Dec 4 12:41:14 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 4 Dec 2011 10:41:14 -0700 Subject: order independent hash? In-Reply-To: <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> Message-ID: On Sun, Dec 4, 2011 at 8:39 AM, 88888 Dihedral wrote: > Thanks for your comments. Are we gonna talk about the way to implement a hash > table or the use of a hash table in programming? Implementing a hash table is not very relevant on a list about Python, which already has them built into the language; and any pure Python implementation would be uselessly slow. If you want to talk about ways to use dicts, please start a different thread for it. As has been pointed out several times now, it is off-topic for this thread, which is about hash *functions*. From dihedral88888 at googlemail.com Sun Dec 4 13:06:23 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 10:06:23 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> Message-ID: <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> A hash that can hash objects is not a trivial hash function On Monday, December 5, 2011 1:41:14 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 8:39 AM, 88888 Dihedral > wrote: > > Thanks for your comments. Are we gonna talk about the way to implement a hash > > table or the use of a hash table in programming? > > Implementing a hash table is not very relevant on a list about Python, > which already has them built into the language; and any pure Python > implementation would be uselessly slow. > > If you want to talk about ways to use dicts, please start a different > thread for it. As has been pointed out several times now, it is > off-topic for this thread, which is about hash *functions*. A hash that can hash objects is not a hash function at all. Are you miss-leading the power of true OOP ? From dihedral88888 at googlemail.com Sun Dec 4 13:06:23 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 10:06:23 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> Message-ID: <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> A hash that can hash objects is not a trivial hash function On Monday, December 5, 2011 1:41:14 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 8:39 AM, 88888 Dihedral > wrote: > > Thanks for your comments. Are we gonna talk about the way to implement a hash > > table or the use of a hash table in programming? > > Implementing a hash table is not very relevant on a list about Python, > which already has them built into the language; and any pure Python > implementation would be uselessly slow. > > If you want to talk about ways to use dicts, please start a different > thread for it. As has been pointed out several times now, it is > off-topic for this thread, which is about hash *functions*. A hash that can hash objects is not a hash function at all. Are you miss-leading the power of true OOP ? From wescpy at gmail.com Sun Dec 4 13:10:20 2011 From: wescpy at gmail.com (wesley chun) Date: Sun, 4 Dec 2011 10:10:20 -0800 Subject: LinuxJournal Readers' Choice Awards 2011 Best {Programming, Scripting} Language Message-ID: in recent news... Python wins LinuxJournal's Readers' Choice Awards 2011 as Best Programming Language: http://www.linuxjournal.com/slideshow/readers-choice-2011?page=27 yee-haw!! it's even more amazing that Python has won this title 3 straight years. let's celebrate and get back to building great things. wait, in other news... Python wins LinuxJournal's Readers' Choice Awards 2011 as Best Scripting Language: http://www.linuxjournal.com/slideshow/readers-choice-2011?page=28 interestingly enough, this happened last year as well: http://www.linuxjournal.com/content/readers-choice-awards-2010 in fact, Python has nearly won this one 6 straight years, from 2006-2011, except bash won in 2009. is it the same people who are voting (practically) every year? :-) cheers, --wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 ? ? http://corepython.com wesley.chun : wescpy-gmail.com : @wescpy python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From python.list at tim.thechases.com Sun Dec 4 13:57:50 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 04 Dec 2011 12:57:50 -0600 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> Message-ID: <4EDBC2AE.2070303@tim.thechases.com> On 12/04/11 08:08, Chris Angelico wrote: > 2011/12/5 Hrvoje Niksic: >> If a Python >> implementation tried to implement dict as a tree, instances of classes >> that define only __eq__ and __hash__ would not be correctly inserted in >> such a dict. > > Couldn't you just make a tree of hash values? Okay, that's probably > not the most useful way to do things, but technically it'd comply with > the spec. From an interface perspective, I suppose it would work. However one of the main computer-science reasons for addressing by a hash is to get O(1) access to items (modulo pessimal hash structures/algorithms which can approach O(N) if everything hashes to the same value/bucket), rather than the O(logN) time you'd get from a tree. So folks reaching for a hash/map might be surprised if performance degraded with the size of the contents. -tkc From ian.g.kelly at gmail.com Sun Dec 4 15:13:01 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 4 Dec 2011 13:13:01 -0700 Subject: order independent hash? In-Reply-To: <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> Message-ID: On Sun, Dec 4, 2011 at 11:06 AM, 88888 Dihedral wrote: >> If you want to talk about ways to use dicts, please start a different >> thread for it. ?As has been pointed out several times now, it is >> off-topic for this thread, which is about hash *functions*. > > A hash that can hash objects is not a hash function at all. Please explain what you think a hash function is, then. Per Wikipedia, "A hash function is any algorithm or subroutine that maps large data sets to smaller data sets, called keys." > Are you miss-leading the power of true OOP ? I have no idea what you are suggesting. I was not talking about OOP at all. From zaphod at beeblebrox.net Sun Dec 4 15:18:40 2011 From: zaphod at beeblebrox.net (Zaphod) Date: Sun, 04 Dec 2011 20:18:40 GMT Subject: Python 2 or 3 References: Message-ID: On Sat, 03 Dec 2011 05:54:19 +0200, Antti J Ylikoski wrote: > I'm in the process of learning Python. I already can code > objet-oriented programs with the language. I have in my hands the > O'Reilly book by Mark Lutz, Programming Python, in two versions: the 2nd > Edition, which covers Python 2, and the 4th edition, which covers Python > 3. It doesn't matter that much which one you learn. The differences aren't that big between the two. You could easily use the 2nd edition but use python 3 and the biggest difference you will find is between print "stuff" vs print("stuff"). In fact, doing it this way will make you a better programmer. > Cheers, Antti "Andy" Ylikoski > Helsinki, Finland, the EU ps: I quite like your Finnish "Dudesons" program. They remind me of some of the rednecks I grew up with here on Vancouver Island (British Columbia, Canada) From blair.bethwaite at gmail.com Sun Dec 4 18:10:53 2011 From: blair.bethwaite at gmail.com (Blairo) Date: Sun, 4 Dec 2011 15:10:53 -0800 (PST) Subject: SSL connection issue with Windows CPython Message-ID: Hi all, I've written a Python API for the Windows Azure Service Management web- service. Requests against the web-service are HTTPS with a client certificate used for authentication. This works fine with CPython (tested with 2.6 and 2.7) on Linux, but something is amiss with the SSL connection with vanilla python.org CPython (again 2.6 and 2.7) on Windows. I've managed to boil it down to a simple test case, which should return a list of Azure data-centre locations (requires an Azure account): -------------------- Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> sub_id = '506...-...-...-...-...' >>> cert = 'c:\\users\\blair\\...\\azure.pem' >>> import socket,ssl >>> s = ssl.wrap_socket(socket.socket(), certfile=cert) >>> s.connect(('management.core.windows.net',443)) >>> s.send("GET /%s/locations HTTP/1.1\r\nAccept-Encoding: identity\r\nX-Ms-Version: 2011-10-01\r\nHost: management.core.windows.net\r\nConnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n" % sub_id) 202 >>> s.read(2048) Traceback (most recent call last): File "", line 1, in s.read(2048) File "C:\Python27\lib\ssl.py", line 138, in read return self._sslobj.read(len) error: [Errno 10054] An existing connection was forcibly closed by the remote host -------------------- What's interesting is that the exact same code works with ActivePython (2.6 and 2.7), output omitted here for brevity. There is more detail on a (currently unanswered) stackoverflow post: http://stackoverflow.com/questions/8342714/python-https-against-azure-service-management-api-fails-on-windows . I'm not sure where/what the difference is. My best guess so far is that ActivePython bundles a newer version of OpenSSL then the python.org version and that the problem must be there. Any further insight would be appreciated. TIA, ~Blair From dihedral88888 at googlemail.com Sun Dec 4 18:17:21 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 15:17:21 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> Message-ID: <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> On Monday, December 5, 2011 4:13:01 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 11:06 AM, 88888 Dihedral > wrote: > >> If you want to talk about ways to use dicts, please start a different > >> thread for it. ?As has been pointed out several times now, it is > >> off-topic for this thread, which is about hash *functions*. > > > > A hash that can hash objects is not a hash function at all. > > Please explain what you think a hash function is, then. Per > Wikipedia, "A hash function is any algorithm or subroutine that maps > large data sets to smaller data sets, called keys." > > > Are you miss-leading the power of true OOP ? > > I have no idea what you are suggesting. I was not talking about OOP at all. In python the (k,v) pair in a dictionary k and v can be both an objects. v can be a tuple or a list. There are some restrictions on k to be an hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both mapped to the same position, then something has to be done to resolve this. From dihedral88888 at googlemail.com Sun Dec 4 18:17:21 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 15:17:21 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> Message-ID: <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> On Monday, December 5, 2011 4:13:01 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 11:06 AM, 88888 Dihedral > wrote: > >> If you want to talk about ways to use dicts, please start a different > >> thread for it. ?As has been pointed out several times now, it is > >> off-topic for this thread, which is about hash *functions*. > > > > A hash that can hash objects is not a hash function at all. > > Please explain what you think a hash function is, then. Per > Wikipedia, "A hash function is any algorithm or subroutine that maps > large data sets to smaller data sets, called keys." > > > Are you miss-leading the power of true OOP ? > > I have no idea what you are suggesting. I was not talking about OOP at all. In python the (k,v) pair in a dictionary k and v can be both an objects. v can be a tuple or a list. There are some restrictions on k to be an hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both mapped to the same position, then something has to be done to resolve this. From anacrolix at gmail.com Sun Dec 4 18:21:12 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Mon, 5 Dec 2011 10:21:12 +1100 Subject: order independent hash? In-Reply-To: <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: Duh. What's the point you're trying to make? On Mon, Dec 5, 2011 at 10:17 AM, 88888 Dihedral wrote: > On Monday, December 5, 2011 4:13:01 AM UTC+8, Ian wrote: >> On Sun, Dec 4, 2011 at 11:06 AM, 88888 Dihedral >> wrote: >> >> If you want to talk about ways to use dicts, please start a different >> >> thread for it. ?As has been pointed out several times now, it is >> >> off-topic for this thread, which is about hash *functions*. >> > >> > A hash that can hash objects is not a hash function at all. >> >> Please explain what you think a hash function is, then. ?Per >> Wikipedia, "A hash function is any algorithm or subroutine that maps >> large data sets to smaller data sets, called keys." >> >> > Are you miss-leading the power of true OOP ? >> >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > v can be a tuple or a list. ?There are some restrictions on k to be an > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > position, then something has to be done to resolve this. > > > > > > > -- > http://mail.python.org/mailman/listinfo/python-list -- ?_? From ian.g.kelly at gmail.com Sun Dec 4 18:24:49 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 4 Dec 2011 16:24:49 -0700 Subject: order independent hash? In-Reply-To: <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral wrote: >> Please explain what you think a hash function is, then. ?Per >> Wikipedia, "A hash function is any algorithm or subroutine that maps >> large data sets to smaller data sets, called keys." >> >> > Are you miss-leading the power of true OOP ? >> >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > v can be a tuple or a list. ?There are some restrictions on k to be an > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > position, then something has to be done to resolve this. I understand how dicts / hash tables work. I don't need you to explain that to me. What you haven't explained is why you stated that a hash function that operates on objects is not a hash function, or what you meant by "misleading the power of true OOP". From tjreedy at udel.edu Sun Dec 4 19:28:12 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 04 Dec 2011 19:28:12 -0500 Subject: order independent hash? In-Reply-To: <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: On 12/4/2011 6:17 PM, 88888 Dihedral wrote: > In python the (k,v) pair in a dictionary k and v can be both an objects. > v can be a tuple or a list. In Python, everything is an object. *tuple* and *list* are subclasses of *object*. The value v for a dict can be any object, and must be an object. -- Terry Jan Reedy From dihedral88888 at googlemail.com Sun Dec 4 19:52:14 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 16:52:14 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral > wrote: > >> Please explain what you think a hash function is, then. ?Per > >> Wikipedia, "A hash function is any algorithm or subroutine that maps > >> large data sets to smaller data sets, called keys." > >> > >> > Are you miss-leading the power of true OOP ? > >> > >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > > v can be a tuple or a list. ?There are some restrictions on k to be an > > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > > position, then something has to be done to resolve this. > > I understand how dicts / hash tables work. I don't need you to > explain that to me. What you haven't explained is why you stated that > a hash function that operates on objects is not a hash function, or > what you meant by "misleading the power of true OOP". If v is a tuple or a list then a dictionary in python can replace a bi-directional list or a tree under the assumption that the hash which accesses values stored in a much faster way when well implemented. From dihedral88888 at googlemail.com Sun Dec 4 19:52:14 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 16:52:14 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral > wrote: > >> Please explain what you think a hash function is, then. ?Per > >> Wikipedia, "A hash function is any algorithm or subroutine that maps > >> large data sets to smaller data sets, called keys." > >> > >> > Are you miss-leading the power of true OOP ? > >> > >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > > v can be a tuple or a list. ?There are some restrictions on k to be an > > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > > position, then something has to be done to resolve this. > > I understand how dicts / hash tables work. I don't need you to > explain that to me. What you haven't explained is why you stated that > a hash function that operates on objects is not a hash function, or > what you meant by "misleading the power of true OOP". If v is a tuple or a list then a dictionary in python can replace a bi-directional list or a tree under the assumption that the hash which accesses values stored in a much faster way when well implemented. From lie.1296 at gmail.com Sun Dec 4 20:59:46 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 12:59:46 +1100 Subject: order independent hash? In-Reply-To: <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> Message-ID: On 12/05/2011 11:52 AM, 88888 Dihedral wrote: > On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: >> On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral >> wrote: >>>> Please explain what you think a hash function is, then. Per >>>> Wikipedia, "A hash function is any algorithm or subroutine that maps >>>> large data sets to smaller data sets, called keys." >>>> >>>>> Are you miss-leading the power of true OOP ? >>>> >>>> I have no idea what you are suggesting. I was not talking about OOP at all. >>> >>> In python the (k,v) pair in a dictionary k and v can be both an objects. >>> v can be a tuple or a list. There are some restrictions on k to be an >>> hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both mapped to the same >>> position, then something has to be done to resolve this. >> >> I understand how dicts / hash tables work. I don't need you to >> explain that to me. What you haven't explained is why you stated that >> a hash function that operates on objects is not a hash function, or >> what you meant by "misleading the power of true OOP". > > If v is a tuple or a list then a dictionary in python can replace a bi-directional list or a tree under the assumption that the hash which accesses values stored in a much faster way when well implemented. trying not to be rude, but the more you talk, the more I"m convince that you're trolling. Welcome to my killfile. From steve+comp.lang.python at pearwood.info Sun Dec 4 21:00:53 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 02:00:53 GMT Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: <4edc25d5$0$11114$c3e8da3@news.astraweb.com> Dihedral, you're back to double posting. Please stop. Send to the mailing list, or to the newsgroup, it doesn't matter. But don't send to both. Further comments below. On Sun, 04 Dec 2011 16:52:14 -0800, 88888 Dihedral wrote: > If v is a tuple or a list then a dictionary in python can replace a > bi-directional list or a tree under the assumption that the hash which > accesses values stored in a much faster way when well implemented. No it can't. The keys in a hash tables are unordered. You get an order when you iterate over them, but that order is arbitrary. Keys in a list or tree are ordered. E.g. collections.OrderedDict remembers the order that keys are added. If Python were to replace OrderedDicts with dicts, it would break code. -- Steven From ethan at stoneleaf.us Sun Dec 4 21:08:01 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 04 Dec 2011 18:08:01 -0800 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> Message-ID: <4EDC2781.4020801@stoneleaf.us> Lie Ryan wrote: > On 12/05/2011 11:52 AM, 88888 Dihedral wrote: >> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: >>> On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral >>> wrote: >>>>> Please explain what you think a hash function is, then. Per >>>>> Wikipedia, "A hash function is any algorithm or subroutine that maps >>>>> large data sets to smaller data sets, called keys." >>>>> >>>>>> Are you miss-leading the power of true OOP ? >>>>> >>>>> I have no idea what you are suggesting. I was not talking about >>>>> OOP at all. >>>> >>>> In python the (k,v) pair in a dictionary k and v can be both an >>>> objects. >>>> v can be a tuple or a list. There are some restrictions on k to be an >>>> hashable type in python's implementation. The key is used to >>>> compute the position of the pair to be stored in a hash table. The >>>> hash function maps key k to the position in the hash table. If >>>> k1!=k2 are both mapped to the same >>>> position, then something has to be done to resolve this. >>> >>> I understand how dicts / hash tables work. I don't need you to >>> explain that to me. What you haven't explained is why you stated that >>> a hash function that operates on objects is not a hash function, or >>> what you meant by "misleading the power of true OOP". >> >> If v is a tuple or a list then a dictionary in python can replace a >> bi-directional list or a tree under the assumption that the hash >> which accesses values stored in a much faster way when well >> implemented. > > trying not to be rude, but the more you talk, the more I"m convince that > you're trolling. Welcome to my killfile. I think he's a bot, and he's been in my killfile for a while now. ~Ethan~ From anacrolix at gmail.com Sun Dec 4 22:02:46 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Mon, 5 Dec 2011 14:02:46 +1100 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> Message-ID: Yes. I sent a mail earlier asking such and it was bounced. I'm one email from also blocking this fellow. On Mon, Dec 5, 2011 at 12:59 PM, Lie Ryan wrote: > On 12/05/2011 11:52 AM, 88888 Dihedral wrote: >> >> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: >>> >>> On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral >>> ?wrote: >>>>> >>>>> Please explain what you think a hash function is, then. ?Per >>>>> Wikipedia, "A hash function is any algorithm or subroutine that maps >>>>> large data sets to smaller data sets, called keys." >>>>> >>>>>> Are you miss-leading the power of true OOP ? >>>>> >>>>> >>>>> I have no idea what you are suggesting. ?I was not talking about OOP at >>>>> all. >>>> >>>> >>>> In python the (k,v) pair in a dictionary k and v can be ?both an >>>> objects. >>>> v can be a tuple or a list. ?There are some restrictions on k to be an >>>> ?hashable type in python's implementation. The key is used to compute >>>> the position of the pair to be stored in a ?hash table. The hash function >>>> maps key k to the position in the hash table. If k1!=k2 are both ?mapped to >>>> the same >>>> position, then something has to be done to resolve this. >>> >>> >>> I understand how dicts / hash tables work. ?I don't need you to >>> explain that to me. ?What you haven't explained is why you stated that >>> a hash function that operates on objects is not a hash function, or >>> what you meant by "misleading the power of true OOP". >> >> >> If v is a tuple or a list then a dictionary in python can replace a >> bi-directional list or a tree under the assumption that the hash which >> ?accesses values stored in a ?much faster way ?when well implemented. > > > trying not to be rude, but the more you talk, the more I"m convince that > you're trolling. Welcome to my killfile. > > -- > http://mail.python.org/mailman/listinfo/python-list -- ?_? From ben+python at benfinney.id.au Sun Dec 4 23:52:16 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 05 Dec 2011 15:52:16 +1100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> Message-ID: <87obvn39j3.fsf@benfinney.id.au> Ethan Furman writes: > Lie Ryan wrote: > > trying not to be rude, but the more you talk, the more I"m convince > > that you're trolling. Welcome to my killfile. > > I think he's a bot, and he's been in my killfile for a while now. Having a ludicrous name doesn't help, and is part of what dropped them into my kill file. Maybe when they give a human-friendly name, and drop the insistence on being right rather than learning something, they can emerge from some of our kill files. -- \ ?We jealously reserve the right to be mistaken in our view of | `\ what exists, given that theories often change under pressure | _o__) from further investigation.? ?Thomas W. Clark, 2009 | Ben Finney From drsalists at gmail.com Mon Dec 5 00:50:08 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Sun, 4 Dec 2011 21:50:08 -0800 Subject: order independent hash? In-Reply-To: References: Message-ID: Two methods: 1) If you need your hash only once in an infrequent while, then save the elements in a list, appending as needed, and sort prior to hashing, as needed 2) If you need your hash more often, you could keep your elements in a treap or red-black tree; these will maintain sortedness throughout the life of the datastructure. 3) If A bunch of log(n) or n or nlog(n) operations doesn't sound appealing, then you might try this one: Create some sort of mapping from your elements to the integers. Then just use a sum. This won't scatter things nearly as well as a cryptographic hash, but it's very fast, because you don't need to reevaluate some of your members as you go. HTH On 11/30/11, Neal Becker wrote: > I like to hash a list of words (actually, the command line args of my > program) > in such a way that different words will create different hash, but not > sensitive > to the order of the words. Any ideas? > > -- > http://mail.python.org/mailman/listinfo/python-list > From ss27051980 at gmail.com Mon Dec 5 02:11:40 2011 From: ss27051980 at gmail.com (Suresh Sharma) Date: Mon, 5 Dec 2011 12:41:40 +0530 Subject: class print method... Message-ID: Hello All, I am new to python and i have stuck up on a particular issue with classes, i understand this might be a very dumb question but please help me out. I have created two classes and whenever i try to print the objects i get this message but not the data, __main__.cards instance at ****(memory location) i even tried using __str__ but calling it also produces the same result. Can anyone please help me how to get my objects printed. I googled a lot but could not find anything relevant. thanks in advance regards suresh -------------- next part -------------- An HTML attachment was scrubbed... URL: From dickinsm at gmail.com Mon Dec 5 02:42:22 2011 From: dickinsm at gmail.com (Mark Dickinson) Date: Sun, 4 Dec 2011 23:42:22 -0800 (PST) Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> <4EDB88FD.9000108@davea.name> Message-ID: On Dec 4, 3:17?pm, Chris Angelico wrote: > On Mon, Dec 5, 2011 at 1:51 AM, Dave Angel wrote: > > In C, the padding to the largest alignment occurs at the > > end of a structure as well as between items. ?Otherwise, an array of the > > struct would not be safely aligned. ?if you have an 8byte item followed by a > > 4 byte item, the total size is 16. > > That's padding of the array, not of the structure. That's a strange way to think of it, especially since the padding also happens for a single struct object when there's no array present. I find it cleaner to think of C as having no padding in arrays, but padding at the end of a struct. See C99 6.7.2.1p15: 'There may be unnamed padding at the end of a structure or union.' There's no mention in the standard of padding for arrays. -- Mark From d at davea.name Mon Dec 5 02:48:45 2011 From: d at davea.name (Dave Angel) Date: Mon, 05 Dec 2011 02:48:45 -0500 Subject: class print method... In-Reply-To: References: Message-ID: <4EDC775D.4000906@davea.name> On 12/05/2011 02:11 AM, Suresh Sharma wrote: > Hello All, > I am new to python and i have stuck up on a particular issue with classes, > i understand this might be a very dumb question but please help me out. > > I have created two classes and whenever i try to print the objects i get > this message but not the data, __main__.cards instance at ****(memory > location) i even tried using __str__ but calling it also produces the same > result. Can anyone please help me how to get my objects printed. I googled > a lot but could not find anything relevant. > > thanks in advance > > regards > suresh > You were close, but you have it backward. You don't call __str__() to print an object, you implement __str__() in your object. If you write a class without also writing __str__(), then print won't know what to do with it. -- DaveA From news3 at mystrobl.de Mon Dec 5 03:01:41 2011 From: news3 at mystrobl.de (Wolfgang Strobl) Date: Mon, 05 Dec 2011 09:01:41 +0100 Subject: Install Python on Windows without Start Menu icons? References: Message-ID: "Pedro Henrique G. Souto" : >On 02/12/2011 16:34, snorble wrote: >> Is it possible to automate the Python installation on Windows using >> the MSI file so it does not add a Start Menu folder? I would like to >> push out Python to all of my office workstations, but I'd like for it >> to be relatively silent from the user's point of view. > >If you just want to run python scripts in those machines (not developing >in it), you can use something like py2exe [http://www.py2exe.org/]. That doesn't answer the question. Snorble might use "ORCA", a MSI editor from Microsoft. http://forums.frontmotion.com/viewtopic.php?f=10&t=837 discusses a similar question for Firefox. I haven't tried it myself, but would give it a try. -- Thank you for observing all safety precautions From dpalao.python at gmail.com Mon Dec 5 03:02:08 2011 From: dpalao.python at gmail.com (DPalao) Date: Mon, 5 Dec 2011 09:02:08 +0100 Subject: 70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object In-Reply-To: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> References: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> Message-ID: <3650_1323072147_pB582GT7023203_201112050902.09089.dpalao.python@gmail.com> El Martes Noviembre 29 2011, DPalao escribi?: > Hello, > I'm trying to use multiprocessing to parallelize a code. There is a number > of tasks (usually 12) that can be run independently. Each task produces a > numpy array, and at the end, those arrays must be combined. > I implemented this using Queues (multiprocessing.Queue): one for input and > another for output. > But the code blocks. And it must be related to the size of the item I put > on the Queue: if I put a small array, the code works well; if the array is > realistically large (in my case if can vary from 160kB to 1MB), the code > blocks apparently forever. > I have tried this: > http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-lar > ge- objects/ > but it didn't work (especifically I put a None sentinel at the end for each > worker). > > Before I change the implementation, > is there a way to bypass this problem with multiprocessing.Queue? > Should I post the code (or a sketchy version of it)? > > TIA, > > David Just for reference. The other day I found the explanation by "ryles" on his/her mail of 27th aug 2009, with title "Re: Q: multiprocessing.Queue size limitations or bug...". It is very clarifying. After having read that I arranged the program such that the main process did not need to know when the others have finished, so I changed the process join call with a queue get call, until a None (one per process) is returned. Best, David From rosuav at gmail.com Mon Dec 5 03:09:13 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 19:09:13 +1100 Subject: struct calcsize discrepency? In-Reply-To: References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> <4EDB88FD.9000108@davea.name> Message-ID: On Mon, Dec 5, 2011 at 6:42 PM, Mark Dickinson wrote: > That's a strange way to think of it, especially since the padding also > happens for a single struct object when there's no array present. ?I > find it cleaner to think of C as having no padding in arrays, but > padding at the end of a struct. ?See C99 6.7.2.1p15: 'There may be > unnamed padding at the end of a structure or union.' ?There's no > mention in the standard of padding for arrays. May be, yes, but since calcsize() is returning 12 when the elements are put in the other order, it would seem to be not counting such padding. The way I look at it, padding is always used to place the beginning of something; in an array, it places the beginning of the second element on a convenient boundary, rather than filling out the first element to that boundary. I tried a similar thing with a couple of C compilers, and both of them gave the same sizeof() value for both orderings, which would imply that they _do_ include such padding in the structure's end. My statement that the padding was removed was based solely on calcsize()'s different result. ChrisA From mdickinson at enthought.com Mon Dec 5 03:20:32 2011 From: mdickinson at enthought.com (Mark Dickinson) Date: Mon, 5 Dec 2011 00:20:32 -0800 (PST) Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> <4EDB88FD.9000108@davea.name> Message-ID: On Dec 5, 8:09?am, Chris Angelico wrote: > May be, yes, but since calcsize() is returning 12 when the elements > are put in the other order, it would seem to be not counting such > padding. Indeed. That's arguably a bug in the struct module, and one that people have had to find workarounds for in the past. See note 3 at: http://docs.python.org/library/struct.html#byte-order-size-and-alignment If it weren't for backwards compatibility issues, I'd say that this should be fixed. -- Mark From alec.taylor6 at gmail.com Mon Dec 5 03:20:43 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Mon, 5 Dec 2011 19:20:43 +1100 Subject: Can't install pycrypto Message-ID: Good afternoon, Unfortunately my pycrpto install fails (tried with pip, easy_install and pip -e git+) Error: http://pastebin.com/wjjfTZvd How do I get this working? Thanks for all suggestions, Alec Taylor From boB Mon Dec 5 03:57:01 2011 From: boB (boB) Date: 5 Dec 2011 02:57:01 -0600 Subject: 70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object References: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> Message-ID: On Mon, 5 Dec 2011 09:02:08 +0100, DPalao wrote: >El Martes Noviembre 29 2011, DPalao escribi?: >> Hello, >> I'm trying to use multiprocessing to parallelize a code. There is a number >> of tasks (usually 12) that can be run independently. Each task produces a >> numpy array, and at the end, those arrays must be combined. >> I implemented this using Queues (multiprocessing.Queue): one for input and >> another for output. >> But the code blocks. And it must be related to the size of the item I put >> on the Queue: if I put a small array, the code works well; if the array is >> realistically large (in my case if can vary from 160kB to 1MB), the code >> blocks apparently forever. >> I have tried this: >> http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-lar >> ge- objects/ >> but it didn't work (especifically I put a None sentinel at the end for each >> worker). >> >> Before I change the implementation, >> is there a way to bypass this problem with multiprocessing.Queue? >> Should I post the code (or a sketchy version of it)? >> >> TIA, >> >> David > >Just for reference. The other day I found the explanation by "ryles" on >his/her mail of 27th aug 2009, with title "Re: Q: multiprocessing.Queue size >limitations or bug...". It is very clarifying. >After having read that I arranged the program such that the main process did >not need to know when the others have finished, so I changed the process join >call with a queue get call, until a None (one per process) is returned. > >Best, > >David Why do people add character like [* SPAM *] to their subject lines ?? Is it supposed to do something ?? I figured since programmers hang out here, maybe one of you know this. Thanks, boB From rosuav at gmail.com Mon Dec 5 04:11:28 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 20:11:28 +1100 Subject: Spam in subject lines (Re: 70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object) Message-ID: On Mon, Dec 5, 2011 at 7:57 PM, wrote: > Why do people add character ?like ? ?[* SPAM *] ?to their ?subject > lines ?? ? Is it supposed to do something ??? ? I figured since > programmers hang out here, maybe one of you know this. People don't. It's something added by a spam filter that thought that the email was likely to be junk (in this case, a 70% probability thereof). If you're curious as to exactly _why_ the filter thought that, check the email headers - it's all there, in exhaustive detail. The purpose of the tag is to let recipients make some simple filter rules (eg "if it has SPAM in the subject, put it in a separate folder") and/or to be able to eyeball spam easily. Chris Angelico From steve+comp.lang.python at pearwood.info Mon Dec 5 04:41:06 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 09:41:06 GMT Subject: Spam in subject lines (Re: multiprocessing.Queue blocks when sending large object) References: Message-ID: <4edc91b2$0$11114$c3e8da3@news.astraweb.com> On Mon, 05 Dec 2011 20:11:28 +1100, Chris Angelico wrote: > On Mon, Dec 5, 2011 at 7:57 PM, wrote: >> Why do people add character ?like ? ?[* SPAM *] ?to their ?subject >> lines ?? ? Is it supposed to do something ??? ? I figured since >> programmers hang out here, maybe one of you know this. > > People don't. It's something added by a spam filter that thought that > the email was likely to be junk (in this case, a 70% probability > thereof). If you're curious as to exactly _why_ the filter thought that, > check the email headers - it's all there, in exhaustive detail. Hilariously, I have occasionally received spam where the spammer included SPAM in their subject line under the mistaken impression that this sign of honesty would make me more likely to read the body of the email. Actually, in hindsight, not so mistaken really... -- Steven From ss27051980 at gmail.com Mon Dec 5 06:18:12 2011 From: ss27051980 at gmail.com (Suresh Sharma) Date: Mon, 5 Dec 2011 16:48:12 +0530 Subject: Fwd: class print method... In-Reply-To: References: <4EDC775D.4000906@davea.name> Message-ID: Pls help its really frustrating ---------- Forwarded message ---------- From: Suresh Sharma Date: Monday, December 5, 2011 Subject: class print method... To: "d at davea.name" Dave, Thanx for the quick response, i am sorry that i did not explain correctly look at the code below inspite of this i am just getting class object at memory location.I am sort i typed all this code on my android in a hurry so.indentation could.not.be.managed but this.similar code when i run all my objects created by class deck are not shown but stored in varioia meory locations. How can i display them. Please help Suit=[aces,clubs,diamonds,hearts] Rank=[2,3,4,5,6,7,8,9,j,Q,K,A] class Card: Def __init__(self,rannk,suiit): Self.suiit=suiit Self.rannk=rannk Def __str__(self): Return suit[suiit],rank[rannk] Class deck: Def __init__(cards): Self.cards=[ ] For suit in range(4): For rank in range(13): Self.cards.append(Card(suit,rank)) Def __str__(self): s = "" For card in self.cards: S = str(self.cards) Return s. On Monday, December 5, 2011, Dave Angel wrote: > On 12/05/2011 02:11 AM, Suresh Sharma wrote: >> >> Hello All, >> I am new to python and i have stuck up on a particular issue with classes, >> i understand this might be a very dumb question but please help me out. >> >> I have created two classes and whenever i try to print the objects i get >> this message but not the data, __main__.cards instance at ****(memory >> location) i even tried using __str__ but calling it also produces the same >> result. Can anyone please help me how to get my objects printed. I googled >> a lot but could not find anything relevant. >> >> thanks in advance >> >> regards >> suresh >> > You were close, but you have it backward. You don't call __str__() to print an object, you implement __str__() in your object. > > If you write a class without also writing __str__(), then print won't know what to do with it. > > -- > > DaveA > > -- Suresh Sharma Regional Project Manager, O2F,Mumbai Maharashtra-400101. -- Suresh Sharma Regional Project Manager, O2F,Mumbai Maharashtra-400101. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon Dec 5 06:35:51 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 22:35:51 +1100 Subject: class print method... In-Reply-To: References: <4EDC775D.4000906@davea.name> Message-ID: On Mon, Dec 5, 2011 at 10:18 PM, Suresh Sharma wrote: > > Suit=[aces,clubs,diamonds,hearts] > Rank=[2,3,4,5,6,7,8,9,j,Q,K,A] > class Card: > Def __init__(self,rannk,suiit): > Self.suiit=suiit > Self.rannk=rannk > > Def __str__(self): > Return suit[suiit],rank[rannk] > > Class deck: > Def __init__(cards): > Self.cards=[ ] > For suit in range(4): > For rank in range(13): > Self.cards.append(Card(suit,rank)) > > Def __str__(self): > s = "" > For card in self.cards: > S = str(self.cards) > Return s. Python is case sensitive, and indentation is significant. Paste your code exactly as you're trying to run it, and we'll be better able to help you. Chris Angelico From dihedral88888 at googlemail.com Mon Dec 5 07:09:34 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 5 Dec 2011 04:09:34 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <952995.674.1323086974877.JavaMail.geo-discussion-forums@preu18> On Monday, December 5, 2011 1:50:08 PM UTC+8, Dan Stromberg wrote: > Two methods: > 1) If you need your hash only once in an infrequent while, then save > the elements in a list, appending as needed, and sort prior to > hashing, as needed > > 2) If you need your hash more often, you could keep your elements in a > treap or red-black tree; these will maintain sortedness throughout the > life of the datastructure. > > 3) If A bunch of log(n) or n or nlog(n) operations doesn't sound > appealing, then you might try this one: Create some sort of mapping > from your elements to the integers. Then just use a sum. This won't > scatter things nearly as well as a cryptographic hash, but it's very > fast, because you don't need to reevaluate some of your members as you > go. > > HTH > A sorted list can behave like a hash table. This is of O(log(n)) in accesses of n items in theory. I agree with you a hash key computation method too slow than a list of n items in accesses for a range of n items should be reloadable. But this is not supported in Python yet. For tedious trivial jobs of non-heavy computing , I'll opt for easy use. From dihedral88888 at googlemail.com Mon Dec 5 07:09:34 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 5 Dec 2011 04:09:34 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <952995.674.1323086974877.JavaMail.geo-discussion-forums@preu18> On Monday, December 5, 2011 1:50:08 PM UTC+8, Dan Stromberg wrote: > Two methods: > 1) If you need your hash only once in an infrequent while, then save > the elements in a list, appending as needed, and sort prior to > hashing, as needed > > 2) If you need your hash more often, you could keep your elements in a > treap or red-black tree; these will maintain sortedness throughout the > life of the datastructure. > > 3) If A bunch of log(n) or n or nlog(n) operations doesn't sound > appealing, then you might try this one: Create some sort of mapping > from your elements to the integers. Then just use a sum. This won't > scatter things nearly as well as a cryptographic hash, but it's very > fast, because you don't need to reevaluate some of your members as you > go. > > HTH > A sorted list can behave like a hash table. This is of O(log(n)) in accesses of n items in theory. I agree with you a hash key computation method too slow than a list of n items in accesses for a range of n items should be reloadable. But this is not supported in Python yet. For tedious trivial jobs of non-heavy computing , I'll opt for easy use. From lie.1296 at gmail.com Mon Dec 5 07:27:59 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 23:27:59 +1100 Subject: 70% [* SPAM *] multiprocessing.Queue blocks when sending large object In-Reply-To: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> References: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> Message-ID: On 11/30/2011 06:09 AM, DPalao wrote: > Hello, > I'm trying to use multiprocessing to parallelize a code. There is a number of > tasks (usually 12) that can be run independently. Each task produces a numpy > array, and at the end, those arrays must be combined. > I implemented this using Queues (multiprocessing.Queue): one for input and > another for output. > But the code blocks. And it must be related to the size of the item I put on > the Queue: if I put a small array, the code works well; if the array is > realistically large (in my case if can vary from 160kB to 1MB), the code > blocks apparently forever. > I have tried this: > http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-large- > objects/ > but it didn't work (especifically I put a None sentinel at the end for each > worker). > > Before I change the implementation, > is there a way to bypass this problem with multiprocessing.Queue? > Should I post the code (or a sketchy version of it)? Transferring data over multiprocessing.Queue involves copying the whole object across an inter-process pipe, so you need to have a reasonably large workload in the processes to justify the cost of the copying to benefit from running the workload in parallel. You may try to avoid the cost of copying by using shared memory (http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes); you can use Queue for communicating when a new data comes in or when a task is done, but put the large data in shared memory. Be careful not to access the data from multiple processes concurrently. In any case, have you tried a multithreaded solution? numpy is a C extension, and I believe it releases the GIL when working, so it wouldn't be in your way to achieve parallelism. From lie.1296 at gmail.com Mon Dec 5 07:41:25 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 23:41:25 +1100 Subject: Fwd: class print method... In-Reply-To: References: <4EDC775D.4000906@davea.name> Message-ID: On 12/05/2011 10:18 PM, Suresh Sharma wrote: > > Pls help its really frustrating > ---------- Forwarded message ---------- > From: Suresh Sharma > Date: Monday, December 5, 2011 > Subject: class print method... > To: "d at davea.name " > > > > Dave, > Thanx for the quick response, i am sorry that i did not explain > correctly look at the code below inspite of this i am just getting class > object at memory location.I am sort i typed all this code on my android > in a hurry so.indentation could.not.be.managed but this.similar code > when i run all my objects created by class deck are not shown but stored > in varioia meory locations. How can i display them. > I think you're in the right track, however I suspect you're running the code in the shell instead of as a script. The shell uses __repr__() to print objects instead of __str__(), so you either need to use 'print' or you need to call str(), note the following: Python 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> suits = ['spades', 'clubs', 'diamonds', 'hearts'] >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] >>> class Card: ... def __init__(self, rank, suit): ... self.suit = suit ... self.rank = rank ... def __str__(self): ... return suits[self.suit] + ' ' + ranks[self.rank] ... >>> Card(2, 3) #1 <__main__.Card instance at 0x7f719c3a20e0> >>> str(Card(2, 3)) #2 of your 'hearts 3' >>> print Card(2, 3) #3 hearts 3 In #1, the output is the __repr__() of your Card class; you can modify this output by overriding the __repr__() on your Card class. In #2, the output is the __repr__() of a string, the string is the return value from __str__() of your Card class. The repr of a string is the string enclosed in quotes, which is why there is an extra pair of quotes. In #3, you're 'print'-ing a string, the string is the return value from __str__() of your Card class. There's no extra quotes, since 'print' prints the string itself, not the repr of the string. From lie.1296 at gmail.com Mon Dec 5 07:52:37 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 23:52:37 +1100 Subject: Install Python on Windows without Start Menu icons? In-Reply-To: References: Message-ID: On 12/05/2011 07:01 PM, Wolfgang Strobl wrote: > "Pedro Henrique G. Souto": > >> On 02/12/2011 16:34, snorble wrote: >>> Is it possible to automate the Python installation on Windows using >>> the MSI file so it does not add a Start Menu folder? I would like to >>> push out Python to all of my office workstations, but I'd like for it >>> to be relatively silent from the user's point of view. >> >> If you just want to run python scripts in those machines (not developing >> in it), you can use something like py2exe [http://www.py2exe.org/]. > > That doesn't answer the question. But it may solve his problem, which is the whole point. Often in discussions, an OP may ask the wrong question that does not solve their problem or is a roundabout way to solve their problem (and it may not necessarily be their fault that they asked the wrong question). Which is why it is often best to describe the actual problem in addition to asking a specific question. From d at davea.name Mon Dec 5 08:09:49 2011 From: d at davea.name (Dave Angel) Date: Mon, 05 Dec 2011 08:09:49 -0500 Subject: Fwd: class print method... In-Reply-To: References: <4EDC775D.4000906@davea.name> Message-ID: <4EDCC29D.7010903@davea.name> On 12/05/2011 07:41 AM, Lie Ryan wrote: > On 12/05/2011 10:18 PM, Suresh Sharma wrote: >> >> Pls help its really frustrating >> ---------- Forwarded message ---------- >> From: Suresh Sharma >> Date: Monday, December 5, 2011 >> Subject: class print method... >> To: "d at davea.name " > > >> >> >> Dave, >> Thanx for the quick response, i am sorry that i did not explain >> correctly look at the code below inspite of this i am just getting class >> object at memory location.I am sort i typed all this code on my android >> in a hurry so.indentation could.not.be.managed but this.similar code >> when i run all my objects created by class deck are not shown but stored >> in varioia meory locations. How can i display them. >> > > I think you're in the right track, however I suspect you're running > the code in the shell instead of as a script. The shell uses > __repr__() to print objects instead of __str__(), so you either need > to use 'print' or you need to call str(), note the following: > > Python 2.7.2+ (default, Oct 4 2011, 20:06:09) > [GCC 4.6.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> suits = ['spades', 'clubs', 'diamonds', 'hearts'] > >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', > 'Q', 'K'] > >>> class Card: > ... def __init__(self, rank, suit): > ... self.suit = suit > ... self.rank = rank > ... def __str__(self): > ... return suits[self.suit] + ' ' + ranks[self.rank] > ... > >>> Card(2, 3) #1 > <__main__.Card instance at 0x7f719c3a20e0> > >>> str(Card(2, 3)) #2 of your > 'hearts 3' > >>> print Card(2, 3) #3 > hearts 3 > > In #1, the output is the __repr__() of your Card class; you can modify > this output by overriding the __repr__() on your Card class. > > In #2, the output is the __repr__() of a string, the string is the > return value from __str__() of your Card class. The repr of a string > is the string enclosed in quotes, which is why there is an extra pair > of quotes. > > In #3, you're 'print'-ing a string, the string is the return value > from __str__() of your Card class. There's no extra quotes, since > 'print' prints the string itself, not the repr of the string. > Lie is most likely correct. When I saw your message, i saw many bugs, but didn't know which were caused by your retyping, and which were actually there. I also saw no mainline. While i was deciding how to respond, Lie came up with his answer. Notice that he fixed lots of your bugs, and explains the distinction of print, as you'd use in your program, and the output of the debugger (or some IDEs. There are other places where defining a __str__() can be useful, such as if you print a list. The __str__() method of list calls the __repr__() for each object inside it. So it can be useful to define both, even if you decide to have them do the same thing. Lie didn't include your Deck class, which has more problems. If you really want print to work on the whole Deck, you're going to have to build a single string for the whole thing. Currently you just return the name of the first card. Instead of return inside the loop, you'll have to construct a string (perhaps by concatenating a new value each time through), and put the return after the loop. Notice also that you top-posted, which is the wrong place for your comments. Please put your comments after whatever part you're quoting from other messages, as Lie and I both demonstrate. -- DaveA From bex.lewis at gmail.com Mon Dec 5 08:38:45 2011 From: bex.lewis at gmail.com (becky_lewis) Date: Mon, 5 Dec 2011 05:38:45 -0800 (PST) Subject: Can't install pycrypto References: Message-ID: <832e164a-d1ab-4be2-ad82-df951a9328fe@n10g2000vbg.googlegroups.com> My best guess from the error and a quick look at the setup.py file is that during setup "chmod 0755 configure" is trying to run but failing. I'm guessing that you're on windows and don't actually have chmod, hence the error. The project on github looks active so you could go and ask for a windows friendly setup.py file and install using: python setup.py install or you could attempt to edit it yourself. I'd suggest asking for a windows friendly file on github as they will either give you a reason for it not being possible or will make the fix and everybody can benefit :) Hope that helps, Becky Lewis On Dec 5, 8:20?am, Alec Taylor wrote: > Good afternoon, > > Unfortunately my pycrpto install fails (tried with pip, easy_install > and pip -e git+) > > Error:http://pastebin.com/wjjfTZvd > > How do I get this working? > > Thanks for all suggestions, > > Alec Taylor From patrickwayodi at gmail.com Mon Dec 5 09:53:28 2011 From: patrickwayodi at gmail.com (patrickwayodi) Date: Mon, 5 Dec 2011 06:53:28 -0800 (PST) Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) Message-ID: Hi, How can I install Python on Debian GNU/Linux? I downloaded the file "Python-2.7.2.tar.bz2" but I don't know how to install it. Patrick. From rosuav at gmail.com Mon Dec 5 10:08:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 02:08:55 +1100 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) In-Reply-To: References: Message-ID: On Tue, Dec 6, 2011 at 1:53 AM, patrickwayodi wrote: > Hi, > How can I install Python on Debian GNU/Linux? I downloaded the file > "Python-2.7.2.tar.bz2" but I don't know how to install it. You should actually already have Python installed. Try typing 'python' at a terminal and see if it invokes the interactive interpreter. ChrisA From brian at python.org Mon Dec 5 10:11:32 2011 From: brian at python.org (Brian Curtin) Date: Mon, 5 Dec 2011 09:11:32 -0600 Subject: Install Python on Windows without Start Menu icons? In-Reply-To: References: Message-ID: On Fri, Dec 2, 2011 at 12:34, snorble wrote: > Is it possible to automate the Python installation on Windows using > the MSI file so it does not add a Start Menu folder? I would like to > push out Python to all of my office workstations, but I'd like for it > to be relatively silent from the user's point of view. > -- > http://mail.python.org/mailman/listinfo/python-list > http://www.python.org/getit/releases/2.4/msi/ outlines some of the MSI command line features, but I don't see the option to not add the Start Menu folder. -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrickwayodi at gmail.com Mon Dec 5 10:40:34 2011 From: patrickwayodi at gmail.com (patrickwayodi) Date: Mon, 5 Dec 2011 07:40:34 -0800 (PST) Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) References: Message-ID: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> > You should actually already have Python installed. Try typing 'python' > at a terminal and see if it invokes the interactive interpreter. > > ChrisA Yes, I have Python installed, but it's an old version. So I want to upgrade to "Python-2.7.2.tar.bz2". From rosuav at gmail.com Mon Dec 5 10:51:21 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 02:51:21 +1100 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) In-Reply-To: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> Message-ID: On Tue, Dec 6, 2011 at 2:40 AM, patrickwayodi wrote: >> You should actually already have Python installed. Try typing 'python' >> at a terminal and see if it invokes the interactive interpreter. >> >> ChrisA > > > Yes, I have Python installed, but it's an old version. So I want to > upgrade to "Python-2.7.2.tar.bz2". Ah gotcha. I believe you can 'sudo apt-get install python2.7' - at least, you can on the Ubuntu system next to me. Not sure though; I build my Python from source straight from Mercurial. What you have there, I think, is a source code snapshot. You'd need to extract it and then do the usual incantation: $ ./configure $ make $ sudo make install If you don't have the compiler/build environment set up, you'll have to do that first. If you aren't too concerned about the exact version you get, the above apt-get line should get you a stable Python in the 2.7 branch. ChrisA From steve+comp.lang.python at pearwood.info Mon Dec 5 11:09:43 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 16:09:43 GMT Subject: Backspace does not erase in stdout Message-ID: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> I have a function which reads characters from stdin and writes stars to stdout, but backspacing does not erase the stars as I expected. Tested in Python 2.6 on Linux. This will almost certainly not work on Windows. import sys, tty, termios def getpass(): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) chars = [] try: tty.setraw(sys.stdin.fileno()) while 1: c = sys.stdin.read(1) if c in '\n\r': # Enter or Return key. break elif c == '\x7f': # Backspace key. if chars: # Rubout previous character. sys.stdout.write('\b') sys.stdout.flush() del chars[-1] else: # Obfuscate the letter typed. sys.stdout.write('*') chars.append(c) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) sys.stdout.write('\n') return ''.join(chars) When I call this function and then type, I get a serious of asterisks as expected. Each time I press the backspace key, the cursor moves one character to the left, but the asterisks remain visible. Is there a way to erase the character other than backspacing, writing a space, then backspacing again? That feels inelegant. -- Steven From rosuav at gmail.com Mon Dec 5 11:13:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 03:13:41 +1100 Subject: Backspace does not erase in stdout In-Reply-To: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano wrote: > > Is there a way to erase the character other than backspacing, writing a > space, then backspacing again? That feels inelegant. Emitting "\b \b" is one very common way to do a destructive backspace. Inelegant? Perhaps, but a common inelegance. ChrisA From steve+comp.lang.python at pearwood.info Mon Dec 5 11:23:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 16:23:21 GMT Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> Message-ID: <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Dec 2011 02:51:21 +1100, Chris Angelico wrote: > On Tue, Dec 6, 2011 at 2:40 AM, patrickwayodi > wrote: >>> You should actually already have Python installed. Try typing 'python' >>> at a terminal and see if it invokes the interactive interpreter. >>> >>> ChrisA >> >> >> Yes, I have Python installed, but it's an old version. So I want to >> upgrade to "Python-2.7.2.tar.bz2". > > Ah gotcha. I believe you can 'sudo apt-get install python2.7' - at > least, you can on the Ubuntu system next to me. Not sure though; I build > my Python from source straight from Mercurial. > > What you have there, I think, is a source code snapshot. You'd need to > extract it and then do the usual incantation: $ ./configure > $ make > $ sudo make install And you have now potentially broken your system python :( Generally speaking, unless you are an expert, you should not use make install when installing Python from source, because it will replace the system Python with the newly installed one. Instead use `sudo make altinstall`. This is exactly the same as install, except it won't replace the python symlink that points to the actual Python executable. That way system tools that call Python get the version they are expecting, together with any libraries installed for their use, while you can call the version you prefer manually. Or set up an alias in your bashrc file. > If you don't have the compiler/build environment set up, you'll have to > do that first. The trickiest part for me is ensuring that tkinter works correctly. After installing Python from source about a dozen times now, I still don't know why sometimes it works and sometimes it doesn't. -- Steven From invalid at invalid.invalid Mon Dec 5 11:23:55 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 5 Dec 2011 16:23:55 +0000 (UTC) Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-12-05, Chris Angelico wrote: > On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano > wrote: >> >> Is there a way to erase the character other than backspacing, writing a >> space, then backspacing again? That feels inelegant. > > Emitting "\b \b" is one very common way to do a destructive backspace. > Inelegant? Perhaps, but a common inelegance. That's pretty much the only way I've seen it done for the past 25 years. What would be more elegant? -- Grant Edwards grant.b.edwards Yow! This PIZZA symbolizes at my COMPLETE EMOTIONAL gmail.com RECOVERY!! From rosuav at gmail.com Mon Dec 5 11:28:27 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 03:28:27 +1100 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) In-Reply-To: <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Dec 6, 2011 at 3:23 AM, Steven D'Aprano wrote: > Generally speaking, unless you are an expert, you should not use make > install when installing Python from source, because it will replace the > system Python with the newly installed one. > > Instead use `sudo make altinstall` Mea culpa, forgot that. Yes, use altinstall. Although it's probably not a problem to replace 2.6.6 with 2.7.2 - I doubt that'll break many things. ChrisA From lists at cheimes.de Mon Dec 5 11:37:08 2011 From: lists at cheimes.de (Christian Heimes) Date: Mon, 05 Dec 2011 17:37:08 +0100 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) In-Reply-To: <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 05.12.2011 17:23, schrieb Steven D'Aprano: > The trickiest part for me is ensuring that tkinter works correctly. After > installing Python from source about a dozen times now, I still don't know > why sometimes it works and sometimes it doesn't. sudo apt-get build-dep python2.7 Done ;) However there are additional pitfalls if you have multiarch support or Kernel 3.x. Both are well documented in my blog http://lipyrary.blogspot.com/ Christian From lists at cheimes.de Mon Dec 5 11:39:40 2011 From: lists at cheimes.de (Christian Heimes) Date: Mon, 05 Dec 2011 17:39:40 +0100 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) In-Reply-To: References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 05.12.2011 17:28, schrieb Chris Angelico: > On Tue, Dec 6, 2011 at 3:23 AM, Steven D'Aprano > wrote: >> Generally speaking, unless you are an expert, you should not use make >> install when installing Python from source, because it will replace the >> system Python with the newly installed one. >> >> Instead use `sudo make altinstall` > > Mea culpa, forgot that. Yes, use altinstall. Although it's probably > not a problem to replace 2.6.6 with 2.7.2 - I doubt that'll break many > things. Except that all 3rd party extensions and packages are missing if you install Python manually. Unless you *really* know what you are doing you shouldn't install Python manually. Debian's backports should provide a well integrated Python 2.7 package. Christian From roy at panix.com Mon Dec 5 12:04:52 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 09:04:52 -0800 (PST) Subject: Scope of variable inside list comprehensions? Message-ID: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Consider the following django snippet. Song(id) raises DoesNotExist if the id is unknown. try: songs = [Song(id) for id in song_ids] except Song.DoesNotExist: print "unknown song id (%d)" % id Is id guaranteed to be in scope in the print statement? I found one thread (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) which says yes, but hints that it might not always be in the future. Now that we're in the future, is that still true? And for Python 3 also? The current docs, http://docs.python.org/tutorial/datastructures.html#list-comprehensions, are mute on this point. From steve+comp.lang.python at pearwood.info Mon Dec 5 12:24:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 17:24:59 GMT Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4edcfe6b$0$29988$c3e8da3$5496439d@news.astraweb.com> On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > On 2011-12-05, Chris Angelico wrote: >> On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano >> wrote: >>> >>> Is there a way to erase the character other than backspacing, writing >>> a space, then backspacing again? That feels inelegant. >> >> Emitting "\b \b" is one very common way to do a destructive backspace. >> Inelegant? Perhaps, but a common inelegance. > > That's pretty much the only way I've seen it done for the past 25 years. > > What would be more elegant? For backspace to actually backspace, and not just move the cursor. Thanks for those who answered, I guess I'll just do the backspace, space, backspace dance. -- Steven From invalid at invalid.invalid Mon Dec 5 12:40:38 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 5 Dec 2011 17:40:38 +0000 (UTC) Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> <4edcfe6b$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-12-05, Steven D'Aprano wrote: > On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > >> On 2011-12-05, Chris Angelico wrote: >>> On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano >>> wrote: >>>> >>>> Is there a way to erase the character other than backspacing, writing >>>> a space, then backspacing again? That feels inelegant. >>> >>> Emitting "\b \b" is one very common way to do a destructive backspace. >>> Inelegant? Perhaps, but a common inelegance. >> >> That's pretty much the only way I've seen it done for the past 25 years. >> >> What would be more elegant? > > For backspace to actually backspace, and not just move the cursor. Ah, but "just move the cursor" is what backspace has always meant. It's been that way for 100 years -- since the days of typewriters and teletypes. ;) > Thanks for those who answered, I guess I'll just do the backspace, > space, backspace dance. After thinking a while, I do remember one program I ran across recently that when you hit backspace would erase the entire line, then rewrite the entire line stopping one character short of where it was before. Even at network speeds it was noticable when the link was encrypted. -- Grant Edwards grant.b.edwards Yow! Inside, I'm already at SOBBING! gmail.com From dpalao.python at gmail.com Mon Dec 5 12:49:44 2011 From: dpalao.python at gmail.com (DPalao) Date: Mon, 5 Dec 2011 18:49:44 +0100 Subject: 70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when sending large object In-Reply-To: References: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> Message-ID: <31727_1323107396_pB5Hno58011636_201112051849.44264.dpalao.python@gmail.com> El Lunes Diciembre 5 2011, boB at mail.python.org escribi?: > On Mon, 5 Dec 2011 09:02:08 +0100, DPalao > > wrote: > >El Martes Noviembre 29 2011, DPalao escribi?: > >> Hello, > >> I'm trying to use multiprocessing to parallelize a code. There is a > >> number of tasks (usually 12) that can be run independently. Each task > >> produces a numpy array, and at the end, those arrays must be combined. > >> I implemented this using Queues (multiprocessing.Queue): one for input > >> and another for output. > >> But the code blocks. And it must be related to the size of the item I > >> put on the Queue: if I put a small array, the code works well; if the > >> array is realistically large (in my case if can vary from 160kB to > >> 1MB), the code blocks apparently forever. > >> I have tried this: > >> http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and- > >> lar ge- objects/ > >> but it didn't work (especifically I put a None sentinel at the end for > >> each worker). > >> > >> Before I change the implementation, > >> is there a way to bypass this problem with multiprocessing.Queue? > >> Should I post the code (or a sketchy version of it)? > >> > >> TIA, > >> > >> David > > > >Just for reference. The other day I found the explanation by "ryles" on > >his/her mail of 27th aug 2009, with title "Re: Q: multiprocessing.Queue > >size limitations or bug...". It is very clarifying. > >After having read that I arranged the program such that the main process > >did not need to know when the others have finished, so I changed the > >process join call with a queue get call, until a None (one per process) > >is returned. > > > >Best, > > > >David > > Why do people add character like [* SPAM *] to their subject > lines ?? Is it supposed to do something ?? I figured since > programmers hang out here, maybe one of you know this. > > Thanks, > boB Obviously it was not me who added the disgusting "70% [* SPAM *]" string to the subject. And I'd like to know the answer too. David From jpiitula at ling.helsinki.fi Mon Dec 5 13:04:04 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 05 Dec 2011 20:04:04 +0200 Subject: Scope of variable inside list comprehensions? References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: Roy Smith writes: > Consider the following django snippet. Song(id) raises DoesNotExist > if the id is unknown. > > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id > > Is id guaranteed to be in scope in the print statement? I found one > thread > (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) > which says yes, but hints that it might not always be in the future. > Now that we're in the future, is that still true? And for Python 3 > also? Another id is in scope in this Python3 example (3.1.2, Ubuntu): >>> try: ... songs = [1/0 for id in [1]] ... except Exception: ... print('Caught', id) ... Caught From ckaynor at zindagigames.com Mon Dec 5 13:04:36 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Mon, 5 Dec 2011 10:04:36 -0800 Subject: Scope of variable inside list comprehensions? In-Reply-To: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: On Mon, Dec 5, 2011 at 9:04 AM, Roy Smith wrote: > Consider the following django snippet. Song(id) raises DoesNotExist if > the id is unknown. > > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id > > Is id guaranteed to be in scope in the print statement? I found one > thread ( > http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) > which says yes, but hints that it might not always be in the future. Now > that we're in the future, is that still true? And for Python 3 also? > In Python2, id will always be in scope, unless the first iteration fails (aka, an empty iterable, or a custom iterable which raises an exception before yielding a result). I believe in Python3, the scoping of list comprehensions was changed to match that of generator expressions, which is that the loop variable(s) fall out of scope outside of the expression. > > The current docs, > http://docs.python.org/tutorial/datastructures.html#list-comprehensions, > are mute on this point. > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Mon Dec 5 13:10:08 2011 From: __peter__ at web.de (Peter Otten) Date: Mon, 05 Dec 2011 19:10:08 +0100 Subject: Scope of variable inside list comprehensions? References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: Roy Smith wrote: > Consider the following django snippet. Song(id) raises DoesNotExist if > the id is unknown. > > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id > > Is id guaranteed to be in scope in the print statement? I found one > thread > (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) > which says yes, but hints that it might not always be in the future. Now > that we're in the future, is that still true? And for Python 3 also? > > The current docs, > http://docs.python.org/tutorial/datastructures.html#list-comprehensions, > are mute on this point. If you are using a generator expression id will already be out of scope in Python 2. In Python 3 list comprehensions have been changed to work the same way: $ cat song.py class DoesNotExist(Exception): pass class Song: def __init__(self, id): if id == 2: raise DoesNotExist ids = [1, 2] try: songs = [Song(i) for i in ids] except DoesNotExist as e: print("song #%d does not exist" % i) $ python song.py song #2 does not exist $ python3 song.py Traceback (most recent call last): File "song.py", line 11, in songs = [Song(i) for i in ids] File "song.py", line 11, in songs = [Song(i) for i in ids] File "song.py", line 7, in __init__ raise DoesNotExist __main__.DoesNotExist During handling of the above exception, another exception occurred: Traceback (most recent call last): File "song.py", line 13, in print("song #%d does not exist" % i) NameError: name 'i' is not defined $ $ cat song_gen.py class DoesNotExist(Exception): pass class Song: def __init__(self, id): if id == 2: raise DoesNotExist ids = [1, 2] try: songs = list(Song(i) for i in ids) except DoesNotExist as e: print("song #%d does not exist" % i) $ python song_gen.py Traceback (most recent call last): File "song_gen.py", line 13, in print("song #%d does not exist" % i) NameError: name 'i' is not defined So you'd rather store the id in the exception. From patrickwayodi at gmail.com Mon Dec 5 13:16:30 2011 From: patrickwayodi at gmail.com (Patrick Wayodi) Date: Mon, 5 Dec 2011 21:16:30 +0300 Subject: Pythonbrew is not installing Python 2.7 successfully Message-ID: Hi, I'm trying to install Python 2.7 using Pythonbrew on a Debian GNU/Linux system, but I've failed. This is the output I got on my terminal: root at debian:~# pythonbrew install /home/guest/python27 Copying /home/guest/python27 into /root/.pythonbrew/build/Python-python27 This could take a while. You can run the following command on another shell to track the status: tail -f /root/.pythonbrew/log/build.log Installing Python-python27 into /root/.pythonbrew/pythons/Python-python27 ERROR: Failed to install Python-python27. See /root/.pythonbrew/log/build.log to see why. pythonbrew install --force python27 root at debian:~# The log file build.log was empty. Please help me. Patrick. From dpalao.python at gmail.com Mon Dec 5 13:28:22 2011 From: dpalao.python at gmail.com (DPalao) Date: Mon, 5 Dec 2011 19:28:22 +0100 Subject: 70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object In-Reply-To: References: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> Message-ID: <31719_1323109709_pB5ISMgK014037_201112051928.22417.dpalao.python@gmail.com> Hi Lie, Thank you for the reply. El Lunes Diciembre 5 2011, Lie Ryan escribi?: > On 11/30/2011 06:09 AM, DPalao wrote: > > Hello, > > I'm trying to use multiprocessing to parallelize a code. There is a > > number of tasks (usually 12) that can be run independently. Each task > > produces a numpy array, and at the end, those arrays must be combined. > > I implemented this using Queues (multiprocessing.Queue): one for input > > and another for output. > > But the code blocks. And it must be related to the size of the item I put > > on the Queue: if I put a small array, the code works well; if the array > > is realistically large (in my case if can vary from 160kB to 1MB), the > > code blocks apparently forever. > > I have tried this: > > http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-l > > arge- objects/ > > but it didn't work (especifically I put a None sentinel at the end for > > each worker). > > > > Before I change the implementation, > > is there a way to bypass this problem with multiprocessing.Queue? > > Should I post the code (or a sketchy version of it)? > > Transferring data over multiprocessing.Queue involves copying the whole > object across an inter-process pipe, so you need to have a reasonably > large workload in the processes to justify the cost of the copying to > benefit from running the workload in parallel. > > You may try to avoid the cost of copying by using shared memory > (http://docs.python.org/library/multiprocessing.html#sharing-state-between- > processes); you can use Queue for communicating when a new data comes in or > when a task is done, but put the large data in shared memory. Be careful > not to access the data from multiple processes concurrently. > Yep, that was my first thought, but the arrays's elements are complex64 (or complex in general), and I don't know how to easily convert from multiprocessing.Array to/from numpy.array when the type is complex. Doing that would require some extra conversions forth and back which make the solution not very attractive to me. I tried with a Manager too, but the array cannot be modified from within the worker processes. In principle, the array I need to share is expected to be, at most, ~2MB in size, and typically should be only <200kB. So, in principle, there is no huge extra workload. But that could change, and I'd like to be prepared for it, so any idea about using an Array or a Manager or another shared memory thing would be great. > In any case, have you tried a multithreaded solution? numpy is a C > extension, and I believe it releases the GIL when working, so it > wouldn't be in your way to achieve parallelism. That possibility I didn't know. What does exactly break the GIL? The sharing of a numpy array? What if I need to also share some other "standard" python data (eg, a dictionary)? Best regards, David From ursusmaximus at gmail.com Mon Dec 5 13:33:56 2011 From: ursusmaximus at gmail.com (Ron) Date: Mon, 5 Dec 2011 10:33:56 -0800 (PST) Subject: LinuxJournal Readers' Choice Awards 2011 Best {Programming, Scripting} Language References: Message-ID: <3ec3d1e0-e734-4fab-ad0a-d3b3f851cd12@y18g2000yqy.googlegroups.com> Hello Wesley, Thanks for the interesting news from Linux Journal. Now, enquring minds want to know, when will there be a Core Python 3? Ron :-) From jeanmichel at sequans.com Mon Dec 5 13:57:15 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 05 Dec 2011 19:57:15 +0100 Subject: Scope of variable inside list comprehensions? In-Reply-To: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: <4EDD140B.4060407@sequans.com> Roy Smith wrote: > Consider the following django snippet. Song(id) raises DoesNotExist if the id is unknown. > > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id > > Is id guaranteed to be in scope in the print statement? I found one thread (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) which says yes, but hints that it might not always be in the future. Now that we're in the future, is that still true? And for Python 3 also? > > The current docs, http://docs.python.org/tutorial/datastructures.html#list-comprehensions, are mute on this point. > For python 2, id will always be defined *as you meant it*. But you're doing something wrong : overiding the 'id' builtin function. With python 3 you will probably print the 'id' builtin function representation, which is correct but not want you want to achieve. The proper way to propagate information with exceptions is using the exception itself: try: songs = [Song(_id) for _id in song_ids] except Song.DoesNotExist, exc: print exc class DoesNotExist(Exception): def __init__(self, songId): self.songId = songId def __str__(self): return "Unkown Song Id %s" % self.songId class Song: def __init__(self, songId): if whatever: raise DoesNotExist(songId) self.id=songId JM From roy at panix.com Mon Dec 5 14:15:04 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 11:15:04 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> Hmmm, the use of id was just a simplification for the sake of posting. The real code is a bit more complicated and used a different variable name, but that's a good point. As far as storing the value in the exception, unfortunately, DoesNotExist is not my exception; it comes from deep within django. I'm just passing it along. From roy at panix.com Mon Dec 5 14:15:04 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 11:15:04 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> Hmmm, the use of id was just a simplification for the sake of posting. The real code is a bit more complicated and used a different variable name, but that's a good point. As far as storing the value in the exception, unfortunately, DoesNotExist is not my exception; it comes from deep within django. I'm just passing it along. From tjreedy at udel.edu Mon Dec 5 14:55:01 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 05 Dec 2011 14:55:01 -0500 Subject: Fwd: class print method... In-Reply-To: <4EDCC29D.7010903@davea.name> References: <4EDC775D.4000906@davea.name> <4EDCC29D.7010903@davea.name> Message-ID: On 12/5/2011 8:09 AM, Dave Angel wrote: > useful, such as if you print a list. The __str__() method of list calls > the __repr__() for each object inside it. So it can be useful to define > both, even if you decide to have them do the same thing. If you want __str__ and __repr__ to do the same thing, you only need *one* def statement and an assignment. def __str__(self): __repr__ = __str__ -- Terry Jan Reedy From storchaka at gmail.com Mon Dec 5 15:05:43 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Mon, 05 Dec 2011 22:05:43 +0200 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: 03.12.11 23:34, Arnaud Delobelle ???????(??): > Is there a simple way to achieve this? I could do something like this: > > def encode(u): > """encode a unicode string in .properties format""" > return u"".join(u"\\u%04x" % ord(c) if ord(c)> 0xFF else c for c > in u).encode("latin_1") You must also encode backslash ('\\'), whitespaces and control characters (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'. And don't forget characters with code >0xFFFF. From tjreedy at udel.edu Mon Dec 5 15:22:05 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 05 Dec 2011 15:22:05 -0500 Subject: Scope of variable inside list comprehensions? In-Reply-To: <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> Message-ID: On 12/5/2011 2:15 PM, Roy Smith wrote: > Hmmm, the use of id was just a simplification for the sake of > posting. The real code is a bit more complicated and used a > different variable name, but that's a good point. > > As far as storing the value in the exception, unfortunately, > DoesNotExist is not my exception; it comes from deep within django. > I'm just passing it along. It is hard to sensibly answer a question when the questioner *significantly* changes the problem in the process of 'simplifying' it. Both of those are significant changes ;-) Changing a name to a built-in name is a complexification, not a simplification, because it introduces new issues that were not in the original. Changing the exception from one you do not control to one you apparently do also changes the appropriate answer. If you do not control the exception and you want guaranteed access to the loop variable with Python 3 (and the upgrade of django to work with Python 3 is more or less done), then use an explicit loop. If you want the loop to continue after an error, instead of stopping, you can put the try/except within the loop, instead of without. This possibility is one advantage of using an explicit loop. songs = [] for song_id in song_ids: try: songs.append(Song(song_id)) except django.error: print("unknown song id {}".format(song_id)) -- Terry Jan Reedy From arnodel at gmail.com Mon Dec 5 15:25:39 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 5 Dec 2011 20:25:39 +0000 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: On 5 December 2011 20:05, Serhiy Storchaka wrote: > 03.12.11 23:34, Arnaud Delobelle ???????(??): > >> Is there a simple way to achieve this? I could do something like this: >> >> def encode(u): >> ? ? """encode a unicode string in .properties format""" >> ? ? return u"".join(u"\\u%04x" % ord(c) if ord(c)> ?0xFF else c for c >> in u).encode("latin_1") > > > You must also encode backslash ('\\'), whitespaces and control characters > (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'. Fortunately there aren't any of these in the keys. > And don't forget characters with code >0xFFFF. I haven't thought of these. I don't think that I have any, but I'll check. Do you know how they are encoded? -- Arnaud From snorble at hotmail.com Mon Dec 5 16:26:41 2011 From: snorble at hotmail.com (snorble) Date: Mon, 5 Dec 2011 13:26:41 -0800 (PST) Subject: Install Python on Windows without Start Menu icons? References: Message-ID: <63f25ebf-aa27-4a0b-b20f-84ccf9da7182@y12g2000vba.googlegroups.com> On Dec 5, 2:01?am, Wolfgang Strobl wrote: > "Pedro Henrique G. Souto" : > > >On 02/12/2011 16:34, snorble wrote: > >> Is it possible to automate the Python installation on Windows using > >> the MSI file so it does not add a Start Menu folder? I would like to > >> push out Python to all of my office workstations, but I'd like for it > >> to be relatively silent from the user's point of view. > > >If you just want to run python scripts in those machines (not developing > >in it), you can use something like py2exe [http://www.py2exe.org/]. > > That doesn't answer the question. > > Snorble might use ?"ORCA", a MSI editor from Microsoft. > > http://forums.frontmotion.com/viewtopic.php?f=10&t=837 > > discusses a similar question for Firefox. > > I haven't tried it myself, but would give it a try. > > -- > Thank you for observing all safety precautions Thank you! I got it working using Orca. Initially I had removed all of the rows from the Shortcut table and saved the .msi file, but the resulting .msi file was only about 600 KB (the original .msi file is about 15 MB). I had to create a .mst transform file, and that allows Python to install without any Start Menu entries. For reference, here's what I did. I opened the python-2.7.2.msi file in Orca, then click the Transform menu, then New Transform. Then go to the Shortcut table and highlight all of the entries and press the Delete key, and acknowledge that it will delete all of the rows. Then click the Transform menu and click Generate Transform and save it (python-2.7.2-no-icons.mst in my case). Once you have the .mst file, you can install it from the command line: msiexec /i python-2.7.2.msi TRANSFORMS=python-2.7.2-no-icons.mst Or to silently install: msiexec /qn /i python-2.7.2.msi TRANSFORMS=python-2.7.2-no-icons.mst From ralf at systemexit.de Mon Dec 5 16:44:57 2011 From: ralf at systemexit.de (Ralf Schmitt) Date: Mon, 05 Dec 2011 22:44:57 +0100 Subject: [ANNOUNCE] pypiserver 0.5.0 - minimal pypi server Message-ID: <87r50ivgkm.fsf@myhost.localnet> Hi, I've just uploaded pypiserver 0.5.0 to the python package index. pypiserver is a minimal PyPI compatible server. It can be used to serve a set of packages and eggs to easy_install or pip. pypiserver is easy to install (i.e. just easy_install pypiserver). It doesn't have any external dependencies. http://pypi.python.org/pypi/pypiserver/ should contain enough information to easily get you started running your own PyPI server in a few minutes. The code is available on github: https://github.com/schmir/pypiserver Changes in version 0.5.0 ------------------------- - make setup.py install without calling 2to3 by changing source code to be compatible with both python 2 and python 3. We now ship a slightly patched version of bottle. The upcoming bottle 0.11 also contains these changes. - make the single-file pypi-server-standalone.py work with python 3 -- Cheers, Ralf From storchaka at gmail.com Mon Dec 5 16:46:42 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Mon, 05 Dec 2011 23:46:42 +0200 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: 05.12.11 22:25, Arnaud Delobelle ???????(??): > On 5 December 2011 20:05, Serhiy Storchaka wrote: >> You must also encode backslash ('\\'), whitespaces and control characters >> (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'. > Fortunately there aren't any of these in the keys. "a=b" is same as "a= b". >> And don't forget characters with code>0xFFFF. > I haven't thought of these. I don't think that I have any, but I'll > check. Do you know how they are encoded? Java uses UTF-16. if i>=0x110000: raise some exception if i>=0x10000: return '\\u%04X\\u%04X' % (0xD800+((i-0x10000)>>10), 0xDC00+(i&0x3FF)) From rosuav at gmail.com Mon Dec 5 17:00:47 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 09:00:47 +1100 Subject: Backspace does not erase in stdout In-Reply-To: References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> <4edcfe6b$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Dec 6, 2011 at 4:40 AM, Grant Edwards wrote: > After thinking a while, I do remember one program I ran across > recently that when you hit backspace would erase the entire line, then > rewrite the entire line stopping one character short of where it was > before. ?Even at network speeds it was noticable when the link was > encrypted. I've seen a few programs that do that. Benefit is that it works even when (a) you're doing more than just "backspace one letter" (eg deleting an entire word), and (b) when your logical line wraps over multiple physical lines. It gets ugly fast, though. ChrisA From questions.anon at gmail.com Mon Dec 5 17:32:35 2011 From: questions.anon at gmail.com (questions anon) Date: Tue, 6 Dec 2011 09:32:35 +1100 Subject: loop through arrays and find maximum Message-ID: I would like to calculate the max and min across many netcdf files. I know how to create one big array and then concatenate and find the numpy.max but when I run this on 1000's of arrays I have a memory error. What I would prefer is to loop through the arrays and produce the maximum without having the make a big array. Does anyone have any ideas as to how I could achieve this? My idea goes something like: netCDF_list=[] maxarray=[] for dir in glob.glob(MainFolder + '*/01/')+ glob.glob(MainFolder + '*/02/')+ glob.glob(MainFolder + '*/12/'): for ncfile in glob.glob(dir + '*.nc'): netCDF_list.append(ncfile) for filename in netCDF_list: ncfile=netCDF4.Dataset( filename) TSFC=ncfile.variables['T_SFC'][:] fillvalue=ncfile.variables['T_SFC']._FillValue TSFC=MA.masked_values(TSFC, fillvalue) for i in TSFC: if i == N.max(TSFC, axis=0): maxarray.append(i) else: pass print maxarray -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Mon Dec 5 17:35:06 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 22:35:06 GMT Subject: Scope of variable inside list comprehensions? References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> On Mon, 05 Dec 2011 19:57:15 +0100, Jean-Michel Pichavant wrote: > The proper way to propagate information with exceptions is using the > exception itself: > > try: > songs = [Song(_id) for _id in song_ids] > except Song.DoesNotExist, exc: > print exc I'm not entirely sure that this is the proper way to propagate the exception. I see far to many people catching exceptions to print them, or worse, to print a generic, useless message like "an error occurred". The problem here is that having caught the exception, songs now does not exist, and will surely cause another, unexpected, exception in a moment or two when the code attempts to use it. Since the error is (apparently) unrecoverable, the right way as far as I can see is: songs = [Song(_id) for _id in song_ids] allowing any exception to be fatal and the traceback to be printed as normal. If the error is recoverable, you will likely need to do more to recover from it than merely print the exception and continue. -- Steven From roy at panix.com Mon Dec 5 17:36:48 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 14:36:48 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> Message-ID: <27498659.319.1323124608749.JavaMail.geo-discussion-forums@yqcv9> Well, in my defense, I did ask a pretty narrow question, "Is id guaranteed to be in scope in the print statement?". While I will admit that not knowing whether I could alter the exception, or whether id masked a builtin or not does complexify answering some questions, those are questions I didn't ask :-) From roy at panix.com Mon Dec 5 17:36:48 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 14:36:48 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> Message-ID: <27498659.319.1323124608749.JavaMail.geo-discussion-forums@yqcv9> Well, in my defense, I did ask a pretty narrow question, "Is id guaranteed to be in scope in the print statement?". While I will admit that not knowing whether I could alter the exception, or whether id masked a builtin or not does complexify answering some questions, those are questions I didn't ask :-) From arnodel at gmail.com Mon Dec 5 17:45:56 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 5 Dec 2011 22:45:56 +0000 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: On 5 December 2011 21:46, Serhiy Storchaka wrote: > 05.12.11 22:25, Arnaud Delobelle ???????(??): >> On 5 December 2011 20:05, Serhiy Storchaka ?wrote: >>> You must also encode backslash ('\\'), whitespaces and control characters >>> (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'. >> Fortunately there aren't any of these in the keys. > > "a=b" is same as "a= b". You're right - all my values are trimmed for whitespace on both sides, so it's OK for me. Although if I wanted to make a general purpose tool I would have to take this into account. I might just escape all whitespace after all, for safety's sake. >>> And don't forget characters with code>0xFFFF. >> I haven't thought of these. ?I don't think that I have any, but I'll >> check. ?Do you know how they are encoded? > > Java uses UTF-16. > > if i>=0x110000: > ? ?raise some exception > if i>=0x10000: > ? ?return '\\u%04X\\u%04X' % (0xD800+((i-0x10000)>>10), 0xDC00+(i&0x3FF)) Thanks. I'm using .encode("latin1", "backslashreplace") now as suggested by Peter Otten. I can't see from the docs if this uses UTF-16 escape sequences. -- Arnaud From roy at panix.com Mon Dec 5 17:57:43 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 14:57:43 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <14157454.1816.1323125863703.JavaMail.geo-discussion-forums@yqbz41> Sigh. I attempted to reduce this to a minimal example to focus the discussion on the question of list comprehension variable scope. Instead I seem to have gotten people off on other tangents. I suppose I should post more of the real code... song_ids = request.POST.getlist('song_id') try: songs = [Song.get(int(id)) for id in song_ids] except Song.DoesNotExist: return HttpResponseBadRequest("unknown song id (%d)" % id) I may be in the minority here, but it doesn't bother me much that my use of 'id' shadows a built-in. Especially in small scopes like this, I use whatever variable names make the the code easiest to read and don't worry about shadowing builtins. I don't have an exhaustive list of builtins in my head, so even if I worried about common ones like file or id, I'm sure I'd miss some others. So I don't sweat it. If shadowing builtins was really evil, they'd be reserved keywords and then you wouldn't be able to do it. From drsalists at gmail.com Mon Dec 5 18:05:26 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Mon, 5 Dec 2011 15:05:26 -0800 Subject: order independent hash? In-Reply-To: <952995.674.1323086974877.JavaMail.geo-discussion-forums@preu18> References: <952995.674.1323086974877.JavaMail.geo-discussion-forums@preu18> Message-ID: On 12/5/11, 88888 Dihedral wrote: > On Monday, December 5, 2011 1:50:08 PM UTC+8, Dan Stromberg wrote: >> Two methods: >> 1) If you need your hash only once in an infrequent while, then save >> the elements in a list, appending as needed, and sort prior to >> hashing, as needed >> >> 2) If you need your hash more often, you could keep your elements in a >> treap or red-black tree; these will maintain sortedness throughout the >> life of the datastructure. >> >> 3) If A bunch of log(n) or n or nlog(n) operations doesn't sound >> appealing, then you might try this one: Create some sort of mapping >> from your elements to the integers. Then just use a sum. This won't >> scatter things nearly as well as a cryptographic hash, but it's very >> fast, because you don't need to reevaluate some of your members as you >> go. >> >> HTH >> > A sorted list can behave like a hash table. This is of O(log(n)) in > accesses > of n items in theory. > > I agree with you a hash key computation method too slow than a list of n > items in accesses for a range of n items should be reloadable. > > But this is not supported in Python yet. > > For tedious trivial jobs of non-heavy computing , I'll opt for easy use. A sorted list is O(log(n)) for lookups, but O(n) for insertions. If you have a process doing both, the table operations are O(n). A hash table that isn't overfilled is O(1) for lookups, O(1) for insertions. But this is not ordered. Here's a straightforward treap implementation for python, with pure python and cython versions: http://pypi.python.org/pypi/treap/0.995 There's also at least one red-black tree implementation available. From tjreedy at udel.edu Mon Dec 5 21:23:17 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 05 Dec 2011 21:23:17 -0500 Subject: Scope of variable inside list comprehensions? In-Reply-To: <27498659.319.1323124608749.JavaMail.geo-discussion-forums@yqcv9> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> <27498659.319.1323124608749.JavaMail.geo-discussion-forums@yqcv9> Message-ID: On 12/5/2011 5:36 PM, Roy Smith wrote: > Well, in my defense, I did ask a pretty narrow question, "Is id > guaranteed to be in scope in the print statement?". Yes for 2.x, guaranteed no for 3.x. If you had simply asked "Is the loop variable of a list comprehension guaranteed to be in scope after the list comprehension?", without a distracting example, that is the answer you would have received. I intend(ed) to inform, not attack, hence no 'defense' needed. > While I will > admit that not knowing whether I could alter the exception, or > whether id masked a builtin or not does complexify answering some > questions, those are questions I didn't ask :-) Except that it bears on the question you did ask because it means that the code will run in 3.x but with different results, whereas a random name will fail in 3.x with a NameError. -- Terry Jan Reedy From rosuav at gmail.com Mon Dec 5 21:35:45 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 13:35:45 +1100 Subject: Scope of variable inside list comprehensions? In-Reply-To: <14157454.1816.1323125863703.JavaMail.geo-discussion-forums@yqbz41> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> <14157454.1816.1323125863703.JavaMail.geo-discussion-forums@yqbz41> Message-ID: On Tue, Dec 6, 2011 at 9:57 AM, Roy Smith wrote: > I may be in the minority here, but it doesn't bother me much that my use of 'id' shadows a built-in. ?Especially in small scopes like this, I use whatever variable names make the the code easiest to read and don't worry about shadowing builtins. ?I don't have an exhaustive list of builtins in my head, so even if I worried about common ones like file or id, I'm sure I'd miss some others. ?So I don't sweat it. > > If shadowing builtins was really evil, they'd be reserved keywords and then you wouldn't be able to do it. Agreed. The name 'id' is one that's shadowed benignly in a lot of code. In the context of the original question, it's not an issue - there'll be no confusion. Python's scoping rules are "do what the programmer probably wants". This works most of the time, but occasionally you get edge cases where things are a bit weird, and C-style explicit scoping (with infinitely nested block scope) begins to look better. But for probably 99% of situations, Python's system "just works". If you need more flexibility in the exception you throw, it may be worth putting together a filter: def HttpSongId(id): try: return Song.get(int(id)) except Song.DoesNotExist: return HttpResponseBadRequest("unknown song id (%d)" % id) song_ids = request.POST.getlist('song_id') songs = [HttpSongId(id) for id in song_ids] This encapsulates things in a somewhat weird way, but if there's any other work to be done at the same time, you could probably come up with a better name for the exception filter function. ChrisA From xahlee at gmail.com Mon Dec 5 23:36:45 2011 From: xahlee at gmail.com (Xah Lee) Date: Mon, 5 Dec 2011 20:36:45 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> Message-ID: <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> On Dec 5, 4:31?am, Tim Bradshaw wrote: > On 2011-12-05 11:51:11 +0000, Xah Lee said: > > > python has more readible syntax, more modern computer language > > concepts, and more robust libraries. These qualities in turn made it > > popular. > > Yet you still post here: why? i don't like python, and i prefer emacs lisp. The primary reason is that python is not functional, especially with python 3. The python community is full of fanatics with their drivels. In that respect, it's not unlike Common Lisp community and Scheme lisp community. see also: ?Python Documentation Problems? http://xahlee.org/perl-python/python_doc_index.html ?Computer Language Design: What's List Comprehension and Why is It Harmful?? http://xahlee.org/comp/list_comprehension.html ?Lambda in Python 3000? http://xahlee.org/perl-python/python_3000.html ?What Languages to Hate? http://xahlee.org/UnixResource_dir/writ/language_to_hate.html ?Xah on Programing Languages? http://xahlee.org/Periodic_dosage_dir/comp_lang.html Xah From rosuav at gmail.com Tue Dec 6 00:10:27 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 16:10:27 +1100 Subject: Questions about LISP and Python. In-Reply-To: <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: On Tue, Dec 6, 2011 at 3:36 PM, Xah Lee wrote: > i don't like python, and i prefer emacs lisp. The primary reason is > that python is not functional, especially with python 3. The python > community is full of fanatics with their drivels. In that respect, > it's not unlike Common Lisp community and Scheme lisp community. So you hate Python. Fine. Why post here? Why not just abandon Python as a dead loss and go code in Lithp? Clearly something is keeping you here. Is it that there's something about Python that you really like, or are you just trolling? ChrisA From s.pasoev at gmail.com Tue Dec 6 00:49:55 2011 From: s.pasoev at gmail.com (Sergi Pasoev) Date: Tue, 6 Dec 2011 10:19:55 +0430 Subject: Single key press Message-ID: Hi. I wonder if it is realistic to get a single key press in Python without ncurses or any similar library. In single key press I mean something like j and k in Gnu less program, you press the key and and it is captured by the script without need to press enter afterwards From ss27051980 at gmail.com Tue Dec 6 00:53:16 2011 From: ss27051980 at gmail.com (Suresh Sharma) Date: Tue, 6 Dec 2011 11:23:16 +0530 Subject: Fwd: class print method... In-Reply-To: References: <4EDC775D.4000906@davea.name> Message-ID: Dave / Ryan Thanks i have got it and it worked after using repr statement. Thanks everyone for their valuable feedback. On Mon, Dec 5, 2011 at 6:11 PM, Lie Ryan wrote: > On 12/05/2011 10:18 PM, Suresh Sharma wrote: > >> >> Pls help its really frustrating >> ---------- Forwarded message ---------- >> From: Suresh Sharma >> Date: Monday, December 5, 2011 >> Subject: class print method... >> To: "d at davea.name " > > >> >> >> Dave, >> Thanx for the quick response, i am sorry that i did not explain >> correctly look at the code below inspite of this i am just getting class >> object at memory location.I am sort i typed all this code on my android >> in a hurry so.indentation could.not.be.managed but this.similar code >> when i run all my objects created by class deck are not shown but stored >> in varioia meory locations. How can i display them. >> >> > I think you're in the right track, however I suspect you're running the > code in the shell instead of as a script. The shell uses __repr__() to > print objects instead of __str__(), so you either need to use 'print' or > you need to call str(), note the following: > > Python 2.7.2+ (default, Oct 4 2011, 20:06:09) > [GCC 4.6.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> suits = ['spades', 'clubs', 'diamonds', 'hearts'] > >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', > 'K'] > >>> class Card: > ... def __init__(self, rank, suit): > ... self.suit = suit > ... self.rank = rank > ... def __str__(self): > ... return suits[self.suit] + ' ' + ranks[self.rank] > ... > >>> Card(2, 3) #1 > <__main__.Card instance at 0x7f719c3a20e0> > >>> str(Card(2, 3)) #2 of your > 'hearts 3' > >>> print Card(2, 3) #3 > hearts 3 > > In #1, the output is the __repr__() of your Card class; you can modify > this output by overriding the __repr__() on your Card class. > > In #2, the output is the __repr__() of a string, the string is the return > value from __str__() of your Card class. The repr of a string is the string > enclosed in quotes, which is why there is an extra pair of quotes. > > In #3, you're 'print'-ing a string, the string is the return value from > __str__() of your Card class. There's no extra quotes, since 'print' prints > the string itself, not the repr of the string. > > -- > http://mail.python.org/**mailman/listinfo/python-list > -- Suresh Sharma Regional Project Manager, O2F,Mumbai Maharashtra-400101. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dihedral88888 at googlemail.com Tue Dec 6 01:27:48 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 5 Dec 2011 22:27:48 -0800 (PST) Subject: Single key press In-Reply-To: References: Message-ID: <29114360.50.1323152868752.JavaMail.geo-discussion-forums@prcu10> On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: > Hi. > > I wonder if it is realistic to get a single key press in Python > without ncurses or > any similar library. In single key press I mean something like j and k > in Gnu less > program, you press the key and and it is captured by the script without need to > press enter afterwards Sounds like the fast key searching for a list in the DOS application in the old days. This is easy in GUI of just tens of items, but for thousands of items such as in a directory listing, some API is really slow. From dihedral88888 at googlemail.com Tue Dec 6 01:27:48 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 5 Dec 2011 22:27:48 -0800 (PST) Subject: Single key press In-Reply-To: References: Message-ID: <29114360.50.1323152868752.JavaMail.geo-discussion-forums@prcu10> On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: > Hi. > > I wonder if it is realistic to get a single key press in Python > without ncurses or > any similar library. In single key press I mean something like j and k > in Gnu less > program, you press the key and and it is captured by the script without need to > press enter afterwards Sounds like the fast key searching for a list in the DOS application in the old days. This is easy in GUI of just tens of items, but for thousands of items such as in a directory listing, some API is really slow. From r.grimm at science-computing.de Tue Dec 6 01:42:35 2011 From: r.grimm at science-computing.de (Rainer Grimm) Date: Mon, 5 Dec 2011 22:42:35 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: <11334186.28.1323153755482.JavaMail.geo-discussion-forums@vbxw7> Hello, > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world. It's only syntactic sugar for the functions map and filter. So functions have to be pure functions. To say it in other words, they have to be side-effect free. But the python construct from above pollutes the namespace with name id. Greetings from Rottenburg, Rainer From wuwei23 at gmail.com Tue Dec 6 02:02:42 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 5 Dec 2011 23:02:42 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: On Dec 6, 2:36?pm, Xah Lee wrote: > The python community is full of fanatics with their drivels. You do know that you could just fuck right off and leave us to it, yes? In general, it's the person who is shrilly imposing their minority opinion on a disinterested audience that deserves the title 'fanatic'. From anacrolix at gmail.com Tue Dec 6 02:54:09 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Tue, 6 Dec 2011 18:54:09 +1100 Subject: Questions about LISP and Python. In-Reply-To: References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: This guy is an even better troll than that 88888 guy. His spelling is equally bad. His essays make some good points, but I don't see why he doesn't shut his trap and move on. ?_? On Tue, Dec 6, 2011 at 6:02 PM, alex23 wrote: > On Dec 6, 2:36?pm, Xah Lee wrote: >> The python community is full of fanatics with their drivels. > > You do know that you could just fuck right off and leave us to it, > yes? > > In general, it's the person who is shrilly imposing their minority > opinion on a disinterested audience that deserves the title 'fanatic'. > -- > http://mail.python.org/mailman/listinfo/python-list From s.pasoev at gmail.com Tue Dec 6 03:29:54 2011 From: s.pasoev at gmail.com (Sergi Pasoev) Date: Tue, 6 Dec 2011 12:59:54 +0430 Subject: Python-list Digest, Vol 99, Issue 27 In-Reply-To: References: Message-ID: I do not want GUI, I just want a console application which will read a single character input. When you read from stdin for example, you have it to be terminated. 2011/12/6 : > Send Python-list mailing list submissions to > ? ? ? ?python-list at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > ? ? ? ?http://mail.python.org/mailman/listinfo/python-list > or, via email, send a message with subject or body 'help' to > ? ? ? ?python-list-request at python.org > > You can reach the person managing the list at > ? ? ? ?python-list-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Python-list digest..." > > Today's Topics: > > ? 1. Re: Questions about LISP and Python. (Xah Lee) > ? 2. Re: Questions about LISP and Python. (Chris Angelico) > ? 3. Re: 70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when > ? ? ?sending ? large object (Dennis Lee Bieber) > ? 4. Single key press (Sergi Pasoev) > ? 5. Re: Fwd: class print method... (Suresh Sharma) > ? 6. Re: Single key press (88888 Dihedral) > ? 7. Re: Single key press (88888 Dihedral) > ? 8. Re: Scope of variable inside list comprehensions? (Rainer Grimm) > ? 9. Re: Questions about LISP and Python. (alex23) > ?10. Re: Questions about LISP and Python. (Matt Joiner) > > > ---------- Message transf?r? ---------- > From:?Xah Lee > To:?python-list at python.org > Date:?Mon, 5 Dec 2011 20:36:45 -0800 (PST) > Subject:?Re: Questions about LISP and Python. > On Dec 5, 4:31?am, Tim Bradshaw wrote: >> On 2011-12-05 11:51:11 +0000, Xah Lee said: >> >> > python has more readible syntax, more modern computer language >> > concepts, and more robust libraries. These qualities in turn made it >> > popular. >> >> Yet you still post here: why? > > i don't like python, and i prefer emacs lisp. The primary reason is > that python is not functional, especially with python 3. The python > community is full of fanatics with their drivels. In that respect, > it's not unlike Common Lisp community and Scheme lisp community. > > see also: > > ?Python Documentation Problems? > http://xahlee.org/perl-python/python_doc_index.html > > ?Computer Language Design: What's List Comprehension and Why is It > Harmful?? > http://xahlee.org/comp/list_comprehension.html > > ?Lambda in Python 3000? > http://xahlee.org/perl-python/python_3000.html > > ?What Languages to Hate? > http://xahlee.org/UnixResource_dir/writ/language_to_hate.html > > ?Xah on Programing Languages? > http://xahlee.org/Periodic_dosage_dir/comp_lang.html > > ?Xah > > > > ---------- Message transf?r? ---------- > From:?Chris Angelico > To:?python-list at python.org > Date:?Tue, 6 Dec 2011 16:10:27 +1100 > Subject:?Re: Questions about LISP and Python. > On Tue, Dec 6, 2011 at 3:36 PM, Xah Lee wrote: >> i don't like python, and i prefer emacs lisp. The primary reason is >> that python is not functional, especially with python 3. The python >> community is full of fanatics with their drivels. In that respect, >> it's not unlike Common Lisp community and Scheme lisp community. > > So you hate Python. Fine. Why post here? Why not just abandon Python > as a dead loss and go code in Lithp? > > Clearly something is keeping you here. Is it that there's something > about Python that you really like, or are you just trolling? > > ChrisA > > > > ---------- Message transf?r? ---------- > From:?Dennis Lee Bieber > To:?python-list at python.org > Date:?Mon, 05 Dec 2011 21:15:09 -0800 > Subject:?Re: 70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when sending large object > On Mon, 5 Dec 2011 18:49:44 +0100, DPalao > wrote: > >>Obviously it was not me who added the disgusting "70% [* SPAM *]" string to >>the subject. And I'd like to know the answer too. >> > ? ? ? ?Based upon your headers, somewhere your message went through a spam > check program... > > X-Greylist: Sender succeeded SMTP AUTH, not delayed by > milter-greylist-4.3.7 > ? ? ? ?(monster.roma2.infn.it [141.108.255.100]); > ? ? ? ?Mon, 05 Dec 2011 09:02:22 +0100 (CET) > X-PMX-Version: 5.6.1.2065439, Antispam-Engine: 2.7.2.376379, > ? ? ? ?Antispam-Data: 2011.12.5.74814 > X-PMX-Spam: 70% > X-PMX-Spam-report: The following antispam rules were triggered by this > message: > ? ? ? ?Rule ? ? ? ? ? ? ? ? ? ? ?Score Description > ? ? ? ?RDNS_SUSP_FORGED_FROM ? ? 3.500 From domain appears to be forged, > ? ? ? ?and arrived via a host with a known suspicious rDNS. > ? ? ? ?SXL_IP_DYNAMIC 3.000 Received via a known dynamic IP (SXL lookup): > ? ? ? ?34.140.47.93.fur > ? ? ? ?FORGED_FROM_GMAIL ? ? ? ? 0.100 Appears to forge gmail in the from > ? ? ? ?FROM_NAME_ONE_WORD ? ? ? ?0.050 Name in From header is a single word > ? ? ? ?BODYTEXTP_SIZE_3000_LESS 0.000 Body size of the text/plain part is > less > ? ? ? ?than 3k > ? ? ? ?BODY_SIZE_1500_1599 0.000 Message body size is 1500 to 1599 bytes > ? ? ? ?BODY_SIZE_2000_LESS 0.000 Message body size is less than 2000 bytes. > ? ? ? ?BODY_SIZE_5000_LESS 0.000 Message body size is less than 5000 bytes. > ? ? ? ?BODY_SIZE_7000_LESS 0.000 Message body size is less than 5000 bytes. > ? ? ? ?RDNS_GENERIC_POOLED 0.000 Sender's PTR record matches generic pooled > -- > ? ? ? ?Wulfraed ? ? ? ? ? ? ? ? Dennis Lee Bieber ? ? ? ? AF6VN > ? ? ? ?wlfraed at ix.netcom.com ? ?HTTP://wlfraed.home.netcom.com/ > > > > > ---------- Message transf?r? ---------- > From:?Sergi Pasoev > To:?python-list at python.org > Date:?Tue, 6 Dec 2011 10:19:55 +0430 > Subject:?Single key press > Hi. > > I wonder if it is realistic to get a single key press in Python > without ncurses or > any similar library. In single key press I mean something like j and k > in Gnu less > program, you press the key and and it is captured by the script without need to > press enter afterwards > > > > ---------- Message transf?r? ---------- > From:?Suresh Sharma > To:?Lie Ryan > Date:?Tue, 6 Dec 2011 11:23:16 +0530 > Subject:?Re: Fwd: class print method... > Dave / Ryan > Thanks i have got it and it worked after using repr statement. Thanks everyone for their valuable feedback. > > > > On Mon, Dec 5, 2011 at 6:11 PM, Lie Ryan wrote: >> >> On 12/05/2011 10:18 PM, Suresh Sharma wrote: >>> >>> >>> Pls help its really frustrating >>> ---------- Forwarded message ---------- >>> From: Suresh Sharma >>> Date: Monday, December 5, 2011 >>> Subject: class print method... >>> To: "d at davea.name " >> > >>> >>> >>> Dave, >>> Thanx for the quick response, i am sorry that i did not explain >>> correctly look at the code below inspite of this i am just getting class >>> object at memory location.I am sort i typed all this code on my android >>> in a hurry so.indentation could.not.be.managed but this.similar code >>> when i run all my objects created by class deck are not shown but stored >>> in varioia meory locations. How can i display them. >>> >> >> I think you're in the right track, however I suspect you're running the code in the shell instead of as a script. The shell uses __repr__() to print objects instead of __str__(), so you either need to use 'print' or you need to call str(), note the following: >> >> Python 2.7.2+ (default, Oct ?4 2011, 20:06:09) >> [GCC 4.6.1] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. >> >>> suits = ['spades', 'clubs', 'diamonds', 'hearts'] >> >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] >> >>> class Card: >> ... ? ? def __init__(self, rank, suit): >> ... ? ? ? ? self.suit = suit >> ... ? ? ? ? self.rank = rank >> ... ? ? def __str__(self): >> ... ? ? ? ? return suits[self.suit] + ' ' + ranks[self.rank] >> ... >> >>> Card(2, 3) #1 >> <__main__.Card instance at 0x7f719c3a20e0> >> >>> str(Card(2, 3)) #2 of your >> 'hearts 3' >> >>> print Card(2, 3) #3 >> hearts 3 >> >> In #1, the output is the __repr__() of your Card class; you can modify this output by overriding the __repr__() on your Card class. >> >> In #2, the output is the __repr__() of a string, the string is the return value from __str__() of your Card class. The repr of a string is the string enclosed in quotes, which is why there is an extra pair of quotes. >> >> In #3, you're 'print'-ing a string, the string is the return value from __str__() of your Card class. There's no extra quotes, since 'print' prints the string itself, not the repr of the string. >> >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > > > -- > Suresh Sharma > Regional Project Manager, > O2F,Mumbai > Maharashtra-400101. > > > > > > ---------- Message transf?r? ---------- > From:?88888 Dihedral > To:?comp.lang.python at googlegroups.com > Date:?Mon, 5 Dec 2011 22:27:48 -0800 (PST) > Subject:?Re: Single key press > On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: >> Hi. >> >> I wonder if it is realistic to get a single key press in Python >> without ncurses or >> any similar library. In single key press I mean something like j and k >> in Gnu less >> program, you press the key and and it is captured by the script without need to >> press enter afterwards > > Sounds like the ?fast key searching for a list in the DOS application in the > old days. > > This is easy in GUI of just tens of items, but for thousands of items such > as in a directory listing, some API is really slow. > > > > > > ---------- Message transf?r? ---------- > From:?88888 Dihedral > To:?python-list at python.org > Date:?Mon, 5 Dec 2011 22:27:48 -0800 (PST) > Subject:?Re: Single key press > On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: >> Hi. >> >> I wonder if it is realistic to get a single key press in Python >> without ncurses or >> any similar library. In single key press I mean something like j and k >> in Gnu less >> program, you press the key and and it is captured by the script without need to >> press enter afterwards > > Sounds like the ?fast key searching for a list in the DOS application in the > old days. > > This is easy in GUI of just tens of items, but for thousands of items such > as in a directory listing, some API is really slow. > > > > > > ---------- Message transf?r? ---------- > From:?Rainer Grimm > To:?python-list at python.org > Date:?Mon, 5 Dec 2011 22:42:35 -0800 (PST) > Subject:?Re: Scope of variable inside list comprehensions? > Hello, > >> ? ? try: >> ? ? ? ? songs = [Song(id) for id in song_ids] >> ? ? except Song.DoesNotExist: >> ? ? ? ? print "unknown song id (%d)" % id > that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world. It's only syntactic sugar for the functions map and filter. So functions have to be pure functions. To say it in other words, they have to be side-effect free. But the python construct from above pollutes the namespace with name id. > > Greetings from Rottenburg, > Rainer > > > > ---------- Message transf?r? ---------- > From:?alex23 > To:?python-list at python.org > Date:?Mon, 5 Dec 2011 23:02:42 -0800 (PST) > Subject:?Re: Questions about LISP and Python. > On Dec 6, 2:36?pm, Xah Lee wrote: >> The python community is full of fanatics with their drivels. > > You do know that you could just fuck right off and leave us to it, > yes? > > In general, it's the person who is shrilly imposing their minority > opinion on a disinterested audience that deserves the title 'fanatic'. > > > > ---------- Message transf?r? ---------- > From:?Matt Joiner > To:?alex23 > Date:?Tue, 6 Dec 2011 18:54:09 +1100 > Subject:?Re: Questions about LISP and Python. > This guy is an even better troll than that 88888 guy. His spelling is > equally bad. His essays make some good points, but I don't see why he > doesn't shut his trap and move on. > > ?_? > > > > On Tue, Dec 6, 2011 at 6:02 PM, alex23 wrote: >> On Dec 6, 2:36?pm, Xah Lee wrote: >>> The python community is full of fanatics with their drivels. >> >> You do know that you could just fuck right off and leave us to it, >> yes? >> >> In general, it's the person who is shrilly imposing their minority >> opinion on a disinterested audience that deserves the title 'fanatic'. >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > -- > http://mail.python.org/mailman/listinfo/python-list From wuwei23 at gmail.com Tue Dec 6 03:39:53 2011 From: wuwei23 at gmail.com (alex23) Date: Tue, 6 Dec 2011 00:39:53 -0800 (PST) Subject: Single key press References: Message-ID: <90d8ede3-f42e-4342-91cf-cb671cd1b63b@c13g2000vbh.googlegroups.com> On Dec 6, 3:49?pm, Sergi Pasoev wrote: > I wonder if it is realistic to get a single key press in Python > without ncurses or any similar library. It's possible using Tkinter in the standard library: http://www.daniweb.com/software-development/python/code/216830 From steve+comp.lang.python at pearwood.info Tue Dec 6 03:48:39 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Dec 2011 08:48:39 GMT Subject: Single key press References: Message-ID: <4eddd6e7$0$11114$c3e8da3@news.astraweb.com> On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: > Hi. > > I wonder if it is realistic to get a single key press in Python without > ncurses or > any similar library. In single key press I mean something like j and k > in Gnu less > program, you press the key and and it is captured by the script without > need to press enter afterwards Coincidentally, I was working on that exact function last night, so you inspired me to put it up on the Python cookbook. This version should work on any POSIX system such as Linux, as well as Windows. I don't know if it will work on Macintosh. http://code.activestate.com/recipes/577977-get-single-keypress/ -- Steven From nobody at nowhere.com Tue Dec 6 03:55:16 2011 From: nobody at nowhere.com (Nobody) Date: Tue, 06 Dec 2011 08:55:16 +0000 Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> <4EDB88FD.9000108@davea.name> Message-ID: On Mon, 05 Dec 2011 00:20:32 -0800, Mark Dickinson wrote: >> May be, yes, but since calcsize() is returning 12 when the elements >> are put in the other order, it would seem to be not counting such >> padding. > > Indeed. That's arguably a bug in the struct module, There's no "arguably" about it. The documentation says: Native size and alignment are determined using the C compiler?s sizeof expression. But given: struct { unsigned long long a; char b[4]; } foo; struct { char b[4]; unsigned long long a; } bar; sizeof(foo) will always equal sizeof(bar). If long long is 8 bytes and has 8-byte alignment, both will be 16. If you want consistency with the in-memory representation used by C/C++ programs (and the on-disk representation used by C/C++ programs which write the in-memory representation directly to file), use ctypes; e.g.: >>> from ctypes import * >>> class foo(Structure): _fields_ = [ ("a", c_ulonglong), ("b", c_char * 4)] >>> sizeof(foo) 16 From alex.mizrahi at gmail.com Tue Dec 6 04:12:31 2011 From: alex.mizrahi at gmail.com (Alex Mizrahi) Date: Tue, 06 Dec 2011 11:12:31 +0200 Subject: Questions about LISP and Python. In-Reply-To: <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: <4edddc81$0$283$14726298@news.sunsite.dk> > i don't like python, and i prefer emacs lisp. The primary reason is > that python is not functional, especially with python 3. The python > community is full of fanatics with their drivels. In that respect, > it's not unlike Common Lisp community and Scheme lisp community. What functional programming features do you use, Xah? Last time I've checked, you didn't care about closures. In this respect, Python is better at functional programming than Emacs Lisp. Do you just like writing arbitrarily long expression? Or you think that eval is functional? From nobody at nowhere.com Tue Dec 6 04:27:29 2011 From: nobody at nowhere.com (Nobody) Date: Tue, 06 Dec 2011 09:27:29 +0000 Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: >> Emitting "\b \b" is one very common way to do a destructive backspace. >> Inelegant? Perhaps, but a common inelegance. > > That's pretty much the only way I've seen it done for the past 25 > years. ... before which, it was BS-DEL-BS. DEL being 127 means that it punches through all of the holes on the paper tape, causing whatever was there previously to be replaced with a DEL character. BS-SPC-BS would only punch through one of the holes. From hansmu at xs4all.nl Tue Dec 6 04:28:32 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 06 Dec 2011 10:28:32 +0100 Subject: Single key press In-Reply-To: <4eddd6e7$0$11114$c3e8da3@news.astraweb.com> References: <4eddd6e7$0$11114$c3e8da3@news.astraweb.com> Message-ID: <4edde040$0$6973$e4fe514c@news2.news.xs4all.nl> On 6/12/11 09:48:39, Steven D'Aprano wrote: > On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: > >> Hi. >> >> I wonder if it is realistic to get a single key press in Python without >> ncurses or >> any similar library. In single key press I mean something like j and k >> in Gnu less >> program, you press the key and and it is captured by the script without >> need to press enter afterwards > > > Coincidentally, I was working on that exact function last night, so you > inspired me to put it up on the Python cookbook. This version should work > on any POSIX system such as Linux, as well as Windows. I don't know if it > will work on Macintosh. > > http://code.activestate.com/recipes/577977-get-single-keypress/ It works fine on MacOS/X, but probably not on MacOS Classic. -- HansM From nobody at nowhere.com Tue Dec 6 04:48:55 2011 From: nobody at nowhere.com (Nobody) Date: Tue, 06 Dec 2011 09:48:55 +0000 Subject: Single key press References: Message-ID: On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: > I wonder if it is realistic to get a single key press in Python > without ncurses or any similar library. On Unix, you need to use termios.tcsetattr() to disable "canonical mode". Otherwise, the tty driver will only pass data up to the application when the user enters a newline or EOF. If you do this, you also need to handle SIGTSTP and SIGCONT, restoring the original terminal settings on SIGTSTP (typically generated by Ctrl-Z) and restoring the application's setting on SIGCONT. From dihedral88888 at googlemail.com Tue Dec 6 05:16:42 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 6 Dec 2011 02:16:42 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: <11334186.28.1323153755482.JavaMail.geo-discussion-forums@vbxw7> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <11334186.28.1323153755482.JavaMail.geo-discussion-forums@vbxw7> Message-ID: <72527.110.1323166602941.JavaMail.geo-discussion-forums@prgk20> On Tuesday, December 6, 2011 2:42:35 PM UTC+8, Rainer Grimm wrote: > Hello, > > > try: > > songs = [Song(id) for id in song_ids] > > except Song.DoesNotExist: > > print "unknown song id (%d)" % id > that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world. It's only syntactic sugar for the functions map and filter. So functions have to be pure functions. To say it in other words, they have to be side-effect free. But the python construct from above pollutes the namespace with name id. > > Greetings from Rottenburg, > Rainer The list might have to grow in a careless way that might lead to a crash in the for inside a list that can't be trapped for errors directly. From jeanmichel at sequans.com Tue Dec 6 05:38:07 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 06 Dec 2011 11:38:07 +0100 Subject: Scope of variable inside list comprehensions? In-Reply-To: <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EDDF08F.4090508@sequans.com> Steven D'Aprano wrote: > On Mon, 05 Dec 2011 19:57:15 +0100, Jean-Michel Pichavant wrote: > > >> The proper way to propagate information with exceptions is using the >> exception itself: >> >> try: >> songs = [Song(_id) for _id in song_ids] >> except Song.DoesNotExist, exc: >> print exc >> > > > I'm not entirely sure that this is the proper way to propagate the > exception. I see far to many people catching exceptions to print them, or > worse, to print a generic, useless message like "an error occurred". > [snip] You misread me, I was referering to passing *information* with exception (in other words, use the exception attributes). In the example I gave, the exception has the songId value responsible for raising the error. I totaly second your opinion on how poor the above handler is (hmm not sure about this grammar construct, it sounds like a Yoda sentence). JM From lanyjie at yahoo.com Tue Dec 6 06:06:04 2011 From: lanyjie at yahoo.com (Yingjie Lan) Date: Tue, 6 Dec 2011 03:06:04 -0800 (PST) Subject: the deceptive continuous assignments Message-ID: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> Hi, I just figured out this with Python3.2 IDLE: >>> class k: pass >>> x=k() >>> x.thing = 1 >>> x.thing 1 >>> x = x.thing = 1 Traceback (most recent call last): ? File "", line 1, in ? ? x = x.thing = 1 AttributeError: 'int' object has no attribute 'thing' >>> x 1 >>> ================ when I do x=x.thing=1, I thought it would be like in C, 1 is first assigned to x.thing, then it is further assigned to x. But what seems to be going on here is that 1 is first assigned to x, then to x.thing (which causes an error). Any reason why would Python deviate from C in this regard? Thanks! Yingjie From andrea.crotti.0 at gmail.com Tue Dec 6 06:28:08 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 06 Dec 2011 11:28:08 +0000 Subject: whitespace cleanup Message-ID: <4EDDFC48.2000903@gmail.com> Now on Emacs I have a hook before every save that cleans up all the "wrong" white spaces, with the 'whitespace-cleanup' function. I would like that also for my non emacsers colleagues, and possibly with a Python script. I looked up around but I can't find anything useful, any advice? Thanks, Andrea From pedro.h.souto at gmail.com Tue Dec 6 06:49:58 2011 From: pedro.h.souto at gmail.com (Pedro Henrique G. Souto) Date: Tue, 06 Dec 2011 09:49:58 -0200 Subject: whitespace cleanup In-Reply-To: <4EDDFC48.2000903@gmail.com> References: <4EDDFC48.2000903@gmail.com> Message-ID: <4EDE0166.1030006@gmail.com> On 06/12/2011 09:28, Andrea Crotti wrote: > Now on Emacs I have a hook before every save that cleans up all the > "wrong" white spaces, > with the 'whitespace-cleanup' function. > > I would like that also for my non emacsers colleagues, and possibly with > a Python script. > I looked up around but I can't find anything useful, any advice? You can use the strip() method: [http://docs.python.org/release/2.3/lib/module-string.html] While reading the file as strings, the strip() method rips out all of the extra whitespace. > Thanks, > Andrea > Good luck! -- Att; Pedro Henrique G. Souto From andrea.crotti.0 at gmail.com Tue Dec 6 06:59:57 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 06 Dec 2011 11:59:57 +0000 Subject: whitespace cleanup In-Reply-To: <4EDE0166.1030006@gmail.com> References: <4EDDFC48.2000903@gmail.com> <4EDE0166.1030006@gmail.com> Message-ID: <4EDE03BD.2030106@gmail.com> On 12/06/2011 11:49 AM, Pedro Henrique G. Souto wrote: > On 06/12/2011 09:28, Andrea Crotti wrote: > > Now on Emacs I have a hook before every save that cleans up all the > > "wrong" white spaces, > > with the 'whitespace-cleanup' function. > > > > I would like that also for my non emacsers colleagues, and possibly > with > > a Python script. > > I looked up around but I can't find anything useful, any advice? > > You can use the strip() method: > [http://docs.python.org/release/2.3/lib/module-string.html] > > While reading the file as strings, the strip() method rips out all of > the extra whitespace. > > > Thanks, > > Andrea > > > > Good luck! > Well it's not so simple, I clearly don't want to strip out whitespace in the beginning of the line, or my nice code will break miserably ;) So I think some magic using tokens/regexp/python grammar would be useful to do something reliable, or is it easier than that? From rosuav at gmail.com Tue Dec 6 07:06:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 23:06:55 +1100 Subject: whitespace cleanup In-Reply-To: <4EDE03BD.2030106@gmail.com> References: <4EDDFC48.2000903@gmail.com> <4EDE0166.1030006@gmail.com> <4EDE03BD.2030106@gmail.com> Message-ID: On Tue, Dec 6, 2011 at 10:59 PM, Andrea Crotti wrote: > Well it's not so simple, I clearly don't want to strip out whitespace in the > beginning of the line, > or my nice code will break miserably ;) The question is: What is "wrong" whitespace? Whatever you declare to be wrong, you can probably come up with a regexp or other spec that will catch it (or conversely, declare some pattern to be "right" and look for anything else). ChrisA From pedro.h.souto at gmail.com Tue Dec 6 07:17:12 2011 From: pedro.h.souto at gmail.com (Pedro Henrique G. Souto) Date: Tue, 06 Dec 2011 10:17:12 -0200 Subject: whitespace cleanup In-Reply-To: References: <4EDDFC48.2000903@gmail.com> <4EDE0166.1030006@gmail.com> <4EDE03BD.2030106@gmail.com> Message-ID: <4EDE07C8.3050004@gmail.com> On 06/12/2011 10:06, Chris Angelico wrote: > On Tue, Dec 6, 2011 at 10:59 PM, Andrea Crotti > wrote: >> Well it's not so simple, I clearly don't want to strip out whitespace in the >> beginning of the line, >> or my nice code will break miserably ;) > > The question is: What is "wrong" whitespace? Whatever you declare to > be wrong, you can probably come up with a regexp or other spec that > will catch it (or conversely, declare some pattern to be "right" and > look for anything else). > > ChrisA Something like PythonTidy does what you want? http://pypi.python.org/pypi/PythonTidy If you like to write your own script, or if what you want is similar, but not the same, the source code is a good place to start: http://lacusveris.com/PythonTidy/PythonTidy-1.20.python -- Att; Pedro Henrique G. Souto From tjreedy at udel.edu Tue Dec 6 07:20:45 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Dec 2011 07:20:45 -0500 Subject: the deceptive continuous assignments In-Reply-To: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> References: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> Message-ID: On 12/6/2011 6:06 AM, Yingjie Lan wrote: > Hi, I just figured out this with Python3.2 IDLE: > >>>> class k: pass >>>> x=k() >>>> x.thing = 1 >>>> x.thing > 1 >>>> x = x.thing = 1 > Traceback (most recent call last): > File "", line 1, in > x = x.thing = 1 > AttributeError: 'int' object has no attribute 'thing' >>>> x > 1 >>>> > > ================ > when I do x=x.thing=1, I thought it would > be like in C, 1 is first assigned to x.thing, > then it is further assigned to x. In C, assignments are expressions, not statements. a = b = c is parsed as a = (b = c). a get the result of (b = c), which happens to be c. In Python, the syntax really should be a =, b = c in that the assignments are more in parallel. > But what seems to be going on here is > that 1 is first assigned to x, then > to x.thing (which causes an error). > > Any reason why would Python deviate > from C in this regard? Python is consistently left to right in expression evaluation. For assignment, it first does left to right on the source side, and then on the target side. "An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right." You have two target lists. x,x.thing = 1,2 would assign 1 to x and then get an error trying to assign a different object to x.thing. In your case, you get an error trying to assign the same object rather than a different object, but the principle is the same. "WARNING: Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are ?safe? (for example a, b = b, a swaps two variables), overlaps within the collection of assigned-to variables are not safe!" You found an unsafe overlap. x.thing = x = 1 would work, though it seems strange (and unlikely in practice) to rebind x to an int after it is bound to a class k instance. -- Terry Jan Reedy From rosuav at gmail.com Tue Dec 6 07:33:26 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 23:33:26 +1100 Subject: the deceptive continuous assignments In-Reply-To: References: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> Message-ID: On Tue, Dec 6, 2011 at 11:20 PM, Terry Reedy wrote: > You found an unsafe overlap. > x.thing = x = 1 > would work, though it seems strange (and unlikely in practice) to rebind x > to an int after it is bound to a class k instance. This code is starting to look like it wants to work with a linked list. >>> class node: def __init__(self,x): self.payload=x self.next=None def walk(self): print("My payload is: "+self.payload) if self.next: self.next.walk() >>> head=tail=node("This") >>> tail.next=tail=node("is") >>> tail.next=tail=node("a") >>> tail.next=tail=node("test.") >>> head.walk() My payload is: This My payload is: is My payload is: a My payload is: test. >>> ChrisA From andrea.crotti.0 at gmail.com Tue Dec 6 08:00:04 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 06 Dec 2011 13:00:04 +0000 Subject: whitespace cleanup In-Reply-To: <4EDE07C8.3050004@gmail.com> References: <4EDDFC48.2000903@gmail.com> <4EDE0166.1030006@gmail.com> <4EDE03BD.2030106@gmail.com> <4EDE07C8.3050004@gmail.com> Message-ID: <4EDE11D4.7060508@gmail.com> On 12/06/2011 12:17 PM, Pedro Henrique G. Souto wrote: > > Something like PythonTidy does what you want? > > http://pypi.python.org/pypi/PythonTidy > > If you like to write your own script, or if what you want is similar, > but not the same, the source code is a good place to start: > > http://lacusveris.com/PythonTidy/PythonTidy-1.20.python > For wrong space I meant what is not pep8 complaint.. Actually even pep8.py is quite good in finding out these things, but it just warns about and doesn't propose to fix the problems. PythonTidy is more what I need, using easy_install doesn't work though, but the direct link on the pypi page does.. From rantingrickjohnson at gmail.com Tue Dec 6 08:13:09 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 6 Dec 2011 05:13:09 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> On Dec 5, 11:10?pm, Chris Angelico wrote: > On Tue, Dec 6, 2011 at 3:36 PM, Xah Lee wrote: > > i don't like python, and i prefer emacs lisp. The primary reason is > > that python is not functional, especially with python 3. The python > > community is full of fanatics with their drivels. In that respect, > > it's not unlike Common Lisp community and Scheme lisp community. > > So you hate Python. Fine. Why post here? Why not just abandon Python > as a dead loss and go code in Lithp? > > Clearly something is keeping you here. Is it that there's something > about Python that you really like, or are you just trolling? I would say Mr. Lee does in fact find some usefulness of Python (as do i) HOWEVER he also laments the asinities that plague the language, the documentation, and especially, this community. Anyone who would take the time to write *articulate* suggestions for grave deficiencies in the Python documentation must care. Mr. Lee, has documented many of the atrocities of the doc in his fine website. I just recently finished reading a few more of his fine suggestions and I especially enjoyed the "os.path.split" rant. PRICELESS! Listen, python is a great language, but not a perfect one. You people seem to get all upset at even the slightest suggestion that Python is not perfect. The haughty arrogance i see day after day around me just makes me sick. Grow up! You are not perfect and neither is Python... or Guido for that matter. This community is chalk full (from top to bottom!) of arrogant, tech geeking, unix hacks who look down on the "outsiders" as excrement. I believe it's high time for these folks to eat a big slice of humble pie -- i just fear there is not enough to go around! O_O From rantingrickjohnson at gmail.com Tue Dec 6 08:21:53 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 6 Dec 2011 05:21:53 -0800 (PST) Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 6, 3:27?am, Nobody wrote: > On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > >> Emitting "\b \b" is one very common way to do a destructive backspace. > >> Inelegant? Perhaps, but a common inelegance. > > > That's pretty much the only way I've seen it done for the past 25 > > years. > > ... before which, it was BS-DEL-BS. > > DEL being 127 means that it punches through all of the holes on the paper > tape, causing whatever was there previously to be replaced with a DEL > character. BS-SPC-BS would only punch through one of the holes. *Wise Observer Speculates:* Why on earth are we "21st century slaves" to an archaic mid 20th century technology that punches holes in paper tape? Anyone? >>> isinstance(Progress, None) True From andrea.crotti.0 at gmail.com Tue Dec 6 08:22:05 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 06 Dec 2011 13:22:05 +0000 Subject: whitespace cleanup In-Reply-To: <4EDE07C8.3050004@gmail.com> References: <4EDDFC48.2000903@gmail.com> <4EDE0166.1030006@gmail.com> <4EDE03BD.2030106@gmail.com> <4EDE07C8.3050004@gmail.com> Message-ID: <4EDE16FD.4030107@gmail.com> So apparently PythonTidy uses the AST (the compiler module) to find out the problems, and generates new nodes with a put method to write out the code with the fix. An awful lot of code is needed to make it work apparently, not very neat. Pep8 instead uses tokenize and regular expressions only, and the code is much neater, not sure how is possible to make it fix things though. From rosuav at gmail.com Tue Dec 6 08:27:48 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Dec 2011 00:27:48 +1100 Subject: Questions about LISP and Python. In-Reply-To: <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: On Wed, Dec 7, 2011 at 12:13 AM, Rick Johnson wrote: > This community is chalk full (from top to bottom!) of arrogant, tech > geeking, unix hacks who look down on the "outsiders" as excrement. I > believe it's high time for these folks to eat a big slice of humble > pie -- i just fear there is not enough to go around! O_O It's also chock full (note that) of trolls. Do we need a python-troll-list at python.org? You two could discuss the upcoming Pyson 4000 (that's Python with a Lithp) and how it's going to revolutionize the world. ChrisA From floris.bruynooghe at gmail.com Tue Dec 6 08:37:47 2011 From: floris.bruynooghe at gmail.com (Floris Bruynooghe) Date: Tue, 6 Dec 2011 05:37:47 -0800 (PST) Subject: Need some IPC pointers In-Reply-To: References: Message-ID: <31237669.168.1323178667071.JavaMail.geo-discussion-forums@vbko11> I'm surprised no one has mentioned zeromq as transport yet. It provides scaling from in proc (between threads) to inter-process and remote machines in a fairly transparent way. It's obviously not the python stdlib and as any system there are downsides too. Regards, Floris From floris.bruynooghe at gmail.com Tue Dec 6 08:37:47 2011 From: floris.bruynooghe at gmail.com (Floris Bruynooghe) Date: Tue, 6 Dec 2011 05:37:47 -0800 (PST) Subject: Need some IPC pointers In-Reply-To: References: Message-ID: <31237669.168.1323178667071.JavaMail.geo-discussion-forums@vbko11> I'm surprised no one has mentioned zeromq as transport yet. It provides scaling from in proc (between threads) to inter-process and remote machines in a fairly transparent way. It's obviously not the python stdlib and as any system there are downsides too. Regards, Floris From rosuav at gmail.com Tue Dec 6 09:03:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Dec 2011 01:03:55 +1100 Subject: Backspace does not erase in stdout In-Reply-To: References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Dec 7, 2011 at 12:21 AM, Rick Johnson wrote: > *Wise Observer Speculates:* Why on earth are we "21st century slaves" > to an archaic mid 20th century technology that punches holes in paper > tape? Anyone? > >>>> isinstance(Progress, None) > True I'm not sure. Let's see... * Manned, powered, heavier-than-air flight: A hundred years. * TCP/IP: Thirty years. * Sliced bread: Eighty years. (What was the greatest thing BEFORE sliced bread?) * The English language: A few hundred, depending on how you count Why are we still eating sliced bread on board aeroplanes while posting in English on internet forums? Clearly we've made no progress since then. What language and communication protocol do you propose for the new python-troll-list? Getting back to the point though... The use of 127 for DEL is a consequence of the exact technology of paper tape. However, the question of whether BS is destructive or not is separate. Should it parallel other characters (draw character at cursor then advance cursor, vs retard cursor then erase character at cursor), or should it parallel cursor movement (eg carriage return, line feed, vertical tab)? If your BS is defined nondestructively, you can implement the destructive version with three characters; if it's defined destructively, you can't implement a nondestructive without some fiddliness (some systems use characters 28-31 for cursor movement). ChrisA From bahamutzero8825 at gmail.com Tue Dec 6 09:05:28 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 06 Dec 2011 08:05:28 -0600 Subject: Need some IPC pointers In-Reply-To: <3519788.203.1322688146008.JavaMail.geo-discussion-forums@yqfv40> References: <3519788.203.1322688146008.JavaMail.geo-discussion-forums@yqfv40> Message-ID: <4EDE2128.6040509@gmail.com> What about named pipes? I don't mind a bit of "if Windows do this, else, do that" as long I'm not coding two or more completely different approaches. I'm not too familiar with named pipes, though; perhaps someone with some experience could chime in. Apparently this didn't go through to Google Groups the first time; it may show up twice for some. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From skip at montanaro.dyndns.org Tue Dec 6 09:26:16 2011 From: skip at montanaro.dyndns.org (Skip Montanaro) Date: Tue, 6 Dec 2011 08:26:16 -0600 (CST) Subject: How to build 64-bit Python on Solaris with GCC? Message-ID: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> I'd like to build a 64-bit version of Python on Solaris using gcc. I did a bit of Googling, but everything I came up with seemed old, inconclusive or assumes the use of the Sun Studio compiler, with which i have no experience. Does anyone have a recipe for the subject build? Thanks, -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ From roy at panix.com Tue Dec 6 09:45:08 2011 From: roy at panix.com (Roy Smith) Date: Tue, 06 Dec 2011 09:45:08 -0500 Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Nobody wrote: > On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > > >> Emitting "\b \b" is one very common way to do a destructive backspace. > >> Inelegant? Perhaps, but a common inelegance. > > > > That's pretty much the only way I've seen it done for the past 25 > > years. > > ... before which, it was BS-DEL-BS. > > DEL being 127 means that it punches through all of the holes on the paper > tape, causing whatever was there previously to be replaced with a DEL > character. BS-SPC-BS would only punch through one of the holes. I don't remember having a DEL key on the ASR-33. It did have a RUBOUT key (http://regmedia.co.uk/2010/06/21/teletype_hereis.jpg) which performed the function you describe. I don't remember if that actually sent a 177 code over the wire or if it was a purely local function. Also, the BS code only caused the printhead to back up. If you wanted to back up the tape, you had to press the "B. SP." button the the punch (http://pichotjm.free.fr/Multi8/TTY/ASR33-3.html). Of course, sending a BS-SPS-BS sequence on a printing terminal didn't do anything about erasing what had previously been printed. To do that, you needed one of those fancy vee-de-oh things. On the ASR-33, to prevent passwords from being readable, you either disabled echo, or sent something like BS-*-BS-X-BS-$-BS-%-BS-#-BS- at -BS-&-BS-H-BS-Z-BS-M in the hopes that nothing would be readable under all that garbage. From roy at panix.com Tue Dec 6 09:49:01 2011 From: roy at panix.com (Roy Smith) Date: Tue, 06 Dec 2011 09:49:01 -0500 Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Rick Johnson wrote: > *Wise Observer Speculates:* Why on earth are we "21st century slaves" > to an archaic mid 20th century technology that punches holes in paper > tape? Anyone? Or to an archaic mid 20th century technology that limited lines to 80 characters? From invalid at invalid.invalid Tue Dec 6 10:22:18 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 6 Dec 2011 15:22:18 +0000 (UTC) Subject: Single key press References: Message-ID: On 2011-12-06, Sergi Pasoev wrote: > I wonder if it is realistic to get a single key press in Python > without ncurses or > any similar library. Yes. Just put the tty associated with stdin in raw mode and make single byte read() calls on it. Remember to restore the tty settings when your program exits. -- Grant Edwards grant.b.edwards Yow! World War III? at No thanks! gmail.com From invalid at invalid.invalid Tue Dec 6 10:24:43 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 6 Dec 2011 15:24:43 +0000 (UTC) Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-12-06, Nobody wrote: > On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > >>> Emitting "\b \b" is one very common way to do a destructive backspace. >>> Inelegant? Perhaps, but a common inelegance. >> >> That's pretty much the only way I've seen it done for the past 25 >> years. > > ... before which, it was BS-DEL-BS. > > DEL being 127 means that it punches through all of the holes on the > paper tape, causing whatever was there previously to be replaced with > a DEL character. > BS-SPC-BS would only punch through one of the holes. Ah yes. And when you read a tape, you silently ignored DEL characters. I had completely fogotten about that. I only used paper tape very briefly, and only for storage, never as a "live" medium for editing. -- Grant Edwards grant.b.edwards Yow! I own seven-eighths of at all the artists in downtown gmail.com Burbank! From dihedral88888 at googlemail.com Tue Dec 6 10:38:15 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 6 Dec 2011 07:38:15 -0800 (PST) Subject: Single key press In-Reply-To: References: Message-ID: <29192698.206.1323185896052.JavaMail.geo-discussion-forums@prfs25> On Tuesday, December 6, 2011 11:22:18 PM UTC+8, Grant Edwards wrote: > On 2011-12-06, Sergi Pasoev wrote: > > > I wonder if it is realistic to get a single key press in Python > > without ncurses or > > any similar library. > > Yes. Just put the tty associated with stdin in raw mode and make > single byte read() calls on it. Remember to restore the tty settings > when your program exits. > > -- > Grant Edwards grant.b.edwards Yow! World War III? > at No thanks! > gmail.com Cheers to virus scan software that has to scan fast and easily for all files under a drive. Easy clean up in APPS. From p.f.moore at gmail.com Tue Dec 6 10:57:25 2011 From: p.f.moore at gmail.com (Paul Moore) Date: Tue, 6 Dec 2011 07:57:25 -0800 (PST) Subject: Referring to the class name from a class variable where inheritance is involved Message-ID: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> I want to set up an inheritance hierarchy. The base class will define a string value which should include the class name, but I don't want people who inherit from my class to have to remember to override the value. If I do this using an instance variable, it's reasonably easy: >>> class Base: ... def __init__(self): ... self.key = 'Key_for_' + self.__class__.__name__ ... def display(self): ... print self.key ... >>> class Inherited(Base): ... pass ... >>> b = Base() >>> i = Inherited() >>> b.display() Key_for_Base >>> i.display() Key_for_Inherited Rather than having the key for every instance, I'd like to use a class variable, but I can't see how I'd make that work (a class variable which is inherited but has a different value in derived classes). I could use a classmethod,but that feels like even more overkill than an instance attribute. Is there a way of doing this via class variables or something, or more relevantly, I guess, what would be the idiomatic way of doing something like this? Thanks, Paul From ian.g.kelly at gmail.com Tue Dec 6 11:58:02 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Dec 2011 09:58:02 -0700 Subject: Questions about LISP and Python. In-Reply-To: <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: On Tue, Dec 6, 2011 at 6:13 AM, Rick Johnson wrote: > I would say Mr. Lee does in fact find some usefulness of Python (as do > i) HOWEVER he also laments the asinities that plague the language, the > documentation, and especially, this community. > > Anyone who would take the time to write *articulate* suggestions for > grave deficiencies in the Python documentation must care. Mr. Lee, has > documented many of the atrocities of the doc in his fine website. I > just recently finished reading a few more of his fine suggestions and > I especially enjoyed the "os.path.split" rant. PRICELESS! I would be much more impressed if instead of simply whining about the documentation, Xah Lee would come off his high horse and actually submit a patch to fix it. That would be the mark of somebody who actually cares about the quality of the documentation. He seems to be suggesting that the problems he points out are just examples of systemic problems that make the whole incomprehensible, yet most of what he specifically identifies seems to be just copy editing for conciseness. What I find hilarious, though, is that Xah Lee has written his own Python documentation, and it's equally bad, if not worse. Just for example, I'll critique an arbitrary section. Let's talk about for loops. http://xahlee.org/perl-python/for_statement.html """This is a example of ?for? statement.. a = range(1,51) # creates a list for x in a: if x % 2 == 0: print x, 'even' """ What is a "for" statement? What does it do? If I'm new to programming, I have no idea, and the above hasn't even attempted to introduce the concept, so that example is meaningless to me. Fortunately, he starts to explain it. """In the above, the percent ?%? symbol calculates the remainder of division. The range(m,n) function gives a list from m to n-1""" Wait, I thought this section was about "for" statements. Why is the author suddenly (and poorly) explaining modulo division and the range function? These things belong in their own section. """Note that in this example, ?for? goes over a list.""" Oh, okay, the author finally is telling us what "for" does. It is used to "go over" lists, apparently so that we can get to the other side. Note that this documentation still has not said anything at all about "looping" or "repeating" or "iterating" that might help me understand what that means. Instead we have "goes over". If I did not already understand what a "for" statement does, this would not make it any clearer. It reminds me of when I was 6 or so and I asked my mother what division was, and she told me that it's how many times a number "goes into" another number. Even though I was mathematically inclined, I didn't understand that all, because I had no idea what it meant for one number to enter another. """Each time making ?x? the value of the element.""" Okay, apparently there's an element involved, and "x" is going to be its value. I really don't think I need to say anything more about this "sentence". """Python also supports ?break? and ?continue? to exit the loop. ?break? will exit the loop. ?continue? will skip code and start the next iteration.""" Finally the author breaks down and uses the words "loop" and "iteration", so if the reader actually makes it this far, then they might actually start to piece together everything that came before this. I also want to highlight his introduction of list comprehensions: http://xahlee.org/perl-python/list_comprehension.html """This construct uses a irregular syntax (called ?list comprehension?) to generate a expression normally built by nested loops. This construct has acquired a incomprehensible name ?list comprehension? in computing industry and academia. ... Remember, this jargonized ?list comprehension? is nothing more than a irregular syntax for building a list from loops. Its purpose is purely of syntactical convenience. Advanced languages such as functional languages often have this power without the syntax irregularity. (For example, Mathematica's Table function.) """ What was that again about "author masturbation" polluting the documentation, Xah? On Tue, Dec 6, 2011 at 6:13 AM, Rick Johnson wrote: > Listen, python is a great language, but not a perfect one. You people > seem to get all upset at even the slightest suggestion that Python is > not perfect. The haughty arrogance i see day after day around me just > makes me sick. Grow up! You are not perfect and neither is Python... > or Guido for that matter. I don't think Python, Guido, or myself are perfect. I think that most people here don't think that either. Python has its warts. For example, I don't like generator syntax. To be a generator, a function only has to have a "yield" statement somewhere in its body. This is not good self-documenting code. A generator should be clearly marked as such somewhere at the top of the definition. I also think that tacking coroutines onto generators was an ugly hack. Generators and coroutines are conceptually very different; let's keep them that way. And I strongly dislike super(); you may have seen me posting previously on the subject. You need to understand that people don't reject your suggestions because they're resistant to change. They reject your suggestions because you make sweeping pronouncements about the way things should be and then expect that everybody will automatically agree with you because you're so obviously right. In this, you seem to think that people should regard you in the way that you imagine they already regard Guido. In the end, you come across as a clown and an egomaniac. From matt at scotweb.co.uk Tue Dec 6 12:29:45 2011 From: matt at scotweb.co.uk (Matt Saxton) Date: Tue, 06 Dec 2011 17:29:45 +0000 Subject: Referring to the class name from a class variable where inheritance is involved In-Reply-To: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> References: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> Message-ID: <4EDE5109.5000209@scotweb.co.uk> On 06/12/11 15:57, Paul Moore wrote: > I want to set up an inheritance hierarchy. The base class will define > a string value which should include the class name, but I don't want > people who inherit from my class to have to remember to override the > value. > > If I do this using an instance variable, it's reasonably easy: > >>>> class Base: > ... def __init__(self): > ... self.key = 'Key_for_' + self.__class__.__name__ > ... def display(self): > ... print self.key > ... >>>> class Inherited(Base): > ... pass > ... >>>> b = Base() >>>> i = Inherited() >>>> b.display() > Key_for_Base >>>> i.display() > Key_for_Inherited > > Rather than having the key for every instance, I'd like to use a class > variable, but I can't see how I'd make that work (a class variable > which is inherited but has a different value in derived classes). I > could use a classmethod,but that feels like even more overkill than an > instance attribute. > > Is there a way of doing this via class variables or something, or more > relevantly, I guess, what would be the idiomatic way of doing > something like this? > > Thanks, > Paul You can use a metaclass for this: >>> class BaseMeta(type): ... def __new__(mcs, name, bases, dict): ... dict['key'] = 'Key_for_%s' % name ... return type.__new__(mcs, name, bases, dict) ... >>> class Base: ... __metaclass__ = BaseMeta ... >>> class Inherited(Base): ... pass ... >>> Base.key 'Key_for_Base' >>> Inherited.key 'Key_for_Inheritor' You can find more info on metaclasses here: http://http://docs.python.org/reference/datamodel.html#customizing-class-creation Regards Matt From tobiah at teranews.com Tue Dec 6 12:33:30 2011 From: tobiah at teranews.com (Tobiah) Date: Tue, 06 Dec 2011 09:33:30 -0800 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: > What is the opinion of the wizards here, shall I learm Python 2 or > Python 3? I'm posting this here because I feel that this point is > interesting to other students of Python. Use the newer version and don't look back. From s.pasoev at gmail.com Tue Dec 6 12:34:36 2011 From: s.pasoev at gmail.com (Sergi Pasoev) Date: Tue, 6 Dec 2011 22:04:36 +0430 Subject: Python-list Digest, Vol 99, Issue 28 In-Reply-To: References: Message-ID: Thanks Steven. Maybe you mean Gnu/Linux when you say Linux. 2011/12/6 : > Send Python-list mailing list submissions to > ? ? ? ?python-list at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > ? ? ? ?http://mail.python.org/mailman/listinfo/python-list > or, via email, send a message with subject or body 'help' to > ? ? ? ?python-list-request at python.org > > You can reach the person managing the list at > ? ? ? ?python-list-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Python-list digest..." > > Today's Topics: > > ? 1. Re: Python-list Digest, Vol 99, Issue 27 (Sergi Pasoev) > ? 2. Re: Single key press (alex23) > ? 3. Re: Single key press (Steven D'Aprano) > ? 4. Re: struct calcsize discrepency? (Nobody) > ? 5. Re: Questions about LISP and Python. (Alex Mizrahi) > ? 6. Re: Backspace does not erase in stdout (Nobody) > ? 7. Re: Single key press (Hans Mulder) > ? 8. Re: Single key press (Nobody) > ? 9. Re: Scope of variable inside list comprehensions? (88888 Dihedral) > ?10. Re: Scope of variable inside list comprehensions? > ? ? ?(Jean-Michel Pichavant) > > > ---------- Message transf?r? ---------- > From:?Sergi Pasoev > To:?python-list at python.org > Date:?Tue, 6 Dec 2011 12:59:54 +0430 > Subject:?Re: Python-list Digest, Vol 99, Issue 27 > I do not want GUI, I just want a console application which will read a > single character > input. When you read from stdin for example, you have it to be terminated. > > 2011/12/6 ?: >> Send Python-list mailing list submissions to >> ? ? ? ?python-list at python.org >> >> To subscribe or unsubscribe via the World Wide Web, visit >> ? ? ? ?http://mail.python.org/mailman/listinfo/python-list >> or, via email, send a message with subject or body 'help' to >> ? ? ? ?python-list-request at python.org >> >> You can reach the person managing the list at >> ? ? ? ?python-list-owner at python.org >> >> When replying, please edit your Subject line so it is more specific >> than "Re: Contents of Python-list digest..." >> >> Today's Topics: >> >> ? 1. Re: Questions about LISP and Python. (Xah Lee) >> ? 2. Re: Questions about LISP and Python. (Chris Angelico) >> ? 3. Re: 70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when >> ? ? ?sending ? large object (Dennis Lee Bieber) >> ? 4. Single key press (Sergi Pasoev) >> ? 5. Re: Fwd: class print method... (Suresh Sharma) >> ? 6. Re: Single key press (88888 Dihedral) >> ? 7. Re: Single key press (88888 Dihedral) >> ? 8. Re: Scope of variable inside list comprehensions? (Rainer Grimm) >> ? 9. Re: Questions about LISP and Python. (alex23) >> ?10. Re: Questions about LISP and Python. (Matt Joiner) >> >> >> ---------- Message transf?r? ---------- >> From:?Xah Lee >> To:?python-list at python.org >> Date:?Mon, 5 Dec 2011 20:36:45 -0800 (PST) >> Subject:?Re: Questions about LISP and Python. >> On Dec 5, 4:31?am, Tim Bradshaw wrote: >>> On 2011-12-05 11:51:11 +0000, Xah Lee said: >>> >>> > python has more readible syntax, more modern computer language >>> > concepts, and more robust libraries. These qualities in turn made it >>> > popular. >>> >>> Yet you still post here: why? >> >> i don't like python, and i prefer emacs lisp. The primary reason is >> that python is not functional, especially with python 3. The python >> community is full of fanatics with their drivels. In that respect, >> it's not unlike Common Lisp community and Scheme lisp community. >> >> see also: >> >> ?Python Documentation Problems? >> http://xahlee.org/perl-python/python_doc_index.html >> >> ?Computer Language Design: What's List Comprehension and Why is It >> Harmful?? >> http://xahlee.org/comp/list_comprehension.html >> >> ?Lambda in Python 3000? >> http://xahlee.org/perl-python/python_3000.html >> >> ?What Languages to Hate? >> http://xahlee.org/UnixResource_dir/writ/language_to_hate.html >> >> ?Xah on Programing Languages? >> http://xahlee.org/Periodic_dosage_dir/comp_lang.html >> >> ?Xah >> >> >> >> ---------- Message transf?r? ---------- >> From:?Chris Angelico >> To:?python-list at python.org >> Date:?Tue, 6 Dec 2011 16:10:27 +1100 >> Subject:?Re: Questions about LISP and Python. >> On Tue, Dec 6, 2011 at 3:36 PM, Xah Lee wrote: >>> i don't like python, and i prefer emacs lisp. The primary reason is >>> that python is not functional, especially with python 3. The python >>> community is full of fanatics with their drivels. In that respect, >>> it's not unlike Common Lisp community and Scheme lisp community. >> >> So you hate Python. Fine. Why post here? Why not just abandon Python >> as a dead loss and go code in Lithp? >> >> Clearly something is keeping you here. Is it that there's something >> about Python that you really like, or are you just trolling? >> >> ChrisA >> >> >> >> ---------- Message transf?r? ---------- >> From:?Dennis Lee Bieber >> To:?python-list at python.org >> Date:?Mon, 05 Dec 2011 21:15:09 -0800 >> Subject:?Re: 70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when sending large object >> On Mon, 5 Dec 2011 18:49:44 +0100, DPalao >> wrote: >> >>>Obviously it was not me who added the disgusting "70% [* SPAM *]" string to >>>the subject. And I'd like to know the answer too. >>> >> ? ? ? ?Based upon your headers, somewhere your message went through a spam >> check program... >> >> X-Greylist: Sender succeeded SMTP AUTH, not delayed by >> milter-greylist-4.3.7 >> ? ? ? ?(monster.roma2.infn.it [141.108.255.100]); >> ? ? ? ?Mon, 05 Dec 2011 09:02:22 +0100 (CET) >> X-PMX-Version: 5.6.1.2065439, Antispam-Engine: 2.7.2.376379, >> ? ? ? ?Antispam-Data: 2011.12.5.74814 >> X-PMX-Spam: 70% >> X-PMX-Spam-report: The following antispam rules were triggered by this >> message: >> ? ? ? ?Rule ? ? ? ? ? ? ? ? ? ? ?Score Description >> ? ? ? ?RDNS_SUSP_FORGED_FROM ? ? 3.500 From domain appears to be forged, >> ? ? ? ?and arrived via a host with a known suspicious rDNS. >> ? ? ? ?SXL_IP_DYNAMIC 3.000 Received via a known dynamic IP (SXL lookup): >> ? ? ? ?34.140.47.93.fur >> ? ? ? ?FORGED_FROM_GMAIL ? ? ? ? 0.100 Appears to forge gmail in the from >> ? ? ? ?FROM_NAME_ONE_WORD ? ? ? ?0.050 Name in From header is a single word >> ? ? ? ?BODYTEXTP_SIZE_3000_LESS 0.000 Body size of the text/plain part is >> less >> ? ? ? ?than 3k >> ? ? ? ?BODY_SIZE_1500_1599 0.000 Message body size is 1500 to 1599 bytes >> ? ? ? ?BODY_SIZE_2000_LESS 0.000 Message body size is less than 2000 bytes. >> ? ? ? ?BODY_SIZE_5000_LESS 0.000 Message body size is less than 5000 bytes. >> ? ? ? ?BODY_SIZE_7000_LESS 0.000 Message body size is less than 5000 bytes. >> ? ? ? ?RDNS_GENERIC_POOLED 0.000 Sender's PTR record matches generic pooled >> -- >> ? ? ? ?Wulfraed ? ? ? ? ? ? ? ? Dennis Lee Bieber ? ? ? ? AF6VN >> ? ? ? ?wlfraed at ix.netcom.com ? ?HTTP://wlfraed.home.netcom.com/ >> >> >> >> >> ---------- Message transf?r? ---------- >> From:?Sergi Pasoev >> To:?python-list at python.org >> Date:?Tue, 6 Dec 2011 10:19:55 +0430 >> Subject:?Single key press >> Hi. >> >> I wonder if it is realistic to get a single key press in Python >> without ncurses or >> any similar library. In single key press I mean something like j and k >> in Gnu less >> program, you press the key and and it is captured by the script without need to >> press enter afterwards >> >> >> >> ---------- Message transf?r? ---------- >> From:?Suresh Sharma >> To:?Lie Ryan >> Date:?Tue, 6 Dec 2011 11:23:16 +0530 >> Subject:?Re: Fwd: class print method... >> Dave / Ryan >> Thanks i have got it and it worked after using repr statement. Thanks everyone for their valuable feedback. >> >> >> >> On Mon, Dec 5, 2011 at 6:11 PM, Lie Ryan wrote: >>> >>> On 12/05/2011 10:18 PM, Suresh Sharma wrote: >>>> >>>> >>>> Pls help its really frustrating >>>> ---------- Forwarded message ---------- >>>> From: Suresh Sharma >>>> Date: Monday, December 5, 2011 >>>> Subject: class print method... >>>> To: "d at davea.name " >>> > >>>> >>>> >>>> Dave, >>>> Thanx for the quick response, i am sorry that i did not explain >>>> correctly look at the code below inspite of this i am just getting class >>>> object at memory location.I am sort i typed all this code on my android >>>> in a hurry so.indentation could.not.be.managed but this.similar code >>>> when i run all my objects created by class deck are not shown but stored >>>> in varioia meory locations. How can i display them. >>>> >>> >>> I think you're in the right track, however I suspect you're running the code in the shell instead of as a script. The shell uses __repr__() to print objects instead of __str__(), so you either need to use 'print' or you need to call str(), note the following: >>> >>> Python 2.7.2+ (default, Oct ?4 2011, 20:06:09) >>> [GCC 4.6.1] on linux2 >>> Type "help", "copyright", "credits" or "license" for more information. >>> >>> suits = ['spades', 'clubs', 'diamonds', 'hearts'] >>> >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] >>> >>> class Card: >>> ... ? ? def __init__(self, rank, suit): >>> ... ? ? ? ? self.suit = suit >>> ... ? ? ? ? self.rank = rank >>> ... ? ? def __str__(self): >>> ... ? ? ? ? return suits[self.suit] + ' ' + ranks[self.rank] >>> ... >>> >>> Card(2, 3) #1 >>> <__main__.Card instance at 0x7f719c3a20e0> >>> >>> str(Card(2, 3)) #2 of your >>> 'hearts 3' >>> >>> print Card(2, 3) #3 >>> hearts 3 >>> >>> In #1, the output is the __repr__() of your Card class; you can modify this output by overriding the __repr__() on your Card class. >>> >>> In #2, the output is the __repr__() of a string, the string is the return value from __str__() of your Card class. The repr of a string is the string enclosed in quotes, which is why there is an extra pair of quotes. >>> >>> In #3, you're 'print'-ing a string, the string is the return value from __str__() of your Card class. There's no extra quotes, since 'print' prints the string itself, not the repr of the string. >>> >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> >> >> >> >> -- >> Suresh Sharma >> Regional Project Manager, >> O2F,Mumbai >> Maharashtra-400101. >> >> >> >> >> >> ---------- Message transf?r? ---------- >> From:?88888 Dihedral >> To:?comp.lang.python at googlegroups.com >> Date:?Mon, 5 Dec 2011 22:27:48 -0800 (PST) >> Subject:?Re: Single key press >> On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: >>> Hi. >>> >>> I wonder if it is realistic to get a single key press in Python >>> without ncurses or >>> any similar library. In single key press I mean something like j and k >>> in Gnu less >>> program, you press the key and and it is captured by the script without need to >>> press enter afterwards >> >> Sounds like the ?fast key searching for a list in the DOS application in the >> old days. >> >> This is easy in GUI of just tens of items, but for thousands of items such >> as in a directory listing, some API is really slow. >> >> >> >> >> >> ---------- Message transf?r? ---------- >> From:?88888 Dihedral >> To:?python-list at python.org >> Date:?Mon, 5 Dec 2011 22:27:48 -0800 (PST) >> Subject:?Re: Single key press >> On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: >>> Hi. >>> >>> I wonder if it is realistic to get a single key press in Python >>> without ncurses or >>> any similar library. In single key press I mean something like j and k >>> in Gnu less >>> program, you press the key and and it is captured by the script without need to >>> press enter afterwards >> >> Sounds like the ?fast key searching for a list in the DOS application in the >> old days. >> >> This is easy in GUI of just tens of items, but for thousands of items such >> as in a directory listing, some API is really slow. >> >> >> >> >> >> ---------- Message transf?r? ---------- >> From:?Rainer Grimm >> To:?python-list at python.org >> Date:?Mon, 5 Dec 2011 22:42:35 -0800 (PST) >> Subject:?Re: Scope of variable inside list comprehensions? >> Hello, >> >>> ? ? try: >>> ? ? ? ? songs = [Song(id) for id in song_ids] >>> ? ? except Song.DoesNotExist: >>> ? ? ? ? print "unknown song id (%d)" % id >> that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world. It's only syntactic sugar for the functions map and filter. So functions have to be pure functions. To say it in other words, they have to be side-effect free. But the python construct from above pollutes the namespace with name id. >> >> Greetings from Rottenburg, >> Rainer >> >> >> >> ---------- Message transf?r? ---------- >> From:?alex23 >> To:?python-list at python.org >> Date:?Mon, 5 Dec 2011 23:02:42 -0800 (PST) >> Subject:?Re: Questions about LISP and Python. >> On Dec 6, 2:36?pm, Xah Lee wrote: >>> The python community is full of fanatics with their drivels. >> >> You do know that you could just fuck right off and leave us to it, >> yes? >> >> In general, it's the person who is shrilly imposing their minority >> opinion on a disinterested audience that deserves the title 'fanatic'. >> >> >> >> ---------- Message transf?r? ---------- >> From:?Matt Joiner >> To:?alex23 >> Date:?Tue, 6 Dec 2011 18:54:09 +1100 >> Subject:?Re: Questions about LISP and Python. >> This guy is an even better troll than that 88888 guy. His spelling is >> equally bad. His essays make some good points, but I don't see why he >> doesn't shut his trap and move on. >> >> ?_? >> >> >> >> On Tue, Dec 6, 2011 at 6:02 PM, alex23 wrote: >>> On Dec 6, 2:36?pm, Xah Lee wrote: >>>> The python community is full of fanatics with their drivels. >>> >>> You do know that you could just fuck right off and leave us to it, >>> yes? >>> >>> In general, it's the person who is shrilly imposing their minority >>> opinion on a disinterested audience that deserves the title 'fanatic'. >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> >> >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > > ---------- Message transf?r? ---------- > From:?alex23 > To:?python-list at python.org > Date:?Tue, 6 Dec 2011 00:39:53 -0800 (PST) > Subject:?Re: Single key press > On Dec 6, 3:49?pm, Sergi Pasoev wrote: >> I wonder if it is realistic to get a single key press in Python >> without ncurses or any similar library. > > It's possible using Tkinter in the standard library: > > http://www.daniweb.com/software-development/python/code/216830 > > > > > ---------- Message transf?r? ---------- > From:?Steven D'Aprano > To:?python-list at python.org > Date:?06 Dec 2011 08:48:39 GMT > Subject:?Re: Single key press > On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: > >> Hi. >> >> I wonder if it is realistic to get a single key press in Python without >> ncurses or >> any similar library. In single key press I mean something like j and k >> in Gnu less >> program, you press the key and and it is captured by the script without >> need to press enter afterwards > > > Coincidentally, I was working on that exact function last night, so you > inspired me to put it up on the Python cookbook. This version should work > on any POSIX system such as Linux, as well as Windows. I don't know if it > will work on Macintosh. > > http://code.activestate.com/recipes/577977-get-single-keypress/ > > > > -- > Steven > > > > ---------- Message transf?r? ---------- > From:?Nobody > To:?python-list at python.org > Date:?Tue, 06 Dec 2011 08:55:16 +0000 > Subject:?Re: struct calcsize discrepency? > On Mon, 05 Dec 2011 00:20:32 -0800, Mark Dickinson wrote: > >>> May be, yes, but since calcsize() is returning 12 when the elements >>> are put in the other order, it would seem to be not counting such >>> padding. >> >> Indeed. ?That's arguably a bug in the struct module, > > There's no "arguably" about it. The documentation says: > > ? ? ? ?Native size and alignment are determined using the C compiler?s sizeof > ? ? ? ?expression. > > But given: > > ? ? ? ?struct { unsigned long long a; char b[4]; } foo; > ? ? ? ?struct { char b[4]; unsigned long long a; } bar; > > sizeof(foo) will always equal sizeof(bar). If long long is 8 bytes and has > 8-byte alignment, both will be 16. > > If you want consistency with the in-memory representation used by > C/C++ programs (and the on-disk representation used by C/C++ programs > which write the in-memory representation directly to file), use ctypes; > e.g.: > > ? ? ? ?>>> from ctypes import * > ? ? ? ?>>> class foo(Structure): > ? ? ? ? ? ? ? ?_fields_ = [ > ? ? ? ? ? ? ? ? ? ? ? ?("a", c_ulonglong), > ? ? ? ? ? ? ? ? ? ? ? ?("b", c_char * 4)] > > ? ? ? ?>>> sizeof(foo) > ? ? ? ?16 > > > > > ---------- Message transf?r? ---------- > From:?Alex Mizrahi > To:?python-list at python.org > Date:?Tue, 06 Dec 2011 11:12:31 +0200 > Subject:?Re: Questions about LISP and Python. >> >> i don't like python, and i prefer emacs lisp. The primary reason is >> that python is not functional, especially with python 3. The python >> community is full of fanatics with their drivels. In that respect, >> it's not unlike Common Lisp community and Scheme lisp community. > > > What functional programming features do you use, Xah? Last time I've checked, you didn't care about closures. In this respect, Python is better at functional programming than Emacs Lisp. > > Do you just like writing arbitrarily long expression? Or you think that eval is functional? > > > > ---------- Message transf?r? ---------- > From:?Nobody > To:?python-list at python.org > Date:?Tue, 06 Dec 2011 09:27:29 +0000 > Subject:?Re: Backspace does not erase in stdout > On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > >>> Emitting "\b \b" is one very common way to do a destructive backspace. >>> Inelegant? Perhaps, but a common inelegance. >> >> That's pretty much the only way I've seen it done for the past 25 >> years. > > ... before which, it was BS-DEL-BS. > > DEL being 127 means that it punches through all of the holes on the paper > tape, causing whatever was there previously to be replaced with a DEL > character. BS-SPC-BS would only punch through one of the holes. > > > > > ---------- Message transf?r? ---------- > From:?Hans Mulder > To:?python-list at python.org > Date:?Tue, 06 Dec 2011 10:28:32 +0100 > Subject:?Re: Single key press > On 6/12/11 09:48:39, Steven D'Aprano wrote: >> >> On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: >> >>> Hi. >>> >>> I wonder if it is realistic to get a single key press in Python without >>> ncurses or >>> any similar library. In single key press I mean something like j and k >>> in Gnu less >>> program, you press the key and and it is captured by the script without >>> need to press enter afterwards >> >> >> >> Coincidentally, I was working on that exact function last night, so you >> inspired me to put it up on the Python cookbook. This version should work >> on any POSIX system such as Linux, as well as Windows. I don't know if it >> will work on Macintosh. >> >> http://code.activestate.com/recipes/577977-get-single-keypress/ > > > It works fine on MacOS/X, but probably not on MacOS Classic. > > -- HansM > > > > > > > ---------- Message transf?r? ---------- > From:?Nobody > To:?python-list at python.org > Date:?Tue, 06 Dec 2011 09:48:55 +0000 > Subject:?Re: Single key press > On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: > >> I wonder if it is realistic to get a single key press in Python >> without ncurses or any similar library. > > On Unix, you need to use termios.tcsetattr() to disable "canonical mode". > Otherwise, the tty driver will only pass data up to the application when > the user enters a newline or EOF. > > If you do this, you also need to handle SIGTSTP and SIGCONT, restoring the > original terminal settings on SIGTSTP (typically generated by Ctrl-Z) and > restoring the application's setting on SIGCONT. > > > > > ---------- Message transf?r? ---------- > From:?88888 Dihedral > To:?python-list at python.org > Date:?Tue, 6 Dec 2011 02:16:42 -0800 (PST) > Subject:?Re: Scope of variable inside list comprehensions? > On Tuesday, December 6, 2011 2:42:35 PM UTC+8, Rainer Grimm wrote: >> Hello, >> >> > ? ? try: >> > ? ? ? ? songs = [Song(id) for id in song_ids] >> > ? ? except Song.DoesNotExist: >> > ? ? ? ? print "unknown song id (%d)" % id >> that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world. It's only syntactic sugar for the functions map and filter. So functions have to be pure functions. To say it in other words, they have to be side-effect free. But the python construct from above pollutes the namespace with name id. >> >> Greetings from Rottenburg, >> Rainer > > The list might have to grow in a careless way that might lead to a crash > in the for inside a list that can't be trapped for errors directly. > > > > > ---------- Message transf?r? ---------- > From:?Jean-Michel Pichavant > To:?Steven D'Aprano > Date:?Tue, 06 Dec 2011 11:38:07 +0100 > Subject:?Re: Scope of variable inside list comprehensions? > Steven D'Aprano wrote: >> >> On Mon, 05 Dec 2011 19:57:15 +0100, Jean-Michel Pichavant wrote: >> >> >>> >>> The proper way to propagate information with exceptions is using the >>> exception itself: >>> >>> try: >>> ? ?songs = [Song(_id) for _id in song_ids] >>> except Song.DoesNotExist, exc: >>> ? ?print exc >>> >> >> >> >> I'm not entirely sure that this is the proper way to propagate the exception. I see far to many people catching exceptions to print them, or worse, to print a generic, useless message like "an error occurred". >> > > [snip] > > You misread me, I was referering to passing *information* with exception (in other words, use the exception attributes). In the example I gave, the exception has the songId value responsible for raising the error. > I totaly second your opinion on how poor the above handler is (hmm not sure about this grammar construct, it sounds like a Yoda sentence). > > JM > > > -- > http://mail.python.org/mailman/listinfo/python-list From s.pasoev at gmail.com Tue Dec 6 12:55:29 2011 From: s.pasoev at gmail.com (Sergi Pasoev) Date: Tue, 6 Dec 2011 22:25:29 +0430 Subject: 2 or 3 ? Message-ID: Yes indeed, use the newer version. I thought this problem was already solved, Python 3 has already been there for long enough time. From ian.g.kelly at gmail.com Tue Dec 6 13:12:14 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Dec 2011 11:12:14 -0700 Subject: Referring to the class name from a class variable where inheritance is involved In-Reply-To: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> References: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> Message-ID: On Tue, Dec 6, 2011 at 8:57 AM, Paul Moore wrote: > I want to set up an inheritance hierarchy. The base class will define > a string value which should include the class name, but I don't want > people who inherit from my class to have to remember to override the > value. > > If I do this using an instance variable, it's reasonably easy: > >>>> class Base: > ... ? ? def __init__(self): > ... ? ? ? ? self.key = 'Key_for_' + self.__class__.__name__ > ... ? ? def display(self): > ... ? ? ? ? print self.key > ... >>>> class Inherited(Base): > ... ? ? pass > ... >>>> b = Base() >>>> i = Inherited() >>>> b.display() > Key_for_Base >>>> i.display() > Key_for_Inherited > > Rather than having the key for every instance, I'd like to use a class > variable, but I can't see how I'd make that work (a class variable > which is inherited but has a different value in derived classes). I > could use a classmethod,but that feels like even more overkill than an > instance attribute. > > Is there a way of doing this via class variables or something, or more > relevantly, I guess, what would be the idiomatic way of doing > something like this? How about a class property? class classproperty(object): def __init__(self, fget): self.__fget = fget def __get__(self, instance, owner): return self.__fget(owner) class BaseClass(object): @classproperty def key(cls): return "Key_for_" + cls.__name__ class DerivedClass(BaseClass): pass assert BaseClass.key == BaseClass().key == "Key_for_BaseClass" assert DerivedClass.key == DerivedClass().key == "Key_for_DerivedClass" If you like, you can also expand classproperty to allow setters and deleters like property does. Cheers, Ian From ian.g.kelly at gmail.com Tue Dec 6 13:21:39 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Dec 2011 11:21:39 -0700 Subject: Referring to the class name from a class variable where inheritance is involved In-Reply-To: References: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> Message-ID: On Tue, Dec 6, 2011 at 11:12 AM, Ian Kelly wrote: > If you like, you can also expand classproperty to allow setters and > deleters like property does. My mistake, you can't do this. __set__ and __delete__ are only invoked on instances, not on the class. Get-only class properties are fine, though. From python.list at tim.thechases.com Tue Dec 6 13:29:30 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 06 Dec 2011 12:29:30 -0600 Subject: Python-list Digest, Vol 99, Issue 28 In-Reply-To: References: Message-ID: <4EDE5F0A.90200@tim.thechases.com> 12/06/11 11:34, Sergi Pasoev wrote: (after trimming all the digest rubbish the way you should have done in the first place) >> Coincidentally, I was working on that exact function last >> night, so you inspired me to put it up on the Python >> cookbook. This version should work on any POSIX system such >> as Linux, as well as Windows. I don't know if it will work >> on Macintosh. >> >> http://code.activestate.com/recipes/577977-get-single-keypress/ > > Thanks Steven. Maybe you mean Gnu/Linux when you say Linux. Sheesh. The guy hands you free code to do what you ask, and 99% of your reply is uselessly-quoted digest, 0.8% is complaining about his terminology, and 0.1% is saying thanks. 1) it's the POSIX API in question here, nothing particularly GNU about the Linux implementation of it. 2) if you want to be pedantic about it, you should really be calling such systems something like Xorg/[Gnome|KDE|Fluxbox|LXDE|OpenBox|BlackBox|FVWM|XFCE]/Linux/GNU/Mozilla/TeX/MySQL/Postgresql/sqlite/git/CVS/Subversion/Bazaar/Mercurial/Python/Perl/PHP/Apache. And why don't you call it that? Because it's a pain, and if you say "Linux" everybody knows that you're talking about an operating system that uses Linux and has other contributions in it. So I'm pretty sure that Steven did *not* mean to type "GNU/Linux" or he would have typed it that way. :) -tkc From p.f.moore at gmail.com Tue Dec 6 13:53:03 2011 From: p.f.moore at gmail.com (Paul Moore) Date: Tue, 6 Dec 2011 18:53:03 +0000 Subject: Referring to the class name from a class variable where inheritance is involved In-Reply-To: References: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> Message-ID: On 6 December 2011 18:12, Ian Kelly wrote: > How about a class property? > > class classproperty(object): > ? ?def __init__(self, fget): > ? ? ? ?self.__fget = fget > ? ?def __get__(self, instance, owner): > ? ? ? ?return self.__fget(owner) Nice :-) About as heavyweight as a classmethod, though, so it doesn't buy me much over that (beyond avoiding a set of parens, which isn't a big deal). Either classmethod or classproperty gives me the key benefit I'm after, which is avoiding the per-instance overhead, though. TBH, this is a huge case of premature optimisation, though, so it's probably little more than an academic or style question. :-) Paul From p.f.moore at gmail.com Tue Dec 6 13:59:48 2011 From: p.f.moore at gmail.com (Paul Moore) Date: Tue, 6 Dec 2011 10:59:48 -0800 (PST) Subject: Referring to the class name from a class variable where inheritance is involved References: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> Message-ID: <5b66b2e5-1ec6-4804-8429-15be3b9c0197@i8g2000vbh.googlegroups.com> On Dec 6, 5:29?pm, Matt Saxton wrote: > You can use a metaclass for this: > > ?>>> class BaseMeta(type): > ... ? ? def __new__(mcs, name, bases, dict): > ... ? ? ? ? dict['key'] = 'Key_for_%s' % name > ... ? ? ? ? return type.__new__(mcs, name, bases, dict) > ... > ?>>> class Base: > ... ? ? __metaclass__ = BaseMeta > ... > ?>>> class Inherited(Base): > ... ? ? pass > ... > ?>>> Base.key > 'Key_for_Base' > ?>>> Inherited.key > 'Key_for_Inheritor' I like that! I tend to think of metaclasses as for "complicated" stuff. It's nice to see a simple use. Paul From ladasky at my-deja.com Tue Dec 6 14:13:19 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 6 Dec 2011 11:13:19 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? Message-ID: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> Hi, folks, Back in 2002, I got back into programming after a nine-year hiatus. I needed a new programming language, was guided to Python 2.2, and was off to the races. I chose the SciTE program editor, and I have been using it ever since. I'm now using Python 2.6 on Ubuntu Linux 10.10. My programming needs have grown more sophisticated, but I'm still using SciTE. Pretty much all of my recent posts to comp.lang.python have concerned multiprocessing. I put together a decent system for my current project, and had it all working. Then I realized that I needed to refactor and expand some code, which I did -- and somehow, I generated a bug that I simply cannot understand. I've been puzzling over it for three days. The error is occurring inside one of my subprocesses. As far as I know, SciTE is limited in what it can do in this situation. The program does not return when a subprocess generates an exception. I see the error message, but then the program simply hangs. I have tried invoking the subprocess directly without scheduling it through multiprocessing.Pool. It works fine. So the problem is occurring inside Pool. I tried opening my code in IDLE, and figured I could step through it, or at least call functions one line at a time. It appears that multiprocessing code is not compatible with IDLE. IDLE simply crashes when I try to invoke any of the important functions. I know, you want me to post a minimal example. Most of the time, that's possible, and I do it. Trust me, this time it isn't. I have about 500 lines of code, split across three files. These implement a neural network, some test data, and multiprocessing methods for network evaluation. I made several concerted changes to the code, and turned a working system into this: ============================================= Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner self.run() File "/usr/lib/python2.6/threading.py", line 484, in run self.__target(*self.__args, **self.__kwargs) File "/usr/lib/python2.6/multiprocessing/pool.py", line 225, in _handle_tasks put(task) TypeError: expected string or Unicode object, NoneType found ============================================= Here's what I think would help me debug this error: I would like to catch the TypeError, and examine the contents of task. I need to accomplish this WITHOUT adding a try...except block to the Python library file multiprocessing/pool.py. I don't know whether this is possible, because the traceback isn't clear about where my OWN code calls the code which is generating the error. After that, if the cause of the error still is not obvious, I might need to go back to the working program. Somehow I want to examine the contents of task when the program works, and no TypeError is being generated. By comparing the two, I hope to see a difference. From that, I should be able to figure out how I have broken what is being fed to Pool.__init__ and/or MapResult.__init__. Any suggestions how I might best accomplish this task? Does this error message look familiar to anyone? More generally, should I consider graduating from SciTE? I have had a look at a few of the more comprehensive IDE's over the years, and I'll have to say that I found them to be intimidating. I found it to be a huge chore just to open a single Python script and run it inside an IDE. It seems like you had to know how to set up a complete, multi- script project before you could even accomplish simple tasks. That steep learning curve is the reason that I didn't choose Java as my programming language. So, if any of you have pertinent recommendations in the IDE department, please feel free to guide me that way. Thanks! From ladasky at my-deja.com Tue Dec 6 14:19:55 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 6 Dec 2011 11:19:55 -0800 (PST) Subject: Clever hack or code abomination? References: Message-ID: On Dec 1, 12:21?am, Chris Angelico wrote: > On Thu, Dec 1, 2011 at 2:15 PM, Roy Smith wrote: > That's a self-contained piece of code.If I came upon it, I'd probably > copy and paste it to IDLE, see what it comes up with, and proceed from > there. +1. That was going to be my comment exactly. From ladasky at my-deja.com Tue Dec 6 14:27:29 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 6 Dec 2011 11:27:29 -0800 (PST) Subject: why is bytearray treated so inefficiently by pickle? References: <4ed24a33$0$6869$e4fe514c@news2.news.xs4all.nl> Message-ID: <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> On a related note, pickling of arrays of float64 objects, as generated by the numpy package for example, are wildly inefficient with memory. A half-million float64's requires about 4 megabytes, but the pickle file I generated from a numpy.ndarray of this size was 42 megabytes. I know that numpy has its own pickle protocol, and that it's supposed to help with this problem. Still, if this is a general problem with Python and pickling numbers, it might be worth solving it in the language itself. From irmen at -NOSPAM-xs4all.nl Tue Dec 6 14:44:02 2011 From: irmen at -NOSPAM-xs4all.nl (Irmen de Jong) Date: Tue, 06 Dec 2011 20:44:02 +0100 Subject: why is bytearray treated so inefficiently by pickle? In-Reply-To: <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> References: <4ed24a33$0$6869$e4fe514c@news2.news.xs4all.nl> <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> Message-ID: <4ede7082$0$6925$e4fe514c@news2.news.xs4all.nl> On 06-12-11 20:27, John Ladasky wrote: > On a related note, pickling of arrays of float64 objects, as generated > by the numpy package for example, are wildly inefficient with memory. > A half-million float64's requires about 4 megabytes, but the pickle > file I generated from a numpy.ndarray of this size was 42 megabytes. > > I know that numpy has its own pickle protocol, and that it's supposed > to help with this problem. Still, if this is a general problem with > Python and pickling numbers, it might be worth solving it in the > language itself. Python provides ample ways for custom types to influence the way they're pickled (getstate/setstate, reduce). Are numpy's arrays are pickled similar to Python's own array types? In that case, when using Python 2.x, they're pickled very inefficiently indeed (every element is encoded with its own token). In Python 3.x, array pickling is very efficient because it stores the machine type representation in the pickle. Irmen From robert.kern at gmail.com Tue Dec 6 15:25:02 2011 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 06 Dec 2011 20:25:02 +0000 Subject: why is bytearray treated so inefficiently by pickle? In-Reply-To: <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> References: <4ed24a33$0$6869$e4fe514c@news2.news.xs4all.nl> <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> Message-ID: On 12/6/11 7:27 PM, John Ladasky wrote: > On a related note, pickling of arrays of float64 objects, as generated > by the numpy package for example, are wildly inefficient with memory. > A half-million float64's requires about 4 megabytes, but the pickle > file I generated from a numpy.ndarray of this size was 42 megabytes. > > I know that numpy has its own pickle protocol, and that it's supposed > to help with this problem. Still, if this is a general problem with > Python and pickling numbers, it might be worth solving it in the > language itself. It is. Use protocol=HIGHEST_PROTOCOL when dumping the array to a pickle. [~] |1> big = np.linspace(0.0, 1.0, 500000) [~] |2> import cPickle [~] |3> len(cPickle.dumps(big)) 11102362 [~] |4> len(cPickle.dumps(big, protocol=cPickle.HIGHEST_PROTOCOL)) 4000135 The original conception for pickle was that it would have an ASCII representation for optimal cross-platform compatibility. These were the days when people still used FTP regularly, and you could easily (and silently!) screw up binary data if you sent it in ASCII mode by accident. This necessarily creates large files for numpy arrays. Further iterations on the pickling protocol let numpy use raw binary data in the pickle. However, for backwards compatibility, the default protocol is the one Python started out with. If you explicitly use the most recent protocol, then you will get the efficiency benefits. -- 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 From nawijn at gmail.com Tue Dec 6 15:49:46 2011 From: nawijn at gmail.com (Marco Nawijn) Date: Tue, 6 Dec 2011 12:49:46 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> Message-ID: <1b100b62-9e1c-46b0-a453-9d73c1104c31@y12g2000vba.googlegroups.com> On Dec 6, 8:13?pm, John Ladasky wrote: > Hi, folks, > > Back in 2002, I got back into programming after a nine-year hiatus. ?I > needed a new programming language, was guided to Python 2.2, and was > off to the races. ?I chose the SciTE program editor, and I have been > using it ever since. ?I'm now using Python 2.6 on Ubuntu Linux 10.10. > > My programming needs have grown more sophisticated, but I'm still > using SciTE. ?Pretty much all of my recent posts to comp.lang.python > have concerned multiprocessing. ?I put together a decent system for my > current project, and had it all working. ?Then I realized that I > needed to refactor and expand some code, which I did -- and somehow, I > generated a bug that I simply cannot understand. ?I've been puzzling > over it for three days. > > The error is occurring inside one of my subprocesses. ?As far as I > know, SciTE is limited in what it can do in this situation. ?The > program does not return when a subprocess generates an exception. ?I > see the error message, but then the program simply hangs. > > I have tried invoking the subprocess directly without scheduling it > through multiprocessing.Pool. ?It works fine. ?So the problem is > occurring inside Pool. > > I tried opening my code in IDLE, and figured I could step through it, > or at least call functions one line at a time. ?It appears that > multiprocessing code is not compatible with IDLE. ?IDLE simply crashes > when I try to invoke any of the important functions. > > I know, you want me to post a minimal example. ?Most of the time, > that's possible, and I do it. ?Trust me, this time it isn't. ?I have > about 500 lines of code, split across three files. ?These implement a > neural network, some test data, and multiprocessing methods for > network evaluation. ?I made several concerted changes to the code, and > turned a working system into this: > > ============================================= > > Exception in thread Thread-1: > Traceback (most recent call last): > ? File "/usr/lib/python2.6/threading.py", line 532, in > __bootstrap_inner > ? ? self.run() > ? File "/usr/lib/python2.6/threading.py", line 484, in run > ? ? self.__target(*self.__args, **self.__kwargs) > ? File "/usr/lib/python2.6/multiprocessing/pool.py", line 225, in > _handle_tasks > ? ? put(task) > TypeError: expected string or Unicode object, NoneType found > > ============================================= > > Here's what I think would help me debug this error: I would like to > catch the TypeError, and examine the contents of task. ?I need to > accomplish this WITHOUT adding a try...except block to the Python > library file multiprocessing/pool.py. ?I don't know whether this is > possible, because the traceback isn't clear about where my OWN code > calls the code which is generating the error. > > After that, if the cause of the error still is not obvious, I might > need to go back to the working program. ?Somehow I want to examine the > contents of task when the program works, and no TypeError is being > generated. ?By comparing the two, I hope to see a difference. ?From > that, I should be able to figure out how I have broken what is being > fed to Pool.__init__ and/or MapResult.__init__. > > Any suggestions how I might best accomplish this task? ?Does this > error message look familiar to anyone? > > More generally, should I consider graduating from SciTE? ?I have had a > look at a few of the more comprehensive IDE's over the years, and I'll > have to say that I found them to be intimidating. ?I found it to be a > huge chore just to open a single Python script and run it inside an > IDE. ?It seems like you had to know how to set up a complete, multi- > script project before you could even accomplish simple tasks. ?That > steep learning curve is the reason that I didn't choose Java as my > programming language. > > So, if any of you have pertinent recommendations in the IDE > department, please feel free to guide me that way. > > Thanks! Hello John, One way of trying to debug the issue could be to use ipython and ipdb. You cadn than run your code from within the ipython shell. The debugger will hold at the type error, but keep the context. At this point you should be able to evaluate task. As a side comment to your IDE remarks. I keep switching between VIM and Aptana/Pydev. The more I learn about VIM the more I feel comfortable and productive. In combination with ipython it is quite a solid development environment. On the other hand Pydev is very user friendly, powerfull and easy to learn. Debugging in Pydev is excellent. Regards, Marco From tjreedy at udel.edu Tue Dec 6 16:42:26 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Dec 2011 16:42:26 -0500 Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? In-Reply-To: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> Message-ID: On 12/6/2011 2:13 PM, John Ladasky wrote: > Exception in thread Thread-1: > Traceback (most recent call last): > File "/usr/lib/python2.6/threading.py", line 532, in > __bootstrap_inner > self.run() > File "/usr/lib/python2.6/threading.py", line 484, in run > self.__target(*self.__args, **self.__kwargs) > File "/usr/lib/python2.6/multiprocessing/pool.py", line 225, in > _handle_tasks > put(task) > TypeError: expected string or Unicode object, NoneType found > > ============================================= > > Here's what I think would help me debug this error: I would like to > catch the TypeError, and examine the contents of task. The traceback says that it is None, which has no contents ;=). > I need to > accomplish this WITHOUT adding a try...except block to the Python > library file multiprocessing/pool.py. I do not understand this statement. You should feel free to make a backup copy of pool.py and then modify it for debugging. > I don't know whether this is > possible, because the traceback isn't clear about where my OWN code > calls the code which is generating the error. It appears to be threading trying to call your code, and failing, that is the problem. But I do not know how threading, multiprocessing.pool, and your code are supposed to interact. > After that, if the cause of the error still is not obvious, I might > need to go back to the working program. Somehow I want to examine the > contents of task when the program works, and no TypeError is being > generated. The traceback says that it is a string. I would start with the line that fails 'put(task)', and work backwards to see where 'task' comes from and how it could become None. It is even possible that multiprocessing.pool has a bug that you ran into. -- Terry Jan Reedy From tjreedy at udel.edu Tue Dec 6 17:11:45 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Dec 2011 17:11:45 -0500 Subject: the deceptive continuous assignments In-Reply-To: References: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> Message-ID: On 12/6/2011 7:33 AM, Chris Angelico wrote: > On Tue, Dec 6, 2011 at 11:20 PM, Terry Reedy wrote: >> You found an unsafe overlap. >> x.thing = x = 1 >> would work, though it seems strange (and unlikely in practice) to rebind x >> to an int after it is bound to a class k instance. > > This code is starting to look like it wants to work with a linked list. > >>>> class node: > def __init__(self,x): > self.payload=x > self.next=None > def walk(self): > print("My payload is: "+self.payload) > if self.next: self.next.walk() > >>>> head=tail=node("This") >>>> tail.next=tail=node("is") >>>> tail.next=tail=node("a") >>>> tail.next=tail=node("test.") >>>> head.walk() > My payload is: This > My payload is: is > My payload is: a > My payload is: test. Cute. I am used to linked lists being built from the bottem up in functional languages with immutable nodes. I might even use something like this. Of course, for a list of any length, walk needs to be iterative. def walk(self): while self: print("My payload is: "+self.payload) self = self.next -- Terry Jan Reedy From rosuav at gmail.com Tue Dec 6 17:20:50 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Dec 2011 09:20:50 +1100 Subject: the deceptive continuous assignments In-Reply-To: References: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> Message-ID: On Wed, Dec 7, 2011 at 9:11 AM, Terry Reedy wrote: > Cute. I am used to linked lists being built from the bottem up in functional > languages with immutable nodes. I might even use something like this. Of > course, for a list of any length, walk needs to be iterative. > > ? ?def walk(self): > ? ? ? ?while self: > > ? ? ? ? ? ?print("My payload is: "+self.payload) > ? ? ? ? ? ?self = self.next Of course, but I was coding top-down and didn't feel like going back to add the "while" :) ChrisA From tjreedy at udel.edu Tue Dec 6 17:24:01 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Dec 2011 17:24:01 -0500 Subject: why is bytearray treated so inefficiently by pickle? In-Reply-To: <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> References: <4ed24a33$0$6869$e4fe514c@news2.news.xs4all.nl> <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> Message-ID: On Nov 30, Irmen de Jong opened a tracker issue with a patch improve bytearray pickling. http://bugs.python.org/issue13503 Yesterday, Dec 5, Antoine Pitrou applied a revised fix. http://hg.python.org/cpython/rev/e2959a6a1440/ The commit message: "Issue #13503: Use a more efficient reduction format for bytearrays with pickle protocol >= 3. The old reduction format is kept with older protocols in order to allow unpickling under Python 2." -- Terry Jan Reedy From ladasky at my-deja.com Tue Dec 6 19:17:23 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 6 Dec 2011 16:17:23 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1b100b62-9e1c-46b0-a453-9d73c1104c31@y12g2000vba.googlegroups.com> Message-ID: Thanks, Marco. I've noticed that the matplotlib reference manual recommends ipython. I haven't been clear what its advantages are, but if interacting with multiprocessing correctly is one of them, I'll try it. If ipython does everything that IDLE does and more, why is IDLE still shipped with Python anyway? I'll follow up on your IDE recommendations too after trying ipython. From anacrolix at gmail.com Tue Dec 6 19:26:54 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Wed, 7 Dec 2011 11:26:54 +1100 Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? In-Reply-To: References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1b100b62-9e1c-46b0-a453-9d73c1104c31@y12g2000vba.googlegroups.com> Message-ID: John I'm in a similar position. I've been using Geany for 2+ years and haven't found anything to replace it. Either the replacement tool makes it too difficult to work with Python correctly, or I spend more time trying to understand it, rather than getting the job done. I also use vim on occasion when GUI isn't an option. I seem to do okay, so I'm not sure you're at any disadvantage. A stand alone graphical debugger would be handy tho... On Wed, Dec 7, 2011 at 11:17 AM, John Ladasky wrote: > Thanks, Marco. > > I've noticed that the matplotlib reference manual recommends ipython. > I haven't been clear what its advantages are, but if interacting with > multiprocessing correctly is one of them, I'll try it. > > If ipython does everything that IDLE does and more, why is IDLE still > shipped with Python anyway? > > I'll follow up on your IDE recommendations too after trying ipython. > -- > http://mail.python.org/mailman/listinfo/python-list -- ?_? From ladasky at my-deja.com Tue Dec 6 19:30:16 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 6 Dec 2011 16:30:16 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> Message-ID: <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> On Dec 6, 1:42?pm, Terry Reedy wrote: > On 12/6/2011 2:13 PM, John Ladasky wrote: > > Exception in thread Thread-1: > > Traceback (most recent call last): > > ? ?File "/usr/lib/python2.6/threading.py", line 532, in > > __bootstrap_inner > > ? ? ?self.run() > > ? ?File "/usr/lib/python2.6/threading.py", line 484, in run > > ? ? ?self.__target(*self.__args, **self.__kwargs) > > ? ?File "/usr/lib/python2.6/multiprocessing/pool.py", line 225, in > > _handle_tasks > > ? ? ?put(task) > > TypeError: expected string or Unicode object, NoneType found > > > ============================================= > > > Here's what I think would help me debug this error: I would like to > > catch the TypeError, and examine the contents of task. > > The traceback says that it is None, which has no contents ;=). I'm not sure about that. I don't submit the task variable, it's something that Pool builds from what I submit. Is task == None when it should be a string? Or, is task an iterable which contains one element which should be a string? And what's supposed to be in that string anyway? My first reading of the source code of Pool didn't make this clear to me. Also, I've noticed that tracebacks from subprocesses are less informative than tracebacks from the parent process. What's missing? Finally, I also recall that multiprocessing invokes pickle to pass data between processes (I still don't understand the need for pickle here). This suggests to me that the string MIGHT contain the pickled code of the method I want to run in the subprocess. But I'm not sure, until I can actually examine the task variable in my working version. > > I need to > > accomplish this WITHOUT adding a try...except block to the Python > > library file multiprocessing/pool.py. > > I do not understand this statement. You should feel free to make a > backup copy of pool.py and then modify it for debugging. Right, so, the last time I tried this with a piece of library code, I ran into some major headaches with import statements. I suppose I could have a look at Pool and see whether it can be extracted cleanly and made to run. > ?> I don't know whether this is > > > possible, because the traceback isn't clear about where my OWN code > > calls the code which is generating the error. > > It appears to be threading trying to call your code, and failing, that > is the problem. But I do not know how threading, multiprocessing.pool, > and your code are supposed to interact. It might be that pickle has somehow managed to pass a null code string to the subprocess, so it has nothing to run. > > After that, if the cause of the error still is not obvious, I might > > need to go back to the working program. ?Somehow I want to examine the > > contents of task when the program works, and no TypeError is being > > generated. > > The traceback says that it is a string. Yes, again... I want to know what that string is supposed to DO. > I would start with the line that fails 'put(task)', and work backwards > to see where 'task' comes from and how it could become None. It is even > possible that multiprocessing.pool has a bug that you ran into. Oh, please don't say that. I'm no computer scientist, and Python has been scrutinized by so many professionals. I couldn't have possibly found a language bug. From tjreedy at udel.edu Tue Dec 6 20:34:37 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Dec 2011 20:34:37 -0500 Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? In-Reply-To: <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> Message-ID: On 12/6/2011 7:30 PM, John Ladasky wrote: > On Dec 6, 1:42 pm, Terry Reedy wrote: >> On 12/6/2011 2:13 PM, John Ladasky wrote: >>> I need to >>> accomplish this WITHOUT adding a try...except block to the Python >>> library file multiprocessing/pool.py. >> >> I do not understand this statement. You should feel free to make a >> backup copy of pool.py and then modify it for debugging. > > Right, so, the last time I tried this with a piece of library code, I > ran into some major headaches with import statements. I suppose I > could have a look at Pool and see whether it can be extracted cleanly > and made to run. I have patched files both in /Lib and /Lib/idle on Windows with no problems except that I had to switch to admin account to make the patch. I probably changed 'copy of x.py' to either 'x.bak' or 'x.py.bak' but I do not remember. Deleting .pyc might or might not help. For a file in Lib, I have also copied to the working directory with my script, which gets prepended to sys.path. This makes restoring the default easier. One would have to copy all of multiprocessing/ for that to work with m.../pool.py. -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Tue Dec 6 22:44:19 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Dec 2011 03:44:19 GMT Subject: Python-list Digest, Vol 99, Issue 28 References: Message-ID: <4edee113$0$30003$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Dec 2011 22:04:36 +0430, Sergi Pasoev wrote: > Thanks Steven. Maybe you mean Gnu/Linux when you say Linux. Maybe I do, maybe I don't. Since you're replying to a digest and neither adjusted the subject line nor trimmed almost a dozen pages of unnecessary quoted text, I have no idea what you are referring to, or whether you are replying to me or some other Steven. -- Steven but perhaps not the right Steven From wuwei23 at gmail.com Tue Dec 6 22:52:23 2011 From: wuwei23 at gmail.com (alex23) Date: Tue, 6 Dec 2011 19:52:23 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: Rick Johnson wrote: > Anyone who would take the time to write *articulate* suggestions for > grave deficiencies in the Python documentation must care. And you're including yourself here by implication, yes? You have _zero_ desire to see any real improvements to Python and instead want to come here on a regular basis and show us how big your brain is. Like Xah, you're unable to grasp that programming is a _communal_ exercise. You both vilify _everyone_ who doesn't loudly proclaim your brilliance. You refuse to accept that there are _ways_ of approaching the community to encourage interaction: being a smug cunt is not one of them. Even worse, the _more_ people point out to you both how wrong you are, the _more_ convinced you're correct, because of a bullshit belief in your superiority. You are both astounding exemplars of the Dunning-Kruger effect. You're a _troll_. You contribute nothing of value, you hijack threads for your own agenda, you are _nothing but a source of noise_ to this list. The months in which you don't post are an absolute goddamn _delight_ and there would be no loss to anyone if you decided to just fuck off and take your exercise in public masturbation elsewhere. From steve+comp.lang.python at pearwood.info Tue Dec 6 23:03:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Dec 2011 04:03:16 GMT Subject: Hints for writing bit-twiddling code in Python Message-ID: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> I have some bit-twiddling code written in Java which I am trying to port to Python. I'm not getting the same results though, and I think the problem is due to differences between Java's signed byte/int/long types, and Python's unified long integer type. E.g. Java's >>> is not exactly the same as Python's >> operator, and a character coerced to a byte in Java is not the same as ord(char) in Python. (The Java byte is in the range -128...127, I think, while the ord in Python is in 0...255.) Can anyone point me to some good resources to help me port the Java code to Python? If it helps, the Java code includes bits like this: long newSeed = (seed & 0xFFFFFFFFL) * 0x41A7L; while (newSeed >= 0x80000000L) { newSeed = (newSeed & 0x7FFFFFFFL) + (newSeed >>> 31L); } seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed; which I've translated into: newseed = (seed & 0xFFFFFFFF)*0x41A7 while (newseed >= 0x80000000): newseed = (newseed & 0x7FFFFFFF) + (newseed >> 31) seed = 0 if newseed == 0x7FFFFFFF else newseed & 0xFFFFFFFF -- Steven From dihedral88888 at googlemail.com Wed Dec 7 00:24:42 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 6 Dec 2011 21:24:42 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: <22982249.140.1323235482591.JavaMail.geo-discussion-forums@prix23> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral > wrote: > >> Please explain what you think a hash function is, then. ?Per > >> Wikipedia, "A hash function is any algorithm or subroutine that maps > >> large data sets to smaller data sets, called keys." > >> > >> > Are you miss-leading the power of true OOP ? > >> > >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > > v can be a tuple or a list. ?There are some restrictions on k to be an > > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > > position, then something has to be done to resolve this. > > I understand how dicts / hash tables work. I don't need you to > explain that to me. What you haven't explained is why you stated that > a hash function that operates on objects is not a hash function, or > what you meant by "misleading the power of true OOP". Do you forget the memory management of a dictionary in Python that has to be linked for a dynamical growing and shrinking number of (k,v) pairs in a long period of time? Avoiding the true non-trivial part in any implementation or use of a dictionary is miss leading ? Probing too deep in the stack or occupying too much in the heap is not bug free? From dihedral88888 at googlemail.com Wed Dec 7 00:24:42 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 6 Dec 2011 21:24:42 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: <22982249.140.1323235482591.JavaMail.geo-discussion-forums@prix23> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral > wrote: > >> Please explain what you think a hash function is, then. ?Per > >> Wikipedia, "A hash function is any algorithm or subroutine that maps > >> large data sets to smaller data sets, called keys." > >> > >> > Are you miss-leading the power of true OOP ? > >> > >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > > v can be a tuple or a list. ?There are some restrictions on k to be an > > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > > position, then something has to be done to resolve this. > > I understand how dicts / hash tables work. I don't need you to > explain that to me. What you haven't explained is why you stated that > a hash function that operates on objects is not a hash function, or > what you meant by "misleading the power of true OOP". Do you forget the memory management of a dictionary in Python that has to be linked for a dynamical growing and shrinking number of (k,v) pairs in a long period of time? Avoiding the true non-trivial part in any implementation or use of a dictionary is miss leading ? Probing too deep in the stack or occupying too much in the heap is not bug free? From drsalists at gmail.com Wed Dec 7 01:08:34 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Tue, 6 Dec 2011 22:08:34 -0800 Subject: Hints for writing bit-twiddling code in Python In-Reply-To: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> References: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> Message-ID: On 12/6/11, Steven D'Aprano wrote: > I have some bit-twiddling code written in Java which I am trying to port > to Python.: > > long newSeed = (seed & 0xFFFFFFFFL) * 0x41A7L; > while (newSeed >= 0x80000000L) { > newSeed = (newSeed & 0x7FFFFFFFL) + (newSeed >>> 31L); > } > seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed; I suspect the problem lies somewhere other than the java and python code you posted. I'm having a bit of a time finding an input value of seed that gives two different results, despite throwing some hard-feeling test cases and lots of random values, using both a 32 bit and a 64 bit JVM. I believe java likes to treat strings like Python 3, but if you use "export LC_ALL=en_US.ISO-8859-1", then it'll behave a little more like Python 2 in that strings have an 8 bit encoding (or at least act like it) that's round-tripable. If worse comes to worse, you could probably write some test harness code for each of the java and python, and then feed them the same inputs - to see which pieces differ and which don't. From steve+comp.lang.python at pearwood.info Wed Dec 7 01:57:09 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Dec 2011 06:57:09 GMT Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> Message-ID: <4edf0e45$0$13933$c3e8da3$76491128@news.astraweb.com> On Tue, 06 Dec 2011 16:30:16 -0800, John Ladasky wrote: >> I would start with the line that fails 'put(task)', and work backwards >> to see where 'task' comes from and how it could become None. It is even >> possible that multiprocessing.pool has a bug that you ran into. > > Oh, please don't say that. I'm no computer scientist, and Python has > been scrutinized by so many professionals. I couldn't have possibly > found a language bug. "Professional" just means they get paid for doing it. Professionals gave us the 2008 banking collapse, the Challenger shuttle explosion, the sinking of the Titanic, trench warfare in World War I, the Chernobyl nuclear meltdown, leaded petrol, "Battlefield Earth" the movie, and the BP Gulf of Mexico oil spill. Amateurs gave us the discovery of electricity, the Roomba, the original Apple computer, GNU software, Linux, Ogg/Vorbis, and the discovery of continental drift. While your modesty is a welcome change from n00bs who imagine that anything about Python that they misunderstood is a bug, don't sell yourself short. You don't need to be a computer scientist to identify bugs in software. -- Steven From rosuav at gmail.com Wed Dec 7 02:12:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Dec 2011 18:12:41 +1100 Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? In-Reply-To: <4edf0e45$0$13933$c3e8da3$76491128@news.astraweb.com> References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> <4edf0e45$0$13933$c3e8da3$76491128@news.astraweb.com> Message-ID: On Wed, Dec 7, 2011 at 5:57 PM, Steven D'Aprano wrote: > On Tue, 06 Dec 2011 16:30:16 -0800, John Ladasky wrote: > >> Oh, please don't say that. ?I'm no computer scientist, and Python has >> been scrutinized by so many professionals. ?I couldn't have possibly >> found a language bug. > > While your modesty is a welcome change from n00bs who imagine that > anything about Python that they misunderstood is a bug, don't sell > yourself short. You don't need to be a computer scientist to identify > bugs in software. Likelihood of something being a bug generally depends heavily on "eyeball density". If a piece of code gets a lot of eyeballs, chances are its bugs have been found (not always but often). Generally, the code that makes up a heavily-used open source project can be expected to have quite a few eyeballs near it as a regular thing; but there's always the obscure bits that don't. Crypto modules have fallen foul of this on occasion, with bugs lurking there far more than might otherwise be expected, on account of such a small portion of coders ever touching cryptography. Of course, it's always less embarrassing to say "I think I'm using this wrong" and have someone say "You found a language bug" than to come in guns blazing with "ur lnguage is teh buggy" (sorry, I don't speak Lame very fluently) only to learn that you spoiled the incantation in some way. The number of people who assume that their first-time code is perfect and the language is hopeless is somewhat astonishing. ChrisA From gallium.arsenide at gmail.com Wed Dec 7 02:15:46 2011 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 6 Dec 2011 23:15:46 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> Message-ID: On Dec 6, 7:30?pm, John Ladasky wrote: > On Dec 6, 1:42?pm, Terry Reedy wrote: > > It is even possible that multiprocessing.pool has a bug > > that you ran into. > > Oh, please don't say that. ?I'm no computer scientist, and > Python has been scrutinized by so many professionals. ?I > couldn't have possibly found a language bug. Scrutiny or no, Python has its fair share of bugs. I think almost all real-world implementations of almost all general-purpose programming languages do. Not long ago a friend of mine (a mathematician, but only a novice programmer) sent me some code dealing with sets that exposed a bug in Python 2.6.1. He invoked the union() method from the set class rather than a set instance, and it took us a long time to figure out why he was getting different results than I was from the same code (I was the one on 2.6.1, and my results were wrong). Fortunately, it was (a) easy enough to work around and (b) fixed in subsequent versions of Python. But it just goes to show that even unsophisticated programmers can stumble upon language bugs. This one wasn't even in the library; it was a built-in. That hasn't shaken my confidence in Python, though. (Also, for what it's worth, I use SciTE as my Python editor as well. I've also used Geany from time to time, and I have no trouble recommending it. It's a step up the IDE ladder from SciTE, but is still tons lighter than Eclipse and its brethren.) John From irmen.NOSPAM at xs4all.nl Wed Dec 7 03:13:25 2011 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Wed, 07 Dec 2011 09:13:25 +0100 Subject: why is bytearray treated so inefficiently by pickle? In-Reply-To: References: <4ed24a33$0$6869$e4fe514c@news2.news.xs4all.nl> <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> Message-ID: <4edf2024$0$6957$e4fe514c@news2.news.xs4all.nl> On 6-12-2011 23:24, Terry Reedy wrote: > On Nov 30, Irmen de Jong opened a tracker issue with a patch improve bytearray pickling. > http://bugs.python.org/issue13503 > > Yesterday, Dec 5, Antoine Pitrou applied a revised fix. > http://hg.python.org/cpython/rev/e2959a6a1440/ > The commit message: > "Issue #13503: Use a more efficient reduction format for bytearrays with pickle protocol >>= 3. The old reduction format is kept with older protocols in order to allow unpickling > under Python 2." > Sure, but this patch only improved the pickle behavior of the bytearray type for protocol level 3. It didn't touch Python 2.x, nor the pickling of arrays (array.array), let alone numpy arrays. Irmen From __peter__ at web.de Wed Dec 7 03:21:55 2011 From: __peter__ at web.de (Peter Otten) Date: Wed, 07 Dec 2011 09:21:55 +0100 Subject: Hints for writing bit-twiddling code in Python References: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > I have some bit-twiddling code written in Java which I am trying to port > to Python. I'm not getting the same results though, and I think the > problem is due to differences between Java's signed byte/int/long types, > and Python's unified long integer type. E.g. Java's >>> is not exactly > the same as Python's >> operator, and a character coerced to a byte in > Java is not the same as ord(char) in Python. (The Java byte is in the > range -128...127, I think, while the ord in Python is in 0...255.) > > Can anyone point me to some good resources to help me port the Java code > to Python? > > If it helps, the Java code includes bits like this: > > long newSeed = (seed & 0xFFFFFFFFL) * 0x41A7L; > while (newSeed >= 0x80000000L) { > newSeed = (newSeed & 0x7FFFFFFFL) + (newSeed >>> 31L); > } > seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed; > > > which I've translated into: > > newseed = (seed & 0xFFFFFFFF)*0x41A7 > while (newseed >= 0x80000000): > newseed = (newseed & 0x7FFFFFFF) + (newseed >> 31) > seed = 0 if newseed == 0x7FFFFFFF else newseed & 0xFFFFFFFF I think you need to take negative ints into account. Try adding if seed & 0x80000000: # 2**31 seed -= 0x100000000 # 2**32, two's complement From matteo.boscolo at boscolini.eu Wed Dec 7 03:22:44 2011 From: matteo.boscolo at boscolini.eu (Matteo Boscolo) Date: Wed, 07 Dec 2011 09:22:44 +0100 Subject: Losing com pointer Message-ID: <4EDF2254.2030809@boscolini.eu> Hi all, I need some help to a com problem.. I got this class: class foo(object): def setComObject(comObject): self.comO=comObject #This is a com object from a cad application def showForm(self) # use the self.comO to read some information from the cad application # Show the pyqt form as child of cad application # do somthing with the form # do somthing with the self.comO <----- Here if pass some time I'm not able to call any method to the com object a=foo() o="get istance of a cad application via com" a.setComObject(o) a.showForm() #<---- here if pass some time I'm not able to call any method to the com object but I I' re call the a.setComObject(o) #Faster and I take less the 30 seconds on the form object it works well It seems a problem of the garbage collector .. but I'm not sure how to debug it .. any help is really appreciated.. it's the last dangerous bug in our application ... Regards, Matteo From storchaka at gmail.com Wed Dec 7 04:33:50 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Wed, 07 Dec 2011 11:33:50 +0200 Subject: Hints for writing bit-twiddling code in Python In-Reply-To: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> References: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> Message-ID: 07.12.11 06:03, Steven D'Aprano ???????(??): > long newSeed = (seed& 0xFFFFFFFFL) * 0x41A7L; > while (newSeed>= 0x80000000L) { > newSeed = (newSeed& 0x7FFFFFFFL) + (newSeed>>> 31L); > } > seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed; seed = (seed & 0xFFFFFFFF) * 0x41A7 % 0x7FFFFFFF From andrea.crotti.0 at gmail.com Wed Dec 7 05:14:41 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Wed, 07 Dec 2011 10:14:41 +0000 Subject: Questions about LISP and Python. In-Reply-To: <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: <4EDF3C91.3000605@gmail.com> On 12/06/2011 04:36 AM, Xah Lee wrote: > i don't like python, and i prefer emacs lisp. The primary reason is > that python is not functional, especially with python 3. The python > community is full of fanatics with their drivels. In that respect, > it's not unlike Common Lisp community and Scheme lisp community. > I love emacs and I love python, but saying that elisp is a better language than python and more functional is plain ridiculous. Elisp is great to extend Emacs, but with dynamic scoping and side effects used everywhere should not even be considered between the functional languages, at least for how it's mainly used. This is a typical usage taken from simple.el (defun pop-global-mark () "Pop off global mark ring and jump to the top location." (interactive) ;; Pop entries which refer to non-existent buffers. (while (and global-mark-ring (not (marker-buffer (car global-mark-ring)))) (setq global-mark-ring (cdr global-mark-ring))) (or global-mark-ring (error "No global mark set")) (let* ((marker (car global-mark-ring)) (buffer (marker-buffer marker)) (position (marker-position marker))) (setq global-mark-ring (nconc (cdr global-mark-ring) (list (car global-mark-ring)))) (set-buffer buffer) (or (and (>= position (point-min)) (<= position (point-max))) (if widen-automatically (widen) (error "Global mark position is outside accessible part of buffer"))) (goto-char position) (switch-to-buffer buffer))) Which means more or elss pop a mark from the mark ring, get the current buffer/position, then set the buffer, widen if necessary, go to some position and switch to that buffer. Nothing wrong with it, but basically it messes around with global state, as most elisp functions do. And how python 3 would be less functional? From tjreedy at udel.edu Wed Dec 7 06:17:51 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 07 Dec 2011 06:17:51 -0500 Subject: Questions about LISP and Python. In-Reply-To: <4EDF3C91.3000605@gmail.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <4EDF3C91.3000605@gmail.com> Message-ID: On 12/7/2011 5:14 AM, Andrea Crotti wrote: > On 12/06/2011 04:36 AM, Xah Lee wrote: >> i don't like python, and i prefer emacs lisp. The primary reason is >> that python is not functional, especially with python 3. The python >> community is full of fanatics with their drivels. In that respect, >> it's not unlike Common Lisp community and Scheme lisp community. >> > > I love emacs and I love python, but saying that elisp is a better language > than python and more functional is plain ridiculous. > Elisp is great to extend Emacs, but with dynamic scoping and side effects > used everywhere should not even be considered between the functional > languages, at least for how it's mainly used. > > This is a typical usage taken from simple.el > (defun pop-global-mark () > "Pop off global mark ring and jump to the top location." > (interactive) > ;; Pop entries which refer to non-existent buffers. > (while (and global-mark-ring (not (marker-buffer (car global-mark-ring)))) > (setq global-mark-ring (cdr global-mark-ring))) > (or global-mark-ring > (error "No global mark set")) > (let* ((marker (car global-mark-ring)) > (buffer (marker-buffer marker)) > (position (marker-position marker))) > (setq global-mark-ring (nconc (cdr global-mark-ring) > (list (car global-mark-ring)))) > (set-buffer buffer) > (or (and (>= position (point-min)) > (<= position (point-max))) > (if widen-automatically > (widen) > (error "Global mark position is outside accessible part of buffer"))) > (goto-char position) > (switch-to-buffer buffer))) > > Which means more or elss pop a mark from the mark ring, get the current > buffer/position, > then set the buffer, widen if necessary, go to some position and switch > to that buffer. > > Nothing wrong with it, but basically it messes around with global state, > as most elisp functions > do. > > And how python 3 would be less functional? It does not pretend that surrounding statements with parentheses turns them into expressions. ;-) -- Terry Jan Reedy From skip at pobox.com Wed Dec 7 06:30:27 2011 From: skip at pobox.com (Skip Montanaro) Date: Wed, 7 Dec 2011 11:30:27 +0000 (UTC) Subject: How to build 64-bit Python on Solaris with GCC? References: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> Message-ID: > Does anyone have a recipe for the subject build? I know Solaris is a minority platform these days, but surely someone has tackled this problem, haven't they? Thx, Skip From kliateni at gmail.com Wed Dec 7 07:47:11 2011 From: kliateni at gmail.com (Karim) Date: Wed, 07 Dec 2011 13:47:11 +0100 Subject: How to build 64-bit Python on Solaris with GCC? In-Reply-To: References: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> Message-ID: <4EDF604F.9070306@gmail.com> Le 07/12/2011 12:30, Skip Montanaro a ?crit : >> Does anyone have a recipe for the subject build? > I know Solaris is a minority platform these days, but surely someone has tackled > this problem, haven't they? > > Thx, > > Skip > > > ./configure make make install Karim From neilc at norwich.edu Wed Dec 7 08:16:15 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 7 Dec 2011 13:16:15 GMT Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <4EDF3C91.3000605@gmail.com> Message-ID: <9k978uFqo3U3@mid.individual.net> On 2011-12-07, Terry Reedy wrote: > It does not pretend that surrounding statements with > parentheses turns them into expressions. > ;-) I like being in a sexpression. Awwwwww YEAAAAH! -- (Neil Cerutti) From hniksic at xemacs.org Wed Dec 7 08:28:40 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Wed, 07 Dec 2011 14:28:40 +0100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> Message-ID: <8739cwbjef.fsf@xemacs.org> Chris Angelico writes: > 2011/12/5 Hrvoje Niksic : >> If a Python implementation tried to implement dict as a tree, >> instances of classes that define only __eq__ and __hash__ would not >> be correctly inserted in such a dict. > > Couldn't you just make a tree of hash values? Okay, that's probably > not the most useful way to do things, but technically it'd comply with > the spec. That's a neat idea. The leaves of the tree would contain a list of items with the same hash, but that's what you effectively get with a linear-probe hash table anyway. As you said, not immediately useful, but one could imagine the technique being of practical use when implementing Python or a Python-compatible language in a foreign environment that supports only tree-based collections. From massi_srb at msn.com Wed Dec 7 12:09:16 2011 From: massi_srb at msn.com (Massi) Date: Wed, 7 Dec 2011 09:09:16 -0800 (PST) Subject: Dynamic variable creation from string Message-ID: Hi everyone, in my script I have a dictionary whose items are couples in the form (string, integer values), say D = {'a':1, 'b':2, 'c':3} This dictionary is passed to a function as a parameter, e.g. : def Sum(D) : return D['a']+D['b']+D['c'] Is there a way to create three variables dynamically inside Sum in order to re write the function like this? def Sum(D) : # Here some magic to create a,b,c from D return a+b+c It is really important that the scope of a,b,c is limited to the Sum function, they must not exisit outside it or inside any other nested functions. Thanks in advance for your help! From gordon at panix.com Wed Dec 7 12:45:26 2011 From: gordon at panix.com (John Gordon) Date: Wed, 7 Dec 2011 17:45:26 +0000 (UTC) Subject: Dynamic variable creation from string References: Message-ID: In Massi writes: > in my script I have a dictionary whose items are couples in the form > (string, integer values), say > D = {'a':1, 'b':2, 'c':3} > This dictionary is passed to a function as a parameter, e.g. : > def Sum(D) : > return D['a']+D['b']+D['c'] > Is there a way to create three variables dynamically inside Sum in > order to re write the function like this? Do you want to sum all the values in D? If so, that's easy: def Sum(D): my_sum = 0 for item in D: my_sum += D[item] return my_sum -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From wm at localhost.localdomain Wed Dec 7 12:46:07 2011 From: wm at localhost.localdomain (Waldek M.) Date: Wed, 7 Dec 2011 18:46:07 +0100 Subject: Dynamic variable creation from string References: Message-ID: On Wed, 7 Dec 2011 09:09:16 -0800 (PST), Massi wrote: > def Sum(D) : > return D['a']+D['b']+D['c'] > > Is there a way to create three variables dynamically inside Sum in > order to re write the function like this? > > def Sum(D) : > # Here some magic to create a,b,c from D > return a+b+c Hello, > It is really important that the scope of a,b,c is limited to the Sum > function, they must not exisit outside it or inside any other nested > functions. > Thanks in advance for your help! Can you clarify a bit? I'm not sure why do you need to define any additional variables at all. You do not return variables from a function - you can return *a value* which may be a simple type or complex type. Or maybe you mean something like creating a global name from inside of a function? Well, that doesn't sound like a good idea, though... >>> def foo(): global a a = 5 >>> foo() >>> print("The variable is: ", a) The variable is: 5 >>> Best regards, Waldek From dihedral88888 at googlemail.com Wed Dec 7 12:48:11 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Wed, 7 Dec 2011 09:48:11 -0800 (PST) Subject: order independent hash? In-Reply-To: <8739cwbjef.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <8739cwbjef.fsf@xemacs.org> Message-ID: <14335292.152.1323280091159.JavaMail.geo-discussion-forums@prgg19> On Wednesday, December 7, 2011 9:28:40 PM UTC+8, Hrvoje Niksic wrote: > Chris Angelico writes: > > > 2011/12/5 Hrvoje Niksic : > >> If a Python implementation tried to implement dict as a tree, > >> instances of classes that define only __eq__ and __hash__ would not > >> be correctly inserted in such a dict. > > > > Couldn't you just make a tree of hash values? Okay, that's probably > > not the most useful way to do things, but technically it'd comply with > > the spec. > > That's a neat idea. The leaves of the tree would contain a list of > items with the same hash, but that's what you effectively get with a > linear-probe hash table anyway. > > As you said, not immediately useful, but one could imagine the technique > being of practical use when implementing Python or a Python-compatible > language in a foreign environment that supports only tree-based > collections. The heap as the root that could be divided like a tree of nodes to be used is funny. There are many ways to implement the heap manager in SW. From rosuav at gmail.com Wed Dec 7 12:52:11 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Dec 2011 04:52:11 +1100 Subject: Dynamic variable creation from string In-Reply-To: References: Message-ID: On Thu, Dec 8, 2011 at 4:09 AM, Massi wrote: > def Sum(D) : > # Here some magic to create a,b,c from D > return a+b+c Welcome to TMTOWTDI land! We do magic here... several different ways. You _may_ be able to do this, which is roughly equivalent to the extract() function in PHP: locals().update(D) However, this is NOT guaranteed to work. It's more likely to work with globals(), but that wouldn't restrict the scope the way you asked. One handy trick that I found on the internet while researching this: Use (or abuse!) function keyword parameters to do the dictionary extraction. You have to explicitly name your desired keys this way, but it may be suitable. (The function doesn't have to be defined inside the other, incidentally.) >>> def Sum(D): def inner_sum(a,b,c,**kwargs): # Real logic goes here. return a+b+c return inner_sum(**D) >>> a={"a":5,"b":10,"c":20} >>> Sum(a) 35 Alternatively, the exec() and eval() functions can be given dictionaries which will serve as their variable scopes, so you could possibly use that. Definitely looking like ugly code though. For something as trivial as Sum(), you'd do best to simply type D['a']+D['b']+D['c'] and be done with it. For something where you're going to use them a lot, probably easiest to be explicit: a,b,c = D['a'],D['b'],D['c'] However, this violates DRY principle, and risks hard-to-trace mismatch bugs. I'd be inclined to simply be explicit all the time. Depending on what D is, you may actually want to consider rewriting this as a class with a member function. class whatever: def Sum(self): return self.a+self.b+self.c There should be one obvious way to do it, says the zen of Python. Frankly, I'm not sure what that one obvious way is, here, although I'm pretty certain that several of the options posited would be very bad for your code! Still, down's very nice... They ARE alternative possibilities. ChrisA From python at mrabarnett.plus.com Wed Dec 7 13:07:47 2011 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 07 Dec 2011 18:07:47 +0000 Subject: Dynamic variable creation from string In-Reply-To: References: Message-ID: <4EDFAB73.7@mrabarnett.plus.com> On 07/12/2011 17:45, John Gordon wrote: > In Massi writes: > >> in my script I have a dictionary whose items are couples in the form >> (string, integer values), say > >> D = {'a':1, 'b':2, 'c':3} > >> This dictionary is passed to a function as a parameter, e.g. : > >> def Sum(D) : >> return D['a']+D['b']+D['c'] > >> Is there a way to create three variables dynamically inside Sum in >> order to re write the function like this? > > Do you want to sum all the values in D? If so, that's easy: > > def Sum(D): > my_sum = 0 > for item in D: > my_sum += D[item] > return my_sum > Or even: def Sum(D): return sum(D.values()) From mbadoiu at gmail.com Wed Dec 7 14:00:20 2011 From: mbadoiu at gmail.com (Mihai Badoiu) Date: Wed, 7 Dec 2011 14:00:20 -0500 Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Message-ID: ok, so the code is something like #process A p = Process(...) p.daemon = 1 p.start() # starts process B ... If process A dies (say error, or ctrl-c), or finishes, then process B also dies. But if process A is killed with the "kill" command, then process B soldiers on... Any idea on how to make process B die when process A gets killed by the "kill" command? thanks, --mihai On Sat, Dec 3, 2011 at 4:01 AM, Jack Keegan wrote: > I think the OP meant when the parent gets killed (by ctrl+c or similar), > not deleted. At least that's what I think when I think of a program being > killed. Is it even possible to send a signal in such a case? > > Cheers, > > Jack > > On Fri, Dec 2, 2011 at 4:27 PM, 88888 Dihedral < > dihedral88888 at googlemail.com> wrote: > >> Please check Erlang that spawn so easily. And there are Python packages >> can do the same task. >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > > > -- > The earth is a very small stage in a vast cosmic arena. Think of the > rivers of blood spilled by all those generals and emperors so that in glory > and in triumph they could become the momentary masters of a fraction of a > dot. > - Carl Sagan [Pale Blue Dot] > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From airween at gmail.com Wed Dec 7 14:41:12 2011 From: airween at gmail.com (=?utf-8?B?SGVnZWTDvHMs?= Ervin) Date: Wed, 7 Dec 2011 20:41:12 +0100 Subject: Insert trusted timestamp to PDF Message-ID: <20111207194110.GA2835@arxnet.hu> Hello Everyone, I'm looking for a tool, which can add a trusted timestamp to an existing PDF file (and can sign - but currently only have to add TS). Could anybody help? Thanks: a. From drsalists at gmail.com Wed Dec 7 14:50:01 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Wed, 7 Dec 2011 11:50:01 -0800 Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Message-ID: On 12/7/11, Mihai Badoiu wrote: > ok, so the code is something like > #process A > p = Process(...) > p.daemon = 1 > p.start() # starts process B > ... > > If process A dies (say error, or ctrl-c), or finishes, then process B also > dies. But if process A is killed with the "kill" command, then process B > soldiers on... > > Any idea on how to make process B die when process A gets killed by the > "kill" command? 1) If all you care about is SIGTERM, SIGHUP and the like (and specifically NOT SIGKILL), you could just install a signal handler that catches any catchable signals you're interested in. Then the signal either kills the children directly, or sets a flag that tells the main process to do some killing shortly. Note that threads and signal handlers don't mix very well - the combination tends to make the main thread immune to control-C, whether you want it to be or not. Also, signal handlers tend to complicate performing I/O, as you're more likely to read short blocks. 2) If you need to handle SIGKILL gracefully, and you have access to the code of the child process, you could make sure that the child isn't setting a SID (?). ssh, I believe, likes to start a new SID, making it immune to signals to the parent. Alternatively, you could add something to the child process' main loop that polls the parent, exiting if the parent no longer exists. 3) If you need to handle SIGKILL gracefully, and you don't have access to the code of the child process, you could use a single extra process that checks for the presense of the parent, and if it doesn't exist any more, then kill the children before exiting itself. From mbadoiu at gmail.com Wed Dec 7 14:53:47 2011 From: mbadoiu at gmail.com (Mihai Badoiu) Date: Wed, 7 Dec 2011 14:53:47 -0500 Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Message-ID: I like 2) the most. I do have access to the child. The child is a process started with multiprocessing.Process(function). How do I _not_ set an SID? thanks, --mihai On Wed, Dec 7, 2011 at 2:50 PM, Dan Stromberg wrote: > On 12/7/11, Mihai Badoiu wrote: > > ok, so the code is something like > > #process A > > p = Process(...) > > p.daemon = 1 > > p.start() # starts process B > > ... > > > > If process A dies (say error, or ctrl-c), or finishes, then process B > also > > dies. But if process A is killed with the "kill" command, then process B > > soldiers on... > > > > Any idea on how to make process B die when process A gets killed by the > > "kill" command? > > 1) If all you care about is SIGTERM, SIGHUP and the like (and > specifically NOT SIGKILL), you could just install a signal handler > that catches any catchable signals you're interested in. Then the > signal either kills the children directly, or sets a flag that tells > the main process to do some killing shortly. Note that threads and > signal handlers don't mix very well - the combination tends to make > the main thread immune to control-C, whether you want it to be or not. > Also, signal handlers tend to complicate performing I/O, as you're > more likely to read short blocks. > > 2) If you need to handle SIGKILL gracefully, and you have access to > the code of the child process, you could make sure that the child > isn't setting a SID (?). ssh, I believe, likes to start a new SID, > making it immune to signals to the parent. Alternatively, you could > add something to the child process' main loop that polls the parent, > exiting if the parent no longer exists. > > 3) If you need to handle SIGKILL gracefully, and you don't have access > to the code of the child process, you could use a single extra process > that checks for the presense of the parent, and if it doesn't exist > any more, then kill the children before exiting itself. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From irmen at -NOSPAM-xs4all.nl Wed Dec 7 14:59:11 2011 From: irmen at -NOSPAM-xs4all.nl (Irmen de Jong) Date: Wed, 07 Dec 2011 20:59:11 +0100 Subject: Insert trusted timestamp to PDF In-Reply-To: References: Message-ID: <4edfc58f$0$6850$e4fe514c@news2.news.xs4all.nl> On 07-12-11 20:41, Heged?s, Ervin wrote: > Hello Everyone, > > I'm looking for a tool, which can add a trusted timestamp to an > existing PDF file (and can sign - but currently only have to add > TS). Note sure what a 'trusted timestamp' is, but pdftk can manipulate pdf files. See http://www.pdflabs.com/docs/pdftk-man-page/ (nothing to do with Python btw!) Irmen From bv6bv6bv6 at hotmail.com Wed Dec 7 15:29:03 2011 From: bv6bv6bv6 at hotmail.com (bv) Date: Wed, 7 Dec 2011 12:29:03 -0800 (PST) Subject: A HOME OF TOLERANCE !!!!!!!!!!!!! Message-ID: <8f88c87a-1003-447b-be51-f98e080a6532@i6g2000vbe.googlegroups.com> A HOME OF TOLERANCE A Home Of Tolerance Media speculation since the horrific terrorist attacks on America has pointed the finger at Muslims and the Arab world, and that has meant ordinary citizens of the US and other Western countries becoming easy prey for anti-faith hooligans. Shame. Sadly, the latest horror to hit the US looks to have been caused by people of Middle Eastern origin, bearing Muslim names. Again, shame. This fuels more hatred for a religion and a people who have nothing to do with these events. This is why I want to explain some basic facts about this noble way we call Islam, before, God forbid, another disaster occurs - next time probably aimed at Muslims. I came to Islam in my late 20s, during my searching period as a wandering pop star. I found a religion that blended scientific reason with spiritual reality in a unifying faith far removed from the headlines of violence, destruction and terrorism. One of the first interesting things I learned in the Koran was that the name of the faith comes from the word salam - peace. Far from the kind of Turko-Arab-centric message I expected, the Koran presented a belief in the universal existence of God, one God for all. It does not discriminate against peoples; it says we may be different colors and from different tribes, but we are all human and "the best of people are the most God- conscious". Today, as a Muslim, I have been shattered by the horror of recent events; the display of death and indiscriminate killing we've all witnessed has dented humanity's confidence in itself. Terror on this scale affects everybody on this small planet, and no one is free from the fallout. Yet we should remember that such violence is almost an everyday occurrence in some Muslim lands: it should not be exacerbated by revenge attacks on more innocent families and communities. Along with most Muslims, I feel it a duty to make clear that such orchestrated acts of incomprehensible carnage have nothing to do with the beliefs of most Muslims. The Koran specifically declares: "If anyone murders an (innocent) person, it will be as if he has murdered the whole of humanity. And if anyone saves a person it will be as if he has saved the whole of humanity." The Koran that our young people learn is full of stories and lessons from the history of humanity as a whole. The Gospels and the Torah are referred to; Jesus and Abraham are mentioned. In fact there is more mention in the Koran of the prophet Moses than of any other. It acknowledges the coexistence of other faiths, and in doing so acknowledges that other cultures can live together in peace. "There is no compulsion in religion," it states, meaning that people should not be compelled to change their faith. Elsewhere it states, "To you, your religion; to me mine." Respect for religious values and justice is at the Koran's core. The Koranic history we teach our young provides ample examples of inter-religious and international relationships; of how to live together. But some extremists take elements of the sacred ******ures out of con****. They act as individuals, and when they can't come together as part of a political structure or consultative process, you find these dissident factions creating their own rules, contrary to the spirit of the Koran - which demands that those recognized as being in charge of Muslims must consult together regarding society's affairs. There is a whole chapter in the Koran entitled Consultation. Communal well being is central to human life, so there is a concept in Islam called Istihsan, which means "to look for the common good". Even though the Koran may lay down a diktat, scholars are also supposed to consider the circumstances prevalent at the time. Sometimes that means choosing the lesser of two evils or even suspending legislation if necessary: for instance, a person who steals bread during a famine is not treated as a thief. Once I wrote in a song, "Where do the children play?" Our sympathy and thoughts go out to the families of all those who lost their lives in this tragic act of violence, as well as all those injured. But life must go on. Children still need to play, and people need to live and learn more about their neighbors so that ignorance doesn't breed more blind fanaticism. Moderation is part of faith, so those who accuse Muslim schools of fostering fanaticism should learn a bit more about Islam. The Prophet (peace be upon him) said, "Ruined are those who insist on hardship in faith," and, "A believer remains within the scope of his religion as long as he doesn't kill another person illegally." Such knowledge and words of guidance are desperately needed at this time, to separate fact from falsehood, and to recognise the Last Prophet's own definition of that which makes a person representative, or otherwise, of the faith he lived and the one we try to teach. by Yusuf Islam (formerly the singer Cat Stevens) Published on Monday, September 24, 2001 by Al-Hewar Magazine. IF YOU WISH TO KNOW MORE ABOUT ISLAM, WE PREFER TO VISIT THE FOLLOWING WEBSITES: http://www.islam-guide.com http://www.islamhouse.com/s/9661 http://www.thisistruth.org http://www.quran-m.com/firas/en1 http://kaheel7.com/eng http://www.knowmuhammad.com http://www.rasoulallah.net/v2/index.aspx?lang=e http://imanway1.com/eng http://www.todayislam.com http://www.discoverislam.com http://www.thetruereligion.org http://www.beconvinced.com http://islamtomorrow.com http://www.usc.edu/dept/MSA/quran http://www.quranforall.org http://www.quranexplorer.com/quran http://www.prophetmuhammed.org http://www.chatislamonline.org/ar http://www.dar-us-salam.com http://youtubeislam.com From matteo.boscolo at boscolini.eu Wed Dec 7 15:57:07 2011 From: matteo.boscolo at boscolini.eu (Matteo Boscolo) Date: Wed, 07 Dec 2011 21:57:07 +0100 Subject: Insert trusted timestamp to PDF In-Reply-To: <20111207194110.GA2835@arxnet.hu> References: <20111207194110.GA2835@arxnet.hu> Message-ID: <4EDFD323.1070003@boscolini.eu> have a look at: http://www.boscolini.eu/Boscolini/index.php?option=com_content&view=article&id=64%3Anumbering-pdf-file-in-python&catid=38%3Aprogramming&Itemid=55&lang=en here I number some pdf files .. regards, Matteo Il 07/12/2011 20:41, Heged?s, Ervin ha scritto: > Hello Everyone, > > I'm looking for a tool, which can add a trusted timestamp to an > existing PDF file (and can sign - but currently only have to add > TS). > > Could anybody help? > > > Thanks: > > > a. > From tjreedy at udel.edu Wed Dec 7 17:59:33 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 07 Dec 2011 17:59:33 -0500 Subject: Dynamic variable creation from string In-Reply-To: References: Message-ID: On 12/7/2011 12:09 PM, Massi wrote: > in my script I have a dictionary whose items are couples in the form > (string, integer values), say > > D = {'a':1, 'b':2, 'c':3} > > This dictionary is passed to a function as a parameter, e.g. : > > def Sum(D) : > return D['a']+D['b']+D['c'] > > Is there a way to create three variables dynamically inside Sum in > order to re write the function like this? > > def Sum(D) : > # Here some magic to create a,b,c from D > return a+b+c No. The set of local names for a function is determined when the definition is executed and the body is compiled. Python 2 had an exception -- 'from x import *' -- that required an alternate complilation pathway. That possibility was eliminated in Python 3 and is now a syntax error. Within functions, 'locals().update(D)' is more or less guaranteed to *not* add local names to the local namespace, even if it does add keys to the locals() dict that shadows the local namespace. > It is really important that the scope of a,b,c is limited to the Sum > function, they must not exisit outside it or inside any other nested > functions. Local names, by definition, are lexically scoped to the function definition and are not visible without. Since nested definitions are part of that lexical scope, local names are always visible within nested definitions. You cannot stop that. The association of local names is usually dynamically limited to one function call. The two exceptions are enclosure by a nested function that survives the function call and generators in a paused state. -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Wed Dec 7 19:03:36 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 Dec 2011 00:03:36 GMT Subject: Dynamic variable creation from string References: Message-ID: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: > Is there a way to create three variables dynamically inside Sum in order > to re write the function like this? > > def Sum(D) : > # Here some magic to create a,b,c from D > return a+b+c No magic is needed. a, b, c = D['a'], D['b'], D['c'] However, there is no way to create variables from an *arbitrary* set of keys. And a good thing too, because how could you use them? def Sum(D): # Magic happens here return a + b + z + foo + spam + fred + ... ??? Since you don't know what variable names will be created, you don't know which variables you need to add. Dynamic creation of variables is ALMOST ALWAYS the wrong approach. In this specific case, what you should do to add up an arbitrary number of values from a dict is: sum(D.values()) > It is really important that the scope of a,b,c is limited to the Sum > function, they must not exisit outside it or inside any other nested > functions. The first part is trivially easy; since you are assigning to local variables, by definition they will be local to the Sum function and will not exist outside it. The second part is impossible, because that is not how Python works. Nested functions in Python can always see variables in their enclosing scope. If you don't want that behaviour, use another language, or don't use nested functions. -- Steven From tjreedy at udel.edu Wed Dec 7 19:27:43 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 07 Dec 2011 19:27:43 -0500 Subject: Dynamic variable creation from string In-Reply-To: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/7/2011 7:03 PM, Steven D'Aprano wrote: > On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: > >> Is there a way to create three variables dynamically inside Sum in order >> to re write the function like this? I should have mentioned in my earlier response that 'variable' is a bit vague and misleading. Python has names bound to objects. >> def Sum(D) : >> # Here some magic to create a,b,c from D >> return a+b+c > > No magic is needed. > > a, b, c = D['a'], D['b'], D['c'] This is not what most people mean by 'dynamically created variables'. The names are static, in the code, before the code is executed. In 2.x, 'from x import *' dynamically creates local names that are not in the code that contains the import. Dynamically creating objects is what Python code does all the time. -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Wed Dec 7 20:59:23 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 Dec 2011 01:59:23 GMT Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ee019fa$0$13933$c3e8da3$76491128@news.astraweb.com> On Wed, 07 Dec 2011 19:27:43 -0500, Terry Reedy wrote: > On 12/7/2011 7:03 PM, Steven D'Aprano wrote: >> On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: >> >>> Is there a way to create three variables dynamically inside Sum in >>> order to re write the function like this? > > I should have mentioned in my earlier response that 'variable' is a bit > vague and misleading. Python has names bound to objects. > >>> def Sum(D) : >>> # Here some magic to create a,b,c from D return a+b+c >> >> No magic is needed. >> >> a, b, c = D['a'], D['b'], D['c'] > > This is not what most people mean by 'dynamically created variables'. I know that. I'm just pointing out that the OP can solve his *stated* problem of creating a *fixed* number of variables with *known* names without any magic. I went on to discuss the case of an unknown number of unknown names, and suggested that the OP not do that, because it is much less useful than people think. > The names are static, in the code, before the code is executed. In 2.x, > 'from x import *' dynamically creates local names that are not in the > code that contains the import. Dynamically creating objects is what > Python code does all the time. Of course. And even dynamically creating names: names don't exist until they are created at runtime. Name deletion is also possible. But what isn't normally done is "dynamic variable creation" in the sense of creating arbitrary variables (names bound to objects) based on names known only at runtime. That's extremely rare, and for good reason. -- Steven From sturdyworks at yahoo.com Wed Dec 7 21:21:54 2011 From: sturdyworks at yahoo.com (B.A.S.) Date: Wed, 7 Dec 2011 18:21:54 -0800 (PST) Subject: How to get Python make to detect /usr/local/ssl Message-ID: <1323310914.67792.YahooMailClassic@web180009.mail.gq1.yahoo.com> Any Python developers out there that help me? I would like to configure/make Python so it uses my local OpenSSL-1.0.0e install in /usr/local/ssl. I have tried uncommenting the promising section of ./Python 2.7.2/Modules/Setup.dist below without success. How to do it! Would like to upgrade SQLite as well... ------ snip of Setup.dist ------------ # Socket module helper for socket(2) _socket socketmodule.c # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto Brad From rosuav at gmail.com Wed Dec 7 22:13:19 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Dec 2011 14:13:19 +1100 Subject: Dynamic variable creation from string In-Reply-To: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 8, 2011 at 11:03 AM, Steven D'Aprano wrote: >> It is really important that the scope of a,b,c is limited to the Sum >> function, they must not exisit outside it or inside any other nested >> functions. > > The second part is impossible, because that is not how Python works. > Nested functions in Python can always see variables in their enclosing > scope. If you don't want that behaviour, use another language, or don't > use nested functions. To the OP: By "nested functions", did you mean actual nested functions (those defined inside this function), or simply functions called from this one? This is a nested function, as the term is usually taken to mean: def outer_function(): a = 1 def inner_function(): b = 2 return a+b print(inner_function()) # Prints 3 The inner function has access to all of the outer function's namespace. But if you meant this: def function_1(): b = 2 return a+b def function_2(): a = 1 print(function_1()) then it's quite the other way around - Python never shares variables in this way (this would have function_1 assume that 'a' is a global name, so if it's not, you'll get a run-time NameError). As Steven says, there's no way in Python to hide variables from an actual nested function. But if you just mean a function called from this one, then what you want is the normal behaviour (and if you actually want to share variables, you pass them as arguments). ChrisA From rantingrickjohnson at gmail.com Wed Dec 7 23:10:22 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Wed, 7 Dec 2011 20:10:22 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: On Dec 6, 10:58?am, Ian Kelly wrote: (snip... beautiful retort) Ian you make some damn good points and i could not help but laugh hysterically at your dissection of Xah's tutorial. I don't 100% agree with everything Mr. Lee rants about, like for instance, documents needing to hyper link every possible sub-subject in every possible subject -- we'd end up with a spaghetti mess of links. Besides, students should follow a "linear" learning curve and not haphazardly jump from subject to subject. However, that being said, what does it prove when you can dissect someones rant with elegant irony? It simply means that no ONE person can write great documentation. They say... "it takes a village to raise a child"... Hmm, i believe it takes a community to create good docs. Q: Do we have a "community" here? Hmm, more on that later... I truly believe that Mr. Lee wants to make Python a better language, and many other languages also! However he has experienced so much resistance to his comments that he has (like many among us) developed what Psychologists call "learned helplessness". Xah: "I found a problem" Communty: "Well write a freaking patch or shut the hell up! Xah: "Why bother, you will just throw it in the trash to spite me no matter how good it is." Commuinty: "You're lazy, you just want to complain" Xah: "But, that's all you will allow me to do!" Who's fault is it that Xah is not a productive part of this community? Hmm? Do we just take the easy way out an blame Xah? Or do we need to observe our "collective" attitudes to perceived "outsiders"? I believe this community has a cancer. A cancer that is rotting us from the inside. A cancer that has metastasis and is spreading like wild fire. *Inquisitive Joe asked:* What is the source of this cancer Rick? The source is a direct result of insufficient leadership. Our current leader has failed us. Maybe he never wanted to be a leader, but when you go and declare yourself a "benevolent dictator for life" you'd damn well better act like one! Why has GvR not admonished the atrocious behavior of some people in this community? Why has GvR not admitted publicly the hideous state of IDLE and Tkinter? Where is the rally call? Where is the community spirit? The future of Pythin is in your hands Mr. Van Rossum. Will you step up and do what needs to be done? Will you have the courage? i sincerely hope so. If not, please step down and allow someone to lead. I await the supreme commanders ascent to power. From rantingrickjohnson at gmail.com Wed Dec 7 23:14:05 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Wed, 7 Dec 2011 20:14:05 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: <16dd31a4-da6d-4e7d-a2fd-38874253bd1b@32g2000yqp.googlegroups.com> On Dec 6, 9:52?pm, alex23 wrote: > (snip rambling nonsense) Alex, i hope you are being theatrical with all this. If not, i fear you may be putting too much stress on your heart. Please calm down. > The months in which you don't post are an absolute goddamn > _delight_ I am working on Python4000 Alex. *Someone* has to re-ignite Guido's original vision. From skippy.hammond at gmail.com Wed Dec 7 23:46:59 2011 From: skippy.hammond at gmail.com (Mark Hammond) Date: Thu, 08 Dec 2011 15:46:59 +1100 Subject: Losing com pointer In-Reply-To: <4EDF2254.2030809@boscolini.eu> References: <4EDF2254.2030809@boscolini.eu> Message-ID: <4EE04143.7060001@gmail.com> On 7/12/2011 7:22 PM, Matteo Boscolo wrote: > Hi all, > I need some help to a com problem.. > > I got this class: > > class foo(object): > def setComObject(comObject): > self.comO=comObject #This is a com object from a cad application > > def showForm(self) > # use the self.comO to read some information from the cad application > # Show the pyqt form as child of cad application > # do somthing with the form > # do somthing with the self.comO <----- Here if pass some time I'm not > able to call any method to the com object What are the symptoms of that? ie, what error do you get? I suspect the remote process is going away - it almost certainly has nothing to do with Python itself deciding to make the object go away. Mark > > a=foo() > o="get istance of a cad application via com" > a.setComObject(o) > a.showForm() #<---- here if pass some time I'm not able to call any > method to the com object > > but I I' re call the > a.setComObject(o) #Faster and I take less the 30 seconds on the form > object it works well > > It seems a problem of the garbage collector .. but I'm not sure how to > debug it .. > > any help is really appreciated.. it's the last dangerous bug in our > application ... > > Regards, > Matteo From airween at gmail.com Thu Dec 8 00:09:21 2011 From: airween at gmail.com (=?utf-8?B?SGVnZWTDvHMs?= Ervin) Date: Thu, 8 Dec 2011 06:09:21 +0100 Subject: Insert trusted timestamp to PDF In-Reply-To: <4edfc58f$0$6850$e4fe514c@news2.news.xs4all.nl> References: <4edfc58f$0$6850$e4fe514c@news2.news.xs4all.nl> Message-ID: <20111208050920.GB2186@arxnet.hu> Hello Irmen, On Wed, Dec 07, 2011 at 08:59:11PM +0100, Irmen de Jong wrote: > On 07-12-11 20:41, Heged?s, Ervin wrote: > >Hello Everyone, > > > >I'm looking for a tool, which can add a trusted timestamp to an > >existing PDF file (and can sign - but currently only have to add > >TS). > > Note sure what a 'trusted timestamp' is, that's very important to understand what does it mean - otherwise it's just groping :) http://tools.ietf.org/html/rfc3161 http://en.wikipedia.org/wiki/Trusted_timestamp > but pdftk can manipulate > pdf files. See http://www.pdflabs.com/docs/pdftk-man-page/ > (nothing to do with Python btw!) looks like it couldn't, but I'll contact them. thanks: a. From alec.taylor6 at gmail.com Thu Dec 8 00:28:01 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Thu, 8 Dec 2011 16:28:01 +1100 Subject: Insert trusted timestamp to PDF In-Reply-To: <20111208050920.GB2186@arxnet.hu> References: <4edfc58f$0$6850$e4fe514c@news2.news.xs4all.nl> <20111208050920.GB2186@arxnet.hu> Message-ID: Just digitally sign the document using python-gnupg /problem-solved! On Thu, Dec 8, 2011 at 4:09 PM, Heged?s, wrote: > Hello Irmen, > > On Wed, Dec 07, 2011 at 08:59:11PM +0100, Irmen de Jong wrote: >> On 07-12-11 20:41, Heged?s, Ervin wrote: >> >Hello Everyone, >> > >> >I'm looking for a tool, which can add a trusted timestamp to an >> >existing PDF file (and can sign - but currently only have to add >> >TS). >> >> Note sure what a 'trusted timestamp' is, > that's very important to understand what does it mean - > otherwise it's just groping :) > > http://tools.ietf.org/html/rfc3161 > http://en.wikipedia.org/wiki/Trusted_timestamp > >> but pdftk can manipulate >> pdf files. See http://www.pdflabs.com/docs/pdftk-man-page/ >> (nothing to do with Python btw!) > > looks like it couldn't, but I'll contact them. > > > thanks: > > > a. > > -- > http://mail.python.org/mailman/listinfo/python-list From airween at gmail.com Thu Dec 8 02:06:16 2011 From: airween at gmail.com (=?utf-8?B?SGVnZWTDvHMs?= Ervin) Date: Thu, 8 Dec 2011 08:06:16 +0100 Subject: Insert trusted timestamp to PDF In-Reply-To: References: <4edfc58f$0$6850$e4fe514c@news2.news.xs4all.nl> <20111208050920.GB2186@arxnet.hu> Message-ID: <20111208070614.GA2572@arxnet.hu> Hello, On Thu, Dec 08, 2011 at 04:28:01PM +1100, Alec Taylor wrote: > Just digitally sign the document using python-gnupg > > /problem-solved! using gnupg to sign a document != add a timestamp to a pdf. May be this doc helps to clear what's the different, and what I want: http://learn.adobe.com/wiki/download/attachments/52658564/digital_signatures_in_acrobat_9x.pdf?version=1 thanks: a. From dihedral88888 at googlemail.com Thu Dec 8 02:18:46 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Wed, 7 Dec 2011 23:18:46 -0800 (PST) Subject: I love the decorator in Python!!! Message-ID: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> I use the @ decorator to behave exactly like a c macro that does have fewer side effects. I am wondering is there other interesting methods to do the jobs in Python? A lot people complained that no macro in Python. Cheers to the rule of Python : If there's none then just go ahead and build one! From marco.rucci at gmail.com Thu Dec 8 02:39:20 2011 From: marco.rucci at gmail.com (marco.rucci at gmail.com) Date: Wed, 7 Dec 2011 23:39:20 -0800 (PST) Subject: Insert trusted timestamp to PDF In-Reply-To: References: Message-ID: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Hi, take a look at this online tool: http://easytimestamping.com It is able to apply RFC3161 compliant trusted timestamps, issued by accredited Certification Authorities. The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. -- Marco Rucci securo.it From marco.rucci at gmail.com Thu Dec 8 02:39:20 2011 From: marco.rucci at gmail.com (marco.rucci at gmail.com) Date: Wed, 7 Dec 2011 23:39:20 -0800 (PST) Subject: Insert trusted timestamp to PDF In-Reply-To: References: Message-ID: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Hi, take a look at this online tool: http://easytimestamping.com It is able to apply RFC3161 compliant trusted timestamps, issued by accredited Certification Authorities. The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. -- Marco Rucci securo.it From alec.taylor6 at gmail.com Thu Dec 8 02:42:47 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Thu, 8 Dec 2011 18:42:47 +1100 Subject: Insert trusted timestamp to PDF In-Reply-To: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> References: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Message-ID: Or the python implementation of that RFC: http://pypi.python.org/pypi/rfc3161/0.1.3 On Thu, Dec 8, 2011 at 6:39 PM, wrote: > Hi, take a look at this online tool: http://easytimestamping.com > > It is able to apply RFC3161 compliant trusted timestamps, issued by accredited Certification Authorities. > > The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) > > If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. > > -- > Marco Rucci > securo.it > -- > http://mail.python.org/mailman/listinfo/python-list From airween at gmail.com Thu Dec 8 02:59:31 2011 From: airween at gmail.com (=?utf-8?B?SGVnZWTDvHMs?= Ervin) Date: Thu, 8 Dec 2011 08:59:31 +0100 Subject: Insert trusted timestamp to PDF In-Reply-To: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> References: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Message-ID: <20111208075930.GA3526@arxnet.hu> hello, On Wed, Dec 07, 2011 at 11:39:20PM -0800, marco.rucci at gmail.com wrote: > Hi, take a look at this online tool: http://easytimestamping.com I need to create the PDF on my server - it could be any online service, but it must to have any kind of API. > It is able to apply RFC3161 compliant trusted timestamps, issued by accredited Certification Authorities. that's no problem in Python... :) http://pypi.python.org/pypi/rfc3161/ > The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) I'm afraid that's not good for us - we need to propagate PDF files in enbedded mode. > If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. sounds good - what do you think, when will you release that about? Thanks: a. From clp2 at rebertia.com Thu Dec 8 03:04:28 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 8 Dec 2011 00:04:28 -0800 Subject: I love the decorator in Python!!! In-Reply-To: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On Wed, Dec 7, 2011 at 11:18 PM, 88888 Dihedral wrote: > I use the @ decorator to behave exactly like a c macro that > does have fewer side effects. > > I am wondering is there other interesting methods to do the > jobs in Python? * Class decorators (http://www.python.org/dev/peps/pep-3129/ ); i.e. using decorators on a class instead of a function/method * Metaclasses (http://docs.python.org/dev/reference/datamodel.html#customizing-class-creation ); a rather more complicated feature capable of serious black magic * Descriptors (http://docs.python.org/dev/reference/datamodel.html#implementing-descriptors ); probably the most rarely directly used of the three Cheers, Chris -- http://rebertia.com From jpiitula at ling.helsinki.fi Thu Dec 8 03:54:48 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 08 Dec 2011 10:54:48 +0200 Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: Terry Reedy writes: > On 12/7/2011 7:03 PM, Steven D'Aprano wrote: > > On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: > > > >> Is there a way to create three variables dynamically inside Sum > >> in order to re write the function like this? > > I should have mentioned in my earlier response that 'variable' is a > bit vague and misleading. Python has names bound to objects. The language reference at python.org uses both terms - name and variable - freely. Here is one instance. # If a name is bound in a block, it is a local variable of that block, # unless declared as nonlocal. If a name is bound at the module level, # it is a global variable. (The variables of the module code block are # local and global.) If a variable is used in a code block but not # defined there, it is a free variable. Perhaps you could consider accepting this. Or trying to change it. Or starting to tell newcomers that the official documentation of the language is, say, vague and misleading. (It seems good to me.) From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Thu Dec 8 03:59:26 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Thu, 08 Dec 2011 09:59:26 +0100 Subject: I love the decorator in Python!!! In-Reply-To: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: Am 08.12.2011 08:18 schrieb 88888 Dihedral: > I use the @ decorator to behave exactly like a c macro that > does have fewer side effects. > > I am wondering is there other interesting methods to do the > jobs in Python? In combination with a generator, you can do many funny things. For example, you can build up a string: def mkstring(f): """Turns a string generator into a string, joining with ", ". """ return ", ".join(f()) def create_answer(): @mkstring def people(): yield "Anna" yield "John" yield "Theo" return "The following people were here: " + people Many other things are thinkable... Thomas From hniksic at xemacs.org Thu Dec 8 04:30:01 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 08 Dec 2011 10:30:01 +0100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> Message-ID: <87y5un9zs6.fsf@xemacs.org> Tim Chase writes: > From an interface perspective, I suppose it would work. However one > of the main computer-science reasons for addressing by a hash is to > get O(1) access to items (modulo pessimal hash structures/algorithms > which can approach O(N) if everything hashes to the same > value/bucket), rather than the O(logN) time you'd get from a tree. So > folks reaching for a hash/map might be surprised if performance > degraded with the size of the contents. In a language like Python, the difference between O(1) and O(log n) is not the primary reason why programmers use dict; they use it because it's built-in, efficient compared to alternatives, and convenient to use. If Python dict had been originally implemented as a tree, I'm sure it would be just as popular. Omitting the factor of O(log n) as functionally equivalent to O(1) is applicable to many situations and is sometimes called "soft-O" notation. One example from practice is the pre-2011 C++, where the standardization committee failed to standardize hash tables on time for the 1998 standard. Although this was widely recognized as an oversight, a large number of programs simply used tree-based std::maps and never noticed a practical difference between between average-constant-time and logarithmic complexity lookups. From hanspeter.sloot at gmail.com Thu Dec 8 04:34:09 2011 From: hanspeter.sloot at gmail.com (HansPeter) Date: Thu, 8 Dec 2011 01:34:09 -0800 (PST) Subject: how to test attribute existence of feedparser objects Message-ID: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> Hi, While using the feedparser library for downloading RSS feeds some of the blog entries seem to have no title. File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__ AttributeError: object has no attribute 'title' Is there a way to test the existence of an attribute? I can use an exception but like below to see whether it exists but this is a clumsy way since the function has to return the title. d=feedparser.parse(url,handlers = [proxy]) try: print "TITLE ",d.feed.title except: print "HAS NO TITLE" wc={} Regards HansPeter From rosuav at gmail.com Thu Dec 8 04:37:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Dec 2011 20:37:55 +1100 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On Thu, Dec 8, 2011 at 7:59 PM, Thomas Rachel wrote: > Many other things are thinkable... And many more are unthinkable. Can we start an International Obfuscated Python Code Contest? It's the only place such... abhorrences can properly flourish. ChrisA From jimmy at retzlaff.com Thu Dec 8 04:57:16 2011 From: jimmy at retzlaff.com (Jimmy Retzlaff) Date: Thu, 8 Dec 2011 01:57:16 -0800 Subject: mrjob v0.3.0 released Message-ID: What is mrjob? ----------------------- mrjob is a Python package that helps you write and run Hadoop Streaming jobs. mrjob fully supports Amazon's Elastic MapReduce (EMR) service, which allows you to buy time on a Hadoop cluster on an hourly basis. It also works with your own Hadoop cluster. Some important features: * Run jobs on EMR, your own Hadoop cluster, or locally (for testing). * Write multi-step jobs (one map-reduce step feeds into the next) * Duplicate your production environment inside Hadoop * Upload your source tree and put it in your job's $PYTHONPATH * Run make and other setup scripts * Set environment variables (e.g. $TZ) * Easily install python packages from tarballs (EMR only) * Setup handled transparently by mrjob.conf config file * Automatically interpret error logs from EMR * SSH tunnel to hadoop job tracker on EMR * Minimal setup * To run on EMR, set $AWS_ACCESS_KEY_ID and $AWS_SECRET_ACCESS_KEY * To run on your Hadoop cluster, install simplejson and make sure $HADOOP_HOME is set. More info: * Install mrjob: pip install mrjob -OR- easy_install mrjob * Documentation: http://packages.python.org/mrjob/ * PyPI: http://pypi.python.org/pypi/mrjob * Mailing list: http://groups.google.com/group/mrjob * Development is hosted at github: http://github.com/Yelp/mrjob What's new? -------------------- mrjob v0.3.0 is a major new release. Full details are at http://packages.python.org/mrjob/whats-new.html - here are a few highlights: v0.3.0, 2011-12-07 * Combiners * *_init() and *_final() for mappers, reducers, and combiners * Custom option parsers * Job flow pooling on EMR (saves time and money!) * SSH log fetching * New EMR diagnostic tools A big thanks to the contributors to this release: Steve Johnson, Dave Marin, Wahbeh Qardaji, Derek Wilson, Jordan Andersen, and Benjamin Goldenberg! -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Thu Dec 8 05:24:30 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 8 Dec 2011 02:24:30 -0800 Subject: how to test attribute existence of feedparser objects In-Reply-To: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> References: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> Message-ID: On Thu, Dec 8, 2011 at 1:34 AM, HansPeter wrote: > Hi, > > While using the feedparser library for downloading RSS feeds some of > the blog entries seem to have no title. > > ?File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__ > AttributeError: object has no attribute 'title' > > Is there a way to test the existence of an attribute? > > I can use an exception but like below to see whether it exists but > this is a clumsy way since the function has to return the title. hasattr(obj, attr_name) See docs.python.org/dev/library/functions.html#hasattr That said, sounds like it won't make much difference in the particular case you mention. Also, never use a bare "except:" clause, unless you know what you're doing and have a really good reason. Do "except AttributeError" in this case. Cheers, Chris -- http://rebertia.com From marco.rucci at gmail.com Thu Dec 8 06:02:23 2011 From: marco.rucci at gmail.com (marco.rucci at gmail.com) Date: Thu, 8 Dec 2011 03:02:23 -0800 (PST) Subject: Insert trusted timestamp to PDF In-Reply-To: References: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Message-ID: <8925363.67.1323342143055.JavaMail.geo-discussion-forums@yqgo15> On Thursday, December 8, 2011 8:59:31 AM UTC+1, Heged?s, Ervin wrote: > > The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) > > I'm afraid that's not good for us - we need to propagate PDF > files in enbedded mode. I'll do some research and let you know if it is feasible. Anyway, it should be possible to embed it later. > > If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. > > sounds good - what do you think, when will you release that > about? The API is basically ready. We are only missing the authentication method. In less then a couple weeks it should be complete. If you want, I can give you access to a beta version of the API, so you can start integrating the service with your application and then add the required authentication process. Can I contact you privately by email? Thanks for your interest, Marco. From marco.rucci at gmail.com Thu Dec 8 06:02:23 2011 From: marco.rucci at gmail.com (marco.rucci at gmail.com) Date: Thu, 8 Dec 2011 03:02:23 -0800 (PST) Subject: Insert trusted timestamp to PDF In-Reply-To: References: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Message-ID: <8925363.67.1323342143055.JavaMail.geo-discussion-forums@yqgo15> On Thursday, December 8, 2011 8:59:31 AM UTC+1, Heged?s, Ervin wrote: > > The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) > > I'm afraid that's not good for us - we need to propagate PDF > files in enbedded mode. I'll do some research and let you know if it is feasible. Anyway, it should be possible to embed it later. > > If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. > > sounds good - what do you think, when will you release that > about? The API is basically ready. We are only missing the authentication method. In less then a couple weeks it should be complete. If you want, I can give you access to a beta version of the API, so you can start integrating the service with your application and then add the required authentication process. Can I contact you privately by email? Thanks for your interest, Marco. From andrea.crotti.0 at gmail.com Thu Dec 8 06:10:07 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 08 Dec 2011 11:10:07 +0000 Subject: Questions about LISP and Python. In-Reply-To: References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: <4EE09B0F.4070009@gmail.com> On 12/08/2011 04:10 AM, Rick Johnson wrote: > ... > > Why has GvR not admonished the atrocious behavior of some people in > this community? Why has GvR not admitted publicly the hideous state of > IDLE and Tkinter? Where is the rally call? Where is the community > spirit? The future of Pythin is in your hands Mr. Van Rossum. Will you > step up and do what needs to be done? Will you have the courage? i > sincerely hope so. If not, please step down and allow someone to lead. > I await the supreme commanders ascent to power. This last part seems like the "lord of the rings", but I guess (even if still hope not) that he's serious :D Supposing even that Guido resigns, why do you think that the power should go to you? Power is not something that you can claim for, you have to earn the right, and ranting doesn't normally buy anything ;) From rosuav at gmail.com Thu Dec 8 06:20:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Dec 2011 22:20:56 +1100 Subject: Questions about LISP and Python. In-Reply-To: <4EE09B0F.4070009@gmail.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> <4EE09B0F.4070009@gmail.com> Message-ID: On Thu, Dec 8, 2011 at 10:10 PM, Andrea Crotti wrote: > Supposing even that Guido resigns, why do you think that the power should go > to you? > Power is not something that you can claim for, you have to earn the right, > and > ranting doesn't normally buy anything ;) Power is something you create. Did you know that you, too, can be head of an open source project the size of Python? It's easy. Just start one! Go to GitHub, Google Code, BitBucket, SourceForge (they all seem to have exactly two capital letters in them - must be important), or any other such site, and start coding your new and most awesome language. You can even make yourself a mailing list so that you can get your revenge on the Evil Python Mafia (hereunder and hereafter the EPM) by ignoring every patch ever submitted! Chris Angelico From kmichael.aye at gmail.com Thu Dec 8 06:22:27 2011 From: kmichael.aye at gmail.com (K.-Michael Aye) Date: Thu, 8 Dec 2011 12:22:27 +0100 Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On 2011-12-08 08:59:26 +0000, Thomas Rachel said: > Am 08.12.2011 08:18 schrieb 88888 Dihedral: >> I use the @ decorator to behave exactly like a c macro that >> does have fewer side effects. >> >> I am wondering is there other interesting methods to do the >> jobs in Python? > > In combination with a generator, you can do many funny things. > > > For example, you can build up a string: > > def mkstring(f): > """Turns a string generator into a string, > joining with ", ". > """ > return ", ".join(f()) > > def create_answer(): > @mkstring > def people(): > yield "Anna" > yield "John" > yield "Theo" > > return "The following people were here: " + people > > > Many other things are thinkable... > > > Thomas I am still perplexed about decorators though, am happily using Python for many years without them, but maybe i am missing something? For example in the above case, if I want the names attached to each other with a comma, why wouldn't I just create a function doing exactly this? Why would I first write a single name generator and then decorate it so that I never can get single names anymore (this is the case, isn't it? Once decorated, I can not get the original behaviour of the function anymore. So, above, why not def mkstring(mylist): with the same function declaration and then just call it with a list of names that I generate elsewhere in my program? I just can't identify the use-case for decorators, but as I said, maybe I am missing something. Michael From anacrolix at gmail.com Thu Dec 8 06:27:03 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Thu, 8 Dec 2011 22:27:03 +1100 Subject: Questions about LISP and Python. In-Reply-To: <4EE09B0F.4070009@gmail.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> <4EE09B0F.4070009@gmail.com> Message-ID: Guido is too busy secretly pouring his cruelty and malice into a master ring to answer trolls. Help yourself to a lesser ring on your way out. On Dec 8, 2011 10:14 PM, "Andrea Crotti" wrote: > On 12/08/2011 04:10 AM, Rick Johnson wrote: > >> ... >> >> Why has GvR not admonished the atrocious behavior of some people in >> this community? Why has GvR not admitted publicly the hideous state of >> IDLE and Tkinter? Where is the rally call? Where is the community >> spirit? The future of Pythin is in your hands Mr. Van Rossum. Will you >> step up and do what needs to be done? Will you have the courage? i >> sincerely hope so. If not, please step down and allow someone to lead. >> I await the supreme commanders ascent to power. >> > > This last part seems like the "lord of the rings", but I guess (even if > still hope not) > that he's serious :D > > Supposing even that Guido resigns, why do you think that the power should > go > to you? > Power is not something that you can claim for, you have to earn the right, > and > ranting doesn't normally buy anything ;) > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bsemene at cyanide-studio.com Thu Dec 8 06:28:21 2011 From: bsemene at cyanide-studio.com (Bastien Semene) Date: Thu, 08 Dec 2011 12:28:21 +0100 Subject: sending a variable to an imported module Message-ID: <4EE09F55.2050504@cyanide-studio.com> Hi list, I'm trying to pass a variable to an imported module without singletons. I've seen in the doc, and tested that I can't use global to do it : === module.py === def testf(): print test === main.py === global test test = 1 imported_module = __import__(module, globals(), locals(), [], -1) importmodule.testf() === output === NameError: global name 'test' is not defined While I was reading many (many) threads about singleton I read people claiming that singletons can always be avoided (I can't remeber the most relevant thread on stackoverflow). I don't want to start a new debate about singletons, I think Internet has enough debates yet. But in my case I'd like to access this variable anywhere and at anytime without having to pass it as a parameter everywhere (this variable is a configuration manager object). How can I achieve that without singletons ? I'm beginner in Python, that's why I'm maybe missing something obvious. Thanks, Bastien Semene From rosuav at gmail.com Thu Dec 8 06:43:12 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Dec 2011 22:43:12 +1100 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > I am still perplexed about decorators though, am happily using Python for > many years without them, but maybe i am missing something? > For example in the above case, if I want the names attached to each other > with a comma, why wouldn't I just create a function doing exactly this? Why > would I first write a single name generator and then decorate it so that I > never can get single names anymore (this is the case, isn't it? Once > decorated, I can not get the original behaviour of the function anymore. The example given is a toy. It's hardly useful. However, there are a number of handy uses for decorators; mostly, they consist of giving a single simple keyword to a complicated set of logic. One example is the @classmethod and @staticmethod decorators - the code to implement them could be uglier than nested inline assembly, but you don't have to care, because you just type "@staticmethod" in front of your def statement and it does its magic. Here's a handy trick that I'm sure someone has done in a more sophisticated way: def trace(func): if debugmode: return lambda *a,**ka: (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] return func Then you put @trace in front of all your functions, and if debugmode is False, nothing will be done - but set it to true, and you get console output at the entry and exit of each function. >>> @trace def test(x): print("Test! "+x) return 5 >>> test("asdf") >test Test! asdf References: <4EE09F55.2050504@cyanide-studio.com> Message-ID: On 8 December 2011 11:28, Bastien Semene wrote: > Hi list, > > I'm trying to pass a variable to an imported module without singletons. > I've seen in the doc, and tested that I can't use global to do it : > > === module.py === > def testf(): > ?print test > > > === main.py === > global test > test = 1 > > imported_module = __import__(module, globals(), locals(), [], -1) > > importmodule.testf() > > === output === > NameError: global name 'test' is not defined > > > > While I was reading many (many) threads about singleton I read people > claiming that singletons can always be avoided (I can't remeber the most > relevant thread on stackoverflow). > I don't want to start a new debate about singletons, I think Internet has > enough debates yet. > > But in my case I'd like to access this variable anywhere and at anytime > without having to pass it as a parameter everywhere (this variable is a > configuration manager object). > How can I achieve that without singletons ? > I'm beginner in Python, that's why I'm maybe missing something obvious. Put it in its own module and import that module in the places where it is needed. E.g. in your example: == settings.py == test = 1 == module.py == import settings def testf(): print settings.testf == main.py == import module module.testf() In every module that you need 'test', import settings and you will be able to access test. This is why singletons are often not needed in Python (just like most other design patterns). -- Arnaud From d at davea.name Thu Dec 8 07:15:26 2011 From: d at davea.name (Dave Angel) Date: Thu, 08 Dec 2011 07:15:26 -0500 Subject: sending a variable to an imported module In-Reply-To: <4EE09F55.2050504@cyanide-studio.com> References: <4EE09F55.2050504@cyanide-studio.com> Message-ID: <4EE0AA5E.8000005@davea.name> On 12/08/2011 06:28 AM, Bastien Semene wrote: > Hi list, > > I'm trying to pass a variable to an imported module without singletons. > I've seen in the doc, and tested that I can't use global to do it : > > === module.py === > def testf(): > print test > > > === main.py === > global test > test = 1 > > imported_module = __import__(module, globals(), locals(), [], -1) > > importmodule.testf() > > === output === > NameError: global name 'test' is not defined > > Please paste your code and your stacktrace, don't retype them. In the above, you spelled 'imported_module" two different ways, and forgot the quotes around "modue", so it couldn't run. There are probably other problems, but what's the point? > > While I was reading many (many) threads about singleton I read people > claiming that singletons can always be avoided (I can't remeber the > most relevant thread on stackoverflow). > I don't want to start a new debate about singletons, I think Internet > has enough debates yet. > > But in my case I'd like to access this variable anywhere and at > anytime without having to pass it as a parameter everywhere (this > variable is a configuration manager object). > How can I achieve that without singletons ? > I'm beginner in Python, that's why I'm maybe missing something obvious. global variables are global only within their own module, but you probably knew that. And using the global keyword in main.py isn't accomplishing anything. Since you're not inside a def or a class, test is already global, as soon as you give it a value. You don't pass values to a module, you load the module. And if the module doesn't have any top-level code, you can "monkey-patch" it to your heart's content, on lines following. If mymodule.py doesn't have a global value test, and you wish it did, you can simply do something like: import mymodule mymodule.test = 42 This attribute of mymodule is totally unrelated to one of the same name in main.py. if you want to refer to it, or to change it again, from main.py, you'd have to use mymodule.test. If the module had top-level code that needed to see your new global, then you'd have a problem, because you can't put it there till after the import returns. Now, most of the time when this sort of thing happens, what you really want is to define another module whose only purpose is to supply these common values. That module should get imported by both your script and your module. -- DaveA From bsemene at cyanide-studio.com Thu Dec 8 08:47:00 2011 From: bsemene at cyanide-studio.com (Bastien Semene) Date: Thu, 08 Dec 2011 14:47:00 +0100 Subject: sending a variable to an imported module In-Reply-To: <4EE0AA5E.8000005@davea.name> References: <4EE09F55.2050504@cyanide-studio.com> <4EE0AA5E.8000005@davea.name> Message-ID: <4EE0BFD4.2020608@cyanide-studio.com> Thanks both, Putting the variable inside a module works well. As the content is an object created inside another module I'm using this trick : module.CONFIG = module.load() So the variable is handled by the module that creates/use it, easy to use and pretty "native" to understand. Le 08/12/2011 13:15, Dave Angel a ?crit : > On 12/08/2011 06:28 AM, Bastien Semene wrote: >> Hi list, >> >> I'm trying to pass a variable to an imported module without singletons. >> I've seen in the doc, and tested that I can't use global to do it : >> >> === module.py === >> def testf(): >> print test >> >> >> === main.py === >> global test >> test = 1 >> >> imported_module = __import__(module, globals(), locals(), [], -1) >> >> importmodule.testf() >> >> === output === >> NameError: global name 'test' is not defined >> >> > Please paste your code and your stacktrace, don't retype them. In the > above, you spelled 'imported_module" two different ways, and forgot > the quotes around "modue", so it couldn't run. There are probably > other problems, but what's the point? >> >> While I was reading many (many) threads about singleton I read people >> claiming that singletons can always be avoided (I can't remeber the >> most relevant thread on stackoverflow). >> I don't want to start a new debate about singletons, I think Internet >> has enough debates yet. >> >> But in my case I'd like to access this variable anywhere and at >> anytime without having to pass it as a parameter everywhere (this >> variable is a configuration manager object). >> How can I achieve that without singletons ? >> I'm beginner in Python, that's why I'm maybe missing something obvious. > global variables are global only within their own module, but you > probably knew that. > > And using the global keyword in main.py isn't accomplishing anything. > Since you're not inside a def or a class, test is already global, as > soon as you give it a value. > > You don't pass values to a module, you load the module. And if the > module doesn't have any top-level code, you can "monkey-patch" it to > your heart's content, on lines following. > > If mymodule.py doesn't have a global value test, and you wish it did, > you can simply do something like: > import mymodule > mymodule.test = 42 > This attribute of mymodule is totally unrelated to one of the same > name in main.py. if you want to refer to it, or to change it again, > from main.py, you'd have to use mymodule.test. > > If the module had top-level code that needed to see your new global, > then you'd have a problem, because you can't put it there till after > the import returns. > > Now, most of the time when this sort of thing happens, what you really > want is to define another module whose only purpose is to supply these > common values. That module should get imported by both your script > and your module. > > From sajuptpm at gmail.com Thu Dec 8 09:09:05 2011 From: sajuptpm at gmail.com (sajuptpm) Date: Thu, 8 Dec 2011 06:09:05 -0800 (PST) Subject: Documentation using Sphinx Message-ID: <6107ecd2-9acf-4334-8877-ca8b9ff90752@b14g2000prn.googlegroups.com> Hi, I am trying source code documentation using Sphinx. Here i have to copy paste all modules in to *.rst file, that is painful. Have any way to create documentation (doc for all modules, classes and methods in the project directory) from project folder quickly. I also plannig to add a code browsing facility in the document, for example: code should display if we click on a method/class name in the doc, does sphinx has this feature??? or what tool i have to choose for that ???. From solutionmanualgroup1 at gmail.com Thu Dec 8 09:22:33 2011 From: solutionmanualgroup1 at gmail.com (kevin norton) Date: Thu, 8 Dec 2011 06:22:33 -0800 (PST) Subject: Test Banks and Solution Manuals Updated List 2012 Message-ID: Hi dear students; We are SolutionManualGroup.We established SolutionManualGroup in 2004. SolutionManualGroup is the leading provider of Solution Manuals and Test Banks help for college and graduate degree students.We have solution manuals and test banks for a competitive price. We also have other manuals more than 3000..We have a lot of solutions manual and test banks in low price to get solution manual you want please send message to us. #Feel free to contact us. solutionmanualgroup [at] gmail [dot] com #If your interested do let me know at http://solutionmanualgroup.blogspot.com/ #All solution manuals are in PDF format... #Full List of Solution Manuals and Test Banks are at http://solutionmanualgroup.blogspot.com/ #Please visit our website and search click Ctrl+F Regards SolutionManualGroup ( solutionmanualgroup at gmail.com ) List of Some Testbanks & Solutions Manual: -A Transition to Advanced Mathematics by Douglas Smith, Maurice Eggen 5 Solution Manual -Accounting by Carl S. Warren, James M. Reeve 24 Instructor's Manual -Accounting by Carl S. Warren, James M. Reeve 24 Test Bank -Accounting and Auditing Research Tools and Strategies by Weirich, Pearson, Churyk 7 Solution Manual -Accounting and Auditing Research Tools and Strategies by Weirich, Pearson, Churyk 7 Cases Solutions -Accounting for Governmental and Nonprofit Entities by Wilson 15 Solution Manual -Accounting for Governmental and Nonprofit Entities by Wilson 15 Test Bank -Accounting Information Systems by George H. Bodnar ,William S. Hopwood Solution Manual -Accounting Information Systems by George H. Bodnar ,William S. Hopwood Test Bank -Accounting Information Systems by Gelinas 8 Solution Manual -Accounting Information Systems by Gelinas 8 Test Bank -Accounting Information Systems by Romney 11 Solution Manual -Accounting Information Systems by Romney 11 Test Bank -Accounting Information Systems by Romney, Steinbart 12 Instructor's Manual -Accounting Information Systems by Romney, Steinbart 12 Solution Manual -Accounting Information Systems by Romney, Steinbart 12 Test Bank -Accounting Information Systems by James A. Hall 7 Solution Manual -Accounting Information Systems by James A. Hall 7 Test Bank -Accounting Information Systems Basic Concepts & Current Issues by Robert L. Hurt 2 Solution Manual -Accounting Information Systems Basic Concepts & Current Issues by Robert L. Hurt 2 Test Bank -Accounting for Decision Making and Control by Zimmerman 6 Solution Manual -Accounting for Decision Making and Control by Zimmerman 6 Test Bank -Accounting for Decision Making and Control by Zimmerman 7 Solution Manual -Accounting for Decision Making and Control by Zimmerman 7 Test Bank ### Full List of Solution Manuals and Test Banks are at http://solutionmanualgroup.blogspot.com/ #### From roy at panix.com Thu Dec 8 09:23:33 2011 From: roy at panix.com (Roy Smith) Date: Thu, 08 Dec 2011 09:23:33 -0500 Subject: Misleading error message of the day Message-ID: I just spent a while beating my head against this one. # Python 2.6 >>> a, b = 'foo' Traceback (most recent call last): File "", line 1, in ValueError: too many values to unpack The real problem is that there's too *few* values to unpack! It should have been a, b = 'foo', 'bar' I understand why it's generating the exception it does (the string is an iterable), but man, did that message throw off my thought processes and lead me down some totally bogus debugging paths. It's an unusual case to want to unpack a string. Maybe the message should changed to "too {many, few} values to unpack (are you sure you wanted to unpack a string?)" if the RHS is a basestring? From andrea.crotti.0 at gmail.com Thu Dec 8 09:39:27 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 08 Dec 2011 14:39:27 +0000 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <4EE0CC1F.602@gmail.com> On 12/08/2011 02:23 PM, Roy Smith wrote: > I just spent a while beating my head against this one. > > # Python 2.6 >>>> a, b = 'foo' > Traceback (most recent call last): > File "", line 1, in > ValueError: too many values to unpack > > The real problem is that there's too *few* values to unpack! It should > have been > > a, b = 'foo', 'bar' > > I understand why it's generating the exception it does (the string is an > iterable), but man, did that message throw off my thought processes and > lead me down some totally bogus debugging paths. > > It's an unusual case to want to unpack a string. Maybe the message > should changed to "too {many, few} values to unpack (are you sure you > wanted to unpack a string?)" if the RHS is a basestring? I had a few errors sometimes because I thought I passed in a list while I passed only a string, which since it's still iterable would just work but explode later. A nicer message wouldn't have really actually helped though, not sure it's worth to make an exception for such a thing.. From rosuav at gmail.com Thu Dec 8 09:40:53 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 01:40:53 +1100 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: On Fri, Dec 9, 2011 at 1:23 AM, Roy Smith wrote: > I just spent a while beating my head against this one. > > # Python 2.6 >>>> a, b = 'foo' > Traceback (most recent call last): > ?File "", line 1, in > ValueError: too many values to unpack Definitely weird! I smell a job for a linter though. If you had just happened to have a two-character string there, it would have quietly succeeded, and left you wondering what was going on - imho rather worse. This isn't something for the language to solve; the same issue would come up if you had something like: a=[1,2,3] b=[4,5,6] c,d=a # oops, mucked up the "a,b" side Or any other iterable. Looks to me like a chance for an informational note from your lint facility, not a change to the language. ChrisA From robert.kern at gmail.com Thu Dec 8 09:47:02 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 08 Dec 2011 14:47:02 +0000 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: On 12/8/11 2:23 PM, Roy Smith wrote: > I just spent a while beating my head against this one. > > # Python 2.6 >>>> a, b = 'foo' > Traceback (most recent call last): > File "", line 1, in > ValueError: too many values to unpack > > The real problem is that there's too *few* values to unpack! It should > have been > > a, b = 'foo', 'bar' > > I understand why it's generating the exception it does (the string is an > iterable), but man, did that message throw off my thought processes and > lead me down some totally bogus debugging paths. > > It's an unusual case to want to unpack a string. Maybe the message > should changed to "too {many, few} values to unpack (are you sure you > wanted to unpack a string?)" if the RHS is a basestring? Would including the respective numbers help your thought processes? ValueError: too many values to unpack (expected 2, got 3) -- 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 From jeanmichel at sequans.com Thu Dec 8 10:03:38 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 08 Dec 2011 16:03:38 +0100 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <4EE0D1CA.7080901@sequans.com> Roy Smith wrote: > I just spent a while beating my head against this one. > > # Python 2.6 > >>>> a, b = 'foo' >>>> > Traceback (most recent call last): > File "", line 1, in > ValueError: too many values to unpack > > The real problem is that there's too *few* values to unpack! It should > have been > > a, b = 'foo', 'bar' > > I understand why it's generating the exception it does (the string is an > iterable), but man, did that message throw off my thought processes and > lead me down some totally bogus debugging paths. > > It's an unusual case to want to unpack a string. Maybe the message > should changed to "too {many, few} values to unpack (are you sure you > wanted to unpack a string?)" if the RHS is a basestring? > string are iterable, considering this, the error is correct. Values to unpack in 'foo' are 'f', 'o', 'o' > a,b,c = 'foo' > print a,b,c f o o JM From modelnine at modelnine.org Thu Dec 8 10:16:56 2011 From: modelnine at modelnine.org (Heiko Wundram) Date: Thu, 08 Dec 2011 16:16:56 +0100 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <4EE0D4E8.4060600@modelnine.org> Am 08.12.2011 15:47, schrieb Robert Kern: > Would including the respective numbers help your thought processes? > ValueError: too many values to unpack (expected 2, got 3) Not possible in the general case (as the right-hand side might be an arbitrary iterable/iterator...). -- --- Heiko. From andrea.crotti.0 at gmail.com Thu Dec 8 10:17:56 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 08 Dec 2011 15:17:56 +0000 Subject: Documentation using Sphinx In-Reply-To: <6107ecd2-9acf-4334-8877-ca8b9ff90752@b14g2000prn.googlegroups.com> References: <6107ecd2-9acf-4334-8877-ca8b9ff90752@b14g2000prn.googlegroups.com> Message-ID: <4EE0D524.7080909@gmail.com> On 12/08/2011 02:09 PM, sajuptpm wrote: > Hi, > I am trying source code documentation using Sphinx. Here i have to > copy paste all modules in to *.rst file, that is painful. Have any way > to create documentation (doc for all modules, classes and methods in > the project directory) from project folder quickly. I also plannig to > add a code browsing facility in the document, for example: code should > display if we click on a method/class name in the doc, does sphinx has > this feature??? or what tool i have to choose for that ???. You should never copy and paste anything ;) Have a look at the autodoc plugin. From roy at panix.com Thu Dec 8 10:30:05 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:30:05 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <32921150.1028.1323358205974.JavaMail.geo-discussion-forums@yqbz41> On Thursday, December 8, 2011 9:47:02 AM UTC-5, Robert Kern wrote: > Would including the respective numbers help your thought processes? > > ValueError: too many values to unpack (expected 2, got 3) I don't know if that would have done the trick for me on this particular one. On the other hand, adding "expected X, got Y" to the message would generally be a good thing. From roy at panix.com Thu Dec 8 10:30:05 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:30:05 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <32921150.1028.1323358205974.JavaMail.geo-discussion-forums@yqbz41> On Thursday, December 8, 2011 9:47:02 AM UTC-5, Robert Kern wrote: > Would including the respective numbers help your thought processes? > > ValueError: too many values to unpack (expected 2, got 3) I don't know if that would have done the trick for me on this particular one. On the other hand, adding "expected X, got Y" to the message would generally be a good thing. From roy at panix.com Thu Dec 8 10:33:50 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:33:50 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant wrote: > string are iterable, considering this, the error is correct. Yes, I understand that the exception is correct. I'm not saying the exception should be changed, just that we have the opportunity to produce a more useful error message. The exception would be equally correct if it was: ValueError: you did something wrong but most people would probably agree that it's not the most useful message that could have been produced. From roy at panix.com Thu Dec 8 10:33:50 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:33:50 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant wrote: > string are iterable, considering this, the error is correct. Yes, I understand that the exception is correct. I'm not saying the exception should be changed, just that we have the opportunity to produce a more useful error message. The exception would be equally correct if it was: ValueError: you did something wrong but most people would probably agree that it's not the most useful message that could have been produced. From roy at panix.com Thu Dec 8 10:42:46 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:42:46 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> On Thursday, December 8, 2011 10:16:56 AM UTC-5, Heiko Wundram wrote: > Am 08.12.2011 15:47, schrieb Robert Kern: > > Would including the respective numbers help your thought processes? > > ValueError: too many values to unpack (expected 2, got 3) > > Not possible in the general case (as the right-hand side might be an > arbitrary iterable/iterator...). Why not? Take this example: def i(): i = 0 while True: print "returning:", i yield i i += 1 a, b = i() ./iter.py returning: 0 returning: 1 returning: 2 Traceback (most recent call last): File "./iter.py", line 10, in a, b = i() ValueError: too many values to unpack The exception was raised when i() returned it's third value, so saying "expected 2, got 3" is exactly correct. Yes, it is true that it might have gotten more if it kept going, but that's immaterial; the fact that it got to 3 is what caused the Holy Hand Grenade to be thrown. From roy at panix.com Thu Dec 8 10:42:46 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:42:46 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> On Thursday, December 8, 2011 10:16:56 AM UTC-5, Heiko Wundram wrote: > Am 08.12.2011 15:47, schrieb Robert Kern: > > Would including the respective numbers help your thought processes? > > ValueError: too many values to unpack (expected 2, got 3) > > Not possible in the general case (as the right-hand side might be an > arbitrary iterable/iterator...). Why not? Take this example: def i(): i = 0 while True: print "returning:", i yield i i += 1 a, b = i() ./iter.py returning: 0 returning: 1 returning: 2 Traceback (most recent call last): File "./iter.py", line 10, in a, b = i() ValueError: too many values to unpack The exception was raised when i() returned it's third value, so saying "expected 2, got 3" is exactly correct. Yes, it is true that it might have gotten more if it kept going, but that's immaterial; the fact that it got to 3 is what caused the Holy Hand Grenade to be thrown. From modelnine at modelnine.org Thu Dec 8 10:50:44 2011 From: modelnine at modelnine.org (Heiko Wundram) Date: Thu, 08 Dec 2011 16:50:44 +0100 Subject: Misleading error message of the day In-Reply-To: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> Message-ID: <4EE0DCD4.30501@modelnine.org> Am 08.12.2011 16:42, schrieb Roy Smith: > The exception was raised when i() returned it's third value, so saying "expected 2, got 3" is exactly correct. Yes, it is true that it might have gotten more if it kept going, but that's immaterial; the fact that it got to 3 is what caused the Holy Hand Grenade to be thrown. Please explain how that error message (in case you're not aiming at the actual count of elements in the source) differs from the curent wording "too many values", as you're simply displaying "expected n, got n+1" where n is visible from the immediate exception output... -- --- Heiko. From andrea.crotti.0 at gmail.com Thu Dec 8 10:55:16 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 08 Dec 2011 15:55:16 +0000 Subject: Misleading error message of the day In-Reply-To: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> Message-ID: <4EE0DDE4.4080802@gmail.com> On 12/08/2011 03:42 PM, Roy Smith wrote: > > Why not? Take this example: > > def i(): > i = 0 > while True: > print "returning:", i > yield i > i += 1 > > a, b = i() > > ./iter.py > returning: 0 > returning: 1 > returning: 2 > Traceback (most recent call last): > File "./iter.py", line 10, in > a, b = i() > ValueError: too many values to unpack > > The exception was raised when i() returned it's third value, so saying "expected 2, got 3" is exactly correct. Yes, it is true that it might have gotten more if it kept going, but that's immaterial; the fact that it got to 3 is what caused the Holy Hand Grenade to be thrown. Yes but how do you know how many values you generated when it quits? I mean I don't know how it work internally, but it should keep a temporary list of the yielded values to be able to find out how many values are there.. From rosuav at gmail.com Thu Dec 8 11:21:31 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 03:21:31 +1100 Subject: Misleading error message of the day In-Reply-To: <4EE0DDE4.4080802@gmail.com> References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <4EE0DDE4.4080802@gmail.com> Message-ID: On Fri, Dec 9, 2011 at 2:55 AM, Andrea Crotti wrote: > Yes but how do you know how many values you generated when it quits? > I mean I don't know how it work internally, but it should keep a temporary > list of the yielded values to be able to find out how many values are > there.. Iterator unpacking works roughly thus: 1) Count up how many results you need (call that N) 2) N times, get a value from the iterator. If StopIteration is raised, swallow it and raise ValueError because there were too few values. 3) Attempt to get one more value from the iterator. If StopIteration is NOT raised, raise ValueError because there were too many values. At no point is the "total size" of the iterator counted (it could, after all, be infinite). When ValueError is raised, all that's known is that StopIteration wasn't raised at the end of the process. ChrisA From lie.1296 at gmail.com Thu Dec 8 11:21:52 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 09 Dec 2011 03:21:52 +1100 Subject: Need some IPC pointers In-Reply-To: <4ED69A1A.3080609@gmail.com> References: <4ED69A1A.3080609@gmail.com> Message-ID: On 12/01/2011 08:03 AM, Andrew Berg wrote: > I've done some research, but I'm not sure what's most appropriate for my > situation. What I want to do is have a long running process that spawns > processes (that aren't necessarily written in Python) and communicates > with them. The children can be spawned at any time and communicate at > any time. Being able to communicate with non-local processes would be > nice, but is not necessary. The implementation needs to be > cross-platform, but child processes will use the same OS as the parent > during runtime. > I don't think I'll ever need to transfer anything complicated or large - > just strings or possibly tuples/lists. I'd rather not go outside the > standard library (but I'd consider it). I don't need to worry about > compatibility with older Python versions; if it only works with Python > 3.2, that's not a problem. > I'm thinking sockets, but perhaps there's something simpler/easier. > Considering your requirements, I'd suggest a RESTful web service. It is fairly trivial to write HTTP clients in most languages, and python's standard library comes with a simple HTTP server so writing the server is easy as well. In context, the "long running process" will be the "server", the "children processes" will be the "client". Writing HTTP client is fairly trivial in most languages, the protocol is platform independent, and it is fairly trivial to communicate with non-local processes over the LAN or the Internet. As a plus, it's well standardized. As the data interchange format, I suggest either xml or json. There is a library for xml and json in almost any popular languages. Python comes with both. From kmichael.aye at gmail.com Thu Dec 8 11:24:11 2011 From: kmichael.aye at gmail.com (K.-Michael Aye) Date: Thu, 8 Dec 2011 17:24:11 +0100 Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On 2011-12-08 11:43:12 +0000, Chris Angelico said: > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: >> I am still perplexed about decorators though, am happily using Python for >> many years without them, but maybe i am missing something? >> For example in the above case, if I want the names attached to each other >> with a comma, why wouldn't I just create a function doing exactly this? Why >> would I first write a single name generator and then decorate it so that I >> never can get single names anymore (this is the case, isn't it? Once >> decorated, I can not get the original behaviour of the function anymore. > > The example given is a toy. It's hardly useful. However, there are a > number of handy uses for decorators; mostly, they consist of giving a > single simple keyword to a complicated set of logic. One example is > the @classmethod and @staticmethod decorators - the code to implement > them could be uglier than nested inline assembly, but you don't have > to care, because you just type "@staticmethod" in front of your def > statement and it does its magic. > > Here's a handy trick that I'm sure someone has done in a more > sophisticated way: > > def trace(func): > if debugmode: > return lambda *a,**ka: > (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] > return func > > Then you put @trace in front of all your functions, and if debugmode > is False, nothing will be done - but set it to true, and you get > console output at the entry and exit of each function. > >>>> @trace > def test(x): > print("Test! "+x) > return 5 > >>>> test("asdf") >> test > Test! asdf > 5 > > Again, it's helpful because it condenses all the logic (including the > 'debugmode' flag) down to a single high level directive: "Trace this > function". > > ChrisA I understand this one, it seems really useful. And maybe i start to sense some more applicability. Like this, with extra flags that could be set at run time, I could influence the way a function is executed without designing the function too complex, but by decorating it, which at the end could be easier to read than complicated if-then statements in the function. Thanks for your example. Michael From andrea.crotti.0 at gmail.com Thu Dec 8 11:34:54 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 08 Dec 2011 16:34:54 +0000 Subject: adding elements to set Message-ID: <4EE0E72E.2040104@gmail.com> I've wasted way too much time for this, which is surely not a Python bug, not something that surprised me a lot. I stupidly gave for granted that adding an object to a set would first check if there are equal elements inside, and then add it. As shown below this is not clearly the case.. Is it possible to get that behaviour implementing another magic method in my C class or I just have use another function to check (as I'm doing now). class C(object): def __init__(self, x): self.x = x def __eq__(self, other): return self.x == other.x if __name__ == '__main__': s = set() c1 = C(1) c2 = C(1) assert c1 == c2 s.add(c1) s.add(c2) print len(s) From rosuav at gmail.com Thu Dec 8 11:38:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 03:38:35 +1100 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On Fri, Dec 9, 2011 at 3:24 AM, K.-Michael Aye wrote: > I understand this one, it seems really useful. And maybe i start to sense > some more applicability. Like this, with extra flags that could be set at > run time, I could influence the way a function is executed without designing > the function too complex, but by decorating it, which at the end could be > easier to read than complicated if-then statements in the function. Right, and less duplication usually results in less bugs, too. Obviously if you go to any greater level of complexity than this you'd want to break the lambda out into a proper function, and possibly play with an indent level (increment on call, decrement on return). You can also easily add the run-time check, and anything else you want to do, too. It'll still all be buried away in the definition of trace(), and on functions that you want to trace, you need still only have "@trace" and nothing else. One piece of sophistication that I would rather like to see, but don't know how to do. Instead of *args,**kwargs, is it possible to somehow copy in the function's actual signature? I was testing this out in IDLE, and the fly help for the function no longer gave useful info about its argument list. This is why I say that this has almost certainly been done before in a much better way. ChrisA From rosuav at gmail.com Thu Dec 8 11:47:24 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 03:47:24 +1100 Subject: adding elements to set In-Reply-To: <4EE0E72E.2040104@gmail.com> References: <4EE0E72E.2040104@gmail.com> Message-ID: On Fri, Dec 9, 2011 at 3:34 AM, Andrea Crotti wrote: > I've wasted way too much time for this, which is surely not a Python bug, > not something that surprised me a lot. > > I stupidly gave for granted that adding an object to a set would first > check if there are equal elements inside, and then add it. It checks for equality using hashes. By default, in Python 2, objects' hashes are their ids - meaning that no two of them hash alike, and you'll get duplicates in your set. (In Python 3, the default appears to be that they're unhashable and hence can't go into the set at all.) class C(object): def __init__(self, x): self.x = x def __eq__(self, other): return self.x == other.x def __hash__(self): return hash(self.x) This chains the hashing requirement to the child, just as it chains the equality check. You can then stuff your objects into a set with more expected results. ChrisA From __peter__ at web.de Thu Dec 8 11:49:42 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Dec 2011 17:49:42 +0100 Subject: adding elements to set References: <4EE0E72E.2040104@gmail.com> Message-ID: Andrea Crotti wrote: > I've wasted way too much time for this, which is surely not a Python bug, > not something that surprised me a lot. > > I stupidly gave for granted that adding an object to a set would first > check if there are equal elements inside, and then add it. > > As shown below this is not clearly the case.. > Is it possible to get that behaviour implementing another magic method > in my C class or I just have use another function to check (as I'm doing > now). > > class C(object): > > def __init__(self, x): > self.x = x > > def __eq__(self, other): > return self.x == other.x > > > if __name__ == '__main__': > s = set() > c1 = C(1) > c2 = C(1) > assert c1 == c2 > s.add(c1) > s.add(c2) > > print len(s) Python's sets are hash-based; you have to implement a __hash__() method for the elements that ensures that c1 == c2 implies hash(c1) == hash(c2). >>> class C(object): ... def __init__(self, x): self.x = x ... def __eq__(self, other): return self.x == other.x ... def __hash__(self): return hash(self.x) ... >>> c1 = C(1) >>> c2 = C(1) >>> c1 == c2 True >>> s = set() >>> s.add(c1) >>> s.add(c2) >>> len(s) 1 From bahamutzero8825 at gmail.com Thu Dec 8 11:54:54 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 08 Dec 2011 10:54:54 -0600 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <4EE0EBDE.6@gmail.com> On 12/8/2011 10:38 AM, Chris Angelico wrote: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I remember seeing this in a PEP that is planned to be implemented in 3.3. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From bahamutzero8825 at gmail.com Thu Dec 8 11:55:29 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 08 Dec 2011 10:55:29 -0600 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <4EE0EC01.2080803@gmail.com> Decorators are great for adding common functionality to several functions without duplicating code. For example, I have one for my IRC bot that checks that the person sending the command is authorized to use the command. It's only "if mask in owner list then execute function else say access denied", but it simplifies the code and the intent of each function that uses it becomes more obvious. I have another that puts functions in a common try/except clause, which again simplifies the functions and makes them easier to understand. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From __peter__ at web.de Thu Dec 8 12:32:04 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Dec 2011 18:32:04 +0100 Subject: adding elements to set References: <4EE0E72E.2040104@gmail.com> Message-ID: Chris Angelico wrote: > It checks for equality using hashes. By default, in Python 2, objects' > hashes are their ids - meaning that no two of them hash alike, and > you'll get duplicates in your set. (In Python 3, the default appears > to be that they're unhashable and hence can't go into the set at all.) $ python3.2 -c'print({object(), object()})' {, } The only thing that has changed (in 2.7) is the algorithm to calculate the hash value. The bits are rotated to turn the four least significant bits into the most signicant ones. According to a comment in Objects/objects.c the change leads to fewer hash collisions. $ python2.6 -c'o = object(); print hash(o) == id(o)' True $ python2.7 -c'o = object(); print hash(o) == id(o)' False $ python2.7 -c'o = object(); print hash(o) == id(o)>>4 | (id(o)&0xF)<<60' True $ python3.2 -c'o = object(); print(hash(o) == id(o)>>4 | (id(o)&0xF)<<60)' True From rosuav at gmail.com Thu Dec 8 12:37:18 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 04:37:18 +1100 Subject: adding elements to set In-Reply-To: References: <4EE0E72E.2040104@gmail.com> Message-ID: On Fri, Dec 9, 2011 at 4:32 AM, Peter Otten <__peter__ at web.de> wrote: > The only thing that has changed (in 2.7) is the algorithm to calculate the > hash value. The bits are rotated to turn the four least significant bits > into the most signicant ones. According to a comment in Objects/objects.c > the change leads to fewer hash collisions. Interesting, but what I saw was this: >>> class C(object): def __init__(self, x): self.x = x def __eq__(self, other): return self.x == other.x >>> s=set() >>> c1=C(1) >>> s.add(c1) Traceback (most recent call last): File "", line 1, in s.add(c1) TypeError: unhashable type: 'C' (This is in IDLE from Python 3.2 on Windows.) However, s.add(object()) works fine. So subclasses don't get that. Odd. Makes sense though - you can't get this unexpected behaviour as easily. ChrisA From python.list at tim.thechases.com Thu Dec 8 12:41:17 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 08 Dec 2011 11:41:17 -0600 Subject: Misleading error message of the day In-Reply-To: <32921150.1028.1323358205974.JavaMail.geo-discussion-forums@yqbz41> References: <32921150.1028.1323358205974.JavaMail.geo-discussion-forums@yqbz41> Message-ID: <4EE0F6BD.4090203@tim.thechases.com> On 12/08/11 09:30, Roy Smith wrote: > On Thursday, December 8, 2011 9:47:02 AM UTC-5, Robert Kern > wrote: > >> Would including the respective numbers help your thought >> processes? >> >> ValueError: too many values to unpack (expected 2, got 3) > > I don't know if that would have done the trick for me on this > particular one. On the other hand, adding "expected X, got Y" > to the message would generally be a good thing. given the nature of the message, and the interaction with iterators-of-arbitrary/infinite length, it might have to be reduced to "Expected N, got more" or for the case where you didn't get enough, you know how many you got: "Expected N, but only got M". But the extra information would certainly be useful in tracking it down. -tkc From ian.g.kelly at gmail.com Thu Dec 8 12:43:28 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Dec 2011 10:43:28 -0700 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I was testing this out in > IDLE, and the fly help for the function no longer gave useful info > about its argument list. You can use the decorator module in PyPI to do this. -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Dec 8 12:43:55 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 08 Dec 2011 17:43:55 +0000 Subject: Questions about LISP and Python. In-Reply-To: References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: <4EE0F75B.7030700@mrabarnett.plus.com> On 08/12/2011 04:10, Rick Johnson wrote: [snip] > I believe this community has a cancer. A cancer that is rotting us > from the inside. A cancer that has metastasis and is spreading like > wild fire. > The problem with a cancer is not that it rots, but that it grows uncontrollably. > *Inquisitive Joe asked:* What is the source of this cancer Rick? > > The source is a direct result of insufficient leadership. Our current > leader has failed us. Maybe he never wanted to be a leader, but when > you go and declare yourself a "benevolent dictator for life" you'd > damn well better act like one! > He never declared himself "benevolent dictator for life", others declared him so. > Why has GvR not admonished the atrocious behavior of some people in > this community? Why has GvR not admitted publicly the hideous state of > IDLE and Tkinter? Where is the rally call? Where is the community > spirit? The future of Pythin is in your hands Mr. Van Rossum. Will you > step up and do what needs to be done? Will you have the courage? i > sincerely hope so. If not, please step down and allow someone to lead. > I await the supreme commanders ascent to power. GvR isn't our leader, we are his followers. There's a difference. :-) From invalid at invalid.invalid Thu Dec 8 13:10:17 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 8 Dec 2011 18:10:17 +0000 (UTC) Subject: Misleading error message of the day References: Message-ID: On 2011-12-08, Roy Smith wrote: > On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant wrote: >> string are iterable, considering this, the error is correct. > > Yes, I understand that the exception is correct. I'm not saying the exception should be changed, just that we have the opportunity to produce a more useful error message. The exception would be equally correct if it was: > > ValueError: you did something wrong My favorite is still the old classic error from and old Unix printer port driver: "lp0 on fire" > but most people would probably agree that it's not the most useful > message that could have been produced. -- Grant Edwards grant.b.edwards Yow! Don't hit me!! I'm in at the Twilight Zone!!! gmail.com From roy at panix.com Thu Dec 8 13:42:45 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 10:42:45 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> Message-ID: <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> (some, very, long, list, of, variable, names, to, get, the, stuff, unpacked, into) = function_that_should_return_a_14_tuple() raises ValueError: too many values to unpack Quick, what's the bug? Did I forget a variable on the LHS, or is my function returning more things than it should? I know it's supposed to be 14, but I don't know which side is wrong. Had it said "... expected 13, got 14", I would know immediately. Error messages should be as explicit as possible. It's just like bug reports. The basic mantra of a bug report is: 1) This is what I did 2) This is what I expected to happen 3) This is what I observed happen 4) This is how what I observed differed from what I expected Saying, "expected X, got Y" is more explicit than "got too many" From roy at panix.com Thu Dec 8 13:42:45 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 10:42:45 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> Message-ID: <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> (some, very, long, list, of, variable, names, to, get, the, stuff, unpacked, into) = function_that_should_return_a_14_tuple() raises ValueError: too many values to unpack Quick, what's the bug? Did I forget a variable on the LHS, or is my function returning more things than it should? I know it's supposed to be 14, but I don't know which side is wrong. Had it said "... expected 13, got 14", I would know immediately. Error messages should be as explicit as possible. It's just like bug reports. The basic mantra of a bug report is: 1) This is what I did 2) This is what I expected to happen 3) This is what I observed happen 4) This is how what I observed differed from what I expected Saying, "expected X, got Y" is more explicit than "got too many" From __peter__ at web.de Thu Dec 8 13:43:01 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Dec 2011 19:43:01 +0100 Subject: adding elements to set References: <4EE0E72E.2040104@gmail.com> Message-ID: Chris Angelico wrote: > On Fri, Dec 9, 2011 at 4:32 AM, Peter Otten <__peter__ at web.de> wrote: >> The only thing that has changed (in 2.7) is the algorithm to calculate >> the hash value. The bits are rotated to turn the four least significant >> bits into the most signicant ones. According to a comment in >> Objects/objects.c the change leads to fewer hash collisions. > > Interesting, but what I saw was this: > >>>> class C(object): > > def __init__(self, x): > self.x = x > > def __eq__(self, other): > return self.x == other.x > >>>> s=set() >>>> c1=C(1) >>>> s.add(c1) > Traceback (most recent call last): > File "", line 1, in > s.add(c1) > TypeError: unhashable type: 'C' > > (This is in IDLE from Python 3.2 on Windows.) > > However, s.add(object()) works fine. So subclasses don't get that. > Odd. Makes sense though - you can't get this unexpected behaviour as > easily. It seems to be even subtler: you can subclass if you don't implement __eq__(): >>> class C(object): pass ... >>> {C()} {<__main__.C object at 0x17defd0>} From duncan.booth at invalid.invalid Thu Dec 8 13:54:30 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 8 Dec 2011 18:54:30 GMT Subject: adding elements to set References: <4EE0E72E.2040104@gmail.com> Message-ID: Chris Angelico wrote: > On Fri, Dec 9, 2011 at 4:32 AM, Peter Otten <__peter__ at web.de> wrote: >> The only thing that has changed (in 2.7) is the algorithm to >> calculate the hash value. The bits are rotated to turn the four least >> significant bits into the most signicant ones. According to a comment >> in Objects/objects.c the change leads to fewer hash collisions. > > Interesting, but what I saw was this: > >>>> class C(object): > > def __init__(self, x): > self.x = x > > def __eq__(self, other): > return self.x == other.x > >>>> s=set() >>>> c1=C(1) >>>> s.add(c1) > Traceback (most recent call last): > File "", line 1, in > s.add(c1) > TypeError: unhashable type: 'C' > > (This is in IDLE from Python 3.2 on Windows.) > > However, s.add(object()) works fine. So subclasses don't get that. > Odd. Makes sense though - you can't get this unexpected behaviour as > easily. > Yes, the documentation describes this although I don't think anything highlights that it is a change from Python 2.x: [http://docs.python.org/py3k/reference/datamodel.html] > If a class does not define an __eq__() method it should not define a > __hash__() operation either; if it defines __eq__() but not > __hash__(), its instances will not be usable as items in hashable > collections. If a class defines mutable objects and implements an > __eq__() method, it should not implement __hash__(), since the > implementation of hashable collections requires that a key?s hash > value is immutable (if the object?s hash value changes, it will be in > the wrong hash bucket). So in Python 2.x you could define __eq__ and get the default __hash__ which would break dictionaries. With Python 3.x defining __eq__ will disable the default __hash__ although if you subclass a class that has both methods you could still get in a mess by redefining one without the other. -- Duncan Booth http://kupuguy.blogspot.com From benjamin.kaplan at case.edu Thu Dec 8 13:57:28 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Thu, 8 Dec 2011 13:57:28 -0500 Subject: Misleading error message of the day In-Reply-To: <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> Message-ID: On Thu, Dec 8, 2011 at 1:42 PM, Roy Smith wrote: > (some, > ?very, > ?long, > ?list, > ?of, > ?variable, > ?names, > ?to, > ?get, > ?the, > ?stuff, > ?unpacked, > ?into) = function_that_should_return_a_14_tuple() > > raises > > ValueError: too many values to unpack > > Quick, what's the bug? ?Did I forget a variable on the LHS, or is my function returning more things than it should? ?I know it's supposed to be 14, but I don't know which side is wrong. ?Had it said "... expected 13, got 14", I would know immediately. > If the RHS was a tuple or a list, yes you could know immediately. But unpacking works with any iterable, so it probably doesn't special-case lists and tuples. Iterables don't have a size- they just keep going until StopIteration is raised. So in EVERY SINGLE CASE, you would get "expected n args, got n+1" even if the iterable would return 24 items instead of 14, or would never stop returning items. > Error messages should be as explicit as possible. ?It's just like bug reports. ?The basic mantra of a bug report is: > > 1) This is what I did > > 2) This is what I expected to happen > > 3) This is what I observed happen > > 4) This is how what I observed differed from what I expected > > Saying, "expected X, got Y" is more explicit than "got too many" > > > -- > http://mail.python.org/mailman/listinfo/python-list From ethan at stoneleaf.us Thu Dec 8 14:09:23 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 08 Dec 2011 11:09:23 -0800 Subject: Misleading error message of the day In-Reply-To: References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> Message-ID: <4EE10B63.2010600@stoneleaf.us> Benjamin Kaplan wrote: > On Thu, Dec 8, 2011 at 1:42 PM, Roy Smith wrote: >> (some, >> very, >> long, >> list, >> of, >> variable, >> names, >> to, >> get, >> the, >> stuff, >> unpacked, >> into) = function_that_should_return_a_14_tuple() >> >> raises >> >> ValueError: too many values to unpack >> >> Quick, what's the bug? Did I forget a variable on the LHS, or is my function returning more things than it should? I know it's supposed to be 14, but I don't know which side is wrong. Had it said "... expected 13, got 14", I would know immediately. >> > > If the RHS was a tuple or a list, yes you could know immediately. But > unpacking works with any iterable, so it probably doesn't special-case > lists and tuples. Iterables don't have a size- they just keep going > until StopIteration is raised. So in EVERY SINGLE CASE, you would get > "expected n args, got n+1" even if the iterable would return 24 items > instead of 14, or would never stop returning items. Not so. There could be fewer, in which you could see "expected 13 args, got 7." ~Ethan~ From benjamin.kaplan at case.edu Thu Dec 8 14:38:28 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Thu, 8 Dec 2011 14:38:28 -0500 Subject: Misleading error message of the day In-Reply-To: <4EE10B63.2010600@stoneleaf.us> References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> <4EE10B63.2010600@stoneleaf.us> Message-ID: On Thu, Dec 8, 2011 at 2:09 PM, Ethan Furman wrote: > Benjamin Kaplan wrote: >> >> On Thu, Dec 8, 2011 at 1:42 PM, Roy Smith wrote: >>> >>> (some, >>> ?very, >>> ?long, >>> ?list, >>> ?of, >>> ?variable, >>> ?names, >>> ?to, >>> ?get, >>> ?the, >>> ?stuff, >>> ?unpacked, >>> ?into) = function_that_should_return_a_14_tuple() >>> >>> raises >>> >>> ValueError: too many values to unpack >>> >>> Quick, what's the bug? ?Did I forget a variable on the LHS, or is my >>> function returning more things than it should? ?I know it's supposed to be >>> 14, but I don't know which side is wrong. ?Had it said "... expected 13, got >>> 14", I would know immediately. >>> >> >> If the RHS was a tuple or a list, yes you could know immediately. But >> unpacking works with any iterable, so it probably doesn't special-case >> lists and tuples. Iterables don't have a size- they just keep going >> until StopIteration is raised. So in EVERY SINGLE CASE, you would get >> "expected n args, got n+1" even if the iterable would return 24 items >> instead of 14, or would never stop returning items. > > > Not so. ?There could be fewer, in which you could see "expected 13 args, got > 7." > You mean like this? >>> a,b,c = ['a','b'] Traceback (most recent call last): File "", line 1, in ValueError: need more than 2 values to unpack From robertb at trdlnk.com Thu Dec 8 14:46:15 2011 From: robertb at trdlnk.com (Robert Boehne) Date: Thu, 08 Dec 2011 13:46:15 -0600 Subject: Sybase module 0.40pre2 released Message-ID: <4EE11407.1000609@trdlnk.com> WHAT IS IT: The Sybase module provides a Python interface to the Sybase relational database system. It supports all of the Python Database API, version 2.0 with extensions. Please downolad, test and report any problems with the pre-release. ** This version is a pre-release not intended for production use ** The module is available here: http://downloads.sourceforge.net/python-sybase/python-sybase-0.40pre2.tar.gz The module home page is here: http://python-sybase.sourceforge.net/ MAJOR CHANGES SINCE 0.40pre1: Better support for building against Sybase 15 on Windows, UNIX/Linux and 64-bit platforms. MAJOR CHANGES SINCE 0.39: Modify the DateTimeAsPython output conversion to return None when NULL is output support for Python without threads Ignore additional non-error codes from Sybase (1918 and 11932) Use outputmap in bulkcopy mode (thanks to patch by Cyrille Froehlich) Raise exception when opening a cursor on a closed connection Added unit tests Added new exception DeadLockError when Sybase is in a deadlock situation Add command properties CS_STICKY_BINDS and CS_HAVE_BINDS Added support for inputmap in bulkcopy reuse command and cursor when calling cursor.execute with same request Use ct_setparam to define ct_cursor parameters types instead of ct_param implicit conversion for CS_DATE_TYPE in CS_DATETIME_TYPE DataBuf Adding ct_cmd_props wrapper Increase DataBuf maxlength for params of a request when using CS_CHAR_TYPE params so that the buf can be reused BUGS CORRECTED SINCE 0.39: Corrected money type when using CS_MONEY4 (close bug 2615821) Corrected thread locking in ct_cmd_props (thanks to patch by Cyrille Froehlich) Corrected bug in type mapping in callproc (thanks to report by Skip Montanaro) Correct passing None in a DataBuf (thanks to patch by Bram Kuijvenhoven) The full ChangeLog is here: https://python-sybase.svn.sourceforge.net/svnroot/python-sybase/tags/r0_40pre2/ChangeLog From jeanmichel at sequans.com Thu Dec 8 14:49:54 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 08 Dec 2011 20:49:54 +0100 Subject: Misleading error message of the day In-Reply-To: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> Message-ID: <4EE114E2.6070200@sequans.com> Roy Smith wrote: > On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant wrote: > >> string are iterable, considering this, the error is correct. >> > > Yes, I understand that the exception is correct. I'm not saying the exception should be changed, just that we have the opportunity to produce a more useful error message. The exception would be equally correct if it was: > > ValueError: you did something wrong > > but most people would probably agree that it's not the most useful message that could have been produced. > You have to opportunity to not use unpacking anymore :o) There is a recent thread were the dark side of unpacking was exposed. Unpacking is a cool feautre for very small applications but should be avoided whenever possible otherwise. JM From enricong at gmail.com Thu Dec 8 14:56:42 2011 From: enricong at gmail.com (Enrico) Date: Thu, 8 Dec 2011 11:56:42 -0800 (PST) Subject: Numpy ndarray to C array Message-ID: <4ee07a56-50f9-4784-a52e-31553f0d3dc8@v29g2000yqv.googlegroups.com> I am trying to pass a multi-dimensional ndarray to C as a multi- dimensional C array for the purposes of passing it to mathematica. They already have a wrapper for a 1-D Python list. where the list is copied to "list". Shown below: static PyObject * mathlink_PutIntegerList(mathlink_Link *self, PyObject *args) { PyObject* seq; PyObject* obj; long i, len, result; int* list; len = PyObject_Length(seq); list = PyMem_New(int, len); for(i = 0; i < len; i++) { obj = PySequence_GetItem(seq, i); list[i] = PyInt_AsLong(obj); } CheckForThreadsAndRunLink(self,result = MLPutIntegerList(self->lp, list, len)); PyMem_Free(list); CHECKNOTEQUAL(result,MLSUCCESS,self); Py_INCREF(Py_None); return Py_None; } I would like to create a similar wrapper which accepts an ndarray and provides the array laid out in memory like a C array declared explicitly as "int a[m][n]...". I also need to pass the length of the array at each level i as dim[i]. Since this is pretty much the only function I plan to wrap, I'd like to avoid using boost, swig, etc. Any help would be appreciated. From robert.kern at gmail.com Thu Dec 8 14:57:15 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 08 Dec 2011 19:57:15 +0000 Subject: Misleading error message of the day In-Reply-To: References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <4EE0DDE4.4080802@gmail.com> Message-ID: On 12/8/11 4:21 PM, Chris Angelico wrote: > On Fri, Dec 9, 2011 at 2:55 AM, Andrea Crotti wrote: >> Yes but how do you know how many values you generated when it quits? >> I mean I don't know how it work internally, but it should keep a temporary >> list of the yielded values to be able to find out how many values are >> there.. > > Iterator unpacking works roughly thus: > > 1) Count up how many results you need (call that N) > 2) N times, get a value from the iterator. If StopIteration is raised, > swallow it and raise ValueError because there were too few values. > 3) Attempt to get one more value from the iterator. If StopIteration > is NOT raised, raise ValueError because there were too many values. > > At no point is the "total size" of the iterator counted (it could, > after all, be infinite). When ValueError is raised, all that's known > is that StopIteration wasn't raised at the end of the process. unpack_iterable() has the original object available to it, not just the iterator. It could opportunistically check for __len__() and fall back to the less informative message when it is absent. -- 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 From ethan at stoneleaf.us Thu Dec 8 15:09:11 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 08 Dec 2011 12:09:11 -0800 Subject: Misleading error message of the day In-Reply-To: References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> <4EE10B63.2010600@stoneleaf.us> Message-ID: <4EE11967.7050904@stoneleaf.us> Benjamin Kaplan wrote: > On Thu, Dec 8, 2011 at 2:09 PM, Ethan Furman wrote: >> Benjamin Kaplan wrote: >>> If the RHS was a tuple or a list, yes you could know immediately. But >>> unpacking works with any iterable, so it probably doesn't special-case >>> lists and tuples. Iterables don't have a size- they just keep going >>> until StopIteration is raised. So in EVERY SINGLE CASE, you would get >>> "expected n args, got n+1" even if the iterable would return 24 items >>> instead of 14, or would never stop returning items. >> >> Not so. There could be fewer, in which you could see "expected 13 args, got >> 7." >> > > You mean like this? > >>>> a,b,c = ['a','b'] > Traceback (most recent call last): > File "", line 1, in > ValueError: need more than 2 values to unpack This is still not as helpful as this would be: ValueError: need 3 values, received 2 ~Ethan~ From ethan at stoneleaf.us Thu Dec 8 15:13:29 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 08 Dec 2011 12:13:29 -0800 Subject: Misleading error message of the day In-Reply-To: <4EE114E2.6070200@sequans.com> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> Message-ID: <4EE11A69.9060207@stoneleaf.us> Jean-Michel Pichavant wrote: > You have to opportunity to not use unpacking anymore :o) There is a > recent thread were the dark side of unpacking was exposed. Unpacking is > a cool feautre for very small applications but should be avoided > whenever possible otherwise. Which thread was that? ~Ethan~ From einazaki668 at yahoo.com Thu Dec 8 15:16:36 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 8 Dec 2011 12:16:36 -0800 (PST) Subject: Python horks on WinXP path names Message-ID: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> I'm running Python 2.7 on WinXP (ActiveState community version) and when I try to do this: if __name__ == '__main__': root = Tkinter.Tk() root.withdraw() fileNames = tkFileDialog.askopenfilenames() root.destroy() print fileNames # windows filename gets for fileName in fileNames: print fileName file = open(fileName, 'r') for line in file.readlines(): print line.strip() I get this: C:\Documents and Settings\eric\Desktop\PythonShop>python picker.py {C:/Documents and Settings/eric/Desktop/PythonShop/cereal.py} { Traceback (most recent call last): File "picker.py", line 31, in file = open(fileName, 'r') IOError: [Errno 2] No such file or directory: u'{' That is, fileName is in a form that open() doesn't recognize. On a BSD box the code works fine. What's going on with the file name and how do I fix it? TIA, eric From Catherine.M.Moroney at jpl.nasa.gov Thu Dec 8 15:17:11 2011 From: Catherine.M.Moroney at jpl.nasa.gov (Catherine Moroney) Date: Thu, 08 Dec 2011 12:17:11 -0800 Subject: tracking variable value changes Message-ID: Hello, Is there a way to create a C-style pointer in (pure) Python so the following code will reflect the changes to the variable "a" in the dictionary "x"? For example: >>> a = 1.0 >>> b = 2.0 >>> x = {"a":a, "b":b} >>> x {'a': 1.0, 'b': 2.0} >>> a = 100.0 >>> x {'a': 1.0, 'b': 2.0} ## at this point, I would like the value ## associated with the "a" key to be 100.0 ## rather than 1.0 If I make "a" and "b" numpy arrays, then changes that I make to the values of a and b show up in the dictionary x. My understanding is that when I redefine the value of "a", that Python is creating a brand-new float with the value of 100.0, whereas when I use numpy arrays I am merely assigning a new value to the same object. Is there some way to rewrite the code above so the change of "a" from 1.0 to 100.0 is reflected in the dictionary. I would like to use simple datatypes such as floats, rather than numpy arrays or classes. I tried using weakref's, but got the error that a weak reference cannot be created to a float. Catherine From ben+python at benfinney.id.au Thu Dec 8 15:23:42 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 09 Dec 2011 07:23:42 +1100 Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: <87ehwe3j8x.fsf@benfinney.id.au> MRAB writes: > GvR isn't our leader, we are his followers. There's a difference. :-) +1 QotW -- \ ?Guaranteed to work throughout its useful life.? ?packaging for | `\ clockwork toy, Hong Kong | _o__) | Ben Finney From ian.g.kelly at gmail.com Thu Dec 8 15:43:05 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Dec 2011 13:43:05 -0700 Subject: Python horks on WinXP path names In-Reply-To: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> References: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> Message-ID: On Thu, Dec 8, 2011 at 1:16 PM, Eric wrote: > I'm running Python 2.7 on WinXP (ActiveState community version) and > when I try to do this: > > if __name__ == '__main__': > ? ?root = Tkinter.Tk() > ? ?root.withdraw() > ? ?fileNames = tkFileDialog.askopenfilenames() > ? ?root.destroy() > ? ?print fileNames > # windows filename gets > for fileName in fileNames: > ? ?print fileName > ? ?file = open(fileName, 'r') > ? ?for line in file.readlines(): > ? ? ? ? ? ?print line.strip() > > > I get this: > > C:\Documents and Settings\eric\Desktop\PythonShop>python picker.py > {C:/Documents and Settings/eric/Desktop/PythonShop/cereal.py} > { > Traceback (most recent call last): > ?File "picker.py", line 31, in > ? ?file = open(fileName, 'r') > IOError: [Errno 2] No such file or directory: u'{' > > That is, fileName is in a form that open() doesn't recognize. ?On a > BSD box the code works fine. ?What's going on with the file name and > how do I fix it? http://bugs.python.org/issue5712 From malaclypse2 at gmail.com Thu Dec 8 15:45:35 2011 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 8 Dec 2011 15:45:35 -0500 Subject: Python horks on WinXP path names In-Reply-To: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> References: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> Message-ID: On Thu, Dec 8, 2011 at 3:16 PM, Eric wrote: > I'm running Python 2.7 on WinXP (ActiveState community version) and > when I try to do this: > > if __name__ == '__main__': > root = Tkinter.Tk() > root.withdraw() > fileNames = tkFileDialog.askopenfilenames() > root.destroy() > print fileNames > # windows filename gets > for fileName in fileNames: > print fileName > file = open(fileName, 'r') > for line in file.readlines(): > print line.strip() > > > I get this: > > C:\Documents and Settings\eric\Desktop\PythonShop>python picker.py > {C:/Documents and Settings/eric/Desktop/PythonShop/cereal.py} > { > Traceback (most recent call last): > File "picker.py", line 31, in > file = open(fileName, 'r') > IOError: [Errno 2] No such file or directory: u'{' > > That is, fileName is in a form that open() doesn't recognize. On a > BSD box the code works fine. What's going on with the file name and > how do I fix it? > Try replacing your current "print fileNames" line with the follwing: print type(fileNames), repr(fileNames) The behavior you're seeing seems to indicate that fileNames is really a single filename (a single unicode string), rather than a list of file names (a list containing strings). I don't know why that would be, but it would be helpful to confirm that that is, indeed, the problem. I guess you should also double check that the version of the code you're running on windows does, in fact, call "tkFileDialog.askopenfilenames()" rather than "tkFileDialog.askopenfilename()" The two calls are just one letter different, but the first one is supposed to return a list, and the second one is supposed to just return a single string. -------------- next part -------------- An HTML attachment was scrubbed... URL: From einazaki668 at yahoo.com Thu Dec 8 15:56:38 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 8 Dec 2011 12:56:38 -0800 (PST) Subject: Python horks on WinXP path names References: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> Message-ID: <74af070b-78ca-487c-b6dc-fea51847f9dd@q16g2000yqn.googlegroups.com> On Dec 8, 2:43?pm, Ian Kelly wrote: > On Thu, Dec 8, 2011 at 1:16 PM, Eric wrote: > > I'm running Python 2.7 on WinXP (ActiveState community version) and > > when I try to do this: > > > if __name__ == '__main__': > > ? ?root = Tkinter.Tk() > > ? ?root.withdraw() > > ? ?fileNames = tkFileDialog.askopenfilenames() > > ? ?root.destroy() > > ? ?print fileNames > > # windows filename gets > > for fileName in fileNames: > > ? ?print fileName > > ? ?file = open(fileName, 'r') > > ? ?for line in file.readlines(): > > ? ? ? ? ? ?print line.strip() > > > I get this: > > > C:\Documents and Settings\eric\Desktop\PythonShop>python picker.py > > {C:/Documents and Settings/eric/Desktop/PythonShop/cereal.py} > > { > > Traceback (most recent call last): > > ?File "picker.py", line 31, in > > ? ?file = open(fileName, 'r') > > IOError: [Errno 2] No such file or directory: u'{' > > > That is, fileName is in a form that open() doesn't recognize. ?On a > > BSD box the code works fine. ?What's going on with the file name and > > how do I fix it? > > http://bugs.python.org/issue5712 Thanks! eric From alister.ware at ntlworld.com Thu Dec 8 15:58:56 2011 From: alister.ware at ntlworld.com (alister) Date: Thu, 08 Dec 2011 20:58:56 GMT Subject: Misleading error message of the day References: Message-ID: On Thu, 08 Dec 2011 18:10:17 +0000, Grant Edwards wrote: > On 2011-12-08, Roy Smith wrote: >> On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant >> wrote: >>> string are iterable, considering this, the error is correct. >> >> Yes, I understand that the exception is correct. I'm not saying the >> exception should be changed, just that we have the opportunity to >> produce a more useful error message. The exception would be equally >> correct if it was: >> >> ValueError: you did something wrong > > My favorite is still the old classic error from and old Unix printer > port driver: > > "lp0 on fire" > >> but most people would probably agree that it's not the most useful >> message that could have been produced. not as useless as "Keyboard Error press F1 to continue" -- "I once witnessed a long-winded, month-long flamewar over the use of mice vs. trackballs...It was very silly." (By Matt Welsh) From malaclypse2 at gmail.com Thu Dec 8 16:12:14 2011 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 8 Dec 2011 16:12:14 -0500 Subject: tracking variable value changes In-Reply-To: References: Message-ID: On Thu, Dec 8, 2011 at 3:17 PM, Catherine Moroney < Catherine.M.Moroney at jpl.nasa.gov> wrote: > Is there some way to rewrite the code above so the change of "a" from > 1.0 to 100.0 is reflected in the dictionary. I would like to use > simple datatypes such as floats, rather than numpy arrays or classes. > No, there is not. You would need to use a mutable data type, and float is not mutable (neither are ints or strings). -- Jerry -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu Dec 8 16:17:13 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 08:17:13 +1100 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: On Fri, Dec 9, 2011 at 7:58 AM, alister wrote: > not as useless as "Keyboard Error press F1 to continue" If it said "press F1 to ignore" then I would agree. This, however, is more akin to "replace user and strike any key to continue", but more implicit. ChrisA From robert.kern at gmail.com Thu Dec 8 16:31:39 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 08 Dec 2011 21:31:39 +0000 Subject: Numpy ndarray to C array In-Reply-To: <4ee07a56-50f9-4784-a52e-31553f0d3dc8@v29g2000yqv.googlegroups.com> References: <4ee07a56-50f9-4784-a52e-31553f0d3dc8@v29g2000yqv.googlegroups.com> Message-ID: On 12/8/11 7:56 PM, Enrico wrote: > I am trying to pass a multi-dimensional ndarray to C as a multi- > dimensional C array for the purposes of passing it to mathematica. > They already have a wrapper for a 1-D Python list. where the list is > copied to "list". Shown below: > I would like to create a similar wrapper which accepts an ndarray and > provides the array laid out in memory like a C array declared > explicitly as "int a[m][n]...". I also need to pass the length of the > array at each level i as dim[i]. Since this is pretty much the only > function I plan to wrap, I'd like to avoid using boost, swig, etc. You will find it better to ask numpy questions on the numpy mailing list: http://www.scipy.org/Mailing_Lists In this case, you are looking for the PyArray_AsCArray() function: http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PyArray_AsCArray -- 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 From greg.ewing at canterbury.ac.nz Thu Dec 8 16:33:10 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 09 Dec 2011 10:33:10 +1300 Subject: tp_new, tp_alloc, tp_init In-Reply-To: References: Message-ID: <9kcoopFv41U1@mid.individual.net> Michael Hennebry wrote: > I've been reading about writing extension types in C and am rather > fuzzy about the relationship between tp_new, tp_alloc and tp_init. > Most especially, why tp_new? It seems to me that tp_alloc and tp_init > would be sufficient. tp_new and tp_init correspond to the Python methods __new__ and __init__, and they're separated for the same reasons they are in Python. tp_alloc is separate because it allows a type to use a custom memory allocator without disturbing the rest of the initialisation mechanism. -- Greg From greg.ewing at canterbury.ac.nz Thu Dec 8 16:46:48 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 09 Dec 2011 10:46:48 +1300 Subject: How to get a correct entry in the menu for a Python application on Mac OS X In-Reply-To: References: Message-ID: <4EE13048.8090307@canterbury.ac.nz> Detlev Offenbach wrote: > I am fairly new to Mac OS X and would like to know, what I have to do to > make my Python application show the correct name in the menu bar. What > did I do so far. I created an application package containing the .plist > file with correct entries and a shell script, that starts the correct > Python interpreter with the the main script. I don't think that will work, because the executable that your shell script is starting is in an app bundle of its own, and MacOSX will be using the plist from that bundle, which just has the generic "Python" name in it. There are a couple of things you could do: 1) Use py2app to create your app bundle. It does the right things -- not sure exactly what, but it works. 2) Hack things at run time. I use the following PyObjC code in PyGUI to set the application name: from Foundation import NSBundle ns_bundle = NSBundle.mainBundle() ns_info = ns_bundle.localizedInfoDictionary() if not ns_info: ns_info = ns_bundle.infoDictionary() ns_info['CFBundleName'] = my_application_name -- Greg From ian.g.kelly at gmail.com Thu Dec 8 16:50:38 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Dec 2011 14:50:38 -0700 Subject: tracking variable value changes In-Reply-To: References: Message-ID: On Thu, Dec 8, 2011 at 1:17 PM, Catherine Moroney wrote: > Hello, > > Is there a way to create a C-style pointer in (pure) Python so the following > code will reflect the changes to the variable "a" in the > dictionary "x"? > > For example: > >>>> a = 1.0 >>>> b = 2.0 >>>> x = {"a":a, "b":b} >>>> x > {'a': 1.0, 'b': 2.0} >>>> a = 100.0 >>>> x > {'a': 1.0, 'b': 2.0} ? ## at this point, I would like the value > ? ? ? ? ? ? ? ? ? ? ? ## associated with the "a" key to be 100.0 > ? ? ? ? ? ? ? ? ? ? ? ## rather than 1.0 > > If I make "a" and "b" numpy arrays, then changes that I make to the values > of a and b show up in the dictionary x. > > My understanding is that when I redefine the value of "a", that Python > is creating a brand-new float with the value of 100.0, whereas when I use > numpy arrays I am merely assigning a new value to the same object. Sort of. In the code above, you are binding a and x["a"] to the same float object. Then when you do "a = 100.0", you are rebinding a but not x["a"]. In the case of arrays it's the same story, except that you can also *modify* the contents of the array instead of rebinding to a new array. In that case both a and x["a"] are still bound to the original array, the contents of which have changed. You can get the same effect with a float by putting it in a container object and binding both variables to the same container objects rather than to the float directly. Then, to change the value, change the contents of the container object. What you use as a container object is up to you. Some use a 1-element list, although I find that ugly. From ben+python at benfinney.id.au Thu Dec 8 17:06:00 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 09 Dec 2011 09:06:00 +1100 Subject: tracking variable value changes References: Message-ID: <87aa723eif.fsf@benfinney.id.au> Catherine Moroney writes: > Is there a way to create a C-style pointer in (pure) Python so the > following code will reflect the changes to the variable "a" in the > dictionary "x"? No, Python doesn't do pointers. Rather, objects have references and that's how the program accesses the objects. > For example: > > >>> a = 1.0 > >>> b = 2.0 > >>> x = {"a":a, "b":b} > >>> x > {'a': 1.0, 'b': 2.0} > >>> a = 100.0 > >>> x > {'a': 1.0, 'b': 2.0} ## at this point, I would like the value > ## associated with the "a" key to be 100.0 > ## rather than 1.0 You might like that, but it's just not how Python works. Python doesn't have C-style pointers. Python also doesn't have variables (even though the documentation uses that term; IMO it's a mistake, and leads to confusion similar to this). What Python has are references to objects. One kind of reference is a name; another kind of reference is a value in a dictionary. The assignment operator ?=? is the binding operator. It binds the reference on the left side to the object on the right side. > If I make "a" and "b" numpy arrays, then changes that I make to the > values of a and b show up in the dictionary x. Yes, because the objects are mutable; you can change them and existing references are still referring to the same object. They don't ?show up in the dictionary?; the dictionary item is just referring to the same object it did before you made the change. > My understanding is that when I redefine the value of "a" Please think of it, instead, as re-binding the name ?a? to a new value. > that Python is creating a brand-new float with the value of 100.0, Yes (or at least that's the abstraction being presented to you; it may not be implemented exactly that way, but it's sufficient that we Python programmers treat it that way). > whereas when I use numpy arrays I am merely assigning a new value to > the same object. No, you're modifying the object. A numpy array itself contains references. By altering one of the elements in an array, you are re-binding one of its references to a different number. > Is there some way to rewrite the code above so the change of "a" from > 1.0 to 100.0 is reflected in the dictionary. I would like to use > simple datatypes such as floats, rather than numpy arrays or classes. Please follow the Python tutorial from beginning to end. Not just read, but do it: work through the exercises to understand what each one is teaching you. That will give you a firm grounding in Python's data model, including mutable and immutable types, references and binding. Do bear in mind what I said above, though, about ?variable? being a misleading term, and ignore its implications from the C language. -- \ ?Good judgement comes from experience. Experience comes from | `\ bad judgement.? ?Frederick P. Brooks | _o__) | Ben Finney From arnodel at gmail.com Thu Dec 8 17:10:01 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Thu, 8 Dec 2011 22:10:01 +0000 Subject: tracking variable value changes In-Reply-To: References: Message-ID: On 8 December 2011 21:50, Ian Kelly wrote: > You can get the same effect with a float by putting it in a container > object and binding both variables to the same container objects rather > than to the float directly. ?Then, to change the value, change the > contents of the container object. ?What you use as a container object > is up to you. ?Some use a 1-element list, although I find that ugly. This kind of trick is not often necessary anyway. It may be a sign that there is a better approach to the problem that the OP is trying to solve. -- Arnaud From frank at am-knie.de Thu Dec 8 17:41:35 2011 From: frank at am-knie.de (Frank van den Boom) Date: Thu, 08 Dec 2011 23:41:35 +0100 Subject: subprocess.Popen under windows 7 Message-ID: Hello, i have something like this under windows 7: print("try command...") arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r", "-p",PASSWORD] p = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=srcdir) output, error = p.communicate() if output: print output print ("Eyerthing is good") press_any_key_to_continue() The script works, but there is a little problem. When I double-click the python file, then the command line will open and the script starts. I can read "try command..." in the command line window under windows 7. But then I have to enter the return key in order that the script will go on. After I had entered the return key the script completed sucessfully and I saw the output. What can I do, to prevent pressing the return key? Thanks. From ethan at stoneleaf.us Thu Dec 8 18:34:13 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 08 Dec 2011 15:34:13 -0800 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <4EE14975.40202@stoneleaf.us> Chris Angelico wrote: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I was testing this out in > IDLE, and the fly help for the function no longer gave useful info > about its argument list. If you want to roll your own, it looks something like this: 2.x code ---------------------------------------------------------------------- def trace(func, debugmode=debugmode): if debugmode: name = func.__name__ argspec = inspect.getargspec(func) signature = inspect.formatargspec( formatvalue=lambda val: "", *argspec)[1:-1] # trim parens new_func = ( 'def _wrapper_(%(signature)s):\n' ' print(">"+func.__name__)\n' ' result = func(%(signature)s)\n' ' print("<"+func.__name__)\n' ' return result\n' % {'signature':signature} ) evaldict = {'func':func} exec new_func in evaldict wrapped = evaldict['_wrapper_'] wrapped.__name__ = name wrapped.__doc__ = func.__doc__ wrapped.__module__ = func.__module__ wrapped.__dict__ = func.__dict__ wrapped.func_defaults = func.func_defaults return wrapped return func ---------------------------------------------------------------------- The downside (which you get even with Michele's decorator module) is that tracebacks aren't quite as clean. ~Ethan~ From ethan at stoneleaf.us Thu Dec 8 18:56:58 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 08 Dec 2011 15:56:58 -0800 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <4EE14ECA.8020801@stoneleaf.us> Chris Angelico wrote: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I was testing this out in > IDLE, and the fly help for the function no longer gave useful info > about its argument list. And here it is with Michele's decorator module: 2.x code (probably the same in 3.x, but haven't tested) ---------------------------------------------------------------- from decorator import decorator debugmode = True def trace(func): if debugmode: @decorator def traced(func, *a, **ka): print(">", func.__name__, a, ka) result = func(*a, **ka) print("<", func.__name__) return result return traced(func) return func @trace def test(x): "a simple test" print("Test! "+x) return 5 ---------------------------------------------------------------- ~Ethan~ From roy at panix.com Thu Dec 8 20:19:50 2011 From: roy at panix.com (Roy Smith) Date: Thu, 08 Dec 2011 20:19:50 -0500 Subject: Misleading error message of the day References: Message-ID: In article , Grant Edwards wrote: > On 2011-12-08, Roy Smith wrote: > > On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant > > wrote: > >> string are iterable, considering this, the error is correct. > > > > Yes, I understand that the exception is correct. I'm not saying the > > exception should be changed, just that we have the opportunity to produce a > > more useful error message. The exception would be equally correct if it > > was: > > > > ValueError: you did something wrong > > My favorite is still the old classic error from and old Unix printer > port driver: > > "lp0 on fire" Well, if you're going to go there, ed had (and probably still does) have but a single all-purpose error message: "?". The old v6 unix chess program was somewhat more verbose. It said, "eh?" (unless I'm mixing that up with something else). From ian at excess.org Thu Dec 8 20:43:57 2011 From: ian at excess.org (Ian Ward) Date: Thu, 08 Dec 2011 20:43:57 -0500 Subject: ANN: Speedometer 2.4 - bandwidth and download monitor Message-ID: <4EE167DD.6050209@excess.org> Announcing Speedometer 2.8 -------------------------- Speedometer home page: http://excess.org/speedometer/ Download: http://excess.org/speedometer/speedometer-2.8.tar.gz New in this release: ==================== - Added a linear scale option: -l. Best used in combination with -m (and possibly -n) to customize the range to be displayed. Thanks to jukie.net for sponsoring this feature. - Replace silly "curved" reading with a weighted moving average - New option to display all values in bits per second: -s - New options to set minimum (-n) and maximum (-m) values displayed on the graphs in bytes/s. Defaults are -n 32 and -m 2**32 - Accept shortened versions of -rx and -tx: -r and -t My intent is to drop use of the original forms in 3.0 and add --long-versions of all options - Use IEC notation of sizes MiB, GiB etc. - Install script as both speedometer.py and speedometer About Speedometer ================= Speedometer is a console bandwidth and file download progress monitor with a linear/logarithmic bandwidth display and a simple command-line interface. Speedometer requires Urwid for full-console bar graph display. Urwid may be downloaded from: http://excess.org/urwid/ Speedometer is released under the GNU LGPL. From lie.1296 at gmail.com Thu Dec 8 20:46:15 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 09 Dec 2011 12:46:15 +1100 Subject: Misleading error message of the day In-Reply-To: <4EE11A69.9060207@stoneleaf.us> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> Message-ID: On 12/09/2011 07:13 AM, Ethan Furman wrote: > Jean-Michel Pichavant wrote: >> You have to opportunity to not use unpacking anymore :o) There is a >> recent thread were the dark side of unpacking was exposed. Unpacking >> is a cool feautre for very small applications but should be avoided >> whenever possible otherwise. > > Which thread was that? perhaps the one that talks about `a, a.foo = 1, 2` blowing up? From steve+comp.lang.python at pearwood.info Thu Dec 8 21:07:30 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Dec 2011 02:07:30 GMT Subject: Misleading error message of the day References: Message-ID: <4ee16d61$0$29977$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Dec 2011 18:10:17 +0000, Grant Edwards wrote: > On 2011-12-08, Roy Smith wrote: >> On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant >> wrote: >>> string are iterable, considering this, the error is correct. >> >> Yes, I understand that the exception is correct. I'm not saying the >> exception should be changed, just that we have the opportunity to >> produce a more useful error message. The exception would be equally >> correct if it was: >> >> ValueError: you did something wrong > > My favorite is still the old classic error from and old Unix printer > port driver: > > "lp0 on fire" > >> but most people would probably agree that it's not the most useful >> message that could have been produced. I forget where I saw this, but somebody took a screen shot of an error message from a GUI application that said something like: A fatal error occurred: no error and then aborted the app. -- Steven From lie.1296 at gmail.com Thu Dec 8 21:08:22 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 09 Dec 2011 13:08:22 +1100 Subject: subprocess.Popen under windows 7 In-Reply-To: References: Message-ID: On 12/09/2011 09:41 AM, Frank van den Boom wrote: > What can I do, to prevent pressing the return key? I didn't have Windows 7 right now, but that shouldn't happen with the code you've given; when trimming code for posting, you should check that the trimmed code still have the exact same problem. From tjreedy at udel.edu Thu Dec 8 21:33:24 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Dec 2011 21:33:24 -0500 Subject: adding elements to set In-Reply-To: References: <4EE0E72E.2040104@gmail.com> Message-ID: On 12/8/2011 1:54 PM, Duncan Booth wrote: > Yes, the documentation describes this although I don't think anything > highlights that it is a change from Python 2.x: > > [http://docs.python.org/py3k/reference/datamodel.html] The Python 3 docs are 're-based' on 3.0, with change notes going forward from that new base. This change should have been in 'What's New in Python 3.0', though I did not find it. -- Terry Jan Reedy From miki.tebeka at gmail.com Thu Dec 8 22:25:34 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 8 Dec 2011 19:25:34 -0800 (PST) Subject: [OT] Book authoring Message-ID: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> Greetings, Any recommendations for a book authoring system that supports the following: 1. Code examples (with syntax highlighting and line numbers) 2. Output HTML, PDF, ePub ... 3. Automatic TOC and index 4. Search (in HTML) - this is a "nice to have" Can I somehow use Sphinx? Thanks, -- Miki From rosuav at gmail.com Thu Dec 8 22:46:45 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 14:46:45 +1100 Subject: Misleading error message of the day In-Reply-To: <4ee16d61$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <4ee16d61$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 9, 2011 at 1:07 PM, Steven D'Aprano wrote: > I forget where I saw this, but somebody took a screen shot of an error > message from a GUI application that said something like: > > A fatal error occurred: no error > > and then aborted the app. An errant error! Sounds like the stuff that happens here... http://thedailywtf.com/Series/Error_0x27_d.aspx This is getting quite off-topic though. ChrisA From roy at panix.com Thu Dec 8 23:44:40 2011 From: roy at panix.com (Roy Smith) Date: Thu, 08 Dec 2011 23:44:40 -0500 Subject: Misleading error message of the day References: <4ee16d61$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Chris Angelico wrote: > http://thedailywtf.com/Series/Error_0x27_d.aspx > > This is getting quite off-topic though. Getting off-topic, perhaps, but your comment really does bring some closure. When I was pondering the original, "too many values to unpack" message, I did indeed utter a few WTFs :-) From wuwei23 at gmail.com Thu Dec 8 23:57:16 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 8 Dec 2011 20:57:16 -0800 (PST) Subject: Misleading error message of the day References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> Message-ID: <36bf64b8-d27f-4f9d-9cf7-c425d4b1d214@q27g2000prh.googlegroups.com> On Dec 9, 11:46?am, Lie Ryan wrote: > perhaps the one that talks about `a, a.foo = 1, 2` blowing up? Are you sure you're not confusing this with the recent thread on 'x = x.thing = 1'? From wuwei23 at gmail.com Fri Dec 9 00:02:44 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 8 Dec 2011 21:02:44 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> On Dec 9, 2:38?am, Chris Angelico wrote: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I was testing this out in > IDLE, and the fly help for the function no longer gave useful info > about its argument list. The 3rd party 'decorator' module takes care of issues like docstrings & function signatures. I'd really like to see some of that functionality in the stdlib though. From ulrich.eckhardt at dominolaser.com Fri Dec 9 03:32:00 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 09 Dec 2011 09:32:00 +0100 Subject: subprocess.Popen under windows 7 In-Reply-To: References: Message-ID: <1lt8r8-rel.ln1@satorlaser.homedns.org> Am 08.12.2011 23:41, schrieb Frank van den Boom: > arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r", > "-p",PASSWORD] The "*" is resolved by the shell, this is not a wildcard that gets passed to the program. At least not normally, your case might be different. > if output: > print output > > print ("Eyerthing is good") Is that Python 2 or 3? That said, if you reduced it to something that doesn't e.g. require 7zip I'd happily run it on an XP system with Python 2.7 to tell you if it works there or not. Doing so would also rule out any influence by 7zip, just in case. Uli From frank at am-knie.de Fri Dec 9 03:42:21 2011 From: frank at am-knie.de (Frank van den Boom) Date: Fri, 09 Dec 2011 09:42:21 +0100 Subject: subprocess.Popen under windows 7 In-Reply-To: References: Message-ID: > > I didn't have Windows 7 right now, but that shouldn't happen with the > code you've given; when trimming code for posting, you should check that > the trimmed code still have the exact same problem. > Here is the hole code: #!/usr/bin/env python # little script to backup recursive a folder with 7zip SOURCE_DIR = "C:/Users/yoicks/Desktop/source" DEST_DIR = "C:/Users/yoicks/Desktop/dest" BACKUP_NAME_PREFIX = "BACKUP" BACKUP_NAME_DELIMITER = "_" METHOD = '7zip' PATH_TO_7ZIP = "C:/Program Files/7-Zip/7z.exe" PASSWORD = "1234" import os, time, shutil, sys, tarfile, subprocess, traceback try: # win32 from msvcrt import getch except ImportError: # unix def getch(): import sys, tty, termios fd = sys.stdin.fileno() old = termios.tcgetattr(fd) try: tty.setraw(fd) return sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old) def press_any_key(): print "Press any key to continue." getch() def exit_with_string(exit_string): print exit_string press_any_key() sys.exit(exit_string) def backup_directory_7zip(srcdir,archive_name): if os.path.exists(archive_name): exit_stop("backup path %s already exists!" % arcpath) try: # see 7zip help arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r", "-p",PASSWORD] print ("try running cmd:\n %s\nin directory\n %s" % (' '.join(arglist),srcdir)) # join because i don't want [ ] sp = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=srcdir) #output, error = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=srcdir).communicate() except: print "Error while running 7zip subprocess.\n" print "Traceback:\n%s"%traceback.format_exc() return False output, error = sp.communicate() #something i tried output = sp.stdout.read() #somtehing i tried error = sp.stderr.read() if output: print output if error: print error return False return archive_name # build backup name print "start backup with python-script...\n" timestr = time.strftime("%Y%m%d_%H%M%S",time.localtime()) if METHOD not in ["7zip"]: exit_stop("METHOD not '7zip'") if not os.path.exists(SOURCE_DIR): exit_stop("SOURCE_DIR: %s doesn't exists" % os.path.abspath(SOURCE_DIR)) if not os.path.exists(DEST_DIR): exit_stop("DEST_DIR: %s doesn't exists" % os.path.abspath(DEST_DIR)) else: print("write backup from %s to %s \n using the %s method...\n" % (os.path.abspath(SOURCE_DIR), os.path.abspath(DEST_DIR), METHOD)) if METHOD == "7zip": try: if not os.path.exists(PATH_TO_7ZIP): exit_stop("Path to 7ZIP %s doesn't exist." % PATH_TO_7ZIP) except NameError: exit_stop("variable PATH_TO_7ZIP not defined") return_value = backup_directory_7zip(srcdir=os.path.abspath(SOURCE_DIR), archive_name=os.path.abspath(os.path.join( DEST_DIR, BACKUP_NAME_PREFIX + BACKUP_NAME_DELIMITER + timestr + ".exe"))) if return_value: print("Backup successfully written.") else: print("FAILURE during the backup") press_any_key() From ladasky at my-deja.com Fri Dec 9 04:10:48 2011 From: ladasky at my-deja.com (John Ladasky) Date: Fri, 9 Dec 2011 01:10:48 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> Message-ID: <9d24a287-1e24-456d-9b3f-73ca272618e3@u10g2000prl.googlegroups.com> Thanks once again to everyone for their recommendations, here's a follow-up. In summary, I'm still baffled. I tried ipython, as Marco Nawijn suggested. If there is some special setting which returns control to the interpreter when a subprocess crashes, I haven't found it yet. Yes, I'm RTFM. As with SciTE, everything just hangs. So I went back to SciTE for now. And I'm doing what Terry Reedy suggested -- I am editing multiprocess.Pool in place. I made a backup, of course. I am using sudo to run SciTE so that I can edit the system files, and not have to worry about chasing path and import statement problems. What I have found, so far, is no evidence that a string is needed in any of the code. What's the task variable? It's a deeply-nested tuple, containing no strings, not even in the WORKING code. This makes me wonder whether that traceback is truly complete. I wrote a routine to display the contents of task, immediately before the offending put(). Here's a breakdown. In the WORKING version: task: 0 0 (see below) {} task[3]: (see below) task[3][0]: (see below) task[3][0][1]: (see below) task[3][0][1][0]: net shape=(2, 3) inp shape=(307, 2) tgt shape=(307, 2) By watching this run, I've learned that task[0] and task[1] are counters for groups of subprocesses and individual subprocesses, respectively. Suppose we have four subprocesses. When everything is working, task[:2] = [0,0] for the first call, then [0,1], [0,2], [0,3]; then, [1,0], [1,1], [1,2], etc. task[2] points to multiprocessing.Pool.mapstar, a one-line function that I never modify. task[4] is an empty dictionary. So it looks like everything that I provide appears in task[3]. task[3] is just a tuple inside a tuple (which is weird). task[3][0] contains the function to be called (in this case, my function, mean_square_error), and then a tuple containing all of the arguments to be passed to that function. The docs say that the function in question must be defined at the top level of the code so that it's importable (it is), and that all the arguments to be sent to that function will be wrapped up in a single tuple -- that is presumably task[3][0][1]. But that presumption is wrong. I wrote a function which creates a collections.namedtuple object of the type SplitData, which contains the function's arguments. It's not task[3][0][1] itself, but the tuple INSIDE it, namely task[3][0][1][0]. More weirdness. You don't need to worry about task[3][0][1][0], other than to note that these are my neural network objects, they are intact, they are the classes I expect, and they are named as I expect -- and that there are NO STRING objects. Now, are there any differences between the working version of my code and the buggy version? Other than a few trivial name changes that I made deliberately, the structure of task looks the SAME... task: 0 0 (see below) {} task[3]: (see below) task[3][0]: (see below) task[3][0][1]: (see below) task[3][0][1][0]: func shape=(2, 3) inp shape=(307, 2) tgt shape=(307, 2) Again, all the action is in task[3]. I was worried about the empty dictionary in task[4] at first, but I've seen this {} in the working program, too. I'm not sure what it does. For completeness, here's mean_square_error() from the working program: def mean_square_error(b): out = array([b.net(i) for i in b.inp]) return sum((out-b.tgt)**2) And, here's error() from the buggy program. def error(b): out = array([b.func(i) for i in b.inp]) return sum((out-b.tgt)**2) I renamed mean_square_error(), because I realized that the mean-square error is the only kind of error I'll ever be computing. I also renamed "net" to "func", in SplitData, reflecting the more general nature of the Cascade class I'm developing. So I mirror that name change here. Other than that, I trust you can see that error() and mean_square_error() are identical. I can call mean_square_error directly with a SplitData tuple and it works. I can call error directly with a SplitData tuple in the broken program, and it ALSO works. I'm only having problems when I try to submit the job through Pool. I tried putting a print trap in error(). When I use Pool then error() never gets called. I suppose that the logical next step is to compare the two Pool instances... onward... :^P From mail at timgolden.me.uk Fri Dec 9 04:28:07 2011 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 09 Dec 2011 09:28:07 +0000 Subject: subprocess.Popen under windows 7 In-Reply-To: <1lt8r8-rel.ln1@satorlaser.homedns.org> References: <1lt8r8-rel.ln1@satorlaser.homedns.org> Message-ID: <4EE1D4A7.2050401@timgolden.me.uk> On 09/12/2011 08:32, Ulrich Eckhardt wrote: > Am 08.12.2011 23:41, schrieb Frank van den Boom: >> arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r", >> "-p",PASSWORD] > > The "*" is resolved by the shell, this is not a wildcard that gets > passed to the program. At least not normally, your case might be different. "... not normally" == "... not on Unix". On Windows, the shell doesn't do any wildcard expansion. The OP is asking about behaviour on Windows 7. TJG From frank at am-knie.de Fri Dec 9 04:47:14 2011 From: frank at am-knie.de (Frank van den Boom) Date: Fri, 09 Dec 2011 10:47:14 +0100 Subject: subprocess.Popen under windows 7 In-Reply-To: References: <1lt8r8-rel.ln1@satorlaser.homedns.org> Message-ID: Thank you very much. Now I have written a little c++ programm which produces some ouput. And now it works fine. There is something wrong with 7zip.exe and the arglist with *. Tonight I will go on and hunt the error. It should be Python 2.7 #!/usr/bin/env python PATH_TO_EXE = "C:/Users/yoicks/Desktop/ausgabe.exe" import os, shutil, sys, subprocess, traceback try: # win32 from msvcrt import getch except ImportError: # unix def getch(): import sys, tty, termios fd = sys.stdin.fileno() old = termios.tcgetattr(fd) try: tty.setraw(fd) return sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old) def press_any_key(): print "Press any key to continue." getch() def exit_with_string(exit_string): print exit_string press_any_key() sys.exit(exit_string) def start_exe (PATH_TO_EXE): try: arglist = [PATH_TO_EXE] print ("try running cmd:\n %s\n" % (' '.join(arglist))) sp = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) except: print "Error while running subprocess.\n" print "Traceback:\n%s"%traceback.format_exc() return False output, error = sp.communicate() if output: print output if error: print error return False return True return_value = start_exe(PATH_TO_EXE) if return_value: print("Backup successfully written.") else: print("FAILURE during the backup") press_any_key() * Englisch - erkannt * Englisch * Deutsch * Englisch * Deutsch From massi_srb at msn.com Fri Dec 9 04:55:28 2011 From: massi_srb at msn.com (Massi) Date: Fri, 9 Dec 2011 01:55:28 -0800 (PST) Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Thank you all for your replies, first of all my Sum function was an example simplifying what I have to do in my real funciton. In general the D dictionary is complex, with a lot of keys, so I was searching for a quick method to access all the variables in it without doing the explicit creation: a, b, c = D['a'], D['b'], D['c'] and without using directly the D dictionary (boring...). When I talked about nested function I meant both cases Chris, but this is not a really tighten bound. I tried to follow the hints of Chris together with some help by google and used the following code: for k in D : exec "%s = D[k]" %k That seems to do the trick, but someone speaks about "dirty code", can anyone point me out which problems this can generate? Again, thank you for your help! From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Fri Dec 9 04:57:59 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Fri, 09 Dec 2011 10:57:59 +0100 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: Am 08.12.2011 12:43 schrieb Chris Angelico: > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: >> I am still perplexed about decorators though, am happily using Python for >> many years without them, but maybe i am missing something? >> For example in the above case, if I want the names attached to each other >> with a comma, why wouldn't I just create a function doing exactly this? Why >> would I first write a single name generator and then decorate it so that I >> never can get single names anymore (this is the case, isn't it? Once >> decorated, I can not get the original behaviour of the function anymore. > > The example given is a toy. It's hardly useful. Right. It was supposed to be an example. In my case, I work with a script used to build a XML file. I change this script from time to time in order to match the requirements. Here I find it useful just to add some more yield statements for adding entries. But now that I think again about it, it's more an example for generators, not so much for decorators - which I like as well. ***** But some useful examples for decorators include 1. "Threadifying" a function, i.e. turning it into a Thread object, or into a callable which in turn starts a thread according to the given parameters. 2. Automatically calling a function if the given module is executed as a script, a kind of replacement for the "if __name__ == '__main__':" stuff. 3. Meta decorators: I find it annoying to have to wrap the function given to the decorator into another one, modifying its properties and returning that in turn. def wrapfunction(decorated): """Wrap a function taking (f, *a, **k) and replace it with a function taking (f) and returning a function taking (*a, **k) which calls our decorated function. """ from functools import wraps @wraps(decorated) def wrapped_outer(f): @wraps(f) def wrapped_inner(*a, **k): return decorated(f, *a, **k) return wrapped_inner return wrapped_outer makes it much easier to create decorators which just wrap a function into another, extending its funtionality: @wrapfunction def return_list(f, *a, **k) return list(f(*a, **k)) is much easier and IMHO much better to read than def return_list(f): """Wrap a function taking (f, *a, **k) and replace it with a function taking (f) and returning a function taking (*a, **k) which calls our decorated function. """ from functools import wraps @wraps(f) def wrapped(*a, **k): return list(f, *a, **k) return wrapped - especially if used multiple times. 3a. This is a modified case of my first example: If you want a function to assemble and return a list instead of a generator object, but prefer "yield" over "ret=[]; ret.append();...", you can do that with this @return_list. 4. So-called "indirect decorators": @spam(eggs) def foo(bar): pass are as well quite tricky to build when taking def indirdeco(ind): from functools import update_wrapper, wraps upd=wraps(ind) # outer wrapper: replaces a call with *a, **k with an updated # lambda, getting the function to be wrapped and applying it and # *a, **k to ind. outerwrapper=lambda *a, **k: upd(lambda f: ind(f, *a, **k)) # We update this as well: return upd(outerwrapper) # We don't update f nor the result of ind() - it is the callee's # business. It is kind of reverse to 3. @indirdeco def addingdeco(f, offset): return lambda *a, **k: f(*a, **k) + offset # Here should maybe be wrapped - it is just supposed to be an # example. 5. Creating a __all__ for a module. Instead of maintaining it somewhere centrally, you can take a class AllList(list): """list which can be called in order to be used as a __all__-adding decorator""" def __call__(self, obj): """for decorators""" self.append(obj.__name__) return obj , do a __all__ = AllList() and subsequently decorate each function with @__all__ 6. Re-use a generator: A generator object is creted upon calling the generator function with parameters and can be used only once. A object wrapping this generator might be useful. # Turn a generator into a iterable object calling the generator. class GeneratorIterable(object): """Take a parameterless generator function and call it on every iteration.""" def __init__(self, gen): # Set object attribute. self.gen = gen def __iter__(self): # Class attribute calls object attribute in order to keep # namespace variety small. return self.gen() @GeneratorIterable def mygen(): yield 1 yield 2 list(mygen) -> [1, 2] list(mygen) -> [1, 2] # again, without the () Might be useful if the object is to be transferred to somewhere else. ***** Some of these decorators are more useful, some less if seen standalone, but very handy if creating other decorators. HTH nevertheless, Thomas From jeanmichel at sequans.com Fri Dec 9 05:03:53 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 09 Dec 2011 11:03:53 +0100 Subject: Misleading error message of the day In-Reply-To: <4EE11A69.9060207@stoneleaf.us> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> Message-ID: <4EE1DD09.4030707@sequans.com> Ethan Furman wrote: > Jean-Michel Pichavant wrote: >> You have to opportunity to not use unpacking anymore :o) There is a >> recent thread were the dark side of unpacking was exposed. Unpacking >> is a cool feautre for very small applications but should be avoided >> whenever possible otherwise. > > Which thread was that? > > > ~Ethan~ "A tuple in order to pass returned values ?" was the thread. JM From robert.kern at gmail.com Fri Dec 9 05:08:08 2011 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 09 Dec 2011 10:08:08 +0000 Subject: I love the decorator in Python!!! In-Reply-To: <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> Message-ID: On 12/9/11 5:02 AM, alex23 wrote: > On Dec 9, 2:38 am, Chris Angelico wrote: >> One piece of sophistication that I would rather like to see, but don't >> know how to do. Instead of *args,**kwargs, is it possible to somehow >> copy in the function's actual signature? I was testing this out in >> IDLE, and the fly help for the function no longer gave useful info >> about its argument list. > > The 3rd party 'decorator' module takes care of issues like docstrings > & function signatures. I'd really like to see some of that > functionality in the stdlib though. Much of it is: http://docs.python.org/library/functools#functools.update_wrapper -- 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 From steve+comp.lang.python at pearwood.info Fri Dec 9 06:27:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Dec 2011 11:27:16 GMT Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> Message-ID: <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Dec 2011 10:30:01 +0100, Hrvoje Niksic wrote: > In a language like Python, the difference between O(1) and O(log n) is > not the primary reason why programmers use dict; they use it because > it's built-in, efficient compared to alternatives, and convenient to > use. If Python dict had been originally implemented as a tree, I'm sure > it would be just as popular. Except for people who needed dicts with tens of millions of items. Remember also that dicts are used for looking up names in Python. Nearly all method calls, attribute accesses, global name lookups, function calls, etc. go through at least one and potentially multiple dict lookups. The simple statement: n = len(x.y) + len(z) likely requires nine dict lookups, and potentially more. In even a small application, there could be tens of millions of dict lookups; changing each of them from O(1) to O(log N) could result in a measurable slowdown to Python code in real applications. That is why dicts are highly optimized for speed. As fast as dicts are, sometimes they aren't fast enough. One common micro- optimization for tight loops and time-critical code is to create local variables from globals or builtins, because local variable access bypasses dict lookup. So people would notice if dicts were slower. -- Steven From __peter__ at web.de Fri Dec 9 06:27:47 2011 From: __peter__ at web.de (Peter Otten) Date: Fri, 09 Dec 2011 12:27:47 +0100 Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Message-ID: Massi wrote: > for k in D : exec "%s = D[k]" %k > > That seems to do the trick, but someone speaks about "dirty code", can > anyone point me out which problems this can generate? exec can run arbitrary code, so everybody reading the above has to go back to the definition of D to verify that it can only contain "safe" keys. Filling D with user-input is right out because a malicious user could do anything he likes. Here's a harmless demo that creates a file: >>> d = {"x = 42\nwith open('tmp.txt', 'w') as f:\n f.write('whatever')\nx": 123} >>> for k in d: exec "%s = d[k]" % k ... >>> x 123 >>> open("tmp.txt").read() 'whatever' From steve+comp.lang.python at pearwood.info Fri Dec 9 06:59:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Dec 2011 11:59:16 GMT Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Message-ID: <4ee1f814$0$29977$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Dec 2011 01:55:28 -0800, Massi wrote: > for k in D : exec "%s = D[k]" %k > > That seems to do the trick, but someone speaks about "dirty code", can > anyone point me out which problems this can generate? Again, thank you > for your help! Just the second-most common source of viruses, malware and security vulnerabilities (behind buffer overflows): code injection attacks. Code injection attacks make up at least three of the top 25 security vulnerabilities on the CWE/SANS list: http://cwe.mitre.org/top25/index.html including the top 2 most dangerous threats (beating even our old friend, the buffer overflow): SQL injection and OS command injection. Your use of exec is vulnerable to attack if a hostile user can fool you into using a dict like this one: D = {'a': '42', 'import os;'\ ' os.system("""echo "ha ha i ownz ur system rm-rf/" """); b': '23', } for k in D : exec "%s = D[k]" % k You might think you're safe from such attacks, but (1) it is MUCH harder to protect against them than you might think; and (2) code has a habit of being re-used. Today your application might only be used by you; next week your code might find itself embedded in a web-application where hostile script kiddies can destroy your server with a single upload. My advice is: (1) If you need to ask why exec is dangerous, you shouldn't touch it. (2) If you're sure you can protect against code injection, you can't. (3) If you think you need exec, you probably don't. (4) If you think you can make exec safe with a prohibited list of dangerous strings, you probably can't. -- Steven From bieffe62 at gmail.com Fri Dec 9 07:00:06 2011 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Fri, 9 Dec 2011 04:00:06 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <8e5b46e4-86a9-4192-ac99-7872d08677fc@x7g2000yqb.googlegroups.com> On 8 Dic, 12:22, K.-Michael Aye wrote: > On 2011-12-08 08:59:26 +0000, Thomas Rachel said: > > > > > Am 08.12.2011 08:18 schrieb 88888 Dihedral: > >> I use the @ decorator to behave exactly like a c macro that > >> does have fewer side effects. > > >> I am wondering is there other interesting methods to do the > >> jobs in Python? > > > In combination with a generator, you can do many funny things. > > > For example, you can build up a string: > > > def mkstring(f): > > ? ? ?"""Turns a string generator into a string, > > ? ? ?joining with ", ". > > ? ? ?""" > > ? ? ?return ", ".join(f()) > > > def create_answer(): > > ? ? ?@mkstring > > ? ? ?def people(): > > ? ? ? ? ?yield "Anna" > > ? ? ? ? ?yield "John" > > ? ? ? ? ?yield "Theo" > > > ? ? ?return "The following people were here: " + people > > > Many other things are thinkable... > > > Thomas > > I am still perplexed about decorators though, am happily using Python > for many years without them, but maybe i am missing something? > For example in the above case, if I want the names attached to each > other with a comma, why wouldn't I just create a function doing exactly > this? Why would I first write a single name generator and then decorate > it so that I never can get single names anymore (this is the case, > isn't it? Once decorated, I can not get the original behaviour of the > function anymore. > So, above, why not > def mkstring(mylist): > with the same function declaration and then just call it with a list of > names that I generate elsewhere in my program? > I just can't identify the use-case for decorators, but as I said, maybe > I am missing something. > > Michael I had/have similar feelings. For instance, this is something that I tought useful, but then I never used in real code. The idea was to find a way to automate this code pattern, which I do a lot: class SomeClass: def __init__(self, some, attribute, here ): self.some, self.attribute, self.here = some, attribute, here In other words, I often define classes in which the constructor list of arguments corresponds one-to-one to class attributes. So I thought of this (it uses class decorators so it only works with Python 3.x ) : class FieldsDecorator: def __init__(self, *names): self.names = names def __call__(self, cls): def constructor(instance, **kwds): for n,v in kwds.items(): if n in self.names: setattr(instance, n, v) else: raise TypeError("%s is not a valid field" % s ) setattr(cls, '__init__', constructor ) return cls @FieldsDecorator("uno", "due") class Prova: pass p = Prova(uno=12, due=9) print (p.uno, p.due ) It works and it is nice, but I don't find it compelling enough to use it. I keep assigning directly the attributes, which is more readable. Decorators are really useful when you have lot of repetitive boilercode that you _want_ to hide, since it has little to do with the problem logic and more to to with the technicalities of the programming language or of some framework that you are using. It is called "separating of concerns" I think, and is one of the principles of Aspect-Oriented Programming (and with decorators you can do some nice AOP exercises ... ). Ciao --- FB From steve+comp.lang.python at pearwood.info Fri Dec 9 07:03:19 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Dec 2011 12:03:19 GMT Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> <4ee1f814$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ee1f907$0$29977$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Dec 2011 11:59:16 +0000, Steven D'Aprano wrote: > Just the second-most common source of viruses, malware and security > vulnerabilities (behind buffer overflows): code injection attacks. Oops, I forgot to go back and revise this sentence. Code injection attacks are now the most common, not second-most common, source of security vulnerabilities. http://cwe.mitre.org/top25/index.html -- Steven From rosuav at gmail.com Fri Dec 9 07:08:23 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 23:08:23 +1100 Subject: Dynamic variable creation from string In-Reply-To: <4ee1f814$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> <4ee1f814$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 9, 2011 at 10:59 PM, Steven D'Aprano wrote: > (4) If you think you can make exec safe with a prohibited list of > dangerous strings, you probably can't. If you think that it's even _possible_ to make exec safe with a blacklist, I have a nice padded cell for you over here. Security is NEVER achieved with blacklists, ONLY whitelists. ChrisA From andrea.crotti.0 at gmail.com Fri Dec 9 07:43:07 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Fri, 09 Dec 2011 12:43:07 +0000 Subject: tracking variable value changes In-Reply-To: References: Message-ID: <4EE2025B.3050905@gmail.com> On 12/08/2011 08:17 PM, Catherine Moroney wrote: > Hello, > > Is there a way to create a C-style pointer in (pure) Python so the > following code will reflect the changes to the variable "a" in the > dictionary "x"? > > For example: > > >>> a = 1.0 > >>> b = 2.0 > >>> x = {"a":a, "b":b} > >>> x > {'a': 1.0, 'b': 2.0} > >>> a = 100.0 > >>> x > {'a': 1.0, 'b': 2.0} ## at this point, I would like the value > ## associated with the "a" key to be 100.0 > ## rather than 1.0 > > If I make "a" and "b" numpy arrays, then changes that I make to the > values of a and b show up in the dictionary x. > > My understanding is that when I redefine the value of "a", that Python > is creating a brand-new float with the value of 100.0, whereas when I > use numpy arrays I am merely assigning a new value to the same object. > > Is there some way to rewrite the code above so the change of "a" from > 1.0 to 100.0 is reflected in the dictionary. I would like to use > simple datatypes such as floats, rather than numpy arrays or classes. > I tried using weakref's, but got the error that a weak reference cannot > be created to a float. > > Catherine Not sure if it's exactly pure python but Traits can actually do this https://github.com/enthought/traits From tinnews at isbd.co.uk Fri Dec 9 09:41:56 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Fri, 9 Dec 2011 14:41:56 +0000 Subject: Contacts/Addressbook application - any good Python ones out there? Message-ID: I'm after an application for managing Contacts (i.e. an Address Book) and as I suspect I will want to 'tune' it a bit Python would be my preferred language. So far I have found :- pycocuma - reasonable but rather old and a bit clunky (uses TCL/Tk) pyaddressbook - newer but very minimal Does anyone have any other suggestions? I'd prefer an application which uses vCards as its native data storage format but that's not vital and I'd also like to have a GUI but again that's not vital, a well designed curses/terminal application would be OK too. -- Chris Green From roy at panix.com Fri Dec 9 09:43:22 2011 From: roy at panix.com (Roy Smith) Date: Fri, 09 Dec 2011 09:43:22 -0500 Subject: Misleading error message of the day References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> Message-ID: In article , Jean-Michel Pichavant wrote: > Ethan Furman wrote: > > Jean-Michel Pichavant wrote: > >> You have to opportunity to not use unpacking anymore :o) There is a > >> recent thread were the dark side of unpacking was exposed. Unpacking > >> is a cool feautre for very small applications but should be avoided > >> whenever possible otherwise. > > > > Which thread was that? > > > > > > ~Ethan~ > "A tuple in order to pass returned values ?" was the thread. > > JM To save everybody the effort of finding it, I think he's talking about https://groups.google.com/d/topic/comp.lang.python/2vcwYfIQSOM/discussion From alec.taylor6 at gmail.com Fri Dec 9 10:12:39 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Sat, 10 Dec 2011 02:12:39 +1100 Subject: Contacts/Addressbook application - any good Python ones out there? In-Reply-To: References: Message-ID: Wammu? On Sat, Dec 10, 2011 at 1:41 AM, wrote: > I'm after an application for managing Contacts (i.e. an Address Book) > and as I suspect I will want to 'tune' it a bit Python would be my > preferred language. > > So far I have found :- > > ? ?pycocuma - reasonable but rather old and a bit clunky (uses TCL/Tk) > ? ?pyaddressbook - newer but very minimal > > Does anyone have any other suggestions? ?I'd prefer an application > which uses vCards as its native data storage format but that's not > vital and I'd also like to have a GUI but again that's not vital, a > well designed curses/terminal application would be OK too. > > -- > Chris Green > -- > http://mail.python.org/mailman/listinfo/python-list From invalid at invalid.invalid Fri Dec 9 10:13:46 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 9 Dec 2011 15:13:46 +0000 (UTC) Subject: [OT] Book authoring References: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> Message-ID: On 2011-12-09, Miki Tebeka wrote: > Greetings, > > Any recommendations for a book authoring system that supports the following: > 1. Code examples (with syntax highlighting and line numbers) > 2. Output HTML, PDF, ePub ... > 3. Automatic TOC and index > 4. Search (in HTML) - this is a "nice to have" http://en.wikipedia.org/wiki/Lightweight_markup_language I've used asciidoc extensively and reStructuredText a little. Asciidoc will produce all the formats you mentioned (though I've only refularly used HTML and PDF). reStructuredText is what's used for Python docs isn't it? > Can I somehow use Sphinx? Don't know what Sphinx is. And there's always the old stand-by LaTeX, but it's a bit more heavyweight with more of a learning curve. OTOH, it does produce text-book quality output. -- Grant Edwards grant.b.edwards Yow! BELA LUGOSI is my at co-pilot ... gmail.com From nicholas.dokos at hp.com Fri Dec 9 10:43:27 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Fri, 09 Dec 2011 10:43:27 -0500 Subject: [OT] Book authoring In-Reply-To: Message from Grant Edwards of "Fri, 09 Dec 2011 15:13:46 GMT." References: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> Message-ID: <9925.1323445407@alphaville.dokosmarshall.org> Grant Edwards wrote: > On 2011-12-09, Miki Tebeka wrote: > > Greetings, > > > > Any recommendations for a book authoring system that supports the following: > > 1. Code examples (with syntax highlighting and line numbers) > > 2. Output HTML, PDF, ePub ... > > 3. Automatic TOC and index > > 4. Search (in HTML) - this is a "nice to have" > > http://en.wikipedia.org/wiki/Lightweight_markup_language > > I've used asciidoc extensively and reStructuredText a little. Asciidoc > will produce all the formats you mentioned (though I've only refularly > used HTML and PDF). reStructuredText is what's used for Python docs > isn't it? > > > Can I somehow use Sphinx? > > Don't know what Sphinx is. > I think Sphinx is used for the python docs: it sits atop rST and does all the transformations/processing to produce the desired output ( http://sphinx.pocoo.org ) > And there's always the old stand-by LaTeX, but it's a bit more > heavyweight with more of a learning curve. OTOH, it does produce > text-book quality output. > There is also orgmode, which has been used for a few books (http://orgmode.org ). I know it does HTML and PDF (the latter through latex), but I'm not sure about ePub: ISTR somebody actually did ePub for his book but I don't remember details. The indexing is manual: add #+index: foo entries as required. But in general, imo, automatic indexing for books sucks raw eggs (it works much better for highly regular source code like the python source base). Nick From nicholas.dokos at hp.com Fri Dec 9 11:18:40 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Fri, 09 Dec 2011 11:18:40 -0500 Subject: [OT] Book authoring In-Reply-To: Message from Nick Dokos of "Fri, 09 Dec 2011 10:43:27 EST." <9925.1323445407@alphaville.dokosmarshall.org> References: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> <9925.1323445407@alphaville.dokosmarshall.org> Message-ID: <10973.1323447520@alphaville.dokosmarshall.org> Nick Dokos wrote: > There is also orgmode, which has been used for a few books > (http://orgmode.org ). I know it does HTML and PDF (the latter through > latex), but I'm not sure about ePub: ISTR somebody actually did ePub for > his book but I don't remember details. Avdi Grimm produced his book "Exceptional Ruby" (http://exceptionalruby.com ) this way, including ePub formats (I hope mentioning Ruby in this context is not a punishable offense...) Apparently, there is calibre (http://calibre-ebook.com/ ) that will take you from HTML to ePub. See this orgmode list article e.g. http://thread.gmane.org/gmane.emacs.orgmode/41826 Nick From skip at pobox.com Fri Dec 9 11:19:12 2011 From: skip at pobox.com (Skip Montanaro) Date: Fri, 9 Dec 2011 16:19:12 +0000 (UTC) Subject: How to build 64-bit Python on Solaris with GCC? References: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> <4EDF604F.9070306@gmail.com> Message-ID: Karim gmail.com> writes: > ./configure > make > make install Thanks. I have several different versions in my local sandbox. None are 64-bit ELFs. Just to make sure I hadn't missed some new development in this area, I cloned the hg repository and build the trunk version from scratch. I get a 32-bit executable on Solaris: % file ./python ./python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), not stripped Skip From andrea.crotti.0 at gmail.com Fri Dec 9 11:25:28 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Fri, 09 Dec 2011 16:25:28 +0000 Subject: [OT] Book authoring In-Reply-To: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> References: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> Message-ID: <4EE23678.5060409@gmail.com> On 12/09/2011 03:25 AM, Miki Tebeka wrote: > Greetings, > > Any recommendations for a book authoring system that supports the following: > 1. Code examples (with syntax highlighting and line numbers) > 2. Output HTML, PDF, ePub ... > 3. Automatic TOC and index > 4. Search (in HTML) - this is a "nice to have" > > Can I somehow use Sphinx? > > Thanks, > -- > Miki I think it depends on what you want exactly. If it's a nice book with a scientific look and many complicated tables/figures than I think that LaTeX is the way to go (maybe even org-mode but it's mainly for emacs-fans). The problem with LaTeX is that it's quite tricky to export to other formats, harder to learn and not as flexible as a python-based solution as Sphinx. I would suggest to try Sphinx and see if you're missing something.. From stefan-usenet at bytereef.org Fri Dec 9 11:41:37 2011 From: stefan-usenet at bytereef.org (Stefan Krah) Date: Fri, 9 Dec 2011 17:41:37 +0100 Subject: How to build 64-bit Python on Solaris with GCC? In-Reply-To: References: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> <4EDF604F.9070306@gmail.com> Message-ID: <20111209164137.GA4912@sleipnir.bytereef.org> Skip Montanaro wrote: > Thanks. I have several different versions in my local sandbox. None > are 64-bit ELFs. Just to make sure I hadn't missed some new development > in this area, I cloned the hg repository and build the trunk version > from scratch. I get a 32-bit executable on Solaris: > > % file ./python > ./python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), > dynamically linked (uses shared libs), not stripped ./configure CFLAGS=-m64 LDFLAGS=-m64 should work with a reasonably recent revision. Stefan Krah From hniksic at xemacs.org Fri Dec 9 11:51:06 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Fri, 09 Dec 2011 17:51:06 +0100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87ty59adtx.fsf@xemacs.org> Steven D'Aprano writes: > Except for people who needed dicts with tens of millions of items. Huge tree-based dicts would be somewhat slower than today's hash-based dicts, but they would be far from unusable. Trees are often used to organize large datasets for quick access. The case of dicts which require frequent access, such as those used to implement namespaces, is different, and more interesting. Those dicts are typically quite small, and for them the difference between O(log n) and O(1) is negligible in both theory (since n is "small", i.e. bounded) and practice. In fact, depending on the details of the implementation, the lookup in a small tree could even be marginally faster. From tinnews at isbd.co.uk Fri Dec 9 11:59:34 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Fri, 9 Dec 2011 16:59:34 +0000 Subject: Contacts/Addressbook application - any good Python ones out there? References: Message-ID: Alec Taylor wrote: > Wammu? > I hadn't really considered gammu/wammu as I saw it as a mobile phone synchrinsation tool, but I've looked a bit harder and it might very well be what I need - thank you! > On Sat, Dec 10, 2011 at 1:41 AM, wrote: > > I'm after an application for managing Contacts (i.e. an Address Book) > > and as I suspect I will want to 'tune' it a bit Python would be my > > preferred language. > > > > So far I have found :- > > > > ? ?pycocuma - reasonable but rather old and a bit clunky (uses TCL/Tk) > > ? ?pyaddressbook - newer but very minimal > > > > Does anyone have any other suggestions? ?I'd prefer an application > > which uses vCards as its native data storage format but that's not > > vital and I'd also like to have a GUI but again that's not vital, a > > well designed curses/terminal application would be OK too. > > > > -- > > Chris Green > > -- > > http://mail.python.org/mailman/listinfo/python-list -- Chris Green From andrecrash3r at gmail.com Fri Dec 9 12:12:01 2011 From: andrecrash3r at gmail.com (=?ISO-8859-1?Q?Andr=E9_Lopes?=) Date: Fri, 9 Dec 2011 17:12:01 +0000 Subject: Execute python within Oracle Message-ID: Hi all, I wrote a simple Java program to be called within an Oracle database. The goal is to execute a Python program within the DB itself, by the means of a Java program. The problem is that when I execute the procedure inside the DB, nothing happens? If I create the same Java class outside the DB and execute it, the python program works perfectly, only inside the DB nothing happens. The program is the following. CREATE OR REPLACE AND COMPILE java source named "OSCommand" as import java.io.*; public class OSCommand{ public static void Run(){ try { Runtime r = Runtime.getRuntime(); Process p = r.exec("cmd /c C:\\Python32\\python.exe C:\\Ficheiros\\SAP\\Novos\\xls2csv.py C:\\Ficheiros\\SAP\\Novos\\20111020_ListagemSAP.xlsx"); } catch (Exception e) { e.printStackTrace(); } } } / Can anyone help? Thanks, Andr? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Fri Dec 9 12:13:21 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Dec 2011 04:13:21 +1100 Subject: order independent hash? In-Reply-To: <87ty59adtx.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> <87ty59adtx.fsf@xemacs.org> Message-ID: On Sat, Dec 10, 2011 at 3:51 AM, Hrvoje Niksic wrote: > The case of dicts which require frequent access, such as those used to > implement namespaces, is different, and more interesting. ?Those dicts > are typically quite small, and for them the difference between O(log n) > and O(1) is negligible in both theory (since n is "small", i.e. bounded) > and practice. ?In fact, depending on the details of the implementation, > the lookup in a small tree could even be marginally faster. This is something where, I am sure, far greater minds than mine delve... but, would a splay tree be effective for name lookups? In most cases, you'll have a huge puddle of names of which you use the tiniest fraction; and a splay tree would, in effect, automatically optimize itself to handle tight loops. ChrisA From ethan at stoneleaf.us Fri Dec 9 12:39:13 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 09 Dec 2011 09:39:13 -0800 Subject: Misleading error message of the day In-Reply-To: <4EE1DD09.4030707@sequans.com> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> <4EE1DD09.4030707@sequans.com> Message-ID: <4EE247C1.2040107@stoneleaf.us> Jean-Michel Pichavant wrote: > Ethan Furman wrote: >> Jean-Michel Pichavant wrote: >>> You have to opportunity to not use unpacking anymore :o) There is a >>> recent thread were the dark side of unpacking was exposed. Unpacking >>> is a cool feautre for very small applications but should be avoided >>> whenever possible otherwise. >> >> Which thread was that? >> >> >> ~Ethan~ > "A tuple in order to pass returned values ?" was the thread. Thanks. ~Ethan~ From ian.g.kelly at gmail.com Fri Dec 9 12:50:31 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 9 Dec 2011 10:50:31 -0700 Subject: Execute python within Oracle In-Reply-To: References: Message-ID: 2011/12/9 Andr? Lopes : > Hi all, > > > > ?? I wrote a simple Java program to be called within an Oracle database. The > goal is to execute a Python program within the DB itself, by the means of a > Java program. The problem is that when I execute the procedure inside the > DB, nothing happens? > > > > ?? If I create the same Java class outside the DB and execute it, the python > program works perfectly, only inside the DB nothing happens. The program is > the following. Have you granted the necessary permissions to execute programs from Java? http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:952229840241 Note that article is for Oracle 8, I'm not sure whether the permissions might have changed since then. Cheers, Ian From dihedral88888 at googlemail.com Fri Dec 9 13:28:49 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 9 Dec 2011 10:28:49 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> On Thursday, December 8, 2011 7:43:12 PM UTC+8, Chris Angelico wrote: > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > > I am still perplexed about decorators though, am happily using Python for > > many years without them, but maybe i am missing something? > > For example in the above case, if I want the names attached to each other > > with a comma, why wouldn't I just create a function doing exactly this? Why > > would I first write a single name generator and then decorate it so that I > > never can get single names anymore (this is the case, isn't it? Once > > decorated, I can not get the original behaviour of the function anymore. > > The example given is a toy. It's hardly useful. However, there are a > number of handy uses for decorators; mostly, they consist of giving a > single simple keyword to a complicated set of logic. One example is > the @classmethod and @staticmethod decorators - the code to implement > them could be uglier than nested inline assembly, but you don't have > to care, because you just type "@staticmethod" in front of your def > statement and it does its magic. > > Here's a handy trick that I'm sure someone has done in a more sophisticated way: > > def trace(func): > if debugmode: > return lambda *a,**ka: > (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] > return func > > Then you put @trace in front of all your functions, and if debugmode > is False, nothing will be done - but set it to true, and you get > console output at the entry and exit of each function. > > >>> @trace > def test(x): > print("Test! "+x) > return 5 > > >>> test("asdf") > >test > Test! asdf > 5 > > Again, it's helpful because it condenses all the logic (including the > 'debugmode' flag) down to a single high level directive: "Trace this > function". > > ChrisA I did use decorators to turn functions into iterables to be traced. From dihedral88888 at googlemail.com Fri Dec 9 13:28:49 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 9 Dec 2011 10:28:49 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> On Thursday, December 8, 2011 7:43:12 PM UTC+8, Chris Angelico wrote: > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > > I am still perplexed about decorators though, am happily using Python for > > many years without them, but maybe i am missing something? > > For example in the above case, if I want the names attached to each other > > with a comma, why wouldn't I just create a function doing exactly this? Why > > would I first write a single name generator and then decorate it so that I > > never can get single names anymore (this is the case, isn't it? Once > > decorated, I can not get the original behaviour of the function anymore. > > The example given is a toy. It's hardly useful. However, there are a > number of handy uses for decorators; mostly, they consist of giving a > single simple keyword to a complicated set of logic. One example is > the @classmethod and @staticmethod decorators - the code to implement > them could be uglier than nested inline assembly, but you don't have > to care, because you just type "@staticmethod" in front of your def > statement and it does its magic. > > Here's a handy trick that I'm sure someone has done in a more sophisticated way: > > def trace(func): > if debugmode: > return lambda *a,**ka: > (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] > return func > > Then you put @trace in front of all your functions, and if debugmode > is False, nothing will be done - but set it to true, and you get > console output at the entry and exit of each function. > > >>> @trace > def test(x): > print("Test! "+x) > return 5 > > >>> test("asdf") > >test > Test! asdf > 5 > > Again, it's helpful because it condenses all the logic (including the > 'debugmode' flag) down to a single high level directive: "Trace this > function". > > ChrisA I did use decorators to turn functions into iterables to be traced. From geoff.bache at gmail.com Fri Dec 9 14:32:29 2011 From: geoff.bache at gmail.com (Geoff Bache) Date: Fri, 9 Dec 2011 11:32:29 -0800 (PST) Subject: Buffering of sys.stdout and sys.stderr in python3 (and documentation) Message-ID: Hi all, Short version: I'm a bit confused in general as to the changes between python2 and python3 regarding how standard output and standard error do buffering. A few things seem to have changed and I've failed to find any documentation of how and why. Also, the meaning of "python -u" seems to have changed and the docs don't seem to reflect the new behaviour (and I can't find any docs about the change either)... Long version: >From rude experiment it seems that: 1) In Python 2.x, standard error was always unbuffered while standard output was buffered by default. In python3, both are buffered. In both cases, "buffered" means line-buffered when writing to the console and not line-buffered when redirected to files. 2) In Python 2.x, the "-u" flag meant everything was totally unbuffered. In Python 3.x, it means that both stdout and stderr are line-buffered also when redirected to files. Are either of these changes documented anywhere? (1) seems important : it can lead to not seeing exception printouts, if stderr is redirected to a file and the program is subsequently terminated with SIGTERM. I just wasted quite a bit of time due to this situation... This is what the Python 3 docs have to say about the -u flag: "Force the binary layer of the stdin, stdout and stderr streams (which is available as their buffer attribute) to be unbuffered. The text I/O layer will still be line-buffered." The "still" seems misleading to me, as it is only relevant if writing to the console. It would be useful to contrast the behaviour with and without "-u" when writing to files I would say. Regards, Geoff Bache From ethan at stoneleaf.us Fri Dec 9 15:01:01 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 09 Dec 2011 12:01:01 -0800 Subject: Dynamic variable creation from string In-Reply-To: <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Message-ID: <4EE268FD.4090806@stoneleaf.us> Massi wrote: > Thank you all for your replies, first of all my Sum function was an > example simplifying what I have to do in my real funciton. In general > the D dictionary is complex, with a lot of keys, so I was searching > for a quick method to access all the variables in it without doing the > explicit creation: > > a, b, c = D['a'], D['b'], D['c'] > > and without using directly the D dictionary (boring...). > When I talked about nested function I meant both cases Chris, but this > is not a really tighten bound. > I tried to follow the hints of Chris together with some help by google > and used the following code: > > for k in D : exec "%s = D[k]" %k > > That seems to do the trick, but someone speaks about "dirty code", can > anyone point me out which problems this can generate? > Again, thank you for your help! Besides the serious security issues, this method won't make the problem any better in Python 3. ~Ethan~ From tinnews at isbd.co.uk Fri Dec 9 17:06:20 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Fri, 9 Dec 2011 22:06:20 +0000 Subject: Contacts/Addressbook application - any good Python ones out there? References: Message-ID: tinnews at isbd.co.uk wrote: > Alec Taylor wrote: > > Wammu? > > > I hadn't really considered gammu/wammu as I saw it as a mobile phone > synchrinsation tool, but I've looked a bit harder and it might very > well be what I need - thank you! > Well one problem with wammu is that you can't do anything with the program unless there's a phone connected. -- Chris Green From ladasky at my-deja.com Fri Dec 9 18:14:30 2011 From: ladasky at my-deja.com (John Ladasky) Date: Fri, 9 Dec 2011 15:14:30 -0800 (PST) Subject: Multiprocessing bug, is information ever omitted from a traceback? Message-ID: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> Hi folks, A tangent off of this thread: http://groups.google.com/group/comp.lang.python/browse_frm/thread/751b7050c756c995# I'm programming in Python 2.6 on Ubuntu Linux 10.10, if it matters. I'm trying to track down a multiprocessing bug. Here's my traceback. All lines of code referenced in the traceback are in the standard library code: Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner self.run() File "/usr/lib/python2.6/threading.py", line 484, in run self.__target(*self.__args, **self.__kwargs) File "/usr/lib/python2.6/multiprocessing/pool.py", line 284, in _handle_tasks put(task) TypeError: expected string or Unicode object, NoneType found Fortunately, I have a working version of my code. I was trying to add new features, and only my new code is causing trouble. This has allowed me to examine the contexts of task when everything works. Task is not a string when the program works. Task is not None when the program doesn't work. In fact, task is a deeply-nested tuple. NO PART of this tuple ever contains any strings, as far as I can tell. More details in my original thread. Now, of course I've seen that the standard traceback shows you the lines where various steps in a chain of function calls were taken. The traceback skips over any lines in the code between successive function calls, and assumes that you can follow along. No problem, I can do that. But when multiprocessing is involved, can this traceback be truncated in some way, for example when code execution switches over to a subprocess? I'm wondering if more code is getting executed after "put(task)" that I'm not seeing. Thanks for any information! From henryx_b at yahoo.it Fri Dec 9 18:16:30 2011 From: henryx_b at yahoo.it (Enrico 'Henryx' Bianchi) Date: Sat, 10 Dec 2011 00:16:30 +0100 Subject: Python 2 or 3 References: Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Tobiah wrote: > Use the newer version and don't look back. Interesting reply, but if I have a platform wich doesn't support Python 3 (e.g. RHEL 5.x)? ]:) Enrico P.S. note that: I *don't* want to recompile Python in production environment -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQEcBAEBAgAGBQJO4pbOAAoJED3SMOGZLYdYQ7sIAI3vfvOyQc5Gx205cDMS7bPK uXxZI7ShqybyEv0NMDapxURQhz59Kc9zh8E/OKDiXohjmkE1YA78K7qSKyrtXTMy ppcGUU5USaQhPZ+RqOEj95aTxQj3CW/8w74rNEirIMn6+yGt4QjWRuGT1K6aUM51 BXF9I22f37z/sJ7x+fZUL9R7G1HA4saRGEiQGxBgkmt6gi28nboOibdxfw9bmP5x aHbpVYQ6yo+7nOf0XZno/pl0zkpDvhS/tNvvuH8kYQIvMLyQZ/f+xZJ6yj58S5Se AGSGXEDRemw0Ge83HjJvmQE3JXjy1fc1gCQSnmqQifXW7h18q99L3okJds+uHnE= =PwK5 -----END PGP SIGNATURE----- From steve+comp.lang.python at pearwood.info Fri Dec 9 19:19:48 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Dec 2011 00:19:48 GMT Subject: Python 2 or 3 References: Message-ID: <4ee2a5a4$0$29977$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Dec 2011 00:16:30 +0100, Enrico 'Henryx' Bianchi wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Tobiah wrote: > >> Use the newer version and don't look back. > > Interesting reply, but if I have a platform wich doesn't support Python > 3 (e.g. RHEL 5.x)? ]:) RHEL supports Python 3, it just doesn't provide Python 3. It almost certainly will work if you install from source. I haven't tried it on RHEL myself, but I have done so on Fedora and Centos, and there's no problem. But be warned that you should not replace the system python with Python 3. When installing, don't use "make install", as that will replace the system Python, instead use "make altinstall". Then the command "python" will still refer to the system Python (probably Python 2.4 or 2.5?), and "python3" should refer to Python 3.x. > Enrico > P.S. note that: I *don't* want to recompile Python in production > environment You shouldn't be learning programming on a production server :) -- Steven From dihedral88888 at googlemail.com Fri Dec 9 19:21:11 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 9 Dec 2011 16:21:11 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> Message-ID: <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> On Saturday, December 10, 2011 2:28:49 AM UTC+8, 88888 Dihedral wrote: > On Thursday, December 8, 2011 7:43:12 PM UTC+8, Chris Angelico wrote: > > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > > > I am still perplexed about decorators though, am happily using Python for > > > many years without them, but maybe i am missing something? > > > For example in the above case, if I want the names attached to each other > > > with a comma, why wouldn't I just create a function doing exactly this? Why > > > would I first write a single name generator and then decorate it so that I > > > never can get single names anymore (this is the case, isn't it? Once > > > decorated, I can not get the original behaviour of the function anymore. > > > > The example given is a toy. It's hardly useful. However, there are a > > number of handy uses for decorators; mostly, they consist of giving a > > single simple keyword to a complicated set of logic. One example is > > the @classmethod and @staticmethod decorators - the code to implement > > them could be uglier than nested inline assembly, but you don't have > > to care, because you just type "@staticmethod" in front of your def > > statement and it does its magic. > > > > Here's a handy trick that I'm sure someone has done in a more sophisticated way: > > > > def trace(func): > > if debugmode: > > return lambda *a,**ka: > > (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] > > return func > > > > Then you put @trace in front of all your functions, and if debugmode > > is False, nothing will be done - but set it to true, and you get > > console output at the entry and exit of each function. > > > > >>> @trace > > def test(x): > > print("Test! "+x) > > return 5 > > > > >>> test("asdf") > > >test > > Test! asdf > > > 5 > > > > Again, it's helpful because it condenses all the logic (including the > > 'debugmode' flag) down to a single high level directive: "Trace this > > function". > > > > ChrisA > > I did use decorators to turn functions into iterables to be traced. It is easy to use decorators in python to mimic those programs in Erlang. From dihedral88888 at googlemail.com Fri Dec 9 19:21:11 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 9 Dec 2011 16:21:11 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> Message-ID: <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> On Saturday, December 10, 2011 2:28:49 AM UTC+8, 88888 Dihedral wrote: > On Thursday, December 8, 2011 7:43:12 PM UTC+8, Chris Angelico wrote: > > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > > > I am still perplexed about decorators though, am happily using Python for > > > many years without them, but maybe i am missing something? > > > For example in the above case, if I want the names attached to each other > > > with a comma, why wouldn't I just create a function doing exactly this? Why > > > would I first write a single name generator and then decorate it so that I > > > never can get single names anymore (this is the case, isn't it? Once > > > decorated, I can not get the original behaviour of the function anymore. > > > > The example given is a toy. It's hardly useful. However, there are a > > number of handy uses for decorators; mostly, they consist of giving a > > single simple keyword to a complicated set of logic. One example is > > the @classmethod and @staticmethod decorators - the code to implement > > them could be uglier than nested inline assembly, but you don't have > > to care, because you just type "@staticmethod" in front of your def > > statement and it does its magic. > > > > Here's a handy trick that I'm sure someone has done in a more sophisticated way: > > > > def trace(func): > > if debugmode: > > return lambda *a,**ka: > > (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] > > return func > > > > Then you put @trace in front of all your functions, and if debugmode > > is False, nothing will be done - but set it to true, and you get > > console output at the entry and exit of each function. > > > > >>> @trace > > def test(x): > > print("Test! "+x) > > return 5 > > > > >>> test("asdf") > > >test > > Test! asdf > > > 5 > > > > Again, it's helpful because it condenses all the logic (including the > > 'debugmode' flag) down to a single high level directive: "Trace this > > function". > > > > ChrisA > > I did use decorators to turn functions into iterables to be traced. It is easy to use decorators in python to mimic those programs in Erlang. From steve+comp.lang.python at pearwood.info Fri Dec 9 19:41:25 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Dec 2011 00:41:25 GMT Subject: tracking variable value changes References: Message-ID: <4ee2aab4$0$29977$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Dec 2011 12:17:11 -0800, Catherine Moroney wrote: > Hello, > > Is there a way to create a C-style pointer in (pure) Python so the > following code will reflect the changes to the variable "a" in the > dictionary "x"? Strictly speaking, no, but there may be a way to get something close. See below. > For example: > > >>> a = 1.0 > >>> b = 2.0 > >>> x = {"a":a, "b":b} > >>> x > {'a': 1.0, 'b': 2.0} > >>> a = 100.0 > >>> x > {'a': 1.0, 'b': 2.0} ## at this point, I would like the value > ## associated with the "a" key to be 100.0 ## > rather than 1.0 The line "a = 100" is a rebinding, and so what you are asking for isn't directly possible. But if you are willing to live with an explicit redirection, you can somewhat simulate a pointer with a list: py> aPtr = [1.0] # not really a pointer, but let's pretend it is py> bPtr = [2.0] py> x = {'a': aPtr, 'b': bPtr} py> x {'a': [1.0], 'b': [2.0]} py> aPtr[0] = 100.0 py> x {'a': [100.0], 'b': [2.0]} If you prefer, you can write a simple class to handle the redirection with the interface of your choice. Something like this might be a good start: class SharedValue: def set(self, value): self.value = value def get(self): return self.value def __repr__(self): # Somewhat dubious. return str(self.value) py> a = SharedValue() # create a pointer py> a.set(1.0) py> x = {'a': a} py> x {'a': 1.0} py> a.set(100.0) py> x {'a': 100.0} Look at the automatic delegation pattern for a way to have operations on "a" automatically apply to the object being pointed to. (This will be *much* simpler if you don't inherit from object.) But be warned, whatever you do, rebinding will behave in the standard Python way. E.g.: py> aPtr = [2000.0] # Oops, rebound the name to something else! py> x # and the connection is lost {'a': [100.0], 'b': [2.0]} > If I make "a" and "b" numpy arrays, then changes that I make to the > values of a and b show up in the dictionary x. Yes, because numpy arrays are mutable objects. In this case, you have two (or more) references to a single object: the name "a", and the entry in dict "x". When you modify the object in either place, the change is visible in both places because they are the same object. But when you rebind the name "a" to another object -- not necessarily a *new* object, just a different one -- there is no way for the dict "x" to notice this change and follow along. > My understanding is that when I redefine the value of "a", that Python > is creating a brand-new float with the value of 100.0, whereas when I > use numpy arrays I am merely assigning a new value to the same object. Correct. Although the float need not be brand-new. Python could (but probably doesn't) re-use an existing float object. -- Steven From python.list at tim.thechases.com Fri Dec 9 20:44:48 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 09 Dec 2011 19:44:48 -0600 Subject: Obtaining user information Message-ID: <4EE2B990.2030908@tim.thechases.com> Currently I can get the currently-logged-in-userid via getpass.getuser() which would yield something like "tchase". Is there a cross-platform way to get the full username (such as from the GECOS field of /etc/passed or via something like NetUserGetInfo on Win32 so I'd get "Tim Chase" instead? Thanks, -tkc From skip at pobox.com Fri Dec 9 22:19:07 2011 From: skip at pobox.com (Skip Montanaro) Date: Sat, 10 Dec 2011 03:19:07 +0000 (UTC) Subject: How to build 64-bit Python on Solaris with GCC? References: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> <4EDF604F.9070306@gmail.com> <20111209164137.GA4912@sleipnir.bytereef.org> Message-ID: > ./configure CFLAGS=-m64 LDFLAGS=-m64 should work with a reasonably > recent revision. Thanks, that did, indeed work with CPython trunk. I eventually switched from gcc to Sun's compiler though because I was getting link warnings. Skip From tjreedy at udel.edu Fri Dec 9 23:43:39 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 09 Dec 2011 23:43:39 -0500 Subject: Buffering of sys.stdout and sys.stderr in python3 (and documentation) In-Reply-To: References: Message-ID: On 12/9/2011 2:32 PM, Geoff Bache wrote: > Hi all, > > Short version: > > I'm a bit confused in general as to the changes between python2 and > python3 regarding how standard output and standard error do buffering. > A few things seem to have changed and I've failed to find any > documentation of how and why. Also, the meaning of "python -u" seems > to have changed and the docs don't seem to reflect the new behaviour > (and I can't find any docs about the change either)... > > Long version: > >> From rude experiment it seems that: > 1) In Python 2.x, standard error was always unbuffered while standard > output was buffered by default. In python3, both are buffered. In > both cases, "buffered" means line-buffered when writing to the console > and not line-buffered when redirected to files. > 2) In Python 2.x, the "-u" flag meant everything was totally > unbuffered. In Python 3.x, it means that both stdout and stderr are > line-buffered also when redirected to files. > > Are either of these changes documented anywhere? (1) seems important : > it can lead to not seeing exception printouts, if stderr is redirected > to a file and the program is subsequently terminated with SIGTERM. I > just wasted quite a bit of time due to this situation... > > This is what the Python 3 docs have to say about the -u flag: > > "Force the binary layer of the stdin, stdout and stderr streams (which > is available as their buffer attribute) to be unbuffered. The text I/O > layer will still be line-buffered." > > The "still" seems misleading to me, as it is only relevant if writing > to the console. It would be useful to contrast the behaviour with and > without "-u" when writing to files I would say. The difference from 2.x should be in What's New in 3.0, except that the new i/o module is in 2.6, so it was not exactly new. You might be able to find more in http://python.org/dev/peps/pep-3116/ You *should* be able to find sufficient info in the 3.x docs. If, after you get other responses (or not), you think the docs need upgrading, open an issue on the tracker at bugs.python.org with suggestions as specific as possible, including changed or new lines of text based on your experience and experiments. -- Terry Jan Reedy From tjreedy at udel.edu Sat Dec 10 00:00:05 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Dec 2011 00:00:05 -0500 Subject: Multiprocessing bug, is information ever omitted from a traceback? In-Reply-To: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> Message-ID: On 12/9/2011 6:14 PM, John Ladasky wrote: > http://groups.google.com/group/comp.lang.python/browse_frm/thread/751b7050c756c995# > > I'm programming in Python 2.6 on Ubuntu Linux 10.10, if it matters. It might, as many bugs have been fixed since. Can you try the same code with the most recent 2.x release, 2.7.2? Do you have working and non-working code that you can publicly release? Can you reduce the size and dependencies so the examples are closer to 'small' than 'large'? And in any case, self-contained? In my first response, I said you might have found a bug. A bogus exception message qualifies. But to do much, we need minimal good/bad examples that run or not on a current release (2.7.2 or 3.2.2). -- Terry Jan Reedy From cosmo_general at yahoo.com Sat Dec 10 00:05:07 2011 From: cosmo_general at yahoo.com (Muddy Coder) Date: Fri, 9 Dec 2011 21:05:07 -0800 (PST) Subject: How to move scrollbar by code? Message-ID: <0a0679bc-796f-42a7-99c6-d2199057f4d8@r6g2000yqr.googlegroups.com> Hi Folks, I am trying to make a listbox that will contain a looong data list, sorted, so I will be able to pre-select a data line by coding. I have done it. Say my listbox contains 1000 data lines, and my program has figured out the data line 321 is needed, so just put the cursor on data line 321. However, my scrollbar is still seating on the top, so I just can view the data line from line 0 to 30 or 40, since I can't see the desired data line 321. I still need to manually pull the scrollbar down to display the data line 321. What I want to do is to grab the adjacent data lines, say from line 300 to 340, and display this lines, while my cursor seating in the middle. I consulted the Python Docs, but did not find such details. Can somebody give an idea? Thanks! Cosmo From rosuav at gmail.com Sat Dec 10 02:30:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Dec 2011 18:30:56 +1100 Subject: How to move scrollbar by code? In-Reply-To: <0a0679bc-796f-42a7-99c6-d2199057f4d8@r6g2000yqr.googlegroups.com> References: <0a0679bc-796f-42a7-99c6-d2199057f4d8@r6g2000yqr.googlegroups.com> Message-ID: On Sat, Dec 10, 2011 at 4:05 PM, Muddy Coder wrote: > I am trying to make a listbox that will contain a looong data list, > sorted, so I will be able to pre-select a data line by coding. I have > done it. Which GUI toolkit are you using? What you want is not the Python language docs, but the docs for that toolkit (GTK, Qt, Tk, etc). ChrisA From cs at zip.com.au Sat Dec 10 02:37:23 2011 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 10 Dec 2011 18:37:23 +1100 Subject: Obtaining user information In-Reply-To: <4EE2B990.2030908@tim.thechases.com> References: <4EE2B990.2030908@tim.thechases.com> Message-ID: <20111210073723.GA939@cskk.homeip.net> On 09Dec2011 19:44, Tim Chase wrote: | Currently I can get the currently-logged-in-userid via | getpass.getuser() which would yield something like "tchase". _If_ you're on a terminal. _And_ that's exactly what you want. Personally I need to the name of geteuid() or getuid() more often. | Is there a cross-platform way to get the full username (such as from | the GECOS field of /etc/passed or via something like NetUserGetInfo | on Win32 so I'd get "Tim Chase" instead? Hmm. Doesn't windows have a posix layer? pwd.getpwnam(os.getuid())[4].split(',')[0] is the best I've got. ANd it probably doesn't work in Windows:-( -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ There's not a woman in his book, the plot hinges on unkindness to animals, and the black characters mostly drown by chapter 29. - P J O'Rourke parodying a PC review of Moby Dick From hansmu at xs4all.nl Sat Dec 10 02:43:59 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Sat, 10 Dec 2011 08:43:59 +0100 Subject: Obtaining user information In-Reply-To: References: Message-ID: <4ee30dbf$0$6933$e4fe514c@news2.news.xs4all.nl> On 10/12/11 02:44:48, Tim Chase wrote: > Currently I can get the currently-logged-in-userid via getpass.getuser() > which would yield something like "tchase". > > Is there a cross-platform way to get the full username (such as from the > GECOS field of /etc/passed or via something like NetUserGetInfo on Win32 > so I'd get "Tim Chase" instead? How about: pwd.getpwuid(os.getuid()).pw_gecos This will give you the GECOS field of /etc/passed. I'd assume it contains "Tim Chase" for your account. Hope this helps, -- HansM From cs at zip.com.au Sat Dec 10 02:59:11 2011 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 10 Dec 2011 18:59:11 +1100 Subject: Obtaining user information In-Reply-To: <4ee30dbf$0$6933$e4fe514c@news2.news.xs4all.nl> References: <4ee30dbf$0$6933$e4fe514c@news2.news.xs4all.nl> Message-ID: <20111210075911.GA3651@cskk.homeip.net> On 10Dec2011 08:43, Hans Mulder wrote: | On 10/12/11 02:44:48, Tim Chase wrote: | >Currently I can get the currently-logged-in-userid via getpass.getuser() | >which would yield something like "tchase". | > | >Is there a cross-platform way to get the full username (such as from the | >GECOS field of /etc/passed or via something like NetUserGetInfo on Win32 | >so I'd get "Tim Chase" instead? | | How about: | pwd.getpwuid(os.getuid()).pw_gecos | This will give you the GECOS field of /etc/passed. | I'd assume it contains "Tim Chase" for your account. Up to a comma is the convention. Eg "Cameron Simpson, x2983". -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ Microsoft - where "cross platform" means "runs in both Win95 and WinNT". - Andy Newman From geoff.bache at gmail.com Sat Dec 10 05:03:10 2011 From: geoff.bache at gmail.com (Geoff Bache) Date: Sat, 10 Dec 2011 02:03:10 -0800 (PST) Subject: Buffering of sys.stdout and sys.stderr in python3 (and documentation) References: Message-ID: <6152c707-8bbf-4e78-a4ec-34651a5ccbfb@p9g2000vbb.googlegroups.com> Hi Terry, > The difference from 2.x should be in What's New in 3.0, except that the > new i/o module is in 2.6, so it was not exactly new. The io module existed in 2.6, but it was not used by default for standard output and standard error. The only mention of this in "What's New in 3.0" is in the section marked for changes that were already in 2.6 (which is wrong in this case), and it notes only that io.TextIOWrapper is now used, but not what implications that has for its behaviour and backward compatibility. > You might be able > to find more inhttp://python.org/dev/peps/pep-3116/ I skimmed through it but couldn't find anything relevant. It seems more "advanced" and implementation-focussed. > > You *should* be able to find sufficient info in the 3.x docs. If, after > you get other responses (or not), you think the docs need upgrading, > open an issue on the tracker at bugs.python.org with suggestions as > specific as possible, including changed or new lines of text based on > your experience and experiments. OK, I'll do that if nobody points me at some existing docs here. Regards, Geoff Bache From henryx_b at yahoo.it Sat Dec 10 06:49:27 2011 From: henryx_b at yahoo.it (Enrico 'Henryx' Bianchi) Date: Sat, 10 Dec 2011 12:49:27 +0100 Subject: Python 2 or 3 References: <4ee2a5a4$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Steven D'Aprano wrote: > RHEL supports Python 3, it just doesn't provide Python 3. True, but as you say later, the only method is to recompile. So, if I want to use Python 3 in a production environment like RHEL, I need: - A development environment similar to production (e.g. if I use RHEL 5 in production, I need at least a CentOS 5.x); - Compile Python 3 in a development environment; - Write the python app; - Release a *huge* package to install. The only bright side is to freeze version of Python and the libraries, but every update (e.g. bug fixing on a library) is by hand > When installing, don't use "make install", as that will replace the > system Python, instead use "make altinstall". Good, I didn't know this option > Then the command "python" > will still refer to the system Python (probably Python 2.4 or 2.5?), and > "python3" should refer to Python 3.x. RHEL (and CentOS) 5.x use Python 2.4 > You shouldn't be learning programming on a production server :) Of course, but if I want to use an application written in Python 3 on a production environment which doesn't support it, I have to prepare at least a development environment similar to production (ok, ok, with a VM is simple, but I need to track the exception) Enrico P.S. an alternative may be cx_freeze, but I don't know exactly hot it works P.P.S. I'm boring, but I would like my point of view because I've found precisely in this case -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQEcBAEBAgAGBQJO40dHAAoJED3SMOGZLYdYIPoH/1J6OljjCJQnmR/uwjEFCrHy TEMpfKodD40gL7lOZLMHnpJrs+Ct2Vo/1+mtAIi+vZ6rkhFG50ykBJMAlMgXkCjt I6fkp9YLKmFX9OjRuJ9qE+2P5PliyNDPKVljyfaXMhalbrtHnn7mrL9524TzhcoG +Ape1U9MPTu3naVULKWK0FjGa/RwhbSOdDOKX2IBmRHFEgtf3dZJ2xNYXUJnhnT8 fbfD87ykXyyVg6LS8c14PPeWnpFeeZBQappjoHg9+XZd8/Y1uH1NuP7k4cepzJB2 Car4lucChW9+llM4mz1BADQZuo4J1v71K5DR8mVXyM2usUlNWelkgR6GVWUmXE0= =trwj -----END PGP SIGNATURE----- From dsdale24 at gmail.com Sat Dec 10 07:41:13 2011 From: dsdale24 at gmail.com (Darren Dale) Date: Sat, 10 Dec 2011 04:41:13 -0800 (PST) Subject: WeakValueDict and threadsafety Message-ID: I am using a WeakValueDict in a way that is nearly identical to the example at the end of http://docs.python.org/library/weakref.html?highlight=weakref#example , where "an application can use objects IDs to retrieve objects that it has seen before. The IDs of the objects can then be used in other data structures without forcing the objects to remain alive, but the objects can still be retrieved by ID if they do." My program is multithreaded, so I added the necessary check for liveliness that was discussed at http://docs.python.org/library/weakref.html?highlight=weakref#weak-reference-objects . Basically, I have: import threading import weakref registry = weakref.WeakValueDictionary() reglock = threading.Lock() def get_data(oid): with reglock: data = registry.get(oid, None) if data is None: data = make_data() registry[id(data)] = data return data I'm concerned that this is not actually thread-safe. When I no longer hold strong references to an instance of data, at some point the garbage collector will kick in and remove that entry from my registry. How can I ensure the garbage collection process does not modify the registry while I'm holding the lock? Thanks, Darren From python.list at tim.thechases.com Sat Dec 10 08:31:41 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 10 Dec 2011 07:31:41 -0600 Subject: Obtaining user information In-Reply-To: <20111210073723.GA939@cskk.homeip.net> References: <4EE2B990.2030908@tim.thechases.com> <20111210073723.GA939@cskk.homeip.net> Message-ID: <4EE35F3D.70706@tim.thechases.com> On 12/10/11 01:37, Cameron Simpson wrote: > On 09Dec2011 19:44, Tim Chase wrote: > | Currently I can get the currently-logged-in-userid via > | getpass.getuser() which would yield something like "tchase". > > _If_ you're on a terminal. _And_ that's exactly what you want. > Personally I need to the name of geteuid() or getuid() more often. yes, it deals with emailing so the local userid and full-name are what I want. > | Is there a cross-platform way to get the full username (such as from > | the GECOS field of /etc/passed or via something like NetUserGetInfo > | on Win32 so I'd get "Tim Chase" instead? > > Hmm. Doesn't windows have a posix layer? > > pwd.getpwnam(os.getuid())[4].split(',')[0] > > is the best I've got. ANd it probably doesn't work in Windows:-( well, that's a more readable version of my hand-crafted opening of /etc/passwd and parsing by hand, so thanks! As you mention, the pwd module isn't available on Win32 so I still have to branch my code. I found Tim Golden's suggestion in a comment on ActiveState[1] that gave this one-liner for Win32: win32net.NetUserGetInfo (win32net.NetGetAnyDCName (), win32api.GetUserName (), 1) By changing the "1" to a "20", one of the returned key/value pairs was "full_name" and the username, so my code currently reads: def get_user_info(): "Return (userid, username) e.g. ('jsmith', 'John Smith')" userid = username = getpass.getuser() if sys.platform.lower().startswith("win"): try: import win32net, win32api USER_INFO_2 = 2 username = win32net.NetUserGetInfo( win32net.NetGetAnyDCName(), win32api.GetUserName(), USER_INFO_2, )["full_name"] or username except ImportError: pass # no win32* module, so default to userid else: import pwd username = pwd.getpwnam(userid).pw_gecos.split(',',1)[0] return userid, username It only addresses Win32 and Posix, but that's what I need for now. Thanks again. -tkc [1] http://code.activestate.com/recipes/66314-get-user-info-on-windows-for-current-user/ From dihedral88888 at googlemail.com Sat Dec 10 09:04:17 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 10 Dec 2011 06:04:17 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> Message-ID: <30440550.1225.1323525857598.JavaMail.geo-discussion-forums@prie17> Just wrap the exec() to spawn for fun. From dihedral88888 at googlemail.com Sat Dec 10 09:04:17 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 10 Dec 2011 06:04:17 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> Message-ID: <30440550.1225.1323525857598.JavaMail.geo-discussion-forums@prie17> Just wrap the exec() to spawn for fun. From dihedral88888 at googlemail.com Sat Dec 10 09:09:47 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 10 Dec 2011 06:09:47 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> Message-ID: <28734265.1226.1323526187915.JavaMail.geo-discussion-forums@prie17> Wrap functions to yield is somewhat like a sub-threading in Erlang. From dihedral88888 at googlemail.com Sat Dec 10 09:09:47 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 10 Dec 2011 06:09:47 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> Message-ID: <28734265.1226.1323526187915.JavaMail.geo-discussion-forums@prie17> Wrap functions to yield is somewhat like a sub-threading in Erlang. From duncan.booth at invalid.invalid Sat Dec 10 11:19:36 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 10 Dec 2011 16:19:36 GMT Subject: WeakValueDict and threadsafety References: Message-ID: Darren Dale wrote: > I'm concerned that this is not actually thread-safe. When I no longer > hold strong references to an instance of data, at some point the > garbage collector will kick in and remove that entry from my registry. > How can I ensure the garbage collection process does not modify the > registry while I'm holding the lock? You can't, but it shouldn't matter. So long as you have a strong reference in 'data' that particular object will continue to exist. Other entries in 'registry' might disappear while you are holding your lock but that shouldn't matter to you. What is concerning though is that you are using `id(data)` as the key and then presumably storing that separately as your `oid` value. If the lifetime of the value stored as `oid` exceeds the lifetime of the strong references to `data` then you might get a new data value created with the same id as some previous value. In other words I think there's a problem here, but nothing to do with the lock. -- Duncan Booth From thudfoo at gmail.com Sat Dec 10 12:19:31 2011 From: thudfoo at gmail.com (xDog Walker) Date: Sat, 10 Dec 2011 09:19:31 -0800 Subject: how to test attribute existence of feedparser objects In-Reply-To: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> References: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> Message-ID: <201112100919.31782.thudfoo@gmail.com> On Thursday 2011 December 08 01:34, HansPeter wrote: > Hi, > > While using the feedparser library for downloading RSS feeds some of > the blog entries seem to have no title. > > File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__ > AttributeError: object has no attribute 'title' > > Is there a way to test the existence of an attribute? > From the Fine Manual for feedparser 5.1: Testing for Existence? Feeds in the real world may be missing elements, even elements that are required by the specification. You should always test for the existence of an element before getting its value. Never assume an element is present. Use standard Python dictionary functions such as has_key to test whether an element exists. Testing if elements are present? >>> import feedparser >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml') >>> d.feed.has_key('title') True >>> d.feed.has_key('ttl') False >>> d.feed.get('title', 'No title') u'Sample feed' >>> d.feed.get('ttl', 60) 60 -- I have seen the future and I am not in it. From steve+comp.lang.python at pearwood.info Sat Dec 10 12:51:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Dec 2011 17:51:16 GMT Subject: how to test attribute existence of feedparser objects References: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> Message-ID: <4ee39c14$0$29977$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Dec 2011 09:19:31 -0800, xDog Walker wrote: > Use standard Python dictionary functions such as has_key to test whether > an element exists. Idiomatic Python code today no longer uses has_key. # Was: d.feed.has_key('title') # Now preferred 'title' in d.feed -- Steven From ladasky at my-deja.com Sat Dec 10 12:53:53 2011 From: ladasky at my-deja.com (John Ladasky) Date: Sat, 10 Dec 2011 09:53:53 -0800 (PST) Subject: Multiprocessing bug, is information ever omitted from a traceback? References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> Message-ID: <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> On Dec 9, 9:00?pm, Terry Reedy wrote: > On 12/9/2011 6:14 PM, John Ladasky wrote: > > >http://groups.google.com/group/comp.lang.python/browse_frm/thread/751... > > > I'm programming in Python 2.6 on Ubuntu Linux 10.10, if it matters. > > It might, as many bugs have been fixed since. > Can you try the same code with the most recent 2.x release, 2.7.2? > Do you have working and non-working code that you can publicly release? > Can you reduce the size and dependencies so the examples are closer to > 'small' than 'large'? And in any case, self-contained? > > In my first response, I said you might have found a bug. A bogus > exception message qualifies. But to do much, we need minimal good/bad > examples that run or not on a current release (2.7.2 or 3.2.2). > > -- > Terry Jan Reedy All right, Terry, you've convinced me to look. This will take some time. I'll hack away at the two versions of my 500-line programs, until I have a minimal example. Why did you specify Python 2.7.2, instead of the 2.7.6 version that is being offered to me by Ubuntu Software Center? Does it matter? From dsdale24 at gmail.com Sat Dec 10 12:56:38 2011 From: dsdale24 at gmail.com (Darren Dale) Date: Sat, 10 Dec 2011 09:56:38 -0800 (PST) Subject: WeakValueDict and threadsafety References: Message-ID: On Dec 10, 11:19?am, Duncan Booth wrote: > Darren Dale wrote: > > I'm concerned that this is not actually thread-safe. When I no longer > > hold strong references to an instance of data, at some point the > > garbage collector will kick in and remove that entry from my registry. > > How can I ensure the garbage collection process does not modify the > > registry while I'm holding the lock? > > You can't, but it shouldn't matter. > > So long as you have a strong reference in 'data' that particular object > will continue to exist. Other entries in 'registry' might disappear while > you are holding your lock but that shouldn't matter to you. > > What is concerning though is that you are using `id(data)` as the key and > then presumably storing that separately as your `oid` value. If the > lifetime of the value stored as `oid` exceeds the lifetime of the strong > references to `data` then you might get a new data value created with the > same id as some previous value. > > In other words I think there's a problem here, but nothing to do with the > lock. Thank you for the considered response. In reality, I am not using id(data). I took that from the example in the documentation at python.org in order to illustrate the basic approach, but it looks like I introduced an error in the code. It should read: def get_data(oid): with reglock: data = registry.get(oid, None) if data is None: data = make_data(oid) registry[oid] = data return data Does that look better? I am actually working on the h5py project (bindings to hdf5), and the oid is an hdf5 object identifier. make_data(oid) creates a proxy object that stores a strong reference to oid. My concern is that the garbage collector is modifying the dictionary underlying WeakValueDictionary at the same time that my multithreaded code is trying to access it, producing a race condition. This morning I wrote a synchronized version of WeakValueDictionary (actually implemented in cython): class _Registry: def __cinit__(self): def remove(wr, selfref=ref(self)): self = selfref() if self is not None: self._delitem(wr.key) self._remove = remove self._data = {} self._lock = FastRLock() __hash__ = None def __setitem__(self, key, val): with self._lock: self._data[key] = KeyedRef(val, self._remove, key) def _delitem(self, key): with self._lock: del self._data[key] def get(self, key, default=None): with self._lock: try: wr = self._data[key] except KeyError: return default else: o = wr() if o is None: return default else: return o Now that I am using this _Registry class instead of WeakValueDictionary, my test scripts and my actual program are no longer producing segfaults. From bahamutzero8825 at gmail.com Sat Dec 10 13:38:30 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 10 Dec 2011 12:38:30 -0600 Subject: Multiprocessing bug, is information ever omitted from a traceback? In-Reply-To: <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> Message-ID: <4EE3A726.6010201@gmail.com> On 12/10/2011 11:53 AM, John Ladasky wrote: > Why did you specify Python 2.7.2, instead of the 2.7.6 version that is > being offered to me by Ubuntu Software Center? Does it matter? There is no Python 2.7.6. I think you have it confused with the version 2.7.2-6. If I'm not mistaken, that appended 6 has to do with packaging and nothing at all to do with the software itself. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From dihedral88888 at googlemail.com Sat Dec 10 13:44:22 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 10 Dec 2011 10:44:22 -0800 (PST) Subject: WeakValueDict and threadsafety In-Reply-To: References: Message-ID: <30381924.754.1323542662530.JavaMail.geo-discussion-forums@prol32> On Sunday, December 11, 2011 1:56:38 AM UTC+8, Darren Dale wrote: > On Dec 10, 11:19?am, Duncan Booth > wrote: > > Darren Dale wrote: > > > I'm concerned that this is not actually thread-safe. When I no longer > > > hold strong references to an instance of data, at some point the > > > garbage collector will kick in and remove that entry from my registry. > > > How can I ensure the garbage collection process does not modify the > > > registry while I'm holding the lock? > > > > You can't, but it shouldn't matter. > > > > So long as you have a strong reference in 'data' that particular object > > will continue to exist. Other entries in 'registry' might disappear while > > you are holding your lock but that shouldn't matter to you. > > > > What is concerning though is that you are using `id(data)` as the key and > > then presumably storing that separately as your `oid` value. If the > > lifetime of the value stored as `oid` exceeds the lifetime of the strong > > references to `data` then you might get a new data value created with the > > same id as some previous value. > > > > In other words I think there's a problem here, but nothing to do with the > > lock. > > Thank you for the considered response. In reality, I am not using > id(data). I took that from the example in the documentation at > python.org in order to illustrate the basic approach, but it looks > like I introduced an error in the code. It should read: > > def get_data(oid): > with reglock: > data = registry.get(oid, None) > if data is None: > data = make_data(oid) > registry[oid] = data > return data > > Does that look better? I am actually working on the h5py project > (bindings to hdf5), and the oid is an hdf5 object identifier. > make_data(oid) creates a proxy object that stores a strong reference > to oid. > > My concern is that the garbage collector is modifying the dictionary > underlying WeakValueDictionary at the same time that my multithreaded > code is trying to access it, producing a race condition. This morning > I wrote a synchronized version of WeakValueDictionary (actually > implemented in cython): > > class _Registry: > > def __cinit__(self): > def remove(wr, selfref=ref(self)): > self = selfref() > if self is not None: > self._delitem(wr.key) > self._remove = remove > self._data = {} > self._lock = FastRLock() > > __hash__ = None > > def __setitem__(self, key, val): > with self._lock: > self._data[key] = KeyedRef(val, self._remove, key) > > def _delitem(self, key): > with self._lock: > del self._data[key] > > def get(self, key, default=None): > with self._lock: > try: > wr = self._data[key] > except KeyError: > return default > else: > o = wr() > if o is None: > return default > else: > return o > > Now that I am using this _Registry class instead of > WeakValueDictionary, my test scripts and my actual program are no > longer producing segfaults. I'll prefer to get iterators and iterables that can accept a global signal called a clock to replace these CS mess. From ladasky at my-deja.com Sat Dec 10 14:02:35 2011 From: ladasky at my-deja.com (John Ladasky) Date: Sat, 10 Dec 2011 11:02:35 -0800 (PST) Subject: Multiprocessing bug, is information ever omitted from a traceback? References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> Message-ID: On Dec 10, 10:38?am, Andrew Berg wrote: > On 12/10/2011 11:53 AM, John Ladasky wrote:> Why did you specify Python 2.7.2, instead of the 2.7.6 version that is > > being offered to me by Ubuntu Software Center? ?Does it matter? > > There is no Python 2.7.6. I think you have it confused with the version > 2.7.2-6. If I'm not mistaken, that appended 6 has to do with packaging > and nothing at all to do with the software itself. > > -- > CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 How annoying, here's how Ubuntu Software Center described it: "Version: 2.7-6 (python2.7)" And here it is in the Synaptic Package Manager: "package: python2.7-minimal, installed version: 2.7-6" At first I considered that this truncated name might be the consequence of a Linux bug. But looking up and down my list of installed software in both Ubuntu Software Center and Synaptic, I can find version names which extend arbitrarily, such as my wxPython installation, for which the version number reads "2.8.11.0-0ubuntu4.1". Now my hypothesis is that someone manually enters the revision numbers into the Linux database, and they made a typo. From rosuav at gmail.com Sat Dec 10 14:02:48 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Dec 2011 06:02:48 +1100 Subject: Multiprocessing bug, is information ever omitted from a traceback? In-Reply-To: <4EE3A726.6010201@gmail.com> References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> <4EE3A726.6010201@gmail.com> Message-ID: On Sun, Dec 11, 2011 at 5:38 AM, Andrew Berg wrote: > On 12/10/2011 11:53 AM, John Ladasky wrote: >> Why did you specify Python 2.7.2, instead of the 2.7.6 version that is >> being offered to me by Ubuntu Software Center? ?Does it matter? > There is no Python 2.7.6. I think you have it confused with the version > 2.7.2-6. If I'm not mistaken, that appended 6 has to do with packaging > and nothing at all to do with the software itself. On 12/9/2011 6:14 PM, John Ladasky wrote: > I'm programming in Python 2.6 on Ubuntu Linux 10.10, if it matters. As with my Ubuntu 10.10 - it's version 2.6.6. (I also altinstalled 3.3 built straight from Mercurial, which is approximately as new a Python as one can have, but that doesn't count.) There's a few differences between 2.6 and 2.7; not usually enough to be concerned about in daily use, but when dealing with weird issues, it helps to have the latest release. ChrisA From duncan.booth at invalid.invalid Sat Dec 10 14:09:50 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 10 Dec 2011 19:09:50 GMT Subject: WeakValueDict and threadsafety References: Message-ID: Darren Dale wrote: > On Dec 10, 11:19?am, Duncan Booth > wrote: >> Darren Dale wrote: > def get_data(oid): > with reglock: > data = registry.get(oid, None) > if data is None: > data = make_data(oid) > registry[oid] = data > return data > > Does that look better? I am actually working on the h5py project > (bindings to hdf5), and the oid is an hdf5 object identifier. > make_data(oid) creates a proxy object that stores a strong reference > to oid. Yes, that looks better. > > Now that I am using this _Registry class instead of > WeakValueDictionary, my test scripts and my actual program are no > longer producing segfaults. > I think that so far as multi-thread race conditions are concerned Python usually tries to guarantee that you won't get seg faults. So if you were getting seg faults my guess would be that either you've found a bug in the WeakValueDictionary implementation or you've got a bug in some of your code outside Python. For example if your proxy object has a __del__ method to clean up the object it is proxying then you could be creating a new object with the same oid as one that is in the process of being destroyed (the object disappears from the WeakValueDictionary before the __del__ method is actually called). Without knowing anything about HDF5 I don't know if that's a problem but I could imagine you could end up creating a new proxy object that references something in the HDF5 library which you then destroy as part of cleaning up a previous incarnation of the object but continue to access through the new proxy. -- Duncan Booth http://kupuguy.blogspot.com From dsdale24 at gmail.com Sat Dec 10 15:14:23 2011 From: dsdale24 at gmail.com (Darren Dale) Date: Sat, 10 Dec 2011 12:14:23 -0800 (PST) Subject: WeakValueDict and threadsafety References: Message-ID: <8d89b83e-59a2-428c-a067-ffe6660729e1@d17g2000yql.googlegroups.com> On Dec 10, 2:09?pm, Duncan Booth wrote: > Darren Dale wrote: > > On Dec 10, 11:19?am, Duncan Booth > > wrote: > >> Darren Dale wrote: > > def get_data(oid): > > ? ? with reglock: > > ? ? ? ? data = registry.get(oid, None) > > ? ? ? ? if data is None: > > ? ? ? ? ? ? data = make_data(oid) > > ? ? ? ? ? ? registry[oid] = data > > ? ? return data > > > Does that look better? I am actually working on the h5py project > > (bindings to hdf5), and the oid is an hdf5 object identifier. > > make_data(oid) creates a proxy object that stores a strong reference > > to oid. > > Yes, that looks better. > > > > > Now that I am using this _Registry class instead of > > WeakValueDictionary, my test scripts and my actual program are no > > longer producing segfaults. > > I think that so far as multi-thread race conditions are concerned Python > usually tries to guarantee that you won't get seg faults. So if you were > getting seg faults my guess would be that either you've found a bug in the > WeakValueDictionary implementation or you've got a bug in some of your code > outside Python. Have you seen Alex Martelli's answer at http://stackoverflow.com/questions/3358770/python-dictionary-is-thread-safe ? The way I read that, it seems pretty clear that deleting items from a dict can lead to crashes in threaded code. (Well, he says as long as you don't performing an assignment or a deletion in threaded code, there may be issues, but at least it shouldn't crash.) > For example if your proxy object has a __del__ method to clean up the > object it is proxying then you could be creating a new object with the same > oid as one that is in the process of being destroyed (the object disappears > from the WeakValueDictionary before the __del__ method is actually called). > > Without knowing anything about HDF5 I don't know if that's a problem but I > could imagine you could end up creating a new proxy object that references > something in the HDF5 library which you then destroy as part of cleaning up > a previous incarnation of the object but continue to access through the new > proxy. We started having problems when HDF5 began recycling oids as soon as their reference count went to zero, which was why we began using IDProxy and the registry. The IDProxy implementation below does have a __dealloc__ method, which we use to decrease the HDF5's internal reference count to the oid. Adding these proxies and registry dealt with the issue of creating a new proxy that references an old oid (even in non-threaded code), but it created a rare (though common enough) segfault in multithreaded code. This synchronized registry is the best I have been able to do, and it seems to address the problem. Could you suggest another approach? cdef IDProxy getproxy(hid_t oid): # Retrieve an IDProxy object appropriate for the given object identifier cdef IDProxy proxy proxy = registry.get(oid, None) if proxy is None: proxy = IDProxy(oid) registry[oid] = proxy return proxy cdef class IDProxy: property valid: def __get__(self): return H5Iget_type(self.id) > 0 def __cinit__(self, id): self.id = id self.locked = 0 def __dealloc__(self): if self.id > 0 and (not self.locked) and H5Iget_type(self.id) > 0 \ and H5Iget_type(self.id) != H5I_FILE: H5Idec_ref(self.id) From roy at panix.com Sat Dec 10 15:47:54 2011 From: roy at panix.com (Roy Smith) Date: Sat, 10 Dec 2011 15:47:54 -0500 Subject: Overriding a global Message-ID: I've got a code pattern I use a lot. In each module, I create a logger for the entire module and log to it all over: logger = logging.getLogger('my.module.name') class Foo: def function(self): logger.debug('stuff') logger.debug('other stuff') and so on. This works, but every once in a while I decide that a particular function needs a more specific logger, so I can adjust the logging level for that function independent of the rest of the module. What I really want to do is: def function(self): logger = logger.getChild('function') logger.debug('stuff') logger.debug('other stuff') which lets me not have to change any lines of code other than inserting the one to redefine logger. Unfortunately, that's not legal Python (it leads to "UnboundLocalError: local variable 'logger' referenced before assignment"). Any ideas on the best way to implement this? From python at mrabarnett.plus.com Sat Dec 10 16:07:18 2011 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 10 Dec 2011 21:07:18 +0000 Subject: Overriding a global In-Reply-To: References: Message-ID: <4EE3CA06.50503@mrabarnett.plus.com> On 10/12/2011 20:47, Roy Smith wrote: > I've got a code pattern I use a lot. In each module, I create a logger > for the entire module and log to it all over: > > logger = logging.getLogger('my.module.name') > > class Foo: > def function(self): > logger.debug('stuff') > logger.debug('other stuff') > > and so on. This works, but every once in a while I decide that a > particular function needs a more specific logger, so I can adjust the > logging level for that function independent of the rest of the module. > What I really want to do is: > > def function(self): > logger = logger.getChild('function') > logger.debug('stuff') > logger.debug('other stuff') > > which lets me not have to change any lines of code other than inserting > the one to redefine logger. Unfortunately, that's not legal Python (it > leads to "UnboundLocalError: local variable 'logger' referenced before > assignment"). > > Any ideas on the best way to implement this? You could use a different name: def function(self): logger2 = logger.getChild('function') logger2.debug('stuff') logger2.debug('other stuff') or use 'globals': def function(self): logger = globals()['logger'].getChild('function') logger.debug('stuff') logger.debug('other stuff') From roy at panix.com Sat Dec 10 16:10:53 2011 From: roy at panix.com (Roy Smith) Date: Sat, 10 Dec 2011 16:10:53 -0500 Subject: Overriding a global References: Message-ID: MRAB wrote: > or use 'globals': > > def function(self): > logger = globals()['logger'].getChild('function') > logger.debug('stuff') > logger.debug('other stuff') Ah-ha! That's precisely what I was looking for. Much appreciated. From lie.1296 at gmail.com Sat Dec 10 18:41:59 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 11 Dec 2011 10:41:59 +1100 Subject: Misleading error message of the day In-Reply-To: <36bf64b8-d27f-4f9d-9cf7-c425d4b1d214@q27g2000prh.googlegroups.com> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> <36bf64b8-d27f-4f9d-9cf7-c425d4b1d214@q27g2000prh.googlegroups.com> Message-ID: On 12/09/2011 03:57 PM, alex23 wrote: > On Dec 9, 11:46 am, Lie Ryan wrote: >> perhaps the one that talks about `a, a.foo = 1, 2` blowing up? > > Are you sure you're not confusing this with the recent thread on 'x = > x.thing = 1'? Ah, yes I do From tjreedy at udel.edu Sat Dec 10 18:57:00 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Dec 2011 18:57:00 -0500 Subject: Multiprocessing bug, is information ever omitted from a traceback? In-Reply-To: References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> <4EE3A726.6010201@gmail.com> Message-ID: On 12/10/2011 2:02 PM, Chris Angelico wrote: > There's a few differences between 2.6 and 2.7; not usually enough to > be concerned about in daily use, but when dealing with weird issues, > it helps to have the latest release. There are 2 issues. First, 2.7.2 has perhaps a couple hundred bug fixes since 2.7.0/2.6.6 were released. So it is possible that this particular problem was fixed, or at least changed. Second (leaving security issues aside), a Py2 bug fix will only be applied to the post-2.7.2 tip and only after the bug has been demonstrated to exist in the post-2.7.2 tip. About the request for minimal code exhibiting the problem: each bug exposes a hole in the test suite. We try to add a new test case with each bug fix so fixed bugs stay fixed. So code examples not only show that the bug is in the latest version, but also serve as the basis for new tests. -- Terry Jan Reedy From lie.1296 at gmail.com Sat Dec 10 18:58:43 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 11 Dec 2011 10:58:43 +1100 Subject: order independent hash? In-Reply-To: <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/09/2011 10:27 PM, Steven D'Aprano wrote: > On Thu, 08 Dec 2011 10:30:01 +0100, Hrvoje Niksic wrote: > >> In a language like Python, the difference between O(1) and O(log n) is >> not the primary reason why programmers use dict; they use it because >> it's built-in, efficient compared to alternatives, and convenient to >> use. If Python dict had been originally implemented as a tree, I'm sure >> it would be just as popular. > > Except for people who needed dicts with tens of millions of items. who should be using a proper DBMS in any case. From tjreedy at udel.edu Sat Dec 10 19:14:16 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Dec 2011 19:14:16 -0500 Subject: Overriding a global In-Reply-To: References: Message-ID: On 12/10/2011 3:47 PM, Roy Smith wrote: > > What I really want to do is: > > def function(self): Add a global statement to rebind a global name: global logger > logger = logger.getChild('function') > logger.debug('stuff') > logger.debug('other stuff') > > which lets me not have to change any lines of code other than inserting > the one to redefine logger. Unfortunately, that's not legal Python (it > leads to "UnboundLocalError: local variable 'logger' referenced before > assignment"). > > Any ideas on the best way to implement this? -- Terry Jan Reedy From rosuav at gmail.com Sat Dec 10 19:17:48 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Dec 2011 11:17:48 +1100 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 11, 2011 at 10:58 AM, Lie Ryan wrote: > On 12/09/2011 10:27 PM, Steven D'Aprano wrote: >> Except for people who needed dicts with tens of millions of items. > > who should be using a proper DBMS in any case. Not necessarily. "Database" usually implies disk-based and relational, features that may well be quite superfluous; and a pure-memory database with no relational facilities... is basically a dict. So why not use one? ChrisA From tjreedy at udel.edu Sat Dec 10 19:19:27 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Dec 2011 19:19:27 -0500 Subject: Overriding a global In-Reply-To: References: Message-ID: On 12/10/2011 7:14 PM, Terry Reedy wrote: > On 12/10/2011 3:47 PM, Roy Smith wrote: >> >> What I really want to do is: >> >> def function(self): > > Add a global statement to rebind a global name: > global logger But I see that that is not what you want to do, which is to override the global name just within the function while still accessing the global name. MRAB's solution does that nicely. >> logger = logger.getChild('function') >> logger.debug('stuff') >> logger.debug('other stuff') >> >> which lets me not have to change any lines of code other than inserting >> the one to redefine logger. Unfortunately, that's not legal Python (it >> leads to "UnboundLocalError: local variable 'logger' referenced before >> assignment"). >> >> Any ideas on the best way to implement this? > > -- Terry Jan Reedy From lie.1296 at gmail.com Sat Dec 10 22:04:55 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 11 Dec 2011 14:04:55 +1100 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/11/2011 11:17 AM, Chris Angelico wrote: > On Sun, Dec 11, 2011 at 10:58 AM, Lie Ryan wrote: >> On 12/09/2011 10:27 PM, Steven D'Aprano wrote: >>> Except for people who needed dicts with tens of millions of items. >> >> who should be using a proper DBMS in any case. > > Not necessarily. "Database" usually implies disk-based and relational, > features that may well be quite superfluous; and a pure-memory > database with no relational facilities... is basically a dict. So why > not use one? It is very unlikely you'd have millions of items in a dict and you're not planning to do any data processing at all. In any case, there are very few use cases which requires the use of a dict with millions of items that wouldn't be better served by a proper database. From nobody at nowhere.com Sun Dec 11 01:42:13 2011 From: nobody at nowhere.com (Nobody) Date: Sun, 11 Dec 2011 06:42:13 +0000 Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Message-ID: On Fri, 09 Dec 2011 01:55:28 -0800, Massi wrote: > Thank you all for your replies, first of all my Sum function was an > example simplifying what I have to do in my real funciton. In general > the D dictionary is complex, with a lot of keys, so I was searching > for a quick method to access all the variables in it without doing the > explicit creation: > > a, b, c = D['a'], D['b'], D['c'] > > and without using directly the D dictionary (boring...). If just you're trying to avoid getting a repetitive strain injury in your right-hand little finger from typing all the [''], you could turn the keys into object attributes, e.g.: class DictObject: def __init__(self, d): for key, value in d.iteritems(): setattr(self, key, value) ... o = DictObject(D) # use o.a, o.b, etc From __peter__ at web.de Sun Dec 11 03:14:43 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 11 Dec 2011 09:14:43 +0100 Subject: Overriding a global References: Message-ID: Roy Smith wrote: > I've got a code pattern I use a lot. In each module, I create a logger > for the entire module and log to it all over: > > logger = logging.getLogger('my.module.name') > > class Foo: > def function(self): > logger.debug('stuff') > logger.debug('other stuff') > > and so on. This works, but every once in a while I decide that a > particular function needs a more specific logger, so I can adjust the > logging level for that function independent of the rest of the module. > What I really want to do is: > > def function(self): > logger = logger.getChild('function') > logger.debug('stuff') > logger.debug('other stuff') > > which lets me not have to change any lines of code other than inserting > the one to redefine logger. Unfortunately, that's not legal Python (it > leads to "UnboundLocalError: local variable 'logger' referenced before > assignment"). > > Any ideas on the best way to implement this? def function(self, logger=logger.getChild("function")): logger.debug("stuff") But the "best way" is to use another name. From hoogendoorn.eelco at gmail.com Sun Dec 11 05:49:23 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Sun, 11 Dec 2011 11:49:23 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE48AB3.7070807@gmail.com> Throwing an idea for a PEP out there: It strikes me that the def func(*args, **kwargs) syntax is rather unpytonic. It certainly did not have that 'for line in file' pythonic obviousness for me as a beginner. Plus, asterikses are impossible to google for, so finding out what exactly they do more or less forces you to write a forum post about it. A more readable form occurred to me, which also happens to be more flexible, and which I think is fully compatible with the syntax of the language: def func(parg, list(args), dict(kwargs)) Perhaps this is considered abuse of notation; dict(kwargs) already has a meaning rather different from the one we try to give it here; but then again the context (being inside a function definition) is unique and easily recognizable. An added advantage would be the possibility of using subclasses of dict and list as well; imagine how much more beautiful a lot of code would be if one could say def func(attrdict(kwargs)) Problems im still wrestling with: the same syntax could not be used when calling a function; that lack of symmetry would make things more confusing, not less. Thoughts? From rosuav at gmail.com Sun Dec 11 06:15:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Dec 2011 22:15:41 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE48AB3.7070807@gmail.com> References: <4EE48AB3.7070807@gmail.com> Message-ID: On Sun, Dec 11, 2011 at 9:49 PM, Eelco Hoogendoorn wrote: > Problems im still wrestling with: the same syntax could not be used when > calling a function; that lack of symmetry would make things more confusing, > not less. That symmetry is a large factor, IMHO. I can write a wrapper function like this: def fixedargs(x,y,z): return wrappedfunc(x,y,z) Or like this: def anyargs(*args,**kwargs): return wrappedfunc(*args,**kwargs) Either way, it's a perfect parallel, and that's very useful for noticing errors. With a keyworded syntax, that's going to be a lot harder. Another issue: You suggest being able to use "attrdict" or some other dict subclass. This means that, rather than being a language construct, this will involve a name lookup. And what happens if you have a class that subclasses both list and dict? Will it get the positional args, the keyword args, or both? Laudable notion, but I'm not sure that it'll actually work in practice. ChrisA From detlev at die-offenbachs.de Sun Dec 11 06:20:20 2011 From: detlev at die-offenbachs.de (Detlev Offenbach) Date: Sun, 11 Dec 2011 12:20:20 +0100 Subject: How to get a correct entry in the menu for a Python application on Mac OS X References: <4EE13048.8090307@canterbury.ac.nz> Message-ID: I got it working by creating a symbolic link to the Python interpreter to be used in my application package and using this symbolic link to start the main Python script. Gregory Ewing wrote: > Detlev Offenbach wrote: >> I am fairly new to Mac OS X and would like to know, what I have to do >> to make my Python application show the correct name in the menu bar. >> What did I do so far. I created an application package containing the >> .plist file with correct entries and a shell script, that starts the >> correct Python interpreter with the the main script. > > I don't think that will work, because the executable that > your shell script is starting is in an app bundle of its > own, and MacOSX will be using the plist from that bundle, > which just has the generic "Python" name in it. > > There are a couple of things you could do: > > 1) Use py2app to create your app bundle. It does the > right things -- not sure exactly what, but it works. > > 2) Hack things at run time. I use the following PyObjC > code in PyGUI to set the application name: > > from Foundation import NSBundle > > ns_bundle = NSBundle.mainBundle() > ns_info = ns_bundle.localizedInfoDictionary() > if not ns_info: > ns_info = ns_bundle.infoDictionary() > ns_info['CFBundleName'] = my_application_name > -- Detlev Offenbach detlev at die-offenbachs.de From duncan.booth at invalid.invalid Sun Dec 11 07:39:44 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 11 Dec 2011 12:39:44 GMT Subject: Verbose and flexible args and kwargs syntax References: <4EE48AB3.7070807@gmail.com> Message-ID: Chris Angelico wrote: > Either way, it's a perfect parallel, and that's very useful for > noticing errors. > > With a keyworded syntax, that's going to be a lot harder. If it used keywords then you could keep symmetry quite easily: def anyargs(arglist args, argdict kwargs): return wrappedfunc(arglist args, argdict kwargs) and you would have the advantage of two new keywords that people could actually search on. > > Another issue: You suggest being able to use "attrdict" or some other > dict subclass. This means that, rather than being a language > construct, this will involve a name lookup. And what happens if you > have a class that subclasses both list and dict? Will it get the > positional args, the keyword args, or both? Irrelevant, you can't subclass both list and dict: >>> class C(list, dict): pass Traceback (most recent call last): File "", line 1, in class C(list, dict): pass TypeError: multiple bases have instance lay-out conflict -- Duncan Booth http://kupuguy.blogspot.com From rosuav at gmail.com Sun Dec 11 08:09:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 12 Dec 2011 00:09:56 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE48AB3.7070807@gmail.com> Message-ID: On Sun, Dec 11, 2011 at 11:39 PM, Duncan Booth wrote: > Chris Angelico wrote: > If it used keywords then you could keep symmetry quite easily: > > ? ?def anyargs(arglist args, argdict kwargs): > ? ? ? ?return wrappedfunc(arglist args, argdict kwargs) > > and you would have the advantage of two new keywords that people could > actually search on. Yes, that's just a strict keywordification of the * and ** symbols. The same argument could be made for eliminating the standard algebraic + operator and replacing it with a keyword "__add__". I don't think that's worthwhile. The OP suggested using 'dict' and 'list' themselves as the keywords, thus allowing the use of subclasses. This would make it unsearchable, or else rather verbose: def anyargs(pack list args, pack dict kwargs): return wrappedfunc(pack list args, pack dict kwargs) With this syntax, what happens if you muck up list/dict? Or alternatively, the briefer syntax: def anyargs(pack list args, pack dict kwargs): return wrappedfunc(pack args, pack kwargs) which breaks the symmetry, and doesn't say which one you're doing - for instance, if you only use one out of list and dict args, it would make sense to have a variable "options" or "args" that gets unpacked to the function's arguments - and there's no way to see whether it's going to be keyword or positional. The verbose syntax has a lot going for it, but it's rather verbose. Why say "pack list args" when you can just say "*args"? Compare initializer syntax between Python and PHP: foo = ["asdf", "qwer", {1:2, 3:4}] $foo = array("asdf", "qwer", array(1=>2, 3=>4)) Is it more Pythonic to use explicitly-named types, or to have simple notation that's clear and easy to read? Or is this a matter for personal preference? >> Another issue: You suggest being able to use "attrdict" or some other >> dict subclass. This means that, rather than being a language >> construct, this will involve a name lookup. And what happens if you >> have a class that subclasses both list and dict? Will it get the >> positional args, the keyword args, or both? > > Irrelevant, you can't subclass both list and dict: > TypeError: multiple bases have instance lay-out conflict Ah. Curious. I've not done much with multiple inheritance in Python (come to think of it, I don't recall when I last used MI in _any_ language). In any case, there's still the potential unclarity as to _which_ of dict and list is the one that's been inherited, which requires a run-time lookup to solve. ChrisA From lists at cheimes.de Sun Dec 11 08:14:13 2011 From: lists at cheimes.de (Christian Heimes) Date: Sun, 11 Dec 2011 14:14:13 +0100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE48AB3.7070807@gmail.com> Message-ID: Am 11.12.2011 13:39, schrieb Duncan Booth: >> Another issue: You suggest being able to use "attrdict" or some other >> dict subclass. This means that, rather than being a language >> construct, this will involve a name lookup. And what happens if you >> have a class that subclasses both list and dict? Will it get the >> positional args, the keyword args, or both? > > Irrelevant, you can't subclass both list and dict: But you can fake it with __instancecheck__ and __subclasscheck__ methods. Christian From hoogendoorn.eelco at gmail.com Sun Dec 11 09:44:37 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Sun, 11 Dec 2011 15:44:37 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE4C1D5.8050203@gmail.com> > Yes, that's just a strict keywordification of the * and ** symbols. > The same argument could be made for eliminating the standard algebraic > + operator and replacing it with a keyword "__add__". I don't think > that's worthwhile. Well, its not quite the same in the sense that algebraic operators are essentially part of 'natural language', or at least extremely widely adopted. They have earned their own special symbols. Argument packing/unpacking is a very specific thing; a small corner of a particular programming language. However, as seen in the light of python 3 head-tail syntax, perhaps the above is not quite true, and one could argue that packing/unpacking of collections is indeed a quite general concept, deserving of its own symbols. Breaking uniformity with that use case would also be a bad thing; ideally, a verbose alternative to all occurances of collection packing/unpacking would be available. That said, a more verbose and flexible syntax would be desirable there too; as of now, the tail is always a list. I havnt read the discussions leading up to those design decisions, but that seems like a compromise to me; something like head,tuple(tail) = someiterable would be preferrable there too, id say From hoogendoorn.eelco at gmail.com Sun Dec 11 09:58:18 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Sun, 11 Dec 2011 15:58:18 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE4C50A.3010205@gmail.com> As for syntax; what about coopting/integrating with the function annotation syntax? so: def func(args: list, kwargs: attrdict) and correspondingly in the function call? a, b:tuple = someiterable? I guess a rule that interprets every function argument annotated as a subclass of list or dict as a special case would severely restrict its intended use though... From johann.spies at gmail.com Sun Dec 11 13:44:38 2011 From: johann.spies at gmail.com (Johann Spies) Date: Sun, 11 Dec 2011 20:44:38 +0200 Subject: Using python on OS X Lion Message-ID: I have recently upgraded my Snow Leopard to Lion and now I am having all problems with python on my system e.g.: I have install PIL with easy_install but when I do import PIL Python cannot find the pil library. Installing psycopg2 in the same way was successful and I can use psycopg2 without a problem. Further, the new version of Python is 2.7.1 in Lion. But when I just run 'python' it calls up 2.6.5 - even after I have moved the application python2.6 to Trash. I see several guidelines for using python on OS X: some using macports, others homebrew and others 'pip' and 'virtualenv' . I come from a Linux background where a proper packaging system like apt (on Debian) prevent these types of problems. So my question: What is the recommended way of setting up a python environment in OS X? And how do I get rid of legacy python libraries on my system? Regards Johann -- Because experiencing your loyal love is better than life itself, my lips will praise you. (Psalm 63:3) -------------- next part -------------- An HTML attachment was scrubbed... URL: From johann.spies at gmail.com Sun Dec 11 13:57:38 2011 From: johann.spies at gmail.com (Johann Spies) Date: Sun, 11 Dec 2011 20:57:38 +0200 Subject: [OT] Book authoring In-Reply-To: <10973.1323447520@alphaville.dokosmarshall.org> References: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> <9925.1323445407@alphaville.dokosmarshall.org> <10973.1323447520@alphaville.dokosmarshall.org> Message-ID: Have a look at Markdown + pandoc. Regards Johann -- Because experiencing your loyal love is better than life itself, my lips will praise you. (Psalm 63:3) -------------- next part -------------- An HTML attachment was scrubbed... URL: From nad at acm.org Sun Dec 11 16:00:37 2011 From: nad at acm.org (Ned Deily) Date: Sun, 11 Dec 2011 13:00:37 -0800 Subject: Using python on OS X Lion References: Message-ID: In article , Johann Spies wrote: > I have recently upgraded my Snow Leopard to Lion and now I am having all > problems with python on my system e.g.: > > I have install PIL with easy_install but when I do > > import PIL > > Python cannot find the pil library. Installing psycopg2 in the same way was > successful and I can use psycopg2 without a problem. > > Further, the new version of Python is 2.7.1 in Lion. But when I just run > 'python' it calls up 2.6.5 - even after I have moved the application > python2.6 to Trash. > > I see several guidelines for using python on OS X: some using macports, > others homebrew and others 'pip' and 'virtualenv' . I come from a Linux > background where a proper packaging system like apt (on Debian) prevent > these types of problems. > > So my question: What is the recommended way of setting up a python > environment in OS X? > > And how do I get rid of legacy python libraries on my system? First, determine which python is being loaded. In OS X 10.7 (Lion), Apple ships 2.7.1, 2.6.7, and 2.5.6 version of Python (all in /usr/bin) so the 2.6.5 is coming from elsewhere. If you previously installed a Python from a python.org installer, the framework will be in /Library/Frameworks/Python.framework (and *not* /System/Library/Frameworks, which is where Apple's Pythons are and should not be modified). The python.org installers, by default, may modify your shell profiles (i.e. .bash_profile, .profile, et al) to prepend its framework bin directory to your shell PATH. Look for something like this: # Setting PATH for MacPython 2.6 # The orginal version is saved in .profile.pysave PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}" export PATH There are more details here http://bugs.python.org/issue7107 Also understand that if you use easy_install or pip, each Python instance needs to have its own version of either one. Apple supplies versions of easy_install in /usr/bin for its Pythons. If you install another version of Python, you'll need to install another easy_install and pip for it as well. -- Ned Deily, nad at acm.org From gelonida at gmail.com Sun Dec 11 16:27:23 2011 From: gelonida at gmail.com (Gelonida N) Date: Sun, 11 Dec 2011 22:27:23 +0100 Subject: unittest. customizing tstloaders / discover() Message-ID: Hi, I'd like to have a custom test loder, which will filter out certain tests or which just searches tests in certain directories. I'd like to use regular expresions as include / exclude rules and I would like to have another filter function, which would check for the existence of certain metavariabels in test suite files WHat would be the best starting point for such an example. Thanks a lot in advance for any info / pointer /tutorial . . .. From steve+comp.lang.python at pearwood.info Sun Dec 11 17:19:42 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Dec 2011 22:19:42 GMT Subject: Verbose and flexible args and kwargs syntax References: Message-ID: <4ee52c7e$0$11091$c3e8da3@news.astraweb.com> On Sun, 11 Dec 2011 11:49:23 +0100, Eelco Hoogendoorn wrote: > Throwing an idea for a PEP out there: > > It strikes me that the def func(*args, **kwargs) syntax is rather > unpytonic. It certainly did not have that 'for line in file' pythonic > obviousness for me as a beginner. Plus, asterikses are impossible to > google for, so finding out what exactly they do more or less forces you > to write a forum post about it. No more so than any other form of punctuation. Plus and minus + - may be so common that just about everyone knows it, but how about | == @ % and even . (dot)? None of these things will be obvious to newbies who have never programmed before. Oh well. Some things you just have to learn. As for impossibility of googling for asterisks, it might help if you spell it correctly http://duckduckgo.com/?q=python+asterisk Fourth hit says: *vargs puts all left-over non-keyword arguments into a tuple called vargs. **kargs puts all left-over keyword arguments into a dictionary called kargs. Even better, this works amazingly well: http://duckduckgo.com/?q=python+* Or you can Read The Fine Manual, which has a section for special symbols: http://docs.python.org/genindex-Symbols.html which is moderately easy to discover from the main page: Main page => Documentation => Browse Current Documentation => General Index => Symbols. Not ideal, but not too bad. > A more readable form occurred to me, which also happens to be more > flexible, and which I think is fully compatible with the syntax of the > language: > > def func(parg, list(args), dict(kwargs)) > > Perhaps this is considered abuse of notation; dict(kwargs) already has a > meaning rather different from the one we try to give it here; but then > again the context (being inside a function definition) is unique and > easily recognizable. I consider that excessively verbose as well as misleading. It is excessively verbose for the same reason as: let y equal x plus 1 would be considered excessively verbose. I'm very fond of some languages with verbose syntax, like Hypertalk and OpenXION where you might say "add 1 to x", but I don't want Python to become them. It's a judgement call as to where a language divides "cryptic punctuation line noise" and "useful short operators", and in my opinion * and ** tuple and dict unpacking fall strongly on the "useful short operators" side. Your opinion may differ, but luckily for me, the BDFL agrees with me :) It is also misleading because args are not collected into a list, but into a tuple. Worse, it suggests that one should be able to generalise to something like this: def func(parg, str(args), int(kwargs), my_func(more_args)): which is incoherent. And lastly, this would require either promoting list and dict to proper keywords, which will not happen, or worse, making them semi-keywords (treated as keywords in some contexts, but not in others), which also will not happen. > Problems im still wrestling with: the same syntax could not be used when > calling a function; that lack of symmetry would make things more > confusing, not less. Precisely. > Thoughts? People learn to spell strings "like this" instead of str(like this), and to use # for comments instead of REMARK or REM. It's not that much of a burden on them to learn to use * and ** -- Steven From tinnews at isbd.co.uk Sun Dec 11 17:28:19 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Sun, 11 Dec 2011 22:28:19 +0000 Subject: Documentation for python-evolution - where? Message-ID: <3dnfr8-h6a.ln1@chris.zbmc.eu> I'm trying to use the python evolution (as in Gnome Evolution) module but I can't find any documetation beyond the odd trivial example and the API documentation at http://doc.conduit-project.org/evolution-python/ (or similar places presumably). The trouble with the API documentation is that it tells me that evolution.ebook has three functions:- get_self_contact(...) list_addressbooks(...) open_addressbook(...) but then there is no documentation at all that I can find which tells me what the functions return and the functions/methods that can be called on the returned data. I know that open_addressbook() returns an address book (surprise!) but then, except for what I can glean from the examples I can find, there doesn't seem to any documentation on what methods I can call on the returned address book to extract data from it. Can anyone point me at anything with a bit of detail about the evolution module? -- Chris Green From tjreedy at udel.edu Sun Dec 11 18:26:39 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 11 Dec 2011 18:26:39 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE48AB3.7070807@gmail.com> References: <4EE48AB3.7070807@gmail.com> Message-ID: On 12/11/2011 5:49 AM, Eelco Hoogendoorn wrote: > Plus, asterikses are impossible to google for, so finding out what exactly > they do more or less forces you to write a forum post about it. There are other means of finding information than Google. Really. 1. A couple of years ago, I wrote a very complete list of symbol syntax uses in Python 3. You can get is at https://xploro.googlecode.com/files/PySymbols.html Unfortunately, searching for 'python syntax symbols' does not show this on the first few pages. 2. The python docs have an index. While the Symbols page does not have most of the entries in the above doc, it does have '* in function calls' and '** in function calls'. 3. The meaning of * and ** in function calls is explained, surprise, in the reference manual section (5.3.4 for Py3) on function calls, which is in the chapter on expressions. -- Terry Jan Reedy From bachth at uni-mainz.de Sun Dec 11 18:27:19 2011 From: bachth at uni-mainz.de (Thomas Bach) Date: Mon, 12 Dec 2011 00:27:19 +0100 Subject: unittest. customizing tstloaders / discover() References: Message-ID: <87y5uiu1t4.fsf@jubold.box> Gelonida N writes: > I'd like to use regular expresions as include / exclude rules > and I would like to have another filter function, which would check for > the existence of certain metavariabels in test suite files Did you have a look at nose? I'm using it and it supports include/exclude rules via RE and lets you select directories to run tests from. I'm not sure about the meta-variable thing, but it supports plug ins that could do the trick? From hoogendoorn.eelco at gmail.com Sun Dec 11 18:44:38 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 00:44:38 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE54066.3020901@gmail.com> > No more so than any other form of punctuation. Plus and minus + - may be > so common that just about everyone knows it, but how about | == @ % and > even . (dot)? None of these things will be obvious to newbies who have > never programmed before. Oh well. > Some things you just have to learn. Yes, some things you just have to learn. Nonetheless, I strongly prefer explicit logical operators over |, would much rather have 'equals' instead of ==, which is stylistic in line with 'is' and explicitly distinguishes between equality and identity comparisons. As for %; it is entirely unclear to me why that obscure operation ever got its own one-character symbol. Ill take 'mod', or even better, 'modulus' any day of the week. The dot is clearly quantitatively in another realm here. 90% of typical python code is attribute accesses. The dot is entirely unambigious and cannot be mistaken for anything else. It reads like a book. > It's a judgement call as to where a language divides "cryptic punctuation > line noise" and "useful short operators", and in my opinion * and ** tuple > and dict unpacking fall strongly on the "useful short operators" side. > Your opinion may differ, but luckily for me, the BDFL agrees with me :) I also agree that it is a value judgement as to which constructs get their own cryptic symbols and which do not, but the are some reasonable guidelines we should be able to agree upon. Obscure operations should not reserve any of the few available characters. Furthermore, the language should not just be formally consistent, but also easy to grasp at a glance, without deciphering subtle semantics of a blurb of weird characters. (some programming languages obviously disagree, but python, as far as I am allowed to speak for it, does not). And most importantly, if you cant come up with a terse syntax that does everything you want to do, the terse syntax should at best be an alternative to the verbose one. > It is also misleading because args are not collected into a list, but > into a tuple. In case you wanted a tuple youd write tuple(args), obviously. Exactly that added flexibility is half of my case in favor. Why shouldnt it be a list when I want it to? > Worse, it suggests that one should be able to generalise to > something like this: > def func(parg, str(args), int(kwargs), my_func(more_args)): > which is incoherent. Sorry, but I dont get this point at all. Does ** suggests one should be able to generalize to ***? The rules are the rules. The real questions, in my mind, are: 1) How useful is this added flexibility? Not insanely, but I can see it making a lot of code significantly more clean. And: 2) How fundamental is collection packing/unpacking? One can easily argue that it is indeed quite fundamental and therefore deserves its own terse symbols, I feel. However, if more flexibility is indeed deemed desirable, such terse syntax quickly gives way to a more verbose one. Can you come up with some terse symbols that will be able to express all of the below and dont make you wish you hadnt rather typed out the names? head, tuple(tail) = iterable head, list(tail) = iterable head, str(tail) = somestring head, generator(tail) = mygenerator And so on. If not, one has to admit that functionality is being sacrificed on the alter of terseness, which seems like a raw deal to me. From hoogendoorn.eelco at gmail.com Sun Dec 11 18:53:22 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 00:53:22 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE54272.8000005@gmail.com> > There are other means of finding information than Google. Really. This is really only a very minor point in my argument, so I dont want to put the focus on this. But really, no. Googling 'myprogramminglanguage conceptimtryingtofigureout' is my first, second and third line of defence. Yes, I could read the reference manual from top to bottom, and if I already knew about the existence of your article then im sure that would be a great help too. But the situation one finds oneself in is seeing two asterikses and not even being aware they are particular to function definitions/invocations. Im fluent in many different languages and well versed in CS concepts and jargon, but I had no idea what to search for when first trying to figure out the meaning of these symbols, and that does not happen often to me. From steve+comp.lang.python at pearwood.info Sun Dec 11 19:21:44 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Dec 2011 00:21:44 GMT Subject: What happened to module.__file__? Message-ID: <4ee54918$0$11091$c3e8da3@news.astraweb.com> I've just started using a Debian system, instead of the usual RedHat based systems I'm used to, and module.__file__ appears to have disappeared for some (but not all) modules. On Fedora: [steve at orac ~]$ python -E Python 2.6.2 (r262:71600, Aug 21 2009, 12:22:21) [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import math >>> math.__file__ '/usr/lib/python2.6/lib-dynload/mathmodule.so' and on Debian squeeze: steve at runes:~$ python -E Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import math >>> math.__file__ Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute '__file__' What's going on? -- Steven From rosuav at gmail.com Sun Dec 11 19:37:26 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 12 Dec 2011 11:37:26 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE54272.8000005@gmail.com> References: <4EE54272.8000005@gmail.com> Message-ID: On Mon, Dec 12, 2011 at 10:53 AM, Eelco Hoogendoorn wrote: > Googling 'myprogramminglanguage conceptimtryingtofigureout' is my first, > second and third line of defence. Yes, I could read the reference manual > from top to bottom, and if I already knew about the existence of your > article then im sure that would be a great help too. Python in particular has very poor internal search. I don't recommend searching the Python docs for anything that you can search for externally using Google, duckduckgo, etc, etc. ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 11 20:11:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Dec 2011 01:11:00 GMT Subject: Verbose and flexible args and kwargs syntax References: Message-ID: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> On Mon, 12 Dec 2011 00:44:38 +0100, Eelco Hoogendoorn wrote: >> No more so than any other form of punctuation. Plus and minus + - may >> be so common that just about everyone knows it, but how about | == @ % >> and even . (dot)? None of these things will be obvious to newbies who >> have never programmed before. Oh well. > >> Some things you just have to learn. > > > Yes, some things you just have to learn. Nonetheless, I strongly prefer > explicit logical operators over |, would much rather have 'equals' > instead of ==, which is stylistic in line with 'is' and explicitly > distinguishes between equality and identity comparisons. No more, or less, explicit than the difference between "==" and "is". > As for %; it is > entirely unclear to me why that obscure operation ever got its own > one-character symbol. Ill take 'mod', or even better, 'modulus' any day > of the week. Modulo is hardly an obscure operation. "What's the remainder...?" is a simple question that people learn about in primary school. And you can blame C for the use of % instead of mod or modulo. > The dot is clearly quantitatively in another realm here. 90% of typical > python code is attribute accesses. I can't imagine what sort of Python code you have seen that you consider 90% attribute access "typical". I've just run the Python tokenizer over my startup.py file, and I get these results: {'COMMENT': 24, 'DEDENT': 29, 'NL': 46, 'NAME': 256, "':'": 30, 'NEWLINE': 83, "'-'": 1, 'NUMBER': 1, "'['": 1, "','": 17, "')'": 37, "'('": 37, "'%'": 2, "'.'": 48, "'=='": 1, "'*'": 1, 'INDENT': 29, "']'": 1, "'='": 28, 'ENDMARKER': 1, 'STRING': 19} That gives attribute access being a little less than 7% of the source code. For the decimal module, the figure is a little less than 5%. > The dot is entirely unambigious and > cannot be mistaken for anything else. It reads like a book. The dot can be easily mistaken for a comma, or for a bit of grit on the monitor, especially at smaller type sizes, or for those with poor eyesight. [...] >> It is also misleading because args are not collected into a list, but >> into a tuple. > > In case you wanted a tuple youd write tuple(args), obviously. Exactly > that added flexibility is half of my case in favor. Why shouldnt it be a > list when I want it to? What sort of list? A built-in list, or whatever sort of object you get when you call the thing currently bound to the name "list"? If you can supply any function at all, what happens if I write this: def func(parg, dict(foo), list(bar)): ... How about this? def func(parg, myfunc(x)): ... What is x now? Should Python try to accumulate arguments by position, or by keyword, or try both and hope one will succeed? Which order should it try first? I believe that your proposal leads to an over-generalisation "call arbitrary functions when handling parameter lists". I don't believe you need this added complication. If you want to your var args as a list, call list(args) inside your function. >> Worse, it suggests that one should be able to generalise to something >> like this: > >> def func(parg, str(args), int(kwargs), my_func(more_args)): > >> which is incoherent. > > Sorry, but I dont get this point at all. Does ** suggests one should be > able to generalize to ***? The rules are the rules. You have missed that the generalization is not just to multiple "chunks" of arguments, but also to arbitrary functions. I thought that both ideas were equally incoherent, but ironically you suggest about that you should be able to call arbitrary functions: tuple, list, attrdict. What else? str? int? > The real questions, in my mind, are: > > 1) How useful is this added flexibility? Not insanely, but I can see it > making a lot of code significantly more clean. I don't. I see it making a small amount of code more verbose and less clean. > And: > > 2) How fundamental is collection packing/unpacking? One can easily argue > that it is indeed quite fundamental and therefore deserves its own terse > symbols, I feel. In Python, tuple unpacking and packing (actually applies to any collection, not just tuples) is *very* fundamental. That's why we can do things like this: a, b, c = my_list x, y = y, x > However, if more flexibility is indeed deemed > desirable, such terse syntax quickly gives way to a more verbose one. > Can you come up with some terse symbols that will be able to express all > of the below and dont make you wish you hadnt rather typed out the > names? > > head, tuple(tail) = iterable In Python 3, that is spelled: head, *tail = iterable tail = tuple(tail) > head, list(tail) = iterable head, *tail = iterable > head, str(tail) = somestring This is ambiguous, I'm not sure what exactly you expect to get as the string. It could arguable be any of: tail = ''.join(map(repr, tail)) tail = ''.join(map(str, tail)) tail = str(tail) or even head, tail = somestring[0], somestring[1:] > head, generator(tail) = mygenerator And this is most easily spelled: head, tail = next(mygenerator), mygenerator -- Steven From rosuav at gmail.com Sun Dec 11 20:13:32 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 12 Dec 2011 12:13:32 +1100 Subject: Documentation for python-evolution - where? In-Reply-To: <3dnfr8-h6a.ln1@chris.zbmc.eu> References: <3dnfr8-h6a.ln1@chris.zbmc.eu> Message-ID: On Mon, Dec 12, 2011 at 9:28 AM, wrote: > I'm trying to use the python evolution (as in Gnome Evolution) module > but I can't find any documetation beyond the odd trivial example and > the API documentation at http://doc.conduit-project.org/evolution-python/ > (or similar places presumably). One of the downsides of software you don't pay money for is that, all too often, there's little or no documentation. One of the upsides of free software is that you can look at the source code. I don't know anything about Python-Evolution, but if you're prepared to do a bit of digging, you can probably find what you want here: http://git.gnome.org/browse/gnome-python-desktop/tree/evolution/ (You probably have a copy of the same content on your own hard disk somewhere, too.) Once you figure out what you wanted, do consider sending a patch to the maintainer(s), improving the docstrings and/or external documentation, so the next person has an easier task. :) Chris Angelico From newsboost at gmail.com Sun Dec 11 20:20:15 2011 From: newsboost at gmail.com (someone) Date: Mon, 12 Dec 2011 02:20:15 +0100 Subject: calling python (matplotlib) from c++ Message-ID: Hi all, I want to call python (matplotlib) from C++. I use ubuntu linux and compile with "-lpython2.7". Besides that, I have this line included: #include I can then do something like this: Py_Initialize(); PyRun_SimpleString("print ' '"); PyRun_SimpleString("print ' '"); PyRun_SimpleString("print ' '"); PyRun_SimpleString("print 'PYTHON RUNNING =============='"); PyRun_SimpleString("from time import time,ctime\n" "print 'Today is',ctime(time())\n"); // Py_Main(argc, argv); // start up the python interpreter Py_Finalize(); However - I cannot seem to pass two arrays of data, so I can plot the data using matplotlib... I cannot even start matplotlib... Please help/advice/come up with suggestions, someone who knows what to do :-) From python at mrabarnett.plus.com Sun Dec 11 20:25:10 2011 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 12 Dec 2011 01:25:10 +0000 Subject: What happened to module.__file__? In-Reply-To: <4ee54918$0$11091$c3e8da3@news.astraweb.com> References: <4ee54918$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4EE557F6.5040900@mrabarnett.plus.com> On 12/12/2011 00:21, Steven D'Aprano wrote: > I've just started using a Debian system, instead of the usual RedHat > based systems I'm used to, and module.__file__ appears to have > disappeared for some (but not all) modules. > > On Fedora: > > [steve at orac ~]$ python -E > Python 2.6.2 (r262:71600, Aug 21 2009, 12:22:21) > [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import math >>>> math.__file__ > '/usr/lib/python2.6/lib-dynload/mathmodule.so' > > > > and on Debian squeeze: > > steve at runes:~$ python -E > Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) > [GCC 4.4.5] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import math >>>> math.__file__ > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'module' object has no attribute '__file__' > > > What's going on? > The documentation for __file__ says: """The __file__ attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.""" Interestingly, none of the versions on Windows that I've tried have that attribute for the math module. Is it platform-dependent? From gelonida at gmail.com Sun Dec 11 20:32:14 2011 From: gelonida at gmail.com (Gelonida N) Date: Mon, 12 Dec 2011 02:32:14 +0100 Subject: unittest. customizing tstloaders / discover() In-Reply-To: <87y5uiu1t4.fsf@jubold.box> References: <87y5uiu1t4.fsf@jubold.box> Message-ID: On 12/12/2011 12:27 AM, Thomas Bach wrote: > Gelonida N writes: > >> I'd like to use regular expresions as include / exclude rules >> and I would like to have another filter function, which would check for >> the existence of certain metavariabels in test suite files > > Did you have a look at nose? I'm using it and it supports > include/exclude rules via RE and lets you select directories to run > tests from. > > I'm not sure about the meta-variable thing, but it supports plug ins > that could do the trick? > I looked at nose very briefly, but too short to form a real opinion and to understand whether nose would have any negative impact on existing unit tests. Do I loose anything if using nose. or example can all unit tests / doc tests still be run from nose? From nfirvine at nfirvine.com Sun Dec 11 21:14:58 2011 From: nfirvine at nfirvine.com (Nick Irvine) Date: Sun, 11 Dec 2011 18:14:58 -0800 (PST) Subject: Outputting raw MIDI in realtime on Linux Message-ID: <77fe6b9f-b148-4855-b238-9089678908de@n22g2000prh.googlegroups.com> I'm trying to output raw, realtime MIDI data (as in note on, note off, etc) in Linux from Python, and I want the easiest way to do it. I've been banging my head on the wall about this for quite some time. The reason for this is to reinvent the old vkeybd application, but it's just me screwing around, so not worth the time doing it in C or creating a whole whack of supporting libraries. What I have so far is vkeybd outputting to Qsynth (via an ALSA VirMidi) to pulseaudio, playing some lovely grand piano. I want to replace vkeybd with a Python program of my own design. Using Qsynth's Messages window, I can monitor the rawish MIDI signals flowing. According to http://www.tldp.org/HOWTO/MIDI-HOWTO-10.html , I should be able to just dump MIDI bytes into /dev/snd/midiC3D0. However, this yields nothing: Qsynth does not show receiving it. I even tried this C example from the OSS docs, with the same result: http://manuals.opensound.com/developer/midi.c.html (I updated the /dev line.) What do people use to output live MIDI on Linux, assuming it's possible? Thanks, Nick Irvine From wuwei23 at gmail.com Sun Dec 11 22:17:41 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 19:17:41 -0800 (PST) Subject: Outputting raw MIDI in realtime on Linux References: <77fe6b9f-b148-4855-b238-9089678908de@n22g2000prh.googlegroups.com> Message-ID: <1683c805-7e4a-4ab4-8b6b-e825b09a9252@v24g2000prn.googlegroups.com> On Dec 12, 12:14?pm, Nick Irvine wrote: > What do people use to output live MIDI on Linux, assuming it's > possible? Hey Nick, I've yet to try this myself although it's long been on my to-do list. There are a couple of packages on PyPI that emit MIDI: http://pypi.python.org/pypi?%3Aaction=search&term=midi There is also an older project that provides a basic midi step sequencer. I can't find a proper package or installer, but you might find something useful in the 'midi_functions.py' file here: http://www.akjmusic.com/software/ From wuwei23 at gmail.com Sun Dec 11 22:31:41 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 19:31:41 -0800 (PST) Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Message-ID: <5e253143-2dd6-4cd7-821d-311ffa7971ec@h37g2000pri.googlegroups.com> On Dec 11, 4:42?pm, Nobody wrote: > If just you're trying to avoid getting a repetitive strain injury in your > right-hand little finger from typing all the [''], you could turn > the keys into object attributes, e.g.: > > ? ? ? ? class DictObject: > ? ? ? ? ? ? def __init__(self, d): > ? ? ? ? ? ? ? ? for key, value in d.iteritems(): > ? ? ? ? ? ? ? ? ? ? setattr(self, key, value) > ? ? ? ? ... > ? ? ? ? o = DictObject(D) > ? ? ? ? # use o.a, o.b, etc I hate this kind of laziness. I'd spend at least 5 minutes trying to work out _why_ someone felt this was necessary and then get annoyed that it was just to avoid typing. From wuwei23 at gmail.com Sun Dec 11 22:35:03 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 19:35:03 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> Message-ID: <37dda2ec-0ff7-4df0-96a7-182656cefede@f36g2000prj.googlegroups.com> On Dec 11, 12:09?am, 88888 Dihedral wrote: > Wrap functions to yield is somewhat like ?a sub-threading in Erlang. Okay, now that you've replied to yourself several times, I'm convinced you're really a markov chainer. You might want to get your owner to tweak your response filter. From electronixtar at gmail.com Sun Dec 11 22:35:10 2011 From: electronixtar at gmail.com (est) Date: Sun, 11 Dec 2011 19:35:10 -0800 (PST) Subject: db agnostic admin for Flask? Message-ID: <21fa456b-67ef-4c00-af59-e3d238302b2e@t36g2000prj.googlegroups.com> Hi guys Are there any db agnostic admin for Flask/Django? Imagine you need CRUD lots of data from Mongodb, Redis, ini file or even remote sources, if such admin allows aggregate data regardless of DB types or syntax, it would be super useful. Anything like this exists? or should I start build one from scratch? From wuwei23 at gmail.com Sun Dec 11 22:36:07 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 19:36:07 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> Message-ID: <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> On Dec 9, 8:08?pm, Robert Kern wrote: > On 12/9/11 5:02 AM, alex23 wrote: > > The 3rd party 'decorator' module takes care of issues like docstrings > > & ?function signatures. I'd really like to see some of that > > functionality in the stdlib though. > > Much of it is: > > ? ?http://docs.python.org/library/functools#functools.update_wrapper Ah, cheers :) Is that a recent addition? The lack of note makes it seem like it was there from the beginning? From emekamicro at gmail.com Sun Dec 11 23:32:00 2011 From: emekamicro at gmail.com (Emeka) Date: Mon, 12 Dec 2011 06:32:00 +0200 Subject: Working with Descriptors Message-ID: Hello All, How do I get the __set__ to work here? import random class Die(object): def __init__(self, sides=6): self.sides = sides def __get__(self, instance, owner): return int(random.random() * self.sides) + 1 def __set__(self, instance, value): instance.__dict__[self.side] = value class Game(object): d6 = Die() d10 = Die(sides=10) d20 = Die(sides=20) Game.d3 = 90 (This failed) Regards, Emeka -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From jyoung79 at kc.rr.com Sun Dec 11 23:39:33 2011 From: jyoung79 at kc.rr.com (jyoung79 at kc.rr.com) Date: Mon, 12 Dec 2011 4:39:33 +0000 Subject: subprocess question Message-ID: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> Wondering if anyone could shed some light on the subprocess module? I'll admit I'm not that great at the shell. If I was wanting to get the size of the trash (on OS X), I could use: >>> os.system('du -sh ~/.Trash/') 11M /Users/jay/.Trash/ 0 Which gives me what I want. However, I've been reading that it's better to use subprocess. I did a test like so, but is this a good way to do this? >>> import subprocess >>> p = subprocess.Popen(['du', '-sh'], cwd='/Users/jay/.Trash/', stdout=subprocess.PIPE) >>> out, err = p.communicate() >>> out ' 11M\t.\n' >>> err >>> And another question - why can't I use the tilde as a shortcut to the home directory? >>> p = subprocess.Popen(['du', '-sh'], cwd='~/.Trash/', stdout=subprocess.PIPE) Traceback (most recent call last): File "", line 1, in File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in __init__ errread, errwrite) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1202, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory: '~/.Trash/' Thanks for looking at my questions. Jay From dihedral88888 at googlemail.com Sun Dec 11 23:51:58 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 11 Dec 2011 20:51:58 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> Message-ID: <13587584.2147.1323665518804.JavaMail.geo-discussion-forums@prnx7> On Monday, December 12, 2011 11:36:07 AM UTC+8, alex23 wrote: > On Dec 9, 8:08?pm, Robert Kern wrote: > > On 12/9/11 5:02 AM, alex23 wrote: > > > The 3rd party 'decorator' module takes care of issues like docstrings > > > & ?function signatures. I'd really like to see some of that > > > functionality in the stdlib though. > > > > Much of it is: > > > > ? ?http://docs.python.org/library/functools#functools.update_wrapper > > Ah, cheers :) Is that a recent addition? The lack of note makes it > seem like it was there from the beginning? To wrap a function properly is different from the 1-line lampda. This is really functional programming. Every function can be decorated to change into a different one easily. There is a method to replace every return action of a python function into an yield action without the source code. From ethan at stoneleaf.us Mon Dec 12 00:00:40 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 11 Dec 2011 21:00:40 -0800 Subject: Dynamic variable creation from string In-Reply-To: <5e253143-2dd6-4cd7-821d-311ffa7971ec@h37g2000pri.googlegroups.com> References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> <5e253143-2dd6-4cd7-821d-311ffa7971ec@h37g2000pri.googlegroups.com> Message-ID: <4EE58A78.7060500@stoneleaf.us> alex23 wrote: > On Dec 11, 4:42 pm, Nobody wrote: >> If just you're trying to avoid getting a repetitive strain injury in your >> right-hand little finger from typing all the [''], you could turn >> the keys into object attributes, e.g.: >> >> class DictObject: >> def __init__(self, d): >> for key, value in d.iteritems(): >> setattr(self, key, value) >> ... >> o = DictObject(D) >> # use o.a, o.b, etc > > I hate this kind of laziness. I'd spend at least 5 minutes trying to > work out _why_ someone felt this was necessary and then get annoyed > that it was just to avoid typing. For me, half of it is to avoid the typing, the other half to avoid the reading. ;) ~Ethan~ From ian.g.kelly at gmail.com Mon Dec 12 00:05:27 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 11 Dec 2011 22:05:27 -0700 Subject: Working with Descriptors In-Reply-To: References: Message-ID: On Sun, Dec 11, 2011 at 9:32 PM, Emeka wrote: > > Hello All, > > How do I get the __set__ to work here? > > import random > > class Die(object): > ??? def __init__(self, sides=6): > ??????? self.sides = sides > > ??? def __get__(self, instance, owner): > ??????? return int(random.random() * self.sides) + 1 > > ??? def __set__(self, instance, value): > ??????? instance.__dict__[self.side] = value > > > > class Game(object): > ??? d6 = Die() > ??? d10 = Die(sides=10) > ??? d20 = Die(sides=20) > > > Game.d3 = 90 (This failed) I'm not sure exactly what it is you're trying to do with this, but there are a couple problems. First of all, at "Game.d3 = 90" you appear to be trying to set the value of a descriptor on a class object. This doesn't work, because __set__ doesn't get called when you try to set the descriptor on the class object directly. It only reassigns the attribute and replaces the descriptor. You need to either create an instance of Game and use the descriptors on that, or put the descriptor in a metaclass. Second, you're assigning to the d3 descriptor, but you never created a descriptor for the d3 attribute. The only descriptors on the Game class are d6, d10, and d20. If you're trying to assign to the descriptor, it would need to exist first. If you're trying to add a new descriptor, you would need to do something like "Game.d3 = Die(sides=3)". From bahamutzero8825 at gmail.com Mon Dec 12 00:24:14 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sun, 11 Dec 2011 23:24:14 -0600 Subject: subprocess question In-Reply-To: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> References: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> Message-ID: <4EE58FFE.1060408@gmail.com> On 12/11/2011 10:39 PM, jyoung79 at kc.rr.com wrote: > And another question - why can't I use the tilde as a shortcut to the home directory? Because subprocess doesn't use the shell (which is what expands the tilde to the invoking user's home directory). I recommend using os.path.join and os.environ anyway. p = subprocess.Popen(['du', '-sh', os.path.join(os.environ['home'], '.Trash')], stdout=subprocess.PIPE) -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From wuwei23 at gmail.com Mon Dec 12 00:45:31 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 21:45:31 -0800 (PST) Subject: tracking variable value changes References: Message-ID: <21825f6f-debe-4c9c-8efb-3f1d557cfe8a@d12g2000prg.googlegroups.com> Andrea Crotti wrote: > Not sure if it's exactly pure python but Traits can actually do thishttps://github.com/enthought/traits At an attribute level, absolutely, but not at the variable level like the OP is requesting. It's a great package, though :) From wuwei23 at gmail.com Mon Dec 12 00:47:52 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 21:47:52 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> <13587584.2147.1323665518804.JavaMail.geo-discussion-forums@prnx7> Message-ID: <4fa80db8-deec-4632-8402-f63f3fb91740@h37g2000pri.googlegroups.com> On Dec 12, 2:51?pm, 88888 Dihedral wrote: > To wrap a function properly is different from the 1-line lampda. > > This is really functional programming. > > Every function can be decorated to change into a different one easily. > > There is ?a method to replace every return action ?of a python function > into an ?yield action without the source code. How does this have _anything_ to do with my exchange with Robert? If you're _not_ a markov chainer, you're trying way too hard to show off what you know, and very little of it seems relevant to the thread. From clp2 at rebertia.com Mon Dec 12 01:02:23 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 11 Dec 2011 22:02:23 -0800 Subject: subprocess question In-Reply-To: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> References: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> Message-ID: On Sun, Dec 11, 2011 at 8:39 PM, wrote: > Wondering if anyone could shed some light on the subprocess module? ?I'll admit I'm not that great at the shell. > > If I was wanting to get the size of the trash (on OS X), I could use: > >>>> os.system('du -sh ~/.Trash/') > ?11M ? ?/Users/jay/.Trash/ > 0 > > Which gives me what I want. ?However, I've been reading that it's better to use subprocess. ?I did a test like so, but is this a good way to do this? > >>>> import subprocess >>>> p = subprocess.Popen(['du', '-sh'], cwd='/Users/jay/.Trash/', stdout=subprocess.PIPE) >>>> out, err = p.communicate() >>>> out > ' 11M\t.\n' You might prefer to use subprocess.check_output(); it slightly simplifies your code: http://docs.python.org/library/subprocess.html#subprocess.check_output > And another question - why can't I use the tilde as a shortcut to the home directory? Because ~ is interpolated by the shell and `subprocess` does not use the shell by default for reasons that include efficiency and security. You can expand ~ yourself using os.path.expanduser(): http://docs.python.org/library/os.path.html#os.path.expanduser Alternatively, you can opt to use the shell by passing shell=True as an argument. Cheers, Chris -- http://rebertia.com From wuwei23 at gmail.com Mon Dec 12 01:50:32 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 22:50:32 -0800 (PST) Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> <5e253143-2dd6-4cd7-821d-311ffa7971ec@h37g2000pri.googlegroups.com> Message-ID: <627c1eb0-ff06-4108-a2df-38a0207bdacc@z22g2000prd.googlegroups.com> On Dec 12, 3:00?pm, Ethan Furman wrote: > For me, half of it is to avoid the typing, the other half to avoid the > reading. ?;) Believe me, I get that, I've been more than guilty of it in the past :) But the reading here is important. If I see a dictionary lookup, I expect it to be accessing a dictionary; if I see an attribute reference, it might be wrapped in a descriptor. (And yes, I'm very much aware that ultimately an attribute lookup _is_ a dictionary lookup, but it's everything else that potentially happens after that point that is important here.) Changing the visual short circuits any understandings; then again, I tend to hate pesudo/mini-DSLs for the same reason... I can't just rely on my understanding of the language, I need to understand a specific implementation and mentally flag whether it's relevant or not. I also don't understand the original desire to create dynamic variables either. Either you know the names in advance, in which case just set up the necessary scaffolding to inject them where you need them, or you _don't_ and there's no value in creating them anyway because you don't know how to refer to them in your code. So if you know the names, use dictionary unpacking: userData = dict(a = 1, b = 2, c = 3) def func(a,b,c): return a + b + c >>> func(**userData) 6 If the dictionary has more values than you need, catch & discard the ones you don't want in **kwargs. From python.leojay at gmail.com Mon Dec 12 02:07:48 2011 From: python.leojay at gmail.com (Leo Jay) Date: Mon, 12 Dec 2011 15:07:48 +0800 Subject: Cannot use multiprocessing and zip together on windows Message-ID: Hi All, I have a file p.zip, there is a __main__.py in it, and the content of __main__.py is: from multiprocessing import Process import os def f(): print 'in f, pid:', os.getpid() if __name__ == '__main__': print 'pid:', os.getpid() p = Process(target=f) p.start() p.join() On linux, I can get expected result for running "python p.zip" But on windows xp, I got: Traceback (most recent call last): File "", line 1, in File "C:\python27\lib\multiprocessing\forking.py", line 346, in main prepare(preparation_data) File "C:\python27\lib\multiprocessing\forking.py", line 454, in prepare assert main_name not in sys.modules, main_name AssertionError: __main__ It seems that the situation described here is similar: http://bugs.python.org/issue10128 But the patch doesn't work for me. Anybody knows how to fix this? Thanks. -- Best Regards, Leo Jay From wuwei23 at gmail.com Mon Dec 12 02:11:18 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 23:11:18 -0800 (PST) Subject: Dynamic variable creation from string References: Message-ID: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> On Dec 8, 3:09?am, Massi wrote: > in my script I have a dictionary whose items are couples in the form > (string, integer values), say > > D = {'a':1, 'b':2, 'c':3} > > This dictionary is passed to a function as a parameter, e.g. : > > def Sum(D) : > ? ? return D['a']+D['b']+D['c'] > > Is there a way to create three variables dynamically inside Sum in > order to re write the function like this? > > def Sum(D) : > ? ? # Here some magic to create a,b,c from D > ? ? return a+b+c Okay, here's a possible solution that doesn't rely on exec, but does use the third-party module byteplay (which I believe limits it to Python 2.5-2.7) and tries to retain as much as possible your syntax (with some slight adjustments): from byteplay import Code, opmap class VariableInjector(dict): def transmute(self, opcode, arg): if (opcode == opmap['LOAD_GLOBAL']) and (arg in self): self._transmuted.append(arg) return opmap['LOAD_FAST'], arg return opcode, arg def make_locals(self, args): locals = [] for arg in args: locals.append((opmap['LOAD_CONST'], self[arg])) locals.append((opmap['STORE_FAST'], arg)) return locals def bind_to(self, function): function.ofunc_code = function.func_code def _(*args, **kwargs): self._transmuted = [] code = Code.from_code(function.ofunc_code) code.code = [self.transmute(op, arg) for op, arg in code.code] code.code = self.make_locals(self._transmuted) + code.code function.func_code = code.to_code() return function(*args, **kwargs) return _ For your example, you'd use it like this: >>> def sum(): ... return a + b + c ... >>> def product(): ... return a * b * c ... >>> data = VariableInjector(a=1,b=2,c=3) >>> sum = data.bind_to(sum) >>> product = data.bind_to(product) >>> sum() 6 >>> product() 6 >>> data {'a': 1, 'c': 3, 'b': 2} >>> data['a'] = 100 >>> sum() 105 >>> product() 600 I'm not sure how rigorous this would be in real use but it's passed the few quick toy cases I've tried it out on. Any thanks should go to Michael Foord, as this borrows heavily from his self-less metaclass example: http://www.voidspace.org.uk/python/articles/metaclasses.shtml From huisky at gmx.com Mon Dec 12 02:22:00 2011 From: huisky at gmx.com (huisky) Date: Sun, 11 Dec 2011 23:22:00 -0800 (PST) Subject: Plot all contents in a two dimension list in a loop? Message-ID: <49b903d3-2b75-4be5-b9e6-f314b66f0906@t16g2000vba.googlegroups.com> Hi, say I have two lists a and b, which are both in two dimensions. I wanna to plot all curves by using pylab.plot(a[i],b[i]) in a loop for the range of the length(a). I got quite a lot of figures, but only the first one with the plot i want. how to eliminate those blank figures poping out? Rgds Huisky From ethan at stoneleaf.us Mon Dec 12 02:47:54 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 11 Dec 2011 23:47:54 -0800 Subject: I love the decorator in Python!!! In-Reply-To: <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> Message-ID: <4EE5B1AA.80909@stoneleaf.us> alex23 wrote: > On Dec 9, 8:08 pm, Robert Kern wrote: >> On 12/9/11 5:02 AM, alex23 wrote: >>> The 3rd party 'decorator' module takes care of issues like docstrings >>> & function signatures. I'd really like to see some of that >>> functionality in the stdlib though. >> Much of it is: >> >> http://docs.python.org/library/functools#functools.update_wrapper > > Ah, cheers :) Is that a recent addition? The lack of note makes it > seem like it was there from the beginning? Not sure how recent it is, but I do know it does *not* handle the signature. ~Ethan~ From greg.ewing at canterbury.ac.nz Mon Dec 12 03:09:43 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 12 Dec 2011 21:09:43 +1300 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> Message-ID: <9klr6aF2ovU1@mid.individual.net> Steven D'Aprano wrote: > Modulo is hardly an obscure operation. "What's the remainder...?" is a > simple question that people learn about in primary school. Well, sort of. The way I remember it, the remainder was just something that fell out as a side effect of division -- the annoying bit left over that you didn't know what to do with. We weren't taught to think of "finding the remainder" as a distinct operation that's useful in its own right. Once we were taught to do proper division with decimal points and everything, the concept of a remainder seemed to get discarded and was never mentioned again. A couple of years later we were briefly introduced to the concept of modulo arithmetic, but as far as I remember, the connection with division and remainders wasn't pointed out. Also, it was presented in a very abstract way, and I couldn't see any practical application for it at the time. (At that age, it hadn't occurred to me that some of the stuff we were being shown might be just pure mathematics for its own sake, and I was often thinking to myself, "Why am I being taught this?") It wasn't until much later when I got into programming that I began to see all the connections and applications. For people who don't become programmers, I suspect they never have much use for remainders in everyday life. Now, in a desperate attempt to stop rambling and get back on topic... > Eelco Hoogendoorn wrote: >>The dot is clearly quantitatively in another realm here. Also it has almost unchallenged supremacy as the attribute access operator in just about every language having some form of struct concept, going back to around Algol 68. Spelling of the mod operator is much more variable. > {'COMMENT': 24, 'DEDENT': 29, 'NL': 46, 'NAME': 256, "':'": 30, > 'NEWLINE': 83, "'-'": 1, 'NUMBER': 1, "'['": 1, "','": 17, "')'": 37, > "'('": 37, "'%'": 2, "'.'": 48, "'=='": 1, "'*'": 1, 'INDENT': 29, "']'": > 1, "'='": 28, 'ENDMARKER': 1, 'STRING': 19} > > That gives attribute access being a little less than 7% of the source > code. However, it's clearly the most commonly used *operator* by a large margin. > The dot can be easily mistaken for a comma, Not in my code, because I always put a space after a comma, and never after an attribute-access dot. (And if you can't tell whether there's a space there or not, you need a bigger font or better glasses. :-) Also, dots sit nicely under my little finger where they're easy to type. I like dots. Dots are a great goodness. -- Greg From greg.ewing at canterbury.ac.nz Mon Dec 12 03:25:40 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 12 Dec 2011 21:25:40 +1300 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE54272.8000005@gmail.com> Message-ID: <9kls47Fa7uU1@mid.individual.net> For what it's worth, googling for "python asterisk" gives this as the very first result: http://www.technovelty.org/code/python/asterisk.html which tells you exactly what you're probably wanting to know if you ask that. To check that this phenomemon isn't restricted to asterisks in particular, I also tried "python plus equals" and got http://stackoverflow.com/questions/2347265/what-does-plus-equals-do-in-python which is also a pretty good result. So the rule of thumb seems to be: if you're trying to google for punctuation, try spelling it out. -- Greg From tjreedy at udel.edu Mon Dec 12 03:29:53 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 03:29:53 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE54272.8000005@gmail.com> References: <4EE54272.8000005@gmail.com> Message-ID: On 12/11/2011 6:53 PM, Eelco Hoogendoorn wrote: >> There are other means of finding information than Google. Really. > > This is really only a very minor point in my argument, so I dont want to > put the focus on this. On the contrary, it is a major point. You want us to change the language so you can program by Google. Sorry, aint't gonna happen. > Googling 'myprogramminglanguage conceptimtryingtofigureout' is my first, > second and third line of defence. Yes, I could read the reference manual > from top to bottom, and if I already knew about the existence of your > article then im sure that would be a great help too. You left out skimming the table of contents and using the index. On the Windows version of the docs, one can just type the entry wanted in the entry box on the Index tab and the lookup is done for you. Two chars to type for '**'. > But the situation > one finds oneself in is seeing two asterikses and not even being aware > they are particular to function definitions/invocations. If you find a symbol in a particular context, the entry for the context seems a reasonable place to start. -- Terry Jan Reedy From tjreedy at udel.edu Mon Dec 12 03:44:51 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 03:44:51 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE54066.3020901@gmail.com> References: <4EE54066.3020901@gmail.com> Message-ID: On 12/11/2011 6:44 PM, Eelco Hoogendoorn wrote: > Can you come up with some terse symbols that will be able to express all > of the below and dont make you wish you hadnt rather typed out the names? > > head, tuple(tail) = iterable > head, list(tail) = iterable > head, str(tail) = somestring > head, generator(tail) = mygenerator The above examples are seldom needed in Python because we have one general method to repeatedly split a sequence into head and tail. it = iter(iterable) # 'it' now represents the sequenced iterable head = next(it) # 'it' now represents the tail after removing the head In other words, next(it) encompasses all of your examples and many more. Because 'it' is mutated to represent the tail, it does not need to be rebound and therefore is not. Iterable unpacking with a *target for leftovers is an entirely different use case. -- Terry Jan Reedy From e.hoogendoorn at uva.nl Mon Dec 12 04:08:04 2011 From: e.hoogendoorn at uva.nl (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 10:08:04 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE5C474.3070500@uva.nl> > On the contrary, it is a major point. Sorry, but im affraid it is up to ME to decide which point I feel are important. No, this is a minor point to me, and one that has been admirably put to rest by pointing out that spelling out the name of the symbol in google directly leads you to the information you are looking for. From hoogendoorn.eelco at gmail.com Mon Dec 12 04:12:22 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 10:12:22 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE5C576.2000307@gmail.com> > The above examples are seldom needed in Python because we have one > general method to repeatedly split a sequence into head and tail. > it = iter(iterable) # 'it' now represents the sequenced iterable > head = next(it) # 'it' now represents the tail after removing the head > In other words, next(it) encompasses all of your examples and many more. > Because 'it' is mutated to represent the tail, it does not need to be > rebound and therefore is not. The question in language design is never 'could we do these things before'. The answer is obvious: yes our CPUs are turing complete; we can do anything. The question is; how would we like to do them? So do you think the new head/tail unpacking features in python 3 are entirely uncalled for? I personally quite like them, but I would like them to be more general. From hoogendoorn.eelco at gmail.com Mon Dec 12 04:32:16 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 10:32:16 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE5CA20.7060502@gmail.com> > No more, or less, explicit than the difference between "==" and "is". == may be taken to mean identity comparison; 'equals' can only mean one thing. Of course 'formally' these symbols are well defined, but so is brainf*ck > Modulo is hardly an obscure operation. "What's the remainder...?" is a > simple question that people learn about in primary school. So is 'how much wood would a woodchucker chuck if a woodchucker could chuck wood?'. But how often does that concept turn up in your code? > And you can blame C for the use of % instead of mod or modulo. I didnt know one of Python's design goals was backwards compatibility with C. > I can't imagine what sort of Python code you have seen that you consider > 90% attribute access "typical". I've just run the Python tokenizer over > my startup.py file, and I get these results: Yes, that was a hyperbole; but quite an often used construct, is it not? > If you can supply any function at all, what happens if I write this: You cannot; only constructors modelling a sequence or a dict, and only in that order. Is that rule clear enough? > I believe that your proposal leads to an over-generalisation "call > arbitrary functions when handling parameter lists". I hope the above clears that up. It is as much about calling functions as ** is about raising kwargs to the power of. > I don't believe you > need this added complication. If you want to your var args as a list, > call list(args) inside your function. We dont strictly 'need' any language construct. Real men use assembler, right? > >/ head, tuple(tail) = iterable /> In Python 3, that is spelled: > head, *tail = iterable > tail = tuple(tail) Yes, I know. How is that not a lot more verbose and worse than what I have proposed in all possible ways? > head, tail = somestring[0], somestring[1:] Well yes, splendid; we can do that with lists too since the dawn of Python. What you are saying here in effect is that you think the head/tail syntax is superfluous; that youd rather see it eliminated than generalized. > head, tail = next(mygenerator), mygenerator Which again of course works, but is yet again of entirely different form than any of the above solutions, while conceptually doing the same thing. Certainly, there is room for improved elegance here? -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Mon Dec 12 04:37:40 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 10:37:40 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE5CB64.7060300@gmail.com> > On the contrary, it is a major point. You want us to change the language > so you can program by Google. Sorry, aint't gonna happen. On the contrary; I believe I get to decide which points I consider important. This one, I do not. Sorry for putting it in the first paragraph. From bachth at uni-mainz.de Mon Dec 12 05:44:07 2011 From: bachth at uni-mainz.de (Thomas Bach) Date: Mon, 12 Dec 2011 11:44:07 +0100 Subject: unittest. customizing tstloaders / discover() References: <87y5uiu1t4.fsf@jubold.box> Message-ID: <8739cqf4so.fsf@jubold.box> Gelonida N writes: > Do I loose anything if using nose. or example can all unit tests / doc > tests still be run from nose? AFAIK you don't loose anything by using nose ? the unittests should all be found and doctests can be run via `--with-doctest', I never used doctests though. regards From jpiitula at ling.helsinki.fi Mon Dec 12 05:59:05 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 12 Dec 2011 12:59:05 +0200 Subject: Verbose and flexible args and kwargs syntax References: Message-ID: Eelco Hoogendoorn writes: > As for %; it is entirely unclear to me why that obscure operation > ever got its own one-character symbol. Ill take 'mod', or even > better, 'modulus' any day of the week. The modulus is not the result but one of the arguments: when numbers x and y are congruent modulo n (stated in terms of the modulo operation: x mod n = y mod n), the modulus is n. A word for x mod n is remainder. I agree about the obscurity of using the percent sign as the operator. A quick google suggests that your use of 'modulus' is now popular among programmers. Past experience in mathematics newsgroups tells me that some mathematicians do not accept the existence of any remainder operator at all. Honest. (I see them but I cannot understand them.) From jeanmichel at sequans.com Mon Dec 12 06:13:33 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 12 Dec 2011 12:13:33 +0100 Subject: Overriding a global In-Reply-To: References: Message-ID: <4EE5E1DD.6040101@sequans.com> Roy Smith wrote: > MRAB wrote: > > >> or use 'globals': >> >> def function(self): >> logger = globals()['logger'].getChild('function') >> logger.debug('stuff') >> logger.debug('other stuff') >> > > Ah-ha! That's precisely what I was looking for. Much appreciated. > Using the same name for 2 different objects is a bad idea in general. In debug mode, i.e. the logger is configured with the debug level, you could simply write down the filename and the line number on your log events. formatter = logging.Formatter('%(name)s : %(filename)s %(lineno)s %(message)s') So you can easily see who logged what (if I'm not wrong, that's what you're trying to do) JM From hoogendoorn.eelco at gmail.com Mon Dec 12 06:18:42 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 03:18:42 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: Message-ID: By the way... Is there any particular reason why some of my replies do not show up on groups.google, and some of them do not show up on mail.python.org? Sorry to annoy people with reposting, but im going to be forced to do some of that until this is cleared up.... From hoogendoorn.eelco at gmail.com Mon Dec 12 06:22:25 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 03:22:25 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: Message-ID: <95663c15-804c-49f3-8168-023a5c93bc5e@w3g2000vbw.googlegroups.com> > The modulus is not the result but one of the arguments: when numbers x > and y are congruent modulo n (stated in terms of the modulo operation: > x mod n = y mod n), the modulus is n. A word for x mod n is remainder. > > I agree about the obscurity of using the percent sign as the operator. > > A quick google suggests that your use of 'modulus' is now popular > among programmers. Past experience in mathematics newsgroups tells me > that some mathematicians do not accept the existence of any remainder > operator at all. Honest. (I see them but I cannot understand them.) You are correct; the thing it computes is the remainder, not the modulus. Nonetheless, 'x modulus y' is how it is put in natural language, but I suppose math.remainder would be my preferred place to put this. From jpiitula at ling.helsinki.fi Mon Dec 12 06:46:56 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 12 Dec 2011 13:46:56 +0200 Subject: Verbose and flexible args and kwargs syntax References: <95663c15-804c-49f3-8168-023a5c93bc5e@w3g2000vbw.googlegroups.com> Message-ID: Eelco writes: > > The modulus is not the result but one of the arguments: when numbers x > > and y are congruent modulo n (stated in terms of the modulo operation: > > x mod n = y mod n), the modulus is n. A word for x mod n is remainder. > > > > I agree about the obscurity of using the percent sign as the operator. > > > > A quick google suggests that your use of 'modulus' is now popular > > among programmers. Past experience in mathematics newsgroups tells me > > that some mathematicians do not accept the existence of any remainder > > operator at all. Honest. (I see them but I cannot understand them.) > > You are correct; the thing it computes is the remainder, not the > modulus. Nonetheless, 'x modulus y' is how it is put in natural > language, but I suppose math.remainder would be my preferred place to > put this. I think it's 'x modulo y', which matches 'x and y are congruent modulo z', but now I fear that programming people have been developing a different habit. From dihedral88888 at googlemail.com Mon Dec 12 07:16:33 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 12 Dec 2011 04:16:33 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <4fa80db8-deec-4632-8402-f63f3fb91740@h37g2000pri.googlegroups.com> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> <13587584.2147.1323665518804.JavaMail.geo-discussion-forums@prnx7> <4fa80db8-deec-4632-8402-f63f3fb91740@h37g2000pri.googlegroups.com> Message-ID: <7918199.244.1323692193737.JavaMail.geo-discussion-forums@prlu4> On Monday, December 12, 2011 1:47:52 PM UTC+8, alex23 wrote: > On Dec 12, 2:51?pm, 88888 Dihedral > wrote: > > To wrap a function properly is different from the 1-line lampda. > > > > This is really functional programming. > > > > Every function can be decorated to change into a different one easily. > > > > There is ?a method to replace every return action ?of a python function > > into an ?yield action without the source code. > > How does this have _anything_ to do with my exchange with Robert? > > If you're _not_ a markov chainer, you're trying way too hard to show > off what you know, and very little of it seems relevant to the thread. I think in the CS way in the 5th generation computer languages. But I also think in the hardware way in the 2,3,4th generations. Why do you need to spawn a thread or a process that can be decorated by wrapping an yield for all kinds of parameter checking of functions? A lousy written function in any computer language indicates a bad taste of art. From hoogendoorn.eelco at gmail.com Mon Dec 12 07:21:15 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 04:21:15 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> Message-ID: <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> > No more, or less, explicit than the difference between "==" and "is". == may be taken to mean identity comparison; 'equals' can only mean one thing. Of course 'formally' these symbols are well defined, but so is brainf*ck > Modulo is hardly an obscure operation. "What's the remainder...?" is a > simple question that people learn about in primary school. So is 'how much wood would a woodchucker chuck if a woodchucker could chuck wood?'. But how often does that concept turn up in your code? > And you can blame C for the use of % instead of mod or modulo. I didnt know one of Python's design goals was backwards compatibility with C. > I can't imagine what sort of Python code you have seen that you consider > 90% attribute access "typical". I've just run the Python tokenizer over > my startup.py file, and I get these results: Yes, that was a hyperbole; but quite an often used construct, is it not? > If you can supply any function at all, what happens if I write this: You cannot; only constructors modelling a sequence or a dict, and only in that order. Is that rule clear enough? > I believe that your proposal leads to an over-generalisation "call > arbitrary functions when handling parameter lists". I hope the above clears that up. It is as much about calling functions as ** is about raising kwargs to the power of. > I don't believe you > need this added complication. If you want to your var args as a list, > call list(args) inside your function. We dont strictly 'need' any language construct. Real men use assembler, right? > > head, tuple(tail) = iterable > In Python 3, that is spelled: > head, *tail = iterable > tail = tuple(tail) Yes, I know. How is that not a lot more verbose and worse than what I have proposed in all possible ways? > head, tail = somestring[0], somestring[1:] Well yes, splendid; we can do that with lists too since the dawn of Python. What you are saying here in effect is that you think the head/tail syntax is superfluous; that youd rather see it eliminated than generalized. > head, tail = next(mygenerator), mygenerator Which again of course works, but is yet again of entirely different form than any of the above solutions, while conceptually doing the same thing. Certainly, there is room for improved elegance here? From dihedral88888 at googlemail.com Mon Dec 12 07:43:20 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 12 Dec 2011 04:43:20 -0800 (PST) Subject: Dynamic variable creation from string In-Reply-To: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> References: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> Message-ID: <21495782.256.1323693800361.JavaMail.geo-discussion-forums@prmw6> On Monday, December 12, 2011 3:11:18 PM UTC+8, alex23 wrote: > On Dec 8, 3:09?am, Massi wrote: > > in my script I have a dictionary whose items are couples in the form > > (string, integer values), say > > > > D = {'a':1, 'b':2, 'c':3} > > > > This dictionary is passed to a function as a parameter, e.g. : > > > > def Sum(D) : > > ? ? return D['a']+D['b']+D['c'] > > > > Is there a way to create three variables dynamically inside Sum in > > order to re write the function like this? > > > > def Sum(D) : > > ? ? # Here some magic to create a,b,c from D > > ? ? return a+b+c > > Okay, here's a possible solution that doesn't rely on exec, but does > use the third-party module byteplay (which I believe limits it to > Python 2.5-2.7) and tries to retain as much as possible your syntax > (with some slight adjustments): > > from byteplay import Code, opmap > > class VariableInjector(dict): > def transmute(self, opcode, arg): > if (opcode == opmap['LOAD_GLOBAL']) and (arg in self): > self._transmuted.append(arg) > return opmap['LOAD_FAST'], arg > return opcode, arg > > def make_locals(self, args): > locals = [] > for arg in args: > locals.append((opmap['LOAD_CONST'], self[arg])) > locals.append((opmap['STORE_FAST'], arg)) > return locals > > def bind_to(self, function): > function.ofunc_code = function.func_code > def _(*args, **kwargs): > self._transmuted = [] > code = Code.from_code(function.ofunc_code) > code.code = [self.transmute(op, arg) for op, arg in > code.code] > code.code = self.make_locals(self._transmuted) + > code.code > function.func_code = code.to_code() > return function(*args, **kwargs) > return _ > > For your example, you'd use it like this: > > >>> def sum(): > ... return a + b + c > ... > >>> def product(): > ... return a * b * c > ... > >>> data = VariableInjector(a=1,b=2,c=3) > >>> sum = data.bind_to(sum) > >>> product = data.bind_to(product) > >>> sum() > 6 > >>> product() > 6 > >>> data > {'a': 1, 'c': 3, 'b': 2} > >>> data['a'] = 100 > >>> sum() > 105 > >>> product() > 600 > > I'm not sure how rigorous this would be in real use but it's passed > the few quick toy cases I've tried it out on. > > Any thanks should go to Michael Foord, as this borrows heavily from > his self-less metaclass example: > http://www.voidspace.org.uk/python/articles/metaclasses.shtml From dihedral88888 at googlemail.com Mon Dec 12 07:49:10 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 12 Dec 2011 04:49:10 -0800 (PST) Subject: Dynamic variable creation from string In-Reply-To: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> References: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> Message-ID: <24728986.18.1323694150411.JavaMail.geo-discussion-forums@prfc16> On Monday, December 12, 2011 3:11:18 PM UTC+8, alex23 wrote: > On Dec 8, 3:09?am, Massi wrote: > > in my script I have a dictionary whose items are couples in the form > > (string, integer values), say > > > > D = {'a':1, 'b':2, 'c':3} > > > > This dictionary is passed to a function as a parameter, e.g. : > > > > def Sum(D) : > > ? ? return D['a']+D['b']+D['c'] > > > > Is there a way to create three variables dynamically inside Sum in > > order to re write the function like this? > > > > def Sum(D) : > > ? ? # Here some magic to create a,b,c from D > > ? ? return a+b+c > > Okay, here's a possible solution that doesn't rely on exec, but does > use the third-party module byteplay (which I believe limits it to > Python 2.5-2.7) and tries to retain as much as possible your syntax > (with some slight adjustments): > > from byteplay import Code, opmap > > class VariableInjector(dict): > def transmute(self, opcode, arg): > if (opcode == opmap['LOAD_GLOBAL']) and (arg in self): > self._transmuted.append(arg) > return opmap['LOAD_FAST'], arg > return opcode, arg > > def make_locals(self, args): > locals = [] > for arg in args: > locals.append((opmap['LOAD_CONST'], self[arg])) > locals.append((opmap['STORE_FAST'], arg)) > return locals > > def bind_to(self, function): > function.ofunc_code = function.func_code > def _(*args, **kwargs): > self._transmuted = [] > code = Code.from_code(function.ofunc_code) > code.code = [self.transmute(op, arg) for op, arg in > code.code] > code.code = self.make_locals(self._transmuted) + > code.code > function.func_code = code.to_code() > return function(*args, **kwargs) > return _ > > For your example, you'd use it like this: > > >>> def sum(): > ... return a + b + c > ... > >>> def product(): > ... return a * b * c > ... > >>> data = VariableInjector(a=1,b=2,c=3) > >>> sum = data.bind_to(sum) > >>> product = data.bind_to(product) > >>> sum() > 6 > >>> product() > 6 > >>> data > {'a': 1, 'c': 3, 'b': 2} > >>> data['a'] = 100 > >>> sum() > 105 > >>> product() > 600 > > I'm not sure how rigorous this would be in real use but it's passed > the few quick toy cases I've tried it out on. > > Any thanks should go to Michael Foord, as this borrows heavily from > his self-less metaclass example: > http://www.voidspace.org.uk/python/articles/metaclasses.shtml This is the way to write an assembler or to roll out a script language to be included in an app by users. From hfaber at invalid.net Mon Dec 12 08:27:40 2011 From: hfaber at invalid.net (Henrik Faber) Date: Mon, 12 Dec 2011 14:27:40 +0100 Subject: Confusion about decorators Message-ID: Hi group, I'm a bit confused regarding decorators. Recently started playing with them with Python3 and wanted (as an excercise) to implement a simple type checker first: I know there are lots of them out there, this is actually one of the reasons I chose that particular function (to compare my solution against other, proven solutions). Starting with a blank slate, I did something along the lines of: class _TypeCheckedFunction(): def __init__(self, decoratedfunction): self._decoratedfunction = decoratedfunction def __call__(self, *args, **kwargs): [...] Actual checking def typecheck(wrappedfunction): checkfunction = _TypeCheckedFunction(wrappedfunction) functools.update_wrapper(checkfunction, wrappedfunction) return checkfunction And decorate my methods like @typecheck def setbar(self, bar: str): This works somewhat. The problem is, however, when the method is actually called. This is what happens: 1. The decorator is called upon import of the decorated class. It creates a _TypeCheckedFunction(setbar) object. 2. When setbar is actually called (blubb.setbar("fooobar")), the __call__ method of the previously created _TypeCheckedFunction is invoked. 3. When trying to call self._decoratedfunction from within that object, this fails: "self" is missing! self._decoratedfunction is only the *function*, not the bound function of the object that contains setbar(). Therefore I cannot proceed here. Solutions that I have seen working usually consist of two functions wrapped in each other, but I do not know why the additional introduction of a class makes everything fail. Can someone please enlighten me? Best regards, Henrik From andrea.crotti.0 at gmail.com Mon Dec 12 08:37:32 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Mon, 12 Dec 2011 13:37:32 +0000 Subject: Confusion about decorators In-Reply-To: References: Message-ID: <4EE6039C.2000205@gmail.com> On 12/12/2011 01:27 PM, Henrik Faber wrote: > Hi group, > > I'm a bit confused regarding decorators. Recently started playing with > them with Python3 and wanted (as an excercise) to implement a simple > type checker first: I know there are lots of them out there, this is > actually one of the reasons I chose that particular function (to compare > my solution against other, proven solutions). > > Starting with a blank slate, I did something along the lines of: > > class _TypeCheckedFunction(): > def __init__(self, decoratedfunction): > self._decoratedfunction = decoratedfunction > > def __call__(self, *args, **kwargs): > [...] Actual checking > > def typecheck(wrappedfunction): > checkfunction = _TypeCheckedFunction(wrappedfunction) > functools.update_wrapper(checkfunction, wrappedfunction) > return checkfunction > > And decorate my methods like > > @typecheck > def setbar(self, bar: str): > > This works somewhat. The problem is, however, when the method is > actually called. This is what happens: > > 1. The decorator is called upon import of the decorated class. It > creates a _TypeCheckedFunction(setbar) object. > 2. When setbar is actually called (blubb.setbar("fooobar")), the > __call__ method of the previously created _TypeCheckedFunction is invoked. > 3. When trying to call self._decoratedfunction from within that object, > this fails: "self" is missing! self._decoratedfunction is only the > *function*, not the bound function of the object that contains setbar(). > Therefore I cannot proceed here. > > Solutions that I have seen working usually consist of two functions > wrapped in each other, but I do not know why the additional introduction > of a class makes everything fail. > > Can someone please enlighten me? > > Best regards, > Henrik Not sure how that could work in general, what does "bar: str" should do? Is that a dictionary? Anyway there is already an implementation if you're interested for type checking: http://oakwinter.com/code/typecheck/ You can have a look at how they do it. From hfaber at invalid.net Mon Dec 12 08:45:11 2011 From: hfaber at invalid.net (Henrik Faber) Date: Mon, 12 Dec 2011 14:45:11 +0100 Subject: Confusion about decorators References: Message-ID: On 12.12.2011 14:37, Andrea Crotti wrote: > On 12/12/2011 01:27 PM, Henrik Faber wrote: >> Hi group, >> >> I'm a bit confused regarding decorators. Recently started playing with >> them with Python3 and wanted (as an excercise) to implement a simple >> type checker first: I know there are lots of them out there, this is >> actually one of the reasons I chose that particular function (to compare >> my solution against other, proven solutions). > > Not sure how that could work in general, what does "bar: str" should do? > Is that a dictionary? No. It's PEP 3107 function annotations. > Anyway there is already an implementation if you're interested for type > checking: > http://oakwinter.com/code/typecheck/ *sigh* no, not really -- this is exactly why I wrote "I know there are lots of them out there". I've actually seen and run http://code.activestate.com/recipes/577299-method-signature-type-checking-decorator-for-pytho/ However, this doesn't do it for me -- I want to know why my solution fails, not just use some other solution without really understanding it. I really would like to understand what's going on. I'm especially puzzled about the fact that in my solution, __call__ is called with only the method's arguments (i.e. "fooobar") in my example instead of two arguments (self, "fooobar"). Best regards, Henrik From arnodel at gmail.com Mon Dec 12 08:45:20 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 12 Dec 2011 13:45:20 +0000 Subject: Confusion about decorators In-Reply-To: References: Message-ID: On 12 December 2011 13:27, Henrik Faber wrote: > Hi group, > > I'm a bit confused regarding decorators. Recently started playing with > them with Python3 and wanted (as an excercise) to implement a simple > type checker first: I know there are lots of them out there, this is > actually one of the reasons I chose that particular function (to compare > my solution against other, proven solutions). > > Starting with a blank slate, I did something along the lines of: > > class _TypeCheckedFunction(): > ? ? ? ?def __init__(self, decoratedfunction): > ? ? ? ? ? ? ? ?self._decoratedfunction = decoratedfunction > > ? ? ? ?def __call__(self, *args, **kwargs): > ? ? ? ? ? ? ? ?[...] Actual checking > > def typecheck(wrappedfunction): > ? ? ? ?checkfunction = _TypeCheckedFunction(wrappedfunction) > ? ? ? ?functools.update_wrapper(checkfunction, wrappedfunction) > ? ? ? ?return checkfunction > > And decorate my methods like > > ? ? ? ?@typecheck > ? ? ? ?def setbar(self, bar: str): > > This works somewhat. The problem is, however, when the method is > actually called. This is what happens: > > 1. The decorator is called upon import of the decorated class. It > creates a _TypeCheckedFunction(setbar) object. > 2. When setbar is actually called (blubb.setbar("fooobar")), the > __call__ method of the previously created _TypeCheckedFunction is invoked. > 3. When trying to call self._decoratedfunction from within that object, > this fails: "self" is missing! self._decoratedfunction is only the > *function*, not the bound function of the object that contains setbar(). > Therefore I cannot proceed here. > > Solutions that I have seen working usually consist of two functions > wrapped in each other, but I do not know why the additional introduction > of a class makes everything fail. > > Can someone please enlighten me? You can (need to?) use the descriptor protocol to deal with methods. from functools import partial class _TypeCheckedFunction(): def __init__(self, decoratedfunction): self._decoratedfunction = decoratedfunction def __call__(self, *args, **kwargs): [...] Actual checking def __get__(self, obj, objtype): return partial(self, obj) (Untested) HTH -- Arnaud From hfaber at invalid.net Mon Dec 12 08:52:21 2011 From: hfaber at invalid.net (Henrik Faber) Date: Mon, 12 Dec 2011 14:52:21 +0100 Subject: Confusion about decorators References: Message-ID: On 12.12.2011 14:45, Arnaud Delobelle wrote: >> Can someone please enlighten me? > > You can (need to?) use the descriptor protocol to deal with methods. > > from functools import partial [...] > def __get__(self, obj, objtype): > return partial(self, obj) Whoa. This is absolutely fantastic, it now works as expected (I get a reference to "self"). I am very amazed -- I've been programming Python for about 5 years now and have never even come close to something as a "descriptor protocol". Python never ceases to amaze me. Do you have any beginners guide how this works? The Pydoc ("Data Model") is comprehensive, but I really don't know where to start to look. Still amazed! Best regards, Henrik From arnodel at gmail.com Mon Dec 12 09:01:31 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 12 Dec 2011 14:01:31 +0000 Subject: Confusion about decorators In-Reply-To: References: Message-ID: On 12 December 2011 13:52, Henrik Faber wrote: > On 12.12.2011 14:45, Arnaud Delobelle wrote: > >>> Can someone please enlighten me? >> >> You can (need to?) use the descriptor protocol to deal with methods. >> >> from functools import partial > [...] >> ? ? ? ?def __get__(self, obj, objtype): >> ? ? ? ? ? ? ? ?return partial(self, obj) > > Whoa. This is absolutely fantastic, it now works as expected (I get a > reference to "self"). > > I am very amazed -- I've been programming Python for about 5 years now > and have never even come close to something as a "descriptor protocol". > Python never ceases to amaze me. Do you have any beginners guide how > this works? The Pydoc ("Data Model") is comprehensive, but I really > don't know where to start to look. Well, I've been using Python for 10 years :) The best reference I know is: http://users.rcn.com/python/download/Descriptor.htm -- Arnaud From jyoung79 at kc.rr.com Mon Dec 12 09:49:06 2011 From: jyoung79 at kc.rr.com (jyoung79 at kc.rr.com) Date: Mon, 12 Dec 2011 14:49:06 +0000 Subject: subprocess question Message-ID: <20111212144906.3F3ZX.24725.root@cdptpa-web36-z01.mail.rr.com> >>>>> import subprocess >>>>> p = subprocess.Popen(['du', '-sh'], cwd='/Users/jay/.Trash/', >> stdout=subprocess.PIPE) >>>>> out, err = p.communicate() >>>>> out >> ' 11M\t.\n' > You might prefer to use subprocess.check_output(); it slightly > simplifies your code: > http://docs.python.org/library/subprocess.html#subprocess.check_output >> And another question - why can't I use the tilde as a shortcut to the home >> directory? > Because ~ is interpolated by the shell and `subprocess` does not use > the shell by default for reasons that include efficiency and security. > You can expand ~ yourself using os.path.expanduser(): > http://docs.python.org/library/os.path.html#os.path.expanduser > Alternatively, you can opt to use the shell by passing shell=True as > an argument. > Cheers, > Chris Thank you very much, Chris and Andrew, for your help. I appreciate it! :-) Jay From nobody at nowhere.com Mon Dec 12 10:16:58 2011 From: nobody at nowhere.com (Nobody) Date: Mon, 12 Dec 2011 15:16:58 +0000 Subject: subprocess question References: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> Message-ID: On Sun, 11 Dec 2011 22:02:23 -0800, Chris Rebert wrote: >>>>> p = subprocess.Popen(['du', '-sh'], cwd='/Users/jay/.Trash/', stdout=subprocess.PIPE) > Alternatively, you can opt to use the shell by passing shell=True as > an argument. Except that the OP is talking about a directory passed to the cwd= parameter, rather than as part of the command, and shell= doesn't affect that. But even if the directory was part of the command, just setting shell=True won't work. On Unix (including MacOSX) the call: subprocess.Popen(['du', '-sh'], shell=True) is equivalent to: subprocess.Popen(['/bin/sh', '-c', 'du', '-sh'], shell=False) This will result in the shell executing "du" with no arguments. The variable expansion "$1" will evaluate to "-sh", but that's meaningless as "$1" doesn't occur in the argument to "-c". The combination of using a list for the "args" parameter along with shell=True is rarely useful on Unix. And using a string for the "args" parameter introduces all of the associated reliability and security issues. The situation is different on Windows, where list/string and shell= are orthogonal. A list is always converted to a string according to the rules by which MSVCRT parses the command line into argv[]. Then, if shell=True, "cmd.exe /c " is prepended to the string (actually, the value of the COMSPEC environment variable is used in place of cmd.exe if it is defined). Setting shell=True allows the "program" to be any file with a registered extension, rather than just an executable. From hfaber at invalid.net Mon Dec 12 10:35:19 2011 From: hfaber at invalid.net (Henrik Faber) Date: Mon, 12 Dec 2011 16:35:19 +0100 Subject: Confusion about decorators References: Message-ID: On 12.12.2011 15:01, Arnaud Delobelle wrote: >> I am very amazed -- I've been programming Python for about 5 years now >> and have never even come close to something as a "descriptor protocol". >> Python never ceases to amaze me. Do you have any beginners guide how >> this works? The Pydoc ("Data Model") is comprehensive, but I really >> don't know where to start to look. > > Well, I've been using Python for 10 years :) The best reference I know is: > > http://users.rcn.com/python/download/Descriptor.htm Everyone starts out as a Padawan and I am no exception :-) Maybe five years from now I'll also have made my way to be a Python Jedi and also shake the ins and outs of descriptors out of my sleeve :-) But I can only repeat myself: Python is such an exceptional language, the more and more I know about it, the more I fall in love! Fantastic. I wish we had these types of language when I was a kid! Best regards and thanks again, Henrik From tjreedy at udel.edu Mon Dec 12 10:36:36 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 10:36:36 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <9klr6aF2ovU1@mid.individual.net> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <9klr6aF2ovU1@mid.individual.net> Message-ID: On 12/12/2011 3:09 AM, Gregory Ewing wrote: > people who don't become programmers, I suspect they never > have much use for remainders in everyday life. Huh? Funny you should say 'everyday'. It is now 10 o'clock. In 20 hours, it will be (10+20) % 12 == 6 o'clock. It is now day 1 of the week. In 9 days it will be day (1+9) % 7 == 3 of the week. Mental calculations are helped by the fact that (a+b) % c == a%c + b%c, so that would actually be 1+2==3. Timekeeping is mostly remaindering, slightly obscured by using 12 instead of 0. -- Terry Jan Reedy From tjreedy at udel.edu Mon Dec 12 10:39:48 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 10:39:48 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: Message-ID: On 12/12/2011 5:59 AM, Jussi Piitulainen wrote: > Past experience in mathematics newsgroups tells me > that some mathematicians do not accept the existence of any remainder > operator at all. Even though they carry hour/minute/second remindering devices on their bodies and put year/month/day remaindering devices on their wall? 'Twould be strange indeed! -- Terry Jan Reedy From tjreedy at udel.edu Mon Dec 12 10:52:25 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 10:52:25 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE5C576.2000307@gmail.com> References: <4EE5C576.2000307@gmail.com> Message-ID: On 12/12/2011 4:12 AM, Eelco Hoogendoorn wrote: >> The above examples are seldom needed in Python because we have one >> general method to repeatedly split a sequence into head and tail. > >> it = iter(iterable) # 'it' now represents the sequenced iterable >> head = next(it) # 'it' now represents the tail after removing the head > >> In other words, next(it) encompasses all of your examples and many more. >> Because 'it' is mutated to represent the tail, it does not need to be >> rebound and therefore is not. > > > The question in language design is never 'could we do these things > before'. The answer is obvious: yes our CPUs are turing complete; we can > do anything. The question is; how would we like to do them? > > So do you think the new head/tail unpacking features in python 3 are > entirely uncalled for? No, *target unpacking (singular) is quite useful in specialized cases. But it is not specifically head/tail unpacking. >>> a,*b,c = 1,2,3,4,5,6 >>> a,b,c (1, [2, 3, 4, 5], 6) >>> *a,b,c = 1,2,3,4,5 >>> a,b,c ([1, 2, 3], 4, 5) > I personally quite like them, but I would like them to be more general. It already is. The *target can be anywhere in the sequence. -- Terry Jan Reedy From nicholas.dokos at hp.com Mon Dec 12 10:55:44 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Mon, 12 Dec 2011 10:55:44 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: Message from Terry Reedy of "Mon, 12 Dec 2011 10:36:36 EST." References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <9klr6aF2ovU1@mid.individual.net> Message-ID: <26632.1323705344@alphaville.dokosmarshall.org> Terry Reedy wrote: > calculations are helped by the fact that (a+b) % c == a%c + b%c, so As long as we understand that == here does not mean "equal", only "congruent modulo c", e.g try a = 13, b = 12, c = 7. Nick From tylershemwell01 at gmail.com Mon Dec 12 10:56:49 2011 From: tylershemwell01 at gmail.com (Tyla (Tyler Shemwell) G-ESTEEM) Date: Mon, 12 Dec 2011 07:56:49 -0800 (PST) Subject: G-ESTEEM-PEOPLE WHO EDUCATED THEMSELVES & EVOLVED INTO THE MOST HAPPY AMERICANS USUALLY RED NECKS & PRO-BLACKS (STAY BOW-LEGGED & THICK). Message-ID: G-ESTEEM-PEOPLE WHO EDUCATED THEMSELVES & EVOLVED INTO THE MOST HAPPY AMERICANS USUALLY RED NECKS & PRO-BLACKS (STAY BOW-LEGGED & THICK). (Article w/ pic of President Obama & ex-Presidents.Bush,Bill Clinton,Bush Sr. & Jimmy Carter). STAY BOW-LEGGED & THICK G-ESTEEM-A tenacious confidence;mental toughness "If god is with me than who can be against me? I can do anything. G- ESTEEM Go hard,we dying soon. G-ESTEEM "The game is in belief" /I'm i doing too much/Or losing my touch/ Text 3060402 to 69937 for 'Night Time by Tyler Shemwell http://binged.it/v9LWsf STAY BOW-LEGGED & THICK Teach me it's more to being a man than feeling up your thigh. For Breast Cancer. G-ESTEEM (w/ pic of President Obama & Wife) http://tyler-shemwell.blogspot.com/2010/11/esteem.html ELECTION 2012 (For Freedom & Watch OUT FOR G-STINKY BAD PEOPLE) From tylershemwell01 at gmail.com Mon Dec 12 10:59:06 2011 From: tylershemwell01 at gmail.com (Tyla (Tyler Shemwell) G-ESTEEM) Date: Mon, 12 Dec 2011 07:59:06 -0800 (PST) Subject: Chew, they smelling like JUMBO BEAN BANQUET in there & they humming in that pic too. By G-ESTEEM Message-ID: They Smelling Like Naked Running Butt in that room. By G-ESTEEM Chew,they smelling like JUMBO BEAN BANQUET in there & they humming in that pic too below... http://www.myspace.com/tylershemwell/blog/544928378 /I'm i doing too much/Or losing my touch/ Text 3060402 to 69937 for 'Night Time by Tyler Shemwell http://binged.it/v9LWsf STAY BOW-LEGGED & THICK. G-ESTEEM They house so funky the pet bird even has the smell From dmitrey15 at gmail.com Mon Dec 12 11:00:07 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Mon, 12 Dec 2011 08:00:07 -0800 (PST) Subject: multiprocessing module question Message-ID: hi all, suppose I have a func F, list [args1,args2,args3,...,argsN] and want to obtain r_i = F(args_i) in parallel mode. My difficulty is: if F returns not None, than I should break calculations, and I can't dig in multiprocessing module documentation how to do it. Order doesn't matter for me (I have to find 1st suitable, where result of F is not None) Could anyone scribe me an example? Thank you in advance, D. From arnodel at gmail.com Mon Dec 12 11:10:00 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 12 Dec 2011 16:10:00 +0000 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE5C576.2000307@gmail.com> Message-ID: On 12 December 2011 15:52, Terry Reedy wrote: > No, *target unpacking (singular) is quite useful in specialized cases. But > it is not specifically head/tail unpacking. > >>>> a,*b,c = 1,2,3,4,5,6 >>>> a,b,c > (1, [2, 3, 4, 5], 6) >>>> *a,b,c = 1,2,3,4,5 >>>> a,b,c > ([1, 2, 3], 4, 5) > >> I personally quite like them, but I would like them to be more general. > > > It already is. The *target can be anywhere in the sequence. > > -- > Terry Jan Reedy You can even have nested sequences! >>> a, (b, *c), *d = 1, "two", 3, 4 -- Arnaud From info at wingware.com Mon Dec 12 11:13:40 2011 From: info at wingware.com (Wingware) Date: Mon, 12 Dec 2011 11:13:40 -0500 Subject: Wing IDE 4.1.2 released Message-ID: <4EE62834.4080506@wingware.com> Hi, Wingware has released version 4.1.2 of Wing IDE, an integrated development environment designed specifically for the Python programming language. Wing IDE is a cross-platform Python IDE that provides a professional code editor with vi, emacs, and other key bindings, auto-completion, call tips, refactoring, context-aware auto-editing, a powerful graphical debugger, version control, unit testing, search, and many other features. **Changes in Version 4.1.2** Highlights of this release include: * Added Create New Package to the Project context menu * Added Open Files tool for navigating to and closing open files * Added new code selection commands in the Edit > Select menu to select current/next/previous statement or scope * Many auto-editing, auto-indent, and auto-completion improvements * Fixed evaluation in shells of files with a coding comment * Several VI mode fixes * About 15 other bug fixes and minor improvements Complete change log: http://wingware.com/pub/wingide/4.1.2/CHANGELOG.txt **New Features in Version 4** Version 4 adds the following new major features: * Refactoring -- Rename/move symbols, extract to function/method, and introduce variable * Find Uses -- Find all points of use of a symbol * Auto-Editing -- Reduce typing burden by auto-entering expected code * Diff/Merge -- Graphical file and repository comparison and merge * Django Support -- Debug Django templates, run Django unit tests, and more * matplotlib Support -- Maintains live-updating plots in shell and debugger * Simplified Licensing -- Includes all OSes and adds Support+Upgrades subscriptions Details on licensing changes: http://wingware.com/news/2011-02-16 **About Wing IDE** Wing IDE is an integrated development environment designed specifically for the Python programming language. It provides powerful editing, testing, and debugging features that help reduce development and debugging time, cut down on coding errors, and make it easier to understand and navigate Python code. Wing IDE can be used to develop Python code for web, GUI, and embedded scripting applications. Wing IDE is available in three product levels: Wing IDE Professional is the full-featured Python IDE, Wing IDE Personal offers a reduced feature set at a low price, and Wing IDE 101 is a free simplified version designed for teaching beginning programming courses with Python. Version 4.0 of Wing IDE Professional includes the following major features: * Professional quality code editor with vi, emacs, and other keyboard personalities * Code intelligence for Python: Auto-completion, call tips, find uses, goto-definition, error indicators, refactoring, context-aware auto-editing, smart indent and rewrapping, and source navigation * Advanced multi-threaded debugger with graphical UI, command line interaction, conditional breakpoints, data value tooltips over code, watch tool, and externally launched and remote debugging * Powerful search and replace options including keyboard driven and graphical UIs, multi-file, wild card, and regular expression search and replace * Version control integration for Subversion, CVS, Bazaar, git, Mercurial, and Perforce * Integrated unit testing with unittest, nose, and doctest frameworks * Django support: Debugs Django templates, provides project setup tools, and runs Django unit tests * Many other features including project manager, bookmarks, code snippets, diff/merge tool, OS command integration, indentation manager, PyLint integration, and perspectives * Extremely configurable and may be extended with Python scripts * Extensive product documentation and How-Tos for Django, matplotlib, Plone, wxPython, PyQt, mod_wsgi, Autodesk Maya, and many other frameworks Please refer to http://wingware.com/wingide/features for a detailed listing of features by product level. System requirements are Windows 2000 or later, OS X 10.3.9 or later (requires X11 Server), or a recent Linux system (either 32 or 64 bit). Wing IDE supports Python versions 2.0.x through 3.2.x and Stackless Python. For more information, see the http://wingware.com/ **Downloads** Wing IDE Professional and Wing IDE Personal are commercial software and require a license to run. A free trial can be obtained directly from the product when launched. Wing IDE Pro -- Full-featured product: http://wingware.com/downloads/wingide/4.1 Wing IDE Personal -- A simplified IDE: http://wingware.com/downloads/wingide-personal/4.1 Wing IDE 101 -- For teaching with Python: http://wingware.com/downloads/wingide-101/4.1 **Purchasing and Upgrading** Wing 4.x requires an upgrade for Wing IDE 2.x and 3.x users at a cost of 1/2 the full product pricing. Upgrade a license: https://wingware.com/store/upgrade Purchase a new license: https://wingware.com/store/purchase Optional Support+Upgrades subscriptions are available for expanded support coverage and free upgrades to new major releases: http://wingware.com/support/agreement Thanks! -- The Wingware Team Wingware | Python IDE Advancing Software Development www.wingware.com From arnodel at gmail.com Mon Dec 12 11:15:05 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 12 Dec 2011 16:15:05 +0000 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <9klr6aF2ovU1@mid.individual.net> Message-ID: On 12 December 2011 15:36, Terry Reedy wrote: > Huh? Funny you should say 'everyday'. It is now 10 o'clock. In 20 hours, it > will be (10+20) % 12 == 6 o'clock. It is now day 1 of the week. In 9 days it > will be day (1+9) % 7 == 3 of the week. Mental calculations are helped by > the fact that (a+b) % c == a%c + b%c You mean (a + b) % c == (a%c + b%c) % c :) -- Arnaud From rosuav at gmail.com Mon Dec 12 11:16:08 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 03:16:08 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <26632.1323705344@alphaville.dokosmarshall.org> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <9klr6aF2ovU1@mid.individual.net> <26632.1323705344@alphaville.dokosmarshall.org> Message-ID: On Tue, Dec 13, 2011 at 2:55 AM, Nick Dokos wrote: > Terry Reedy wrote: >> calculations are helped by the fact that (a+b) % c == a%c + b%c, so > > As long as we understand that == here does not mean "equal", only > "congruent modulo c", e.g try a = 13, b = 12, c = 7. This is the basis of the grade-school "casting out nines" method of checking arithmetic. Set c=9 and you can calculate N%c fairly readily (digit sum - I'm assuming here that the arithmetic is being done in decimal); the sum of the remainders should equal the remainder of the sum, but there's the inherent assumption that if the remainders sum to something greater than nine, you digit-sum it to get the true remainder. (Technically the sum of the digits of a base-10 number is not the same as that number mod 9, but if you accept that 0 == 9, it works fine.) ChrisA From rosuav at gmail.com Mon Dec 12 11:19:39 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 03:19:39 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <9klr6aF2ovU1@mid.individual.net> Message-ID: On Tue, Dec 13, 2011 at 3:15 AM, Arnaud Delobelle wrote: > > You mean (a + b) % c == (a%c + b%c) % c > > :) It's just integer wraparound. Modulo 9 is the same as "render this number in base 9 and take the last digit" (and printing a number in base 9 would normally be done with mod 9 division), and most people can wrap their heads around the way an odometer wraps around. ChrisA From robert.kern at gmail.com Mon Dec 12 11:27:40 2011 From: robert.kern at gmail.com (Robert Kern) Date: Mon, 12 Dec 2011 16:27:40 +0000 Subject: I love the decorator in Python!!! In-Reply-To: <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> Message-ID: On 12/12/11 3:36 AM, alex23 wrote: > On Dec 9, 8:08 pm, Robert Kern wrote: >> On 12/9/11 5:02 AM, alex23 wrote: >>> The 3rd party 'decorator' module takes care of issues like docstrings >>> & function signatures. I'd really like to see some of that >>> functionality in the stdlib though. >> >> Much of it is: >> >> http://docs.python.org/library/functools#functools.update_wrapper > > Ah, cheers :) Is that a recent addition? The lack of note makes it > seem like it was there from the beginning? The module was added in Python 2.5 as noted at the top of the page. -- 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 From massi_srb at msn.com Mon Dec 12 11:30:54 2011 From: massi_srb at msn.com (Massi) Date: Mon, 12 Dec 2011 08:30:54 -0800 (PST) Subject: PyDev and multithreaded application debug Message-ID: <3f193cae-deab-4d3f-a7d6-01ae4611515d@t16g2000vba.googlegroups.com> Hi everyone, I've just started to use pydev to develop my python application and I'm encountering some problems to debug it. The application I'm dealing with is a multithreaded application; when I try to debug it with pydev, it seems not to be able to handle the execution of multiple threads. The problem is that when the execution reaches a breakpoint in one of the threads, all the other ones don't stop, but they continue their execution. Have I to make some specific settings or am I missing something? Thanks in advance! From robert.kern at gmail.com Mon Dec 12 11:38:00 2011 From: robert.kern at gmail.com (Robert Kern) Date: Mon, 12 Dec 2011 16:38:00 +0000 Subject: What happened to module.__file__? In-Reply-To: <4EE557F6.5040900@mrabarnett.plus.com> References: <4ee54918$0$11091$c3e8da3@news.astraweb.com> <4EE557F6.5040900@mrabarnett.plus.com> Message-ID: On 12/12/11 1:25 AM, MRAB wrote: > On 12/12/2011 00:21, Steven D'Aprano wrote: >> I've just started using a Debian system, instead of the usual RedHat >> based systems I'm used to, and module.__file__ appears to have >> disappeared for some (but not all) modules. >> >> On Fedora: >> >> [steve at orac ~]$ python -E >> Python 2.6.2 (r262:71600, Aug 21 2009, 12:22:21) >> [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. >>>>> import math >>>>> math.__file__ >> '/usr/lib/python2.6/lib-dynload/mathmodule.so' >> >> >> >> and on Debian squeeze: >> >> steve at runes:~$ python -E >> Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) >> [GCC 4.4.5] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. >>>>> import math >>>>> math.__file__ >> Traceback (most recent call last): >> File "", line 1, in >> AttributeError: 'module' object has no attribute '__file__' >> >> >> What's going on? >> > The documentation for __file__ says: """The __file__ attribute is not > present for C modules that are statically linked into the interpreter; > for extension modules loaded dynamically from a shared library, it is > the pathname of the shared library file.""" > > Interestingly, none of the versions on Windows that I've tried have > that attribute for the math module. Is it platform-dependent? It is build-dependent. Windows builds typically have math and several other stdlib "extension" modules built into the PythonXY.dll . Unix builds typically, but apparently not always, leave mathmodule.so and others as separate extension modules. -- 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 From tinnews at isbd.co.uk Mon Dec 12 11:40:43 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Mon, 12 Dec 2011 16:40:43 +0000 Subject: Documentation for python-evolution - where? References: <3dnfr8-h6a.ln1@chris.zbmc.eu> Message-ID: Chris Angelico wrote: > On Mon, Dec 12, 2011 at 9:28 AM, wrote: > > I'm trying to use the python evolution (as in Gnome Evolution) module > > but I can't find any documetation beyond the odd trivial example and > > the API documentation at http://doc.conduit-project.org/evolution-python/ > > (or similar places presumably). > > One of the downsides of software you don't pay money for is that, all > too often, there's little or no documentation. One of the upsides of > free software is that you can look at the source code. I don't know > anything about Python-Evolution, but if you're prepared to do a bit of > digging, you can probably find what you want here: > > http://git.gnome.org/browse/gnome-python-desktop/tree/evolution/ > Yes, thank you, I couldn't even find that. > (You probably have a copy of the same content on your own hard disk > somewhere, too.) > > Once you figure out what you wanted, do consider sending a patch to > the maintainer(s), improving the docstrings and/or external > documentation, so the next person has an easier task. :) > Actually I'm not sure if it's down to the docstrings because the help available from Python itself stops (not unreasonably) at the interface to the C library code. What I was after (and you have told me where it is) was the functions/methods available from the C library. -- Chris Green From corleone82 at gmail.com Mon Dec 12 11:41:00 2011 From: corleone82 at gmail.com (Juan Perez) Date: Mon, 12 Dec 2011 16:41:00 +0000 Subject: automate commands to an .exe console program through python Message-ID: Hi, I'm new to this mailing list and new in python as well. I need to automate a .exe console program in windows, and send key characters like 'a', 'M' ... I don't need to take the output as is printed in a different text log, but I'll have to send the messages to the program. I had this running in an autoIT script which with only activating cmd window, and with the "send" parameter I had all already done. But I'm having a lot of problems with python, I've tried with process.call, communicate(), os.popen ... but no results. I can't comunicate with the program and even i can't maintain it opened more than a few seconds. If I open a program like notepad.exe it remains opened but when I try to comunicate with the stdin pipe just don't write anything to the program. Someone has any idea about that, Thanks and sorry for my english level, Juan -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Mon Dec 12 11:50:55 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Mon, 12 Dec 2011 11:50:55 -0500 Subject: unittest. customizing tstloaders / discover() In-Reply-To: <8739cqf4so.fsf@jubold.box> References: <87y5uiu1t4.fsf@jubold.box> <8739cqf4so.fsf@jubold.box> Message-ID: Nose is absolutely the way to go for your testing needs. You can put "__test__ = False" in modules or classes to stop test collection. On Mon, Dec 12, 2011 at 5:44 AM, Thomas Bach wrote: > Gelonida N writes: > >> Do I loose anything if using nose. or example can all unit tests / doc >> tests still be run from nose? > > AFAIK you don't loose anything by using nose ? the unittests should all > be found and doctests can be run via `--with-doctest', I never used > doctests though. > > regards > -- > http://mail.python.org/mailman/listinfo/python-list From jpiitula at ling.helsinki.fi Mon Dec 12 11:52:49 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 12 Dec 2011 18:52:49 +0200 Subject: Verbose and flexible args and kwargs syntax References: Message-ID: Terry Reedy writes: > On 12/12/2011 5:59 AM, Jussi Piitulainen wrote: > > > Past experience in mathematics newsgroups tells me > > that some mathematicians do not accept the existence of any remainder > > operator at all. > > Even though they carry hour/minute/second remindering devices on their > bodies and put year/month/day remaindering devices on their wall? > 'Twould be strange indeed! They recognize modular arithmetic but for some reason insist that there is no such _binary operation_. But as I said, I don't understand their concern. (Except the related concern about some programming languages, not Python, where the remainder does not behave well with respect to division.) From hoogendoorn.eelco at gmail.com Mon Dec 12 12:12:10 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 09:12:10 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: > > I personally quite like them, but I would like them to be more general. > > It already is. The *target can be anywhere in the sequence. Im not sure if this is a genuine understanding, or trollish obtuseness. Yes, the target can be anywhere in the sequence. And yes, the resulting list can contain objects of any type, so its very flexible in that regard too. But to relate it to the topic of this thread: no, the syntax does not allow one to select the type of the resulting sequence. It always constructs a list. Yes, we can cast the list to be whatever we want on the next line, but the question is whether this language design can be improved upon. The choice of a list feels arbitrary, adding another line to cast it to something else would be even more verbose, and whats more, there would be serious performance implications if one should seek to apply this pattern to a deque/linkedlist; it would make taking off the head/tail of the list from a constant to a linear operation. That is: >>> head, deque(tail) = somedeque Is better in every way I can think of (readability, consistence, performance) than: >>> head, *tail = somedeque >>> tail = deque(tail) From hoogendoorn.eelco at gmail.com Mon Dec 12 12:29:11 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 09:29:11 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: Message-ID: > They recognize modular arithmetic but for some reason insist that > there is no such _binary operation_. But as I said, I don't understand > their concern. (Except the related concern about some programming > languages, not Python, where the remainder does not behave well with > respect to division.) They might not be willing to define it, but as soon as we programmers do, well, we did. Having studied the contemporary philosophy of mathematics, their concern is probably that in their minds, mathematics is whatever some dead guy said it was, and they dont know of any dead guy ever talking about a modulus operation, so therefore it 'does not exist'. Whatever you want to call the concept we are talking about, or whether you care to talk about it at all, it is most certainly a binary operation, since there are two arguments involved. There is no way around that. From gheskett at wdtv.com Mon Dec 12 12:46:07 2011 From: gheskett at wdtv.com (gene heskett) Date: Mon, 12 Dec 2011 12:46:07 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <26632.1323705344@alphaville.dokosmarshall.org> Message-ID: <201112121246.07854.gheskett@wdtv.com> On Monday, December 12, 2011 12:44:27 PM Chris Angelico did opine: > On Tue, Dec 13, 2011 at 2:55 AM, Nick Dokos wrote: > > Terry Reedy wrote: > >> calculations are helped by the fact that (a+b) % c == a%c + b%c, so > > > > As long as we understand that == here does not mean "equal", only > > "congruent modulo c", e.g try a = 13, b = 12, c = 7. > > This is the basis of the grade-school "casting out nines" method of > checking arithmetic. Set c=9 and you can calculate N%c fairly readily > (digit sum - I'm assuming here that the arithmetic is being done in > decimal); the sum of the remainders should equal the remainder of the > sum, but there's the inherent assumption that if the remainders sum to > something greater than nine, you digit-sum it to get the true > remainder. > > (Technically the sum of the digits of a base-10 number is not the same > as that number mod 9, but if you accept that 0 == 9, it works fine.) > > ChrisA And that is precisely the reason I have failed to understand why the 1-10 decimal system seems to have hung on for several hundred years when it is clearly broken. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: Grub first, then ethics. -- Bertolt Brecht From nicholas.dokos at hp.com Mon Dec 12 12:58:09 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Mon, 12 Dec 2011 12:58:09 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: Message from Jussi Piitulainen of "12 Dec 2011 18:52:49 +0200." References: Message-ID: <29931.1323712689@alphaville.dokosmarshall.org> Jussi Piitulainen wrote: > Terry Reedy writes: > > On 12/12/2011 5:59 AM, Jussi Piitulainen wrote: > > > > > Past experience in mathematics newsgroups tells me > > > that some mathematicians do not accept the existence of any remainder > > > operator at all. > > > > Even though they carry hour/minute/second remindering devices on their > > bodies and put year/month/day remaindering devices on their wall? > > 'Twould be strange indeed! > > They recognize modular arithmetic but for some reason insist that > there is no such _binary operation_. But as I said, I don't understand > their concern. (Except the related concern about some programming > languages, not Python, where the remainder does not behave well with > respect to division.) They are probably arguing that it's uniquely defined only on ZxN and that there are different conventions to extend it to ZxZ (the programming languages problem that you allude to above - although I don't know what you mean by "does not behave well wrt division"). See http://en.wikipedia.org/wiki/Remainder If you choose one convention and stick to it, it becomes a well-defined binary operation. C99 goes one way, python goes a different way (and mathematics textbooks generally go a third way) and they are all happy, as long as they don't try to talk to each other (e.g., porting C99 programs to python unthinkingly leads to trouble - duh). It was implementation dependent in old C (whatever the hardware would give you), which predictably - with 20-20 hindsight - turned out to be a Very Bad Idea. Nick PS Z = integers, N = non-negative integers From nicholas.dokos at hp.com Mon Dec 12 13:02:13 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Mon, 12 Dec 2011 13:02:13 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: Message from gene heskett of "Mon, 12 Dec 2011 12:46:07 EST." <201112121246.07854.gheskett@wdtv.com> References: <26632.1323705344@alphaville.dokosmarshall.org> <201112121246.07854.gheskett@wdtv.com> Message-ID: <30011.1323712933@alphaville.dokosmarshall.org> gene heskett wrote: > On Monday, December 12, 2011 12:44:27 PM Chris Angelico did opine: > > > On Tue, Dec 13, 2011 at 2:55 AM, Nick Dokos > wrote: > > > Terry Reedy wrote: > > >> calculations are helped by the fact that (a+b) % c == a%c + b%c, so > > > > > > As long as we understand that == here does not mean "equal", only > > > "congruent modulo c", e.g try a = 13, b = 12, c = 7. > > > > This is the basis of the grade-school "casting out nines" method of > > checking arithmetic. Set c=9 and you can calculate N%c fairly readily > > (digit sum - I'm assuming here that the arithmetic is being done in > > decimal); the sum of the remainders should equal the remainder of the > > sum, but there's the inherent assumption that if the remainders sum to > > something greater than nine, you digit-sum it to get the true > > remainder. > > > > (Technically the sum of the digits of a base-10 number is not the same > > as that number mod 9, but if you accept that 0 == 9, it works fine.) > > > > ChrisA > > And that is precisely the reason I have failed to understand why the 1-10 It's not clear from the above what you mean by "that is presicely the reason": what is "that"? > decimal system seems to have hung on for several hundred years when it is > clearly broken. > "broken" how? Thanks, Nick From d at davea.name Mon Dec 12 13:04:34 2011 From: d at davea.name (Dave Angel) Date: Mon, 12 Dec 2011 13:04:34 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <201112121246.07854.gheskett@wdtv.com> References: <26632.1323705344@alphaville.dokosmarshall.org> <201112121246.07854.gheskett@wdtv.com> Message-ID: <4EE64232.8080301@davea.name> On 12/12/2011 12:46 PM, gene heskett wrote: > On Monday, December 12, 2011 12:44:27 PM Chris Angelico did opine: > >> This is the basis of the grade-school "casting out nines" method of >> checking arithmetic. Set c=9 and you can calculate N%c fairly readily >> (digit sum - I'm assuming here that the arithmetic is being done in >> decimal); the sum of the remainders should equal the remainder of the >> sum, but there's the inherent assumption that if the remainders sum to >> something greater than nine, you digit-sum it to get the true >> remainder. >> >> (Technically the sum of the digits of a base-10 number is not the same >> as that number mod 9, but if you accept that 0 == 9, it works fine.) >> >> ChrisA > And that is precisely the reason I have failed to understand why the 1-10 > decimal system seems to have hung on for several hundred years when it is > clearly broken. > I assume this was facetious, but in case not, I'd point out that any other number base will have similar modulo characteristics, except for base 2, where all numbers are congruent modulo 1, so it doesn't do much for checking values. For example, if you were using a number system of base 8, you could do "casting out sevens" by adding the digits together. -- DaveA From ian.g.kelly at gmail.com Mon Dec 12 13:09:04 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 11:09:04 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> Message-ID: On Mon, Dec 12, 2011 at 5:21 AM, Eelco wrote: > You cannot; only constructors modelling a sequence or a dict, and > only > in that order. Is that rule clear enough? The dict constructor can receive either a sequence or a mapping, so if I write this: def func(a, b, dict(c)): what will I get? Probably I would want the equivalent of: def func(a, b, **c): but you seem to be saying that I would actually get the equivalent of this: def func(a, b, *c): c = dict(c) Cheers, Ian From jpiitula at ling.helsinki.fi Mon Dec 12 13:09:48 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 12 Dec 2011 20:09:48 +0200 Subject: Verbose and flexible args and kwargs syntax References: Message-ID: Eelco writes: > > They recognize modular arithmetic but for some reason insist that > > there is no such _binary operation_. But as I said, I don't understand > > their concern. (Except the related concern about some programming > > languages, not Python, where the remainder does not behave well with > > respect to division.) > > They might not be willing to define it, but as soon as we programmers > do, well, we did. > > Having studied the contemporary philosophy of mathematics, their > concern is probably that in their minds, mathematics is whatever some > dead guy said it was, and they dont know of any dead guy ever talking > about a modulus operation, so therefore it 'does not exist'. > > Whatever you want to call the concept we are talking about, or whether > you care to talk about it at all, it is most certainly a binary > operation, since there are two arguments involved. There is no way > around that. Yes, I think you nailed it. But I guess I'll still be confused the next time I meet one of them. Happens to me. :) From hoogendoorn.eelco at gmail.com Mon Dec 12 13:17:11 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 10:17:11 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> Message-ID: <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> On Dec 12, 7:09?pm, Ian Kelly wrote: > On Mon, Dec 12, 2011 at 5:21 AM, Eelco wrote: > > You cannot; only constructors modelling a sequence or a dict, and > > only > > in that order. Is that rule clear enough? > > The dict constructor can receive either a sequence or a mapping, so if > I write this: > > ? ? ? ? def func(a, b, dict(c)): > > what will I get? ?Probably I would want the equivalent of: > > ? ? ? ? def func(a, b, **c): > > but you seem to be saying that I would actually get the equivalent of this: > > ? ? ? ? def func(a, b, *c): > ? ? ? ? ? ? c = dict(c) > > Cheers, > Ian Im not sure if I was clear on that, but I dont care what the constructors accept; I meant to overload on the concept the underlying type models. Dicts model a mapping, lists/tuples model a sequence. MI deriving from both these models is illegal anyway, so one can unambigiously overload on that trait. The syntax only superficially resembles 'call the dict constructor with the object passed into kwargs'. What its supposed to mean is exactly the same as **kwargs, but with added flexibility. From ian.g.kelly at gmail.com Mon Dec 12 13:35:28 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 11:35:28 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> Message-ID: On Mon, Dec 12, 2011 at 11:17 AM, Eelco wrote: > Im not sure if I was clear on that, but I dont care what the > constructors accept; I meant to overload on the concept the underlying > type models. Dicts model a mapping, lists/tuples model a sequence. MI > deriving from both these models is illegal anyway, so one can > unambigiously overload on that trait. False. >>> from collections import * >>> class Foo(Sequence, Mapping): ... def __init__(self, items): ... self._items = items ... def __getitem__(self, item): ... return self._items[item] ... def __len__(self): ... return len(self._items) ... >>> foo1 = Foo(range(5, 10)) >>> foo2 = Foo({'one': 1, 'two': 2}) >>> foo1[3] 8 >>> foo2['one'] 1 Or are you saying that only classes specifically derived from list, tuple, or dict should be considered, and custom containers that are not derived from any of those but implement the correct protocols should be excluded? If so, that sounds less than ideal. Cheers, Ian From hoogendoorn.eelco at gmail.com Mon Dec 12 13:51:24 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 10:51:24 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> Message-ID: > False. I stand corrected. > Or are you saying that only classes specifically derived from list, > tuple, or dict should be considered, and custom containers that are > not derived from any of those but implement the correct protocols > should be excluded? ?If so, that sounds less than ideal. That might be a desirable constraint from an implementational/ performance aspect anyway, but I agree, less than ideal. Either way, its not hard to add some detail to the semantics to allow all this. Even this function definition: def func(Foo(args), Foo(kwargs)) ...could even be defined unambigiously by overloading first on base type, and if that does not uniquely determine the args and kwargs, fall back on positionality, so that: def func(Foo(args), dict(kwargs)) def func(list(args), Foo(kwargs)) would be uniquely defined as well. From hoogendoorn.eelco at gmail.com Mon Dec 12 14:05:26 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 11:05:26 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> Message-ID: <406c1cbb-2a8c-40d9-a76e-7329ebaaa2cd@l24g2000yqm.googlegroups.com> To get back on topic a little bit, lets get back to the syntax of all this: I think we all agree that recycling the function call syntax is less than ideal, since while it works in special contexts like a function signature, its symmetric counterpart inside a function call already has the meaning of a function call. In general, we face the problem of specifying metadata about a variable, or a limited form of type constraint. What we want is similar to function annotations in python 3; in line with that, we could have more general variable annotations. With an important conceptual distinction; function annotations are meaningless to python, but the annotations I have in mind should modify semantics directly. However, its still conceptually close enough that we might want to use the colon syntax here too. To distinguish it from function annotations, we could use a double colon (double colon is an annotation with non-void semantics; quite a simple rule); or to maintain an historic link with the existing packing/unpacking syntax, we could look at an augmented form of the asteriks notation. For instance: def func(list*args, dict*kwargs) <- list-of-args, dict-of-kwargs def func(args::list, kwargs::dict) <- I like the readability of this one even better; args-list and kwargs-dict And: head, deque*tail = somedeque head, tail::deque = somedeque Or some variants thereof From hoogendoorn.eelco at gmail.com Mon Dec 12 14:20:51 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 11:20:51 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> <406c1cbb-2a8c-40d9-a76e-7329ebaaa2cd@l24g2000yqm.googlegroups.com> Message-ID: <79502f0f-bf90-4629-96cb-e50ca42125c9@r6g2000yqr.googlegroups.com> On Dec 12, 8:05?pm, Eelco wrote: > To get back on topic a little bit, lets get back to the syntax of all > this: I think we all agree that recycling the function call syntax is > less than ideal, since while it works in special contexts like a > function signature, its symmetric counterpart inside a function call > already has the meaning of a function call. > > In general, we face the problem of specifying metadata about a > variable, or a limited form of type constraint. > > What we want is similar to function annotations in python 3; in line > with that, we could have more general variable annotations. With an > important conceptual distinction; function annotations are meaningless > to python, but the annotations I have in mind should modify semantics > directly. However, its still conceptually close enough that we might > want to use the colon syntax here too. To distinguish it from function > annotations, we could use a double colon (double colon is an > annotation with non-void semantics; quite a simple rule); or to > maintain an historic link with the existing packing/unpacking syntax, > we could look at an augmented form of the asteriks notation. > > For instance: > > def func(list*args, dict*kwargs) <- list-of-args, dict-of-kwargs > def func(args::list, kwargs::dict) <- I like the readability of this > one even better; args-list and kwargs-dict > > And: > > head, deque*tail = somedeque > head, tail::deque = somedeque > > Or some variants thereof As for calling functions; calling a function with the content of a collection type rather than the collection as an object itself is a rather weird special case operation I suppose, but we can cover it with the same syntax: def func(args::tuple, kwargs::dict): funccall(args::, kwargs::) <- void type constraint means unpacking, for symmetry with args/kwargs aggregation funccall(::args, ::kwargs) <- I like this better, to emphasize it being 'the other side' of the same coin, and quite close to ** syntax Sequence and Mapping unpacking dont need their own symbols, if things are done like this, since in the function declaration the meaning is clear from the type of the annotations used, plus their position, and in the call the meaning is clear from the type of the object undergoing to unpacking operation. From still_jagmaster at yahoo.com Mon Dec 12 14:33:20 2011 From: still_jagmaster at yahoo.com (J A) Date: Mon, 12 Dec 2011 19:33:20 GMT Subject: executable builder References: <26288884-27b8-4f48-8ee6-af4be4364f6e@d16g2000yqb.googlegroups.com> Message-ID: <20111212143311usenet@terrranews.com> I am having some issues compiling an exe myself but most of that stems from the version of python I am using. That being said, try using GUI2EXE http://code.google.com/p/gui2exe this program makes the setup file go a lot easier, also it will tie in a few different tools like py2exe which is what I have been working with, also it does include py2app (the mac version). If you decide to go this route please pay careful attention to http://py2exe.org/index.cgi/Tutorial#Step52 if using python26, this bit of instruction could have saved me hours of banging my head on my desk. > On Thursday, July 01, 2010 3:16 AM King wrote: > Hi, > > I am trying to build python a cross platform python executable builder > to deploy python app. I tried various tools such as py2exe, > pyinstaller, cx_freeze but some how they are not upto the mark except > py2exe. Unfortunately py2exe is working only on windows based systems. > > The task is divide into 3 steps: > 1. Collect all the modules(.pyo, .pyd, .dll, etc) required for your > project. > 2. set up temporary environment for python > 3. run main.py using :python.exe main.py > > Initially I will not be creating an executable using main.py. I would be > using shell scripting to execute the main.py. > > Q1. Which is most efficient way to fine all the modules required by > your "main.py". > Q2. For example, I have copied all the modules in "d:\project\lib\*.*" > python.exe, python.dll, ms*.dll, main.py are copied to "d: > \project". > Before executing "python.exe main.py", I have to set up an > environment var and point it to "d:\project\lib", so > that python.exe can find the path for searching/loading > modules. How do I do that? > Q3. How do you convert your "main.py" into an executable? > > Prashant From brenNOSPAMbarn at NObrenSPAMbarn.net Mon Dec 12 14:45:10 2011 From: brenNOSPAMbarn at NObrenSPAMbarn.net (OKB (not okblacke)) Date: Mon, 12 Dec 2011 19:45:10 +0000 (UTC) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > And you can blame C for the use of % instead of mod or modulo. Anytime you can blame C for something, you can also blame a bunch of other languages for foolishly perpetuating the inanities of C. -- --OKB (not okblacke) Brendan Barnwell "Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail." --author unknown From rosuav at gmail.com Mon Dec 12 15:49:27 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 07:49:27 +1100 Subject: Documentation for python-evolution - where? In-Reply-To: References: <3dnfr8-h6a.ln1@chris.zbmc.eu> Message-ID: On Tue, Dec 13, 2011 at 3:40 AM, wrote: > Actually I'm not sure if it's down to the docstrings because the help > available from Python itself stops (not unreasonably) at the interface > to the C library code. ?What I was after (and you have told me where > it is) was the functions/methods available from the C library. Ah, yes, I know that problem! Let's see, how many high level languages/libraries have I used that have simply exposed a lower-level API without documenting it? REXX/REXXUtil and SysSetObjectData was the first. More recently, Pike and the GTK/GTK2 modules. In between, oh so many others. Depending on how much is exposed and how transparent the local layer, it may still be worth writing up some better documentation. Or if not, it may be of value for the docs to incorporate a link to some "upstream documentation", which would accomplish the same thing. (It's a shot at immortality - get your name in a big project's revision history!) ChrisA From rosuav at gmail.com Mon Dec 12 15:53:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 07:53:35 +1100 Subject: automate commands to an .exe console program through python In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 3:41 AM, Juan Perez wrote: > I need to automate a .exe console program in windows, and send key > characters like 'a', 'M' ... > I had this running in an autoIT script which with only activating cmd > window, and with the "send" parameter I had all already done. But I'm having > a lot of problems with python, I've tried with process.call, communicate(), > os.popen ... but no results. I can't comunicate with the program and even i > can't maintain it opened more than? a few seconds. If I open a program like > notepad.exe it remains opened but when I try to comunicate with the stdin > pipe just don't write anything to the program. GUI programs such as Notepad usually don't read from STDIN, which is where text goes if you write to a pipe. You may have to check out how, exactly, the program accepts commands; your autoIT script is probably sending keystrokes using the Wndows GUI, so it works as long as the program has focus. Ideally, look into whether the program has an actual automation mode - and if it doesn't have one, ask the programmer to expose his code directly to a Python script. Hey, it's worth a shot! :) ChrisA From einazaki668 at yahoo.com Mon Dec 12 16:15:11 2011 From: einazaki668 at yahoo.com (Eric) Date: Mon, 12 Dec 2011 13:15:11 -0800 (PST) Subject: curses (or something) for windows Message-ID: <7a4bc959-bd45-49aa-9061-459cac28e2ca@a17g2000yqj.googlegroups.com> Is there something similar to curses available for the Windows version of Python (2.7, community edition)? Preferably something built-in. In general, I'm looking to do gui-ish things from within the command window. Also, in particular, is there a way to get the console size (rows, cols). I've seen how to do it in unix land but not for windows. TIA, eric From steve+comp.lang.python at pearwood.info Mon Dec 12 16:28:22 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Dec 2011 21:28:22 GMT Subject: Overriding a global References: Message-ID: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: > Using the same name for 2 different objects is a bad idea in general. We have namespaces precisely so you don't need to care about making names globally unique. -- Steven From d at davea.name Mon Dec 12 16:43:54 2011 From: d at davea.name (Dave Angel) Date: Mon, 12 Dec 2011 16:43:54 -0500 Subject: Overriding a global In-Reply-To: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EE6759A.8090102@davea.name> On 12/12/2011 04:28 PM, Steven D'Aprano wrote: > On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: > >> Using the same name for 2 different objects is a bad idea in general. > We have namespaces precisely so you don't need to care about making names > globally unique. > > True, but in this code, the function is trying to both use the global value, but also a local that deliberately has the same name, but a different meaning and "value". The CPython compiler doesn't make this easy, and I think the globals() technique is unnecessarily obscure, as is the default-argument trick. If a function knows of the presence of a global, it's not asking too much for it to not re-use the same name in local scope. Since it seems to be in vogue to propose language changes, how about a new place for 'as' ? def myfunc(): global logger as g_logger logger = g_logger.debug('stuff').getChild('function') -- DaveA From joshua.landau.ws at gmail.com Mon Dec 12 16:47:23 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 12 Dec 2011 21:47:23 +0000 Subject: Overriding a global In-Reply-To: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: Wouldn't this be nicer, though?: def getChildLogger(id): return logger.getChild(id) def someFunc(): logger = getChildLogger("someFunc") -- UNTESTED -- No messing around with globals this way, and it's more extendable. And 'globals()["logger"].getChild("someFunc")' reads like a brick. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Mon Dec 12 16:58:33 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 16:58:33 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE5C576.2000307@gmail.com> Message-ID: On 12/12/2011 12:12 PM, Eelco wrote: > Im not sure if this is a genuine understanding, or trollish > obtuseness. If you are referring to what I write, it is based on genuine understanding of Python. > Yes, the target can be anywhere in the sequence. And yes, the > resulting list can contain objects of any type, so its very flexible > in that regard too. > > But to relate it to the topic of this thread: no, the syntax does not > allow one to select the type of the resulting sequence. It always > constructs a list. One use case of *target is to ignore the stuff collected in the target because one only wants a few end values from the iterable. Another is to pull stuff out because one wants to iterate through the rest. For both uses, a list is as good as anything. > Yes, we can cast the list to be whatever we want on the next line, Convert. For the very few cases one wants to do this, it is quite adequate. > but the question is whether this language design can be improved upon. Not easily. > The choice of a list feels arbitrary, On the contrary, a list is precisely what is needed to collect an indefinite number of leftovers. > adding another line to cast it to > something else would be even more verbose, and whats more, there would > be serious performance implications if one should seek to apply this > pattern to a deque/linkedlist; it would make taking off the head/tail > of the list from a constant to a linear operation. For a linked list, no *target and no copying is needed: head, tail = llist >>>> head, deque(tail) = somedeque > > Is better in every way I can think of (readability, consistence, > performance) than: >>>> head, *tail = somedeque >>>> tail = deque(tail) But your suggestion is much worse in each way than head = somedeque.popleft() To repeat, there is no reason to copy even once. If one does not want to mutate the deque, then one mutates an iterator view of the deque. -- Terry Jan Reedy From ben+python at benfinney.id.au Mon Dec 12 17:27:09 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 13 Dec 2011 09:27:09 +1100 Subject: Overriding a global References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87k4611l4y.fsf@benfinney.id.au> Dave Angel writes: > True, but in this code, the function is trying to both use the global > value, but also a local that deliberately has the same name, but a > different meaning and "value". The CPython compiler doesn't make this > easy, and I think the globals() technique is unnecessarily obscure, as > is the default-argument trick. I disagree. The language makes it difficult, and it *should* be difficult to do what you describe. The tricks to achieve it are obscure and ugly, which is a good thing IMO: they're a code smell that the design of the code needs changing. Or, in brief: they're not unnecessarily obscure, they're as obscure as they need to be. > If a function knows of the presence of a global, it's not asking too > much for it to not re-use the same name in local scope. Yes. -- \ ?Airports are ugly. Some are very ugly. Some attain a degree of | `\ ugliness that can only be the result of a special effort.? | _o__) ?Douglas Adams, _The Long Dark Tea-Time Of The Soul_ | Ben Finney From hoogendoorn.eelco at gmail.com Mon Dec 12 18:40:38 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 15:40:38 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: > > Im not sure if this is a genuine understanding, or trollish > > obtuseness. > > If you are referring to what I write, it is based on genuine > understanding of Python. This is getting 'interesting'. In a way. I meant to write 'misunderstanding', as I think the context made quite clear. So again this adds another layer of doubt as to whether you are being obtuse for its own sake, or if there is yet another layer of misunderstanding stacked on top of the others. Either way, lets continue with the benefit of the doubt. > One use case of *target is to ignore the stuff collected in the target > because one only wants a few end values from the iterable. Another is to > pull stuff out because one wants to iterate through the rest. For both > uses, a list is as good as anything. So what is the point of having different sequence types, if a list can do anything? I would argue that the different performance characteristics and differences in semantics give each sequence type ample reason for existence. > Convert. For the very few cases one wants to do this, it is quite adequate. Or so you opine. As you may recall, the original idea was motivated by args/kwargs syntactic clarity and flexibility; I merely shifted focus to this use case of collection unpacking since it is somewhat simpler and easier to discuss. Keep that in mind, should you seek to build a case for that assertion in the future. > ?> but the question is whether this language design can be improved upon. > > Not easily. Again, so you opine. Care to give a refutation of my proposed double colon syntax? Ignoring for a moment whether or not it is useful; does it clash with anything? I havnt been able to think of anything in the past few hours, but im not taking that to mean much; there are probably some subtleties I am overlooking. I think it dos not clash with slicing syntax for instance, but im not sure. > For a linked list, no *target and no copying is needed: > > head, tail = llist I have no idea what this means. > >>>> head, deque(tail) = somedeque > > > Is better in every way I can think of (readability, consistence, > > performance) than: > >>>> head, *tail = somedeque > >>>> tail = deque(tail) > > But your suggestion is much worse in each way than > > head = somedeque.popleft() No its not. First of all, its not semantically equivalent; popleft mutates a collection type, and collection unpacking does not; it creates a set of disjoint views of the collection's data. Whether its worse in terms of readability is debatable, but in terms of the other two stated metrics, your claim of it being any kind of worse is objectively false. Furthermore, this brings us back again to the point I raised several times before. Yes, obviously you can already DO these things, but NOT within the same uniform collection unpacking syntactic construct. Again, you have failed to point out what is wrong with supplying a type constrain to python and let it do the right thing based on that; to reiterate: head, tail::deque = deque No need to mutate anything; python can create the view of the linkedlist internally. A single unambigious syntax, and single unambigious semantics, for all sequence types. Whats not to like? If you dont like the extra characters you have to type; there is of course such a thing as defaults. You can choose: head, tail:: = deque; if the type constraint is omitted, we could make tail a list by default, or my preferred solution, infer it from the right hand side type. In case of the former, all you had to do was type :: instead of *, and your python universe would otherwise be completely unchanged. If thats 'not easily', I dont know what is. > To repeat, there is no reason to copy even once. If one does not want to > mutate the deque, then one mutates an iterator view of the deque. And arrive at yet another construction to do the same thing. From wuwei23 at gmail.com Mon Dec 12 18:45:06 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 12 Dec 2011 15:45:06 -0800 (PST) Subject: Dynamic variable creation from string References: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> <24728986.18.1323694150411.JavaMail.geo-discussion-forums@prfc16> Message-ID: <2b414cd2-0f0a-45bf-b61e-9205704ac03a@u10g2000prl.googlegroups.com> On Dec 12, 10:49?pm, 88888 Dihedral wrote: > This is the way to write an assembler or > to roll out a script language to be included in an app > by users. This is a garbage comment that has absolutely nothing to do with the topic at hand _at all_. From wuwei23 at gmail.com Mon Dec 12 18:47:42 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 12 Dec 2011 15:47:42 -0800 (PST) Subject: curses (or something) for windows References: <7a4bc959-bd45-49aa-9061-459cac28e2ca@a17g2000yqj.googlegroups.com> Message-ID: <2d8d5df7-d1e0-4f2a-943f-5b7335ab8608@l18g2000pro.googlegroups.com> On Dec 13, 7:15?am, Eric wrote: > Is there something similar to curses available for the Windows version > of Python (2.7, community edition)? ?Preferably something built-in. > In general, I'm looking to do gui-ish things from within the command > window. > > Also, in particular, is there a way to get the console size (rows, > cols). ?I've seen how to do it in unix land but not for windows. > > TIA, > eric Check out http://www.lfd.uci.edu/~gohlke/pythonlibs/ It's a fantastic resource, Win32 & 64 versions of a lot of packages. From joshua.landau.ws at gmail.com Mon Dec 12 18:48:09 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 12 Dec 2011 23:48:09 +0000 Subject: Overriding a global In-Reply-To: <87k4611l4y.fsf@benfinney.id.au> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: > > > If a function knows of the presence of a global, it's not asking too > > much for it to not re-use the same name in local scope. > > Yes. It's just a function wanting to act as-if it were in a different environment than its default. By that same reasoning you could state that "If a function knows of the presence of a built-in, it's not asking too much for it to not re-use the same name in local scope." Yet if rebinding "id" is such a crime, why is it so oft done? Rebinding logger locally in a function is really no different to a subclass rebinding a variable from its main class using that class' value. *The only difference is that, in that case, you have an alternate binding to the original value.* * * >>> class A(): ... val = 1 ... >>> class B(A): ... val = str(val) # Obviously, this doesn't work ... NameError: name 'val' is not defined >>> class B(A): ... val = str(A.val) # But it's OK as we can just do this ^^ ... >>> B().val '1' >>> The only reason it's not minded with classes is because there's a good way to do it. I get that my analogy doesn't use globals, but the idea of extending a more-global attribute locally is shared between the concepts. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wuwei23 at gmail.com Mon Dec 12 18:54:09 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 12 Dec 2011 15:54:09 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> Message-ID: <4588e1ef-6eec-45ff-abcd-d414f2a787d6@r16g2000prr.googlegroups.com> On Dec 12, 10:21?pm, Eelco wrote: > > ?Modulo is hardly an obscure operation. "What's the remainder...?" is a > > ?simple question that people learn about in primary school. > > So is 'how much wood would a woodchucker chuck if a woodchucker could > chuck wood?'. But how often does that concept turn up in your code? That comment right there? That's the moment every serious coder stopped paying attention to a single word you say. From wuwei23 at gmail.com Mon Dec 12 19:27:45 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 12 Dec 2011 16:27:45 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: On Dec 13, 3:12?am, Eelco wrote: > But to relate it to the topic of this thread: no, the syntax does not > allow one to select the type of the resulting sequence. It always > constructs a list. So by this argument, _every_ function that returns a list should take an optional argument to specify an alternative form of sequence. What, exactly, is so onerous about coercing your list to _whatever_ type you want? You know, like everybody else has been. What does this _gain_ you other than one less line of code? From ian.g.kelly at gmail.com Mon Dec 12 19:34:25 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 17:34:25 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> Message-ID: On Mon, Dec 12, 2011 at 11:51 AM, Eelco wrote: > Either way, its not hard to add some detail to the semantics to allow > all this. Even this function definition: > > def func(Foo(args), Foo(kwargs)) > > ...could even be defined unambigiously by overloading first on base > type, and if that does not uniquely determine the args and kwargs, > fall back on positionality, so that: > > def func(Foo(args), dict(kwargs)) > def func(list(args), Foo(kwargs)) > > would be uniquely defined as well. That solves some of the problems, but if I just have: def func(SequenceOrMappingType(args)): That's going to unpack positionally. If I want it to unpack keywords instead, how would I change the definition to indicate that? From einazaki668 at yahoo.com Mon Dec 12 19:45:24 2011 From: einazaki668 at yahoo.com (Eric) Date: Mon, 12 Dec 2011 16:45:24 -0800 (PST) Subject: curses (or something) for windows References: <7a4bc959-bd45-49aa-9061-459cac28e2ca@a17g2000yqj.googlegroups.com> <2d8d5df7-d1e0-4f2a-943f-5b7335ab8608@l18g2000pro.googlegroups.com> Message-ID: On Dec 12, 5:47?pm, alex23 wrote: > On Dec 13, 7:15?am, Eric wrote: > > > Is there something similar to curses available for the Windows version > > of Python (2.7, community edition)? ?Preferably something built-in. > > In general, I'm looking to do gui-ish things from within the command > > window. > > > Also, in particular, is there a way to get the console size (rows, > > cols). ?I've seen how to do it in unix land but not for windows. > > > TIA, > > eric > > Check outhttp://www.lfd.uci.edu/~gohlke/pythonlibs/ > > It's a fantastic resource, Win32 & 64 versions of a lot of packages. Wowee that's a lot of stuff. Thanks for the tip. eric From wuwei23 at gmail.com Mon Dec 12 19:46:52 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 12 Dec 2011 16:46:52 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> Message-ID: On Dec 13, 2:27?am, Robert Kern wrote: > On 12/12/11 3:36 AM, alex23 wrote: > > > On Dec 9, 8:08 pm, Robert Kern ?wrote: > >> On 12/9/11 5:02 AM, alex23 wrote: > >>> The 3rd party 'decorator' module takes care of issues like docstrings > >>> & ? ?function signatures. I'd really like to see some of that > >>> functionality in the stdlib though. > > >> Much of it is: > > >> ? ?http://docs.python.org/library/functools#functools.update_wrapper > > > Ah, cheers :) Is that a recent addition? The lack of note makes it > > seem like it was there from the beginning? > > The module was added in Python 2.5 as noted at the top of the page. I had thought you meant it now included function signature handling, as that was the context at the time. No biggie. From cpblpublic at gmail.com Mon Dec 12 20:00:08 2011 From: cpblpublic at gmail.com (C Barrington-Leigh) Date: Mon, 12 Dec 2011 17:00:08 -0800 (PST) Subject: Simple legend code no longer works after upgrade to Ubuntu 11.10 Message-ID: <35d63565-3439-480e-bf05-964acf1e318a@13g2000vbu.googlegroups.com> rom pylab import * plot([0,0],[1,1],label='Ubuntu 11.10') Before I upgraded to 2.7.2+ / 4 OCt 2011, the following code added a comment line with a legend. Now, the same code makes the legend appear "off-screen", ie way outside the axes limits. Can anyone help? And/or is there a new way to add a title and footer to the legend? Thanks! lh=legend(fancybox=True,shadow=False) lh.get_frame().set_alpha(0.5) from matplotlib.offsetbox import TextArea, VPacker fontsize=lh.get_texts()[0].get_fontsize() legendcomment=TextArea('extra comments here', textprops=dict(size=fontsize)) show() # Looks fine here lh._legend_box = VPacker(pad=5, sep=0, children=[lh._legend_box,legendcomment], align="left") lh._legend_box.set_figure(gcf()) draw() From cpblpublic at gmail.com Mon Dec 12 20:04:27 2011 From: cpblpublic at gmail.com (C Barrington-Leigh) Date: Mon, 12 Dec 2011 17:04:27 -0800 (PST) Subject: Simple legend code no longer works after upgrade to Ubuntu 11.10 Message-ID: <5845f1b6-cb4e-49b3-9e4d-9d290a76ae68@u32g2000yqe.googlegroups.com> """ Before I upgraded to 2.7.2+ / 4 OCt 2011, the following code added a comment line to an axis legend using matplotlib / pylab. Now, the same code makes the legend appear "off-screen", ie way outside the axes limits. Can anyone help? And/or is there a new way to add a title and footer to the legend? Thanks! """ from pylab import * plot([0,0],[1,1],label='Ubuntu 11.10') lh=legend(fancybox=True,shadow=False) lh.get_frame().set_alpha(0.5) from matplotlib.offsetbox import TextArea, VPacker fontsize=lh.get_texts()[0].get_fontsize() legendcomment=TextArea('extra comments here', textprops=dict(size=fontsize)) show() # Looks fine here lh._legend_box = VPacker(pad=5, sep=0, children=[lh._legend_box,legendcomment], align="left") lh._legend_box.set_figure(gcf()) draw() From steve+comp.lang.python at pearwood.info Mon Dec 12 20:34:37 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 01:34:37 GMT Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: <4ee6abac$0$11091$c3e8da3@news.astraweb.com> On Mon, 12 Dec 2011 16:58:33 -0500, Terry Reedy wrote: > On 12/12/2011 12:12 PM, Eelco wrote: >> Yes, we can cast the list to be whatever we want on the next line, > > Convert. For the very few cases one wants to do this, it is quite > adequate. +1 with the small proviso that there's not much you can usefully do with a dict. If you convert to an ordered dict, the initial order is lost by the time you see it; if you want to allow duplicates, duplicates are likewise lost (or in a function call, an exception is raised). In Python 4000, I'd like to see some way to pass arbitrary parameters to functions, including duplicates. That is, emulating the richness of command line argument handling in functions. In Python 3.x, I'd like to see OrderedDict become a built-in, and then **kwargs can collect named arguments in an ordered dict without losing the order. -- Steven From steve+comp.lang.python at pearwood.info Mon Dec 12 20:35:52 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 01:35:52 GMT Subject: Dynamic variable creation from string References: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> <24728986.18.1323694150411.JavaMail.geo-discussion-forums@prfc16> <2b414cd2-0f0a-45bf-b61e-9205704ac03a@u10g2000prl.googlegroups.com> Message-ID: <4ee6abf8$0$11091$c3e8da3@news.astraweb.com> On Mon, 12 Dec 2011 15:45:06 -0800, alex23 wrote: > On Dec 12, 10:49?pm, 88888 Dihedral > wrote: >> This is the way to write an assembler or to roll out a script language >> to be included in an app by users. > > This is a garbage comment that has absolutely nothing to do with the > topic at hand _at all_. Please stop responding to the bot. -- Steven From ian.g.kelly at gmail.com Mon Dec 12 20:41:34 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 18:41:34 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE5C576.2000307@gmail.com> Message-ID: On Mon, Dec 12, 2011 at 4:40 PM, Eelco wrote: >> For a linked list, no *target and no copying is needed: >> >> head, tail = llist > > I have no idea what this means. Each node of a linked list consists of a data member and a "next" member, that holds the next node in the list. The natural container for this and the way it is usually implemented in Python is a 2-element tuple. For example: llist = ('one', ('two', ('three', ('four', None)))) would be a linked list as typically constructed in Python, and it can be used in pretty much the same way that lists are used in Lisp. The result of the operation: head, tail = llist is: head = 'one' tail = ('two', ('three', ('four', None))) and as Terry pointed out, no copying is required to do this. I believe deque is also implemented as a doubly-linked list, which is probably why you keep referring to it as such, but that is an implementation detail. When you say "linked list" in relation to Python, the preceding is what comes to mind. >> >>>> head, deque(tail) = somedeque >> >> > Is better in every way I can think of (readability, consistence, >> > performance) than: >> >>>> head, *tail = somedeque >> >>>> tail = deque(tail) >> >> But your suggestion is much worse in each way than >> >> head = somedeque.popleft() > > No its not. First of all, its not semantically equivalent; popleft > mutates a collection type, and collection unpacking does not; it > creates a set of disjoint views of the collection's data. Whether its > worse in terms of readability is debatable, but in terms of the other > two stated metrics, your claim of it being any kind of worse is > objectively false. I definitely disagree on readability. Skimming this thread as I have been, it took me a while to get that your proposed syntax would create a second deque sharing internal state with the original, rather than creating a simple copy of the deque, which is what it looks like it should do. Incidentally, while that change is not really central to your syntax proposal, I think it would be very messy. For example, how do you propose keeping the length elements in sync? Inserting an item in one may or may not affect the length of the other. If I append an item to the end of one deque, should the other automatically be extended as well? What if the tail node of the second deque occurs after the tail node of the deque being appended? Does the appended element then get inserted into the middle of the second deque (I think it would have to be)? If I insert an element into the longer (second) deque that just happens to be immediately after the tail of the shorter deque, does *that* cause the shorter deque to be automatically extended? And likewise for operations at the head of the deque. None of these questions have obvious answers as to the "right" way to it, and for that reason I think this is probably best left to the user to implement a custom deque view class with whatever semantics they prefer. > Furthermore, this brings us back again to the point I raised several > times before. Yes, obviously you can already DO these things, but NOT > within the same uniform collection unpacking syntactic construct. > Again, you have failed to point out what is wrong with supplying a > type constrain to python and let it do the right thing based on that; > to reiterate: > > head, tail::deque = deque Python users generally follow the rule "explicit is better than implicit". Setting a general constraint and letting the language "do the right thing" is a kind of black magic that feels off because it tends to break that rule. But that's not to say that black magic never wins -- just look at super() and the MRO. > If you dont like the extra characters you have to type; there is of > course such a thing as defaults. You can choose: > head, tail:: = deque; if the type constraint is omitted, we could make > tail a list by default, or my preferred solution, infer it from the > right hand side type. I prefer the former. If it always creates a list by default, then the programmer reading this code three years later knows exactly what the type of tail is. If it instead infers it from the sequence being unpacked, then the programmer is forced to infer it as well, and it won't necessarily be obvious or even consistent. Cheers, Ian From d at davea.name Mon Dec 12 20:46:14 2011 From: d at davea.name (Dave Angel) Date: Mon, 12 Dec 2011 20:46:14 -0500 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: <4EE6AE66.5080605@davea.name> On 12/12/2011 06:48 PM, Joshua Landau wrote: >>> If a function knows of the presence of a global, it's not asking too >>> much for it to not re-use the same name in local scope. >> Yes. > > It's just a function wanting to act as-if it were in a different > environment than its default. By that same reasoning you could state that > "If a function knows of the presence of a built-in, it's not asking > too much for it to not re-use the same name in local scope." > It's entirely different. It's only the same if the function actually tries to call the built-in, then also wants a local variable with a different purpose. Think a little about what I mean that the function "knows of the presence." I did not say the programmer knows of the presence. -- DaveA From steve+comp.lang.python at pearwood.info Mon Dec 12 20:48:43 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 01:48:43 GMT Subject: Overriding a global References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: <4ee6aefa$0$11091$c3e8da3@news.astraweb.com> On Tue, 13 Dec 2011 09:27:09 +1100, Ben Finney wrote: > Dave Angel writes: > >> True, but in this code, the function is trying to both use the global >> value, but also a local that deliberately has the same name, but a >> different meaning and "value". The CPython compiler doesn't make this >> easy, and I think the globals() technique is unnecessarily obscure, as >> is the default-argument trick. > > I disagree. The language makes it difficult, and it *should* be > difficult to do what you describe. > > The tricks to achieve it are obscure and ugly, which is a good thing > IMO: they're a code smell that the design of the code needs changing. Devil's Advocate: perhaps not. Think of local and global names as analogous to instance and class attributes. There are good use cases for making something a class attribute, while allowing instances to override that name with an instance attribute. I see a reasonable case for saying "use this global, unless a local overrides it". Similarly, globals override built-ins with the same name; while monkeypatching needs to be used with care, it is a legitimate technique. To a human reader, the following pseudocode might be ambiguous, but either case makes sense: x = 1 def spam(): print x # prints 1 x = 2 # does this create a new local x, or modify the old global x? print x # unambiguously prints 2 print x # prints 1 or 2 Python doesn't allow this, but another language might; in Python, a reasonable way to get similar behaviour might be: x = 1 def spam(): print globals()['x'] x = 2 # unambiguously creates a new local x print x # unambiguously prints 2 print x # unambiguously prints 1 -- Steven From steve+comp.lang.python at pearwood.info Mon Dec 12 21:43:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 02:43:00 GMT Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> Message-ID: <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> On Mon, 12 Dec 2011 04:21:15 -0800, Eelco wrote: >> No more, or less, explicit than the difference between "==" and "is". > > == may be taken to mean identity comparison; 'equals' can only mean one > thing. Nonsense. "Equals" can be taken to mean anything the language designer chooses, same as "==". There is no language police that enforces The One True Meaning Of Equals. In fact, there is no one true meaning of equals. Even my tiny Pocket Oxford dictionary lists five definitions. It is ironic that the example you give, that of identity, is the standard definition of equals in mathematics. 2*2 = 4 does not merely say that "there is a thing, 2*2, which has the same value as a different thing, 4", but that both sides are the same thing. Two times two *is* four. All numbers are, in some sense, singletons and equality implies identity. A language designer might choose to define equals as an identity test, or as a looser "values are the same" test where the value of an object or variable is context dependent, *regardless* of how they are spelled: = == === "is" "equals" or even "flibbertigibbet" if they wanted to be whimsical. The design might allow types to define their own sense of equality. Triangle.equals(other_triangle) might be defined to treat any two congruent triangles as equal; set equality could be defined as an isomorphism relation; string equality could be defined as case- insensitive, or to ignore leading and trailing whitespace. Regardless of whether you or I *would* make those choices, we *could* make those choices regardless of how our language spells the equality test. > Of course 'formally' these symbols are well defined, but so is > brainf*ck I don't understand your point here. >> Modulo is hardly an obscure operation. "What's the remainder...?" is a >> simple question that people learn about in primary school. > > > So is 'how much wood would a woodchucker chuck if a woodchucker could > chuck wood?'. But how often does that concept turn up in your code? You didn't make a statement about how often modulo turns up in code (which is actually quite frequently, and possibly more frequently than regular division), but about the obscurity of the operation. Taking the remainder is not an obscure operation. The names "modulo" and "modulus" may be obscure to those who haven't done a lot of mathematics, but the concept of remainder is not. "How many pieces are left over after dividing into equal portions" is something which even small children get. >> And you can blame C for the use of % instead of mod or modulo. > > I didnt know one of Python's design goals was backwards compatibility > with C. Don't be silly. You know full well Python is not backwards compatible with C, even if they do share some syntactical features. C is merely one of many languages which have influenced Python, as are Haskell, ABC, Pascal, Algol 68, Perl (mostly in the sense of "what not to do" ), Lisp, and probably many others. It merely happens that C's use of the notation % for the remainder operation likely influenced Python's choice of the same notation. I note that the *semantics* of the operation differs in the two languages, as I understand that the behaviour of % with negative arguments is left undefined by the C standard, while Python does specify the behaviour. >> I can't imagine what sort of Python code you have seen that you >> consider 90% attribute access "typical". I've just run the Python >> tokenizer over my startup.py file, and I get these results: > > Yes, that was a hyperbole; but quite an often used construct, is it not? It's hard, but not quite impossible, to write useful Python code without it, so yes. >> If you can supply any function at all, what happens if I write this: > > > You cannot; only constructors modelling a sequence or a dict, and only > in that order. Is that rule clear enough? But why limit yourself to those restrictive rules? If I want to collect a sequence of arguments into a string, why shouldn't I be allowed to write this? def func(parg, str(args)): ... If I want to sum a collection of arguments, why not write this? def func(pargs, sum(args)): ... Isn't that better than this? def func(pargs, *args): args = sum(args) ... But no. I don't mean those examples to be taken seriously: when you answer to your own satisfaction why they are bad ideas, you may be closer to understanding why I believe your idea is also a bad idea. >> I believe that your proposal leads to an over-generalisation "call >> arbitrary functions when handling parameter lists". > > I hope the above clears that up. It is as much about calling functions > as ** is about raising kwargs to the power of. I don't understand this comment. Nobody has suggested that ** in function parameter lists is the exponentiation operator. As for "calling functions", how else do you expect to generate a type if you don't call the type constructor? One of your early examples was something like: def func(parg, attrdict(kwargs)): ... If you expect kwargs to be an attrdict, which is not a built-in, presumably you will have needed to have defined attrdict as a type or function, and this type or function will get called at run time to collect the kwargs. That is all. >> I don't believe you >> need this added complication. If you want to your var args as a list, >> call list(args) inside your function. > > We dont strictly 'need' any language construct. Real men use assembler, > right? "We're not using assembly" is not a reason to add a feature to a language. Every feature adds cost to the language: * harder to implement; * harder to maintainer; * larger code base; * more documentation to be written; * more tests to be written; * more for users to learn etc. >> > head, tuple(tail) = iterable >> In Python 3, that is spelled: >> head, *tail = iterable >> tail = tuple(tail) > > Yes, I know. How is that not a lot more verbose and worse than what I > have proposed in all possible ways? That *specific* syntax, outside of function declarations, is something I've often thought might be useful. But if I were to argue its case, I would allow arbitrary functions, and treat it as syntactic sugar for: head, *tail = iterable tail = func(tail) # or possibly func(*tail) But that's pie-in-the-sky musing. I certainly wouldn't put it in function parameter lists. Param lists should be declarations, not code. Apart from the most limited collation of args, code belongs inside the body of the function, not the param list: def foo(a, 2*b+1, c): # double the second arg and add 1 >> head, tail = somestring[0], somestring[1:] > > Well yes, splendid; we can do that with lists too since the dawn of > Python. What you are saying here in effect is that you think the > head/tail syntax is superfluous; that youd rather see it eliminated than > generalized. No. It is not "head/tail" syntax, but sequence unpacking, and it has been nicely generalised to allow things like this in Python 3: a, b, c, d, *lump, x, y z = any_iterable any_iterable isn't limited to a list, str, or other object which supports slicing. It can be any object supporting the iteration protocol. What I'm saying is that there is no need to OVER-generalise this to specify the type of *lump within the packing operation. If you want lump to be something other that Python's choice, perform the conversion yourself. -- Steven From rosuav at gmail.com Mon Dec 12 22:08:28 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 14:08:28 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Tue, Dec 13, 2011 at 1:43 PM, Steven D'Aprano wrote: > It merely happens that C's > use of the notation % for the remainder operation likely influenced > Python's choice of the same notation. Considering that Python also had the notion that "integer divided by integer yields integer" until Py3, I would say it's extremely likely that most of Python's division facilities were modelled off C. That's not a bad thing; gives you a set of operations that a large number of people will grok, and only a small number of oddities. > I note that the *semantics* of the operation differs in the two > languages, as I understand that the behaviour of % with negative > arguments is left undefined by the C standard, while Python does specify > the behaviour. ... and there's the proof that "modelled off" does not mean "slavishly follows". This lack of definition is a weakness in C. > def foo(a, 2*b+1, c): ?# double the second arg and add 1 No, that should subtract 1 from the second arg and halve it. The expression you give there has to match the value from the parameter list. This syntax would be a huge boon to Python. Imagine how much easier this could make things: def foo(sum(x)): return x print(foo(120)) # will print a list of numbers that sum to 120 ChrisA From ian.g.kelly at gmail.com Mon Dec 12 23:52:11 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 21:52:11 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Mon, Dec 12, 2011 at 7:43 PM, Steven D'Aprano wrote: > If I want to collect a sequence of arguments into a string, why shouldn't > I be allowed to write this? > > ? ?def func(parg, str(args)): ... Obviously, because the correct syntax would be: def func(parg, ''.join(args)): ... :-P From ian.g.kelly at gmail.com Tue Dec 13 00:50:44 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 22:50:44 -0700 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On Mon, Dec 12, 2011 at 4:48 PM, Joshua Landau wrote: > Rebinding logger locally in a function is really no > different to a subclass rebinding a variable from its main class using that > class' value. The only difference is that, in that case, you have an > alternate binding to the original value. No, there is another difference, the reason for rebinding the name. In a subclass, you would rebind a class attribute because that particular attribute, which you need to change, is used and expected by external code, either in the base class or in code that uses its API (or both). Local variables in functions, on the other hand, are not externally visible, so there is no need to do this in order to conform to the expectations of external code. All it does in that case is to sow potential confusion. From hoogendoorn.eelco at gmail.com Tue Dec 13 03:30:06 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 00:30:06 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: On Dec 13, 1:27?am, alex23 wrote: > On Dec 13, 3:12?am, Eelco wrote: > > > But to relate it to the topic of this thread: no, the syntax does not > > allow one to select the type of the resulting sequence. It always > > constructs a list. > > So by this argument, _every_ function that returns a list should take > an optional argument to specify an alternative form of sequence. > > What, exactly, is so onerous about coercing your list to _whatever_ > type you want? You know, like everybody else has been. > > What does this _gain_ you other than one less line of code? 1) Turning two lines of code into a single more readable one is nothing to scoff at 2) After-the-fact conversion is O(n), getting the view you want right away is O(1) Not every function needs this flexibility; many specialized functions could not care less. But collection unpacking is quite a general thing, and for the record; slicing a tuple returns a tuple. Would you rather have that return a list too? From hoogendoorn.eelco at gmail.com Tue Dec 13 03:31:21 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 00:31:21 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> Message-ID: On Dec 13, 1:34?am, Ian Kelly wrote: > On Mon, Dec 12, 2011 at 11:51 AM, Eelco wrote: > > Either way, its not hard to add some detail to the semantics to allow > > all this. Even this function definition: > > > def func(Foo(args), Foo(kwargs)) > > > ...could even be defined unambigiously by overloading first on base > > type, and if that does not uniquely determine the args and kwargs, > > fall back on positionality, so that: > > > def func(Foo(args), dict(kwargs)) > > def func(list(args), Foo(kwargs)) > > > would be uniquely defined as well. > > That solves some of the problems, but if I just have: > > ? ? ? ? def func(SequenceOrMappingType(args)): > > That's going to unpack positionally. ?If I want it to unpack keywords > instead, how would I change the definition to indicate that? That should raise an ambiguity error. But honestly, how often have you worked with SequenceOrMappingType's? I think this is a rather palatable constraint. From joshua.landau.ws at gmail.com Tue Dec 13 03:34:59 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 13 Dec 2011 08:34:59 +0000 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On 13/12/2011, Ian Kelly wrote: > On Mon, Dec 12, 2011 at 4:48 PM, Joshua Landau > wrote: >> Rebinding logger locally in a function is really no >> different to a subclass rebinding a variable from its main class using >> that >> class' value. The only difference is that, in that case, you have an >> alternate binding to the original value. > > No, there is another difference, the reason for rebinding the name. > In a subclass, you would rebind a class attribute because that > particular attribute, which you need to change, is used and expected > by external code, either in the base class or in code that uses its > API (or both). Local variables in functions, on the other hand, are > not externally visible, so there is no need to do this in order to > conform to the expectations of external code. All it does in that > case is to sow potential confusion. > So you're saying you should never extend methods or attributes that aren't meant to be used as part of of the API? Because I can claim guilty on this point. I'd make this longer, but I've got class now :( From hoogendoorn.eelco at gmail.com Tue Dec 13 03:44:20 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 00:44:20 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: <2ff41b2e-90e3-4fae-9951-67bb257ed6ed@m10g2000vbc.googlegroups.com> On Dec 13, 2:41?am, Ian Kelly wrote: > On Mon, Dec 12, 2011 at 4:40 PM, Eelco wrote: > >> For a linked list, no *target and no copying is needed: > > >> head, tail = llist > > > I have no idea what this means. > > Each node of a linked list consists of a data member and a "next" > member, that holds the next node in the list. ?The natural container > for this and the way it is usually implemented in Python is a > 2-element tuple. ?For example: > > llist = ('one', ('two', ('three', ('four', None)))) > > would be a linked list as typically constructed in Python, and it can > be used in pretty much the same way that lists are used in Lisp. ?The > result of the operation: > > head, tail = llist > > is: > > head = 'one' > tail = ('two', ('three', ('four', None))) > > and as Terry pointed out, no copying is required to do this. ?I > believe deque is also implemented as a doubly-linked list, which is > probably why you keep referring to it as such, but that is an > implementation detail. ?When you say "linked list" in relation to > Python, the preceding is what comes to mind. 'for i in llist' is not quite going to fly is it? Thats probably the reason noone ever uses that construct; its not a proper sequence type. > >> >>>> head, deque(tail) = somedeque > > >> > Is better in every way I can think of (readability, consistence, > >> > performance) than: > >> >>>> head, *tail = somedeque > >> >>>> tail = deque(tail) > > >> But your suggestion is much worse in each way than > > >> head = somedeque.popleft() > > > No its not. First of all, its not semantically equivalent; popleft > > mutates a collection type, and collection unpacking does not; it > > creates a set of disjoint views of the collection's data. Whether its > > worse in terms of readability is debatable, but in terms of the other > > two stated metrics, your claim of it being any kind of worse is > > objectively false. > > I definitely disagree on readability. ?Skimming this thread as I have > been, it took me a while to get that your proposed syntax would create > a second deque sharing internal state with the original, rather than > creating a simple copy of the deque, which is what it looks like it > should do. Thats a matter of reading up on the semantics of collection unpacking. To be honest I dont even know what they are for lists, since I dont use python 3. But whatever is the case, the important thing is that the semantics should be uniform regardless of the type to be unpacked. > Incidentally, while that change is not really central to your syntax > proposal, I think it would be very messy. ?For example, how do you > propose keeping the length elements in sync? ?Inserting an item in one > may or may not affect the length of the other. ?If I append an item to > the end of one deque, should the other automatically be extended as > well? ?What if the tail node of the second deque occurs after the tail > node of the deque being appended? ?Does the appended element then get > inserted into the middle of the second deque (I think it would have to > be)? ?If I insert an element into the longer (second) deque that just > happens to be immediately after the tail of the shorter deque, does > *that* cause the shorter deque to be automatically extended? ?And > likewise for operations at the head of the deque. > > None of these questions have obvious answers as to the "right" way to > it, and for that reason I think this is probably best left to the user > to implement a custom deque view class with whatever semantics they > prefer. Good point. Copy-on-write semantics could be used, but really one should have several linked list types reflecting the underlying implementations. I would like to have an immutable singly linked list builtin of the standard functional type, which you can only unpack from one end and renders these issues moot, plus a builtin doubly linked list with copy-on-write or copy-on-unpacking semantics. > > Furthermore, this brings us back again to the point I raised several > > times before. Yes, obviously you can already DO these things, but NOT > > within the same uniform collection unpacking syntactic construct. > > Again, you have failed to point out what is wrong with supplying a > > type constrain to python and let it do the right thing based on that; > > to reiterate: > > > head, tail::deque = deque > > Python users generally follow the rule "explicit is better than > implicit". ?Setting a general constraint and letting the language "do > the right thing" is a kind of black magic that feels off because it > tends to break that rule. ?But that's not to say that black magic > never wins -- just look at super() and the MRO. We are not talking black magic here; we are talking about an EXPLICIT type constraint provided on the very same line. > > If you dont like the extra characters you have to type; there is of > > course such a thing as defaults. You can choose: > > head, tail:: = deque; if the type constraint is omitted, we could make > > tail a list by default, or my preferred solution, infer it from the > > right hand side type. > > I prefer the former. ?If it always creates a list by default, then the > programmer reading this code three years later knows exactly what the > type of tail is. ?If it instead infers it from the sequence being > unpacked, then the programmer is forced to infer it as well, and it > won't necessarily be obvious or even consistent. Well perhaps, but not always knowing the type of your objects at write- time is inherent to weakly typed languages; this happens all the time. Not knowing the type of the sequence to be unpacked is in a sense an asset; I can use this construct in a function, and unpack any sequence type in a manner appropriate for it. About the result of the unpacking I will know just as much as about the input to it; that they are the same type. From antoon.pardon at rece.vub.ac.be Tue Dec 13 04:15:41 2011 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Tue, 13 Dec 2011 10:15:41 +0100 Subject: Overriding a global In-Reply-To: References: Message-ID: <4EE717BD.10301@rece.vub.ac.be> On 12/10/2011 09:47 PM, Roy Smith wrote: > I've got a code pattern I use a lot. In each module, I create a logger > for the entire module and log to it all over: > > logger = logging.getLogger('my.module.name') > > class Foo: > def function(self): > logger.debug('stuff') > logger.debug('other stuff') > > and so on. This works, but every once in a while I decide that a > particular function needs a more specific logger, so I can adjust the > logging level for that function independent of the rest of the module. > What I really want to do is: > > def function(self): > logger = logger.getChild('function') > logger.debug('stuff') > logger.debug('other stuff') > > which lets me not have to change any lines of code other than inserting > the one to redefine logger. Unfortunately, that's not legal Python (it > leads to "UnboundLocalError: local variable 'logger' referenced before > assignment"). > > Any ideas on the best way to implement this? > How about two global references: globallogger = logger = logging.getLogger('my.module.name') def function(self): logger = globallogger.getChild('function') logger.debug('stuff') logger.debug('other stuff') -- Antoon Pardon From hoogendoorn.eelco at gmail.com Tue Dec 13 04:15:46 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 01:15:46 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> Message-ID: <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> On Dec 13, 3:43?am, Steven D'Aprano wrote: > On Mon, 12 Dec 2011 04:21:15 -0800, Eelco wrote: > >> No more, or less, explicit than the difference between "==" and "is". > > > == may be taken to mean identity comparison; 'equals' can only mean one > > thing. > > Nonsense. "Equals" can be taken to mean anything the language designer > chooses, same as "==". There is no language police that enforces The One > True Meaning Of Equals. In fact, there is no one true meaning of equals. > Even my tiny Pocket Oxford dictionary lists five definitions. > > It is ironic that the example you give, that of identity, is the standard > definition of equals in mathematics. 2*2 = 4 does not merely say that > "there is a thing, 2*2, which has the same value as a different thing, > 4", but that both sides are the same thing. Two times two *is* four. All > numbers are, in some sense, singletons and equality implies identity. We are not talking mathemathics, we are talking programming languages. Identity versus value equality is a well established concept within its jargon. Within this context, 'equals' and 'is' have clearly defined meanings. Indeed within broader use, including everyday language, the distinction is more subtle and therefore less useful, but obeying a linguistic rule that holds within computer science is better than making something up just for python, even though its not the yet better rule that any five year old might grasp. > > Of course 'formally' these symbols are well defined, but so is > > brainf*ck > > I don't understand your point here. Hence the above. > >> ?Modulo is hardly an obscure operation. "What's the remainder...?" is a > >> ?simple question that people learn about in primary school. > > > So is 'how much wood would a woodchucker chuck if a woodchucker could > > chuck wood?'. But how often does that concept turn up in your code? > > You didn't make a statement about how often modulo turns up in code > (which is actually quite frequently, and possibly more frequently than > regular division), but about the obscurity of the operation. Taking the > remainder is not an obscure operation. The names "modulo" and "modulus" > may be obscure to those who haven't done a lot of mathematics, but the > concept of remainder is not. "How many pieces are left over after > dividing into equal portions" is something which even small children get. So 'frequency of use' is no valid interpretation of 'obscurity'? Im not a native speaker, but im pretty sure it is. > >> And you can blame C for the use of % instead of mod or modulo. > > > I didnt know one of Python's design goals was backwards compatibility > > with C. > > Don't be silly. You know full well Python is not backwards compatible > with C, even if they do share some syntactical features. Of course I was being silly; I know this use is following a historical precedent; but id rather see it rectified in the previous version of python rather than the next. My sillyness was prompted by the percieved pointlessness of your remark. Of course python is not trying to be backwards compatible with C; so why bring it up then? > >> ?If you can supply any function at all, what happens if I write this: > > > You cannot; only constructors modelling a sequence or a dict, and only > > in that order. Is that rule clear enough? > > But why limit yourself to those restrictive rules? > > If I want to collect a sequence of arguments into a string, why shouldn't > I be allowed to write this? > > ? ? def func(parg, str(args)): ... > > If I want to sum a collection of arguments, why not write this? > > ? ? def func(pargs, sum(args)): ... > > Isn't that better than this? > > ? ? def func(pargs, *args): > ? ? ? ? args = sum(args) > ? ? ? ? ... > > But no. I don't mean those examples to be taken seriously: when you > answer to your own satisfaction why they are bad ideas, you may be closer > to understanding why I believe your idea is also a bad idea. They are bad ideas because they truely do not lead to the execution of different code, but are merely a reordering, mixing statements in with a function declaration. I am proposing no such thing; again, the type(arg) notation I have dropped, and never was meant to have anything to do with function calling; it is a way of supplying an optional type constraint, so in analogy with function annotations, I changed that to arg::type. Again, this has nothing to do with calling functions on arguments. First off, type constraints must have some use; all those languages cant be wrong. Ofcourse, no type constraints also can not be wrong; see all those other languages. And I obviously dont mean to make type constraits mandatory in python, all im saying is that optionally allowing them can open some doors in the right places. The * and ** syntax are also in effect type constraints, saying 'this is a dict to collect the remaining kwargs in', but in a rather cryptic and needlessly ungeneral method. #define a function with args-list and kwargs-attrdict def func(args::list, kwargs::attrdict) #unpack the sequence args into positional arguments, and the mapping kwargs into keyword arguments func(::args, ::kwargs) So now that I have explained what my idea actually is, perhaps youd like to explain again why it is a bad idea? > >> I believe that your proposal leads to an over-generalisation "call > >> arbitrary functions when handling parameter lists". > > > I hope the above clears that up. It is as much about calling functions > > as ** is about raising kwargs to the power of. > > I don't understand this comment. Nobody has suggested that ** in function > parameter lists is the exponentiation operator. No more or less than I suggested this is about calling functions on input parameters. The syntax might be superficially similar, but thats all. > As for "calling functions", how else do you expect to generate a type if > you don't call the type constructor? One of your early examples was > something like: > > def func(parg, attrdict(kwargs)): ... > > If you expect kwargs to be an attrdict, which is not a built-in, > presumably you will have needed to have defined attrdict as a type or > function, and this type or function will get called at run time to > collect the kwargs. That is all. I would like attrdict to be a builtin, and as long as it isnt, that specific example probably wouldnt work. But all I wnat python to do is whatever it is doing to build a dict, but then for another type instead. Hence, a typeconstraint. > > >> ?> ?head, tuple(tail) = iterable > >> ?In Python 3, that is spelled: > >> ?head, *tail = iterable > >> ?tail = tuple(tail) > > > Yes, I know. How is that not a lot more verbose and worse than what I > > have proposed in all possible ways? > > That *specific* syntax, outside of function declarations, is something > I've often thought might be useful. But if I were to argue its case, I > would allow arbitrary functions, and treat it as syntactic sugar for: > > head, *tail = iterable > tail = func(tail) ?# or possibly func(*tail) > > But that's pie-in-the-sky musing. I certainly wouldn't put it in function > parameter lists. Param lists should be declarations, not code. Apart from > the most limited collation of args, code belongs inside the body of the > function, not the param list: I agree that this is a bad idea; see the above. > >> head, tail = somestring[0], somestring[1:] > > > Well yes, splendid; we can do that with lists too since the dawn of > > Python. What you are saying here in effect is that you think the > > head/tail syntax is superfluous; that youd rather see it eliminated than > > generalized. > > No. > > It is not "head/tail" syntax, but sequence unpacking, and it has been > nicely generalised to allow things like this in Python 3: > > a, b, c, d, *lump, x, y z = any_iterable > > any_iterable isn't limited to a list, str, or other object which supports > slicing. It can be any object supporting the iteration protocol. > > What I'm saying is that there is no need to OVER-generalise this to > specify the type of *lump within the packing operation. If you want lump > to be something other that Python's choice, perform the conversion > yourself. Oh, so your primary objection is a semantic nitpick, and the latter is an OVERgeneralization? Why not restrict unpacking only to lists as RHS, and convert all your iterables to lists first? Who needs this overgeneralization of allowing any sequence type to be unpacked, right? To answer that question: for the same reasons. The conversion is wasteful; allowing python to do the right thing based on a typeconstraint is not. Plus, it is less code, and more readable code; the only rule you have to learn is quite general, which is that :: is a type constraint annotation; no need to remember specifics, like 'unpacking always returns lists for some arbitrary reason'. From hoogendoorn.eelco at gmail.com Tue Dec 13 04:26:02 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 01:26:02 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> <2ff41b2e-90e3-4fae-9951-67bb257ed6ed@m10g2000vbc.googlegroups.com> Message-ID: <824d74b4-5495-447a-a9cb-addc22092a44@da3g2000vbb.googlegroups.com> > > Python users generally follow the rule "explicit is better than > > implicit". ?Setting a general constraint and letting the language "do > > the right thing" is a kind of black magic that feels off because it > > tends to break that rule. ?But that's not to say that black magic > > never wins -- just look at super() and the MRO. > > We are not talking black magic here; we are talking about an EXPLICIT > type constraint provided on the very same line. To hammer that point home: would you say the following C# code performs 'black magic'? float x = 3; After all, exactly the same thing is going on; C# will 'do the right thing'; convert the integer literal 3 to a floating point value, based on the type constraint placed on x. From fabiofz at gmail.com Tue Dec 13 04:33:38 2011 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Tue, 13 Dec 2011 07:33:38 -0200 Subject: PyDev and multithreaded application debug In-Reply-To: <3f193cae-deab-4d3f-a7d6-01ae4611515d@t16g2000vba.googlegroups.com> References: <3f193cae-deab-4d3f-a7d6-01ae4611515d@t16g2000vba.googlegroups.com> Message-ID: On Mon, Dec 12, 2011 at 2:30 PM, Massi wrote: > Hi everyone, I've just started to use pydev to develop my python > application and I'm encountering some problems to debug it. The > application I'm dealing with is a multithreaded application; when I > try to debug it with pydev, it seems not to be able to handle the > execution of multiple threads. The problem is that when the execution > reaches a breakpoint in one of the threads, all the other ones don't > stop, but they continue their execution. > Have I to make some specific settings or am I missing something? > Thanks in advance! Actually, that's expected, as you've only hit the breakpoint at that thread... if you want, you can press the pause button to stop the other threads. Cheers, Fabio From hoogendoorn.eelco at gmail.com Tue Dec 13 04:50:18 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 01:50:18 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> Message-ID: > To answer that question: for the same reasons. The conversion is > wasteful; allowing python to do the right thing based on a > typeconstraint is not. Plus, it is less code, and more readable code; > the only rule you have to learn is quite general, which is that :: is > a type constraint annotation; no need to remember specifics, like > 'unpacking always returns lists for some arbitrary reason'. Oh my bad; actually, that should be: 'collecting the remainder of an unpacked iterable using * will always yield a list. That is, unless the construct appears inside a function definition; then somehow a tuple is always the right choice' From peter at www.pjb.com.au Tue Dec 13 04:53:32 2011 From: peter at www.pjb.com.au (Peter Billam) Date: 13 Dec 2011 09:53:32 GMT Subject: Outputting raw MIDI in realtime on Linux References: <77fe6b9f-b148-4855-b238-9089678908de@n22g2000prh.googlegroups.com> <1683c805-7e4a-4ab4-8b6b-e825b09a9252@v24g2000prn.googlegroups.com> Message-ID: On 2011-12-12, alex23 wrote: > On Dec 12, 12:14?pm, Nick Irvine wrote: >> What do people use to output live MIDI on Linux, assuming it's >> possible? > I've yet to try this myself although it's long been on my to-do list. > There are a couple of packages on PyPI that emit MIDI: > http://pypi.python.org/pypi?%3Aaction=search&term=midi Check out http://www.pjb.com.au/midi/index.html http://www.pjb.com.au/midi/MIDI.html but what you _really_ want is Patricio Paez' alsaseq and alsamidi http://pp.com.mx/python/alsaseq which I translated into Lua and Perl: http://www.pjb.com.au/comp/lua/midialsa.html http://search.cpan.org/perldoc?MIDI::ALSA although by now my translations have developed slightly more features and fewer quirks than the original... Hope this helps, Peter -- Peter Billam www.pjb.com.au www.pjb.com.au/comp/contact.html From jeanmichel at sequans.com Tue Dec 13 04:54:51 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 13 Dec 2011 10:54:51 +0100 Subject: Overriding a global In-Reply-To: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EE720EB.40301@sequans.com> Steven D'Aprano wrote: > On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: > > >> Using the same name for 2 different objects is a bad idea in general. >> > > We have namespaces precisely so you don't need to care about making names > globally unique. > > > I don't get your point, namespaced names are unique, by definition. foo.aname <> bar.aname The OP showed a code where there was a confusion between a global name and a local one. There's no namespace involved. Having a local name identical to a global one is a bad idea, def. JM From arnodel at gmail.com Tue Dec 13 05:15:33 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Tue, 13 Dec 2011 10:15:33 +0000 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> Message-ID: On 13 December 2011 09:50, Eelco wrote: >> To answer that question: for the same reasons. The conversion is >> wasteful; allowing python to do the right thing based on a >> typeconstraint is not. Plus, it is less code, and more readable code; >> the only rule you have to learn is quite general, which is that :: is >> a type constraint annotation; no need to remember specifics, like >> 'unpacking always returns lists for some arbitrary reason'. > > Oh my bad; actually, that should be: > > 'collecting the remainder of an unpacked iterable using * will always > yield a list. That is, unless the construct appears inside a function > definition; then somehow a tuple is always the right choice' When you quote somebody (even yourself), it would be helpful if you attributed your quote. -- Arnaud From hoogendoorn.eelco at gmail.com Tue Dec 13 05:39:52 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 02:39:52 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> Message-ID: On 13 dec, 11:15, Arnaud Delobelle wrote: > On 13 December 2011 09:50, Eelco wrote: > > >> To answer that question: for the same reasons. The conversion is > >> wasteful; allowing python to do the right thing based on a > >> typeconstraint is not. Plus, it is less code, and more readable code; > >> the only rule you have to learn is quite general, which is that :: is > >> a type constraint annotation; no need to remember specifics, like > >> 'unpacking always returns lists for some arbitrary reason'. > > > Oh my bad; actually, that should be: > > > 'collecting the remainder of an unpacked iterable using * will always > > yield a list. That is, unless the construct appears inside a function > > definition; then somehow a tuple is always the right choice' > > When you quote somebody (even yourself), it would be helpful if you > attributed your quote. > > -- > Arnaud Ah yes; im more used to proper forums, still getting used to these mailing-list things. But point taken. From hoogendoorn.eelco at gmail.com Tue Dec 13 05:46:13 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 02:46:13 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> Message-ID: <1b95ae2c-1e94-4860-9dc8-1e5a2ff0537d@i8g2000vbh.googlegroups.com> With all this being said, I must say that the notion of indtroducing type constraints into Python is quite a radical one*, and one that should not be taken lightly, so I understand the general conservative vibe the notion is getting. It probably has implications beyond just collection types, and if youd introduce such a feature, you would like to introduce it only once, and correctly the first time around. Ill probably start a new thread soon, recapping the accumulated insight, and capping all the OT threads that have spawned. *even though the asteriks syntax is infact a limited form of exactly that From andrea.crotti.0 at gmail.com Tue Dec 13 05:59:00 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 10:59:00 +0000 Subject: change a file with a context manager Message-ID: <4EE72FF4.8010203@gmail.com> So I have the following problem, I need something to copy a file to a certain position and restore it after. So I wrote this simple context manager: class WithCorrectEasyInstall(object): def __enter__(self): import pkg_resources from shutil import copyfile easy_install_file = pkg_resources.resource_filename('psi.devsonly', 'windows_easy_install.pth') self.global_easy_install = 'c:/python25/Lib/site-packages/easy-install.pth' # can use mmap, a temporary file or just write to string self.stored = open(self.global_easy_install).read() logger.debug("copying the correct easy_install.pth file to the global position") copyfile(easy_install_file, self.global_easy_install) def __exit__(self, type, value, traceback): #TODO: make sure it's restored also when quitting in some other ways # restore the old file open(self.global_easy_install, 'w').write(self.stored) logger.debug("restoring the old easy_install.pth file") The problem is that this is not executed for example when the program is interrupted with for example a KeyboardInterrupt. But that's also an exception, why is it not caught by the context manager? And if there are better way to do what I'm trying to achieve they are welcome :) From gawali.jitesh at gmail.com Tue Dec 13 06:08:10 2011 From: gawali.jitesh at gmail.com (Jitesh) Date: Tue, 13 Dec 2011 03:08:10 -0800 (PST) Subject: NumPy book review copies Message-ID: Hi, As a part of our reviewing program, we are giving away limited number of copies (print & electronic) of our recent publication NumPy 1.5 Beginner's Guide to people interested in reviewing the book. Book-link: http://link.packtpub.com/sibctR You need to publish your review/feedback on either your blog or on websites like Barnes and Noble, Slashdot and so on as per your choice. We also encourage uploading the review on Amazon since it gives buyers a chance to know about the book through your perspective. For more information, please get in touch with me on my email jiteshg AT packtpub DOT com Thanks, Jitesh From steve+comp.lang.python at pearwood.info Tue Dec 13 06:13:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 11:13:16 GMT Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> Message-ID: <4ee7334c$0$29979$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Dec 2011 01:15:46 -0800, Eelco wrote: > On Dec 13, 3:43?am, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Mon, 12 Dec 2011 04:21:15 -0800, Eelco wrote: >> >> No more, or less, explicit than the difference between "==" and >> >> "is". >> >> > == may be taken to mean identity comparison; 'equals' can only mean >> > one thing. [...] > We are not talking mathemathics, we are talking programming languages. What *I* am talking about is your assertion that there is only one possible meaning for "equals" in the context of a programing language. This is *simply not correct*. You don't have to believe me, just look at the facts. It's hard to find languages that use the word "equals" (or very close to it) rather than equals signs, but here are four languages which do: (1) OpenXION: Equals in OpenXION is weakly typed, like Perl: >1 + 1.0 equals "2" true (2) C# uses method notation: a.Equals(b) can be overridden, but for many types it defaults to value equality, that is, the equivalent to Python's a == b. (3) Ruby uses a.equal?(b) for "reference equality", that is, the equivalent of Python's "is" operator: irb(main):001:0> a = "abc" => "abc" irb(main):002:0> b = "abc" => "abc" irb(main):003:0> a.equal?(b) => false irb(main):004:0> a == b => true (4) Mathematica's Equal[x, y] can test values and expressions for equality. It may return true, false, or unevaluated (i.e. itself). Four languages that use Equals (or close to it) with four different behaviours. > Identity versus value equality is a well established concept within its > jargon. Within this context, 'equals' and 'is' have clearly defined > meanings. Incorrect. Most programming languages do not even have a concept of identity: identity is only(?) relevant to reference languages, like Python, where variables are references to objects. Even for languages that have a concept of identity, most don't don't call it "is". Objective-C calls it "==", PHP calls it "===", C# calls it object.ReferenceEquals. (Python, Algol 68, and VB .NET are three which do call it "is".) For stack-based languages like Forth, it doesn't even make sense to talk about identity, since values aren't variables: they're just values on a stack, not values in a fixed location, or bound to a known name. Again, all this goes to demonstrate that the language designer is free to choose any behaviour they like, and give it any name they like. [...] > So 'frequency of use' is no valid interpretation of 'obscurity'? Im not > a native speaker, but im pretty sure it is. No. Things which are obscure are used in language infrequently, because if they were common they would not be obscure. But things which are used infrequently are not necessarily obscure. An example in common language: "Napoleon Bonaparte" does not come up in conversation very frequently, but he is not an obscure historical figure. An example from programming: very few people need to use the trigonometric functions sin, cos, tan in their code. But they are not obscure functions: most people remember them from school. People who have forgotten almost everything about mathematics except basic arithmetic probably remember sin, cos and tan. But they never use them. >> >> And you can blame C for the use of % instead of mod or modulo. >> >> > I didnt know one of Python's design goals was backwards compatibility >> > with C. >> >> Don't be silly. You know full well Python is not backwards compatible >> with C, even if they do share some syntactical features. > > Of course I was being silly; I know this use is following a historical > precedent; but id rather see it rectified in the previous version of > python rather than the next. My sillyness was prompted by the percieved > pointlessness of your remark. Of course python is not trying to be > backwards compatible with C; so why bring it up then? Because you asked why Python uses the % operator for remainder. [...] > They are bad ideas because they truely do not lead to the execution of > different code, but are merely a reordering, mixing statements in with a > function declaration. I am proposing no such thing; again, the type(arg) > notation I have dropped, and never was meant to have anything to do with > function calling; it is a way of supplying an optional type constraint, > so in analogy with function annotations, I changed that to arg::type. > Again, this has nothing to do with calling functions on arguments. You have not thought about this carefully enough. Consider what happens when this code gets called: def f(*args): pass f(a, b, c) The Python virtual machine (interpreter, if you prefer) must take three arguments a, b, c and create a tuple from them. This must happen at runtime, because the value of the objects is not known at compile time. So at some point between f(a, b, c) being called and the body of f being entered, a tuple must be created, and the values of a, b, c must be collated into a single tuple. Now extend this reasoning to your proposal: def f(args:FOO): pass At runtime, the supplied arguments must be collated into a FOO, whatever FOO happens to be. Hence, the function that creates FOO objects must be called before the body of f can be entered. This doesn't happen for free. Whether you do it manually, or have the Python interpreter do it, it still needs to be done. > First off, type constraints must have some use; all those languages cant > be wrong. But you're not talking about type constraints. You're not instructing the function to reject arguments which have the wrong type, you are instructing it to collate multiple arguments into a list (instead of a tuple like Python currently does). def f(*args) *constructs* a tuple, it doesn't perform a type-check. -- Steven From steve+comp.lang.python at pearwood.info Tue Dec 13 06:15:32 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 11:15:32 GMT Subject: Overriding a global References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Dec 2011 10:54:51 +0100, Jean-Michel Pichavant wrote: > Steven D'Aprano wrote: >> On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: >> >> >>> Using the same name for 2 different objects is a bad idea in general. >>> >>> >> We have namespaces precisely so you don't need to care about making >> names globally unique. >> >> >> > I don't get your point, namespaced names are unique, by definition. > > foo.aname <> bar.aname Assuming foo and bar are not names for the same object, there are at least three namespaces here: the local namespace, where foo and bar can be found, the foo.__dict__ namespace, and the bar.__dict__ namespace. > The OP showed a code where there was a confusion between a global name > and a local one. There's no namespace involved. Having a local name > identical to a global one is a bad idea, def. Of course there are namespaces involved. There is the global namespace, and the local namespace. That's how you can have x inside a function without it overwriting global x outside of it, because they are different namespaces. Which is my point. When I write this: x = 1 def spam(): x = 2 def ham(): x = 3 The three x's don't clash because they are in three separate namespaces. -- Steven From corleone82 at gmail.com Tue Dec 13 06:21:07 2011 From: corleone82 at gmail.com (Juan Perez) Date: Tue, 13 Dec 2011 11:21:07 +0000 Subject: automate commands to an .exe console program through python In-Reply-To: References: Message-ID: Well, if developers have to change something to the code, I'd continue with autoit, since is closer to real enviroment. You are right, autoit work by send parametres to GUI focused, but really I don't think it's something necessary for my purposes as I need only send parameters to a windows cmd console. Do you know if is that possible by piping standard input? I've tried but with no results. What I'd need is open a new windows console get the pid and send messages to it through stdin pipe. But I've been unable to do it so far. Thanks, Juan 2011/12/12 Chris Angelico > On Tue, Dec 13, 2011 at 3:41 AM, Juan Perez wrote: > > I need to automate a .exe console program in windows, and send key > > characters like 'a', 'M' ... > > I had this running in an autoIT script which with only activating cmd > > window, and with the "send" parameter I had all already done. But I'm > having > > a lot of problems with python, I've tried with process.call, > communicate(), > > os.popen ... but no results. I can't comunicate with the program and > even i > > can't maintain it opened more than a few seconds. If I open a program > like > > notepad.exe it remains opened but when I try to comunicate with the stdin > > pipe just don't write anything to the program. > > GUI programs such as Notepad usually don't read from STDIN, which is > where text goes if you write to a pipe. You may have to check out how, > exactly, the program accepts commands; your autoIT script is probably > sending keystrokes using the Wndows GUI, so it works as long as the > program has focus. > > Ideally, look into whether the program has an actual automation mode - > and if it doesn't have one, ask the programmer to expose his code > directly to a Python script. Hey, it's worth a shot! :) > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue Dec 13 06:24:59 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 22:24:59 +1100 Subject: automate commands to an .exe console program through python In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 10:21 PM, Juan Perez wrote: > Well, if developers have to change something to the code, I'd continue with > autoit, since is closer to real enviroment. You are right, autoit work by > send parametres to GUI focused, but really I don't think it's something > necessary for my purposes as I need only send parameters to a windows cmd > console. Do you know if is that possible by piping standard input? I've > tried but with no results. What I'd need is open a new windows console get > the pid and send messages to it through stdin pipe. But I've been unable to > do it so far. The easiest way to see whether a pipe will work or not is to put the commands into a file and use redirection: NameOfProgram <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> <1b95ae2c-1e94-4860-9dc8-1e5a2ff0537d@i8g2000vbh.googlegroups.com> Message-ID: <4ee736d6$0$29979$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Dec 2011 02:46:13 -0800, Eelco wrote: > With all this being said, I must say that the notion of indtroducing > type constraints into Python is quite a radical one*, Not that radical. Here's the creator of Python musing about adding optional type checks to Python: http://www.artima.com/weblogs/viewpost.jsp?thread=85551 http://www.artima.com/weblogs/viewpost.jsp?thread=86641 http://www.artima.com/weblogs/viewpost.jsp?thread=87182 [...] > *even though the asteriks syntax is infact a limited form of exactly > that It absolutely is not. def f(*args, **kwargs) constructs a tuple and a dict, it does not type-check that the function is passed a tuple and a dict as arguments. These are completely different things. -- Steven From mail at timgolden.me.uk Tue Dec 13 06:34:31 2011 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 13 Dec 2011 11:34:31 +0000 Subject: automate commands to an .exe console program through python In-Reply-To: References: Message-ID: <4EE73847.30404@timgolden.me.uk> On 13/12/2011 11:21, Juan Perez wrote: > Well, if developers have to change something to the code, I'd continue > with autoit, since is closer to real enviroment. You are right, autoit > work by send parametres to GUI focused, but really I don't think it's > something necessary for my purposes as I need only send parameters to a > windows cmd console. Do you know if is that possible by piping standard > input? I've tried but with no results. What I'd need is open a new > windows console get the pid and send messages to it through stdin pipe. > But I've been unable to do it so far. I think that you're thinking about this the wrong way. You're talking about automating a Console program as though it were notepad: by sending keystrokes. Mixed in with that you've got some kind of notion of piping stdin/stdout. If you've got a (non-GUI) program which you usually run in a Console context, it will either take its input from stdin -- in which case, ignore the fact it runs in a console and feed it via subprocess -- or it will take input from some kind of Console-level getch method, in which case you'll have to use something like SendKeys [1] or perhaps WinPexpect [2] Don't know if that helps at all... TJG [1] http://www.rutherfurd.net/python/sendkeys/ [2] https://bitbucket.org/geertj/winpexpect/wiki/Home From storchaka at gmail.com Tue Dec 13 07:17:30 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 13 Dec 2011 14:17:30 +0200 Subject: Fixing the XML batteries In-Reply-To: References: Message-ID: <4EE7425A.2090006@gmail.com> 09.12.11 17:09, Dirkjan Ochtman wrote: > On Fri, Dec 9, 2011 at 09:02, Stefan Behnel wrote: > An at least somewhat informed +1 from me. The ElementTree API is a > very good way to deal with XML from Python, and it deserves to be > promoted over the included alternatives. > > Let's deprecate the NiCad batteries and try to guide users toward the > Li-Ion ones. I use xml.dom.minidom for XML canonization and convertion: doc = xml.dom.minidom.parse( io.BytesIO( data0 ) ) encoding = forceEncoding or doc.encoding or str( 'UTF-8' ) writer = io.BytesIO() writer = codecs.getwriter( encoding )( writer, 'xmlcharrefreplace' ) doc.writexml( writer, encoding = encoding ) data = writer.getvalue() How to do this with xml.etree.ElementTree? From storchaka at gmail.com Tue Dec 13 07:18:26 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 13 Dec 2011 14:18:26 +0200 Subject: Fixing the XML batteries In-Reply-To: References: Message-ID: <4EE74292.4080708@gmail.com> 09.12.11 17:09, Dirkjan Ochtman wrote: > On Fri, Dec 9, 2011 at 09:02, Stefan Behnel wrote: > An at least somewhat informed +1 from me. The ElementTree API is a > very good way to deal with XML from Python, and it deserves to be > promoted over the included alternatives. > > Let's deprecate the NiCad batteries and try to guide users toward the > Li-Ion ones. I use xml.dom.minidom for XML canonization and convertion: doc = xml.dom.minidom.parse( io.BytesIO( data0 ) ) encoding = forceEncoding or doc.encoding or str( 'UTF-8' ) writer = io.BytesIO() writer = codecs.getwriter( encoding )( writer, 'xmlcharrefreplace' ) doc.writexml( writer, encoding = encoding ) data = writer.getvalue() How to do this with xml.etree.ElementTree? From hoogendoorn.eelco at gmail.com Tue Dec 13 07:19:26 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 04:19:26 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> <1b95ae2c-1e94-4860-9dc8-1e5a2ff0537d@i8g2000vbh.googlegroups.com> <4ee736d6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13 dec, 12:28, Steven D'Aprano wrote: > On Tue, 13 Dec 2011 02:46:13 -0800, Eelco wrote: > > With all this being said, I must say that the notion of indtroducing > > type constraints into Python is quite a radical one*, > > Not that radical. Here's the creator of Python musing about adding > optional type checks to Python: > > http://www.artima.com/weblogs/viewpost.jsp?thread=85551http://www.artima.com/weblogs/viewpost.jsp?thread=86641http://www.artima.com/weblogs/viewpost.jsp?thread=87182 Good find; but still radical enough that it hasnt been implemented. Note that these musing are trying to adress a yet far more general problem of specifying arbitrary types constraints on anything; I am primarily interested in specifying container types in the special case of collection packing/unpacking syntax, with further extensions nothing but a welcome addon. The fact that the former was judged infeasible does not mean the more modest goal of the latter might not be attainable. > > *even though the asteriks syntax is infact a limited form of exactly > > that > > It absolutely is not. def f(*args, **kwargs) constructs a tuple and a > dict, it does not type-check that the function is passed a tuple and a > dict as arguments. These are completely different things. Which is of course not something I ever proposed; I never said anything about checking types of existing data; im talking about coercing types of newly created data, like the target of a collection packing. That is exactly what *args and **kwargs also do. From stefan_ml at behnel.de Tue Dec 13 07:32:53 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 13 Dec 2011 13:32:53 +0100 Subject: Fixing the XML batteries In-Reply-To: <4EE7425A.2090006@gmail.com> References: <4EE7425A.2090006@gmail.com> Message-ID: Serhiy Storchaka, 13.12.2011 13:17: > 09.12.11 17:09, Dirkjan Ochtman wrote: >> An at least somewhat informed +1 from me. The ElementTree API is a >> very good way to deal with XML from Python, and it deserves to be >> promoted over the included alternatives. >> >> Let's deprecate the NiCad batteries and try to guide users toward the >> Li-Ion ones. I stripped my name from the quoted context because I didn't say this. > I use xml.dom.minidom for XML canonization and convertion: Do you mean "canonicalisation"? I.e. C14N? That's not what the code below is doing, not at all. > doc = xml.dom.minidom.parse( io.BytesIO( data0 ) ) > encoding = forceEncoding or doc.encoding or str( 'UTF-8' ) > writer = io.BytesIO() > writer = codecs.getwriter( encoding )( writer, 'xmlcharrefreplace' ) > doc.writexml( writer, encoding = encoding ) > data = writer.getvalue() > > How to do this with xml.etree.ElementTree? In Python 2.7/3.2, ElementTree has support for C14N serialisation, just pass the option method="c14n". Stefan From corleone82 at gmail.com Tue Dec 13 07:35:28 2011 From: corleone82 at gmail.com (Juan Perez) Date: Tue, 13 Dec 2011 12:35:28 +0000 Subject: automate commands to an .exe console program through python In-Reply-To: <4EE73847.30404@timgolden.me.uk> References: <4EE73847.30404@timgolden.me.uk> Message-ID: Thank you for your responses, My problem is thinking in windows console somewhat like linux shell, and do same things I did with pipes in C programming. But it seems not to be the case. At a glance I dare say that sendkeys module is useful enough, but if I've understood well, you only can send messages to focused window, that will report some inestabilities if windows focused is changed. You won't be able to touch anything in the current laptop. So, is there any way to identify the a process/window and send keystrokes in a smarter way and/or is it possible to identify a window an get the focus on it? I'm worried that pexpect is a too dificult way... Thanks, Juan 2011/12/13 Tim Golden > On 13/12/2011 11:21, Juan Perez wrote: > >> Well, if developers have to change something to the code, I'd continue >> with autoit, since is closer to real enviroment. You are right, autoit >> work by send parametres to GUI focused, but really I don't think it's >> something necessary for my purposes as I need only send parameters to a >> windows cmd console. Do you know if is that possible by piping standard >> input? I've tried but with no results. What I'd need is open a new >> windows console get the pid and send messages to it through stdin pipe. >> But I've been unable to do it so far. >> > > I think that you're thinking about this the wrong way. You're talking > about automating a Console program as though it were notepad: by sending > keystrokes. Mixed in with that you've got some kind of notion of piping > stdin/stdout. > > If you've got a (non-GUI) program which you usually run in a Console > context, it will either take its input from stdin -- in which case, > ignore the fact it runs in a console and feed it via subprocess -- > or it will take input from some kind of Console-level getch method, > in which case you'll have to use something like SendKeys [1] or perhaps > WinPexpect [2] > > Don't know if that helps at all... > > TJG > > > [1] http://www.rutherfurd.net/**python/sendkeys/ > > [2] https://bitbucket.org/geertj/**winpexpect/wiki/Home > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue Dec 13 07:40:50 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 23:40:50 +1100 Subject: automate commands to an .exe console program through python In-Reply-To: References: <4EE73847.30404@timgolden.me.uk> Message-ID: On Tue, Dec 13, 2011 at 11:35 PM, Juan Perez wrote: > My problem is thinking in windows console somewhat like linux shell, and do > same things I did with pipes in C programming. But it seems not to be the > case. It is (at least, it supports the basics of pipes and redirection); but what you're looking at here is an application that appears not to use STDIN. > At a glance I dare say that sendkeys module is useful? enough, but if I've > understood well, you only can send messages to focused window, that will > report some inestabilities if windows focused is changed. You won't be able > to touch anything in the current laptop. So, is there any way to identify > the a process/window and send keystrokes in a smarter way and/or is it > possible to identify a window an get the focus on it? You may be able to put focus to the window before sending it keys, but this is sounding extremely hackish (nearly as bad as the Magic: The Gathering Online trade-bot system). I'm thinking here the best option may be to give this a dedicated virtual machine and let it do whatever it likes - effectively, have an entire "computer" dedicated to just this application and its automation harness. That would probably make things simple enough that you don't need to worry too much about focus, sending keystrokes to other windows, etc. ChrisA From hoogendoorn.eelco at gmail.com Tue Dec 13 07:47:58 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 04:47:58 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> <4ee7334c$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13 dec, 12:13, Steven D'Aprano wrote: > On Tue, 13 Dec 2011 01:15:46 -0800, Eelco wrote: > > On Dec 13, 3:43?am, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> wrote: > >> On Mon, 12 Dec 2011 04:21:15 -0800, Eelco wrote: > >> >> No more, or less, explicit than the difference between "==" and > >> >> "is". > > >> > == may be taken to mean identity comparison; 'equals' can only mean > >> > one thing. > [...] > > We are not talking mathemathics, we are talking programming languages. > > What *I* am talking about is your assertion that there is only one > possible meaning for "equals" in the context of a programing language. > This is *simply not correct*. > > You don't have to believe me, just look at the facts. It's hard to find > languages that use the word "equals" (or very close to it) rather than > equals signs, but here are four languages which do: That python is not the only language to not get this quite as right as could be is known to me. But within computer science as a discipline, equality and identity comparisons have a clear enough meaning. 'is' has a narrower meaning than 'equals'. '==' has no meaning whatsoever in computer science. > Again, all this goes to demonstrate that the language designer is free to > choose any behaviour they like, and give it any name they like. Certainly you demonstrated as much. Programming languages are created by people, and they have a tendency to make mistakes, and to have to compromise (backwards compatibility, and so on). Thats a seperate matter from 'what ought to be done', when discussing optimal language design. > > So 'frequency of use' is no valid interpretation of 'obscurity'? Im not > > a native speaker, but im pretty sure it is. > > No. Things which are obscure are used in language infrequently, because > if they were common they would not be obscure. But things which are used > infrequently are not necessarily obscure. > > An example in common language: "Napoleon Bonaparte" does not come up in > conversation very frequently, but he is not an obscure historical figure. > > An example from programming: very few people need to use the > trigonometric functions sin, cos, tan in their code. But they are not > obscure functions: most people remember them from school. People who have > forgotten almost everything about mathematics except basic arithmetic > probably remember sin, cos and tan. But they never use them. I dont think its terribly interesting to debate whether the term obscure applies to trigonometric functions or not: the important matter is that they are where they should be; under math.cos, etc. They dont have their own special character, and I hope you agree that is as it should be. I use trig far more often than modulus, so that argues in favor of modulus being under math too; infact I used modulus quite recently, but naturally it was in a piece of code that should be done in C eventually anyway (evaluating subdivision surfaces) > Because you asked why Python uses the % operator for remainder. So you ARE implying python has backwards compatibility with C as a design goal? Otherwise the given answer to this question is nonsensical. > > [...] > > > They are bad ideas because they truely do not lead to the execution of > > different code, but are merely a reordering, mixing statements in with a > > function declaration. I am proposing no such thing; again, the type(arg) > > notation I have dropped, and never was meant to have anything to do with > > function calling; it is a way of supplying an optional type constraint, > > so in analogy with function annotations, I changed that to arg::type. > > Again, this has nothing to do with calling functions on arguments. > > You have not thought about this carefully enough. Consider what happens > when this code gets called: > > def f(*args): pass > > f(a, b, c) > > The Python virtual machine (interpreter, if you prefer) must take three > arguments a, b, c and create a tuple from them. This must happen at > runtime, because the value of the objects is not known at compile time. > So at some point between f(a, b, c) being called and the body of f being > entered, a tuple must be created, and the values of a, b, c must be > collated into a single tuple. > > Now extend this reasoning to your proposal: > > def f(args:FOO): pass > > At runtime, the supplied arguments must be collated into a FOO, whatever > FOO happens to be. Hence, the function that creates FOO objects must be > called before the body of f can be entered. This doesn't happen for free. > Whether you do it manually, or have the Python interpreter do it, it > still needs to be done. Of course the python interpreted needs to do this; and in case non- builtin types are allowed, the mechanism is going to be through their constructor. But thats a detail; the syntax doesnt say: 'please call this constructor for me', any more than **kwargs says 'please call a dict constructor for me', even though equivalent operations are obviously going on under the hood as part of the process. Yes, whatever type you have needs to be constructed, and its not magically going to happen, its going to cost CPU cycles. The question is, do you end up constructing both a tuple and a list, or do you construct the list directly? > > First off, type constraints must have some use; all those languages cant > > be wrong. > > But you're not talking about type constraints. You're not instructing the > function to reject arguments which have the wrong type, you are > instructing it to collate multiple arguments into a list (instead of a > tuple like Python currently does). def f(*args) *constructs* a tuple, it > doesn't perform a type-check. I am talking about type constraints, but as seems to be the usual pattern in our miscommunications, you seek to attach a specific far fetched meaning to it that I never intended. Insofar as I understand these terms, a type-constraint is part of a declaration; float x; in C- family languages, args::list in python 4 perhaps. It narrows down the semantics for further usage of that symbol. A type-check is something along the lines of type(args)==list, a runtime thing and something completely different. I havnt mentioned the latter at all, explicitly or implicitly, as far as im aware. From rosuav at gmail.com Tue Dec 13 08:14:33 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Dec 2011 00:14:33 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> <4ee7334c$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Dec 13, 2011 at 11:47 PM, Eelco wrote: >> def f(*args) *constructs* a tuple, it >> doesn't perform a type-check. > > I am talking about type constraints... A type-check is something > along the lines of type(args)==list, a runtime thing and something > completely different. I havnt mentioned the latter at all, explicitly > or implicitly, as far as im aware. I'm not sure what you mean by a "type constraint". Here's how I understand such: float|int foobar; //Declare that the variable 'foobar' is allowed to hold a float or an int foobar = 3.2; //Legal. foobar = 1<<200; //Also legal (a rather large integer value) foobar = "hello"; //Not legal Python doesn't have any such thing (at least, not in-built). Any name may be bound to any value - or if you like, any variable can contain any type. Same applies to argument passing - you can even take an unbound method and call it with some completely different type of object as its first parameter. (I can't think of ANY situation in which this would not be insanely confusing, tbh.) When you gather a function's arguments into a tuple, list, or any other such container object, what you're doing is constructing another object and stuffing it with references to the original arguments. That involves building a new object, where otherwise you simply bind the arguments' names to the original objects directly. Now, it is perfectly conceivable to have designed Python to _always_ pass tuples around. Instead of a set of arguments, you just always pass exactly one tuple to a function, and that function figures out what to do with the args internally. If that's the way the language is built, then it is the _caller_, not the callee, who builds that tuple; and we still have the same consideration if we want a list instead. So suppose you can have a user-defined object type instead of list/dict. How are you going to write that type's __init__ function? Somewhere along the way, you need to take a variable number of arguments and bundle them up into a single one... so somewhere, you need the interpreter to build it for you. This is going to end up exactly the same as just accepting the tuple and then passing that to a constructor, like the list example. Keep things transparent and you make debugging a LOT easier. ChrisA From jeanmichel at sequans.com Tue Dec 13 08:30:42 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 13 Dec 2011 14:30:42 +0100 Subject: Overriding a global In-Reply-To: <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EE75382.9060104@sequans.com> Steven D'Aprano wrote: > On Tue, 13 Dec 2011 10:54:51 +0100, Jean-Michel Pichavant wrote: > > >> Steven D'Aprano wrote: >> >>> On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: >>> >>> >>> >>>> Using the same name for 2 different objects is a bad idea in general. >>>> >>>> >>>> >>> We have namespaces precisely so you don't need to care about making >>> names globally unique. >>> >>> >>> >>> >> I don't get your point, namespaced names are unique, by definition. >> >> foo.aname <> bar.aname >> > > Assuming foo and bar are not names for the same object, there are at > least three namespaces here: the local namespace, where foo and bar can > be found, the foo.__dict__ namespace, and the bar.__dict__ namespace. > > > >> The OP showed a code where there was a confusion between a global name >> and a local one. There's no namespace involved. Having a local name >> identical to a global one is a bad idea, def. >> > > Of course there are namespaces involved. There is the global namespace, > and the local namespace. That's how you can have x inside a function > without it overwriting global x outside of it, because they are different > namespaces. Which is my point. > > When I write this: > > x = 1 > > def spam(): > x = 2 > > def ham(): > x = 3 > > The three x's don't clash because they are in three separate namespaces. > > I know that, why do I have the feel this is juste a semantic issue ? Aren't you/I/we confusion namespaces & scopes ? Anyway, semantic is not what we're interested in here. writing x = 1 def spam(): x = 2 is in general a bad idea. That was my point. JM From hoogendoorn.eelco at gmail.com Tue Dec 13 08:35:27 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 05:35:27 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> <4ee7334c$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13 dec, 14:14, Chris Angelico wrote: > On Tue, Dec 13, 2011 at 11:47 PM, Eelco wrote: > >> def f(*args) *constructs* a tuple, it > >> doesn't perform a type-check. > > > I am talking about type constraints... A type-check is something > > along the lines of type(args)==list, a runtime thing and something > > completely different. I havnt mentioned the latter at all, explicitly > > or implicitly, as far as im aware. > > I'm not sure what you mean by a "type constraint". Here's how I understand such: > > float|int foobar; //Declare that the variable 'foobar' is allowed to > hold a float or an int > foobar = 3.2; //Legal. > foobar = 1<<200; //Also legal (a rather large integer value) > foobar = "hello"; //Not legal > > Python doesn't have any such thing (at least, not in-built). Agreed on what a type constraint is, and that python does not really have any, unless one counts the current use of asterikses, which are infact a limited form of type constrait (*tail is not just any object, but one holding a list, which modifies the semantics of the assignment statement 'head,*tail=sequence' from a regular tuple unpacking to a specific form of the more general collection unpacking syntax); The idea is to enrich this syntax; to add optional and limited type constraints to python, specifically to enrich collection packing/ unpacking syntax, but perhaps the concept can be further generalized. > So suppose you can have a user-defined object type instead of > list/dict. How are you going to write that type's __init__ function? > Somewhere along the way, you need to take a variable number of > arguments and bundle them up into a single one... so somewhere, you > need the interpreter to build it for you. This is going to end up > exactly the same as just accepting the tuple and then passing that to > a constructor, like the list example. Keep things transparent and you > make debugging a LOT easier. Agreed; for user defined collection types there would not be a performance benefit over converting a tuple, since this is exactly what will happen anyway, but for collection types derived from any of the builtins, python could optimize away the intermediate and construct the desired collection type directly from the available information. From hfaber at invalid.net Tue Dec 13 08:36:39 2011 From: hfaber at invalid.net (Henrik Faber) Date: Tue, 13 Dec 2011 14:36:39 +0100 Subject: Signature-preserving decorators Message-ID: Hi group, when decorating a method in Python3, by use of the functools.update_wrapper function, it can be achieved that the docstring and name of the original function is preseverved. However, the prototype is lost: When looking into the Python help, I have lots of entries that look like: getfoo(*args, **kwargs) -> int setbar(*args, **kwargs) As you can imagine, this is really not very self-explanatory. I've seen a solution which constructs a wrapper's wrapper function using inspection and eval -- this looks really dirty to me, however. Then there's the "decorator" external module -- but I'd like to do it with on-board tools. Is this possible in Python3 with too much of a hassle? Best regards, Joe From andrea.crotti.0 at gmail.com Tue Dec 13 09:21:33 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 14:21:33 +0000 Subject: change a file with a context manager In-Reply-To: <4EE72FF4.8010203@gmail.com> References: <4EE72FF4.8010203@gmail.com> Message-ID: <4EE75F6D.5070604@gmail.com> On 12/13/2011 10:59 AM, Andrea Crotti wrote: > So I have the following problem, I need something to copy a file to a > certain position > and restore it after. > So I wrote this simple context manager: > > class WithCorrectEasyInstall(object): > > def __enter__(self): > import pkg_resources > from shutil import copyfile > easy_install_file = > pkg_resources.resource_filename('psi.devsonly', > 'windows_easy_install.pth') > self.global_easy_install = > 'c:/python25/Lib/site-packages/easy-install.pth' > # can use mmap, a temporary file or just write to string > self.stored = open(self.global_easy_install).read() > logger.debug("copying the correct easy_install.pth file to the > global position") > copyfile(easy_install_file, self.global_easy_install) > > def __exit__(self, type, value, traceback): > #TODO: make sure it's restored also when quitting in some > other ways > # restore the old file > open(self.global_easy_install, 'w').write(self.stored) > logger.debug("restoring the old easy_install.pth file") > > > The problem is that this is not executed for example when the program > is interrupted > with for example a KeyboardInterrupt. > But that's also an exception, why is it not caught by the context > manager? > > And if there are better way to do what I'm trying to achieve they are > welcome :) Alright it was probably a stupid idea to start with. The problem is that if the easy_install is wrong even the imports in the module will not work, so I can't even start to do something. Another possible option is to just add to the path everything contained in that correct easy_install.pth file. Or is there a way to reload the path settings? From dihedral88888 at googlemail.com Tue Dec 13 09:21:58 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 13 Dec 2011 06:21:58 -0800 (PST) Subject: Dynamic variable creation from string In-Reply-To: <4ee6abf8$0$11091$c3e8da3@news.astraweb.com> References: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> <24728986.18.1323694150411.JavaMail.geo-discussion-forums@prfc16> <2b414cd2-0f0a-45bf-b61e-9205704ac03a@u10g2000prl.googlegroups.com> <4ee6abf8$0$11091$c3e8da3@news.astraweb.com> Message-ID: <14074134.993.1323786118117.JavaMail.geo-discussion-forums@pruu5> On Tuesday, December 13, 2011 9:35:52 AM UTC+8, Steven D'Aprano wrote: > On Mon, 12 Dec 2011 15:45:06 -0800, alex23 wrote: > > > On Dec 12, 10:49?pm, 88888 Dihedral > > wrote: > >> This is the way to write an assembler or to roll out a script language > >> to be included in an app by users. > > > > This is a garbage comment that has absolutely nothing to do with the > > topic at hand _at all_. > > Please stop responding to the bot. > > > > -- > Steven Do we need to talk aout auto, extern, static in python? Of course not for an interpreter? From storchaka at gmail.com Tue Dec 13 09:27:56 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 13 Dec 2011 16:27:56 +0200 Subject: Fixing the XML batteries In-Reply-To: References: <4EE7425A.2090006@gmail.com> Message-ID: 13.12.11 14:32, Stefan Behnel ???????(??): > I stripped my name from the quoted context because I didn't say this. I am glad to hear this. ;) >> I use xml.dom.minidom for XML canonization and convertion: > Do you mean "canonicalisation"? I.e. C14N? That's not what the code > below is doing, not at all. No, only converting to certain textual representation. Converting entities to text (if possible), converting "'" to '"' for quoting, sorting attributes, etc. >> How to do this with xml.etree.ElementTree? > In Python 2.7/3.2, ElementTree has support for C14N serialisation, just > pass the option method="c14n". Thanks, I will try this. But I need 2.6+ compatibility. From stefan_ml at behnel.de Tue Dec 13 09:59:52 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 13 Dec 2011 15:59:52 +0100 Subject: Fixing the XML batteries In-Reply-To: References: <4EE7425A.2090006@gmail.com> Message-ID: Serhiy Storchaka, 13.12.2011 15:27: > 13.12.11 14:32, Stefan Behnel ???????(??): >> I stripped my name from the quoted context because I didn't say this. > > I am glad to hear this. ;) It matches my opinion though. >>> I use xml.dom.minidom for XML canonization and convertion: >> Do you mean "canonicalisation"? I.e. C14N? That's not what the code >> below is doing, not at all. > > No, only converting to certain textual representation. Converting entities > to text (if possible), converting "'" to '"' for quoting, sorting > attributes, etc. Yes, that's what C14N is there for, typically used for cryptography, hashing, etc. However, MiniDOM doesn't implement that standard, so you're on your own here. >>> How to do this with xml.etree.ElementTree? >> In Python 2.7/3.2, ElementTree has support for C14N serialisation, just >> pass the option method="c14n". > > Thanks, I will try this. But I need 2.6+ compatibility. The ET module is actually quite short (<1700 lines), so you can just copy the Py2.7 version into your sources and optionally import it on older Python releases. Since you only seem to depend on the serialiser (which is worth using anyway because it is much faster in the Py2.7 version), older platform versions of cET should also work just fine with that module copy, so you can basically just import everything from xml.etree.cElementTree and use the ElementTree class and the tostring() function from your own local version if the platform version is too old. Note that ET is also still available as a separately installable package, may or may not be simpler to use for you. Stefan From andrea.crotti.0 at gmail.com Tue Dec 13 10:42:49 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 15:42:49 +0000 Subject: logging issues Message-ID: <4EE77279.10104@gmail.com> I think is simple but I can't get it to work as I wish. Suppose I have a big application, my idea is that the running script sets a global logging level and then all the imported modules would act consequently. In my codebase, however, unless I set the level for each of the loggers I don't get the debug messages, not really sure why yet. To check if I understood, logging.getLogger() returns the root logger, and every other logger should inherit its settings, is that correct? What happens if there is another logging.getLogger() somewhere else in the code after the initialization? Does that become the root logger overriding the old one? The example below instead works as expected, so maybe is something with my codebase... # m1.py import logging logging.basicConfig() # if no name is specified return the root logger, that's how it works logger = logging.getLogger() logger.setLevel(logging.DEBUG) class ToLogEvents(object): def __init__(self): self.logger = logging.getLogger('ToLogEvents') def important(self): self.logger.warning("This is an important message") self.logger.debug("debug message") if __name__ == '__main__': logger.debug("debug from m1") from m2 import logging_function logging_function() t = ToLogEvents() t.important() # m2.py import logging logger = logging.getLogger(__name__) def logging_function(): logger.debug("debug message") logger.info("info message") From tinnews at isbd.co.uk Tue Dec 13 10:48:01 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Tue, 13 Dec 2011 15:48:01 +0000 Subject: Using methodcaller in a list sort - any examples anywhere? Message-ID: I want to sort a list of 'things' (they're fairly complex objects) by the contents of one of the fields I can extract from the 'things' using a Python function. So I have a list L which is a list of objects of some sort. I can output the contents of a field in the list as follows:- for k in L: print k.get_property('family-name') How can I sort the list first? As I said it seems like a methodcaller is the answer but I don't see how. I want to sort the list of objects not just produce a sorted list of names. -- Chris Green From gialloporpora at gmail.com Tue Dec 13 11:04:55 2011 From: gialloporpora at gmail.com (gialloporpora) Date: Tue, 13 Dec 2011 17:04:55 +0100 Subject: Getting the current directory path and the path where python script is saved Message-ID: Dear all, I would like to create a little script to rename my files, I think to use argparse module to create it. I would like to know how to obtain the path where script is executed and the path where the file script is stored. For example, if I save my script in c:\python27 and I put the directory in %path% I can execute it from any folder, I would like to get the two directory path. Thanks Sandro -- *gialloporpora: RT @themitcho: telnet miku.acm.uiuc.edu Do it now. You will not regret it. #nyan /via @beaulebens * - http://bit.ly/u2boVC Sto ascoltando: *Pink Floyd ? Echoe Part I * - http://bit.ly/vgIGQe From python.list at tim.thechases.com Tue Dec 13 11:08:06 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 13 Dec 2011 10:08:06 -0600 Subject: Using methodcaller in a list sort - any examples anywhere? In-Reply-To: References: Message-ID: <4EE77866.8060103@tim.thechases.com> On 12/13/11 09:48, tinnews at isbd.co.uk wrote: > I want to sort a list of 'things' (they're fairly complex objects) by > the contents of one of the fields I can extract from the 'things' > using a Python function. > > So I have a list L which is a list of objects of some sort. I can > output the contents of a field in the list as follows:- > > for k in L: > print k.get_property('family-name') > > How can I sort the list first? As I said it seems like a methodcaller > is the answer but I don't see how. I want to sort the list of objects > not just produce a sorted list of names. You want either sorted(..., key=...) to sort and return a copy (leaving the original unmodified) or .sort(key=...) to sort the list in-place: class MyObj(object): def __init__(self, fn): self.fn = fn def get_property(self, s): return "%s: %s" % (s, self.fn) def __str__(self): return self.fn __repr__ = __str__ source = [ MyObj("Doug"), MyObj("Carol"), MyObj("Bill"), MyObj("Adam"), ] print "Unsorted source before:" print repr(source) print "Using a lambda:" print repr(sorted(source, key=lambda mo: mo.get_property("family-name"))) print "Using methodcaller:" from operator import methodcaller print repr(sorted(source, key=methodcaller("get_property", "family-name"))) print "Source still unsorted after:" print repr(source) source.sort(key=lambda mo: mo.get_property("family-name")) print "Source now sorted:" print repr(source) yields the following: Unsorted source before: [Doug, Carol, Bill, Adam] Using a lambda: [Adam, Bill, Carol, Doug] Using methodcaller: [Adam, Bill, Carol, Doug] Source still unsorted after: [Doug, Carol, Bill, Adam] Source now sorted: [Adam, Bill, Carol, Doug] I'm partial to the lambda version over the methodcaller version unless there's a reason to dynamically get the method-name as a string. But that's just a personal preference. -tkc From jeanmichel at sequans.com Tue Dec 13 11:11:31 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 13 Dec 2011 17:11:31 +0100 Subject: logging issues In-Reply-To: <4EE77279.10104@gmail.com> References: <4EE77279.10104@gmail.com> Message-ID: <4EE77933.2060705@sequans.com> Andrea Crotti wrote: > I think is simple but I can't get it to work as I wish. > Suppose I have a big application, my idea is that the running > script sets a global logging level and then all the imported modules > would act consequently. > > In my codebase, however, unless I set the level for each of the loggers > I don't get the debug messages, not really sure why yet. > > To check if I understood, > logging.getLogger() returns the root logger, and every other logger > should inherit its settings, is that correct? Not really, if you use the classic way, your subloggers should be created an never configured. Their default behavior is to raise any log event to its parent. That way, all event logs end up being handled by the root logger. > > What happens if there is another logging.getLogger() somewhere else > in the code after the initialization? Does that become the root logger > overriding the old one? multiple calls to logging.getLogger() always return the very same root logger (the root one). Loggers are static objects handled by the loggin module, once a logger is created (identified by its name, the root logger's name is "") it will never be destroyed. Multiple call the getLogger(name) with the same name returns the same object. Logger names are of the form "grandparent.parent.child" > > The example below instead works as expected, so maybe is something > with my codebase... your codebase which is ??? [snip some working code] As a general advices - try to never configure a child logger, you should only need to create one whithout setting its level nor its handler. A logger is responsible of raising log events, not handling them. The root logger is supposed to handle them. - Configure only the root logger and do it in the main function, and only there. Configuring means calling basicConfig, setting the level, adding a handler etc... everything except the creation. JM From __peter__ at web.de Tue Dec 13 11:13:50 2011 From: __peter__ at web.de (Peter Otten) Date: Tue, 13 Dec 2011 17:13:50 +0100 Subject: Using methodcaller in a list sort - any examples anywhere? References: Message-ID: tinnews at isbd.co.uk wrote: > I want to sort a list of 'things' (they're fairly complex objects) by > the contents of one of the fields I can extract from the 'things' > using a Python function. > > So I have a list L which is a list of objects of some sort. I can > output the contents of a field in the list as follows:- > > for k in L: > print k.get_property('family-name') > > How can I sort the list first? As I said it seems like a methodcaller > is the answer but I don't see how. I want to sort the list of objects > not just produce a sorted list of names. The most obvious way is to use a custom function def get_family_name(obj): return obj.get_property("family-name") L.sort(key=get_family_name) However, since you already know about methodcaller """ class methodcaller(builtins.object) | methodcaller(name, ...) --> methodcaller object | | Return a callable object that calls the given method on its operand. | After, f = methodcaller('name'), the call f(r) returns r.name(). | After, g = methodcaller('name', 'date', foo=1), the call g(r) returns | r.name('date', foo=1). """" L.sort(key=methodcaller("get_property", "family-name")) From andrea.crotti.0 at gmail.com Tue Dec 13 11:21:15 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 16:21:15 +0000 Subject: logging issues In-Reply-To: <4EE77933.2060705@sequans.com> References: <4EE77279.10104@gmail.com> <4EE77933.2060705@sequans.com> Message-ID: <4EE77B7B.9090408@gmail.com> On 12/13/2011 04:11 PM, Jean-Michel Pichavant wrote: > Not really, if you use the classic way, your subloggers should be > created an never configured. Their default behavior is to raise any > log event to its parent. That way, all event logs end up being handled > by the root logger. > Ok thanks now it's more clear.your codebase which is ??? > > [snip some working code] > > As a general advices > - try to never configure a child logger, you should only need to > create one whithout setting its level nor its handler. A logger is > responsible of raising log events, not handling them. The root logger > is supposed to handle them. > - Configure only the root logger and do it in the main function, and > only there. Configuring means calling basicConfig, setting the level, > adding a handler etc... everything except the creation. > > JM It turned out that I was calling logger = logging.getLogger() In other modules too, and then apparently the logging level was modified automatically to the default one, is that possible? changing with logger = logging.getLogger(__name__) everywhere solved my problems.. From andrea.crotti.0 at gmail.com Tue Dec 13 11:24:05 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 16:24:05 +0000 Subject: boolean from a function Message-ID: <4EE77C25.1060600@gmail.com> I'm not sure for how long I had this bug, and I could not understand the problem. I had a function which would return a boolean def func_bool(): if x: return True else: return False Now somewhere else I had if func_bool: # do something I could not quite understand why it was always true, until I finally noticed that the () were missing. Is there some tool to avoid these stupid mistakes? (pylint doesn't warn me on that) I don't think I will ever (or almost) have to use a function as a boolean, instead of its return value... From ian.g.kelly at gmail.com Tue Dec 13 11:26:58 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Dec 2011 09:26:58 -0700 Subject: Signature-preserving decorators In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 6:36 AM, Henrik Faber wrote: > Hi group, > > when decorating a method in Python3, by use of the > functools.update_wrapper function, it can be achieved that the docstring > and name of the original function is preseverved. > > However, the prototype is lost: When looking into the Python help, I > have lots of entries that look like: > > getfoo(*args, **kwargs) -> int > > setbar(*args, **kwargs) > > As you can imagine, this is really not very self-explanatory. I've seen > a solution which constructs a wrapper's wrapper function using > inspection and eval -- this looks really dirty to me, however. Then > there's the "decorator" external module -- but I'd like to do it with > on-board tools. > > Is this possible in Python3 with too much of a hassle? The decorator module also uses inspection and eval to do it, by the way. Currently there is no pretty way to do it that I know of, but see PEP 362: http://www.python.org/dev/peps/pep-0362/ That PEP has unfortunately been in the "Open" state for quite a long time now, but it seems to me that a lot of people are starting to get interested in this issue, so maybe it will start to pick up some steam before too long. From neilc at norwich.edu Tue Dec 13 11:29:08 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 13 Dec 2011 16:29:08 GMT Subject: Getting the current directory path and the path where python script is saved References: Message-ID: <9kpcqkFcoU1@mid.individual.net> On 2011-12-13, gialloporpora wrote: > Dear all, > I would like to create a little script to rename my files, I think to > use argparse module to create it. > I would like to know how to obtain the path where script is executed and > the path where the file script is stored. > > For example, if I save my script in c:\python27 and I put the > directory in %path% I can execute it from any folder, I would > like to get the two directory path. Current working directory is available with os.getcwd() The location of the script you are running may be available as os.path.basename(sys.argv[0]). -- Neil Cerutti From bachth at uni-mainz.de Tue Dec 13 11:38:10 2011 From: bachth at uni-mainz.de (Thomas Bach) Date: Tue, 13 Dec 2011 17:38:10 +0100 Subject: Getting the current directory path and the path where python script is saved References: Message-ID: <87y5ug2zrh.fsf@jubold.box> gialloporpora writes: > I would like to know how to obtain the path where script is executed > and the path where the file script is stored. Does this help: In [16]: import os In [19]: os.getcwd() Out[19]: '/home/vince' In [21]: os.__file__ Out[21]: '/home/vince/src/pyramid_install/lib/python2.7/os.pyc' regards From tinnews at isbd.co.uk Tue Dec 13 11:48:51 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Tue, 13 Dec 2011 16:48:51 +0000 Subject: Using methodcaller in a list sort - any examples anywhere? References: Message-ID: Peter Otten <__peter__ at web.de> wrote: > tinnews at isbd.co.uk wrote: > > > I want to sort a list of 'things' (they're fairly complex objects) by > > the contents of one of the fields I can extract from the 'things' > > using a Python function. > > > > So I have a list L which is a list of objects of some sort. I can > > output the contents of a field in the list as follows:- > > > > for k in L: > > print k.get_property('family-name') > > > > How can I sort the list first? As I said it seems like a methodcaller > > is the answer but I don't see how. I want to sort the list of objects > > not just produce a sorted list of names. > > The most obvious way is to use a custom function > > def get_family_name(obj): > return obj.get_property("family-name") > L.sort(key=get_family_name) > > However, since you already know about methodcaller > > """ > class methodcaller(builtins.object) > | methodcaller(name, ...) --> methodcaller object > | > | Return a callable object that calls the given method on its operand. > | After, f = methodcaller('name'), the call f(r) returns r.name(). > | After, g = methodcaller('name', 'date', foo=1), the call g(r) returns > | r.name('date', foo=1). > """" > > L.sort(key=methodcaller("get_property", "family-name")) > OK, thanks, just what I wanted. -- Chris Green From sagysrael at gmail.com Tue Dec 13 11:50:11 2011 From: sagysrael at gmail.com (Sagy Drucker) Date: Tue, 13 Dec 2011 08:50:11 -0800 (PST) Subject: icmp and raw sockets in python Message-ID: hello i am relatively new to python, so please be considerate... i'm implementing a server and a client via raw_sockets. i have the necessary privileges. now, the server i defined so: host = socket.gethostbyname(socket.gethostname()) address = (host, 22224) sockSer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) sockSer.bind(address) sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) packet, addr = sockSer .recvfrom(4096) # wait for packet from client Q1) why can't i simply type: hosts = 'localhost'. if i do so, it doesn't allow me to write the line: sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON). only when doing gethostbyname(socket.gethostname()) i get 192.168.1.101 and then it works. in a different class: the client socket: host = socket.gethostbyname(socket.gethostname()) address = (host, 22224) sockCli = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) Q2) do i also need to type: sockCli.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) or maybe sockCli.connect(address) for the client socket? now, the problems arise when i do the following: 1) send a packet from client to socket: header=... payload='a' sockCli.sendto(header + payload, address) 2) receive packet in server and send file to client: while(true): data, addr = sockSer.recvfrom(4096) header2=... payload2='b' sockSer.sendto(header2 + payload2, addr) now, my important question is: Q3) the server sent only 1 packet to client, with payload 'b'. what happens is, my client actually receives 2 packets in the while loop: first packet is what the client itself sent to server, and the other packet is from the client got from the server. hence my output is 'ab' instead of simply 'b' why is this happening??? NOTE: i didn't type the entire code, but i think my syntax,parsing,header composition etc.. are correct. is there an obvious problem in my code? if necessary i'll upload the entire code. thanks From andrea.crotti.0 at gmail.com Tue Dec 13 12:03:01 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 17:03:01 +0000 Subject: logging issues In-Reply-To: <4EE77B7B.9090408@gmail.com> References: <4EE77279.10104@gmail.com> <4EE77933.2060705@sequans.com> <4EE77B7B.9090408@gmail.com> Message-ID: <4EE78545.8010309@gmail.com> And by the way suppose I have a script which takes as input the log level and a list of possible filters. In another file I have a couple of functions as below which are called in that order. Is that supposed to work? In theory I'm getting both times the same logger. The set_verbosity seems to do its job, the second is also called and the filters are added but I don't see the output formatted as it should.. Maybe I can only set it up once? def set_verbosity(log_level): """Set the desired log level """ root_logger = logging.getLogger() root_logger.setLevel(LOG_LEVELS[log_level]) def setup_logging(to_filter): root_logger = logging.getLogger() hdlr = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) # a list of regular expressions to filter out? if to_filter: for f in to_filter: logger.debug("adding filter for name %s" % f) filt = logging.Filter(f) hdlr.addFilter(filt) root_logger.addHandler(hdlr) From invalid at invalid.invalid Tue Dec 13 12:20:35 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 13 Dec 2011 17:20:35 +0000 (UTC) Subject: boolean from a function References: Message-ID: On 2011-12-13, Andrea Crotti wrote: > Now somewhere else I had > > if func_bool: > # do something > > I could not quite understand why it was always true, until I finally > noticed that the () were missing. Is there some tool to avoid these > stupid mistakes? (pylint doesn't warn me on that) I don't think I > will ever (or almost) have to use a function as a boolean, instead of > its return value... FWIW, I have do use the truth value of a function (rather than it's return value) when writing code that uses callbacks: def foo(callback=None): # do some stuff if callback: callback() # do some more stuff It probably would be better to use "if callback is not None:", but I find I usually skip "is not None". -- Grant Edwards grant.b.edwards Yow! Will it improve my at CASH FLOW? gmail.com From prakash.stack at gmail.com Tue Dec 13 12:22:54 2011 From: prakash.stack at gmail.com (prakash jp) Date: Tue, 13 Dec 2011 22:52:54 +0530 Subject: text to html Message-ID: Hi All, Want to publish a log file as a web page, is there a parser to retain the format of the text as is and then convert to html. Please provide the relevant pointers Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From pedro.h.souto at gmail.com Tue Dec 13 12:30:40 2011 From: pedro.h.souto at gmail.com (Pedro Henrique Guedes Souto) Date: Tue, 13 Dec 2011 15:30:40 -0200 Subject: text to html In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 3:22 PM, prakash jp wrote: > > Hi All, > > Want to publish a log file as a web page, is there a parser to retain the format of the text as is and then convert to html. Please provide the relevant pointers Hey, You can use this:?http://txt2tags.org/ Att; Pedro Henrique Guedes Souto [pedro.h.souto at gmail.com] From ethan at stoneleaf.us Tue Dec 13 12:42:05 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 13 Dec 2011 09:42:05 -0800 Subject: boolean from a function In-Reply-To: <4EE77C25.1060600@gmail.com> References: <4EE77C25.1060600@gmail.com> Message-ID: <4EE78E6D.6060505@stoneleaf.us> Andrea Crotti wrote: > I'm not sure for how long I had this bug, and I could not understand the > problem. > > I had a function which would return a boolean > > def func_bool(): > if x: > return True > else: return False > > Now somewhere else I had > > if func_bool: > # do something > > I could not quite understand why it was always true, until I finally > noticed that the () were missing. > Is there some tool to avoid these stupid mistakes? (pylint doesn't warn > me on that) > I don't think I will ever (or almost) have to use a function as a > boolean, instead of its return value... Heh, I do believe I've been bitten by that a couple times. The only defense I'm aware of is good unit tests. ~Ethan~ From duncan.booth at invalid.invalid Tue Dec 13 12:42:51 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 13 Dec 2011 17:42:51 GMT Subject: boolean from a function References: Message-ID: Andrea Crotti wrote: > I'm not sure for how long I had this bug, and I could not understand > the problem. > > I had a function which would return a boolean > > def func_bool(): > if x: > return True > else: return False > > Now somewhere else I had > > if func_bool: > # do something > > I could not quite understand why it was always true, until I finally > noticed that the () were missing. > Is there some tool to avoid these stupid mistakes? (pylint doesn't > warn me on that) > I don't think I will ever (or almost) have to use a function as a > boolean, instead of its return value... For this particular example why don't you just write 'if x: # do something'? More generally rather than having a global function make it a property on an object. class Snark: def __init__(self, something): self.boojum = ... @property def is_a_bookum(self): return self.boojum hunted = Snark(whatever) Then you can write: if hunted.is_a_boojum: self.vanish_away() -- Duncan Booth http://kupuguy.blogspot.com From invalid at invalid.invalid Tue Dec 13 13:14:06 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 13 Dec 2011 18:14:06 +0000 (UTC) Subject: text to html References: Message-ID: On 2011-12-13, Pedro Henrique Guedes Souto wrote: > On Tue, Dec 13, 2011 at 3:22 PM, prakash jp wrote: > >> Want to publish a log file as a web page, is there a parser to retain >> the format of the text as is and then convert to html. Please provide >> the relevant pointers > > > Hey, You can use this:?http://txt2tags.org/ I think thats a bit too likely to interpret stuff in the log messages as formatting directives. Two options come to mind: 1. Don't use HTML. Return the logfile as-is with Content-Type: text/plain. [That way the user can also save it if needed.] 2. Escape the file contents using cgi.escape() and then stick the escaped contents inside
 
tags. That's not as nice, since saving it is no longer a useful option. -- Grant Edwards grant.b.edwards Yow! Now KEN and BARBIE at are PERMANENTLY ADDICTED to gmail.com MIND-ALTERING DRUGS ... From ian.g.kelly at gmail.com Tue Dec 13 13:15:21 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Dec 2011 11:15:21 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <2ff41b2e-90e3-4fae-9951-67bb257ed6ed@m10g2000vbc.googlegroups.com> References: <4EE5C576.2000307@gmail.com> <2ff41b2e-90e3-4fae-9951-67bb257ed6ed@m10g2000vbc.googlegroups.com> Message-ID: On Tue, Dec 13, 2011 at 1:44 AM, Eelco wrote: > 'for i in llist' is not quite going to fly is it? Thats probably the > reason noone ever uses that construct; its not a proper sequence type. Not really a problem, because fortunately Python makes it super-easy to create custom iterators. def listiter(llist): while llist: head, llist = llist yield head And you're done. If you like, you could also wrap this up in a class with all the sequence-related magic methods you want, although you lose the simplicity of the literal syntax by doing so. If this is rarely used, it is more likely because custom containers are going to be less efficient than built-ins, but if you want to do functional programming and are not overly concerned with the speed of iteration, this is not a bad way to do it. > Good point. Copy-on-write semantics could be used, but really one > should have several linked list types reflecting the underlying > implementations. I would like to have an immutable singly linked list > builtin of the standard functional type, which you can only unpack > from one end and renders these issues moot, plus a builtin doubly > linked list with copy-on-write or copy-on-unpacking semantics. Copy-on-write could be implemented with any type. You don't need a doubly linked list for that. > We are not talking black magic here; we are talking about an EXPLICIT > type constraint provided on the very same line. An explicit type constraint with very different semantics depending on what particular type you specify and what particular type you're unpacking from, as I had understood it before. Now you seem to be saying that it would always be a copy, but sharing state with copy-on-write possible, which is a different situation. > Well perhaps, but not always knowing the type of your objects at write- > time is inherent to weakly typed languages; this happens all the time. > Not knowing the type of the sequence to be unpacked is in a sense an > asset; I can use this construct in a function, and unpack any sequence > type in a manner appropriate for it. About the result of the unpacking > I will know just as much as about the input to it; that they are the > same type. Just because the issue is inherent doesn't mean we should contribute to it. Knowing that an object is an arbitrary sequence is fine if all you want to do is iterate and index it. If you want to do anything else, then it's important to know the type. The copy-on-write suggestion does make the type-matching approach a bit more attractive. On the other hand, it's also more fragile (what if the type being unpacked can't be constructed from an iterable? For example, a database cursor), so that approach potentially needs additional error-handling. Anyway, the more I think about it, that concern is really more of an issue for straight copying. One of my pet peeves is that I prefer list(x) for copying sequences rather than the more common x[::]. The latter is fine if all I need is an immutable sequence of uncertain type to iterate and index over -- but then why did I need to make a copy? Unpacking implies different use cases, though, and maybe a good argument can be made for it to match type. From storchaka at gmail.com Tue Dec 13 13:57:40 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 13 Dec 2011 20:57:40 +0200 Subject: Fixing the XML batteries In-Reply-To: References: <4EE7425A.2090006@gmail.com> Message-ID: 13.12.11 16:59, Stefan Behnel ???????(??): > It matches my opinion though. I would be glad to divide your intentions, however ElementTree looks less documented than minidom, and is not full replacement. For example, I haven't found how to get XML encoding. Also, at use of ElementTree instead of minidom the suffix "ns0:" is added to each element. I do not see how to _create_ a new element and to write it with header. And DOM interface is more habitual for those who works with some other languages. > Yes, that's what C14N is there for, typically used for cryptography, > hashing, etc. However, MiniDOM doesn't implement that standard, so > you're on your own here. MiniDOM quite suited me earlier in this respect. I will pass to C14N as soon as I will be can. > The ET module is actually quite short (<1700 lines), so you can just > copy the Py2.7 version into your sources and optionally import it on > older Python releases. Since you only seem to depend on the serialiser > (which is worth using anyway because it is much faster in the Py2.7 > version), older platform versions of cET should also work just fine with > that module copy, so you can basically just import everything from > xml.etree.cElementTree and use the ElementTree class and the tostring() > function from your own local version if the platform version is too old. > > Note that ET is also still available as a separately installable > package, may or may not be simpler to use for you. I thank, it is too bulky for my small scripts (which I have decided to update from Python 2.3 or 2.4 to modern Python 3 and 2.6+). I will better postpone high-grade migration for half-year or year while the Python 2.7 and 3.2 won't appear in stable versions of popular distributives. I thank you for ET, it really is more convenient at some applications (especially at work with the text in elements). From joshua.landau.ws at gmail.com Tue Dec 13 14:01:42 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 13 Dec 2011 19:01:42 +0000 Subject: Overriding a global In-Reply-To: <4EE75382.9060104@sequans.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> Message-ID: On 13 December 2011 13:30, Jean-Michel Pichavant wrote: > > writing > > x = 1 > > def spam(): > x = 2 > > is in general a bad idea. That was my point. Why? I have a few (probably wrong) guesses. Because you expect it to be the same every time you use it? Well, then this should be "in general a bad idea": x = 1; print(x); x = 2; print(x) Even though it makes total sense to me. Is it because it's used to different purpose between similarly-looking functions? This looks fine, though: def func1(): x=1; print(x) def func2(): x=2; print(x) Is it because it looks like a reassignment of the more global x? I don't have an example here but, simply put, I don't believe this. We can use "id" as our own local variable without thinking that we're tampering with "__builtins__.id". I don't see it as much of a leap from builtin to global (except that you **can** do "dir = 1; del dir; dir" without error). That said, I'm sorta' just guessing the reason you might think it's a bad idea. -------------- next part -------------- An HTML attachment was scrubbed... URL: From cs at zip.com.au Tue Dec 13 14:11:27 2011 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 14 Dec 2011 06:11:27 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: Message-ID: <20111213191126.GA1363@cskk.homeip.net> On 13Dec2011 00:30, Eelco wrote: | On Dec 13, 1:27?am, alex23 wrote: | > On Dec 13, 3:12?am, Eelco wrote: | > > But to relate it to the topic of this thread: no, the syntax does not | > > allow one to select the type of the resulting sequence. It always | > > constructs a list. | > | > So by this argument, _every_ function that returns a list should take | > an optional argument to specify an alternative form of sequence. | > | > What, exactly, is so onerous about coercing your list to _whatever_ | > type you want? You know, like everybody else has been. | > | > What does this _gain_ you other than one less line of code? | | 1) Turning two lines of code into a single more readable one is | nothing to scoff at | 2) After-the-fact conversion is O(n), getting the view you want right | away is O(1) Regarding (2), it has already cost you O(n) to get there. So your O(1) is a little ingenuous. -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ I'm a volunteer EMT-I on our local ambulance service; one of our Paramedics calls squid style motorcycle accidents "ZoomSplats", as in "we had a good ZoomSplat the other night". ;-) - Scott From ian.g.kelly at gmail.com Tue Dec 13 14:34:41 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Dec 2011 12:34:41 -0700 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau wrote: >> No, there is another difference, the reason for rebinding the name. >> In a subclass, you would rebind a class attribute because that >> particular attribute, which you need to change, is used and expected >> by external code, either in the base class or in code that uses its >> API (or both). ?Local variables in functions, on the other hand, are >> not externally visible, so there is no need to do this in order to >> conform to the expectations of external code. ?All it does in that >> case is to sow potential confusion. >> > So you're saying you should never extend methods or attributes that > aren't meant to be used as part of of the API? Because I can claim > guilty on this point. No, I'm only saying that replacing attributes in subclasses is accepted because it is necessary due to external dependencies, and that local variables in functions don't have that excuse. From rurpy at yahoo.com Tue Dec 13 14:37:40 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 13 Dec 2011 11:37:40 -0800 (PST) Subject: Fixing the XML batteries References: <4EE7425A.2090006@gmail.com> Message-ID: <3defcdde-f93a-48ec-9fcc-129d684d711b@o9g2000yqa.googlegroups.com> On Dec 13, 5:32?am, Stefan Behnel wrote: ... > In Python 2.7/3.2, ElementTree has support for C14N serialisation, just > pass the option method="c14n". Where does one find information in the Python documentation about this? From stefan_ml at behnel.de Tue Dec 13 14:38:54 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 13 Dec 2011 20:38:54 +0100 Subject: Fixing the XML batteries In-Reply-To: References: <4EE7425A.2090006@gmail.com> Message-ID: Serhiy Storchaka, 13.12.2011 19:57: > 13.12.11 16:59, Stefan Behnel ???????(??): >> It matches my opinion though. > > I would be glad to divide your intentions, however ElementTree looks less > documented than minidom It's certainly a lot smaller, which makes its API easier to learn and remember. > and is not full replacement. It's good enough for a surprisingly large part of all XML processing needs, and if you need more, there's lxml for that. > For example, I haven't found how to get XML encoding. True - lxml provides it, but plain ET doesn't. However, I can't think of any major use cases where you'd care about the encoding of the original input file. Just use what suites your needs on the way back out. UTF-8 will usually do just fine. > Also, at use of ElementTree instead > of minidom the suffix "ns0:" is added to each element. That's a "prefix", not a suffix. And since prefixes are basically useless for XML processing, it isn't commonly a problem whether they are called 'nsXY' or 'abcdefg'. It's the parser's duty to handle them for you. > I do not see how to _create_ a new element element = Element('tagname') > and to write it with header. That's called a "declaration". You can get it with, e.g., ElementTree(element).write(encoding='utf8') By default, ET doesn't write it unless it can put useful information into it. (Note that the XML spec makes the declaration optional for XML 1.0 serialisation as UTF-8.) > And DOM interface is more habitual for those who works with some other > languages. Not really. DOM is also considered unwieldy in many other languages. Even in a language as unwieldy as Java it's being frowned upon these days. In JavaScript, it has basically been replaced by jQuery, and many other languages also have substantially more "natural" ways to deal with XML than the DOM. It's true, though, that ElementTree isn't a widely known interface outside of the Python world. >> Yes, that's what C14N is there for, typically used for cryptography, >> hashing, etc. However, MiniDOM doesn't implement that standard, so >> you're on your own here. > > MiniDOM quite suited me earlier in this respect. I will pass to C14N as > soon as I will be can. > >> The ET module is actually quite short (<1700 lines), so you can just >> copy the Py2.7 version into your sources and optionally import it on >> older Python releases. Since you only seem to depend on the serialiser >> (which is worth using anyway because it is much faster in the Py2.7 >> version), older platform versions of cET should also work just fine with >> that module copy, so you can basically just import everything from >> xml.etree.cElementTree and use the ElementTree class and the tostring() >> function from your own local version if the platform version is too old. >> >> Note that ET is also still available as a separately installable >> package, may or may not be simpler to use for you. > > I thank, it is too bulky for my small scripts (which I have decided to > update from Python 2.3 or 2.4 to modern Python 3 and 2.6+). I will better > postpone high-grade migration for half-year or year while the Python 2.7 > and 3.2 won't appear in stable versions of popular distributives. In case you are only dealing with small in-house scripts, I'd suggest installing ET 1.3 (or, even better, lxml) on the machines where you want to use it. Then you no longer have to care about those dependencies. > I thank you for ET, it really is more convenient at some applications > (especially at work with the text in elements). Careful. ;) I'm just the author of lxml, not of ET. That would be Fredrik Lundh. Stefan From joshua.landau.ws at gmail.com Tue Dec 13 14:43:54 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 13 Dec 2011 19:43:54 +0000 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On 13 December 2011 19:34, Ian Kelly wrote: > On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau > wrote: > >> No, there is another difference, the reason for rebinding the name. > >> In a subclass, you would rebind a class attribute because that > >> particular attribute, which you need to change, is used and expected > >> by external code, either in the base class or in code that uses its > >> API (or both). Local variables in functions, on the other hand, are > >> not externally visible, so there is no need to do this in order to > >> conform to the expectations of external code. All it does in that > >> case is to sow potential confusion. > >> > > So you're saying you should never extend methods or attributes that > > aren't meant to be used as part of of the API? Because I can claim > > guilty on this point. > > No, I'm only saying that replacing attributes in subclasses is > accepted because it is necessary due to external dependencies, and > that local variables in functions don't have that excuse. > But they aren't needed due to external dependencies if they're implementation-specific and not part of the API, no? -------------- next part -------------- An HTML attachment was scrubbed... URL: From richard.dijkstra at planet.nl Tue Dec 13 14:46:55 2011 From: richard.dijkstra at planet.nl (Richard) Date: Tue, 13 Dec 2011 11:46:55 -0800 (PST) Subject: pyside dynamic buttons Message-ID: <57fb3cd8-fae3-4818-a76d-ccd8ebd27c5b@c13g2000vbh.googlegroups.com> Hi, We are making a screen with data driven buttons. I can add buttons but can't redraw the buttons for new values. The buttuns are placed in a frame. I draw the buttons on the screen in a for loop: def drawS1Butons(self): n=0 for self.S1 in self.Org.getVpuChilds(self.Focus): self.S1.button = QtGui.QPushButton(self.frame_2) # Add method to the button class !?!? self.S1.button.setGeometry(QtCore.QRect(80, n*30, 161, 23)) self.S1.button.setObjectName(self.S1.name) self.S1.button.setText(QtGui.QApplication.translate("MainWindow", self.S1.name, None, QtGui.QApplication.UnicodeUTF8)) n=n+1 How can I delete buttons? Thanks, Richard From rurpy at yahoo.com Tue Dec 13 14:50:41 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 13 Dec 2011 11:50:41 -0800 (PST) Subject: Fixing the XML batteries References: <4EE7425A.2090006@gmail.com> Message-ID: <3306306d-c701-40ed-b6f4-847773857db5@c18g2000yqj.googlegroups.com> On Dec 13, 5:32?am, Stefan Behnel wrote: ... > In Python 2.7/3.2, ElementTree has support for C14N serialisation, just > pass the option method="c14n". Where in the Python docs can one find information about this? [previous post disappeared, sorry if I double posted or replied to author inadvertently.] From ian.g.kelly at gmail.com Tue Dec 13 14:54:09 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Dec 2011 12:54:09 -0700 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On Tue, Dec 13, 2011 at 12:43 PM, Joshua Landau wrote: > On 13 December 2011 19:34, Ian Kelly wrote: >> >> On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau >> wrote: >> >> No, there is another difference, the reason for rebinding the name. >> >> In a subclass, you would rebind a class attribute because that >> >> particular attribute, which you need to change, is used and expected >> >> by external code, either in the base class or in code that uses its >> >> API (or both). ?Local variables in functions, on the other hand, are >> >> not externally visible, so there is no need to do this in order to >> >> conform to the expectations of external code. ?All it does in that >> >> case is to sow potential confusion. >> >> >> > So you're saying you should never extend methods or attributes that >> > aren't meant to be used as part of of the API? Because I can claim >> > guilty on this point. >> >> No, I'm only saying that replacing attributes in subclasses is >> accepted because it is necessary due to external dependencies, and >> that local variables in functions don't have that excuse. > > > But they aren't needed due to external dependencies if they're > implementation-specific and not part of the API, no? By "external dependencies" I mean anything that's not specifically part of the subclass. This includes the base class. If they're not part of the API, then the base class presumably uses them for something, and by replacing them, you change the behavior of that base functionality. That's an external dependency. From hoogendoorn.eelco at gmail.com Tue Dec 13 15:14:15 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 12:14:15 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: Message-ID: <0c87865e-d8a2-41b6-91b4-09278ee2ffcf@cs7g2000vbb.googlegroups.com> On Dec 13, 8:11?pm, Cameron Simpson wrote: > On 13Dec2011 00:30, Eelco wrote: > | On Dec 13, 1:27?am, alex23 wrote: > | > On Dec 13, 3:12?am, Eelco wrote: > | > > But to relate it to the topic of this thread: no, the syntax does not > | > > allow one to select the type of the resulting sequence. It always > | > > constructs a list. > | > > | > So by this argument, _every_ function that returns a list should take > | > an optional argument to specify an alternative form of sequence. > | > > | > What, exactly, is so onerous about coercing your list to _whatever_ > | > type you want? You know, like everybody else has been. > | > > | > What does this _gain_ you other than one less line of code? > | > | 1) Turning two lines of code into a single more readable one is > | nothing to scoff at > | 2) After-the-fact conversion is O(n), getting the view you want right > | away is O(1) > > Regarding (2), it has already cost you O(n) to get there. So your O(1) > is a little ingenuous. Well, yes, but if one takes a given sequence as input (at least O(n) complexity to obtain it in the first place, indeed), and then wants to, say, recursively unwind it, the cost of the total operation is O(n) versus O(n^2) And besides, O(n) < 2*O(n); perhaps of lesser concern than different orders, but still. From stefan_ml at behnel.de Tue Dec 13 15:21:46 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 13 Dec 2011 21:21:46 +0100 Subject: Fixing the XML batteries In-Reply-To: <3defcdde-f93a-48ec-9fcc-129d684d711b@o9g2000yqa.googlegroups.com> References: <4EE7425A.2090006@gmail.com> <3defcdde-f93a-48ec-9fcc-129d684d711b@o9g2000yqa.googlegroups.com> Message-ID: rurpy at yahoo.com, 13.12.2011 20:37: > On Dec 13, 5:32 am, Stefan Behnel wrote: >> In Python 2.7/3.2, ElementTree has support for C14N serialisation, just >> pass the option method="c14n". > > Where does one find information in the Python documentation about > this? Hmm, interesting. I though it had, but now when I click on the stdlib doc link to read the module source (hint, hint), I can see that it only has the hooks. The C14N support module of ET 1.3 was not integrated into the stdlib. Sorry for not verifying this earlier. So you actually need the external package for C14N support. See here: http://effbot.org/zone/elementtree-13-intro.htm http://hg.effbot.org/et-2009-provolone/src/tip/elementtree/elementtree/ElementC14N.py Just to emphasize this once again: it's not more than a single module that you can copy into your own code as a fallback import, or deploy in your local installations. Stefan From joshua.landau.ws at gmail.com Tue Dec 13 15:24:29 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 13 Dec 2011 20:24:29 +0000 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On 13 December 2011 19:54, Ian Kelly wrote: > On Tue, Dec 13, 2011 at 12:43 PM, Joshua Landau > wrote: > > On 13 December 2011 19:34, Ian Kelly wrote: > >> > >> On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau > >> wrote: > >> >> No, there is another difference, the reason for rebinding the name. > >> >> In a subclass, you would rebind a class attribute because that > >> >> particular attribute, which you need to change, is used and expected > >> >> by external code, either in the base class or in code that uses its > >> >> API (or both). Local variables in functions, on the other hand, are > >> >> not externally visible, so there is no need to do this in order to > >> >> conform to the expectations of external code. All it does in that > >> >> case is to sow potential confusion. > >> >> > >> > So you're saying you should never extend methods or attributes that > >> > aren't meant to be used as part of of the API? Because I can claim > >> > guilty on this point. > >> > >> No, I'm only saying that replacing attributes in subclasses is > >> accepted because it is necessary due to external dependencies, and > >> that local variables in functions don't have that excuse. > > > > > > But they aren't needed due to external dependencies if they're > > implementation-specific and not part of the API, no? > > By "external dependencies" I mean anything that's not specifically > part of the subclass. This includes the base class. If they're not > part of the API, then the base class presumably uses them for > something, and by replacing them, you change the behavior of that base > functionality. That's an external dependency. > Touch?. On this point, I yield. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Tue Dec 13 15:32:43 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 12:32:43 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> <2ff41b2e-90e3-4fae-9951-67bb257ed6ed@m10g2000vbc.googlegroups.com> Message-ID: <26b97534-1624-4afb-8869-f04afc800e22@4g2000yqu.googlegroups.com> On Dec 13, 7:15?pm, Ian Kelly wrote: > On Tue, Dec 13, 2011 at 1:44 AM, Eelco wrote: > > 'for i in llist' is not quite going to fly is it? Thats probably the > > reason noone ever uses that construct; its not a proper sequence type. > > Not really a problem, because fortunately Python makes it super-easy > to create custom iterators. > > def listiter(llist): > ? ? while llist: > ? ? ? ? head, llist = llist > ? ? ? ? yield head > > And you're done. ?If you like, you could also wrap this up in a class > with all the sequence-related magic methods you want, although you > lose the simplicity of the literal syntax by doing so. ?If this is > rarely used, it is more likely because custom containers are going to > be less efficient than built-ins, but if you want to do functional > programming and are not overly concerned with the speed of iteration, > this is not a bad way to do it. Fair enough. Still, I dont readily see myself using the construct, and I do feel myself longing for something like that to be included as a builtin collection type. Im not sure why python is so stingy with the collections it provides. > > Good point. Copy-on-write semantics could be used, but really one > > should have several linked list types reflecting the underlying > > implementations. I would like to have an immutable singly linked list > > builtin of the standard functional type, which you can only unpack > > from one end and renders these issues moot, plus a builtin doubly > > linked list with copy-on-write or copy-on-unpacking semantics. > > Copy-on-write could be implemented with any type. ?You don't need a > doubly linked list for that. True > > We are not talking black magic here; we are talking about an EXPLICIT > > type constraint provided on the very same line. > > An explicit type constraint with very different semantics depending on > what particular type you specify and what particular type you're > unpacking from, as I had understood it before. ?Now you seem to be > saying that it would always be a copy, but sharing state with > copy-on-write possible, which is a different situation. Yes, I think consistent semantics over all sequence types is very important. Although, returning a view for immutable collections like tuples and 'functional lists' (for lack of a better term), and always returning a copy for mutable container types (lists and doubly-linked- lists / deques) is not so bad either, imo. Just one extra simple rule that is clear and obvious enough. > > Well perhaps, but not always knowing the type of your objects at write- > > time is inherent to weakly typed languages; this happens all the time. > > Not knowing the type of the sequence to be unpacked is in a sense an > > asset; I can use this construct in a function, and unpack any sequence > > type in a manner appropriate for it. About the result of the unpacking > > I will know just as much as about the input to it; that they are the > > same type. > > Just because the issue is inherent doesn't mean we should contribute > to it. How are we contributing to it? If we dont know the type of the RHS, we dont know the type of the LHS either, but its not like we lost any information. If we do know the type of the RHS, then we do know the type of the LHS as well; its conserved. > Anyway, the more I think about it, that concern is really more of an > issue for straight copying. ?One of my pet peeves is that I prefer > list(x) for copying sequences rather than the more common x[::]. ?The > latter is fine if all I need is an immutable sequence of uncertain > type to iterate and index over -- but then why did I need to make a > copy? ?Unpacking implies different use cases, though, and maybe a good > argument can be made for it to match type. Thanks for the constructive feedback; something to think about. From rurpy at yahoo.com Tue Dec 13 16:02:18 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 13 Dec 2011 13:02:18 -0800 (PST) Subject: Fixing the XML batteries References: <4EE7425A.2090006@gmail.com> <3defcdde-f93a-48ec-9fcc-129d684d711b@o9g2000yqa.googlegroups.com> Message-ID: <0b1c9504-7b2c-4c13-8ffc-a91edcfa4a93@f39g2000yqn.googlegroups.com> On Dec 13, 1:21?pm, Stefan Behnel wrote: > ru... at yahoo.com, 13.12.2011 20:37: > > > On Dec 13, 5:32 am, Stefan Behnel wrote: > >> In Python 2.7/3.2, ElementTree has support for C14N serialisation, just > >> pass the option method="c14n". > > > Where does one find information in the Python documentation about > > this? > > Hmm, interesting. I though it had, but now when I click on the stdlib doc > link to read the module source (hint, hint), I realize the source is available (having had to use it way too many time in the past, not just with ET), but that does not justify omission from the docs. However the point is moot since (as you say) it seems the Python-distributed ET doesn't contain the c14n feature. > I can see that it only has the > hooks. The C14N support module of ET 1.3 was not integrated into the > stdlib. Sorry for not verifying this earlier. > > So you actually need the external package for C14N support. See here: > > http://effbot.org/zone/elementtree-13-intro.htm > > http://hg.effbot.org/et-2009-provolone/src/tip/elementtree/elementtre... > > Just to emphasize this once again: it's not more than a single module that > you can copy into your own code as a fallback import, or deploy in your > local installations. Right, but many times I try to avoid external dependencies when feasible. Thanks for the clarifications. From gialloporpora at gmail.com Tue Dec 13 16:39:44 2011 From: gialloporpora at gmail.com (gialloporpora) Date: Tue, 13 Dec 2011 22:39:44 +0100 Subject: Getting the current directory path and the path where python script is saved In-Reply-To: <9kpcqkFcoU1@mid.individual.net> References: <9kpcqkFcoU1@mid.individual.net> Message-ID: Risposta al messaggio di Neil Cerutti : > Current working directory is available with os.getcwd() > > The location of the script you are running may be available as > os.path.basename(sys.argv[0]). Thank you very much, Neil, is exactly what I wanted. Sandro -- *gialloporpora: RT @themitcho: telnet miku.acm.uiuc.edu Do it now. You will not regret it. #nyan /via @beaulebens * - http://bit.ly/u2boVC Sto ascoltando: *Pink Floyd ? Echoe Part I * - http://bit.ly/vgIGQe From tjreedy at udel.edu Tue Dec 13 16:57:31 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 13 Dec 2011 16:57:31 -0500 Subject: change a file with a context manager In-Reply-To: <4EE75F6D.5070604@gmail.com> References: <4EE72FF4.8010203@gmail.com> <4EE75F6D.5070604@gmail.com> Message-ID: On 12/13/2011 9:21 AM, Andrea Crotti wrote: > Another possible option is to just add to the path everything contained > in that > correct easy_install.pth file. > Or is there a way to reload the path settings? If you are talking about sys.path, you can certainly copy, mutate or replace, and restore sys.path. The details depend on whether you mutate or replace the original. -- Terry Jan Reedy From ethan at stoneleaf.us Tue Dec 13 18:21:12 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 13 Dec 2011 15:21:12 -0800 Subject: xml, minidom, ElementTree In-Reply-To: <4EE528BD.2040102@v.loewis.de> References: <4EE1C9AB.2040301@v.loewis.de> <4EE528BD.2040102@v.loewis.de> Message-ID: <4EE7DDE8.2050706@stoneleaf.us> In the near future I will need to parse and rewrite parts of a xml files created by a third-party program (PrintShopMail, for the curious). It contains both binary and textual data. There has been some strong debate about the merits of minidom vs ElementTree. Recommendations? ~Ethan~ From martin.hellwig at gmail.com Tue Dec 13 18:21:30 2011 From: martin.hellwig at gmail.com (Martin P. Hellwig) Date: Tue, 13 Dec 2011 23:21:30 +0000 Subject: icmp and raw sockets in python In-Reply-To: References: Message-ID: On 13/12/2011 16:50, Sagy Drucker wrote: > hello Hi > i am relatively new to python, so please be considerate... As I am only responding to one of your questions, perhaps it would be best if you don't get any other more helpful replies to split your questions up and post them separately. > > i'm implementing a server and a client via raw_sockets. > i have the necessary privileges. > > now, the server i defined so: > host = socket.gethostbyname(socket.gethostname()) > address = (host, 22224) > sockSer = socket.socket(socket.AF_INET, socket.SOCK_RAW, > socket.IPPROTO_ICMP) > sockSer.bind(address) > sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) > packet, addr = sockSer .recvfrom(4096) # wait for packet from client > > Q1) why can't i simply type: hosts = 'localhost'. > if i do so, it doesn't allow me to write the line: > sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON). > only when doing gethostbyname(socket.gethostname()) i get > 192.168.1.101 > and then it works. > Well localhost should resolve to 127.0.0.1/8 which is attached to the loopback interface, my gut feeling is that this interface has a particular set of restrictions which you are encountering. Sorry I can't be more helpful. -- mph From steve+comp.lang.python at pearwood.info Tue Dec 13 18:37:08 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 23:37:08 GMT Subject: boolean from a function References: Message-ID: <4ee7e1a4$0$29979$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Dec 2011 16:24:05 +0000, Andrea Crotti wrote: > I'm not sure for how long I had this bug, and I could not understand the > problem. > > I had a function which would return a boolean > > def func_bool(): > if x: > return True > else: return False x is a global? Poor design. But in any case, instead of an explicit if...else block, the canonical way to convert an arbitrary object to True/ False is with bool: def func_bool(): return bool(x) But you don't need it. See below. > Now somewhere else I had > > if func_bool: > # do something That would be better written as: if x: ... since func_bool always refers to x, it is just a needless level of indirection that doesn't buy you anything. > I could not quite understand why it was always true, until I finally > noticed that the () were missing. > Is there some tool to avoid these stupid mistakes? (pylint doesn't warn > me on that) Can't help you with that. Have you tried pychecker? I can't help you with that either :) -- Steven From vincent.vandevyvre at swing.be Tue Dec 13 21:37:35 2011 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Wed, 14 Dec 2011 03:37:35 +0100 Subject: pyside dynamic buttons In-Reply-To: <57fb3cd8-fae3-4818-a76d-ccd8ebd27c5b@c13g2000vbh.googlegroups.com> References: <57fb3cd8-fae3-4818-a76d-ccd8ebd27c5b@c13g2000vbh.googlegroups.com> Message-ID: <4EE80BEF.8070201@swing.be> An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Tue Dec 13 22:18:42 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 03:18:42 GMT Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: Message-ID: <4ee81592$0$11091$c3e8da3@news.astraweb.com> On Mon, 12 Dec 2011 09:29:11 -0800, Eelco wrote: [quoting Jussi Piitulainen ] >> They recognize modular arithmetic but for some reason insist that there >> is no such _binary operation_. But as I said, I don't understand their >> concern. (Except the related concern about some programming languages, >> not Python, where the remainder does not behave well with respect to >> division.) I've never come across this, and frankly I find it implausible that *actual* mathematicians would say that. Likely you are misunderstanding a technical argument about remainder being a relation rather than a bijunction. The argument would go something like this: "Remainder is not uniquely defined. For example, the division of -42 by -5 can be written as either: 9*-5 + 3 = -42 8*-5 + -2 = -42 so the remainder is either 3 or -2. Hence remainder is not a bijection (1:1 function)." The existence of two potential answers for the remainder is certainly correct, but the conclusion that remainder is not a binary operation doesn't follow. It is a binary relation. Mathematicians are well able to deal with little inconveniences like this, e.g. consider the square root: 10**2 = 100 (-10)**2 = 100 therefore the square root of 100 is ?10 Mathematicians get around this by defining the square root operator ? as *only* the principle value of the square root relation, that is, the positive root. Hence: ?100 = 10 only If you want both roots, you have to explicitly ask for them both: ??100 Similarly, we can sensibly define the remainder or modulus operator to consistently return a non-negative remainder, or to do what Python does, which is to return a remainder with the same sign as the divisor: >>> 42 % 5 2 >>> -42 % 5 3 >>> 42 % -5 -3 >>> -42 % -5 -2 There may be practical or logical reasons for preferring one over the other, but either choice would make remainder a bijection. One might even define two separate functions/operators, one for each behaviour. > They might not be willing to define it, but as soon as we programmers > do, well, we did. > > Having studied the contemporary philosophy of mathematics, their concern > is probably that in their minds, mathematics is whatever some dead guy > said it was, and they dont know of any dead guy ever talking about a > modulus operation, so therefore it 'does not exist'. You've studied the contemporary philosophy of mathematics huh? How about studying some actual mathematics before making such absurd pronouncements on the psychology of mathematicians? -- Steven From tjreedy at udel.edu Wed Dec 14 00:01:06 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Dec 2011 00:01:06 -0500 Subject: xml, minidom, ElementTree In-Reply-To: <4EE7DDE8.2050706@stoneleaf.us> References: <4EE1C9AB.2040301@v.loewis.de> <4EE528BD.2040102@v.loewis.de> <4EE7DDE8.2050706@stoneleaf.us> Message-ID: On 12/13/2011 6:21 PM, Ethan Furman wrote: > In the near future I will need to parse and rewrite parts of a xml files > created by a third-party program (PrintShopMail, for the curious). > It contains both binary and textual data. > > There has been some strong debate about the merits of minidom vs > ElementTree. > > Recommendations? People's reaction to the DOM interface seem quite varied, with a majority, perhaps, being negative. I personally would look at both enough to understand the basic API model to see where *I* fit. -- Terry Jan Reedy From showell30 at yahoo.com Wed Dec 14 00:42:52 2011 From: showell30 at yahoo.com (Steve Howell) Date: Tue, 13 Dec 2011 21:42:52 -0800 (PST) Subject: AttributeError in "with" statement (3.2.2) Message-ID: I'm using Python 3.2.2, and the following program gives me an error that I don't understand: class Foo: pass foo = Foo() foo.name = "Steve" def add_goodbye_function(obj): def goodbye(): print("goodbye " + obj.name) obj.goodbye = goodbye add_goodbye_function(foo) foo.goodbye() # outputs goodbye Steve foo.__exit__ = foo.goodbye foo.__exit__() # outputs goodbye Steve with foo: # fails with AttributeError: __exit__ print("doing stuff") I am dynamically adding an attribute __exit__ to the variable foo, which works fine when I call it directly, but it fails when I try to use foo as the expression in the with statement. Here is the full output: > python3 with.coffee goodbye Steve goodbye Steve Traceback (most recent call last): File "with.coffee", line 17, in with foo: # fails with AttributeError: AttributeError: __exit__ What am I doing wrong? From ericsnowcurrently at gmail.com Wed Dec 14 01:05:53 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Tue, 13 Dec 2011 23:05:53 -0700 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 10:42 PM, Steve Howell wrote: > I'm using Python 3.2.2, and the following program gives me an error > that I don't understand: > > class Foo: > ?pass > > foo = Foo() > foo.name = "Steve" > > def add_goodbye_function(obj): > ?def goodbye(): > ? ?print("goodbye " + obj.name) > ?obj.goodbye = goodbye > > add_goodbye_function(foo) > foo.goodbye() # outputs goodbye Steve > foo.__exit__ = foo.goodbye > foo.__exit__() # outputs goodbye Steve > > with foo: # fails with AttributeError: ?__exit__ > ?print("doing stuff") > > I am dynamically adding an attribute __exit__ to the variable foo, > which works fine when I call it directly, but it fails when I try to > use foo as the expression in the with statement. ?Here is the full > output: > >> python3 with.coffee > goodbye Steve > goodbye Steve > Traceback (most recent call last): > ?File "with.coffee", line 17, in > ? ?with foo: # fails with AttributeError: > AttributeError: __exit__ > > What am I doing wrong? That is a tricky one. As with many of the special methods (start and end with __) in Python, the underlying mechanism in the interpreter is directly pulling the function from the class object. It does not look to the instance object for the function at any time. See http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes. -eric From tjreedy at udel.edu Wed Dec 14 01:29:13 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Dec 2011 01:29:13 -0500 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: Message-ID: On 12/14/2011 1:05 AM, Eric Snow wrote: > On Tue, Dec 13, 2011 at 10:42 PM, Steve Howell wrote: >> I'm using Python 3.2.2, and the following program gives me an error >> that I don't understand: >> >> class Foo: >> pass >> >> foo = Foo() >> foo.name = "Steve" >> >> def add_goodbye_function(obj): >> def goodbye(): >> print("goodbye " + obj.name) >> obj.goodbye = goodbye >> >> add_goodbye_function(foo) >> foo.goodbye() # outputs goodbye Steve >> foo.__exit__ = foo.goodbye >> foo.__exit__() # outputs goodbye Steve foo.goodbye, aliased as foo.__exit__, is a *function* attribute of the foo *instance*. >> with foo: # fails with AttributeError: __exit__ >> print("doing stuff") >> >> I am dynamically adding an attribute __exit__ to the variable foo, >> which works fine when I call it directly, but it fails when I try to >> use foo as the expression in the with statement. Here is the full >> output: >> >>> python3 with.coffee >> goodbye Steve >> goodbye Steve >> Traceback (most recent call last): >> File "with.coffee", line 17, in >> with foo: # fails with AttributeError: >> AttributeError: __exit__ >> >> What am I doing wrong? To complement what Eric says below: The with statement is looking for an instance *method*, which by definition, is a function attribute of a *class* (the class of the context manager) that takes an instance of the class as its first parameter. Notice that goodbye does not take any parameter. The first parameter is conventionally called 'self', but that is not a requirement. That it be there, that it expect to be bound to an instance, and that the function be bound to the class are requirements. > That is a tricky one. > > As with many of the special methods (start and end with __) in Python, > the underlying mechanism in the interpreter is directly pulling the > function from the class object. It does not look to the instance > object for the function at any time. See > http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes. -- Terry Jan Reedy From stefan_ml at behnel.de Wed Dec 14 02:22:25 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 14 Dec 2011 08:22:25 +0100 Subject: xml, minidom, ElementTree In-Reply-To: References: <4EE1C9AB.2040301@v.loewis.de> <4EE528BD.2040102@v.loewis.de> <4EE7DDE8.2050706@stoneleaf.us> Message-ID: Terry Reedy, 14.12.2011 06:01: > On 12/13/2011 6:21 PM, Ethan Furman wrote: >> In the near future I will need to parse and rewrite parts of a xml files >> created by a third-party program (PrintShopMail, for the curious). >> It contains both binary and textual data. >> >> There has been some strong debate about the merits of minidom vs >> ElementTree. >> >> Recommendations? > > People's reaction to the DOM interface seem quite varied, with a majority, > perhaps, being negative. I personally would look at both enough to > understand the basic API model to see where *I* fit. The API is one thing, yes, but there's also the fact that MiniDOM doesn't scale. If your XML files are of a notable size (a couple of MB), MiniDOM may simply not be able to handle them. I collected some numbers in a blog post. Note that this is using a recent CPython 3.3 build which has an optimised Unicode string implementation, thus yielding lower memory requirements on average than Py2.x. http://blog.behnel.de/index.php?p=197 The memory consumption makes a difference of a factor of 5-10 compared to cElementTree, which makes it two orders of magnitude larger than the size of the serialised file. You may be able to stuff one such file into memory, but you'll quickly get into trouble when you try to do parallel processing or otherwise use more than one document at a time. Stefan From hoogendoorn.eelco at gmail.com Wed Dec 14 02:49:43 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 23:49:43 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Dec 14, 4:18?am, Steven D'Aprano wrote: > > They might not be willing to define it, but as soon as we programmers > > do, well, we did. > > > Having studied the contemporary philosophy of mathematics, their concern > > is probably that in their minds, mathematics is whatever some dead guy > > said it was, and they dont know of any dead guy ever talking about a > > modulus operation, so therefore it 'does not exist'. > > You've studied the contemporary philosophy of mathematics huh? > > How about studying some actual mathematics before making such absurd > pronouncements on the psychology of mathematicians? The philosophy was just a sidehobby to the study of actual mathematics; and you are right, studying their works is the best way to get to know them. Speaking from that vantage point, I can say with certainty that the vast majority of mathematicians do not have a coherent philosophy, and they adhere to some loosely defined form of platonism. Indeed that is absurd in a way. Even though you may trust these people to be perfectly functioning deduction machines, you really shouldnt expect them to give sensible answers to the question of which are sensible axioms to adopt. They dont have a reasoned answer to this, they will by and large defer to authority. From steve+comp.lang.python at pearwood.info Wed Dec 14 03:01:24 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 08:01:24 GMT Subject: AttributeError in "with" statement (3.2.2) References: Message-ID: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > To complement what Eric says below: The with statement is looking for an > instance *method*, which by definition, is a function attribute of a > *class* (the class of the context manager) that takes an instance of the > class as its first parameter. I'm not sure that is correct... I don't think that there is anything "by definition" about where methods live. Particularly not in Python where instance methods can be attributes of the instance itself. >>> class Test(object): ... def method(self): ... print("This method is an attribute of the class.") ... >>> t = Test() >>> t.method() This method is an attribute of the class. >>> >>> import types >>> t.method = types.MethodType( ... lambda self: print( ... "This method is an attribute of the instance."), t) >>> t.method() This method is an attribute of the instance. So the normal lookup rules that apply to data attributes, namely instance, then class, then superclasses, also applies to methods in Python. In languages that don't allow that sort of thing, like Java, you need to use convoluted design patterns like Dynamic Proxy to make it work. In Python, you just create a method and attach it on the instance. http://stackoverflow.com/questions/8260740/override-a-method-for-an- instance-of-a-class But this doesn't apply for special dunder attributes like __exit__, for speed reasons. (For new-style classes only, classic classes have no such special casing. This makes automatic delegation a breeze in Python 2 with classic classes, and a PITA in Python 3. Boo hiss.) -- Steven From pip.261 at gmail.com Wed Dec 14 03:28:45 2011 From: pip.261 at gmail.com (Felipe O) Date: Wed, 14 Dec 2011 03:28:45 -0500 Subject: Property Abuse Message-ID: Hi All, I was wondering what everyone's thought process was regarding properties. Lately I find I've been binging on them and have classes with > 10 properties. While pylint doesn't complain (yet), it tends to be picky about keeping instance attribute counts low, so I figure there's something against that. How do you guys decide between using properties versus getter methods, or how do you refactor them if neither? Cheers! -Felipe -------------- next part -------------- An HTML attachment was scrubbed... URL: From jpiitula at ling.helsinki.fi Wed Dec 14 03:56:02 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2011 10:56:02 +0200 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano writes: > On Mon, 12 Dec 2011 09:29:11 -0800, Eelco wrote: > > [quoting Jussi Piitulainen ] > >> They recognize modular arithmetic but for some reason insist that > >> there is no such _binary operation_. But as I said, I don't > >> understand their concern. (Except the related concern about some > >> programming languages, not Python, where the remainder does not > >> behave well with respect to division.) > > I've never come across this, and frankly I find it implausible that > *actual* mathematicians would say that. Likely you are > misunderstanding a technical argument about remainder being a > relation rather than a bijunction. The argument would go something > like this: (For 'bijunction', read 'function'.) I'm not misunderstanding any argument. There was no argument. There was a blanket pronouncement that _in mathematics_ mod is not a binary operator. I should learn to challenge such pronouncements and ask what the problem is. Maybe next time. But you are right that I don't know how actual mathematicians these people are. I'm not a mathematician. I don't know where to draw the line. A Finnish actual mathematician stated a similar prejudice towards mod as a binary operator in a Finnish group. I asked him what is wrong with Knuth's definition (remainder after flooring division), and I think he conceded that it's not wrong. Number theorists just choose to work with congruence relations. I have no problem with that. He had experience with students who confused congruences modulo some modulus with a binary operation, and mixed up their notations because of that. That is a reason to be suspicious, but it is a confusion on the part of the students. Graham, Knuth, Patashnik contrast the two concepts explicitly, no confusion there. And I know that there are many ways to define division and remainder so that x div y + x rem y = x. Boute's paper cited in [1] advocates a different one and discusses others. [1] But I think the argument "there are several such functions, therefore, _in mathematics_, there is no such function" is its own caricature. > "Remainder is not uniquely defined. For example, the division of -42 > by -5 can be written as either: > > 9*-5 + 3 = -42 > 8*-5 + -2 = -42 > > so the remainder is either 3 or -2. Hence remainder is not a bijection > (1:1 function)." Is someone saying that _division_ is not defined because -42 div -5 is somehow both 9 and 8? Hm, yes, I see that someone might. The two operations, div and rem, need to be defined together. (There is no way to make remainder a bijection. You mean it is not a function if it is looked at in a particular way.) [The square root was relevant but I snipped it.] > Similarly, we can sensibly define the remainder or modulus operator > to consistently return a non-negative remainder, or to do what > Python does, which is to return a remainder with the same sign as > the divisor: ... > There may be practical or logical reasons for preferring one over > the other, but either choice would make remainder a bijection. One > might even define two separate functions/operators, one for each > behaviour. Scheme is adopting flooring division, ceiling-ing division, rounding division, truncating division, centering division, and the Euclidean division advocated by Boute, and the corresponding remainders. There is no better way to bring home to a programmer the points that there are different ways to define these, and they come as div _and_ rem. From jpiitula at ling.helsinki.fi Wed Dec 14 04:04:04 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2011 11:04:04 +0200 Subject: Verbose and flexible args and kwargs syntax References: Message-ID: Nick Dokos writes: > Jussi Piitulainen wrote: > > They recognize modular arithmetic but for some reason insist that > > there is no such _binary operation_. But as I said, I don't > > understand their concern. (Except the related concern about some > > programming languages, not Python, where the remainder does not > > behave well with respect to division.) > > They are probably arguing that it's uniquely defined only on ZxN and > that there are different conventions to extend it to ZxZ (the > programming languages problem that you allude to above - although I > don't know what you mean by "does not behave well wrt division"). I think Boute [1] says Standard Pascal or some such language failed to have x div y + x rem y = x, but I can't check the reference now. That at least waes what I had in mind. Having x rem y but leaving it underspecified is another such problem: then it is unspecified whether the equation holds. [1] > If you choose one convention and stick to it, it becomes a > well-defined binary operation. That's what I'd like to think. From paul.nospam at rudin.co.uk Wed Dec 14 04:38:55 2011 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 14 Dec 2011 09:38:55 +0000 Subject: xml, minidom, ElementTree References: <4EE1C9AB.2040301@v.loewis.de> <4EE528BD.2040102@v.loewis.de> Message-ID: <87vcpjqyq8.fsf@no-fixed-abode.cable.virginmedia.net> Ethan Furman writes: > In the near future I will need to parse and rewrite parts of a xml files > created by a third-party program (PrintShopMail, for the curious). > It contains both binary and textual data. > > There has been some strong debate about the merits of minidom vs > ElementTree. > > Recommendations? I tend to start with BeautifulSoup, and think about other things if that doesn't work out. It might just be me, but I find it easier to get stuff done using BeautifulSoup than either minidom or ElementTree. From andrea.crotti.0 at gmail.com Wed Dec 14 04:42:08 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Wed, 14 Dec 2011 09:42:08 +0000 Subject: boolean from a function In-Reply-To: <4ee7e1a4$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee7e1a4$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EE86F70.1090305@gmail.com> On 12/13/2011 11:37 PM, Steven D'Aprano wrote: > > x is a global? Poor design. But in any case, instead of an explicit > if...else block, the canonical way to convert an arbitrary object to True/ > False is with bool: > > def func_bool(): > return bool(x) > > But you don't need it. See below. > > > No no it was just to show the pattern, it wasn't the actual code. I don't like to have useless indirections, so I wouldn't do that... I like the idea of the property (from Duncan Booth) but the thing is that that function looks like it's doing something (from its name), it's not just a simple property. In the case of the square class Sq(object): def __init__(self, x, y): self.x = x self.y It makes perfect sense to have "area" as a property, because you can either compute it and cache it or compute it on demand. It feels a bit less natural to create a property on something that is less simple than that imho.. From paul.nospam at rudin.co.uk Wed Dec 14 04:43:36 2011 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 14 Dec 2011 09:43:36 +0000 Subject: % is not an operator References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: <87r507qyif.fsf@no-fixed-abode.cable.virginmedia.net> Steven D'Aprano writes: > The existence of two potential answers for the remainder is certainly > correct, but the conclusion that remainder is not a binary operation > doesn't follow. It is a binary relation. This depends on your definition of "operation". Normally an operation is a function, rather than just a relation. From __peter__ at web.de Wed Dec 14 05:02:26 2011 From: __peter__ at web.de (Peter Otten) Date: Wed, 14 Dec 2011 11:02:26 +0100 Subject: AttributeError in "with" statement (3.2.2) References: Message-ID: Steve Howell wrote: > I'm using Python 3.2.2, and the following program gives me an error > that I don't understand: > > class Foo: > pass > > foo = Foo() > foo.name = "Steve" > > def add_goodbye_function(obj): > def goodbye(): > print("goodbye " + obj.name) > obj.goodbye = goodbye > > add_goodbye_function(foo) > foo.goodbye() # outputs goodbye Steve > foo.__exit__ = foo.goodbye > foo.__exit__() # outputs goodbye Steve > > with foo: # fails with AttributeError: __exit__ > print("doing stuff") > > I am dynamically adding an attribute __exit__ to the variable foo, > which works fine when I call it directly, but it fails when I try to > use foo as the expression in the with statement. Here is the full > output: > >> python3 with.coffee > goodbye Steve > goodbye Steve > Traceback (most recent call last): > File "with.coffee", line 17, in > with foo: # fails with AttributeError: > AttributeError: __exit__ If you don't know it yet, contextlib.contextmanager offers a highlevel alternative: >>> @contextmanager ... def goodbye(obj): ... try: ... yield obj ... finally: ... print("goodbye", obj.name) ... >>> with goodbye(Foo()) as foo: ... print("hello", foo.name) ... hello Steve goodbye Steve From hoogendoorn.eelco at gmail.com Wed Dec 14 05:09:32 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 14 Dec 2011 02:09:32 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> On 14 dec, 09:56, Jussi Piitulainen wrote: > Steven D'Aprano writes: > > On Mon, 12 Dec 2011 09:29:11 -0800, Eelco wrote: > > > [quoting Jussi Piitulainen ] > > >> They recognize modular arithmetic but for some reason insist that > > >> there is no such _binary operation_. But as I said, I don't > > >> understand their concern. (Except the related concern about some > > >> programming languages, not Python, where the remainder does not > > >> behave well with respect to division.) > > > I've never come across this, and frankly I find it implausible that > > *actual* mathematicians would say that. Likely you are > > misunderstanding a technical argument about remainder being a > > relation rather than a bijunction. The argument would go something > > like this: > > (For 'bijunction', read 'function'.) > > I'm not misunderstanding any argument. There was no argument. There > was a blanket pronouncement that _in mathematics_ mod is not a binary > operator. I should learn to challenge such pronouncements and ask what > the problem is. Maybe next time. > > But you are right that I don't know how actual mathematicians these > people are. I'm not a mathematician. I don't know where to draw the > line. > > A Finnish actual mathematician stated a similar prejudice towards mod > as a binary operator in a Finnish group. I asked him what is wrong > with Knuth's definition (remainder after flooring division), and I > think he conceded that it's not wrong. Number theorists just choose to > work with congruence relations. I have no problem with that. > > He had experience with students who confused congruences modulo some > modulus with a binary operation, and mixed up their notations because > of that. That is a reason to be suspicious, but it is a confusion on > the part of the students. Graham, Knuth, Patashnik contrast the two > concepts explicitly, no confusion there. > > And I know that there are many ways to define division and remainder > so that x div y + x rem y = x. Boute's paper cited in [1] advocates a > different one and discusses others. > > [1] > > But I think the argument "there are several such functions, therefore, > _in mathematics_, there is no such function" is its own caricature. Indeed. Obtaining a well defined function is just a matter of picking a convention and sticking with it. Arguably, the most elegant thing to do is to define integer division and remainder as a single operation; which is not only the logical thing to do mathematically, but might work really well programmatically too. The semantics of python dont really allow for this though. One could have: d, r = a // b But it wouldnt work that well in composite expressions; selecting the right tuple index would be messy and a more verbose form would be preferred. However, performance-wise its also clearly the best solution, as one often needs both output arguments and computing them simultaniously is most efficient. At least numpy should have something like: d, r = np.integer_division(a, b) And something similar in the math module for scalars. > > "Remainder is not uniquely defined. For example, the division of -42 > > by -5 can be written as either: > > > ? ? 9*-5 + 3 = -42 > > ? ? 8*-5 + -2 = -42 > > > so the remainder is either 3 or -2. Hence remainder is not a bijection > > (1:1 function)." > > Is someone saying that _division_ is not defined because -42 div -5 is > somehow both 9 and 8? Hm, yes, I see that someone might. The two > operations, div and rem, need to be defined together. > > (There is no way to make remainder a bijection. You mean it is not a > function if it is looked at in a particular way.) Surjection is the word you are looking for That is, if one buys the philosophy of modernists like bourbaki in believing there is much to be gained by such pedantry. From jeanmichel at sequans.com Wed Dec 14 05:14:53 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 14 Dec 2011 11:14:53 +0100 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> Message-ID: <4EE8771D.1050902@sequans.com> Joshua Landau wrote: > On 13 December 2011 13:30, Jean-Michel Pichavant > > wrote: > > writing > > x = 1 > > def spam(): > x = 2 > > is in general a bad idea. That was my point. > > > Why? I have a few (probably wrong) guesses. > > Because you expect it to be the same every time you use it? > Well, then this should be "in general a bad idea": > x = 1; print(x); x = 2; print(x) you're changing the value of x, that's fine. In the example above, the assignement is not the problem. The problem is that you create 2 different 'x', one in globals(), and one in locals(). Using the same name within 2 implicit namespaces is a bad idead in general because it can be difficult to know which one is used. If you want to have fun, try this code, prepared to be amazed. There's something funny with the global statement, it's applied on the whole block no matter where it is stated in the block. x=1 # global def spam(): x = 2 # local (or so you may think) print x global x # I need to use the global one now print x print locals() For more fun you could create a 'x' name in __builtin__ and import it so that people never know which x you are using. > Even though it makes total sense to me. > > Is it because it's used to different purpose between similarly-looking > functions? > This looks fine, though: > def func1(): x=1; print(x) > def func2(): x=2; print(x) > > Is it because it looks like a reassignment of the more global x? > I don't have an example here but, simply put, I don't believe this. We > can use "id" as our own local variable without thinking that we're > tampering with "__builtins__.id". I don't see it as much of a leap > from builtin to global (except that you /*can*/ do "dir = 1; del dir; > dir" without error). > > That said, I'm sorta' just guessing the reason you might think it's a > bad idea. The problem makes little sense when using names like x or func1. Besides namespace issues, naming 2 *different objects* with the same meaningful name is usually a bad idea and points the fact that your names are no that meaningful. To go back to the original post, having a 'logger' that may name 2 different logger object during the execution is a bad idea. One quick way to fix it is to name the logger 'currentLogger', this way you warn the reader that the logger named by curentLogger may change over time. As someone sugggested in this thread one other option is to use a different name for the second logger. JM From rosuav at gmail.com Wed Dec 14 05:32:11 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Dec 2011 21:32:11 +1100 Subject: Overriding a global In-Reply-To: <4EE8771D.1050902@sequans.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> Message-ID: On Wed, Dec 14, 2011 at 9:14 PM, Jean-Michel Pichavant wrote: > The problem makes little sense when using names like x or func1. Besides > namespace issues, naming 2 *different objects* with the same meaningful name > is usually a bad idea and points the fact that your names are no that > meaningful. So... it's a bad idea for me to use 'i' many times in my code, with the same name having different meanings in different places? In languages with infinitely-nesting scopes (one of Python's great lacks, imho), I've often had three different variables with the same names, all perfectly valid, and all doing what they should. It's not just loop indices - I used to have a piece of code in which 'res' was a MySQL resource being processed in a loop, and I had three nested loops. Each time I referenced 'res', it used the innermost available resource, which was precisely what I wanted. If I'd arbitrarily had to guarantee that all variable names were unique, I would have had completely unnecessary fiddling around. Python wouldn't let you do that with three nested 'res'es in one function, but you can perfectly reasonably have a global and a local. It makes perfect sense... which is a good reason for keeping it legal. ChrisA From candide at free.invalid Wed Dec 14 06:12:07 2011 From: candide at free.invalid (candide) Date: Wed, 14 Dec 2011 12:12:07 +0100 Subject: Regexp : repeated group identification Message-ID: <4ee88488$0$27871$426a74cc@news.free.fr> Consider the following code # ---------------------------- import re z=re.match('(Spam\d)+', 'Spam4Spam2Spam7Spam8') print z.group(0) print z.group(1) # ---------------------------- outputting : ---------------------------- Spam4Spam2Spam7Spam8 Spam8 ---------------------------- The '(Spam\d)+' regexp is tested against 'Spam4Spam2Spam7Spam8' and the regexp matches the string. Group numbered one within the regex '(Spam\d)+' refers to Spam\d The fours substrings Spam4 Spam2 Spam7 and Spam8 match the group numbered 1. So I don't understand why z.group(1) gives the last substring (ie Spam8 as the output shows), why not an another one, Spam4 for example ? From narges.javaheri at gmail.com Wed Dec 14 06:15:53 2011 From: narges.javaheri at gmail.com (narges javaheri) Date: Wed, 14 Dec 2011 12:15:53 +0100 Subject: Question (PDE solvers in Python) Message-ID: Hello there, Based on your experiences, what's the best (the most reliable) package for solving a system of nonlinear coupled PDEs (partial differential equations) in 3D using Python, preferably by FEM (finite element method)? Regards, Narges -------------- next part -------------- An HTML attachment was scrubbed... URL: From vlastimil.brom at gmail.com Wed Dec 14 06:34:16 2011 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Wed, 14 Dec 2011 12:34:16 +0100 Subject: Regexp : repeated group identification In-Reply-To: <4ee88488$0$27871$426a74cc@news.free.fr> References: <4ee88488$0$27871$426a74cc@news.free.fr> Message-ID: 2011/12/14 candide : > Consider the following code > > # ---------------------------- > import re > > z=re.match('(Spam\d)+', 'Spam4Spam2Spam7Spam8') > print z.group(0) > print z.group(1) > # ---------------------------- > > outputting : > > ---------------------------- > Spam4Spam2Spam7Spam8 > Spam8 > ---------------------------- > > The '(Spam\d)+' regexp is tested against 'Spam4Spam2Spam7Spam8' and the > regexp matches the string. > > Group numbered one within the regex '(Spam\d)+' refers to Spam\d > > The fours substrings > > Spam4 ? Spam2 ? Spam7 ?and ?Spam8 > > match the group numbered 1. > > So I don't understand why z.group(1) gives the last substring (ie Spam8 as > the output shows), why not an another one, Spam4 for example ? > -- > http://mail.python.org/mailman/listinfo/python-list Hi, you may find a tiny notice in the re docs on this: http://docs.python.org/library/re.html#re.MatchObject.group "If a group is contained in a part of the pattern that matched multiple times, the last match is returned." If you need to work with the content captured in the repeated group, you may check the new regex implementation: http://pypi.python.org/pypi/regex Which has a special "captures" method of the match object for this (beyond many other improvements): >>> import regex >>> m=regex.match('(Spam\d)+', 'Spam4Spam2Spam7Spam8') >>> m.captures(1) ['Spam4', 'Spam2', 'Spam7', 'Spam8'] >>> hth, vbr From rustompmody at gmail.com Wed Dec 14 06:47:28 2011 From: rustompmody at gmail.com (rusi) Date: Wed, 14 Dec 2011 03:47:28 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: <851cbabf-60b6-4806-bef5-e16f6089b08d@h37g2000pri.googlegroups.com> On Dec 14, 1:56?pm, Jussi Piitulainen wrote: > > Is someone saying that _division_ is not defined because -42 div -5 is > somehow both 9 and 8? Hm, yes, I see that someone might. The two > operations, div and rem, need to be defined together. ----------------------------- Haskell defines a quot-rem pair and a div-mod pair as follows: (from http://www.haskell.org/onlinereport/basic.html) (x `quot` y)*y + (x `rem` y) == x (x `div` y)*y + (x `mod` y) == x `quot` is integer division truncated toward zero, while the result of `div` is truncated toward negative infinity. From rosuav at gmail.com Wed Dec 14 06:53:38 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Dec 2011 22:53:38 +1100 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <851cbabf-60b6-4806-bef5-e16f6089b08d@h37g2000pri.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <851cbabf-60b6-4806-bef5-e16f6089b08d@h37g2000pri.googlegroups.com> Message-ID: On Wed, Dec 14, 2011 at 10:47 PM, rusi wrote: > `quot` is integer division truncated toward zero, while the result of > `div` is truncated toward negative infinity. All these problems just because of negative numbers. They ought never to have been invented. At least nobody rounds toward positive infinity... oh wait, that's legal too. ChrisA From arnodel at gmail.com Wed Dec 14 06:55:34 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Wed, 14 Dec 2011 11:55:34 +0000 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: On 14 December 2011 07:49, Eelco wrote: > On Dec 14, 4:18?am, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> > They might not be willing to define it, but as soon as we programmers >> > do, well, we did. >> >> > Having studied the contemporary philosophy of mathematics, their concern >> > is probably that in their minds, mathematics is whatever some dead guy >> > said it was, and they dont know of any dead guy ever talking about a >> > modulus operation, so therefore it 'does not exist'. >> >> You've studied the contemporary philosophy of mathematics huh? >> >> How about studying some actual mathematics before making such absurd >> pronouncements on the psychology of mathematicians? > > The philosophy was just a sidehobby to the study of actual > mathematics; and you are right, studying their works is the best way > to get to know them. Speaking from that vantage point, I can say with > certainty that the vast majority of mathematicians do not have a > coherent philosophy, and they adhere to some loosely defined form of > platonism. Indeed that is absurd in a way. Even though you may trust > these people to be perfectly functioning deduction machines, you > really shouldnt expect them to give sensible answers to the question > of which are sensible axioms to adopt. They dont have a reasoned > answer to this, they will by and large defer to authority. Please come down from your vantage point for a few moments and consider how insulting your remarks are to people who have devoted most of their intellectual energy to the study of mathematics. So you've studied a bit of mathematics and a bit of philosophy? Good start, keep working at it. You think that every mathematician should be preoccupied with what axioms to adopt, and why? Mathematics is a very large field of study and yes, some mathematicians are concerned with these issues (they are called logicians) but for most it isn't really about axioms. Mathematics is bigger than the axioms that we use to formalise it. Most mathematicians do not need to care about what precise axiomatisation underlies the mathematics that they practise because they are thinking on a much higher level. Just like we do not worry about what machine language instruction actually performs each step of the Python program we are writing. You say that mathematicians defer to authority, but do you really think that thousands of years of evolution and refinement in mathematics are to be discarded lightly? I think not. It's good to have original ideas, to pursue them and to believe in them, but it would be foolish to think that they are superior to knowledge which has been accumulated over so many generations. You claim that mathematicians have a poor understanding of philosophy. It may be so for many of them, but how is this a problem? I doesn't prevent them from having a deep understanding of their field of mathematics. Do philosophers have a good understanding of mathematics? Cheers, -- Arnaud From jeanmichel at sequans.com Wed Dec 14 07:05:19 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 14 Dec 2011 13:05:19 +0100 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> Message-ID: <4EE890FF.3080708@sequans.com> Chris Angelico wrote: > On Wed, Dec 14, 2011 at 9:14 PM, Jean-Michel Pichavant > wrote: > >> The problem makes little sense when using names like x or func1. Besides >> namespace issues, naming 2 *different objects* with the same meaningful name >> is usually a bad idea and points the fact that your names are no that >> meaningful. >> > > So... it's a bad idea for me to use 'i' many times in my code, with > the same name having different meanings in different places? In > languages with infinitely-nesting scopes (one of Python's great lacks, > imho), I've often had three different variables with the same names, > all perfectly valid, and all doing what they should. It's not just > loop indices - I used to have a piece of code in which 'res' was a > MySQL resource being processed in a loop, and I had three nested > loops. Each time I referenced 'res', it used the innermost available > resource, which was precisely what I wanted. If I'd arbitrarily had to > guarantee that all variable names were unique, I would have had > completely unnecessary fiddling around. > > Python wouldn't let you do that with three nested 'res'es in one > function, but you can perfectly reasonably have a global and a local. > It makes perfect sense... which is a good reason for keeping it legal. > > ChrisA > Bad ideas : i = 5 def spam(): for i,v in enumerate([1,2,3,4]): for i,v in enumerate(['a','b', 'c']): print i, v print i,v # bad surprise good ideas : # global nameThatWillNotBeUsedlocally = 'foo' def spam(): for qtyIndex, quantity in enumerate([5,6,3,1]): for fruitIndex, fruit in enumerate(['orange', 'banana']): print fruitIndex, fruit print qtyIndex, quantity While a lot of people still use i,j,k,v to handler values and indexes, I think it's a bad idea. I'm just stating an opinion from my personnal python experience. I know some people can successfully use the hard way. JM From rosuav at gmail.com Wed Dec 14 07:21:29 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Dec 2011 23:21:29 +1100 Subject: Overriding a global In-Reply-To: <4EE890FF.3080708@sequans.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> <4EE890FF.3080708@sequans.com> Message-ID: On Wed, Dec 14, 2011 at 11:05 PM, Jean-Michel Pichavant wrote: > Chris Angelico wrote: >> >> So... it's a bad idea for me to use 'i' many times in my code, with >> the same name having different meanings in different places? In >> languages with infinitely-nesting scopes... > Bad ideas : > > i = 5 > > def spam(): > ?for i,v in enumerate([1,2,3,4]): > ? for i,v in enumerate(['a','b', 'c']): > ? ? print i, v > ? print i,v # bad surprise That's my point. It's not safe to do it in Python, because the "inner" local i is the same as the "outer" local i. Python doesn't have block scope the way most C-like languages do. int spam() { for (int i=0;i<5;++i) { for (int i=2;i<4;++i) write("inner "+i+"\n"); write("outer "+i+"\n"); } } Works perfectly, and the only cost is that variables must be formally declared. In Python, you can kinda fudge that sort of thing with nested functions. def spam(): q=2 # just to prove that the scopes really are nested for i in range(5): def ham(): for i in range(2,4): print("q = %d, i = %d"%(q,i)) ham() print("i = %d"%i) It's somewhat clunky, but it does give the illusion of block scope. Inners mask outers, outers are visible to inner functions. It's an odd construct though. Very odd. So... I think I've figured out how to implement from __future__ import braces. #define { def innerfunc(): #define } innerfunc() And there you are, out of your difficulty at once! ChrisA From jpiitula at ling.helsinki.fi Wed Dec 14 07:22:27 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2011 14:22:27 +0200 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> Message-ID: Eelco writes: > On 14 dec, 09:56, Jussi Piitulainen wrote: > > But I think the argument "there are several such functions, > > therefore, _in mathematics_, there is no such function" is its own > > caricature. > > Indeed. Obtaining a well defined function is just a matter of > picking a convention and sticking with it. > > Arguably, the most elegant thing to do is to define integer division > and remainder as a single operation; which is not only the logical > thing to do mathematically, but might work really well > programmatically too. > > The semantics of python dont really allow for this though. One could > have: > > d, r = a // b > > But it wouldnt work that well in composite expressions; selecting the > right tuple index would be messy and a more verbose form would be > preferred. However, performance-wise its also clearly the best > solution, as one often needs both output arguments and computing them > simultaniously is most efficient. The current Scheme draft does this. For each rounding method, it provides an operation that provides both the quotient and the remainder, an operation that provides the quotient, and an operation that provides the remainder. The both-values operation is more awkward to compose, as you rightly say. It's just a matter of naming them all. Python has a good default integer division as the pair of operators // and %. Python also supports the returning of several values from functions as tuples. It can be done. > > Is someone saying that _division_ is not defined because -42 div > > -5 is somehow both 9 and 8? Hm, yes, I see that someone might. The > > two operations, div and rem, need to be defined together. > > > > (There is no way to make remainder a bijection. You mean it is not > > a function if it is looked at in a particular way.) > > Surjection is the word you are looking for Um, no, I mean function. The allegedly alleged problem is that there may be two (or more) different values for f(x,y), which makes f not a _function_ (and the notation f(x,y) maybe inappropriate). Surjectivity is as much beside the point as bijectivity, but I think we have surjectivity for rem: Z * Z -> Z if we use a definition that produces both positive and negative remainders, or rem: Z * Z -> N if we have non-negative remainders (and include 0 in N, which is another bone of contention). We may or may not want to exclude 0 as the modulus, or divisor if you like. It is at least a special case. It's injectivity that fails: 9 % 4 == 6 % 5 == 3 % 2, while Python quite sensibly has (9, 4) != (6, 5) != (3, 2). (How I love the chaining of the comparisons.) > That is, if one buys the philosophy of modernists like bourbaki in > believing there is much to be gained by such pedantry. I think something is gained. Not sure I would call it philosophy. From corleone82 at gmail.com Wed Dec 14 07:31:21 2011 From: corleone82 at gmail.com (Juan Perez) Date: Wed, 14 Dec 2011 12:31:21 +0000 Subject: automate commands to an .exe console program through python In-Reply-To: References: <4EE73847.30404@timgolden.me.uk> Message-ID: Hi, Well if I can remmeber since last time I did something similar in C, it was close stdin channel in the open devices table (I don't know if it is the correct name in english, I learnt it in spanish) and put a pipe on it and then create a fork, parent it comunicates to the child through the pipe, so if child run a shell program, through exec, you should be able to comunicate to his stdin through his parent... I think that was correct, if I remmember well... My app is console app, working by basic commands like send cd or dir to the console. I thought redirecting stdin (keypad) I could send this basic comands to it... but obviously I was completely wrong... I can not use dedicated virtual machines, and it has to be as similar as user environment as possible, and it can appear new windows that takes focus, so I have to assure i can get the focus every time before sending commands.... Quite tricky stuff.... if it is possible to do something like that in Python I can struggle a bit more, if not better leave right now before wasting time with nonsenses... Thanks, Juan 2011/12/13 Chris Angelico > On Tue, Dec 13, 2011 at 11:35 PM, Juan Perez wrote: > > My problem is thinking in windows console somewhat like linux shell, and > do > > same things I did with pipes in C programming. But it seems not to be the > > case. > > It is (at least, it supports the basics of pipes and redirection); but > what you're looking at here is an application that appears not to use > STDIN. > > > At a glance I dare say that sendkeys module is useful enough, but if > I've > > understood well, you only can send messages to focused window, that will > > report some inestabilities if windows focused is changed. You won't be > able > > to touch anything in the current laptop. So, is there any way to identify > > the a process/window and send keystrokes in a smarter way and/or is it > > possible to identify a window an get the focus on it? > > You may be able to put focus to the window before sending it keys, but > this is sounding extremely hackish (nearly as bad as the Magic: The > Gathering Online trade-bot system). I'm thinking here the best option > may be to give this a dedicated virtual machine and let it do whatever > it likes - effectively, have an entire "computer" dedicated to just > this application and its automation harness. That would probably make > things simple enough that you don't need to worry too much about > focus, sending keystrokes to other windows, etc. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Dec 14 07:32:34 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 12:32:34 GMT Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Dec 2011 10:56:02 +0200, Jussi Piitulainen wrote: > Steven D'Aprano writes: >> On Mon, 12 Dec 2011 09:29:11 -0800, Eelco wrote: >> >> [quoting Jussi Piitulainen ] >> >> They recognize modular arithmetic but for some reason insist that >> >> there is no such _binary operation_. But as I said, I don't >> >> understand their concern. (Except the related concern about some >> >> programming languages, not Python, where the remainder does not >> >> behave well with respect to division.) >> >> I've never come across this, and frankly I find it implausible that >> *actual* mathematicians would say that. Likely you are misunderstanding >> a technical argument about remainder being a relation rather than a >> bijunction. The argument would go something like this: > > (For 'bijunction', read 'function'.) Oops, you're right of course. It's been about 20 years since I've needed to care about the precise difference between a bijection and a function, and I made a mistake. And then to add to my shame, I also misspelt bijection. > I'm not misunderstanding any argument. There was no argument. There was > a blanket pronouncement that _in mathematics_ mod is not a binary > operator. I should learn to challenge such pronouncements and ask what > the problem is. Maybe next time. So this was *one* person making that claim? I understand that, in general, mathematicians don't have much need for a remainder function in the same way programmers do -- modulo arithmetic is far more important. But there's a world of difference between saying "In mathematics, extracting the remainder is not important enough to be given a special symbol and treated as an operator" and saying "remainder is not a binary operator". The first is reasonable; the second is not. > But you are right that I don't know how actual mathematicians these > people are. I'm not a mathematician. I don't know where to draw the > line. > > A Finnish actual mathematician stated a similar prejudice towards mod as > a binary operator in a Finnish group. I asked him what is wrong with > Knuth's definition (remainder after flooring division), and I think he > conceded that it's not wrong. Number theorists just choose to work with > congruence relations. I have no problem with that. Agreed. [...] > (There is no way to make remainder a bijection. You mean it is not a > function if it is looked at in a particular way.) You're right, of course -- remainder cannot be 1:1. I don't know what I was thinking. -- Steven From hoogendoorn.eelco at gmail.com Wed Dec 14 07:33:06 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 14 Dec 2011 04:33:06 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> On 14 dec, 12:55, Arnaud Delobelle wrote: > On 14 December 2011 07:49, Eelco wrote: > > On Dec 14, 4:18?am, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> wrote: > >> > They might not be willing to define it, but as soon as we programmers > >> > do, well, we did. > > >> > Having studied the contemporary philosophy of mathematics, their concern > >> > is probably that in their minds, mathematics is whatever some dead guy > >> > said it was, and they dont know of any dead guy ever talking about a > >> > modulus operation, so therefore it 'does not exist'. > > >> You've studied the contemporary philosophy of mathematics huh? > > >> How about studying some actual mathematics before making such absurd > >> pronouncements on the psychology of mathematicians? > > > The philosophy was just a sidehobby to the study of actual > > mathematics; and you are right, studying their works is the best way > > to get to know them. Speaking from that vantage point, I can say with > > certainty that the vast majority of mathematicians do not have a > > coherent philosophy, and they adhere to some loosely defined form of > > platonism. Indeed that is absurd in a way. Even though you may trust > > these people to be perfectly functioning deduction machines, you > > really shouldnt expect them to give sensible answers to the question > > of which are sensible axioms to adopt. They dont have a reasoned > > answer to this, they will by and large defer to authority. > > Please come down from your vantage point for a few moments and > consider how insulting your remarks are to people who have devoted > most of their intellectual energy to the study of mathematics. ?So > you've studied a bit of mathematics and a bit of philosophy? ?Good > start, keep working at it. Thanks, I intend to. > You think that every mathematician should be preoccupied with what > axioms to adopt, and why? Of course I dont. If you wish to restrict your attention to the exploration of the consequences of axioms others throw at you, that is a perfectly fine specialization. Most mathematicians do exactly that, and thats fine. But that puts them in about as ill a position to judged what is, or shouldnt be defined, as the average plumber. Compounding the problem is not just that they do not wish to concern themselves with the inductive aspect of mathematics, they would like to pretend it does not exist at all. For instance, if you point out to them a 19th century mathematician used very different axioms than a 20th century one, (and point out they were both fine mathematicians that attained results universally celebrated), they will typically respond emotionally; get angry or at least annoyed. According to their pseudo-Platonist philosophy, mathematics should not have an inductive side, axioms are set in stone and not a human affair, and the way they answer the question as to where knowledge about the 'correct' mathematical axioms comes from is by an implicit or explicit appeal to authority. They dont explain how it is that they can see 'beyond the platonic cave' to find the 'real underlying truth', they quietly assume somebody else has figured it out in the past, and leave it at that. > You say that mathematicians defer to authority, but do you really > think that thousands of years of evolution and refinement in > mathematics are to be discarded lightly? ?I think not. ?It's good to > have original ideas, to pursue them and to believe in them, but it > would be foolish to think that they are superior to knowledge which > has been accumulated over so many generations. For what its worth; insofar as my views can be pidgeonholed, im with the classicists (pre-20th century), which indeed has a long history. Modernists in turn discard large swaths of that. Note that its largely an academic debate though; everybody agrees that 1+1=2. But there are some practical consequences; if I were the designated science-Tsar, all transfinite-analysist would be out on the street together with the homeopaths, for instance. > You claim that mathematicians have a poor understanding of philosophy. > ?It may be so for many of them, but how is this a problem? ?I doesn't > prevent them from having a deep understanding of their field of > mathematics. ?Do philosophers have a good understanding of > mathematics? As a rule of thumb: absolutely not, no. I dont think I can think of any philosopher who turned his attention to mathematics that ever wrote anything interesting. All the interesting writers had their boots on mathematical ground; Quine, Brouwer, Weyl and the earlier renaissance men like Gauss and contemporaries. The fragmentation of disciplines is infact a major problem in my opinion though. Most physicists take their mathematics from the ivory- math tower, and the mathematicians shudder at the idea of listning back to see which of what they cooked up is actually anything but mental masturbation, in the meanwhile cranking out more gibberish about alephs. If any well-reasoned philosophy enters into the mix, its usually in the spare time of one of the physicists, but it is assuredly not coming out of the philosophy department. There is something quite wrong with that state of affairs. From steve+comp.lang.python at pearwood.info Wed Dec 14 07:38:31 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 12:38:31 GMT Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> Message-ID: <4ee898c6$0$29979$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Dec 2011 02:09:32 -0800, Eelco wrote: > Arguably, the most elegant thing to do is to define integer division and > remainder as a single operation; which is not only the logical thing to > do mathematically, but might work really well programmatically too. > > The semantics of python dont really allow for this though. One could > have: > > d, r = a // b That would be: >>> divmod(17, 5) (3, 2) > But it wouldnt work that well in composite expressions; selecting the > right tuple index would be messy and a more verbose form would be > preferred. However, performance-wise its also clearly the best solution, > as one often needs both output arguments and computing them > simultaniously is most efficient. Premature optimization. -- Steven From hoogendoorn.eelco at gmail.com Wed Dec 14 07:41:23 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 14 Dec 2011 04:41:23 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> Message-ID: <0dd668cd-815c-4027-9421-5fc22ed89b68@n6g2000vbg.googlegroups.com> On 14 dec, 13:22, Jussi Piitulainen wrote: > > > Is someone saying that _division_ is not defined because -42 div > > > -5 is somehow both 9 and 8? Hm, yes, I see that someone might. The > > > two operations, div and rem, need to be defined together. > > > > (There is no way to make remainder a bijection. You mean it is not > > > a function if it is looked at in a particular way.) > > > Surjection is the word you are looking for > > Um, no, I mean function. The allegedly alleged problem is that there > may be two (or more) different values for f(x,y), which makes f not a > _function_ (and the notation f(x,y) maybe inappropriate). > > Surjectivity is as much beside the point as bijectivity, but I think > we have surjectivity for rem: Z * Z -> Z if we use a definition that > produces both positive and negative remainders, or rem: Z * Z -> N if > we have non-negative remainders (and include 0 in N, which is another > bone of contention). We may or may not want to exclude 0 as the > modulus, or divisor if you like. It is at least a special case. > > It's injectivity that fails: 9 % 4 == 6 % 5 == 3 % 2, while Python > quite sensibly has (9, 4) != (6, 5) != (3, 2). (How I love the > chaining of the comparisons.) My reply was more to the statement you quoted than to yours; sorry for the confusion. Yes, we have surjectivity and not injectivity, thats all I was trying to say. > > That is, if one buys the philosophy of modernists like bourbaki in > > believing there is much to be gained by such pedantry. > > I think something is gained. Not sure I would call it philosophy. Agreed; its more the notion that one stands to gain much real knowledge by writing volumnius books about these matters that irks me, but I guess thats more a matter of taste than philosophy. From steve+comp.lang.python at pearwood.info Wed Dec 14 07:53:22 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 12:53:22 GMT Subject: Overriding a global References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> Message-ID: <4ee89c41$0$29979$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Dec 2011 13:05:19 +0100, Jean-Michel Pichavant wrote: > Bad ideas : > > i = 5 > > def spam(): > for i,v in enumerate([1,2,3,4]): > for i,v in enumerate(['a','b', 'c']): > print i, v > print i,v # bad surprise The bad surprise happens because you are using the same name twice in *one* namespace, the local scope. This example has nothing to do with local/global name clashes: the existence of global i is irrelevant. Python's scoping rules work correctly, and global i is not affected by the local i. Programming languages use multiple namespaces so that you don't need to make your variable names globally unique. There are languages that don't distinguish between local and global. Python is not one of them. The programmer should feel free to use local names without worrying too much if they accidentally use a global name. Having said that, re-using names isn't *entirely* risk free, because if you use a global name locally, and then try to *also* access the global name, you will fail. This is called shadowing, and the problem with shadowing is when you do it by accident. (Newbies are particularly prone to this, especially when they call variables "str", "list", etc.) But it is the "by accident" part that is dangerous: there is nothing wrong with shadowing globals or builtins when you do it by design. > good ideas : > > # global > nameThatWillNotBeUsedlocally = 'foo' Oh please. Names can be too long as well as too short. > def spam(): > for qtyIndex, quantity in enumerate([5,6,3,1]): > for fruitIndex, fruit in enumerate(['orange', 'banana']): > print fruitIndex, fruit > print qtyIndex, quantity More sensible naming conventions are to be encouraged, but verbose names just for the sake of verbosity is not. spam() is a five line function; if the programmer can't keep track of the meaning of loop variables i and j over five lines, perhaps they should consider a change of career and get a job more suited to their intellectual prowess. I hear McDonalds is hiring. If spam() were larger and more complex, then more expressive names would be valuable. But in the simple example you give, it just adds noise. -- Steven From rosuav at gmail.com Wed Dec 14 07:54:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Dec 2011 23:54:35 +1100 Subject: [GENERAL] Philosophical question In-Reply-To: <4EE8976F.7000204@gmx.net> References: <4EE8976F.7000204@gmx.net> Message-ID: On Wed, Dec 14, 2011 at 11:32 PM, Andreas wrote: > Hi, > > I asked elsewhere about the best way to store db credentials within a > user-session of a web-app. > > It appeared that it was for everybody but me evident that instead of heaving > a db-role+passwd for every user of an application it was better to have just > 1 set of db-credentials for the application and recreate a user management > within the app instead using the existing user handling of the dbms. > > That way the app checks the user's password as a md5 in some table and > remembers "user is logged in" for later. The actual queries would be done > with a common set of real db credentials. This is I think the most common way to do things in web apps these days. It's viable, at least; whether or not it's the best option is another question. As a side point, simply MD5'ing a user's password is sadly insufficient for proper security. (But at least you're not considering storing plain-text passwords.) If anyone managed to get hold of your users table, they'd be able to recognize any of the most common passwords. You'll want to use some kind of salt; and preferably, a newer and stronger algorithm than MD5. One simple way to do this is to concatenate the user name or numeric ID with a constant string of your own invention, and then put the password after that - so if user 'fred' signs up with password 'barney', you might hash 'fredNaClbarney', which has a SHA-1 of 2DC074250DDA7A903FE6A11B1AEC1EF0A80A0408. Without knowing that you use "NaCl" as your salt, an attacker would have some difficulty brute-forcing the passwords; and including the username means that if someone else uses the same password, the hashes will differ. That aside, your idea more or less matches up with what a large number of web sites do. ChrisA From candide at free.invalid Wed Dec 14 07:57:33 2011 From: candide at free.invalid (candide) Date: Wed, 14 Dec 2011 13:57:33 +0100 Subject: Regexp : repeated group identification In-Reply-To: References: <4ee88488$0$27871$426a74cc@news.free.fr> Message-ID: <4ee89d3d$0$7725$426a74cc@news.free.fr> Le 14/12/2011 12:34, Vlastimil Brom a ?crit : > "If a group is contained in a part of the pattern that matched > multiple times, the last match is returned." > I missed this point, your answer matches my question ;) thanks. > If you need to work with the content captured in the repeated group, > you may check the new regex implementation: > http://pypi.python.org/pypi/regex > > Which has a special "captures" method of the match object for this > (beyond many other improvements): > >>>> import regex >>>> m=regex.match('(Spam\d)+', 'Spam4Spam2Spam7Spam8') >>>> m.captures(1) > ['Spam4', 'Spam2', 'Spam7', 'Spam8'] >>>> > Thanks for the reference and the example. I didn't know of this reimplementation, hoping it offers the Aho-Corasick algorithm allowing multiple keys search. From jpiitula at ling.helsinki.fi Wed Dec 14 08:09:48 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2011 15:09:48 +0200 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <851cbabf-60b6-4806-bef5-e16f6089b08d@h37g2000pri.googlegroups.com> Message-ID: rusi writes: > On Dec 14, 1:56?pm, Jussi Piitulainen > wrote: > > > > Is someone saying that _division_ is not defined because -42 div -5 is > > somehow both 9 and 8? Hm, yes, I see that someone might. The two > > operations, div and rem, need to be defined together. > ----------------------------- > Haskell defines a quot-rem pair and a div-mod pair as follows: > (from http://www.haskell.org/onlinereport/basic.html) > > (x `quot` y)*y + (x `rem` y) == x > (x `div` y)*y + (x `mod` y) == x > > `quot` is integer division truncated toward zero, while the result of > `div` is truncated toward negative infinity. Exactly what I mean. (I gave an incorrect equation but meant this.) From jpiitula at ling.helsinki.fi Wed Dec 14 08:21:38 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2011 15:21:38 +0200 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano writes: > On Wed, 14 Dec 2011 10:56:02 +0200, Jussi Piitulainen wrote: > > I'm not misunderstanding any argument. There was no > > argument. There was a blanket pronouncement that _in mathematics_ > > mod is not a binary operator. I should learn to challenge such > > pronouncements and ask what the problem is. Maybe next time. > > So this was *one* person making that claim? I've seen it a few times from a few different posters, all on Usenet or whatever this thing is nowadays called. I think I was careful to say _some_ mathematicians, but not careful to check that any of them were actually mathematicians speaking as mathematicians. The context seems to be a cultural divide between maths and cs. Too much common ground yet very different interests? > I understand that, in general, mathematicians don't have much need > for a remainder function in the same way programmers do -- modulo > arithmetic is far more important. But there's a world of difference > between saying "In mathematics, extracting the remainder is not > important enough to be given a special symbol and treated as an > operator" and saying "remainder is not a binary operator". The first > is reasonable; the second is not. Yes. From ramapraba2653 at gmail.com Wed Dec 14 08:28:07 2011 From: ramapraba2653 at gmail.com (SUPREME) Date: Wed, 14 Dec 2011 05:28:07 -0800 (PST) Subject: HOT ACTRESS Message-ID: FOR GOOD JOBS SITES TO YOU http://goodjobssites.blogspot.com/ DEEPIKA PADUKONE SPICY WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/deepika-padukone.html KATRINA KAIF LATEST HOT WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/katrina-kaif-wallpapers.html LOVE FAILURE MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/love-failure-movie-stills.html 4FRIENDS MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/4-friends-movie-stills.html NANDEESWARUDU MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/nandeeswarudu-movie-stills.html HARI PRIYA HOT PHOTO STILLS http://actressgallery-kalyani.blogspot.com/2011/12/haripariya-actress.html SHRUTI HASSAN HOT IN 3 MOVIE http://actressgallery-kalyani.blogspot.com/2011/11/shruti-hassan-in-3-movie.html PANJA MOVIE LATEST STILLS http://actressgallery-kalyani.blogspot.com/2011/11/panja-movie-stills.html URIMI LATEST MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/11/urimi-movie-stills.html NIPPU MOVIE WORKING STILLS http://actressgallery-kalyani.blogspot.com/2011/11/nippu-movie-stills.html FOR FAST UPDATES IN FILM INDUSTRY DEEKSHA SETH HOT PHOTO STILLS http://allyouwants.blogspot.com/2011/01/deeksha-seth-hot-photo-shoot-stills.html KATRINA KAIF RARE PHOTOS http://allyouwants.blogspot.com/2011/12/katrina-kaif.html PARVATHI MELTON LATEST PHOTOSHOOT http://allyouwants.blogspot.com/2011/11/pravthi-melton-latest-photoshoot.html PANJAA FAME HOT PHOTOS http://allyouwants.blogspot.com/2011/11/panja-fame-sarah-jane-dias-hot.html NIKITHA IN HOT BLUE SAREE http://allyouwants.blogspot.com/2011/01/nikitha-hot-stills-in-blue-saree.html TRISHA LATEST PHOTO STILLS http://allyouwants.blogspot.com/2011/10/trisha-gorgeous-photos-on-jfw-magazine.html FOR ONLY HOT GUYS SEE THIS LATEST HOT KATRINA KAIF PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/katrina-kaif-hot.html ANUSHKA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/anushka-hot.html TAMANNA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/tamanna-hot.html PARUL HOT PHOTO STILLS http://hotactress-kalyani.blogspot.com/2011/12/parul-hot.html ADITI AGARWAL NEW ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/12/aditi-agarwal-hot.html PAYAL GHOSH HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/payal-ghosh-hot.html RAGINI DWIVEDI HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/12/ragini-dwivedi.html PARVATHI MELTON LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/parvathi-melton-hot.html SARAH JANE DIAS HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/sarah-jane-dias-hot.html KAJAL AGARWAL HOT SAREE STILLS http://hotactress-kalyani.blogspot.com/2011/11/kajal-agarwal-hot-in-saree.html POONAM KAUR HOT ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/11/poonam-kaur-hot.html From hoogendoorn.eelco at gmail.com Wed Dec 14 08:29:35 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 14 Dec 2011 05:29:35 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> <4ee898c6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <0d38ea3b-46ff-4fbc-9a1c-8e028c3a7d8b@n10g2000vbg.googlegroups.com> On Dec 14, 1:38?pm, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 02:09:32 -0800, Eelco wrote: > > Arguably, the most elegant thing to do is to define integer division and > > remainder as a single operation; which is not only the logical thing to > > do mathematically, but might work really well programmatically too. > > > The semantics of python dont really allow for this though. One could > > have: > > > d, r = a // b > > That would be: > > >>> divmod(17, 5) > > (3, 2) Cool; if only it were in the math module id be totally happy. > > But it wouldnt work that well in composite expressions; selecting the > > right tuple index would be messy and a more verbose form would be > > preferred. However, performance-wise its also clearly the best solution, > > as one often needs both output arguments and computing them > > simultaniously is most efficient. > > Premature optimization. We are talking language design here, not language use. Whether or not this is premature is a decision that should be left to the user, if at all possible, which in this case it very well is; just provide multiple functions to cover all use cases (only return divisor, only return remainder, or both) From jeanmichel at sequans.com Wed Dec 14 08:35:14 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 14 Dec 2011 14:35:14 +0100 Subject: Overriding a global In-Reply-To: <4ee89c41$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> <4ee89c41$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EE8A612.1010504@sequans.com> Steven D'Aprano wrote: > On Wed, 14 Dec 2011 13:05:19 +0100, Jean-Michel Pichavant wrote: > > >> Bad ideas : >> >> i = 5 >> >> def spam(): >> for i,v in enumerate([1,2,3,4]): >> for i,v in enumerate(['a','b', 'c']): >> print i, v >> print i,v # bad surprise >> > > The bad surprise happens because you are using the same name twice in > *one* namespace, the local scope. This example has nothing to do with > local/global name clashes: the existence of global i is irrelevant. > Python's scoping rules work correctly, and global i is not affected by > the local i. > > Programming languages use multiple namespaces so that you don't need to > make your variable names globally unique. There are languages that don't > distinguish between local and global. Python is not one of them. The > programmer should feel free to use local names without worrying too much > if they accidentally use a global name. > > Having said that, re-using names isn't *entirely* risk free, because if > you use a global name locally, and then try to *also* access the global > name, you will fail. This is called shadowing, and the problem with > shadowing is when you do it by accident. (Newbies are particularly prone > to this, especially when they call variables "str", "list", etc.) But it > is the "by accident" part that is dangerous: there is nothing wrong with > shadowing globals or builtins when you do it by design. > > > >> good ideas : >> >> # global >> nameThatWillNotBeUsedlocally = 'foo' >> > > Oh please. Names can be too long as well as too short. > > > >> def spam(): >> for qtyIndex, quantity in enumerate([5,6,3,1]): >> for fruitIndex, fruit in enumerate(['orange', 'banana']): >> print fruitIndex, fruit >> print qtyIndex, quantity >> > > More sensible naming conventions are to be encouraged, but verbose names > just for the sake of verbosity is not. spam() is a five line function; if > the programmer can't keep track of the meaning of loop variables i and j > over five lines, perhaps they should consider a change of career and get > a job more suited to their intellectual prowess. I hear McDonalds is > hiring. > > If spam() were larger and more complex, then more expressive names would > be valuable. But in the simple example you give, it just adds noise. > > The next time I'll illustrate meaningful names, I'll write a 3000 lines function, just to be sure no one states that my point does'nt apply to a function named spam which only counts from 1 to 3. And don't answer that the spam function above does not count from 1 to 3, I know it doesn't. For anyone interested in the actual topic, a good reading is http://tottinge.blogsome.com/meaningfulnames/#Mult_Meanings JM From vlastimil.brom at gmail.com Wed Dec 14 08:38:36 2011 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Wed, 14 Dec 2011 14:38:36 +0100 Subject: Regexp : repeated group identification In-Reply-To: <4ee89d3d$0$7725$426a74cc@news.free.fr> References: <4ee88488$0$27871$426a74cc@news.free.fr> <4ee89d3d$0$7725$426a74cc@news.free.fr> Message-ID: 2011/12/14 candide : ... > > Thanks for the reference and the example. I didn't know of this > reimplementation, hoping it offers the Aho-Corasick algorithm allowing > multiple keys search. > -- > http://mail.python.org/mailman/listinfo/python-list Hi, I am not sure about the underlying algorithm (it could as well be an internal expansion of the alternatives like ...|...|...), but you can use a list (set, actually) of alternatives to search for. check the "named lists" feature, \L<...> hth, vbr From rosuav at gmail.com Wed Dec 14 08:39:07 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Dec 2011 00:39:07 +1100 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <0d38ea3b-46ff-4fbc-9a1c-8e028c3a7d8b@n10g2000vbg.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> <4ee898c6$0$29979$c3e8da3$5496439d@news.astraweb.com> <0d38ea3b-46ff-4fbc-9a1c-8e028c3a7d8b@n10g2000vbg.googlegroups.com> Message-ID: On Thu, Dec 15, 2011 at 12:29 AM, Eelco wrote: > On Dec 14, 1:38?pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> That would be: >> >> >>> divmod(17, 5) >> >> (3, 2) > > Cool; if only it were in the math module id be totally happy. That's easily solved. import math math.divmod=divmod del __builtins__.divmod :) ChrisA From prologic at shortcircuit.net.au Wed Dec 14 08:54:52 2011 From: prologic at shortcircuit.net.au (James Mills) Date: Wed, 14 Dec 2011 05:54:52 -0800 (PST) Subject: text to html References: Message-ID: <53d45c20-26f7-475d-99a6-adf3a206470b@s10g2000prs.googlegroups.com> On Dec 14, 3:30?am, Pedro Henrique Guedes Souto wrote: > On Tue, Dec 13, 2011 at 3:22 PM, prakash jp wrote: > > Want to publish a log file as a web page, is there a parser to retain the format of the text as is and then convert to html. Please provide the relevant pointers Hey Pedro, You could also use pygments to nicely format the logfile perhaps even with Syntax Highlighting. https://bitbucket.org/freecodeteam/hpaste/src/749c36d184a6/hpaste.circuits/paste.py You could also provide an "Download" link that services the file up as Content-Type: text/plain cheers James From ian.g.kelly at gmail.com Wed Dec 14 10:45:04 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 14 Dec 2011 08:45:04 -0700 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <0d38ea3b-46ff-4fbc-9a1c-8e028c3a7d8b@n10g2000vbg.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> <4ee898c6$0$29979$c3e8da3$5496439d@news.astraweb.com> <0d38ea3b-46ff-4fbc-9a1c-8e028c3a7d8b@n10g2000vbg.googlegroups.com> Message-ID: On Wed, Dec 14, 2011 at 6:29 AM, Eelco wrote: > On Dec 14, 1:38?pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Wed, 14 Dec 2011 02:09:32 -0800, Eelco wrote: >> > Arguably, the most elegant thing to do is to define integer division and >> > remainder as a single operation; which is not only the logical thing to >> > do mathematically, but might work really well programmatically too. >> >> > The semantics of python dont really allow for this though. One could >> > have: >> >> > d, r = a // b >> >> That would be: >> >> >>> divmod(17, 5) >> >> (3, 2) > > Cool; if only it were in the math module id be totally happy. Probably it's not in math because it's not a thin wrapper around a C math library function, which is how the module was conceived. There are already some exceptions in the math module, but I think they are all newer than divmod. From dihedral88888 at googlemail.com Wed Dec 14 11:08:32 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Wed, 14 Dec 2011 08:08:32 -0800 (PST) Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> Message-ID: <26514903.334.1323878912493.JavaMail.geo-discussion-forums@pruu23> On Wednesday, December 14, 2011 4:01:24 PM UTC+8, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > > > To complement what Eric says below: The with statement is looking for an > > instance *method*, which by definition, is a function attribute of a > > *class* (the class of the context manager) that takes an instance of the > > class as its first parameter. > > I'm not sure that is correct... I don't think that there is anything "by > definition" about where methods live. Particularly not in Python where > instance methods can be attributes of the instance itself. > > >>> class Test(object): > ... def method(self): > ... print("This method is an attribute of the class.") > ... > >>> t = Test() > >>> t.method() > This method is an attribute of the class. > >>> > >>> import types > >>> t.method = types.MethodType( > ... lambda self: print( > ... "This method is an attribute of the instance."), t) > >>> t.method() > This method is an attribute of the instance. > > > So the normal lookup rules that apply to data attributes, namely > instance, then class, then superclasses, also applies to methods in > Python. In languages that don't allow that sort of thing, like Java, you > need to use convoluted design patterns like Dynamic Proxy to make it > work. In Python, you just create a method and attach it on the instance. > > http://stackoverflow.com/questions/8260740/override-a-method-for-an- > instance-of-a-class > > But this doesn't apply for special dunder attributes like __exit__, for > speed reasons. (For new-style classes only, classic classes have no such > special casing. This makes automatic delegation a breeze in Python 2 with > classic classes, and a PITA in Python 3. Boo hiss.) > > > > -- > Steven In Python an instance of an object of a class can have its own method. A living object can use those methods in the class definition and can acquire a new method at runtime. From arnodel at gmail.com Wed Dec 14 11:13:59 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Wed, 14 Dec 2011 16:13:59 +0000 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> Message-ID: On 14 December 2011 12:33, Eelco wrote: > On 14 dec, 12:55, Arnaud Delobelle wrote: >> On 14 December 2011 07:49, Eelco wrote: >> > On Dec 14, 4:18?am, Steven D'Aprano > > +comp.lang.pyt... at pearwood.info> wrote: >> >> > They might not be willing to define it, but as soon as we programmers >> >> > do, well, we did. >> >> >> > Having studied the contemporary philosophy of mathematics, their concern >> >> > is probably that in their minds, mathematics is whatever some dead guy >> >> > said it was, and they dont know of any dead guy ever talking about a >> >> > modulus operation, so therefore it 'does not exist'. >> >> >> You've studied the contemporary philosophy of mathematics huh? >> >> >> How about studying some actual mathematics before making such absurd >> >> pronouncements on the psychology of mathematicians? >> >> > The philosophy was just a sidehobby to the study of actual >> > mathematics; and you are right, studying their works is the best way >> > to get to know them. Speaking from that vantage point, I can say with >> > certainty that the vast majority of mathematicians do not have a >> > coherent philosophy, and they adhere to some loosely defined form of >> > platonism. Indeed that is absurd in a way. Even though you may trust >> > these people to be perfectly functioning deduction machines, you >> > really shouldnt expect them to give sensible answers to the question >> > of which are sensible axioms to adopt. They dont have a reasoned >> > answer to this, they will by and large defer to authority. >> >> Please come down from your vantage point for a few moments and >> consider how insulting your remarks are to people who have devoted >> most of their intellectual energy to the study of mathematics. ?So >> you've studied a bit of mathematics and a bit of philosophy? ?Good >> start, keep working at it. > > Thanks, I intend to. > >> You think that every mathematician should be preoccupied with what >> axioms to adopt, and why? > > Of course I dont. If you wish to restrict your attention to the > exploration of the consequences of axioms others throw at you, that is > a perfectly fine specialization. Most mathematicians do exactly that, > and thats fine. But that puts them in about as ill a position to > judged what is, or shouldnt be defined, as the average plumber. You are completely mistaken. Whatever the axiomatisation of the mathematics that we do, we can still do the same mathematics. We don't even need an axiomatic basis to do mathematics. In fact, the formalisation of mathematics has always come after the mathematics were well established. Euclid, Dedekind, Peano, Zermelo, Frankael, didn't create axiomatic systems out of nothing. They axiomatised pre-existing theories. Axiomatising a theory is just one way of exploring it. > Compounding the problem is not just that they do not wish to concern > themselves with the inductive aspect of mathematics, they would like > to pretend it does not exist at all. For instance, if you point out to > them a 19th century mathematician used very different axioms than a > 20th century one, (and point out they were both fine mathematicians > that attained results universally celebrated), they will typically > respond emotionally; get angry or at least annoyed. According to their > pseudo-Platonist philosophy, mathematics should not have an inductive > side, axioms are set in stone and not a human affair, and the way they > answer the question as to where knowledge about the 'correct' > mathematical axioms comes from is by an implicit or explicit appeal to > authority. They dont explain how it is that they can see 'beyond the > platonic cave' to find the 'real underlying truth', they quietly > assume somebody else has figured it out in the past, and leave it at > that. Again, you are completely mis-representing the situation. In my experience, most mathematicians (I'm not talking about undergraduate students here) do not see the axioms are the root of the mathematics that they do. Formal systems are just one way to explore mathematics. Of course they can in some cases be very useful and enlightening. As for inductive reasoning, I really can't understand your point. Of course mathematicians use inductive reasoning all the time. Where do you think the Riemann Hypothesis comes from? Or Fermat's last theorem? Do you think that mathematicians prove results before they even think about them? On the other hand, a result needs to be proved to be accepted by the mathematical community, and inductive reasoning is not valid in proofs. That's in the nature of mathematics. >> You say that mathematicians defer to authority, but do you really >> think that thousands of years of evolution and refinement in >> mathematics are to be discarded lightly? ?I think not. ?It's good to >> have original ideas, to pursue them and to believe in them, but it >> would be foolish to think that they are superior to knowledge which >> has been accumulated over so many generations. > > For what its worth; insofar as my views can be pidgeonholed, im with > the classicists (pre-20th century), which indeed has a long history. > Modernists in turn discard large swaths of that. Note that its largely > an academic debate though; everybody agrees that 1+1=2. But there are > some practical consequences; if I were the designated science-Tsar, > all transfinite-analysist would be out on the street together with the > homeopaths, for instance. It's telling that on the one hand you criticise mathematicians for not questioning the "axioms which are thrown at them", on the other hand you feel able to discard a perfectly fine piece of mathematics, that of the study of transfinite numbers, because it doesn't fit nicely with traditional views. The fact is that at the end of the 19th century mathematics had reached a crisis point. >> You claim that mathematicians have a poor understanding of philosophy. >> ?It may be so for many of them, but how is this a problem? ?I doesn't >> prevent them from having a deep understanding of their field of >> mathematics. ?Do philosophers have a good understanding of >> mathematics? > > As a rule of thumb: absolutely not, no. I dont think I can think of > any philosopher who turned his attention to mathematics that ever > wrote anything interesting. All the interesting writers had their > boots on mathematical ground; Quine, Brouwer, Weyl and the earlier > renaissance men like Gauss and contemporaries. > > The fragmentation of disciplines is infact a major problem in my > opinion though. Most physicists take their mathematics from the ivory- > math tower, and the mathematicians shudder at the idea of listning > back to see which of what they cooked up is actually anything but > mental masturbation, in the meanwhile cranking out more gibberish > about alephs. Only a minority of mathematicians have an interest in "alephs", as you call them. IMHO, the mathematics they do is perfectly valid. The exploration of the continuum hypothesis has led to the creation of very powerful mathematical techniques and gives an insight into the very foundations of mathematics. Again, on the one hand you criticise mathematicians for not questioning the axioms they work with, but those who investigate the way these axioms interact you accuse of "mental masturbation". -- Arnaud From ian.g.kelly at gmail.com Wed Dec 14 11:27:35 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 14 Dec 2011 09:27:35 -0700 Subject: Property Abuse In-Reply-To: References: Message-ID: On Wed, Dec 14, 2011 at 1:28 AM, Felipe O wrote: > Hi All, > I was wondering what everyone's thought process was regarding properties. > Lately I find I've been binging on them and have classes with > 10 > properties. While pylint doesn't complain (yet), it tends to be picky about > keeping instance attribute counts low, so I figure there's something against > that. How do you guys decide between using properties versus getter methods, > or how do you refactor them if neither? I prefer direct instance attribute access where possible*, properties where necessary, and methods where an argument is needed or the relationship is more complex than get/set/delete. * One of the strengths of Python's property system** is that you can switch between plain attributes and mutable properties as needed without breaking dependent code. Often I see people doing this, which drives me nuts with its useless verbosity, when a plain instance attribute would have sufficed: @property def foo(self): return self._foo @foo.setter def foo(self, value): self._foo = value ** As opposed, for instance, to the .NET property system. You can't arbitrarily switch between public member variables and public properties in .NET, because it breaks ABI. Cheers, Ian From dihedral88888 at googlemail.com Wed Dec 14 11:28:57 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Wed, 14 Dec 2011 08:28:57 -0800 (PST) Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <26514903.334.1323878912493.JavaMail.geo-discussion-forums@pruu23> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <26514903.334.1323878912493.JavaMail.geo-discussion-forums@pruu23> Message-ID: <13752584.332.1323880138063.JavaMail.geo-discussion-forums@pruu5> On Thursday, December 15, 2011 12:08:32 AM UTC+8, 88888 Dihedral wrote: > On Wednesday, December 14, 2011 4:01:24 PM UTC+8, Steven D'Aprano wrote: > > On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > > > > > To complement what Eric says below: The with statement is looking for an > > > instance *method*, which by definition, is a function attribute of a > > > *class* (the class of the context manager) that takes an instance of the > > > class as its first parameter. > > > > I'm not sure that is correct... I don't think that there is anything "by > > definition" about where methods live. Particularly not in Python where > > instance methods can be attributes of the instance itself. > > > > >>> class Test(object): > > ... def method(self): > > ... print("This method is an attribute of the class.") > > ... > > >>> t = Test() > > >>> t.method() > > This method is an attribute of the class. > > >>> > > >>> import types > > >>> t.method = types.MethodType( > > ... lambda self: print( > > ... "This method is an attribute of the instance."), t) > > >>> t.method() > > This method is an attribute of the instance. > > > > > > So the normal lookup rules that apply to data attributes, namely > > instance, then class, then superclasses, also applies to methods in > > Python. In languages that don't allow that sort of thing, like Java, you > > need to use convoluted design patterns like Dynamic Proxy to make it > > work. In Python, you just create a method and attach it on the instance. > > > > http://stackoverflow.com/questions/8260740/override-a-method-for-an- > > instance-of-a-class > > > > But this doesn't apply for special dunder attributes like __exit__, for > > speed reasons. (For new-style classes only, classic classes have no such > > special casing. This makes automatic delegation a breeze in Python 2 with > > classic classes, and a PITA in Python 3. Boo hiss.) > > > > > > > > -- > > Steven > > In Python an instance of an object of a class can have its own method. > A living object can use those methods in the class definition and > can acquire a new method at runtime. Therefore, it is possible for an object to build its decision tree of actions toward a problem of various parameters in the run time. From jeanmichel at sequans.com Wed Dec 14 11:37:15 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 14 Dec 2011 17:37:15 +0100 Subject: Property Abuse In-Reply-To: References: Message-ID: <4EE8D0BB.6020004@sequans.com> Ian Kelly wrote: > On Wed, Dec 14, 2011 at 1:28 AM, Felipe O wrote: > >> Hi All, >> I was wondering what everyone's thought process was regarding properties. >> Lately I find I've been binging on them and have classes with > 10 >> properties. While pylint doesn't complain (yet), it tends to be picky about >> keeping instance attribute counts low, so I figure there's something against >> that. How do you guys decide between using properties versus getter methods, >> or how do you refactor them if neither? >> > > I prefer direct instance attribute access where possible*, properties > where necessary, and methods where an argument is needed or the > relationship is more complex than get/set/delete. > > * One of the strengths of Python's property system** is that you can > switch between plain attributes and mutable properties as needed > without breaking dependent code. Often I see people doing this, which > drives me nuts with its useless verbosity, when a plain instance > attribute would have sufficed: > > @property > def foo(self): > return self._foo > > @foo.setter > def foo(self, value): > self._foo = value > > ** As opposed, for instance, to the .NET property system. You can't > arbitrarily switch between public member variables and public > properties in .NET, because it breaks ABI. > > Cheers, > Ian > I second this opinion, plain attributes are what's required most of the time. JM From joshua.landau.ws at gmail.com Wed Dec 14 11:54:09 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 14 Dec 2011 16:54:09 +0000 Subject: Overriding a global In-Reply-To: <4EE8771D.1050902@sequans.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> Message-ID: On 14 December 2011 10:14, Jean-Michel Pichavant wrote: > Joshua Landau wrote: > > On 13 December 2011 13:30, Jean-Michel Pichavant > jeanmichel at sequans.com**>> wrote: >> >> writing >> >> x = 1 >> >> def spam(): >> x = 2 >> >> is in general a bad idea. That was my point. >> >> >> Why? I have a few (probably wrong) guesses. >> >> Because you expect it to be the same every time you use it? >> Well, then this should be "in general a bad idea": >> x = 1; print(x); x = 2; print(x) >> > you're changing the value of x, that's fine. In the example above, the > assignement is not the problem. The problem is that you create 2 different > 'x', one in globals(), and one in locals(). Using the same name within 2 > implicit namespaces is a bad idead in general because it can be difficult > to know which one is used. > > If you want to have fun, try this code, prepared to be amazed. There's > something funny with the global statement, it's applied on the whole block > no matter where it is stated in the block. > x=1 # global > > def spam(): > x = 2 # local (or so you may think) > print x > global x # I need to use the global one now > print x > print locals() > :3: SyntaxWarning: name 'x' is assigned to before global declaration I think it's a null point. We're not talking about "globals" being wrong (or was this poor design) for efficiency reasons. There's an error there for reason. > For more fun you could create a 'x' name in __builtin__ and import it so > that people never know which x you are using. > > Even though it makes total sense to me. >> >> Is it because it's used to different purpose between similarly-looking >> functions? >> This looks fine, though: >> def func1(): x=1; print(x) >> def func2(): x=2; print(x) >> >> Is it because it looks like a reassignment of the more global x? >> I don't have an example here but, simply put, I don't believe this. We >> can use "id" as our own local variable without thinking that we're >> tampering with "__builtins__.id". I don't see it as much of a leap from >> builtin to global (except that you /*can*/ do "dir = 1; del dir; dir" >> without error). >> >> >> That said, I'm sorta' just guessing the reason you might think it's a bad >> idea. >> > > The problem makes little sense when using names like x or func1. Besides > namespace issues, naming 2 *different objects* with the same meaningful > name is usually a bad idea and points the fact that your names are no that > meaningful. To go back to the original post, having a 'logger' that may > name 2 different logger object during the execution is a bad idea. One > quick way to fix it is to name the logger 'currentLogger', this way you > warn the reader that the logger named by curentLogger may change over time. > > As someone sugggested in this thread one other option is to use a > different name for the second logger. So the only problem is that "x" isn't meaningful? def countToTen(): for number in range(1, 10): print(number) def countToTwenty(): for number in range(1, 20): print(number) .. Looks fine to me. Using currentLogger is just padding, in my opinion. *Every *value is "current". That changes nothing. As long as you never infer that "logger" is static or totally global (no "global logger" in global namespace or terming it "LOGGER") I would never assume as much, as with every other variable I see. Unless I see "global " in a function, I need to hold the belief that can change. In regards to a second name - yes this could work and in many cases would be desirable, but it doesn't really help this circumstance. [assume, for a moment, a lot of functions used a local logger] "localLogger" would tell you very little about what the logger actually *is*. You still have to look that up. [end assumption] Additionally, this would make changing a logger that uses the default to a local one *much* harder. And don't say "but you can just always make a local copy", as then you lose the advantage of a global. Typing something like "logger = childLogger(id)" to the start of a function call *is explicit*, it's clean, and it makes sense to have a default that's global. You're not appending cruft ("current") and you have consistency. If you added "logger = globalLogger" to every function start as well you can argue that it's better. I agree it's more explicit. But then you lose unneeded if only a small portion of your code localises logger. But I would recommend it if a large portion of code used local variants. AND: > The next time I'll illustrate meaningful names, I'll write a 3000 lines > function, just to be sure no one states that my point does'nt apply to a > function named spam which only counts from 1 to 3. > And don't answer that the spam function above does not count from 1 to 3, > I know it doesn't. You're acting in sarcasm to a comment on scale, when you yourself said that one of my comments was invalid due to names that were scaled down for exampling. It seems a bit hypocritical to me. That said, not all functions are long. If the short ones use short names that's fine: I'm pretty sure you said it's not. And in regards to the link: 1) __add__ says otherwise (technically, the operator "+"). It's rarely confused me. 2) That's not what we're discussing. As it said: "As long as the parameter lists are semantically equal and the desired result is the same, all is well." They're doing semantically the same thing (to different log levels) with the same parameter lists and they're not class methods. You /could/ say that the semantics are different, but classes act in a context in the same way local variables can be thought of doing, and semantics are the same for them. Instead of a different self, it's a different log file/level. Same semantics. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ericsnowcurrently at gmail.com Wed Dec 14 11:56:10 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Wed, 14 Dec 2011 09:56:10 -0700 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 11:05 PM, Eric Snow wrote: > On Tue, Dec 13, 2011 at 10:42 PM, Steve Howell wrote: >> I'm using Python 3.2.2, and the following program gives me an error >> that I don't understand: >> >> class Foo: >> ?pass >> >> foo = Foo() >> foo.name = "Steve" >> >> def add_goodbye_function(obj): >> ?def goodbye(): >> ? ?print("goodbye " + obj.name) >> ?obj.goodbye = goodbye >> >> add_goodbye_function(foo) >> foo.goodbye() # outputs goodbye Steve >> foo.__exit__ = foo.goodbye >> foo.__exit__() # outputs goodbye Steve >> >> with foo: # fails with AttributeError: ?__exit__ >> ?print("doing stuff") >> >> I am dynamically adding an attribute __exit__ to the variable foo, >> which works fine when I call it directly, but it fails when I try to >> use foo as the expression in the with statement. ?Here is the full >> output: >> >>> python3 with.coffee >> goodbye Steve >> goodbye Steve >> Traceback (most recent call last): >> ?File "with.coffee", line 17, in >> ? ?with foo: # fails with AttributeError: >> AttributeError: __exit__ >> >> What am I doing wrong? > > That is a tricky one. > > As with many of the special methods (start and end with __) in Python, > the underlying mechanism in the interpreter is directly pulling the > function from the class object. ?It does not look to the instance > object for the function at any time. ?See > http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes. > > -eric Incidently, if you add the function directly to the class, everything works out: class Foo(object): # or "class Foo:" under Python 3 pass foo = Foo() foo.name = "Steve" def add_goodbye_function(cls): def goodbye(self, *args, **kwargs): print("goodbye " + self.name) cls.goodbye = goodbye add_goodbye_function(type(foo)) foo.goodbye() # outputs goodbye Steve Foo.__exit__ = foo.goodbye foo.__exit__() # outputs goodbye Steve Foo.__enter__ = (lambda self: None) with foo: print("doing stuff") However, perhaps a better approach would be to put the different pieces directly into the class: class Foo: # python 3 def __init__(self, name): self.name = name def goodbye(self): print("goodbye " + self.name) def __enter__(self): pass def __exit__(self, *args, **kwargs): self.goodbye() foo = Foo("Steve") foo.goodbye() # outputs goodbye Steve foo.__exit__() # outputs goodbye Steve with foo: print("doing stuff") If you want to be more dynamic about it you can do it, but it involves black magic. Chances are really good that being explicit through your class definition is the right approach. -eric From jeanmichel at sequans.com Wed Dec 14 12:06:19 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 14 Dec 2011 18:06:19 +0100 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> Message-ID: <4EE8D78B.108@sequans.com> Joshua Landau wrote: > [snip] > Using currentLogger is just padding, in my opinion. *Every *value is > "current". Not always. I try to keep names on the same object because that object is supposed to be named that way. I can change one of the object attribute, but the object named that way keep being the same. Class Foo: self.__init__(self): self.banana = 5 myFoo = Foo() Now there's a slight difference between myFoo = Exception() and myFoo.banana = 4 The first statement rebind myFoo to something complitely different. the second statement change one of the object rightfully named myFoo .. attribute (not sure about this construct :D ) Int being inmutable, you can rebind a name without changing its meaning. > > In regards to a second name - yes this could work and in many cases > would be desirable, but it doesn't really help this circumstance. > [assume, for a moment, a lot of functions used a local logger] > "localLogger" would tell you very little about what the logger > actually /is/. You still have to look that up. [end assumption] > Additionally, this would make changing a logger that uses the default > to a local one /much/ harder. And don't say "but you can just always > make a local copy", as then you lose the advantage of a global. > > Typing something like "logger = childLogger(id)" to the start of a > function call *is explicit*, it's clean, and it makes sense to have a > default that's global. You're not appending cruft ("current") and you > have consistency. If you added "logger = globalLogger" to every > function start as well you can argue that it's better. I agree it's > more explicit. But then you lose unneeded if only a small portion of > your code localises logger. But I would recommend it if a large > portion of code used local variants. > > AND: > > The next time I'll illustrate meaningful names, I'll write a 3000 > lines function, just to be sure no one states that my point > does'nt apply to a function named spam which only counts from 1 to 3. > And don't answer that the spam function above does not count from > 1 to 3, I know it doesn't. > > > You're acting in sarcasm to a comment on scale, when you yourself said > that one of my comments was invalid due to names that were scaled down > for exampling. It seems a bit hypocritical to me. That said, not all > functions are long. If the short ones use short names that's fine: I'm > pretty sure you said it's not. > > And in regards to the link: > 1) __add__ says otherwise (technically, the operator "+"). It's rarely > confused me. > 2) That's not what we're discussing. As it said: "As long as the > parameter lists are semantically equal and the desired result is the > same, all is well." They're doing semantically the same thing (to > different log levels) with the same parameter lists and they're not > class methods. You /could/ say that the semantics are different, but > classes act in a context in the same way local variables can be > thought of doing, and semantics are the same for them. Instead of a > different self, it's a different log file/level. Same semantics. I'd like to argue about that but I won't cause I have the feeling my lack of ultra precise english would cause me more trouble. Note that I'm not blaming anyone but me, no sarcasm inside. JM From hoogendoorn.eelco at gmail.com Wed Dec 14 12:15:47 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 14 Dec 2011 09:15:47 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> Message-ID: <91f6f2ec-25ac-4ca8-b654-f605a7c8a53e@c13g2000vbh.googlegroups.com> 'Kindof' off-topic, but what the hell :). On Dec 14, 5:13?pm, Arnaud Delobelle wrote: > On 14 December 2011 12:33, Eelco wrote: > > On 14 dec, 12:55, Arnaud Delobelle wrote: > >> On 14 December 2011 07:49, Eelco wrote: > >> > On Dec 14, 4:18?am, Steven D'Aprano >> > +comp.lang.pyt... at pearwood.info> wrote: > >> >> > They might not be willing to define it, but as soon as we programmers > >> >> > do, well, we did. > > >> >> > Having studied the contemporary philosophy of mathematics, their concern > >> >> > is probably that in their minds, mathematics is whatever some dead guy > >> >> > said it was, and they dont know of any dead guy ever talking about a > >> >> > modulus operation, so therefore it 'does not exist'. > > >> >> You've studied the contemporary philosophy of mathematics huh? > > >> >> How about studying some actual mathematics before making such absurd > >> >> pronouncements on the psychology of mathematicians? > > >> > The philosophy was just a sidehobby to the study of actual > >> > mathematics; and you are right, studying their works is the best way > >> > to get to know them. Speaking from that vantage point, I can say with > >> > certainty that the vast majority of mathematicians do not have a > >> > coherent philosophy, and they adhere to some loosely defined form of > >> > platonism. Indeed that is absurd in a way. Even though you may trust > >> > these people to be perfectly functioning deduction machines, you > >> > really shouldnt expect them to give sensible answers to the question > >> > of which are sensible axioms to adopt. They dont have a reasoned > >> > answer to this, they will by and large defer to authority. > > >> Please come down from your vantage point for a few moments and > >> consider how insulting your remarks are to people who have devoted > >> most of their intellectual energy to the study of mathematics. ?So > >> you've studied a bit of mathematics and a bit of philosophy? ?Good > >> start, keep working at it. > > > Thanks, I intend to. > > >> You think that every mathematician should be preoccupied with what > >> axioms to adopt, and why? > > > Of course I dont. If you wish to restrict your attention to the > > exploration of the consequences of axioms others throw at you, that is > > a perfectly fine specialization. Most mathematicians do exactly that, > > and thats fine. But that puts them in about as ill a position to > > judged what is, or shouldnt be defined, as the average plumber. > > You are completely mistaken. ?Whatever the axiomatisation of the > mathematics that we do, we can still do the same mathematics. ?We > don't even need an axiomatic basis to do mathematics. ?In fact, the > formalisation of mathematics has always come after the mathematics > were well established. ? ?Euclid, Dedekind, Peano, Zermelo, Frankael, > didn't create axiomatic systems out of nothing. ?They axiomatised > pre-existing theories. > > Axiomatising a theory is just one way of exploring it. Yes, axiomization is to some extent a side-show. We know what it is that we want mathematics to be, and we try to find the axioms that lead to those conclusions. Not qualitatively different from any other form of induction (of the epistemological rather than mathematical kind). Still, different axioms or meta-mathematics give subtly different results, not to mention are as different to work with as assembler and haskell. There are no alephs if you start from a constructive basis, for instance. Im not sure what 'Axiomatising a theory is just one way of exploring it' means. One does not axiomatize a single theory; that would be trivial (A is true because thats what I define A to be). One constructs a single set of axioms from which a nontrivial set of theorems follow. The way id put it, is that axiomazation is about being explicit in what it is that you assume, trying to minimalize that, and being systematic about what conclusions that forces you to embrace. Could you be more precise as to how I am 'completely mistaken'? I acknowledge that my views are outside the mainstream, so its no news to me many would think so, but it would be nice to know what im arguing against in this thread precisely. > > Compounding the problem is not just that they do not wish to concern > > themselves with the inductive aspect of mathematics, they would like > > to pretend it does not exist at all. For instance, if you point out to > > them a 19th century mathematician used very different axioms than a > > 20th century one, (and point out they were both fine mathematicians > > that attained results universally celebrated), they will typically > > respond emotionally; get angry or at least annoyed. According to their > > pseudo-Platonist philosophy, mathematics should not have an inductive > > side, axioms are set in stone and not a human affair, and the way they > > answer the question as to where knowledge about the 'correct' > > mathematical axioms comes from is by an implicit or explicit appeal to > > authority. They dont explain how it is that they can see 'beyond the > > platonic cave' to find the 'real underlying truth', they quietly > > assume somebody else has figured it out in the past, and leave it at > > that. > > Again, you are completely mis-representing the situation. ?In my > experience, most mathematicians (I'm not talking about undergraduate > students here) do not see the axioms are the root of the mathematics > that they do. ?Formal systems are just one way to explore mathematics. > ?Of course they can in some cases be very useful and enlightening. Its your word versus mine I suppose. > As for inductive reasoning, I really can't understand your point. ?Of > course mathematicians use inductive reasoning all the time. ?Where do > you think the Riemann Hypothesis comes from? Or Fermat's last theorem? > ?Do you think that mathematicians prove results before they even think > about them? ?On the other hand, a result needs to be proved to be > accepted by the mathematical community, and inductive reasoning is not > valid in proofs. ?That's in the nature of mathematics. We mean something different by the term it seems. What you describe, I would call intuition. Which is indeed very important in mathematics, and indeed no substitute for deduction. By induction, I mean the process of reducing particular facts/observations/theorems to a more compact body of theory/axioms that imply the same. In a way, its the inverse of deduction (seeing which body of conclusions follows from a given set of axioms) > >> You say that mathematicians defer to authority, but do you really > >> think that thousands of years of evolution and refinement in > >> mathematics are to be discarded lightly? ?I think not. ?It's good to > >> have original ideas, to pursue them and to believe in them, but it > >> would be foolish to think that they are superior to knowledge which > >> has been accumulated over so many generations. > > > For what its worth; insofar as my views can be pidgeonholed, im with > > the classicists (pre-20th century), which indeed has a long history. > > Modernists in turn discard large swaths of that. Note that its largely > > an academic debate though; everybody agrees that 1+1=2. But there are > > some practical consequences; if I were the designated science-Tsar, > > all transfinite-analysist would be out on the street together with the > > homeopaths, for instance. > > It's telling that on the one hand you criticise mathematicians for not > questioning the "axioms which are thrown at them", on the other hand > you feel able to discard a perfectly fine piece of mathematics, that > of the study of transfinite numbers, because it doesn't fit nicely > with traditional views. ?The fact is that at the end of the 19th > century mathematics had reached a crisis point. It is a shame you both fail to specify by what metric it is a perfectly fine piece of mathematics, and yet more egregiously, put words into my mouth as to what I think is wrong with it. That makes for slow and painful debating. My objection to transfinite analysis is that it is not scientific, in the sense that I judge most parts of mathematics to be. I am not questioning its deductive validity; that something mathematicians can generally be trusted with. My contention is that the axioms that give rise to transfinite analysis are of the same 'validity' as any random set of axioms you could pull from a random number generator. All sets of axioms have implications, but we dont study all possible sets of axioms. Studying a random set of axioms leads to an arbitrary number of nonsensical results; nonsensical in the common day useage of the word, and nonsensical in a philosophical sense; as not relating to any sense-impressions, or synthetic propositions. Transfinite analysis does not give any results of any relevance that im aware of, but id love to be proven wrong. The fact that we do get this cancerous outgrowth of implications called transfinite analysis is a hint that these axioms are borked; not a beautiful view on a world of truth beyond our senses that only mathematics can give us. (again, in my minority opinion). Id love to debate you as to where exactly I suspect things went wrong, but its a lengthy story, and its really not the right place I suppose; nor the right time, I have to cook. > >> You claim that mathematicians have a poor understanding of philosophy. > >> ?It may be so for many of them, but how is this a problem? ?I doesn't > >> prevent them from having a deep understanding of their field of > >> mathematics. ?Do philosophers have a good understanding of > >> mathematics? > > > As a rule of thumb: absolutely not, no. I dont think I can think of > > any philosopher who turned his attention to mathematics that ever > > wrote anything interesting. All the interesting writers had their > > boots on mathematical ground; Quine, Brouwer, Weyl and the earlier > > renaissance men like Gauss and contemporaries. > > > The fragmentation of disciplines is infact a major problem in my > > opinion though. Most physicists take their mathematics from the ivory- > > math tower, and the mathematicians shudder at the idea of listning > > back to see which of what they cooked up is actually anything but > > mental masturbation, in the meanwhile cranking out more gibberish > > about alephs. > > Only a minority of mathematicians have an interest in "alephs", as you > call them. I know. > IMHO, the mathematics they do is perfectly valid. ?The > exploration of the continuum hypothesis has led to the creation of > very powerful mathematical techniques and gives an insight into the > very foundations of mathematics. Like I said, I dont question its deductive validity. As for providing insight into the deductive process; probably, but so do Sudoku's, and I dont see them being state-sponsored across the globe. Any self- created puzzle will do for that purpose; and I suspect the same time spent on real puzzles has the same effect, plus more. > Again, on the one hand you criticise > mathematicians for not questioning the axioms they work with, but > those who investigate the way these axioms interact you accuse of > "mental masturbation". In my, admittedly outsider view of things, transfinite analysis are rather sad deduction machines. Id be delighted if you could show me one that has done some kind of reflection as to why they so fervently keep chasing the ghosts that the likes of Hilbert and Russel conjured up for them, or that have even bothered to expose themselves to the mockery that the likes of Gauss would have showered upon them (or Feynmann, for a more recent but imperfect analog). From showell30 at yahoo.com Wed Dec 14 12:16:43 2011 From: showell30 at yahoo.com (Steve Howell) Date: Wed, 14 Dec 2011 09:16:43 -0800 (PST) Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> Message-ID: <9440c6ea-f844-460a-ab88-a8890648c70e@t38g2000prg.googlegroups.com> On Dec 14, 12:01?am, Steven D'Aprano wrote: > [...] > > So the normal lookup rules that apply to data attributes, namely > instance, then class, then superclasses, also applies to methods in > Python. In languages that don't allow that sort of thing, like Java, you > need to use convoluted design patterns like Dynamic Proxy to make it > work. In Python, you just create a method and attach it on the instance. > > http://stackoverflow.com/questions/8260740/override-a-method-for-an- > instance-of-a-class > > But this doesn't apply for special dunder attributes like __exit__, for > speed reasons. (For new-style classes only, classic classes have no such > special casing. This makes automatic delegation a breeze in Python 2 with > classic classes, and a PITA in Python 3. Boo hiss.) Thanks to all who responded. Basically, I think the special casing of the "dunder" attributes threw me off. Typically, I would just build context managers from a class, but I wanted to experiment with instances that were built up without the standard Python class mechanisms, instead following a Javascript-like closure-based object creation model. This is what I came up with: class WithWrapper: def __init__(self, obj): self.obj = obj def __enter__(self): self.obj['enter']() def __exit__(self, *args): self.obj['exit'](*args) def greeter_context(hello, goodbye): return { 'enter': lambda: print("---\n" + hello), 'exit': lambda *args: print(goodbye) } gc_french = greeter_context("salut", "a plus tard") with WithWrapper(gc_french): print("doing stuff") gc_slang = greeter_context("yo", "later") with WithWrapper(gc_slang): print("doing stuff") From as at sci.fi Wed Dec 14 13:40:28 2011 From: as at sci.fi (Anssi Saari) Date: Wed, 14 Dec 2011 20:40:28 +0200 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: Christian Heimes writes: >> Mea culpa, forgot that. Yes, use altinstall. Although it's probably >> not a problem to replace 2.6.6 with 2.7.2 - I doubt that'll break many >> things. > > Except that all 3rd party extensions and packages are missing if you > install Python manually. True, they would have to be built or at least installed manually also. Major work, especially for a beginner. Shouldn't pick Debian Stable and then want current software... Then again, even the standard install of Python has plenty of stuff. I installed 2.7.2 on my Debian system just to try out some of the new Tk stuff. > Debian's backports should provide a well integrated Python 2.7 > package. But it doesn't. Python 2.7.2 is in Wheezy, which is the current testing version of Debian. Looks like it has about 700 release critical bugs, so it'll be a while until release. From cosmo_general at yahoo.com Wed Dec 14 13:47:24 2011 From: cosmo_general at yahoo.com (Muddy Coder) Date: Wed, 14 Dec 2011 10:47:24 -0800 (PST) Subject: What is this widget? Message-ID: Hi Folks, I am trying to write letters on a photo that is opened in a canvas. So I think I must need a widget to contain the letters I will type in. I tried to use a Label, it worked. But, a Label covered part of the photo underneath, so I can't use it. I saw some software did such a thing nicely: a box popped on a photo, with dotted lines as borders, expandable. When such a box was re-sized with a mouse, the font size in the box also got changed. The box has no background color, so it does not cover a rectangle area on the photo. I need such a widget, but I don't know what is this one. I tried Text, Label, but they all come with Window-like stuff, so they cover some photo content. Can somebody points me a direction? Thanks! Cosmo From lie.1296 at gmail.com Wed Dec 14 14:14:19 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 15 Dec 2011 06:14:19 +1100 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: Message-ID: On 12/15/2011 03:56 AM, Eric Snow wrote: > On Tue, Dec 13, 2011 at 11:05 PM, Eric Snow wrote: > > If you want to be more dynamic about it you can do it, but it involves > black magic. Chances are really good that being explicit through your > class definition is the right approach. Note that the black spice is to use the __class__ attribute: foo.__class__.__exit__ = foo.goodbye From ericsnowcurrently at gmail.com Wed Dec 14 14:46:58 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Wed, 14 Dec 2011 12:46:58 -0700 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: Message-ID: On Wed, Dec 14, 2011 at 12:14 PM, Lie Ryan wrote: > On 12/15/2011 03:56 AM, Eric Snow wrote: >> >> On Tue, Dec 13, 2011 at 11:05 PM, Eric Snow >> ?wrote: >> >> If you want to be more dynamic about it you can do it, but it involves >> black magic. ?Chances are really good that being explicit through your >> class definition is the right approach. > > > Note that the black spice is to use the __class__ attribute: > > foo.__class__.__exit__ = foo.goodbye Good point. 'type(foo).__exit__ ...' might be even better. Regardless, to get the dynamicism to which Steve originally referred (make it work when added directly to the instance), you have to use a metaclass, which is black magic[1]. However, rarely is that sort of thing needed, so the relatively superfluous details would likely only cloud the question at hand. -eric [1] I will point out that metaclasses aren't really all that bad. They are a tool worth understanding, even if you don't use them all the time. Understanding them also opens up a whole new world of understanding how Python works, particularly regarding name lookup. From d at davea.name Wed Dec 14 14:48:52 2011 From: d at davea.name (Dave Angel) Date: Wed, 14 Dec 2011 14:48:52 -0500 Subject: What is this widget? In-Reply-To: References: Message-ID: <4EE8FDA4.10704@davea.name> On 12/14/2011 01:47 PM, Muddy Coder wrote: > Hi Folks, > > I am trying to write letters on a photo that is opened in a canvas. So > I think I must need a widget to contain the letters I will type in. I > tried to use a Label, it worked. But, a Label covered part of the > photo underneath, so I can't use it. I saw some software did such a > thing nicely: a box popped on a photo, with dotted lines as borders, > expandable. When such a box was re-sized with a mouse, the font size > in the box also got changed. The box has no background color, so it > does not cover a rectangle area on the photo. I need such a widget, > but I don't know what is this one. I tried Text, Label, but they all > come with Window-like stuff, so they cover some photo content. Can > somebody points me a direction? Thanks! > > > Cosmo I don't have an answer, but it'd be much easier for others if you specified what gui toolkit you're using, and probably also the OS and Python versions. -- DaveA From nukeymusic at gmail.com Wed Dec 14 15:32:52 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Wed, 14 Dec 2011 12:32:52 -0800 (PST) Subject: replacing timestamps in file [newbie] Message-ID: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> I have a file which has the data in the following format: Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 . . the first field is a timestamp, I'd like to replace it with the time in seconds starting from the first one like this: 0 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 27 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 54 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 Is there a function in python to achieve this? thanks in advance nukey From miki.tebeka at gmail.com Wed Dec 14 15:56:01 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 14 Dec 2011 12:56:01 -0800 (PST) Subject: replacing timestamps in file [newbie] In-Reply-To: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> References: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> Message-ID: <22395461.354.1323896161063.JavaMail.geo-discussion-forums@prez15> You can either work with datetime module (see datetime.datetime.strptime) or with the time module (see time.strptime and time.mktime) From tjreedy at udel.edu Wed Dec 14 15:57:40 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Dec 2011 15:57:40 -0500 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> Message-ID: On 12/14/2011 5:09 AM, Eelco wrote: > Arguably, the most elegant thing to do is to define integer division > and remainder as a single operation; It actually is, as quotient and remainder are calculated together. The microprocessors I know of expose this (as does Python). 'a divmod b' puts the quotient in one register and the remainder in another. If you ask for just one of the two values, both are calculated and one is grabbed while the other is returned. > which is not only the logical > thing to do mathematically, but might work really well > programmatically too. > > The semantics of python dont really allow for this though. One could > have: > > d, r = a // b >>> a,b = divmod(10,3) >>> a,b (3, 1) With CPython, int.__divmod__ lightly wraps and exposes the processor operation. > But it wouldnt work that well in composite expressions; selecting the > right tuple index would be messy and a more verbose form would be > preferred. That is why we have >>> a == 10 // 3 True >>> b == 10 % 3 True In both cases, I believe CPython calls int.__divmod__ (or the lower level equivalent) to calculate both values, and one is returned while the other is ignored. It it the same when one does long division by hand. > However, performance-wise its also clearly the best > solution, as one often needs both output arguments and computing them > simultaniously is most efficient. As indicated above, there is really no choice but to calculate both at once. If one needs both a//b and a%b, one should explicitly call divmod once and save (name) both values, instead of calling it implicitly twice and tossing half the answer each time. -- Terry Jan Reedy From rosuav at gmail.com Wed Dec 14 16:07:10 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Dec 2011 08:07:10 +1100 Subject: replacing timestamps in file [newbie] In-Reply-To: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> References: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> Message-ID: On Thu, Dec 15, 2011 at 7:32 AM, nukeymusic wrote: > I have a file which has the data in the following format: > Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > > the first field is a timestamp, I'd like to replace it with the time > in seconds starting from the first one like this: > 27 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 You're looking for several things here, all of which Python can do. I'll give you a few pointers - have a browse of the documentation. 1) Reading a file line-by-line is usually best done with the iterator idiom. Open your file with the "open()" function, read in any headers, and then use "for line in inputfile:" to loop over the data. 2) Parse the line into separate pieces using the split() and join() methods of the string object 3) Use the time.strptime() function to convert the string date into something more usable 4) Maintain a variable with "previous time" and take the difference each time. 5) Write the modified lines to another file. Python's pretty good with this sort of job. It won't let you down! Chris Angelico From einazaki668 at yahoo.com Wed Dec 14 17:20:40 2011 From: einazaki668 at yahoo.com (Eric) Date: Wed, 14 Dec 2011 14:20:40 -0800 (PST) Subject: file data => array(s) Message-ID: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> I'm trying to read some file data into a set of arrays. The file data is just four columns of numbers, like so: 1.2 2.2 3.3 0.5 0.1 0.2 1.0 10.1 ... and so on I'd like to read this into four arrays, one array for each column. Alternatively, I guess something like this is okay too: [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on] I came up with the following for the four array option: file = open(fileName, 'r') for line in file.readlines(): d1, e1, d2, e2 = map(float, line.split()) data1.append(d1) # where data1, err1, data2, err2 are init-ed as empty lists err1.append(e1) data2.append(d2) err2.append(e2) file.close() But somehow it doesn't seem very python-esque (I'm thinking there's a more elegant and succinct way to do it in python). I've also tried replacing the above "map" line with: d = d + map(float, line.split()) # where d is initialized as d = [] But all I get is one long flat list, not what I want. So is the map and append method the best I can do or is there a slicker way? One more thing, no numpy. Nothing against numpy but I'm curious to see what can be done with just the box stock python install. TIA, eric From d at davea.name Wed Dec 14 17:59:25 2011 From: d at davea.name (Dave Angel) Date: Wed, 14 Dec 2011 17:59:25 -0500 Subject: file data => array(s) In-Reply-To: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> Message-ID: <4EE92A4D.3070602@davea.name> On 12/14/2011 05:20 PM, Eric wrote: > I'm trying to read some file data into a set of arrays. The file data > is just four columns of numbers, like so: > > 1.2 2.2 3.3 0.5 > 0.1 0.2 1.0 10.1 > ... and so on > > I'd like to read this into four arrays, one array for each column. > Alternatively, I guess something like this is okay too: > > [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on] > > I came up with the following for the four array option: > > file = open(fileName, 'r') > for line in file.readlines(): The readlines() call is a waste of time/space. file is already an iterator that'll return lines for you. > d1, e1, d2, e2 = map(float, line.split()) > data1.append(d1) # where data1, err1, data2, err2 are init-ed > as empty lists > err1.append(e1) > data2.append(d2) > err2.append(e2) > file.close() > > But somehow it doesn't seem very python-esque (I'm thinking there's a > more elegant and succinct way to do it in python). I've also tried > replacing the above "map" line with: > > d = d + map(float, line.split()) # where d is initialized as d > = [] > > But all I get is one long flat list, not what I want. > > So is the map and append method the best I can do or is there a > slicker way? > > One more thing, no numpy. Nothing against numpy but I'm curious to > see what can be done with just the box stock python install. > > TIA, > eric When I see a problem like this, I turn to zip(). It's got some powerful uses when rows and columns need inverting. I didn't try it on an actual file, but the following works: linedata = [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1] ] data, err1, data2, err2 = zip(*linedata) print data print err1 print data2 print err2 So you could try (untested) file = open(filename, "r") linedata = [ map(float, line) for line in file] data, err1, data2, err2 = zip(*linedata) file.close() Note that your code won't work (and mine probably won't either) if one of the lines has 3 or 5 items. Or if one of the numbers isn't legal format for a float. So you need to think about error checking, or decide whether a partial result is important. -- DaveA From tjreedy at udel.edu Wed Dec 14 18:13:36 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Dec 2011 18:13:36 -0500 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> Message-ID: On 12/14/2011 3:01 AM, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > >> To complement what Eric says below: The with statement is looking for an >> instance *method*, which by definition, is a function attribute of a >> *class* (the class of the context manager) that takes an instance of the >> class as its first parameter. > > I'm not sure that is correct... I don't think that there is anything "by > definition" about where methods live. From the Python glossary: "method: A function which is defined inside a class body." That is actually a bit too narrow, as a function can be added to the class after it is defined. But the point then is that it is treated as if defined inside the class body. > Particularly not in Python where > instance methods can be attributes of the instance itself. This is access, not definition or actual location. The glossary entry go on to say: "If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self)." This does *not* happen if a callable is found in the instance-specific dictionary. An instance method is a function (callable) attribute of a class that gets special treatment when accessed (indirectly) through an instance of that class (or subclass thereof). >>>> class Test(object): > ... def method(self): > ... print("This method is an attribute of the class.") > ... >>>> t = Test() >>>> t.method() > This method is an attribute of the class. The bound method t.method is an instance the class exposed as types.MethodType. In other words, isinstance(t.method, types.MethodType) == True >>>> import types >>>> t.method = types.MethodType( > ... lambda self: print( > ... "This method is an attribute of the instance."), t) Calling any old fruit an apple does not make it one. Calling any old function a method does not make it one. 'types.MethodType' is the exposed name of the class the interpreter uses to create bound methods from a method and an instance of the class containing the method. I believe the interpreter does an isinstance check, but it must do that before calling the class, and not in the bound method constructor itself. In any case, a bound method is not a method. So the printed statement is not true. In this case, the result is not really even a bound method, as the function argument is not a method, so we cannot even ask if the second arg is an instance of the function class container. MethodType is a special case of functools.partial, which was added later. You could have used the latter to the same effect. Or you could have used any old function that printed the same thing. There is no relation between the object passed as the second arg of MethodType and what you do with the resulting callable. Either 't' could be something else. See below. >>>> t.method() > This method is an attribute of the instance. Yes, the callable (which is not a method) is (currently) an attribute of the instance. But that is irrelevant to its operation. t.method is just a callable, in particular, a pseudo bound method, not a method. It is *not* supplying the instance it is called on as the first parameter of the callable. The arguemnt (which is not used) has already been supplied. These produce the same output: class B: pass b = B() b.method = t.method b.method() f = t.method f() t.method = lambda: print("This method is an attribute of the instance.") t.method() > So the normal lookup rules that apply to data attributes, namely > instance, then class, then superclasses, also applies to methods in > Python. When you ask the interpreter to resolve a.x, x is just a supposed attribute, and the interpreter has no idea what class the result should be. > But this doesn't apply for special dunder attributes like __exit__, for > speed reasons. It does not apply to dunder *methods* because they are reserved names defined to be (bound to) methods. So the interpreter knowing that it is looking for a method and that methods have to be attributes of classes, goes directly to the class. -- Terry Jan Reedy From tjreedy at udel.edu Wed Dec 14 18:23:44 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Dec 2011 18:23:44 -0500 Subject: What is this widget? In-Reply-To: References: Message-ID: On 12/14/2011 1:47 PM, Muddy Coder wrote: > Hi Folks, > > I am trying to write letters on a photo that is opened in a canvas. So > I think I must need a widget to contain the letters I will type in. I > tried to use a Label, it worked. But, a Label covered part of the > photo underneath, so I can't use it. You could use a entry widget over or outside the canvas for the user to enter the text, and then close that box and write the entered text to the canvas. Whether you can make the background of the entry box transparent probably depends on the gui toolkit. -- Terry Jan Reedy From einazaki668 at yahoo.com Wed Dec 14 18:25:25 2011 From: einazaki668 at yahoo.com (Eric) Date: Wed, 14 Dec 2011 15:25:25 -0800 (PST) Subject: file data => array(s) References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> Message-ID: On Dec 14, 4:59?pm, Dave Angel wrote: > Note that your code won't work (and mine probably won't either) if one > of the lines has 3 or 5 items. ?Or if one of the numbers isn't legal > format for a float. ?So you need to think about error checking, or > decide whether a partial result is important. > > -- > > DaveA Haven't tried your suggestion yet, I just wanted to comment on this last part real quick. I have the same concern, my plan is to wrap all that stuff up in a "try:" construct. In such cases the program only has to kick out a simple, sensible (for non-programmers) error message and quit. BTW, I didn't say it originally, but this is for 2.7 and hopefully it'll be easy to carry over to 3.2. Thanks, eric From steve+comp.lang.python at pearwood.info Wed Dec 14 18:27:58 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 23:27:58 GMT Subject: file data => array(s) References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> Message-ID: <4ee930fd$0$29979$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Dec 2011 14:20:40 -0800, Eric wrote: > I'm trying to read some file data into a set of arrays. The file data > is just four columns of numbers, like so: > > 1.2 2.2 3.3 0.5 > 0.1 0.2 1.0 10.1 > ... and so on > > I'd like to read this into four arrays, one array for each column. > Alternatively, I guess something like this is okay too: > > [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on] First thing: due to the fundamental nature of binary floating point numbers, if you convert text like "0.1" to a float, you don't get 0.1, you get 0.10000000000000001. That is because 0.1000...01 is the closest possible combination of fractions of 1/2, 1/4, 1/8, ... that adds up to 1/10. If this fact disturbs you, you can import the decimal module and use decimal.Decimal instead; otherwise forget I said anything and continue using float. I will assume you're happy with floats. Assuming the file is small, say, less than 50MB, I'd do it like this: # Version for Python 2.x f = open(filename, 'r') text = f.read() # Grab the whole file at once. numbers = map(float, text.split()) f.close() That gives you a single list [1.2, 2.2, 3.3, 0.5, 0.1, 0.2, ...] which you can now split into groups of four. There are lots of ways to do this. Here's an inefficient way which hopefully will be simple to understand: result = [] while numbers != []: result.append(numbers[0:4]) del numbers[0:4] Here is a much more efficient method which is only a tad harder to understand: result = [] for start in range(0, len(numbers), 4): result.append(numbers[start:start+4]) And just for completeness, here is an advanced technique using itertools: n = len(numbers)//4 numbers = iter(numbers) from itertools import islice result = [list(islice(numbers, 4)) for i in range(n)] Be warned that this version throws away any partial group left over at the end; if you don't want that, change the line defining n to this instead: n = len(numbers)//4 + (len(numbers)%4 > 0) -- Steven From wuwei23 at gmail.com Wed Dec 14 19:45:08 2011 From: wuwei23 at gmail.com (alex23) Date: Wed, 14 Dec 2011 16:45:08 -0800 (PST) Subject: PyCharm, .idea, and source control Message-ID: <8a4e9467-60a9-425c-bbdd-6bd48b7b7bc8@f30g2000pri.googlegroups.com> Hey everyone, I've been using PyCharm for the past month and only just hit an issue that I'm hoping someone else may have some experience with resolving. My problem has to do with PyCharm storing project configuration files in its .idea folder inside the project. This is both a mix of project-level settings and individual customisations. The recommendation is to check this folder into the repo as well - excluding the files of individual settings - but I'm the only developer here using PyCharm and polluting the project with one user's settings would not be received well. More so, even if I _did_ this, I'd still need to be storing my individual settings elsewhere. So what I'm currently doing is having git ignore the .idea folder, and then subsequently turning the folder into its own repo and storing it separately. Ideally I'd just like to be able to have all of the project-config folders in one user-definable location, but I haven't found any way to do that. Has any one else hit this issue, and if so, any tips of resolving it elegantly? From cosmo_general at yahoo.com Wed Dec 14 21:17:00 2011 From: cosmo_general at yahoo.com (Muddy Coder) Date: Wed, 14 Dec 2011 18:17:00 -0800 (PST) Subject: What is this widget? -- again Message-ID: <2766f2ed-8033-4a0e-b29e-1f4797c55af2@u5g2000vbd.googlegroups.com> Hi Folks, Sorry for the unclear question in last post. Well, I am using Tkinter to do GUI, and I just don't know what kind of widget can let me do annotation on an image being displayed. An example is the Paint of Windows: a dotted line box appearing on a image to hold a typed in text. I just can't figure out what widget in Tkinter is equivalent to that dotted line box. Please help, thanks again! Cosmo From cosmo_general at yahoo.com Wed Dec 14 21:26:56 2011 From: cosmo_general at yahoo.com (Muddy Coder) Date: Wed, 14 Dec 2011 18:26:56 -0800 (PST) Subject: How to use a color value returned by colorChooser? Message-ID: Hi Folks, This should be a simple question, but I just can't get an answer from Phython Docs. You see, when we created a widget, and need to tweak the color, we just simply configure it, such as: abutton = Button(root, text='Foo') abutton.config(fg='blue') so we can make the Button color in blue. However, when I choose a color by a color Dialog, askcolor, and then I will get a color value as: (0, 0, 255) ..... So, I want to use this returned color value to configure my existing widgets, with widget.config() syntax. The Python interpreter does not take it. Therefore, I think I must need to covert this returned color value into a symbolic name, such as 'blue', 'red' and so on, then the widget.config() syntax can be used. How can I convert this color value? Please help, thanks folks! Cosmo From rustompmody at gmail.com Wed Dec 14 22:43:21 2011 From: rustompmody at gmail.com (rusi) Date: Wed, 14 Dec 2011 19:43:21 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> <91f6f2ec-25ac-4ca8-b654-f605a7c8a53e@c13g2000vbh.googlegroups.com> Message-ID: <359cdc5e-c955-42fc-a419-3722bae45850@k5g2000pra.googlegroups.com> On Dec 14, 10:15?pm, Eelco wrote: > 'Kindof' off-topic, but what the hell :). We keep having these debates -- so I wonder how off-topic it is... And so do famous CSists: http://research.microsoft.com/en-us/um/people/gurevich/opera/123.pdf : : > > Again, you are completely mis-representing the situation. ?In my > > experience, most mathematicians (I'm not talking about undergraduate > > students here) do not see the axioms are the root of the mathematics > > that they do. ?Formal systems are just one way to explore mathematics. > > ?Of course they can in some cases be very useful and enlightening. > > Its your word versus mine I suppose. Some older discussions: http://groups.google.com/group/comp.lang.python/browse_thread/thread/46435c36f3a13621/896579b757126243?lnk=gst&q=rusi+steven+platonism#896579b757126243 http://groups.google.com/group/comp.lang.python/browse_thread/thread/d36dcd2e2e175d1e/45dd596bc050ac2d?lnk=gst&q=rusi+steven+platonism#45dd596bc050ac2d From python at mrabarnett.plus.com Wed Dec 14 23:10:55 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 04:10:55 +0000 Subject: How to use a color value returned by colorChooser? In-Reply-To: References: Message-ID: <4EE9734F.90109@mrabarnett.plus.com> On 15/12/2011 02:26, Muddy Coder wrote: > Hi Folks, > > This should be a simple question, but I just can't get an answer from > Phython Docs. You see, when we created a widget, and need to tweak the > color, we just simply configure it, such as: > > abutton = Button(root, text='Foo') > abutton.config(fg='blue') > > so we can make the Button color in blue. However, when I choose a > color by a color Dialog, askcolor, and then I will get a color value > as: (0, 0, 255) ..... So, I want to use this returned color value to > configure my existing widgets, with widget.config() syntax. The Python > interpreter does not take it. Therefore, I think I must need to covert > this returned color value into a symbolic name, such as 'blue', 'red' > and so on, then the widget.config() syntax can be used. How can I > convert this color value? Please help, thanks folks! > Look here: http://effbot.org/tkinterbook/tkinter-widget-styling.htm From steve+comp.lang.python at pearwood.info Thu Dec 15 00:01:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 Dec 2011 05:01:21 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4ee97f20$0$11091$c3e8da3@news.astraweb.com> On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: > On 12/14/2011 3:01 AM, Steven D'Aprano wrote: >> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: >> >>> To complement what Eric says below: The with statement is looking for >>> an instance *method*, which by definition, is a function attribute of >>> a *class* (the class of the context manager) that takes an instance of >>> the class as its first parameter. >> >> I'm not sure that is correct... I don't think that there is anything >> "by definition" about where methods live. > > From the Python glossary: > "method: A function which is defined inside a class body." > > That is actually a bit too narrow, as a function can be added to the > class after it is defined. But the point then is that it is treated as > if defined inside the class body. First off, let me preface this by saying that I'm not necessarily saying that the above glossary definition needs to be changed. For most purposes, it is fine, since *nearly always* methods are created as functions defined inside the class body. But it needs to be understood in context as a simplified, hand-wavy definition which covers 99% of the common cases, and not a precise, definitive technical definition. To give an analogy, it is like defining mammals as "hairy animals which give birth to live young", which is correct for all mammals except for monotremes, which are mammals which lay eggs. So I'm happy for the glossary entry to stay as is, because complicating it would confuse the average coder more than it would educate them. But the definition as given is strictly wrong, because it fails to capture the essence of what distinguishes a method from other objects. It mistakes *how* you normally create a method for *what* a method is, a little like defining "a hamburger is a foodstuff you get from McDonalds by giving them money". Here are three ways that the definition fails: (1) You can create a function inside a class, and it remains a function, so long as the class constructor (metaclass) never gets to build a method object from the function. It is easy to do: just hide it inside a wrapper object. >>> class FunctionInsideClass(object): ... def func(x, y): # define a function inside a class ... return x + 2*y ... print(type(func)) # confirm is actually is a function ... attr = (func,) # hide it from the metaclass ... del func ... >>> print(type(FunctionInsideClass.attr[0])) (2) Instead of hiding the function from the metaclass, you can change the metaclass to something which doesn't make methods out of functions. I won't show an example, because it's tricky to get right (or at least *I* find metaclasses tricky). (3) So the definition is too broad: you can have functions defined inside classes that are not methods. But it is also too narrow: you can have methods outside of classes. I'm not talking about bound and unbound methods, but about *creating* the method from scratch outside of a class. When you call the method constructor directly, you can create a method from a function defined outside of a class. >>> def func(self, a): ... return self + a ... >>> type(func) # Definitely a function. >>> obj = types.MethodType(func, 42) >>> obj(23) # Works as expected. 65 >>> type(obj) So there's a method which has never been inside a class, and couldn't even if you tried: int is a built-in immutable type. So what are methods? In Python, methods are wrappers around functions which automatically pass the instance to the inner function object. Under normal circumstances, you create methods by declaring functions inside a class, but that's not the only way to create methods, and it is not the only place they can be found. Note that this definition can easily be adapted to describe class methods and static methods too: class methods are wrappers around functions that automatically pass the class to the inner function, and static methods are wrappers which don't pass any automatic arguments. >> Particularly not in Python where >> instance methods can be attributes of the instance itself. > > This is access, not definition or actual location. Not so. In the example I gave, the method *really is* inside the instance, stored in the instance __dict__ and not the class __dict__. > The glossary entry go > on to say: "If called as an attribute of an instance of that class, the > method will get the instance object as its first argument (which is > usually called self)." This does *not* happen if a callable is found in > the instance-specific dictionary. That's right. Methods are special not because of where they are, but because of what they are. > An instance method is a function > (callable) attribute of a class that gets special treatment when > accessed (indirectly) through an instance of that class (or subclass > thereof). Methods aren't functions at all, not in the isinstance sense. They are functions in the sense that any callable object is a function, i.e. they are callable sub-routines. But they're not functions in the sense of being instances of type "function". They are wrappers around functions. Other languages may do things differently, but that's what Python does. You can even retrieve the function object from the wrapper: >>> obj.__func__ >>>>> class Test(object): >> ... def method(self): >> ... print("This method is an attribute of the class.") ... >>>>> t = Test() >>>>> t.method() >> This method is an attribute of the class. > > The bound method t.method is an instance the class exposed as > types.MethodType. In other words, isinstance(t.method, types.MethodType) > == True > >>>>> import types >>>>> t.method = types.MethodType( >> ... lambda self: print( >> ... "This method is an attribute of the instance."), t) > > Calling any old fruit an apple does not make it one. Calling any old > function a method does not make it one. I'm not *calling* a function a method, I'm *creating* a method from a function object. There is no difference between a method created with types.MethodType and a method automagically created inside a class except for the location where the object is stored. The location is irrelevant. > 'types.MethodType' is the exposed name of the class the interpreter uses > to create bound methods from a method and an instance of the class > containing the method. I believe the interpreter does an isinstance > check, but it must do that before calling the class, and not in the > bound method constructor itself. In any case, a bound method is not a > method. That's an astonishing statement. What is your evidence for this amazing claim that bound methods are not actually methods? What are they then, if not methods? > In this case, the result is not really even a bound method, as the > function argument is not a method, so we cannot even ask if the second > arg is an instance of the function class container. MethodType is a > special case of functools.partial, which was added later. You could have > used the latter to the same effect. Or you could have used any old > function that printed the same thing. Good grief. Is it really your argument that the types.MethodType isn't actually the type of methods, but a fake that lies about returning methods? Well, that's easy enough to test: >>> class K(object): ... def f(self): ... pass ... >>> k = K() >>> type(k.f) is types.MethodType True Unless you are going to accuse me of faking the interpreter output (perhaps I monkey-patched the type built-in?) that is definitive proof that types.MethodType is not fake, it actually is the method type used in classes. > There is no relation between the object passed as the second arg of > MethodType and what you do with the resulting callable. Either 't' could > be something else. See below. > >>>>> t.method() >> This method is an attribute of the instance. > > Yes, the callable (which is not a method) is (currently) an attribute of > the instance. But that is irrelevant to its operation. t.method is just > a callable, in particular, a pseudo bound method, not a method. It is > *not* supplying the instance it is called on as the first parameter of > the callable. Of course it is. It is because I made it to be that way. I encourage you to experiment with Python's introspection tools, perhaps put a few calls to print inside a "pseudo bound method" (your words, there's nothing pseudo about it) and satisfy yourself that the instance passed is the same instance as it is called from *under the circumstances shown*. > The arguemnt (which is not used) has already been > supplied. These produce the same output: > > class B: pass > b = B() > b.method = t.method > b.method() Yes. So what? You can take a bound object and attach it to any other object and see the same results -- this doesn't mean it isn't a bound object. That's how bound objects work! Your objection fails because any method will work the same way: >>> class K: pass ... >>> class Example: ... def foo(self): ... print(self) ... >>> x = Example() >>> k = K() >>> k.attr = x.foo >>> k.attr() <__main__.Example object at 0xb70fcc0c> No trickery with types.MethodType, no metaclass magic, no sleight of hand, just stock standard Python behaviour. -- Steven From python at mrabarnett.plus.com Thu Dec 15 00:15:58 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 05:15:58 +0000 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4ee97f20$0$11091$c3e8da3@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4EE9828E.8010202@mrabarnett.plus.com> On 15/12/2011 05:01, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: > >> On 12/14/2011 3:01 AM, Steven D'Aprano wrote: >>> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: >>> >>>> To complement what Eric says below: The with statement is looking for >>>> an instance *method*, which by definition, is a function attribute of >>>> a *class* (the class of the context manager) that takes an instance of >>>> the class as its first parameter. >>> >>> I'm not sure that is correct... I don't think that there is anything >>> "by definition" about where methods live. >> >> From the Python glossary: >> "method: A function which is defined inside a class body." >> >> That is actually a bit too narrow, as a function can be added to the >> class after it is defined. But the point then is that it is treated as >> if defined inside the class body. > > First off, let me preface this by saying that I'm not necessarily saying > that the above glossary definition needs to be changed. For most > purposes, it is fine, since *nearly always* methods are created as > functions defined inside the class body. But it needs to be understood in > context as a simplified, hand-wavy definition which covers 99% of the > common cases, and not a precise, definitive technical definition. > > To give an analogy, it is like defining mammals as "hairy animals which > give birth to live young", which is correct for all mammals except for > monotremes, which are mammals which lay eggs. > [snip] Or the naked mole-rat. Or cetaceans (whales). From tyler at tysdomain.com Thu Dec 15 02:14:48 2011 From: tyler at tysdomain.com (Littlefield, Tyler) Date: Thu, 15 Dec 2011 00:14:48 -0700 Subject: using twisted as a client-server hybrid Message-ID: <4EE99E68.2060104@tysdomain.com> Hello all: I have a quick question--I am working on a project where a system will connect to me to get commands. The idea is to make the server the "client," used for dispatching commands, so I'm trying to find a way that I can set it up to listen, but poll stdin somehow for input. Is this a possibility? -- Take care, Ty Web: http://tds-solutions.net The Aspen project: a light-weight barebones mud engine http://code.google.com/p/aspenmud Sent from my toaster. From steve+comp.lang.python at pearwood.info Thu Dec 15 02:21:31 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 Dec 2011 07:21:31 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4ee99ffa$0$11091$c3e8da3@news.astraweb.com> On Thu, 15 Dec 2011 05:15:58 +0000, MRAB wrote: > On 15/12/2011 05:01, Steven D'Aprano wrote: >> On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: >> >>> On 12/14/2011 3:01 AM, Steven D'Aprano wrote: >>>> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: >>>> >>>>> To complement what Eric says below: The with statement is looking >>>>> for an instance *method*, which by definition, is a function >>>>> attribute of a *class* (the class of the context manager) that >>>>> takes an instance of the class as its first parameter. >>>> >>>> I'm not sure that is correct... I don't think that there is anything >>>> "by definition" about where methods live. >>> >>> From the Python glossary: >>> "method: A function which is defined inside a class body." >>> >>> That is actually a bit too narrow, as a function can be added to the >>> class after it is defined. But the point then is that it is treated >>> as if defined inside the class body. >> >> First off, let me preface this by saying that I'm not necessarily >> saying that the above glossary definition needs to be changed. For most >> purposes, it is fine, since *nearly always* methods are created as >> functions defined inside the class body. But it needs to be understood >> in context as a simplified, hand-wavy definition which covers 99% of >> the common cases, and not a precise, definitive technical definition. >> >> To give an analogy, it is like defining mammals as "hairy animals which >> give birth to live young", which is correct for all mammals except for >> monotremes, which are mammals which lay eggs. >> > [snip] > Or the naked mole-rat. Or cetaceans (whales). Naked mole-rats and cetaceans do have hair, just not very much of it. E.g. http://marinelife.about.com/od/cetaceans/f/whaleshair.htm -- Steven From rosuav at gmail.com Thu Dec 15 02:34:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Dec 2011 18:34:22 +1100 Subject: using twisted as a client-server hybrid In-Reply-To: <4EE99E68.2060104@tysdomain.com> References: <4EE99E68.2060104@tysdomain.com> Message-ID: On Thu, Dec 15, 2011 at 6:14 PM, Littlefield, Tyler wrote: > Hello all: > I have a quick question--I am working on a project where a system will > connect to me to get commands. The idea is to make the server the "client," > used for dispatching commands, so ?I'm trying to find a way that I can set > it up to listen, but poll stdin somehow for input. Is this a possibility? One easy way to do that is to use separate threads, one to watch stdin (preferably the main thread) and one to listen on the socket or whatever the other end uses. Another option is to use async calls and callbacks. But to answer your simplest question, yes, it most definitely is. ChrisA From steve+comp.lang.python at pearwood.info Thu Dec 15 02:47:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 Dec 2011 07:47:05 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4ee9a5f9$0$11091$c3e8da3@news.astraweb.com> On Thu, 15 Dec 2011 05:01:21 +0000, Steven D'Aprano wrote: >> From the Python glossary: >> "method: A function which is defined inside a class body." >> >> That is actually a bit too narrow, as a function can be added to the >> class after it is defined. But the point then is that it is treated as >> if defined inside the class body. [...] > But the definition as given is strictly wrong, because it fails to > capture the essence of what distinguishes a method from other objects. [...] TL;DR http://users.rcn.com/python/download/Descriptor.htm#functions-and-methods Short and sweet. -- Steven From mohandayalank at gmail.com Thu Dec 15 03:28:33 2011 From: mohandayalank at gmail.com (D. Mohan M. Dayalan) Date: Thu, 15 Dec 2011 00:28:33 -0800 (PST) Subject: combuter is use full message Message-ID: <96723de3-cf3c-446f-8659-833215c6342b@z22g2000prd.googlegroups.com> http;//123maza.com/48/moon670/ From hoogendoorn.eelco at gmail.com Thu Dec 15 04:44:56 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Thu, 15 Dec 2011 01:44:56 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> <91f6f2ec-25ac-4ca8-b654-f605a7c8a53e@c13g2000vbh.googlegroups.com> <359cdc5e-c955-42fc-a419-3722bae45850@k5g2000pra.googlegroups.com> Message-ID: On Dec 15, 4:43?am, rusi wrote: > On Dec 14, 10:15?pm, Eelco wrote: > > > 'Kindof' off-topic, but what the hell :). > > > We keep having these debates -- so I wonder how off-topic it is... > And so do famous CSists:http://research.microsoft.com/en-us/um/people/gurevich/opera/123.pdf > Well, you are right, there are some deep links here. My view of what is wrong with mainstream mathematics is its strange interpretation of the semantics of classical logic. (And I dont think any other schools get it quite right either; I think finitists may avoid the mistakes of others, but are rightfully accussed of being needlessly restrictive, for instance) This is best illustrated by means of the principle of explosion. It rests on assuming a contradiction, and then assigning rather peculiar semantics to them. What is typically left unstated are the semantics of symbol lookup, but apparently it is implicitly understood one can pick whatever value upon encountering a contradicting symbol. There is no well defined rule for the lookup of a twice-defined symbol. Of course the sane thing to do, to a mind grown up around computer languages, upon encountering a twice defined symbol, is not to continue to generate deductions from both branches, but to throw an exception and interrupt the specific line of reasoning that depends on this contradicting symbol right then and there. Conceptually, we can see something is wrong with these undefined semantics right away. A logical system that allows you to draw conclusions as to where the pope shits from assertions about natural numbers could not more obviously be broken. If you dont have this broken way of dealing with contradictions, one does not have to do one of many silly and arbitrary things to make infinity work, such as making a choice between one-to-one correspondence and subset-relations for determining the cardinality of a set; one can simply admit the concept of infinity, while useful, is not consistent, keep the contradiction well handled instead of having it explode in your face (or explode into the field of transfinite analysis; a consequece of 'dealing' with these issues by rejecting the intuitively obviously true relation between subset relations and cardinality), and continue reasoning with the branches of your argument that you are interested in. In other words, what logic needs is a better exception-handling system, which completes the circle with programming languages quite nicely. :) From robert.kern at gmail.com Thu Dec 15 05:47:49 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 15 Dec 2011 10:47:49 +0000 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/14/11 12:32 PM, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 10:56:02 +0200, Jussi Piitulainen wrote: >> I'm not misunderstanding any argument. There was no argument. There was >> a blanket pronouncement that _in mathematics_ mod is not a binary >> operator. I should learn to challenge such pronouncements and ask what >> the problem is. Maybe next time. > > So this was *one* person making that claim? > > I understand that, in general, mathematicians don't have much need for a > remainder function in the same way programmers do -- modulo arithmetic is > far more important. But there's a world of difference between saying "In > mathematics, extracting the remainder is not important enough to be given > a special symbol and treated as an operator" and saying "remainder is not > a binary operator". The first is reasonable; the second is not. The professional mathematicians that I know personally don't say that "remainder is not a binary operator". They *do* say that "modulo is not an operator" in mathematics just because they have reserved that word and the corresponding notation to define the congruence relations. So for example, the following two statements are equivalent: 42 = 2 mod 5 2 = 42 mod 5 The "mod 5" notation modifies the entire equation (or perhaps the = sign if you like to think about it like that), not the term it is immediately next to. Python's % operator is a binary operator that binds to a particular term, not the whole equation. The following two are not equivalent statements: 42 == 2 % 5 2 == 42 % 5 It's mostly kvetching on their part that programming language designers misunderstood the notation and applied the name to something that is confusingly almost, but not quite, the same thing. They aren't saying that you couldn't *define* such an operator; they would just prefer that we didn't abuse the name. But really, it's their fault for using notation that looks like an operator. -- 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 From rustompmody at gmail.com Thu Dec 15 05:56:19 2011 From: rustompmody at gmail.com (rusi) Date: Thu, 15 Dec 2011 02:56:19 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> <91f6f2ec-25ac-4ca8-b654-f605a7c8a53e@c13g2000vbh.googlegroups.com> <359cdc5e-c955-42fc-a419-3722bae45850@k5g2000pra.googlegroups.com> Message-ID: On Dec 15, 2:44?pm, Eelco wrote: > In other words, what logic needs is a better exception-handling > system, which completes the circle with programming languages quite > nicely. :) Cute... but dangerously recursive (if taken literally) Remember that logic is the foundation of programming language semantics. And your idea (suggests) that programming language semantics be made (part of) the foundation of logic. Of course I assume you are not being very literal. Still the dangers of unnoticed circularity are often... well unnoticed :-) eg. McCarthy gave the semantics of lisp in lisp -- a lisp interpreter in lisp is about a page of code. It probably was a decade before someone realized that the same semantics would 'work' for lazy or applicative (eager) order evaluation. This then begs the question what exactly it means for that semantics to 'work'... From rosuav at gmail.com Thu Dec 15 05:58:30 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Dec 2011 21:58:30 +1100 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 15, 2011 at 9:47 PM, Robert Kern wrote: > 42 = 2 mod 5 > ?2 = 42 mod 5 It might make more sense to programmers if you think of it as written: 42 = 2, mod 5 2 = 42, mod 5 ChrisA From y.turgut at gmail.com Thu Dec 15 05:59:04 2011 From: y.turgut at gmail.com (yeet) Date: Thu, 15 Dec 2011 02:59:04 -0800 (PST) Subject: Screen Control Fullscreen ON/OFF Message-ID: Hi all, I would like to make fullscreen white and fullscreen black using Python on Linux. With in the specs of the LCD, I want to be able to display fullscreen white and black approximately at 30Hz. Frequency (on/off per second) will be input manually which is between 1-40Hz. Any idea where to start ? Can Python perform such fast thing ? From hoogendoorn.eelco at gmail.com Thu Dec 15 05:59:07 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Thu, 15 Dec 2011 02:59:07 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 15, 11:47?am, Robert Kern wrote: > On 12/14/11 12:32 PM, Steven D'Aprano wrote: > > > On Wed, 14 Dec 2011 10:56:02 +0200, Jussi Piitulainen wrote: > >> I'm not misunderstanding any argument. There was no argument. There was > >> a blanket pronouncement that _in mathematics_ mod is not a binary > >> operator. I should learn to challenge such pronouncements and ask what > >> the problem is. Maybe next time. > > > So this was *one* person making that claim? > > > I understand that, in general, mathematicians don't have much need for a > > remainder function in the same way programmers do -- modulo arithmetic is > > far more important. But there's a world of difference between saying "In > > mathematics, extracting the remainder is not important enough to be given > > a special symbol and treated as an operator" and saying "remainder is not > > a binary operator". The first is reasonable; the second is not. > > The professional mathematicians that I know personally don't say that "remainder > is not a binary operator". They *do* say that "modulo is not an operator" in > mathematics just because they have reserved that word and the corresponding > notation to define the congruence relations. So for example, the following two > statements are equivalent: > > ? ?42 = 2 mod 5 > ? ?2 = 42 mod 5 > > The "mod 5" notation modifies the entire equation (or perhaps the = sign if you > like to think about it like that), not the term it is immediately next to. > Python's % operator is a binary operator that binds to a particular term, not > the whole equation. The following two are not equivalent statements: > > ? ?42 == 2 % 5 > ? ?2 == 42 % 5 > > It's mostly kvetching on their part that programming language designers > misunderstood the notation and applied the name to something that is confusingly > almost, but not quite, the same thing. They aren't saying that you couldn't > *define* such an operator; they would just prefer that we didn't abuse the name. > But really, it's their fault for using notation that looks like an operator. > > -- > 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 Thanks Robert, I think you cut right through the confusion there. To tie it back in with python language design; all the more reason not to opt for pseudo-backwards compatibility. If python wants a remainder function, call it 'remainder'. Not 'rem', not 'mod', and certainly not '%'. Its the more pythonic way; a self-describing name, rather than poorly defined or poorly understood cryptology. From rustompmody at gmail.com Thu Dec 15 06:04:40 2011 From: rustompmody at gmail.com (rusi) Date: Thu, 15 Dec 2011 03:04:40 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4f2f6742-b1b2-4605-8004-b6aa431b8513@k5g2000pra.googlegroups.com> On Dec 15, 3:58?pm, Chris Angelico wrote: > On Thu, Dec 15, 2011 at 9:47 PM, Robert Kern wrote: > > ?42 = 2 mod 5 > > ?2 = 42 mod 5 > > It might make more sense to programmers if you think of it as written: > > 42 = 2, mod 5 > 2 = 42, mod 5 > > ChrisA For the record I should say that the guy who taught me abstract algebra, said about as much: He said that the notation a == b mod n should be written as a ==n b (read the == as 3 horizontal lines and the n as a subscript) From y.turgut at gmail.com Thu Dec 15 06:12:10 2011 From: y.turgut at gmail.com (yeet) Date: Thu, 15 Dec 2011 03:12:10 -0800 (PST) Subject: Screen Control Fullscreen ON/OFF References: Message-ID: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> On Dec 15, 12:59?pm, yeet wrote: > Hi all, > > I would like to make fullscreen white and fullscreen black using > Python on Linux. With in the specs of the LCD, I want to be able to > display fullscreen white and black approximately at 30Hz. Frequency > (on/off per second) will be input manually which is between 1-40Hz. > Any idea where to start ? Can Python perform such fast thing ? Forgot to mention and sorry for the pollution ; My LCD has 2ms respond time thus it can handle a maximum of 50Hz ON/ OFF (white/black) thus seems to fit my 1-40Hz range. I am not asking or looking for a fully working code, I just need some directions. From hoogendoorn.eelco at gmail.com Thu Dec 15 06:37:35 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Thu, 15 Dec 2011 03:37:35 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> <91f6f2ec-25ac-4ca8-b654-f605a7c8a53e@c13g2000vbh.googlegroups.com> <359cdc5e-c955-42fc-a419-3722bae45850@k5g2000pra.googlegroups.com> Message-ID: On Dec 15, 11:56?am, rusi wrote: > On Dec 15, 2:44?pm, Eelco wrote: > > > In other words, what logic needs is a better exception-handling > > system, which completes the circle with programming languages quite > > nicely. :) > > Cute... but dangerously recursive (if taken literally) > Remember that logic is the foundation of programming language > semantics. > And your idea (suggests) that programming language semantics be made > (part of) the foundation of logic. > > Of course I assume you are not being very literal. > Still the dangers of unnoticed circularity are often... well > unnoticed :-) Well, logic as a language has semantics, one way or the other. This circularity is a general theme in epistemology, and one that fits well with the view of deduction-induction as a closed loop cycle. Knowledge does not flow from axioms to theorems; axioms without an encompassing context are meaningless symbols. Its a body of knowledge as a whole that should be put to the test; the language and the things we express in it are inseperable. (the not-quite-famous-enough Quine in a nutshell) The thing is that our semantics of logic are quite primitive; cooked up in a time where people spent far less time thinking about these things, and having a far narrower base of experience to draw ideas from. They didnt have the luxury of already having grown up studying a dozen formal languages before embarking on creating their own. It other words, the semantics of logic is a legacy piece of crap, but an insanely firmly entrenched one. I mean, there are many sensible ways of defining semantics of conflicting symbols, but you'll find on studying these things that the guys who (often implicitly) laid down these rules didnt even seemed to have consciously thought about them. Not because they were stupid; far from it, but for similar reasons as to why the x86 architecture wasnt concieved of the day after the invention of the transistor. From nizamov.shawkat at gmail.com Thu Dec 15 06:44:46 2011 From: nizamov.shawkat at gmail.com (Nizamov Shawkat) Date: Thu, 15 Dec 2011 12:44:46 +0100 Subject: Screen Control Fullscreen ON/OFF In-Reply-To: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> References: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> Message-ID: >> >> I would like to make fullscreen white and fullscreen black using >> Python on Linux. With in the specs of the LCD, I want to be able to >> display fullscreen white and black approximately at 30Hz. Frequency >> (on/off per second) will be input manually which is between 1-40Hz. >> Any idea where to start ? Can Python perform such fast thing ? > > Forgot to mention and sorry for the pollution ; > > My LCD has 2ms respond time thus it can handle a maximum of 50Hz ON/ > OFF (white/black) thus seems to fit my 1-40Hz range. I am not asking > or looking for a fully working code, I just need some directions. It depends on whether you want sync to vblank or not. If not, that is pretty easy - use sleep() or something similar. If you have to use sync (screen is always either black or white, never partly black and white) then it is much much more difficult. Actually I do not know of any way to sync to it. Once upon a time I had a P-3/500 linux box with homemade python program that would wait for RTC interrupts (which was set to 8192 Hz frequency) and performed different combinations of ADC/DAC/Digital-IO at required timesteps . It worked really very well and with submillisecond resolution. Hope it helps, S.Nizamov From nizamov.shawkat at gmail.com Thu Dec 15 07:18:10 2011 From: nizamov.shawkat at gmail.com (Nizamov Shawkat) Date: Thu, 15 Dec 2011 13:18:10 +0100 Subject: Screen Control Fullscreen ON/OFF In-Reply-To: References: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> Message-ID: > It depends on whether you want sync to vblank or not. If not, that is > pretty easy - use sleep() or something similar. If you have to use > sync (screen is always either black or white, never partly black and > white) then it is much much more difficult. Actually I do not know of > any way to sync to it. But you do not have to sync to vblank anyway. So you can turn on vblank sync for the videocard and then you will have either completely black or completely white screen at each single time point, but this will be delayed in regard to what you set in python. Hope this helps, S.Nizamov From jpiitula at ling.helsinki.fi Thu Dec 15 07:48:06 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 15 Dec 2011 14:48:06 +0200 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4f2f6742-b1b2-4605-8004-b6aa431b8513@k5g2000pra.googlegroups.com> Message-ID: rusi writes: > On Dec 15, 3:58?pm, Chris Angelico wrote: > > On Thu, Dec 15, 2011 at 9:47 PM, Robert Kern wrote: > > > ?42 = 2 mod 5 > > > ?2 = 42 mod 5 > > > > It might make more sense to programmers if you think of it as > > written: > > > > 42 = 2, mod 5 > > 2 = 42, mod 5 > > > > ChrisA > > For the record I should say that the guy who taught me abstract > algebra, said about as much: > He said that the notation > a == b mod n > should be written as > a ==n b > (read the == as 3 horizontal lines and the n as a subscript) I think the modulus is usually given in parentheses and preferably some whitespace: in text, a == b (mod n), using == for the triple -, and in a display: a == b (mod n). I think even a == b == c (mod n), without repeating the modulus every time. (A subscript sounds good if the modulus is simple. Perhaps it often is.) That way it does not even look like a binary operator. I think Graham, Knuth, and Patashnik play it nicely in their book Concrete Mathematics, where they have both mods: the congruence relation, and the binary operator. The book is targeted for computer scientists. As if mathematicians didn't use the exact same notations for different purposes, even in the same context, and often with no problems whatsoever as long as all parties happen to know what they are talking about. Often the uses are analogous, but at least the two main uses of (x,y) differ wildly. (So Knuth uses (x .. y) for the interval, but he is a programmer.) From showell30 at yahoo.com Thu Dec 15 08:35:55 2011 From: showell30 at yahoo.com (Steve Howell) Date: Thu, 15 Dec 2011 05:35:55 -0800 (PST) Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Dec 14, 9:01?pm, Steven D'Aprano wrote: > [...] > So what are methods? In Python, methods are wrappers around functions > which automatically pass the instance to the inner function object. Under > normal circumstances, you create methods by declaring functions inside a > class, but that's not the only way to create methods, and it is not the > only place they can be found. > I've always understood methods as basically function wrappers that pass in the instance, so it's good to hear somebody else formulate it that way. For the special methods like __enter__ and __exit__, the tricky part isn't understanding what would happen once the methods were called; the tricky part is getting them to be called in the first place, if they were not declared inside the class or attached to the class. import types class Blank: pass foo = Blank() foo.name = "foo1" foo.__exit__ = types.MethodType(lambda self, *args: print(self.name), foo) foo.__exit__() # works like a method in python3, prints foo1 with foo: pass From ulrich.eckhardt at dominolaser.com Thu Dec 15 09:19:13 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Thu, 15 Dec 2011 15:19:13 +0100 Subject: Screen Control Fullscreen ON/OFF In-Reply-To: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> References: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> Message-ID: <28cpr8-rt.ln1@satorlaser.homedns.org> Am 15.12.2011 12:12, schrieb yeet: > My LCD has 2ms respond time thus it can handle a maximum of 50Hz ON/ > OFF (white/black) thus seems to fit my 1-40Hz range. You might want to ask Santa for a new calculator, as in my book a response time of 2ms would be enough for 250Hz (period = 2 * 2ms). Reminds me of a hack that used a special pattern on a CRT to emit DCF77 signals, reprogramming any suitable radio-controlled clock in range. What are you trying to do, just out of curiosity? (c: Uli From nathan.alexander.rice at gmail.com Thu Dec 15 09:34:29 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Thu, 15 Dec 2011 09:34:29 -0500 Subject: Odd behavior of object equality/identity in the context of relative vs fully qualified imports Message-ID: I just ran into this yesterday, and I am curious if there is a rational behind it... I have a class that uses a dictionary to dispatch from other classes (k) to functions for those classes (v). I recently ran into a bug where the dictionary would report that a class which was clearly in the dictionary's keys was giving a KeyError. id() produced two distinct values, which I found to be curious, and issubclass/isinstance tests also failed. When I inspected the two classes, I found that the only difference between the two was the __module__ variable, which in one case had a name relative to the current module (foo), and in another case had the fully qualified name (bar.foo). When I went ahead and changed the import statement for the module to import bar.foo rather than import foo, everything worked as expected. My first thought was that I had another foo module in an old version of the bar package somewhere on my pythonpath; After a thorough search this proved not to be the case. Has anyone else run into this? Is this intended behavior? If so, why? Nathan From dihedral88888 at googlemail.com Thu Dec 15 09:40:57 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 15 Dec 2011 06:40:57 -0800 (PST) Subject: Screen Control Fullscreen ON/OFF In-Reply-To: References: Message-ID: <20668600.306.1323960057492.JavaMail.geo-discussion-forums@pruu23> On Thursday, December 15, 2011 6:59:04 PM UTC+8, yeet wrote: > Hi all, > > I would like to make fullscreen white and fullscreen black using > Python on Linux. With in the specs of the LCD, I want to be able to > display fullscreen white and black approximately at 30Hz. Frequency > (on/off per second) will be input manually which is between 1-40Hz. > Any idea where to start ? Can Python perform such fast thing ? Check pygame, pyimg and sdllib and wxpython. Python is a high level language that was not bound to hardware. One of the major strengths of Python is very easy to be supported by programs in other languages, especially in C, Pascal and Fortran. Also programs in Python are easy to debug and maintain. From alec.taylor6 at gmail.com Thu Dec 15 09:43:21 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 16 Dec 2011 01:43:21 +1100 Subject: =?windows-1252?Q?Re=3A_ERP_with_Supply_chain_management_=28incl=2E_POS=29_and?= =?windows-1252?Q?_Customer_relationship_management_=97_What=27s_available=3F?= In-Reply-To: References: Message-ID: On Thu, Dec 15, 2011 at 5:54 AM, Anurag Chourasia wrote: > Hi Alec, > > I am building a POS/CRM (Loyalty Management) system as well. > > So far, the best I could find to use as a base is this one > > https://github.com/rosarior/django-inventory > > In any case, I felt that even starting from scratch is not a bad > idea.....Please let me know if you get more pointers. We can even work on > this one together. > > Regards, > Anurag > Thanks Anurag, I will look into it, however I'm thinking of writing my whole project as extensions to satchmo. (modularised though, so you'd be able to enable only a few features, and even disable ecommerce) Would be great to have you on board (if you're still interested, contact me off-list) From d at davea.name Thu Dec 15 10:08:51 2011 From: d at davea.name (Dave Angel) Date: Thu, 15 Dec 2011 10:08:51 -0500 Subject: Odd behavior of object equality/identity in the context of relative vs fully qualified imports In-Reply-To: References: Message-ID: <4EEA0D83.6070807@davea.name> On 12/15/2011 09:34 AM, Nathan Rice wrote: > I just ran into this yesterday, and I am curious if there is a > rational behind it... > > I have a class that uses a dictionary to dispatch from other classes > (k) to functions for those classes (v). I recently ran into a bug > where the dictionary would report that a class which was clearly in > the dictionary's keys was giving a KeyError. id() produced two > distinct values, which I found to be curious, and > issubclass/isinstance tests also failed. When I inspected the two > classes, I found that the only difference between the two was the > __module__ variable, which in one case had a name relative to the > current module (foo), and in another case had the fully qualified name > (bar.foo). When I went ahead and changed the import statement for the > module to import bar.foo rather than import foo, everything worked as > expected. My first thought was that I had another foo module in an > old version of the bar package somewhere on my pythonpath; After a > thorough search this proved not to be the case. > > Has anyone else run into this? Is this intended behavior? If so, why? > > Nathan Hard to tell with such generic information. But I'm guessing you imported your script from some other module, creating a circular import sequence. The circular can be a problem in its own right. But even worse, if the script is part of the chain is that it's loaded twice, with different names. And any top-level items, such as classes, will be instantiated twice as well. is your script called foo.py by any chance? -- DaveA From rantingrickjohnson at gmail.com Thu Dec 15 10:33:49 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Thu, 15 Dec 2011 07:33:49 -0800 (PST) Subject: What is this widget? References: Message-ID: <0c31c34c-e446-420e-8aa8-f5313612463f@g41g2000yqa.googlegroups.com> On Dec 14, 12:47?pm, Muddy Coder wrote: > Hi Folks, > > I am trying to write letters on a photo that is opened in a canvas. So > I think I must need a widget to contain the letters I will type in. I > tried to use a Label, it worked. But, a Label covered part of the > photo underneath, so I can't use it. I saw some software did such a > thing nicely: a box popped on a photo, with dotted lines as borders, > expandable. When such a box was re-sized with a mouse, the font size > in the box also got changed. The box has no background color, so it > does not cover a rectangle area on the photo. I need such a widget, > but I don't know what is this one. I tried Text, Label, but they all > come with Window-like stuff, so they cover some photo content. Can > somebody points me a direction? Thanks! Listen Muddy, this is not first time that you've come here seeking advice with a cryptic and ambiguous post. People have been very patient and even asked you to be more explicit with your questions. I highly advise you to re-write your original post and include which GUI library you are using and any other pertinent info. My guess is that you are using Tkinter. If so, i believe the canvas has a method called "create_text" which will do what you want. To make the input easier to code, use a Tkinter Entry widget stuffed into a dialog -- tkSimpleDialog makes this, well, simple. PS: Clean up you act Muddy! From passiday at gmail.com Thu Dec 15 10:49:57 2011 From: passiday at gmail.com (Passiday) Date: Thu, 15 Dec 2011 07:49:57 -0800 (PST) Subject: Can't get Apache2 tp process py (Ubuntu 11.10) Message-ID: <1544851.511.1323964197163.JavaMail.geo-discussion-forums@vbxw7> Hello, Sorry if this is not exactly appropriate forum where to ask Apache question, but I though't here would some Apache-experienced people probably hang out. Well, I can't get my Apache2 to process Python *.py files. I checked numerous tutorials how to enable it, and did the install steps: - Installed the libapache2-mod-python package - Added the following lines to the section of /etc/apache2/sites-available/default file: AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On I made this simple test.py file: def index(req): return "Test successful"; When I request the file, ie, [url]http://localhost/test.py[/url], I get the download prompt, and the Python script is just downloaded, not processed. The error log /var/log/apache2 shows these lines that kind of confirm that the module is loaded: [Thu Dec 15 15:12:38 2011] [notice] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads. [Thu Dec 15 15:12:38 2011] [notice] mod_python: using mutex_directory /tmp [Thu Dec 15 15:12:38 2011] [notice] Apache/2.2.20 (Ubuntu) PHP/5.3.6-13ubuntu3.3 with Suhosin-Patch mod_python/3.3.1 Python/2.7.2+ configured -- resuming normal operations As one can see, there are no any errors that would tell there's something wrong happening. Any ideas what installation step did I do wrong or skipped? From rantingrickjohnson at gmail.com Thu Dec 15 10:53:55 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Thu, 15 Dec 2011 07:53:55 -0800 (PST) Subject: What is this widget? -- again References: <2766f2ed-8033-4a0e-b29e-1f4797c55af2@u5g2000vbd.googlegroups.com> Message-ID: <587e3547-11b1-477f-8e9e-aa3caf60dfa5@c18g2000yqj.googlegroups.com> On Dec 14, 8:17?pm, Muddy Coder wrote: > Hi Folks, > > Sorry for the unclear question in last post. Well, I am using Tkinter > to do GUI, and I just don't know what kind of widget can let me do > annotation on an image being displayed. An example is the Paint of > Windows: a dotted line box appearing on a image to hold a typed in > text. I just can't figure out what widget in Tkinter is equivalent to > that dotted line box. Please help, thanks again! > > Cosmo No such widget exists in Tkinter. As Dennis said the code must be written. I can assure you that this functionality can be created with Tkinter (because i've done it myself) however it requires an in-depth knowledge of Tkinter -- which apparently you don't have since you are asking the question. Waring: NEVER USE ANY MICROSOFT INTERFACE AS INSPIRATION!!! Whist you ponder the intricacies professional of image annotation, i suggest you follow my advice in your OTHER post (of the same title) and use the "canvas.create_text" method to draw text on top of images THEN you need to learn how to move "canvas items" around the canvas using the mouse THEN you need to intercept right clicks on the text item and show a dialog with an entry widget stuffed inside so the user can edit the text THEN update the canvas text from user input. When solving a problem you need to follow a linear path. The first step in this path is to draw text on a canvas. 1. Draw text to a canvas. 2. Intercept right click events on a "canvas text". 3. Build a custom dialog using tkSimpleDialog (or use a floating "canvas window") to display a Tkinter Entry widget so the user can edit the current value of the "canvas text". 4. Fetch the value of Tkinter "canvas text" 5. Load the value into the entry widget. 6. Update the value of the canvas text item based on user input returned from the dialog. Extra Credit: * capture mouse drag events and allow user manipulation of "canvas text" x-y position. http://infohost.nmt.edu/tcc/help/pubs/tkinter/ http://effbot.org/tkinterbook/ ...may the source be with you. From dihedral88888 at googlemail.com Thu Dec 15 11:32:28 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 15 Dec 2011 08:32:28 -0800 (PST) Subject: What is this widget? -- again In-Reply-To: <587e3547-11b1-477f-8e9e-aa3caf60dfa5@c18g2000yqj.googlegroups.com> References: <2766f2ed-8033-4a0e-b29e-1f4797c55af2@u5g2000vbd.googlegroups.com> <587e3547-11b1-477f-8e9e-aa3caf60dfa5@c18g2000yqj.googlegroups.com> Message-ID: <17035503.315.1323966748123.JavaMail.geo-discussion-forums@prix23> On Thursday, December 15, 2011 11:53:55 PM UTC+8, Rick Johnson wrote: > On Dec 14, 8:17?pm, Muddy Coder wrote: > > Hi Folks, > > > > Sorry for the unclear question in last post. Well, I am using Tkinter > > to do GUI, and I just don't know what kind of widget can let me do > > annotation on an image being displayed. An example is the Paint of > > Windows: a dotted line box appearing on a image to hold a typed in > > text. I just can't figure out what widget in Tkinter is equivalent to > > that dotted line box. Please help, thanks again! > > > > Cosmo > > No such widget exists in Tkinter. As Dennis said the code must be > written. I can assure you that this functionality can be created with > Tkinter (because i've done it myself) however it requires an in-depth > knowledge of Tkinter -- which apparently you don't have since you are > asking the question. > > Waring: NEVER USE ANY MICROSOFT INTERFACE AS INSPIRATION!!! > > Whist you ponder the intricacies professional of image annotation, i > suggest you follow my advice in your OTHER post (of the same title) > and use the "canvas.create_text" method to draw text on top of images > THEN you need to learn how to move "canvas items" around the canvas > using the mouse THEN you need to intercept right clicks on the text > item and show a dialog with an entry widget stuffed inside so the user > can edit the text THEN update the canvas text from user input. When > solving a problem you need to follow a linear path. The first step in > this path is to draw text on a canvas. > > 1. Draw text to a canvas. > 2. Intercept right click events on a "canvas text". > 3. Build a custom dialog using tkSimpleDialog (or use a floating > "canvas window") to display a Tkinter Entry widget so the user can > edit the current value of the "canvas text". > 4. Fetch the value of Tkinter "canvas text" > 5. Load the value into the entry widget. > 6. Update the value of the canvas text item based on user input > returned from the dialog. > > Extra Credit: > * capture mouse drag events and allow user manipulation of "canvas > text" x-y position. > > http://infohost.nmt.edu/tcc/help/pubs/tkinter/ > http://effbot.org/tkinterbook/ > > ...may the source be with you. Check BOA and wxpython and work out the examples. It is kind of boring and not too easy to be accustomed to use. A simpler set of the toolkit pygame and pyimg to explore is another way to advance. Study the source of pyimg is very helpful. From jenn.duerr at gmail.com Thu Dec 15 11:36:37 2011 From: jenn.duerr at gmail.com (noydb) Date: Thu, 15 Dec 2011 08:36:37 -0800 (PST) Subject: test for list equality Message-ID: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> I want to test for equality between two lists. For example, if I have two lists that are equal in content but not in order, I want a return of 'equal' -- dont care if they are not in the same order. In order to get that equality, would I have to sort both lists regardless? if yes, how (having issues with list.sort)? Another way i tried, that I think is kind-of roundabout is like x = [2, 5, 1, 88, 9] y = [5, 2, 9, 1, 88] inBoth = list(set(x) & set(y)) and then test that list.count is equal between inBoth and x and/or y. Any better suggestions? Thanks for any help! From dmitrey15 at gmail.com Thu Dec 15 11:45:31 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Thu, 15 Dec 2011 08:45:31 -0800 (PST) Subject: Ann: OpenOpt and FuncDesigner 0.37 Message-ID: <73f2f0e5-761a-45c1-a4dc-245e7088f53d@g41g2000yqa.googlegroups.com> Hi all, I'm glad to inform you about new release 0.37 (2011-Dec-15) of our free software: OpenOpt (numerical optimization): IPOPT initialization time gap (time till first iteration) for FuncDesigner models has been decreased Some improvements and bugfixes for interalg, especially for "search all SNLE solutions" mode (Systems of Non Linear Equations) Eigenvalue problems (EIG) (in both OpenOpt and FuncDesigner) Equality constraints for GLP (global) solver de Some changes for goldenSection ftol stop criterion GUI func "manage" - now button "Enough" works in Python3, but "Run/ Pause" not yet (probably something with threading and it will be fixed in Python instead) FuncDesigner: Major sparse Automatic differentiation improvements for badly- vectorized or unvectorized problems with lots of constraints (except of box bounds); some problems now work many times or orders faster (of course not faster than vectorized problems with insufficient number of variable arrays). It is recommended to retest your large-scale problems with useSparse = 'auto' | True| False Two new methods for splines to check their quality: plot and residual Solving ODE dy/dt = f(t) with specifiable accuracy by interalg Speedup for solving 1-dimensional IP by interalg SpaceFuncs and DerApproximator: Some code cleanup You may trace OpenOpt development information in our recently created entries in Twitter and Facebook, see http://openopt.org for details. For more information visit http://openopt.org Regards, D. From roy at panix.com Thu Dec 15 11:48:23 2011 From: roy at panix.com (Roy Smith) Date: Thu, 15 Dec 2011 08:48:23 -0800 (PST) Subject: How to generate "a, b, c, and d"? Message-ID: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some standard way to do this, handling all the special cases? [] ==> '' ['a'] ==> 'a' ['a', 'b'] ==> 'a and b' ['a', 'b', 'c', 'd'] ==> 'a, b, and c' It seems like the kind of thing django.contrib.humanize would handle, but alas, it doesn't. From y.turgut at gmail.com Thu Dec 15 11:57:13 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Thu, 15 Dec 2011 08:57:13 -0800 (PST) Subject: Screen Control Fullscreen ON/OFF References: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> <28cpr8-rt.ln1@satorlaser.homedns.org> Message-ID: On Dec 15, 4:19?pm, Ulrich Eckhardt wrote: > Am 15.12.2011 12:12, schrieb yeet: > > > My LCD has 2ms respond time thus it can handle a maximum of 50Hz ON/ > > OFF (white/black) thus seems to fit my 1-40Hz range. > > You might want to ask Santa for a new calculator, as in my book a > response time of 2ms would be enough for 250Hz (period = 2 * 2ms). > > Reminds me of a hack that used a special pattern on a CRT to emit DCF77 > signals, reprogramming any suitable radio-controlled clock in range. > What are you trying to do, just out of curiosity? > > (c: > > Uli Yes that's correct, 50Hz limit is the limit of NVIDIA CUDA Linux drivers limit. Screen can go higher rates on sucky windows. I am trying to generate a visual stimulus that will be used for fNIR and EEG captures. On Dec 15, 2:18 pm, Nizamov Shawkat wrote: > > It depends on whether you want sync to vblank or not. If not, that is > > pretty easy - use sleep() or something similar. If you have to use > > sync (screen is always either black or white, never partly black and > > white) then it is much much more difficult. Actually I do not know of > > any way to sync to it. > > But you do not have to sync to vblank anyway. So you can turn on > vblank sync for the videocard and then you will have either completely > black or completely white screen at each single time point, but this > will be delayed in regard to what you set in python. > > Hope this helps, > S.Nizamov It's not easy to do this basing on time, I think doing per frames is a much better option. It's sounded like a very simple task at first but I realize it's not that easy.Maybe I should write the screen blinker in C or Assembly then call it from Python. From nathan.alexander.rice at gmail.com Thu Dec 15 12:11:45 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Thu, 15 Dec 2011 12:11:45 -0500 Subject: Odd behavior of object equality/identity in the context of relative vs fully qualified imports In-Reply-To: <4EEA0D83.6070807@davea.name> References: <4EEA0D83.6070807@davea.name> Message-ID: It would be more work than I want to go into to provide full context (unless it is to file a bug report, if it actually is a bug). I verified that there are no cyclical dependency issues using snakefood, and I doublechecked that just changing the import from full to partial name is sufficient to reintroduce the bug. Can I get confirmation that this is not expected behavior? I will go ahead and file a bug report it that is the case. Nathan On Thu, Dec 15, 2011 at 10:08 AM, Dave Angel wrote: > On 12/15/2011 09:34 AM, Nathan Rice wrote: >> >> I just ran into this yesterday, and I am curious if there is a >> rational behind it... >> >> I have a class that uses a dictionary to dispatch from other classes >> (k) to functions for those classes (v). ?I recently ran into a bug >> where the dictionary would report that a class which was clearly in >> the dictionary's keys was giving a KeyError. ?id() produced two >> distinct values, which I found to be curious, and >> issubclass/isinstance tests also failed. ?When I inspected the two >> classes, I found that the only difference between the two was the >> __module__ variable, which in one case had a name relative to the >> current module (foo), and in another case had the fully qualified name >> (bar.foo). ?When I went ahead and changed the import statement for the >> module to import bar.foo rather than import foo, everything worked as >> expected. ?My first thought was that I had another foo module in an >> old version of the bar package somewhere on my pythonpath; ?After a >> thorough search this proved not to be the case. >> >> Has anyone else run into this? ?Is this intended behavior? ?If so, why? >> >> Nathan > > Hard to tell with such generic information. ?But I'm guessing you imported > your script from some other module, creating a circular import sequence. > ?The circular can be a problem in its own right. ?But even worse, if the > script is part of the chain is that it's loaded twice, with different names. > ?And any top-level items, such as classes, will be instantiated twice as > well. ?is your script called foo.py by any chance? > > -- > > DaveA > From python at mrabarnett.plus.com Thu Dec 15 12:27:14 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 17:27:14 +0000 Subject: How to generate "a, b, c, and d"? In-Reply-To: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> Message-ID: <4EEA2DF2.9080107@mrabarnett.plus.com> On 15/12/2011 16:48, Roy Smith wrote: > I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some standard way to do this, handling all the special cases? > > [] ==> '' > ['a'] ==> 'a' > ['a', 'b'] ==> 'a and b' > ['a', 'b', 'c', 'd'] ==> 'a, b, and c' > > It seems like the kind of thing django.contrib.humanize would handle, but alas, it doesn't. How about this: def and_list(items): if len(items) <= 2: return " and ".join(items) return ", ".join(items[ : -1]) + ", and " + items[-1] print(and_list([])) print(and_list(['a'])) print(and_list(['a', 'b'])) print(and_list(['a', 'b', 'c'])) print(and_list(['a', 'b', 'c', 'd'])) From jenn.duerr at gmail.com Thu Dec 15 12:49:27 2011 From: jenn.duerr at gmail.com (noydb) Date: Thu, 15 Dec 2011 09:49:27 -0800 (PST) Subject: test for list equality References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> Message-ID: <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> On Dec 15, 11:36?am, noydb wrote: > I want to test for equality between two lists. ?For example, if I have > two lists that are equal in content but not in order, I want a return > of 'equal' -- dont care if they are not in the same order. ?In order > to get that equality, would I have to sort both lists regardless? ?if > yes, how (having issues with list.sort)? > > Another way i tried, that I think is kind-of roundabout is like > x = [2, 5, 1, 88, 9] > y = [5, 2, 9, 1, 88] > inBoth = list(set(x) & set(y)) > > and then test that list.count is equal between inBoth and x and/or y. > > Any better suggestions? > > Thanks for any help! My sort issue... as in this doesn't work >>> if x.sort == y.sort: ... print 'equal' ... else: ... print 'not equal' ... not equal ??? From python at mrabarnett.plus.com Thu Dec 15 12:54:21 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 17:54:21 +0000 Subject: test for list equality In-Reply-To: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> Message-ID: <4EEA344D.1050601@mrabarnett.plus.com> On 15/12/2011 16:36, noydb wrote: > I want to test for equality between two lists. For example, if I have > two lists that are equal in content but not in order, I want a return > of 'equal' -- dont care if they are not in the same order. In order > to get that equality, would I have to sort both lists regardless? if > yes, how (having issues with list.sort)? > > Another way i tried, that I think is kind-of roundabout is like > x = [2, 5, 1, 88, 9] > y = [5, 2, 9, 1, 88] > inBoth = list(set(x)& set(y)) > > and then test that list.count is equal between inBoth and x and/or y. > > > Any better suggestions? > > Thanks for any help! You could count the number of times each item occurs using the Counter class in the collections module: >>> x = [2, 5, 1, 88, 9] >>> y = [5, 2, 9, 1, 88] >>> from collections import Counter >>> cx = Counter(x) >>> cy = Counter(y) >>> cx Counter({88: 1, 1: 1, 2: 1, 5: 1, 9: 1}) >>> cx == cy True From gordon at panix.com Thu Dec 15 12:57:02 2011 From: gordon at panix.com (John Gordon) Date: Thu, 15 Dec 2011 17:57:02 +0000 (UTC) Subject: test for list equality References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: In <61edc02c-4f86-45ef-82a1-61c7013003b4 at t38g2000yqe.googlegroups.com> noydb writes: > My sort issue... as in this doesn't work > >>> if x.sort =3D=3D y.sort: > ... print 'equal' > ... else: > ... print 'not equal' > ... > not equal > ??? Use x.sort() instead of x.sort . -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From miki.tebeka at gmail.com Thu Dec 15 12:59:33 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 15 Dec 2011 09:59:33 -0800 (PST) Subject: test for list equality In-Reply-To: <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> > My sort issue... as in this doesn't work > >>> if x.sort == y.sort: You're missing the () to make it a function call. Also list.sort() returns none, it mutates the original list. You can either sorted(x) == sorted(y) or set(x) == set(y) From stefan_ml at behnel.de Thu Dec 15 13:01:12 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 15 Dec 2011 19:01:12 +0100 Subject: test for list equality In-Reply-To: <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: noydb, 15.12.2011 18:49: > On Dec 15, 11:36 am, noydb wrote: >> I want to test for equality between two lists. For example, if I have >> two lists that are equal in content but not in order, I want a return >> of 'equal' -- dont care if they are not in the same order. In order >> to get that equality, would I have to sort both lists regardless? if >> yes, how (having issues with list.sort)? >> >> Another way i tried, that I think is kind-of roundabout is like >> x = [2, 5, 1, 88, 9] >> y = [5, 2, 9, 1, 88] >> inBoth = list(set(x)& set(y)) >> >> and then test that list.count is equal between inBoth and x and/or y. >> >> Any better suggestions? >> >> Thanks for any help! > > My sort issue... as in this doesn't work > >>> if x.sort == y.sort: > ... print 'equal' > ... else: > ... print 'not equal' > ... > not equal alist.sort() is a method, so you have to call it in order to execute it. alist.sort will only give you a reference to the method. Comparing that to another method reference returns False, as expected. Also, calling it does not return anything (useful), it modifies the list in place. If you want to create a new list (which you don't want in this case, but anyway), you can use the sorted() builtin function. Stefan From python.list at tim.thechases.com Thu Dec 15 13:01:28 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 15 Dec 2011 12:01:28 -0600 Subject: How to generate "a, b, c, and d"? In-Reply-To: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> Message-ID: <4EEA35F8.4090502@tim.thechases.com> On 12/15/11 10:48, Roy Smith wrote: > I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some standard way to do this, handling all the special cases? > > [] ==> '' > ['a'] ==> 'a' > ['a', 'b'] ==> 'a and b' > ['a', 'b', 'c', 'd'] ==> 'a, b, and c' > > It seems like the kind of thing django.contrib.humanize would handle, but alas, it doesn't. If you have a list, it's pretty easy as MRAB suggests. For arbitrary iterators, it's a bit more complex. Especially with the odd edge-case of 2 items where there's no comma before the conjunction (where >2 has the comma before the conjunction). If you were willing to forgo the Oxford comma, it would tidy up the code a bit. Sample code below -tkc def gen_list(i, conjunction="and"): i = iter(i) first = i.next() try: prev = i.next() except StopIteration: yield first else: more_than_two = False for item in i: if not more_than_two: yield first yield prev prev = item more_than_two = True if more_than_two: yield "%s %s" % (conjunction, prev) else: yield "%s %s %s" % (first, conjunction, prev) def listify(lst, conjunction="and"): return ', '.join(gen_list(lst, conjunction)) for test, expected in ( ([], ''), (['a'], 'a'), (['a', 'b'], 'a and b'), (['a', 'b', 'c'], 'a, b, and c'), (['a', 'b', 'c', 'd'], 'a, b, c, and d'), ): result = listify(test) print "%r -> %r (got %r) %s" % ( test, expected, result, result == expected and "PASSED" or "FAILED" ) From ian.g.kelly at gmail.com Thu Dec 15 13:03:40 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 15 Dec 2011 11:03:40 -0700 Subject: Odd behavior of object equality/identity in the context of relative vs fully qualified imports In-Reply-To: References: Message-ID: On Thu, Dec 15, 2011 at 7:34 AM, Nathan Rice wrote: > I just ran into this yesterday, and I am curious if there is a > rational behind it... > > I have a class that uses a dictionary to dispatch from other classes > (k) to functions for those classes (v). ?I recently ran into a bug > where the dictionary would report that a class which was clearly in > the dictionary's keys was giving a KeyError. ?id() produced two > distinct values, which I found to be curious, and > issubclass/isinstance tests also failed. ?When I inspected the two > classes, I found that the only difference between the two was the > __module__ variable, which in one case had a name relative to the > current module (foo), and in another case had the fully qualified name > (bar.foo). ?When I went ahead and changed the import statement for the > module to import bar.foo rather than import foo, everything worked as > expected. ?My first thought was that I had another foo module in an > old version of the bar package somewhere on my pythonpath; ?After a > thorough search this proved not to be the case. > > Has anyone else run into this? ?Is this intended behavior? ?If so, why? I think this may be the same problem that the poster in this thread encountered: http://groups.google.com/group/comp.lang.python/browse_thread/thread/961a90219a61e19d/9ed06601603b58d4 Normally I believe the __module__ attribute should be fully qualified regardless of whether the import was absolute or relative. That you're not seeing this suggests that the "relative" import may actually be an absolute import starting from a different sys.path entry. From ckaynor at zindagigames.com Thu Dec 15 13:04:01 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Thu, 15 Dec 2011 10:04:01 -0800 Subject: test for list equality In-Reply-To: References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: On Thu, Dec 15, 2011 at 9:57 AM, John Gordon wrote: > In <61edc02c-4f86-45ef-82a1-61c7013003b4 at t38g2000yqe.googlegroups.com> > noydb writes: > > > My sort issue... as in this doesn't work > > >>> if x.sort =3D=3D y.sort: > > ... print 'equal' > > ... else: > > ... print 'not equal' > > ... > > not equal > > > ??? > > Use x.sort() instead of x.sort . > And you cannot use the method in-line - it mutates the list in place, returning None. If you either do not wish to mutate the list, or you absolutely want to do the sort in-line, you need to use the sorted built-in: if sorted(x) == sorted(y): ... However, this will, temporary, use double the memory. > > -- > John Gordon A is for Amy, who fell down the stairs > gordon at panix.com B is for Basil, assaulted by bears > -- Edward Gorey, "The Gashlycrumb Tinies" > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Dec 15 13:06:01 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 18:06:01 +0000 Subject: test for list equality In-Reply-To: <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: <4EEA3709.5080606@mrabarnett.plus.com> On 15/12/2011 17:49, noydb wrote: > On Dec 15, 11:36 am, noydb wrote: >> I want to test for equality between two lists. For example, if I have >> two lists that are equal in content but not in order, I want a return >> of 'equal' -- dont care if they are not in the same order. In order >> to get that equality, would I have to sort both lists regardless? if >> yes, how (having issues with list.sort)? >> >> Another way i tried, that I think is kind-of roundabout is like >> x = [2, 5, 1, 88, 9] >> y = [5, 2, 9, 1, 88] >> inBoth = list(set(x)& set(y)) >> >> and then test that list.count is equal between inBoth and x and/or y. >> >> Any better suggestions? >> >> Thanks for any help! > > My sort issue... as in this doesn't work >>>> if x.sort == y.sort: > ... print 'equal' > ... else: > ... print 'not equal' > ... > not equal > > > ??? .sort is a method which sorts the list in-place and returns None. You must provide the () if you want to call it, otherwise you just get a reference to the method: >>> x [2, 5, 1, 88, 9] >>> x.sort >>> x.sort() >>> x [1, 2, 5, 9, 88] There's also a function "sorted" which returns its argument as a sorted list. The argument itself isn't altered: >>> y = [5, 2, 9, 1, 88] >>> sorted(y) [1, 2, 5, 9, 88] >>> y [5, 2, 9, 1, 88] It's all in the documentation! :-) From jenn.duerr at gmail.com Thu Dec 15 13:07:39 2011 From: jenn.duerr at gmail.com (noydb) Date: Thu, 15 Dec 2011 10:07:39 -0800 (PST) Subject: test for list equality References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: <18c38490-c667-4774-ad26-c68ab042f72b@q16g2000yqn.googlegroups.com> Ahh, I see (on the sort issue), thanks All! Still, any other slicker ways to do this? Just for learning. From python at mrabarnett.plus.com Thu Dec 15 13:12:33 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 18:12:33 +0000 Subject: test for list equality In-Reply-To: <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: <4EEA3891.4060004@mrabarnett.plus.com> On 15/12/2011 17:59, Miki Tebeka wrote: >> My sort issue... as in this doesn't work >> >>> if x.sort == y.sort: > You're missing the () to make it a function call. > Also list.sort() returns none, it mutates the original list. > You can either > sorted(x) == sorted(y) > or > set(x) == set(y) But don't use sets if there may be duplicates. From darnold992000 at yahoo.com Thu Dec 15 13:12:39 2011 From: darnold992000 at yahoo.com (darnold) Date: Thu, 15 Dec 2011 10:12:39 -0800 (PST) Subject: test for list equality References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: On Dec 15, 11:59?am, Miki Tebeka wrote: > > My sort issue... as in this doesn't work > > >>> if x.sort == y.sort: > > You're missing the () to make it a function call. > Also list.sort() returns none, it mutates the original list. > You can either > ? ? sorted(x) == sorted(y) > or > ? ? set(x) == set(y) I'm pretty sure we don't want to use set() since it throws away duplicates: >>> x = [1,2,3,4] >>> y = [1,1,2,2,3,3,4] >>> sorted(x) == sorted(y) False >>> set(x) == set(y) True From nagle at animats.com Thu Dec 15 13:15:27 2011 From: nagle at animats.com (John Nagle) Date: Thu, 15 Dec 2011 10:15:27 -0800 Subject: MySQLdb for Python 2.7.2 for Windows 32 bit? Message-ID: <4eea393b$0$1665$742ec2ed@news.sonic.net> Is there a MySQLdb binary for Python 2.7.2 for Windows 32 bit? There's a 2.7 binary at "http://www.lfd.uci.edu/~gohlke/pythonlibs/" but not a 2.7.2 binary. (Also, using modules from untrusted sites is riskier than it used to be. GPL code for major packages with added spyware and adware has appeared on major sites. CNet was heavily criticized for bundling NMap with adware on "download.com") John Nagle From ethan at stoneleaf.us Thu Dec 15 13:19:51 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 15 Dec 2011 10:19:51 -0800 Subject: How to generate "a, b, c, and d"? In-Reply-To: <4EEA35F8.4090502@tim.thechases.com> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> Message-ID: <4EEA3A47.5020106@stoneleaf.us> Tim Chase wrote: > On 12/15/11 10:48, Roy Smith wrote: >> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, >> "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some >> standard way to do this, handling all the special cases? >> >> [] ==> '' >> ['a'] ==> 'a' >> ['a', 'b'] ==> 'a and b' >> ['a', 'b', 'c', 'd'] ==> 'a, b, and c' >> >> It seems like the kind of thing django.contrib.humanize would handle, >> but alas, it doesn't. > > If you have a list, it's pretty easy as MRAB suggests. For arbitrary > iterators, it's a bit more complex. Especially with the odd edge-case > of 2 items where there's no comma before the conjunction (where >2 has > the comma before the conjunction). If you were willing to forgo the > Oxford comma, it would tidy up the code a bit. Sample code below Why go through all that instead of just converting the iterator into a list at the beginning of MRAB's solution and then running with it? ~Ethan~ From python.list at tim.thechases.com Thu Dec 15 13:20:10 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 15 Dec 2011 12:20:10 -0600 Subject: test for list equality In-Reply-To: <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: <4EEA3A5A.3060705@tim.thechases.com> On 12/15/11 11:59, Miki Tebeka wrote: >> My sort issue... as in this doesn't work >>>>> if x.sort == y.sort: > You're missing the () to make it a function call. > Also list.sort() returns none, it mutates the original list. > You can either > sorted(x) == sorted(y) > or > set(x) == set(y) Duplicates cause issues in the set() version: a = [1,2,3,4] b = a + a print sorted(a) == sorted(b) # False print set(a) == set(b) # True They mean different things, and the OP may want one or the other depending on how they want to consider duplicates. -tkc From __peter__ at web.de Thu Dec 15 13:23:37 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 15 Dec 2011 19:23:37 +0100 Subject: Odd behavior of object equality/identity in the context of relative vs fully qualified imports References: Message-ID: Nathan Rice wrote: > I just ran into this yesterday, and I am curious if there is a > rational behind it... > > I have a class that uses a dictionary to dispatch from other classes > (k) to functions for those classes (v). I recently ran into a bug > where the dictionary would report that a class which was clearly in > the dictionary's keys was giving a KeyError. id() produced two > distinct values, which I found to be curious, and > issubclass/isinstance tests also failed. When I inspected the two > classes, I found that the only difference between the two was the > __module__ variable, which in one case had a name relative to the > current module (foo), and in another case had the fully qualified name > (bar.foo). When I went ahead and changed the import statement for the > module to import bar.foo rather than import foo, everything worked as > expected. My first thought was that I had another foo module in an > old version of the bar package somewhere on my pythonpath; After a > thorough search this proved not to be the case. > > Has anyone else run into this? Is this intended behavior? If so, why? Not exactly intended, but a logical side effect of how Python identifies its modules. The problem is that you have an entry in sys.path that reaches into the bar package. When you import foo Python does not check whether [path1]/bar/foo.py points to the same file as [path2]/foo.py, it just verifies that the name "foo" is not in the sys.modules cache before it physically imports the file. Therefore you get two distinct imports of "foo.py" stored in the cache as "foo" and "bar.foo". As Python's classes are not declarations, but objects themselves you get distinct classes just as with the following >>> classes = set() >>> for i in range(3): ... class A: pass ... >>> classes = set() >>> for i in range(3): ... class A: pass ... classes.add(A) ... >>> classes set([, , ]) To avoid the problem just remove the offending [path2] from sys.path and always import the foo subpackage with import bar.foo # everywhere or from . import foo # inside bar PS: You may run into similar problems when you import the main module of a program. It will end up in the module cache as filename_sans_extension and "__main__". From miki.tebeka at gmail.com Thu Dec 15 13:25:42 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 15 Dec 2011 10:25:42 -0800 (PST) Subject: test for list equality In-Reply-To: References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: <30504278.623.1323973542217.JavaMail.geo-discussion-forums@yqbo42> > > set(x) == set(y) > > Duplicates cause issues in the set() version: You're right, I stand corrected. From miki.tebeka at gmail.com Thu Dec 15 13:25:42 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 15 Dec 2011 10:25:42 -0800 (PST) Subject: test for list equality In-Reply-To: References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: <30504278.623.1323973542217.JavaMail.geo-discussion-forums@yqbo42> > > set(x) == set(y) > > Duplicates cause issues in the set() version: You're right, I stand corrected. From ian.g.kelly at gmail.com Thu Dec 15 13:28:21 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 15 Dec 2011 11:28:21 -0700 Subject: test for list equality In-Reply-To: <18c38490-c667-4774-ad26-c68ab042f72b@q16g2000yqn.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <18c38490-c667-4774-ad26-c68ab042f72b@q16g2000yqn.googlegroups.com> Message-ID: On Thu, Dec 15, 2011 at 11:07 AM, noydb wrote: > Ahh, I see (on the sort issue), thanks All! > > Still, any other slicker ways to do this? ?Just for learning. MRAB's collections.Counter suggestion is what I would do. Very tidy, and also more efficient I think: O(n) instead of O(n log n). From jenn.duerr at gmail.com Thu Dec 15 13:32:30 2011 From: jenn.duerr at gmail.com (noydb) Date: Thu, 15 Dec 2011 10:32:30 -0800 (PST) Subject: test for list equality References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <18c38490-c667-4774-ad26-c68ab042f72b@q16g2000yqn.googlegroups.com> Message-ID: <60b72ba6-5456-4e8a-99f2-da2879641f70@r28g2000yqj.googlegroups.com> Thanks All, Thanks MRAB -- good note on the sort() vs sorted(). From einazaki668 at yahoo.com Thu Dec 15 13:37:59 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 15 Dec 2011 10:37:59 -0800 (PST) Subject: file data => array(s) References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> Message-ID: <388939ad-b465-4dd0-8eb8-51395ee643a7@o7g2000yqk.googlegroups.com> On Dec 14, 4:59?pm, Dave Angel wrote: > On 12/14/2011 05:20 PM, Eric wrote: > > > > > > > > > I'm trying to read some file data into a set of arrays. ?The file data > > is just four columns of numbers, like so: > > > ? ? 1.2 ? ?2.2 ? 3.3 ?0.5 > > ? ? 0.1 ? 0.2 ? ?1.0 ?10.1 > > ? ? ... and so on > > > I'd like to read this into four arrays, one array for each column. > > Alternatively, I guess something like this is okay too: > > > ? ? [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on] > > > I came up with the following for the four array option: > > > ? ? file = open(fileName, 'r') > > ? ? for line in file.readlines(): > > The readlines() call is a waste of time/space. ?file is already an > iterator that'll return lines for you. > > > > > > > > > ? ? ? ?d1, e1, d2, e2 = map(float, line.split()) > > ? ? ? ?data1.append(d1) ?# where data1, err1, data2, err2 are init-ed > > as empty lists > > ? ? ? ?err1.append(e1) > > ? ? ? ?data2.append(d2) > > ? ? ? ?err2.append(e2) > > ? ? file.close() > > > But somehow it doesn't seem very python-esque (I'm thinking there's a > > more elegant and succinct way to do it in python). ?I've also tried > > replacing the above "map" line with: > > > ? ? ? ?d = d + map(float, line.split()) ?# where d is initialized as d > > = [] > > > But all I get is one long flat list, not what I want. > > > So is the map and append method the best I can do or is there a > > slicker way? > > > One more thing, no numpy. ?Nothing against numpy but I'm curious to > > see what can be done with just the box stock python install. > > > TIA, > > eric > > When I see a problem like this, I turn to zip(). ?It's got some powerful > uses when rows and columns need inverting. > > I didn't try it on an actual file, but the following works: > linedata = ? ?[[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1] ] > > data, err1, data2, err2 = zip(*linedata) > > print data > print err1 > print data2 > print err2 > > So you could try (untested) > > file = open(filename, "r") > linedata = [ map(float, line) for line in file] > data, err1, data2, err2 = zip(*linedata) > file.close() > > DaveA Neat. This is what I had in mind for a python-esque solution. Only thing is "map(float,line)" should be "map(float,line.split()). Looks like it should be easy enough to weed out any funky data sets because between map() and zip() it's fairly picky about the amount and type of data. Finally, the input files I'll be using for real aren't just four columns of data. The beginning of the file may have comments (optional) and will have two lines of text to identify the data. Maybe I can still do it w/o readlines. Thanks, eric From einazaki668 at yahoo.com Thu Dec 15 13:51:03 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 15 Dec 2011 10:51:03 -0800 (PST) Subject: file data => array(s) References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> <4ee930fd$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1744e277-69ba-4648-a86f-023af83ca939@u32g2000yqe.googlegroups.com> On Dec 14, 5:27?pm, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 14:20:40 -0800, Eric wrote: > > I'm trying to read some file data into a set of arrays. ?The file data > > is just four columns of numbers, like so: > > > ? ?1.2 ? ?2.2 ? 3.3 ?0.5 > > ? ?0.1 ? 0.2 ? ?1.0 ?10.1 > > ? ?... and so on > > > I'd like to read this into four arrays, one array for each column. > > Alternatively, I guess something like this is okay too: > > > ? ?[[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on] > > First thing: due to the fundamental nature of binary floating point > numbers, if you convert text like "0.1" to a float, you don't get 0.1, > you get 0.10000000000000001. That is because 0.1000...01 is the closest > possible combination of fractions of 1/2, 1/4, 1/8, ... that adds up to > 1/10. > > If this fact disturbs you, you can import the decimal module and use > decimal.Decimal instead; otherwise forget I said anything and continue > using float. I will assume you're happy with floats. > Yeah, I don't think it'll be a problem. As I understand it a float in python is a double in C and all our old C programs used doubles. From PDP-11 to MIPS3k to P2 I've seen what I think may have been rounding or precision errors but I haven't heard any complaints because of them. Thanks, eric From python.list at tim.thechases.com Thu Dec 15 13:51:49 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 15 Dec 2011 12:51:49 -0600 Subject: How to generate "a, b, c, and d"? In-Reply-To: <4EEA3A47.5020106@stoneleaf.us> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> <4EEA3A47.5020106@stoneleaf.us> Message-ID: <4EEA41C5.60107@tim.thechases.com> On 12/15/11 12:19, Ethan Furman wrote: > Tim Chase wrote: >> On 12/15/11 10:48, Roy Smith wrote: >>> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, >>> "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some >>> standard way to do this, handling all the special cases? >> >> If you have a list, it's pretty easy as MRAB suggests. For arbitrary >> iterators, it's a bit more complex. Especially with the odd edge-case >> of 2 items where there's no comma before the conjunction (where>2 has >> the comma before the conjunction). If you were willing to forgo the >> Oxford comma, it would tidy up the code a bit. > > Why go through all that instead of just converting the iterator into a > list at the beginning of MRAB's solution and then running with it? For the fun/challenge? Because you have a REALLY big data source that you don't want to keep in memory (in addition the resulting string)? Yeah, for most non-pathological cases, it would make more sense to just make it a list and then deal with the 4 cases (no elements, one element, 2 elements, and >2 elements) individually. -tkc From roy at panix.com Thu Dec 15 14:01:14 2011 From: roy at panix.com (Roy Smith) Date: Thu, 15 Dec 2011 11:01:14 -0800 (PST) Subject: How to generate "a, b, c, and d"? In-Reply-To: References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> <4EEA3A47.5020106@stoneleaf.us> Message-ID: <19419800.394.1323975674835.JavaMail.geo-discussion-forums@vbyc2> FWIW, I ended up with: n = len(names) if n == 0: return '' if n == 1: return names[0] pre = ', '.join(names[:-1]) post = names[-1] return '%s, and %s' (pre, post) the slice-and-join() takes care of both the 2 and >2 element cases at the same time :) It would be nice if there were some standard way to do this. I'm sure I've seen something that was essentially a join() that took two delimiters; one for most elements, the other a special-case for the last one. I can't remember where I saw it. I'm guessing in some web framework. From roy at panix.com Thu Dec 15 14:01:14 2011 From: roy at panix.com (Roy Smith) Date: Thu, 15 Dec 2011 11:01:14 -0800 (PST) Subject: How to generate "a, b, c, and d"? In-Reply-To: References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> <4EEA3A47.5020106@stoneleaf.us> Message-ID: <19419800.394.1323975674835.JavaMail.geo-discussion-forums@vbyc2> FWIW, I ended up with: n = len(names) if n == 0: return '' if n == 1: return names[0] pre = ', '.join(names[:-1]) post = names[-1] return '%s, and %s' (pre, post) the slice-and-join() takes care of both the 2 and >2 element cases at the same time :) It would be nice if there were some standard way to do this. I'm sure I've seen something that was essentially a join() that took two delimiters; one for most elements, the other a special-case for the last one. I can't remember where I saw it. I'm guessing in some web framework. From waitmeforever at hotmail.com Thu Dec 15 14:09:55 2011 From: waitmeforever at hotmail.com (Yang Chun-Kai) Date: Fri, 16 Dec 2011 03:09:55 +0800 Subject: Localhost client-server simple ssl socket test program problems Message-ID: Hello,everyone!! I am writing a simple ssl client-server test program on my personal laptop. And I encounter some problems with my simple programs. Please give me some helps.-------------------------------------------------------------------------------------------------------------------------------------------------------- My server code: import socketimport sslbindsocket = socket.socket()bindsocket.bind(('127.0.0.1', 1234))bindsocket.listen(5)print 'server is waiting for connection...'newsocket, fromaddr = bindsocket.accept()print 'start ssl socket...'connstream = ssl.wrap_socket(newsocket, server_side=True, certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", ssl_version=ssl.PROTOCOL_SSLv23)data = connstream.read()print 'connected from address', fromaddrprint 'received data as', repr(data)connstream.close() My client code: import socketimport ssls = socket.socket(socket.AF_INET, socket.SOCK_STREAM)ssl_sock = ssl.wrap_socket(s, ca_certs="/home/ckyang/PHA/testsslsocket/myCA.crt", cert_reqs=ssl.CERT_REQUIRED)ssl_sock.connect(("127.0.0.1", 1234))ssl_sock.write("hello")ssl_sock.close() -----------------------------------------------------------------------------------------------------------------------------------------------------------Server side error: File "views.py", line 17, in connstream = ssl.wrap_socket(newsocket, server_side=True, certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", ssl_version=ssl.PROTOCOL_SSLv23) File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket ciphers=ciphers) File "/usr/lib/python2.7/ssl.py", line 119, in __init__ ciphers)ssl.SSLError: [Errno 336265218] _ssl.c:347: error:140B0002:SSL routines:SSL_CTX_use_PrivateKey_file:system lib Client side error: File "client.py", line 10, in ssl_sock.connect(("127.0.0.1", 1234)) File "/usr/lib/python2.7/ssl.py", line 299, in connect self.do_handshake() File "/usr/lib/python2.7/ssl.py", line 283, in do_handshake self._sslobj.do_handshake()socket.error: [Errno 104] Connection reset by peer ------------------------------------------------------------------------------------------------------------------------------------------------------------So what is wrong with my code? The codes are so simple and so much like python official site sample demonstration, but I still cant get it work, so frustrating. Seems the problem happened on server side then cause client side cant connect well, is that right? My platform is ubuntu, with openssl 0.9.8 and python 2.7. All certificates and keys self-signed by openssl for test convenience. This is the site for referrence : http://andyjeffries.co.uk/articles/x509-encrypted-authenticated-socket-ruby-client Or should I need a real certificate issued by a real CA to let things work? Any tips or suggestions welcomed, thank you very much~ Good day. Kay -------------- next part -------------- An HTML attachment was scrubbed... URL: From rhettnaxel at gmail.com Thu Dec 15 14:24:37 2011 From: rhettnaxel at gmail.com (Alexander) Date: Thu, 15 Dec 2011 14:24:37 -0500 Subject: [Tutor] Localhost client-server simple ssl socket test program problems In-Reply-To: References: Message-ID: 2011/12/15 Yang Chun-Kai > Hello,everyone!! > > I am writing a simple ssl client-server test program on my personal laptop. > > And I encounter some problems with my simple programs. > > Please give me some helps. > > -------------------------------------------------------------------------------------------------------------------------------------------------------- > > My server code: > > import socket > import ssl > bindsocket = socket.socket() > bindsocket.bind(('127.0.0.1', 1234)) > bindsocket.listen(5) > print 'server is waiting for connection...' > newsocket, fromaddr = bindsocket.accept() > print 'start ssl socket...' > connstream = ssl.wrap_socket(newsocket, server_side=True, > certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", > keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", > ssl_version=ssl.PROTOCOL_SSLv23) > data = connstream.read() > print 'connected from address', fromaddr > print 'received data as', repr(data) > connstream.close() > > My client code: > > import socket > import ssl > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) > ssl_sock = ssl.wrap_socket(s, > ca_certs="/home/ckyang/PHA/testsslsocket/myCA.crt", > cert_reqs=ssl.CERT_REQUIRED) > ssl_sock.connect(("127.0.0.1", 1234)) > ssl_sock.write("hello") > ssl_sock.close() > > > ----------------------------------------------------------------------------------------------------------------------------------------------------------- > Server side error: > > File "views.py", line 17, in & lt;module> > connstream = ssl.wrap_socket(newsocket, server_side=True, > certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", > keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", > ssl_version=ssl.PROTOCOL_SSLv23) > File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket > ciphers=ciphers) > File "/usr/lib/python2.7/ssl.py", line 119, in __init__ > ciphers) > ssl.SSLError: [Errno 336265218] _ssl.c:347: error:140B0002:SSL > routines:SSL_CTX_use_PrivateKey_file:system lib > > Client side error: > > File "client.py", line 10, in > ssl_sock.connect(("127.0.0.1", 1234)) > File "/usr/lib/python2.7/ssl.py", line 299, in connect** > self.do_handshake() > File "/usr/lib/python2.7/ssl.py", line 283, in do_handshake > self._sslobj.do_handshake() > socket.error: [Errno 104] Connection reset by peer > > > ------------------------------------------------------------------------------------------------------------------------------------------------------------ > So what is wrong with my code? > > The codes are so simple and so much like python official site sample > demonstration, but I still cant get it work, so frustrating. > > Seems the problem happened on server side then cause client side cant > connect well, is that right? > > ** > My platform is ubuntu, with openssl 0.9.8 and python 2.7. > > All certificates and keys self-signed by openssl for test convenience. > > This is the site for referrence : > http://andyjeffries.co.uk/articles/x509-encrypted-authenticated-socket-ruby-client > > Or should I need a real certificate issued by a real CA to let things work? > > Any tips or suggestions welcomed, thank you very much~ > > Good day. > > Kay > > ** > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > You're trying to connect to the same port on localhost as a client and a server? I don't know for certain but I don't think that should work. Two computers? -- Alexander 7D9C597B -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Dec 15 14:27:09 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 19:27:09 +0000 Subject: How to generate "a, b, c, and d"? In-Reply-To: <4EEA41C5.60107@tim.thechases.com> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> <4EEA3A47.5020106@stoneleaf.us> <4EEA41C5.60107@tim.thechases.com> Message-ID: <4EEA4A0D.5030406@mrabarnett.plus.com> On 15/12/2011 18:51, Tim Chase wrote: > On 12/15/11 12:19, Ethan Furman wrote: >> Tim Chase wrote: >>> On 12/15/11 10:48, Roy Smith wrote: >>>> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the >>>> string, "a, b, c, and d" (I'll settle for no comma after 'c'). >>>> Is there some standard way to do this, handling all the special >>>> cases? >>> >>> If you have a list, it's pretty easy as MRAB suggests. For >>> arbitrary iterators, it's a bit more complex. Especially with >>> the odd edge-case of 2 items where there's no comma before the >>> conjunction (where>2 has the comma before the conjunction). If >>> you were willing to forgo the Oxford comma, it would tidy up the >>> code a bit. >> >> Why go through all that instead of just converting the iterator >> into a list at the beginning of MRAB's solution and then running >> with it? > > For the fun/challenge? Because you have a REALLY big data source > that you don't want to keep in memory (in addition the resulting > string)? > > Yeah, for most non-pathological cases, it would make more sense to > just make it a list and then deal with the 4 cases (no elements, one > element, 2 elements, and>2 elements) individually. > I was going to question it too, but then I wondered what would happen if there were a very large number of items and the string would be too big for memory, for example, writing a list of all the numbers from one to a billion to a file. From python at mrabarnett.plus.com Thu Dec 15 14:34:04 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 19:34:04 +0000 Subject: file data => array(s) In-Reply-To: <388939ad-b465-4dd0-8eb8-51395ee643a7@o7g2000yqk.googlegroups.com> References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> <388939ad-b465-4dd0-8eb8-51395ee643a7@o7g2000yqk.googlegroups.com> Message-ID: <4EEA4BAC.3070806@mrabarnett.plus.com> On 15/12/2011 18:37, Eric wrote: [snip] > > Neat. This is what I had in mind for a python-esque solution. [snip] FYI, the word is "Pythonic" when talking about the programming language. The word "Pythonesque" refers to Monty Python. From lists at cheimes.de Thu Dec 15 14:45:43 2011 From: lists at cheimes.de (Christian Heimes) Date: Thu, 15 Dec 2011 20:45:43 +0100 Subject: Localhost client-server simple ssl socket test program problems In-Reply-To: References: Message-ID: Am 15.12.2011 20:09, schrieb Yang Chun-Kai: > Server side error: > > File "views.py", line 17, in > connstream = ssl.wrap_socket(newsocket, server_side=True, > certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", > keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", > ssl_version=ssl.PROTOCOL_SSLv23) > File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket > ciphers=ciphers) > File "/usr/lib/python2.7/ssl.py", line 119, in __init__ > ciphers) > ssl.SSLError: [Errno 336265218] _ssl..c:347: error:140B0002:SSL > routines:SSL_CTX_use_PrivateKey_file:system lib This error is most likely caused by an encrypted private key. Python's SSL lib doesn't support encrypted private keys for sockets. You can encrypt the private key with openssl rsa -in /etc/home/ckyang/PHA/testsslsocket/mypha.key -out /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key Christian From invalid at invalid.invalid Thu Dec 15 14:50:27 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 15 Dec 2011 19:50:27 +0000 (UTC) Subject: Screen Control Fullscreen ON/OFF References: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> Message-ID: On 2011-12-15, Nizamov Shawkat wrote: >>> >>> I would like to make fullscreen white and fullscreen black using >>> Python on Linux. With in the specs of the LCD, I want to be able to >>> display fullscreen white and black approximately at 30Hz. Frequency >>> (on/off per second) will be input manually which is between 1-40Hz. >>> Any idea where to start ? Can Python perform such fast thing ? >> >> Forgot to mention and sorry for the pollution ; >> >> My LCD has 2ms respond time thus it can handle a maximum of 50Hz ON/ >> OFF (white/black) thus seems to fit my 1-40Hz range. I am not asking >> or looking for a fully working code, I just need some directions. > > > It depends on whether you want sync to vblank or not. If not, that is > pretty easy - use sleep() or something similar. If you have to use > sync (screen is always either black or white, never partly black and > white) then it is much much more difficult. Actually I do not know of > any way to sync to it. I'm _guessing_ the results won't be acceptible unless you switch during the veritical blanking period. (_is_ there a vertical blanking period with a DVI or HDMI output?). I have vague recollections that I think opengl has methods to do that. If there is an easy way to do it, it'll probably be using pygame: http://pygame.org/news.html If pygame can't do it, try pyopengl: http://pyopengl.sourceforge.net/ Here's a thread on how to do it on OSX: http://www.idevgames.com/forums/thread-2974.html -- Grant Edwards grant.b.edwards Yow! My face is new, my at license is expired, and I'm gmail.com under a doctor's care!!!! From waitmeforever at hotmail.com Thu Dec 15 15:09:14 2011 From: waitmeforever at hotmail.com (Yang Chun-Kai) Date: Fri, 16 Dec 2011 04:09:14 +0800 Subject: Localhost client-server simple ssl socket test program problems In-Reply-To: References: , Message-ID: Thanks for tips. But I dont understand one thing is if Python's SSL lib doesn't support encrypted private keys for sockets. Then why should we "encrypt" the private key with "openssl rsa -in /etc/home/ckyang/PHA/testsslsocket/mypha.key -out /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key" again? Shouldn't that be decrypted? And also this solution is not the right one, I use mypha-nopasswd.key replace the original one, still not work. So sad. But thanks. ^ ^ Kay > To: python-list at python.org > From: lists at cheimes.de > Subject: Re: Localhost client-server simple ssl socket test program problems > Date: Thu, 15 Dec 2011 20:45:43 +0100 > > Am 15.12.2011 20:09, schrieb Yang Chun-Kai: > > Server side error: > > > > File "views.py", line 17, in > > connstream = ssl.wrap_socket(newsocket, server_side=True, > > certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", > > keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", > > ssl_version=ssl.PROTOCOL_SSLv23) > > File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket > > ciphers=ciphers) > > File "/usr/lib/python2.7/ssl.py", line 119, in __init__ > > ciphers) > > ssl.SSLError: [Errno 336265218] _ssl..c:347: error:140B0002:SSL > > routines:SSL_CTX_use_PrivateKey_file:system lib > > This error is most likely caused by an encrypted private key. Python's > SSL lib doesn't support encrypted private keys for sockets. You can > encrypt the private key with > > openssl rsa -in /etc/home/ckyang/PHA/testsslsocket/mypha.key -out > /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key > > Christian > > > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From fabiofz at gmail.com Thu Dec 15 15:18:14 2011 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Thu, 15 Dec 2011 18:18:14 -0200 Subject: PyDev 2.3.0 Released Message-ID: Hi All, PyDev 2.3.0 has been released Details on PyDev: http://pydev.org Details on its development: http://pydev.blogspot.com Release Highlights: ------------------------------- * Pep8.py integrated (must be enabled in PyDev > Editor > Code Analysis > pep8.py). * Faster PyDev startup (internal Jython upgraded to version 2.2.1 -- and also optimized for PyDev). * Action to select/deselect scope (Shift+Alt+Up/Down). * Fix: cache issue where the PYTHONPATH in memory became different from the PYTHONPATH configured for a project. * Fix: OutOfMemoryError when dealing with PyOpenGL. * Fix: deadlock (could occur in a race condition when importing a project with an existing Python configuration). * Fix: code-completion integration issue with IPython 011 (patch from jonahkichwacoders). * Fix: annotation could remain in editor after removing a marker. * Fix: BadLocationException on extract local refactoring. What is PyDev? --------------------------- PyDev is a plugin that enables users to use Eclipse for Python, Jython and IronPython development -- making Eclipse a first class Python IDE -- It comes with many goodies such as code completion, syntax highlighting, syntax analysis, refactor, debug and many others. Cheers, -- Fabio Zadrozny ------------------------------------------------------ Software Developer Appcelerator http://appcelerator.com/ Aptana http://aptana.com/ PyDev - Python Development Environment for Eclipse http://pydev.org http://pydev.blogspot.com From lists at cheimes.de Thu Dec 15 15:19:14 2011 From: lists at cheimes.de (Christian Heimes) Date: Thu, 15 Dec 2011 21:19:14 +0100 Subject: Localhost client-server simple ssl socket test program problems In-Reply-To: References: , Message-ID: Am 15.12.2011 21:09, schrieb Yang Chun-Kai: > Thanks for tips. > > But I dont understand one thing is if Python's SSL lib doesn't support > encrypted private keys for sockets. > > Then why should we "encrypt" the private key with "openssl rsa -in > /etc/home/ckyang/PHA/testsslsocket/mypha.key -out > > /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key" again? > > Shouldn't that be decrypted? > > And also this solution is not the right one , I use mypha-nopasswd.key > replace the original one, still not work. IIRC the command should decrypt the key. Did it prompt for a password? The error could be caused by other issues. For example the key and cert must be in PEM Format. The PKS#12 isn't supported. I'm not sure if Python's builtin SSL module loads DER certs. You may also missing a valid CA cert chain. Python 2.x's SSL module doesn't support cert directories so you have to provide a chain file. The certs in the chain file must be in the right order, too. Christian From greg.ewing at canterbury.ac.nz Thu Dec 15 15:34:52 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 16 Dec 2011 09:34:52 +1300 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <9kv3vhFn8U1@mid.individual.net> MRAB wrote: >> To give an analogy, it is like defining mammals as "hairy animals which >> give birth to live young", which is correct for all mammals except for >> monotremes, which are mammals which lay eggs. > > Or the naked mole-rat. Or cetaceans (whales). The way I understand it, the main characteristic shared by all mammals is the presence of mammary glands in females. To wrest this back on topic, sometimes a definition can be improved without making it any more complicated. In the case of methods, perhaps instead of "defined inside a class body" it could say "bound to a name in a class namespace". That's what really matters, not how it came to be there. -- Greg From bouleetbil at gmail.com Thu Dec 15 15:36:07 2011 From: bouleetbil at gmail.com (bouleetbil bouleetbil) Date: Thu, 15 Dec 2011 21:36:07 +0100 Subject: LoadLibrary and extern ctype Message-ID: Hi, I've a little problem for define this : extern enum __pmerrno_t { PM_ERR_MEMORY = 1, PM_ERR_SYSTEM, .... PM_ERR_WRONG_ARCH } pm_errno; I've write : pacman=cdll.LoadLibrary("libpacman.so") ... #errors ( PM_ERR_MEMORY , PM_ERR_SYSTEM, ... PM_ERR_WRONG_ARCH )=map(ctypes.c_int, xrange(1,63)) I would use pacman.pm_errno. That should be a integer but if I print the value it I've : <_FuncPtr object at 0xb73ce984> Howto find the value (or define ) pacman.pm_errno ? The complete code can be find here : http://dors.frugalware.org/tmpgit/frugal-tweak/py-pacman/py-pacman.py and the .h is here http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=blob_plain;f=lib/libpacman/pacman.h;h=0f2a191681085dee99f485bad43a620043528904;hb=HEAD -- #--------------------------------------------------------------------------- # Bouleetbil # #--------------------------------------------------------------------------- # _ _ # (o)--(o) # /.______.\ # \_______/ # ./ \. # ( . , ) # \ \_\\//_/ / # ~~ ~~ ~~ #--------------------------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu Dec 15 15:36:25 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Dec 2011 07:36:25 +1100 Subject: [Tutor] Localhost client-server simple ssl socket test program problems In-Reply-To: References: Message-ID: On Fri, Dec 16, 2011 at 6:24 AM, Alexander wrote: > You're trying to connect to the same port on localhost as a client and a > server? I don't know for certain but I don't think that should work. > Two computers? That's bind() on the server and connect() on the client - the normal way to do things. That part of the code is fine (far as I can tell - haven't actually run the code, just eyeballed it). Chris Angelico From joshua.landau.ws at gmail.com Thu Dec 15 15:42:37 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 15 Dec 2011 20:42:37 +0000 Subject: Overriding a global In-Reply-To: <4EE8D78B.108@sequans.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> <4EE8D78B.108@sequans.com> Message-ID: On 14 December 2011 17:06, Jean-Michel Pichavant wrote: > Joshua Landau wrote: > >> [snip] >> Using currentLogger is just padding, in my opinion. *Every *value is >> "current". >> > Not always. I try to keep names on the same object because that object is > supposed to be named that way. > I can change one of the object attribute, but the object named that way > keep being the same. > > Class Foo: > self.__init__(self): > self.banana = 5 > > myFoo = Foo() > > Now there's a slight difference between > > myFoo = Exception() > and > myFoo.banana = 4 > > The first statement rebind myFoo to something complitely different. > the second statement change one of the object rightfully named myFoo .. > attribute (not sure about this construct :D ) > > Int being inmutable, you can rebind a name without changing its meaning. > type(myFoo) before != type(myFoo) after type(myFoo.bannana) before == type(myFoo.bannana) after type(logger) before == type(logger) after You are changing the int to a new value, but it's OK for you - if I understand rightly - because it's not changing its "meaning". The "meaning" of logger is the same too, as it does the same thing, in approximately the same way. I agree that "logger = Exception()" would be vary bad in that sense, too. The point is you use it the same way and it's not going to cause problems from misunderstanding what the object type is. And because "logger = childLogger('this_function')" is explicit and hopefully at the top 'o the function, any conceivable errors should be pretty obvious. Note that I used quotation marks around "meaning" not to mock the use or whatnot, but because I was trying to show that I was trying to use the same version of the word you did. I don't know if that was clear from the passage itself. That said, you did keep the myFoo the same, even with the int change. That makes the change global to all myFoo instances. But I didn't get that as a main point from your argument, and if it was I fear we're too close to recursive arguments. And in regards to your own commentary on your English, I wouldn't like you to see it as trouble. I'm currently thoroughly enjoying the debate, and wouldn't like to see myself scaring you off... -------------- next part -------------- An HTML attachment was scrubbed... URL: From waitmeforever at hotmail.com Thu Dec 15 15:58:01 2011 From: waitmeforever at hotmail.com (Yang Chun-Kai) Date: Fri, 16 Dec 2011 04:58:01 +0800 Subject: Localhost client-server simple ssl socket test program problems In-Reply-To: References: , , , , Message-ID: Hello~ Thanks for your fast reply. No, it doesn't ask for password, just a single line with "writing RSA kay", then mypha-nopasswd.key appeared. If my key is not in PEM Format, can openssl with simple commands to switch it to? Or I should re-do the self-signed process with some certain key-words / parameters? And what you mean about Python 2.x's SSL module doesn't support cert directories ? Can you be more specific about that ^^. Do you mean parameters with certfile and keyfile those two should put together or CA certificate need to be chained with other CA? Thanks. Kay > To: python-list at python.org > From: lists at cheimes.de > Subject: Re: Localhost client-server simple ssl socket test program problems > Date: Thu, 15 Dec 2011 21:19:14 +0100 > > Am 15.12.2011 21:09, schrieb Yang Chun-Kai: > > Thanks for tips. > > > > But I dont understand one thing is if Python's SSL lib doesn't support > > encrypted private keys for sockets. > > > > Then why should we "encrypt" the private key with "openssl rsa -in > > /etc/home/ckyang/PHA/testsslsocket/mypha.key -out > > > > /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key" again? > > > > Shouldn't that be decrypted? > > > > And also this solution is not the right one , I use mypha-nopasswd.key > > replace the original one, still not work. > > IIRC the command should decrypt the key. Did it prompt for a password? > > The error could be caused by other issues. For example the key and cert > must be in PEM Format. The PKS#12 isn't supported. I'm not sure if > Python's builtin SSL module loads DER certs. > > You may also missing a valid CA cert chain. Python 2.x's SSL module > doesn't support cert directories so you have to provide a chain file. > The certs in the chain file must be in the right order, too. > > Christian > > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From icanbob at gmail.com Thu Dec 15 16:17:29 2011 From: icanbob at gmail.com (bobicanprogram) Date: Thu, 15 Dec 2011 13:17:29 -0800 (PST) Subject: Ann: Python-SIMPL online tutorial updated to include examples of cloud interfacing Message-ID: The Python-SIMPL tutorial is at: http://www.icanprogram.com/06py/lesson1/lesson1.html Several lessons (3,4, 6 &7) have been enhanced to include examples of Python apps interfacing to the cloud housed on a Linode. The open source SIMPL toolkit provides a very lean Send/Receive/Reply (QNX style) messaging library. A SIMPL application consists of 2 or more interacting SIMPL modules. A SIMPL module can be written in any number of supported languages including Python, C, C++, JAVA, Tcl/Tk or PHP. More importantly they can be mixed in any given SIMPL application. Through the use of generic surrogates, SIMPL modules are "naturally" cloud aware often without even a recompile. In this tutorial a SIMPL receiver module (written in C) is exported unchanged to the Linode cloud. Examples of Python modules connecting to this Linode hosted module via the SIMPL TCP/IP surrogates, SIMPL tclSurrogate daemon, and Python-CGI are all illustrated. bob From ian.g.kelly at gmail.com Thu Dec 15 16:22:37 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 15 Dec 2011 14:22:37 -0700 Subject: How to generate "a, b, c, and d"? In-Reply-To: <4EEA41C5.60107@tim.thechases.com> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> <4EEA3A47.5020106@stoneleaf.us> <4EEA41C5.60107@tim.thechases.com> Message-ID: On Thu, Dec 15, 2011 at 11:51 AM, Tim Chase wrote: > For the fun/challenge? ?Because you have a REALLY big data source that you > don't want to keep in memory (in addition the resulting string)? If you have that much data, then I question why you would want to build such a large human-readable list in the first place. Nobody is going to want to read that no matter how you format it. From tjreedy at udel.edu Thu Dec 15 18:15:01 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Dec 2011 18:15:01 -0500 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <4f2f6742-b1b2-4605-8004-b6aa431b8513@k5g2000pra.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4f2f6742-b1b2-4605-8004-b6aa431b8513@k5g2000pra.googlegroups.com> Message-ID: On 12/15/2011 6:04 AM, rusi wrote: > On Dec 15, 3:58 pm, Chris Angelico wrote: >> On Thu, Dec 15, 2011 at 9:47 PM, Robert Kern wrote: >>> 42 = 2 mod 5 >>> 2 = 42 mod 5 >> >> It might make more sense to programmers if you think of it as written: >> >> 42 = 2, mod 5 >> 2 = 42, mod 5 Better, using ascii text, would be 42 =mod5 2 where =mod is a parameterized equivalence relation that is coarser than = (which is =mod-infinity). divmod(a,inf) = 0,a. =mod1 is the most coarse relation in that it make every count equivalent. divmod(a,1) = a,1. > For the record I should say that the guy who taught me abstract > algebra, said about as much: > He said that the notation > a == b mod n > should be written as > a ==n b > (read the == as 3 horizontal lines and the n as a subscript) The 3 horizontal line symbol is often used for equivalence relations other than =. -- Terry Jan Reedy From passiday at gmail.com Thu Dec 15 18:20:34 2011 From: passiday at gmail.com (Passiday) Date: Thu, 15 Dec 2011 15:20:34 -0800 (PST) Subject: Can't get Apache2 tp process py (Ubuntu 11.10) In-Reply-To: <1544851.511.1323964197163.JavaMail.geo-discussion-forums@vbxw7> References: <1544851.511.1323964197163.JavaMail.geo-discussion-forums@vbxw7> Message-ID: <22446792.35.1323991234400.JavaMail.geo-discussion-forums@yqkx20> Ok, figured this out by myself. There was an apache config conflict, and the config without mod_python enabled took over the config with mod_python enabled. From jdjodrey at yahoo.com Thu Dec 15 19:20:49 2011 From: jdjodrey at yahoo.com (Joshua Jodrey) Date: Thu, 15 Dec 2011 16:20:49 -0800 (PST) Subject: Python Interpreter Error with PyDev (Eclipse IDE) Message-ID: <1323994849.34934.YahooMailNeo@web120504.mail.ne1.yahoo.com> Hi, This is my first email to this list, so I apologize if there's a better mailing-list for this type of question. I'm looking to get started with python development, so I downloaded Python 3.2.2 and PyDev IDE for Eclipse. ?When I go to configure my interpreter for a new python project, Windows gives me the following error: "python.exe has stopped working" and then Eclipse shows "Error getting info on interpreter "See error log for details. No output was in the standard output when trying to create the interpreter info. The error output contains:>>Fatal Python error: Py_Initialize: unable to load the file system codec ? File "C:\csvn\Python25\\lib\encodings\__init__.py", line 120 ? ? raise CodecRegistryError,\ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^ SyntaxError: invalid syntax This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. <<" Does anybody have any idea what this means or what I'm doing wrong? ?I've tried?re-installing?Python, but still can't get it to work. And again, if I'm in the wrong place, please direct me to the proper mailing-list or forum. Thanks! Josh P.S. I'm not?subscribed?to this mailer, so please include me in your replies! -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Thu Dec 15 19:39:17 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Dec 2011 19:39:17 -0500 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4ee97f20$0$11091$c3e8da3@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: On 12/15/2011 12:01 AM, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: > >> On 12/14/2011 3:01 AM, Steven D'Aprano wrote: >>> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: >>> >>>> To complement what Eric says below: The with statement is looking for >>>> an instance *method*, which by definition, is a function attribute of >>>> a *class* (the class of the context manager) that takes an instance of >>>> the class as its first parameter. Note in the above that I am talking about *instance methods*. >>> I'm not sure that is correct... I don't think that there is anything >>> "by definition" about where methods live. Since you are disagreeing with my statement that *instance methods* are class attributes, you had better be claiming that *instance methods* can live elsewhere, else your statement has no sense. >> From the Python glossary: >> "method: A function which is defined inside a class body." >> >> That is actually a bit too narrow, as a function can be added to the >> class after it is defined. But the point then is that it is treated as >> if defined inside the class body. Actually, I agree with Greg that rewording like he or I have suggested would be a good idea. > So I'm happy for the glossary entry to stay as is, because complicating > it would confuse the average coder more than it would educate them. > But the definition as given is strictly wrong, because it fails to > capture the essence of what distinguishes a method from other objects. It > mistakes *how* you normally create a method for *what* a method is, a > little like defining "a hamburger is a foodstuff you get from McDonalds > by giving them money". > > Here are three ways that the definition fails: > > (1) You can create a function inside a class, and it remains a function, > so long as the class constructor (metaclass) never gets to build a method > object from the function. It is easy to do: just hide it inside a wrapper > object. > >>>> class FunctionInsideClass(object): > ... def func(x, y): # define a function inside a class > ... return x + 2*y > ... print(type(func)) # confirm is actually is a function > ... attr = (func,) # hide it from the metaclass > ... del func The function is not an attribute of the class, so my revised definition does not fail. > >>>> print(type(FunctionInsideClass.attr[0])) > > (2) Instead of hiding the function from the metaclass, you can change the > metaclass to something which doesn't make methods out of functions. I > won't show an example, because it's tricky to get right (or at least *I* > find metaclasses tricky). The default metaclass does not 'make methods out of functions'. Rather, functions that are attributes of an instance of 'type' are treated as methods *when accessed* via an instance of that instance. In Py 2, they were wrapped as unbound methods when accessed via the class, but no longer in Py 3, which simplifies things. Now, perhaps you can define a metaclass that disables method behavior, but practically everything one say about normal Python functioning goes out the window when one invokes 'metaclasses' (which do not even have to be classes!). So I do not consider this relevant to the discussion. > (3) So the definition is too broad: you can have functions defined inside > classes that are not methods. But it is also too narrow: you can have > methods outside of classes. I'm not talking about bound and unbound > methods, but about *creating* the method from scratch outside of a class. > When you call the method constructor directly, you can create a method > from a function defined outside of a class. > >>>> def func(self, a): > ... return self + a > ... >>>> type(func) # Definitely a function. > >>>> obj = types.MethodType(func, 42) As I explained before, the intended input of MethodType is an *instance method* and an instance of the class it is an attribute of. (Or, I suspect, a class method and class, which is why the appropriate check in each case should be outside the call. But I am sticking with instance methods here.) If so, the output is a *bound method*. In your example above, func is not an instance method and obj is not a bound method. It is simply an partially evaluated curried function or if you prefer, a bound function. Take you pick, or make up your own term, but it is NOT an instance method, which is the subject under discussion. So obj has nothing to do with the definition of instance method and whether I had any authority for the definition I gave to the OP to help him solve his problem. >>>> obj(23) # Works as expected. > 65 Yes, and I can think of three other ways to make an 'add42' function. >>>> type(obj) > So what? That means 'bound method', but since your input function to MethodType is not a method, its output is not a bound method. > So there's a method which has never been inside a class, and couldn't > even if you tried: int is a built-in immutable type. Calling it a 'method' when it is not even a bound method does not make it an instance method, which is the subject of discussion. > So what are methods? In Python, methods are wrappers around functions > which automatically pass the instance to the inner function object. These are bound methods. The instance methods are the functions wrapped. >>> Particularly not in Python where >>> instance methods can be attributes of the instance itself. >> >> This is access, not definition or actual location. > > Not so. In the example I gave, the method *really is* inside the > instance, stored in the instance __dict__ and not the class __dict__. Calling the object stored in the instance __dict__ a 'method' does not make it an instance method. >> The glossary entry go >> on to say: "If called as an attribute of an instance of that class, the >> method will get the instance object as its first argument (which is >> usually called self)." This does *not* happen if a callable is found in >> the instance-specific dictionary. > > That's right. Here you agree that instance methods are special because of where they are and how accessed, because that is what the glossary, with my comment, just said. > Methods are special not because of where they are, Here you disagree. >> An instance method is a function >> (callable) attribute of a class that gets special treatment when >> accessed (indirectly) through an instance of that class (or subclass >> thereof). > > Methods aren't functions at all, not in the isinstance sense. Please, I just specifically clarified that I meant function in the generic mathemetical callable sense. There is no single function class for there to be an 'isinstance sense'. 'Callable(f)' means 'hasattr(f, '__call__') [snip] >> 'types.MethodType' is the exposed name of the class the interpreter uses >> to create bound methods from a method and an instance of the class >> containing the method. I believe the interpreter does an isinstance >> check, but it must do that before calling the class, and not in the >> bound method constructor itself. In any case, a bound method is not a >> method. Not an instance method, which is the usual default meaning of 'method' when not qualified. Sorry if you missed that and got confused. >> In this case, the result is not really even a bound method, as the >> function argument is not a method, so we cannot even ask if the second >> arg is an instance of the function class container. MethodType is a >> special case of functools.partial, which was added later. You could have >> used the latter to the same effect. Or you could have used any old >> function that printed the same thing. > > Good grief. Is it really your argument that the types.MethodType isn't > actually the type of methods, Good grief. As I explained, it is the type of *bound methods*. When you feed it an instance method and an object of the method's class, it outputs a bound (instance) method. When you feed it a class method and the corresponding class, I presume it outputs a bound (class) method. These are the two ways the interpreter uses it. If a user such as you feeds it any old function and an object that has no relation to the function (other than its signature), then the result is a generic bound function and not specifically a bound method. > but a fake that lies about returning methods? It returns a bound method when you input a (instance/class) method, as the interpreter does in its routine operation. I am baffled that you are so insistent on confusing instance methods with bound instance methods and bound functions. I told the OP that he needed instance methods and what they are and that indeed is what they are and what he needs. -- Terry Jan Reedy From tjreedy at udel.edu Thu Dec 15 19:57:22 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Dec 2011 19:57:22 -0500 Subject: How to generate "a, b, c, and d"? In-Reply-To: <4EEA2DF2.9080107@mrabarnett.plus.com> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA2DF2.9080107@mrabarnett.plus.com> Message-ID: On 12/15/2011 12:27 PM, MRAB wrote: > On 15/12/2011 16:48, Roy Smith wrote: >> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, >> "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some >> standard way to do this, handling all the special cases? >> >> [] ==> '' >> ['a'] ==> 'a' >> ['a', 'b'] ==> 'a and b' >> ['a', 'b', 'c', 'd'] ==> 'a, b, and c' >> >> It seems like the kind of thing django.contrib.humanize would handle, >> but alas, it doesn't. > > How about this: > > def and_list(items): > if len(items) <= 2: > return " and ".join(items) > > return ", ".join(items[ : -1]) + ", and " + items[-1] To avoid making a slice copy, last = items.pop() return ", ".join(items) + (", and " + last) I parenthesized the last two small items to avoid copying the long string twice with two appends. Even better is items[-1] = "and " + items[-1] return ", ".join(items) so the entire output is created in one operation with no copy. But I would only mutate the list if I started with items = list(iterable) where iterable was the input, so I was mutating a private copy. -- Terry Jan Reedy From tjreedy at udel.edu Thu Dec 15 20:02:05 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Dec 2011 20:02:05 -0500 Subject: test for list equality In-Reply-To: <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: On 12/15/2011 12:59 PM, Miki Tebeka wrote: >> My sort issue... as in this doesn't work >>>>> if x.sort == y.sort: > You're missing the () to make it a function call. > Also list.sort() returns none, it mutates the original list. > You can either > sorted(x) == sorted(y) > or > set(x) == set(y) or x.sort(); y.sort(); x == y -- Terry Jan Reedy From wuwei23 at gmail.com Thu Dec 15 21:14:14 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 15 Dec 2011 18:14:14 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: Eelco wrote: > To tie it back in with python language design; all the more reason not > to opt for pseudo-backwards compatibility. If python wants a remainder > function, call it 'remainder'. Not 'rem', not 'mod', and certainly not > '%'. Good luck with the PEP. > Its the more pythonic way; a self-describing name, rather than > poorly defined or poorly understood cryptology. "Although practicality beats purity." I'm still utterly agog that anyone finds the operator % confusing. From rosuav at gmail.com Thu Dec 15 21:42:17 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Dec 2011 13:42:17 +1100 Subject: How to generate "a, b, c, and d"? In-Reply-To: References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA2DF2.9080107@mrabarnett.plus.com> Message-ID: On Fri, Dec 16, 2011 at 11:57 AM, Terry Reedy wrote: > ?items[-1] = "and " + items[-1] > ?return ", ".join(items) This works only if you're sure there are at least two items, and if you don't mind two items coming out as "a, and b". ChrisA From python at mrabarnett.plus.com Thu Dec 15 21:58:40 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 16 Dec 2011 02:58:40 +0000 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EEAB3E0.80809@mrabarnett.plus.com> On 16/12/2011 02:14, alex23 wrote: > Eelco wrote: >> To tie it back in with python language design; all the more reason >> not to opt for pseudo-backwards compatibility. If python wants a >> remainder function, call it 'remainder'. Not 'rem', not 'mod', and >> certainly not '%'. Python has "def", "del", "int", "str", "len", and so on. "rem" or "mod" (Ada has both, I believe) would be in keeping with the language. > > Good luck with the PEP. > >> Its the more pythonic way; a self-describing name, rather than >> poorly defined or poorly understood cryptology. > > "Although practicality beats purity." > > I'm still utterly agog that anyone finds the operator % confusing. In financial circles it could be an operator for calculating percentages, eg. "5 % x" would be 5 percent of x. It's an oddity, but an established one. :-) From steve+comp.lang.python at pearwood.info Thu Dec 15 22:34:10 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Dec 2011 03:34:10 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4eeabc32$0$29979$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Dec 2011 05:35:55 -0800, Steve Howell wrote: > For the special methods like __enter__ and __exit__, the tricky part > isn't understanding what would happen once the methods were called; the > tricky part is getting them to be called in the first place, if they > were not declared inside the class or attached to the class. If you *must* have per-instance special methods, my advice is to use a bit of scaffolding like this: class Whatever: def __enter__(self, *args): try: enter = self.__dict__['__enter__'] except KeyError: do_something_else() # or just let the exception occur else: enter(*args) Otherwise just live with the limitation that you can override all methods per-instance *except* dunders, and design your application accordingly. -- Steven From rz1991 at foxmail.com Thu Dec 15 23:55:13 2011 From: rz1991 at foxmail.com (=?gbk?B?yO7vow==?=) Date: Fri, 16 Dec 2011 12:55:13 +0800 Subject: Help about Xlib Library in Python Message-ID: Hi, A question about Xlib Library in Python troubled me for several days and I finally found this email list. I hope someone could answer my question. I think it is easy for experienced user. I would like to write a small script to response my mouse click in root screen and write something in the terminal. My script is like this, but it does not work in my computer. from Xlib import X import Xlib.display def main(): display = Xlib.display.Display() root = display.screen().root root.change_attributes(event_mask= X.ButtonPressMask | X.ButtonReleaseMask) while True: event = root.display.next_event() print "1" if __name__ == "__main__": main() Any hints are welcome Thank you! Ruan zheng -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Fri Dec 16 00:01:57 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Dec 2011 16:01:57 +1100 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <4EEAB3E0.80809@mrabarnett.plus.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> Message-ID: On Fri, Dec 16, 2011 at 1:58 PM, MRAB wrote: > In financial circles it could be an operator for calculating > percentages, eg. "5 % x" would be 5 percent of x. > > It's an oddity, but an established one. :-) And I would be most sorry to see % renamed to mod in Python. "Hello, %s! My favourite number is %d." mod ("Fred",42) # This just looks wrong. ChrisA From deface.vr at gmail.com Fri Dec 16 00:16:37 2011 From: deface.vr at gmail.com (vengal b) Date: Thu, 15 Dec 2011 21:16:37 -0800 (PST) Subject: online data entry jobs Message-ID: <2c0d2aac-57fa-4a28-ab0c-2a70a7453857@n13g2000prf.googlegroups.com> online data entry jobs http://venuonlinejobs.blogspot.com From tjreedy at udel.edu Fri Dec 16 00:26:10 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 16 Dec 2011 00:26:10 -0500 Subject: How to generate "a, b, c, and d"? In-Reply-To: References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA2DF2.9080107@mrabarnett.plus.com> Message-ID: On 12/15/2011 9:42 PM, Chris Angelico wrote: > On Fri, Dec 16, 2011 at 11:57 AM, Terry Reedy wrote: >> items[-1] = "and " + items[-1] >> return ", ".join(items) > > This works only if you're sure there are at least two items, and if > you don't mind two items coming out as "a, and b". Please read the context that you removed. The original second return line and my replacements come after if len(items) <= 2: return " and ".join(items) -- Terry Jan Reedy From wuwei23 at gmail.com Fri Dec 16 00:30:48 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 15 Dec 2011 21:30:48 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> Message-ID: On Dec 16, 3:01?pm, Chris Angelico wrote: > And I would be most sorry to see % renamed to mod in Python. > > "Hello, %s! My favourite number is %d." mod ("Fred",42) ? # This just > looks wrong. Finally we can give this operator a more fitting name - I propose 'inject' - and put an end to this insane desire to leverage off pre- existing knowledge of other languages. Furthermore, I suggest that no two languages should ever have identical semantics, just to avoid potential confusion. New concepts for all! From ian.g.kelly at gmail.com Fri Dec 16 01:08:02 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 15 Dec 2011 23:08:02 -0700 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <4EEAB3E0.80809@mrabarnett.plus.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> Message-ID: On Dec 15, 2011 8:01 PM, "MRAB" wrote: > Python has "def", "del", "int", "str", "len", and so on. "rem" or "mod" > (Ada has both, I believe) would be in keeping with the language. I think I would have to object to "rem" purely on the basis that it denotes comments in BASIC. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alec.taylor6 at gmail.com Fri Dec 16 01:30:41 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 16 Dec 2011 17:30:41 +1100 Subject: test for list equality In-Reply-To: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> Message-ID: Just for fun, use the Hungarian Algorithm (Python implementation: http://software.clapper.org/munkres/) On Fri, Dec 16, 2011 at 3:36 AM, noydb wrote: > I want to test for equality between two lists. ?For example, if I have > two lists that are equal in content but not in order, I want a return > of 'equal' -- dont care if they are not in the same order. ?In order > to get that equality, would I have to sort both lists regardless? ?if > yes, how (having issues with list.sort)? > > Another way i tried, that I think is kind-of roundabout is like > x = [2, 5, 1, 88, 9] > y = [5, 2, 9, 1, 88] > inBoth = list(set(x) & set(y)) > > and then test that list.count is equal between inBoth and x and/or y. > > > Any better suggestions? > > Thanks for any help! > -- > http://mail.python.org/mailman/listinfo/python-list From greg.ewing at canterbury.ac.nz Fri Dec 16 01:56:19 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 16 Dec 2011 19:56:19 +1300 Subject: =?UTF-8?B?UmU6IEVSUCB3aXRoIFN1cHBseSBjaGFpbiBtYW5hZ2VtZW50IChpbmM=?= =?UTF-8?B?bC4gUE9TKSBhbmQgQ3VzdG9tZXIgcmVsYXRpb25zaGlwIG1hbmFnZW1lbnQg4oCU?= =?UTF-8?B?IFdoYXQncyBhdmFpbGFibGU/?= In-Reply-To: References: Message-ID: <9l08cjFlaoU1@mid.individual.net> > On Thu, Dec 15, 2011 at 5:54 AM, Anurag Chourasia > wrote: > >>I am building a POS/CRM (Loyalty Management) system as well. Is it just me, or does the phrase "Loyalty Management" have a faintly ominous ring to it? -- Greg From ian.g.kelly at gmail.com Fri Dec 16 02:11:10 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Dec 2011 00:11:10 -0700 Subject: test for list equality In-Reply-To: References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> Message-ID: On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor wrote: > Just for fun, use the Hungarian Algorithm > > (Python implementation: http://software.clapper.org/munkres/) That's a pretty silly approach, but okay: def listequals(a, b): if len(a) != len(b): return False matrix = [[int(item_a != item_b) for item_b in b] for item_a in a] path = Munkres().compute(matrix) return sum(matrix[r][c] for (r, c) in path) == 0 From ian.g.kelly at gmail.com Fri Dec 16 02:32:11 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Dec 2011 00:32:11 -0700 Subject: test for list equality In-Reply-To: References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> Message-ID: On Fri, Dec 16, 2011 at 12:11 AM, Ian Kelly wrote: > On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor wrote: >> Just for fun, use the Hungarian Algorithm >> >> (Python implementation: http://software.clapper.org/munkres/) > > That's a pretty silly approach, but okay: > > def listequals(a, b): > ? ?if len(a) != len(b): > ? ? ? ?return False > ? ?matrix = [[int(item_a != item_b) for item_b in b] for item_a in a] > ? ?path = Munkres().compute(matrix) > ? ?return sum(matrix[r][c] for (r, c) in path) == 0 Amendment -- it seems that Hungarian implementation fails on an empty matrix: def listequals(a, b): if len(a) == len(b) == 0: return True if len(a) != len(b): return False matrix = [[int(item_a != item_b) for item_b in b] for item_a in a] path = Munkres().compute(matrix) return sum(matrix[r][c] for (r, c) in path) == 0 From ulrich at dorda.net Fri Dec 16 03:52:11 2011 From: ulrich at dorda.net (Ulrich) Date: Fri, 16 Dec 2011 00:52:11 -0800 (PST) Subject: Class: @property -> .__dict__ Message-ID: Good morning, I wonder if someone could please help me out with the @property function as illustrated in the following example. class te(): def __init__(self): self.a = 23 @property def b(self): return 2 * self.a t = te() In [4]: t.a Out[4]: 23 In [5]: t.b Out[5]: 46 #works asexpected so far, now let's have a look into t.__dict__ In [6]: t.__dict__ Out[6]: {'a': 23} -> b does not show up. Could anyone please explain me why this does not work / how to get b into .__dict__ / hint me to an explanation? Thanks a lot in advance! Cheers, Ulrich From steve+comp.lang.python at pearwood.info Fri Dec 16 04:03:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Dec 2011 09:03:05 GMT Subject: Class: @property -> .__dict__ References: Message-ID: <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Dec 2011 00:52:11 -0800, Ulrich wrote: > Good morning, > > I wonder if someone could please help me out with the @property function > as illustrated in the following example. > > class te(): > def __init__(self): > self.a = 23 > @property > def b(self): > return 2 * self.a [...] > Could anyone please explain me why this does not work / how to get b > into .__dict__ / hint me to an explanation? b is a property object. Like any other assignment inside the body of the class, it is shared across all instances, so you won't find it in the instance's personal dict. You will find it in the shared class dict. t.__dict__['b'] # not found te.__dict__['b'] # will return the property object (By the way: it is the usual convention to start the name of a class with initial capital, so Te would be a better name.) To get something into the instance dict, you need to assign it onto the instance: t.x = 42 # puts 'x':42 into t.__dict__ -- Steven From ulrich.dorda at gmail.com Fri Dec 16 04:11:49 2011 From: ulrich.dorda at gmail.com (Ulrich) Date: Fri, 16 Dec 2011 01:11:49 -0800 (PST) Subject: Class: @property -> .__dict__ References: <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <3fe1021b-75ae-4e3d-a5b0-175ad00aefa4@i6g2000vbh.googlegroups.com> On Dec 16, 10:03?am, Steven D'Aprano wrote: > On Fri, 16 Dec 2011 00:52:11 -0800, Ulrich wrote: > > Good morning, > > > I wonder if someone could please help me out with the @property function > > as illustrated in the following example. > > > class te(): > > ? ? def __init__(self): > > ? ? ? ? self.a = 23 > > ? ? @property > > ? ? def b(self): > > ? ? ? ? return 2 * self.a > [...] > > Could anyone please explain me why this does not work / how to get b > > into .__dict__ / hint me to an explanation? > > b is a property object. Like any other assignment inside the body of the > class, it is shared across all instances, so you won't find it in the > instance's personal dict. You will find it in the shared class dict. > > t.__dict__['b'] ?# not found > te.__dict__['b'] ?# will return the property object > > (By the way: it is the usual convention to start the name of a class with > initial capital, so Te would be a better name.) > > To get something into the instance dict, you need to assign it onto the > instance: > > t.x = 42 ?# puts 'x':42 into t.__dict__ > > -- > Steven Hi Steven, Thanks a lot for your quick and helpful answer! This would imply that I have to search in the dict of the class and the dict of the instance. - works nicely. I wonder if there is somewhere a "merge of the two" already available. In the meantime, I came across dir() In [7]: dir(t) Out[7]: ['__doc__', '__init__', '__module__', 'a', 'b'] This seems to return 'a' and 'b', but now crashes @property def attributelist(self): # find all attributes to the class that are of type numpy arrays: return [attr for attr in self.__dict__ if isinstance(getattr(self, attr), numpy.ndarray)] From ulrich.dorda at gmail.com Fri Dec 16 04:14:41 2011 From: ulrich.dorda at gmail.com (Ulrich) Date: Fri, 16 Dec 2011 01:14:41 -0800 (PST) Subject: Class: @property -> .__dict__ References: <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> <3fe1021b-75ae-4e3d-a5b0-175ad00aefa4@i6g2000vbh.googlegroups.com> Message-ID: <08b0a976-dd2a-4728-b865-4d04f4e5b7fb@n10g2000vbg.googlegroups.com> On Dec 16, 10:11?am, Ulrich wrote: > On Dec 16, 10:03?am, Steven D'Aprano > > > > > > > > > +comp.lang.pyt... at pearwood.info> wrote: > > On Fri, 16 Dec 2011 00:52:11 -0800, Ulrich wrote: > > > Good morning, > > > > I wonder if someone could please help me out with the @property function > > > as illustrated in the following example. > > > > class te(): > > > ? ? def __init__(self): > > > ? ? ? ? self.a = 23 > > > ? ? @property > > > ? ? def b(self): > > > ? ? ? ? return 2 * self.a > > [...] > > > Could anyone please explain me why this does not work / how to get b > > > into .__dict__ / hint me to an explanation? > > > b is a property object. Like any other assignment inside the body of the > > class, it is shared across all instances, so you won't find it in the > > instance's personal dict. You will find it in the shared class dict. > > > t.__dict__['b'] ?# not found > > te.__dict__['b'] ?# will return the property object > > > (By the way: it is the usual convention to start the name of a class with > > initial capital, so Te would be a better name.) > > > To get something into the instance dict, you need to assign it onto the > > instance: > > > t.x = 42 ?# puts 'x':42 into t.__dict__ > > > -- > > Steven > > Hi Steven, > > Thanks a lot for your quick and helpful answer! > > This would imply that I have to search in the dict of the class and > the dict of the instance. - works nicely. > > I wonder if there is somewhere a "merge of the two" already available. > > In the meantime, I came across dir() > In [7]: dir(t) > Out[7]: ['__doc__', '__init__', '__module__', 'a', 'b'] > > This seems to return 'a' and 'b', but now crashes > > @property > def attributelist(self): > ? ? ? ? # find all attributes to the class that are of type numpy > arrays: > ? ? ? ? return [attr for attr in self.__dict__ if > isinstance(getattr(self, attr), numpy.ndarray)] hi again, I must have hit the send accidently before finishing. This attributelist should return me all attributes of type numpy.ndarry. if I replace it to def attributelist(self): # find all attributes to the class that are of type numpy arrays: return [attr for attr in dir(self) if isinstance(getattr(self, attr), numpy.ndarray)] it crashes going into some kind of endless loop. Do you happen to have any idea? thanks again! cheers, ulrich From steve+comp.lang.python at pearwood.info Fri Dec 16 04:22:35 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Dec 2011 09:22:35 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: [...] After reading your post, I think I have worked out where our disagreement lines: you think that bound methods and instance methods are not the same thing, and that a function defined inside a class is different from a function outside of a class. For example, you say: > If so, the output is a *bound method*. In your example > above, func is not an instance method and obj is not a bound method. It > is simply an partially evaluated curried function or if you prefer, a > bound function. Take you pick, or make up your own term, but it is NOT > an instance method, and later on: >> So what are methods? In Python, methods are wrappers around functions >> which automatically pass the instance to the inner function object. > > These are bound methods. The instance methods are the functions wrapped. I am afraid you are mistaken. What you say may very well apply to other languages, but in Python, def creates functions no matter where you execute it. Always and without exception. I admit to an earlier mistake: I thought that conversion from function to method occurred once, when the class statement was executed, but I was mistaken. Re-reading Raymond Hettinger's excellent article on the descriptor protocol reminded me that methods are created on an as-needed basis, at runtime. Back to methods and def. So let's see what happens in Python 3.1: >>> def spam(self): # Clearly a function. ... pass ... >>> class K(object): ... def ham(self): # Allegedly an instance method ... pass ... According to your various statements, spam is a function and ham is an instance method. Merely storing a outside function inside a class doesn't create an instance method, it creates what you call a bound method (although I'm unclear what you think the difference is). So let's see what happens when we compare an alleged "bound method that is not an instance method" with an actual instance method: >>> K.spam = spam >>> type(K.spam) is type(K.ham) True Their types are identical. We get the same thing when we compare an actual function with a function object created inside a class, which you claim is actually an instance method: >>> type(spam) is type(K.__dict__['ham']) # Bypass descriptor protocol True Nor is there any difference in type between bound and unbound methods: they are both instance methods differing only in whether or not they have the first argument "self" available. Bound is an adjective, not part of the type: small method, obfuscated method, buggy method, or bound method. Python 3 no longer uses unbound methods, since they are functionally identical to the unwrapped function, so this snippet is from Python 2.6: py> k = K() py> type(k.ham) is type(K().ham) # bound vs bound True py> type(k.ham) is type(K.ham) # bound vs unbound True In Python, instance methods are wrappers around function objects; they are created on call, and generally do not exist *anywhere* until needed, or if you store a reference to them. >>> k = K() >>> a = k.ham >>> b = k.ham >>> a is b False Under normal circumstances, the only persistent object is the function, which you can extract from the (class or instance) __dict__ or the method wrapper: >>> a.__func__ is K.__dict__['ham'] True Methods are created by the descriptor protocol: when you use the normal a.b syntax to access K.__dict__['ham'], the metaclass calls the __get__ method of function ham. If you call it from an instance, you get a method object bound to that instance. If you call it from the class, in Python 2 you get a method object not bound to an instance, in Python 3 you get the function without a wrapper. [Aside: class methods and static methods also work the same way, via __get__ and the descriptor protocol, but behave differently. Class methods reuse the instance method type, which is somewhat confusing, and static methods just return the function.] So, what the hell does all this mean in practice? Most of the time, absolutely nothing. That's why I started this discussion with the disclaimer that I didn't think it was worth changing the (not quite right) definition of "method" you originally quoted. Most of the time, we only access methods via the instance.method syntax, which gives us an instance method. By the principle that if it quacks like a duck and swims like a duck, it is good enough to call it a duck, I'm happy to agree that ham here is a method: class K: def ham(self): pass That covers 99% of all use-cases and is good enough for most situations. But the OP ran into one of those edge cases in the 1%, where things are not so simple. He was attempting to create an instance method called "__exit__" and store it in the instance __dict__ instead of the class __dict__. This is a perfectly reasonable thing to do, but there are two gotchas to it: * The descriptor protocol doesn't get used for instance lookups. That's why you can't stick properties in an instance, only in the class. * CPython, and I believe Jython, don't look up special dunder methods like __exit__ on the instance, only on the class. So even if the OP manually created an instance method rather than relying on the descriptor protocol, his per-instance __exit__ would not be called without extra work. -- Steven From __peter__ at web.de Fri Dec 16 04:32:45 2011 From: __peter__ at web.de (Peter Otten) Date: Fri, 16 Dec 2011 10:32:45 +0100 Subject: Class: @property -> .__dict__ References: <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> <3fe1021b-75ae-4e3d-a5b0-175ad00aefa4@i6g2000vbh.googlegroups.com> <08b0a976-dd2a-4728-b865-4d04f4e5b7fb@n10g2000vbg.googlegroups.com> Message-ID: Ulrich wrote: > if I replace it to > def attributelist(self): > # find all attributes to the class that are of type numpy > arrays: > return [attr for attr in dir(self) if > isinstance(getattr(self, attr), numpy.ndarray)] > > it crashes going into some kind of endless loop. > > Do you happen to have any idea? dir(self) finds an attribute named "attributelist", getattr(self, "attributelist") then tries to calculate the value of that attribute, invokes dir(self) which finds an attribute named "attributelist" and so on ad infinitum or the stack overflows. Try (untested) @property def attributelist(self): return [attr for attr in dir(self) if attr != "attributelist" and isinstance(getattr(self, attr), numpy.ndarray)] to avoid the infinite recursion. From ulrich.eckhardt at dominolaser.com Fri Dec 16 04:45:22 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 16 Dec 2011 10:45:22 +0100 Subject: modifying a time.struct_time Message-ID: Hi! I'm trying to create a struct_time that is e.g. one year ahead or a month back in order to test some parsing/formatting code with different dates. Now, the straightforward approach is t = time.localtime() t.tm_year += 1 This fails with "TypeError: readonly attribute". This kind-of makes sense, as an immutable object allows you to use it as key in a dict. The second approach is this: l = list(t) # convert to a sequence l[0] += 1 # increment year t = time.struct_time(l) # convert to a struct_time This works but is ugly, because the code relies on the order inside the list and uses magic numbers to access them. The order is AFAICT not accessible programmatically but only documented, and not even in a way that makes clear that it is part of the API and as such actualy guaranteed. I could try to assert that the indices match using "if l[0] is t.tm_year", but this is still ugly. The next approach I tried was to simply create a derived class: class my_time(time.struct_time): pass This fails again with "TypeError: Error when calling the metaclass bases, type 'time.struct_time' is not an acceptable base type. I could try to encapsulate a struct_time and delegate attribute access to it in order to do this, but it also seems overkill. Also, using an immutable type as a baseclass and delegating access to members seems like hackery to me, prone to fail in situations where it is least expected. Then I tried duck typing. If it quacks like a duck, it better not be a crocodile! This looks like this: struct my_time(object): pass t = my_time() t.tm_year = 2012 t.tm_month = 12 t.tm... # other fields accordingly time.mktime(t) This fails with "TypeError: argument must be 9-item sequence, not my_time". I thought about using a collections.namedtuple, because a namedtuple is a tuple and therefore also a sequence, but that only leads me back to the problem that time.mktime() takes a sequence and the order of the sequence is not accessible programmatically. A last approach was to convert the thing to a dict and back. Alas, there is no conversion to a dict, otherwise d = dict(t) d['tm_year'] += 1 t = time.struct_time(d) would have been a straightforward approach. Does anyone have a suggestion how to solve this elegantly and pythonically? Also, what I'm wondering is if the lack of a clear way should be considered a bug or not. Cheers! Uli From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Fri Dec 16 05:02:51 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Fri, 16 Dec 2011 11:02:51 +0100 Subject: Class: @property -> .__dict__ In-Reply-To: References: Message-ID: Am 16.12.2011 09:52 schrieb Ulrich: > Could anyone please explain me why this does not work / how to get b > into .__dict__ / hint me to an explanation? b is not a data element of the particular instance, but it lives in the class. It is, roughly spoken, a "kind of method", just to be used without calling. These things are called "data descriptors" (http://docs.python.org/reference/datamodel.html#descriptors). t.b is internally reflected to te.__dict__['b'].__get__(t, te) in order to call the __get__ method of the property object. This is done on every call and the value is, in your case, derived from the instance value a. HTH, Thomas From hoogendoorn.eelco at gmail.com Fri Dec 16 05:25:59 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Fri, 16 Dec 2011 02:25:59 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> Message-ID: <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> On Dec 16, 6:30?am, alex23 wrote: > On Dec 16, 3:01?pm, Chris Angelico wrote: > > > And I would be most sorry to see % renamed to mod in Python. > > > "Hello, %s! My favourite number is %d." mod ("Fred",42) ? # This just > > looks wrong. > > Finally we can give this operator a more fitting name - I propose > 'inject' - and put an end to this insane desire to leverage off pre- > existing knowledge of other languages. > > Furthermore, I suggest that no two languages should ever have > identical semantics, just to avoid potential confusion. > > New concepts for all! Dont get me started on that one. Its that I never work with strings... 'leverage of pre-existing knowledge'... I would hardly call the particular names of functions the knowledge about a language. The only argument that bears any weight with me is backwards compatibility with itself. Pseudo-backwards compatibility with other languages, I couldnt not care less for. From mazen.harake at gmail.com Fri Dec 16 05:32:13 2011 From: mazen.harake at gmail.com (Mazen Harake) Date: Fri, 16 Dec 2011 11:32:13 +0100 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: Hi, Easiest way is to change the time to seconds, add as many seconds as a year/month/week/day/hour/minutes represent and then transform it back. E.g. >>> time.time() 1324031491.026137 >>> time.time() + 3600 # Add an hour 1324035105.082003 >>> time.gmtime(time.time() + 3600) time.struct_time(tm_year=2011, tm_mon=12, tm_mday=16, tm_hour=11, tm_min=31, tm_sec=57, tm_wday=4, tm_yday=350, tm_isdst=0) >>> On 16 December 2011 10:45, Ulrich Eckhardt wrote: > Hi! > > I'm trying to create a struct_time that is e.g. one year ahead or a month > back in order to test some parsing/formatting code with different dates. > > Now, the straightforward approach is > > ?t = time.localtime() > ?t.tm_year += 1 > > This fails with "TypeError: readonly attribute". This kind-of makes sense, > as an immutable object allows you to use it as key in a dict. > > > The second approach is this: > > ?l = list(t) # convert to a sequence > ?l[0] += 1 # increment year > ?t = time.struct_time(l) # convert to a struct_time > > This works but is ugly, because the code relies on the order inside the list > and uses magic numbers to access them. The order is AFAICT not accessible > programmatically but only documented, and not even in a way that makes clear > that it is part of the API and as such actualy guaranteed. I could try to > assert that the indices match using "if l[0] is t.tm_year", but this is > still ugly. > > > The next approach I tried was to simply create a derived class: > > ?class my_time(time.struct_time): > ? ? ?pass > > This fails again with "TypeError: Error when calling the metaclass bases, > type 'time.struct_time' is not an acceptable base type. I could try to > encapsulate a struct_time and delegate attribute access to it in order to do > this, but it also seems overkill. Also, using an immutable type as a > baseclass and delegating access to members seems like hackery to me, prone > to fail in situations where it is least expected. > > > Then I tried duck typing. If it quacks like a duck, it better not be a > crocodile! This looks like this: > > ?struct my_time(object): pass > ?t = my_time() > ?t.tm_year = 2012 > ?t.tm_month = 12 > ?t.tm... # other fields accordingly > ?time.mktime(t) > > This fails with "TypeError: argument must be 9-item sequence, not my_time". > I thought about using a collections.namedtuple, because a namedtuple is a > tuple and therefore also a sequence, but that only leads me back to the > problem that time.mktime() takes a sequence and the order of the sequence is > not accessible programmatically. > > > A last approach was to convert the thing to a dict and back. Alas, there is > no conversion to a dict, otherwise > > ?d = dict(t) > ?d['tm_year'] += 1 > ?t = time.struct_time(d) > > would have been a straightforward approach. > > > Does anyone have a suggestion how to solve this elegantly and pythonically? > Also, what I'm wondering is if the lack of a clear way should be considered > a bug or not. > > > Cheers! > > Uli > > -- > http://mail.python.org/mailman/listinfo/python-list From hoogendoorn.eelco at gmail.com Fri Dec 16 05:36:50 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Fri, 16 Dec 2011 02:36:50 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2a747b91-23bd-4d84-87f7-96609ac3c1dd@ct2g2000vbb.googlegroups.com> On Dec 16, 3:58?am, MRAB wrote: > On 16/12/2011 02:14, alex23 wrote: > > > Eelco ?wrote: > >> To tie it back in with python language design; all the more reason > >> not to opt for pseudo-backwards compatibility. If python wants a > >> remainder function, call it 'remainder'. Not 'rem', not 'mod', and > >> certainly not '%'. > > Python has "def", "del", "int", "str", "len", and so on. "rem" or "mod" > (Ada has both, I believe) would be in keeping with the language. def and del are keywords, and thus in another league. Having shorthand notation for types is somewhat defensible, though I believe I would prefer a more verbose form there too; how often to you encounter these in python anyway? len is a bit of an eeysore to me too; I understand having it as a builtin is a matter of optimization or something, but I do wish we would be given the option of just saying list.length > > Good luck with the PEP. > > >> Its the more pythonic way; a self-describing name, rather than > >> poorly defined or poorly understood cryptology. > > > "Although practicality beats purity." > > > I'm still utterly agog that anyone finds the operator % confusing. > > In financial circles it could be an operator for calculating > percentages, eg. "5 % x" would be 5 percent of x. > > It's an oddity, but an established one. :-) Well yes, thats the only argument ive heard so far that resonated with me. These syntax details are not a very big deal, and backwards compatibility with yourself is quite a big deal. Its nice to keep 'what ought to have been done' and 'what ought we to do' seperate in such discussions. Im not sure we ought to change these syntax details (I mean relating to mod and such), but I am quite sure of what I would have done if I could go back in time. From rosuav at gmail.com Fri Dec 16 05:44:42 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Dec 2011 21:44:42 +1100 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: On Fri, Dec 16, 2011 at 8:45 PM, Ulrich Eckhardt wrote: > I'm trying to create a struct_time that is e.g. one year ahead or a month > back in order to test some parsing/formatting code with different dates. Do you need it to be one exact calendar year, or would it make sense to add/subtract integers from a Unix time? t = time.time() + 365*86400 # Not actually a year ahead, it's 365 days ahead t = time.localtime(t) # if you want a struct_time ChrisA From steve+comp.lang.python at pearwood.info Fri Dec 16 05:44:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Dec 2011 10:44:49 GMT Subject: modifying a time.struct_time References: Message-ID: <4eeb2120$0$29979$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Dec 2011 10:45:22 +0100, Ulrich Eckhardt wrote: > Hi! > > I'm trying to create a struct_time that is e.g. one year ahead or a > month back in order to test some parsing/formatting code with different > dates. [...] > The second approach is this: > > l = list(t) # convert to a sequence > l[0] += 1 # increment year > t = time.struct_time(l) # convert to a struct_time > > This works but is ugly, because the code relies on the order inside the > list and uses magic numbers to access them. The order is AFAICT not > accessible programmatically but only documented, and not even in a way > that makes clear that it is part of the API and as such actualy > guaranteed. I could try to assert that the indices match using "if l[0] > is t.tm_year", but this is still ugly. Feel free to propose a feature enhancement to time.struct_time, but the order of the fields is stable and won't change. So ugly or not, that way is guaranteed to work. [...] > Then I tried duck typing. If it quacks like a duck, it better not be a > crocodile! This looks like this: > > struct my_time(object): pass "struct"? [...] > Does anyone have a suggestion how to solve this elegantly and > pythonically? Also, what I'm wondering is if the lack of a clear way > should be considered a bug or not. Not a bug, but it does seem a very old and inelegant API more suited to hairy C programmers gathered around a smokey fire in a cave chewing on old dinosaur bones, and not worthy of space-age Python coders flying around on anti-gravity belts. -- Steven From mail at timgolden.me.uk Fri Dec 16 05:50:19 2011 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 16 Dec 2011 10:50:19 +0000 Subject: modifying a time.struct_time In-Reply-To: <4eeb2120$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4eeb2120$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EEB226B.7080000@timgolden.me.uk> On 16/12/2011 10:44, Steven D'Aprano wrote: [ on time.struct_time ] > Not a bug, but it does seem a very old and inelegant API more suited to > hairy C programmers gathered around a smokey fire in a cave chewing on > old dinosaur bones, and not worthy of space-age Python coders flying > around on anti-gravity belts. +1 QOTW TJG From richa.sinha at globallogic.com Fri Dec 16 05:54:34 2011 From: richa.sinha at globallogic.com (Richa Sinha) Date: Fri, 16 Dec 2011 16:24:34 +0530 Subject: GlobalLogic PYTHON/JAVA positions Message-ID: Hi All , Trust you doing well.. GlobalLogic is looking for highly skilled *Python Developers/Sr.Developers* , *JAVA DEVELOPERS/Sr. Developers* for one of our strategic engagements with * GOOGLE. * Following are the details: > >> *Python Developer(s)/Sr. Developers* >> >> >> >> Experience: 1-5 Yrs >> >> Min Qualification: BS/BE/B.Tech/MCA Regular >> >> Job Location: Client site- Gurgaon (India) Google office >> > Positions: 20 >> >> * * >> >> *Requisites:* >> >> >> - * * OOPS concepts in Python >> - Exposure to python as a language , not only as a script >> - Good knowledge of File handling and XML parsing >> - Good to have experience in Multithreading in python. >> - Usage of inbuilt data types. >> - Implementation Of various design patterns >> - Should have Worked On python specific packages >> - Should have Worked On python specific frameworks like >> django, zope, >> - Exposure to middlewares, decorators, etc. >> - He should have worked on web apps instead of desktop apps. >> >> For Python: Send updated CV to richa.sinha at globallogic.com > * >> * >> >> *JAVA Positions: * >> >> * >> * >> >> *Min Qualification*: BS/BE/B.Tech/MCA or equivalent. >> >> *Location* ? Client site- Gurgaon (India) Google office >> >> *Positions: 10* >> *Experience*: Total work exp should be 2 - 5 yrs >> >> - Work experience and relevant experience in internet architecture >> and software development. >> - Fluency in at least two of the following languages: Java, >> JavaScript. >> - Expertise in object-oriented programming and design patterns >> (primarily MVC). >> - Hands on experience in database design, SQL and any ORM tool like >> Hibernate/JDO etc. >> - Experience in TDD based developmen. >> >> >> *Desired Skills*: >> >> - Innovativeness; ability to think outside the box . >> - Creativity and problem-solving ability . >> - Very good communication skills; must be able to gather requirements >> from potential users and discuss the requirements effectively with a >> technical team of developers . >> - Previous experience in developing web applications will be an added >> advantage. >> Working knowledge of linux environment is a plus. >> - Previous experience of working with Google tools/gadgets is a big >> plus. >> >> For Java : Send updated CV to ankit.bansal1 at globallogic.com * Do visit* www.globallogic.com* **Share this mail with your friends, alumni groups,communities etc., spread the great opportunity :) * -- Regards, Richa Sinha | Talent Acquisition *Leaders in Software R&D Services* *www.globallogic.com* ARGENTINA |CHINA | INDIA | ISRAEL | UKRAINE | UK | USA Office: +91-120-406-2378 http://www.youtube.com/watch?v=V4pwY800TII -------------- next part -------------- An HTML attachment was scrubbed... URL: From mohandayalank at gmail.com Fri Dec 16 06:20:36 2011 From: mohandayalank at gmail.com (D. Mohan M. Dayalan) Date: Fri, 16 Dec 2011 03:20:36 -0800 (PST) Subject: ckjdlkfnl,ndf,nfd,fndfnkdnk mmdlssdlndnll; k; as; lkds sjdsljdlskjdsl; kdslksdl; ddlk Message-ID: <5663ea95-735e-4859-a04f-8fcf3a7deb4d@24g2000prd.googlegroups.com> http;//123maza.com/48/moon670/ From andrea.crotti.0 at gmail.com Fri Dec 16 06:37:54 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Fri, 16 Dec 2011 11:37:54 +0000 Subject: merging argparse parsers Message-ID: <4EEB2D92.10200@gmail.com> I would like to have something like merged_parser = LoggingParser() + OtherParser() Which should create an argument parser with all the options composed. Now for that I think I would need to subclass the argument, and something fancy with the overloading. The problem is that apparently there is no Argument class, but add_argument just change the object internal data structures. So the only alternative I see now is to do something like: def add_project_argument(parser): parser.add_argument('project_path', help='paths of the project to run') which is not very neat and modifies the object in place. Any other ideas? From waitmeforever at hotmail.com Fri Dec 16 07:54:54 2011 From: waitmeforever at hotmail.com (Yang Chun-Kai) Date: Fri, 16 Dec 2011 20:54:54 +0800 Subject: Localhost client-server simple ssl socket test program problems In-Reply-To: References: , Message-ID: > To: python-list at python.org > From: lists at cheimes.de > Subject: Re: Localhost client-server simple ssl socket test program problems > Date: Thu, 15 Dec 2011 20:45:43 +0100 > > Am 15.12.2011 20:09, schrieb Yang Chun-Kai: > > Server side error: > > > > File "views.py", line 17, in > > connstream = ssl.wrap_socket(newsocket, server_side=True, > > certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", > > keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", > > ssl_version=ssl.PROTOCOL_SSLv23) > > File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket > > ciphers=ciphers) > > File "/usr/lib/python2.7/ssl.py", line 119, in __init__ > > ciphers) > > ssl.SSLError: [Errno 336265218] _ssl..c:347: error:140B0002:SSL > > routines:SSL_CTX_use_PrivateKey_file:system lib > > This error is most likely caused by an encrypted private key. Python's > SSL lib doesn't support encrypted private keys for sockets. You can > encrypt the private key with >>>>>> >>>I generate the server private key with "openssl genrsa -out mypha.key 2048".>>>But this seems the standard command to do it.>>>How do I get the private key without encrypted ?>>>Or should I always do this and encrypt it again to get it decrypted ?>>>If I use the encrypted key and .csr to produce my certificate will that be different from decrypted key?>>>Thanks.>>>Kay>>> > openssl rsa -in /etc/home/ckyang/PHA/testsslsocket/mypha.key -out > /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key > > Christian > > > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From kj4eit at gmail.com Fri Dec 16 08:21:50 2011 From: kj4eit at gmail.com (Brad Tilley) Date: Fri, 16 Dec 2011 05:21:50 -0800 (PST) Subject: Make a small function thread safe Message-ID: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> Hey guys, I have a C++ function that I'd like to replicate (as closely as possible) in Python. Here's an example: 107 void increment_counter( unsigned int& counter ) 108 { 109 boost::mutex::scoped_lock lock( counter_lock ); 110 ++counter; 111 } A thread locks the function on entrance and then releases it on exit. What is the equivalent way to do this in Python? Many thanks! Brad From ulrich.eckhardt at dominolaser.com Fri Dec 16 08:32:03 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 16 Dec 2011 14:32:03 +0100 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: Am 16.12.2011 10:45, schrieb Ulrich Eckhardt: > I'm trying to create a struct_time that is e.g. one year ahead or a > month back in order to test some parsing/formatting code with different > dates. There is something I stumbled across that helps and that is the datetime module, which seems more reasonably pythonic, and which allows operations on dates like adding a Concerning the idea to use seconds, I'd rather not, because already the number of seconds per minute ranges from 60 to 62, and it doesn't get better with things like months (28...31 days), years (365...366 days) and all other types built upon them. Considering the question if the current state is buggy, I'm definitely +1 on it. I do understand that this API is not going to change, but explicitly documenting in "help(time)" that the order is fixed and possibly making the order programmatically available are not changes but useful additions, IMHO. Also, conversion from/to a dict and perhaps a link to the datetime module would have saved me some futile attempts. Thanks to all responders, I wish you a happy weekend! Uli From tim.wintle at teamrubber.com Fri Dec 16 08:33:14 2011 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Fri, 16 Dec 2011 13:33:14 +0000 Subject: Make a small function thread safe In-Reply-To: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> Message-ID: <1324042394.23801.6.camel@tim-laptop> On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote: > 107 void increment_counter( unsigned int& counter ) > 108 { > 109 boost::mutex::scoped_lock lock( counter_lock ); > 110 ++counter; > 111 } with counter_lock: counter += 1 ... where counter_lock is a threading.Lock instance. (see docs for the threading module) Tim From kj4eit at gmail.com Fri Dec 16 09:24:19 2011 From: kj4eit at gmail.com (Brad Tilley) Date: Fri, 16 Dec 2011 09:24:19 -0500 Subject: Make a small function thread safe In-Reply-To: <1324042394.23801.6.camel@tim-laptop> References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: On Fri, Dec 16, 2011 at 8:33 AM, Tim Wintle wrote: > On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote: > > 107 void increment_counter( unsigned int& counter ) > > 108 { > > 109 boost::mutex::scoped_lock lock( counter_lock ); > > 110 ++counter; > > 111 } > > > with counter_lock: > counter += 1 > > > ... where counter_lock is a threading.Lock instance. > > (see docs for the threading module) So something like this then: import threading shared_container = [] lock = threading.Lock() class thread_example( threading.Thread ): def __init__( self ): threading.Thread.__init__ (self) def run(t): lock shared_container.append(t.name) # main threads = [] for i in xrange(10): thread = thread_example() threads.append(thread) for thread in threads: thread.start() for item in shared_container: print item -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Fri Dec 16 09:24:53 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 17 Dec 2011 01:24:53 +1100 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: On Sat, Dec 17, 2011 at 12:32 AM, Ulrich Eckhardt wrote: > Concerning the idea to use seconds, I'd rather not, because already the > number of seconds per minute ranges from 60 to 62, and it doesn't get better > with things like months (28...31 days), years (365...366 days) and all other > types built upon them. Right, which is why I asked how important the difference between "365 days" and "1 year" is. Obviously if your goal is one entire calendar year, then you don't want to duplicate the work of figuring out how many seconds that is. ChrisA From kj4eit at gmail.com Fri Dec 16 09:30:26 2011 From: kj4eit at gmail.com (Brad Tilley) Date: Fri, 16 Dec 2011 09:30:26 -0500 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: On Fri, Dec 16, 2011 at 9:24 AM, Brad Tilley wrote: > > > On Fri, Dec 16, 2011 at 8:33 AM, Tim Wintle wrote: > >> On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote: >> > 107 void increment_counter( unsigned int& counter ) >> > 108 { >> > 109 boost::mutex::scoped_lock lock( counter_lock ); >> > 110 ++counter; >> > 111 } >> >> >> with counter_lock: >> counter += 1 >> >> >> ... where counter_lock is a threading.Lock instance. >> >> (see docs for the threading module) > > > > > So something like this then: > > import threading > > shared_container = [] > lock = threading.Lock() > > class thread_example( threading.Thread ): > > def __init__( self ): > threading.Thread.__init__ (self) > > def run(t): > lock > shared_container.append(t.name) > > # main > > threads = [] > for i in xrange(10): > thread = thread_example() > threads.append(thread) > > for thread in threads: > thread.start() > > for item in shared_container: > print item > > Or perhaps run should look like this instead: def run(t): lock.acquire() shared_container.append(t.name) lock.release() That seems a bit barbaric to me, not sure. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tim.wintle at teamrubber.com Fri Dec 16 09:36:20 2011 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Fri, 16 Dec 2011 14:36:20 +0000 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: <1324046180.26617.6.camel@tim-laptop> On Fri, 2011-12-16 at 09:24 -0500, Brad Tilley wrote: > So something like this then: > > import threading > > shared_container = [] > lock = threading.Lock() > > class thread_example( threading.Thread ): > > def __init__( self ): > threading.Thread.__init__ (self) > > def run(t): > lock > shared_container.append(t.name) should be: def run(t): with lock: shared_container.append(t.name) (or lock.acquire() and lock.release() as you mentioned) > # main > > threads = [] > for i in xrange(10): > thread = thread_example() > threads.append(thread) > > for thread in threads: > thread.start() you'll either need to lock again here, or join each thread: for thread in threads: thread.join() > for item in shared_container: > print item Tim From kj4eit at gmail.com Fri Dec 16 10:05:32 2011 From: kj4eit at gmail.com (Brad Tilley) Date: Fri, 16 Dec 2011 07:05:32 -0800 (PST) Subject: Make a small function thread safe References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: On Dec 16, 9:36?am, Tim Wintle wrote: > should be: > ? ? ? def run(t): > ? ? ? ? ? with lock: > ? ? ? ? ? ? ? shared_container.append(t.name) > > (or lock.acquire() and lock.release() as you mentioned) Thanks Tim. The with statement is closer to the C++ code (IMO) more so than the explicit acquire() and release() so I'll use that approach. I appreciate your advice. Brad From invalid at invalid.invalid Fri Dec 16 10:12:13 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 16 Dec 2011 15:12:13 +0000 (UTC) Subject: ERP with Supply chain management (incl. POS) and Customer relationship management ??? What's available? References: <9l08cjFlaoU1@mid.individual.net> Message-ID: On 2011-12-16, Gregory Ewing wrote: >> On Thu, Dec 15, 2011 at 5:54 AM, Anurag Chourasia >> wrote: >> >>>I am building a POS/CRM (Loyalty Management) system as well. > > Is it just me, or does the phrase "Loyalty Management" have > a faintly ominous ring to it? Yea, sounds a bit like Newspeak -- maybe it's what the secret police do under a dictatorship. -- Grant Edwards grant.b.edwards Yow! It's some people at inside the wall! This is gmail.com better than mopping! From emile at fenx.com Fri Dec 16 11:07:42 2011 From: emile at fenx.com (Emile van Sebille) Date: Fri, 16 Dec 2011 08:07:42 -0800 Subject: =?windows-1252?Q?Re=3A_ERP_with_Supply_chain_managemen?= =?windows-1252?Q?t_=28incl=2E_POS=29_and_Customer_relationship?= =?windows-1252?Q?_management_=97_What=27s_available=3F?= In-Reply-To: References: Message-ID: On 12/15/2011 6:43 AM Alec Taylor said... > On Thu, Dec 15, 2011 at 5:54 AM, Anurag Chourasia > wrote: >> Hi Alec, >> >> I am building a POS/CRM (Loyalty Management) system as well. >> >> So far, the best I could find to use as a base OpenERP? Emile From ian.g.kelly at gmail.com Fri Dec 16 11:23:47 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Dec 2011 09:23:47 -0700 Subject: Class: @property -> .__dict__ In-Reply-To: References: <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> <3fe1021b-75ae-4e3d-a5b0-175ad00aefa4@i6g2000vbh.googlegroups.com> <08b0a976-dd2a-4728-b865-4d04f4e5b7fb@n10g2000vbg.googlegroups.com> Message-ID: On Fri, Dec 16, 2011 at 2:32 AM, Peter Otten <__peter__ at web.de> wrote: > Ulrich wrote: > >> if I replace it to >> def attributelist(self): >> ? ? ? ? ?# find all attributes to the class that are of type numpy >> arrays: >> ? ? ? ? ?return [attr for attr in dir(self) if >> isinstance(getattr(self, attr), numpy.ndarray)] >> >> it crashes going into some kind of endless loop. >> >> Do you happen to have any idea? > > dir(self) finds an attribute named "attributelist", getattr(self, > "attributelist") then tries to calculate the value of that attribute, > invokes dir(self) which finds an attribute named "attributelist" and so on > ad infinitum or the stack overflows. Try (untested) > > @property > def attributelist(self): > ? ?return [attr for attr in dir(self) if attr != "attributelist" and > ? ? ? ? ? ?isinstance(getattr(self, attr), numpy.ndarray)] > > to avoid the infinite recursion. Or remove attributelist from the class (it feels more like a generic function than a class property to me) or make it a method instead of a property. From gordon at panix.com Fri Dec 16 11:49:56 2011 From: gordon at panix.com (John Gordon) Date: Fri, 16 Dec 2011 16:49:56 +0000 (UTC) Subject: re.sub(): replace longest match instead of leftmost match? Message-ID: According to the documentation on re.sub(), it replaces the leftmost matching pattern. However, I want to replace the *longest* matching pattern, which is not necessarily the leftmost match. Any suggestions? I'm working with IPv6 CIDR strings, and I want to replace the longest match of "(0000:|0000$)+" with ":". But when I use re.sub() it replaces the leftmost match, even if there is a longer match later in the string. I'm also looking for a regexp that will remove leading zeroes in each four-digit group, but will leave a single zero if the group was all zeroes. Thanks! -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From jeanpierreda at gmail.com Fri Dec 16 11:56:32 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 16 Dec 2011 11:56:32 -0500 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: Message-ID: You could use re.finditer to find the longest match, and then replace it manually by hand (via string slicing). (a match is the longest if (m.end() - m.start()) is the largest -- so, max(re.finditer(...), key=lambda m: (m.end() = m.start())) -- Devin P.S. does anyone else get bothered by how it's slice.start and slice.stop, but match.start() and match.end() ? On Fri, Dec 16, 2011 at 11:49 AM, John Gordon wrote: > According to the documentation on re.sub(), it replaces the leftmost > matching pattern. > > However, I want to replace the *longest* matching pattern, which is > not necessarily the leftmost match. ?Any suggestions? > > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(0000:|0000$)+" with ":". ?But when I use re.sub() it replaces > the leftmost match, even if there is a longer match later in the string. > > I'm also looking for a regexp that will remove leading zeroes in each > four-digit group, but will leave a single zero if the group was all > zeroes. > > Thanks! > > -- > John Gordon ? ? ? ? ? ? ? ? ? A is for Amy, who fell down the stairs > gordon at panix.com ? ? ? ? ? ? ?B is for Basil, assaulted by bears > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-- Edward Gorey, "The Gashlycrumb Tinies" > > -- > http://mail.python.org/mailman/listinfo/python-list From python at mrabarnett.plus.com Fri Dec 16 12:36:51 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 16 Dec 2011 17:36:51 +0000 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: Message-ID: <4EEB81B3.6020600@mrabarnett.plus.com> On 16/12/2011 16:49, John Gordon wrote: > According to the documentation on re.sub(), it replaces the leftmost > matching pattern. > > However, I want to replace the *longest* matching pattern, which is > not necessarily the leftmost match. Any suggestions? > > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(0000:|0000$)+" with ":". But when I use re.sub() it replaces > the leftmost match, even if there is a longer match later in the string. > > I'm also looking for a regexp that will remove leading zeroes in each > four-digit group, but will leave a single zero if the group was all > zeroes. > How about this: result = re.sub(r"\b0+(\d)\b", r"\1", string) From rustompmody at gmail.com Fri Dec 16 12:38:56 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 16 Dec 2011 09:38:56 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> Message-ID: <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> On Dec 16, 3:25?pm, Eelco wrote: > Pseudo-backwards compatibility with other > languages, I couldnt not care less for. Double negations n Goedelian situations have interesting implications (tho here its triple) From nathan.alexander.rice at gmail.com Fri Dec 16 12:48:41 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Fri, 16 Dec 2011 12:48:41 -0500 Subject: Making the case for "typed" lists/iterators in python Message-ID: I realize this has been discussed in the past, I hope that I am presenting a slightly different take on the subject that will prove interesting. This is primarily motivated by my annoyance with using comprehensions in certain circumstances. Currently, if you want to perform successive transformations on the elements of a list, a couple of options: 1. Successive comprehensions: L2 = [X(e) for e in L1] L3 = [Y(e) for e in L2] L4 = [Z(e) for e in L3] or L2 = [e.X() for e in L1] This gets the job done and gives you access to all the intermediate values, but isn't very succinct, particularly if you are in the habit of using informative identifiers. 2. One comprehension: L2 = [Z(X(Y(e))) for e in L1] or L2 = [e.X().Y().Z() for e in L1] This gets the job done, but doesn't give you access to all the intermediate values, and tends to be pretty awful to read. Having "typed" lists let you take preexisting string/int/etc methods and expose them in a vectorized context and provides an easy way for developers to support both vectors and scalars in a single function (you could easily "fix" other people's functions dynamically to support both). Additionally, "typed" lists/iterators will allow improved code analysis and optimization. The PyPy people have already stated that they are working on implementing different strategies for lists composed of a single type, so clearly there is already community movement in this direction. Just compare the above examples to their type-aware counterparts: L2 = X(L1) L2 = L1.X() L2 = Z(Y(X(L1))) L2 = L1.X().Y().Z() Also, this would provide a way to clean up stuff like: "\n".join(l.capitalize() for l in my_string.split("\n")) into: my_string.split("\n").capitalize().join_this("\n") Before anyone gets up in arms at the idea of statically typed python, what I am suggesting here would be looser than that. Basically, I believe it would be a good idea in instances where it is known that a list of single type is going to be returned, to return a list subclass (for example, StringList, IntegerList, etc). To avoid handcuffing people with types, the standard list modification methods could be hooked so that if an object of an incorrect type is placed in the list, a warning is raised and the list converts to a generic object list. The only stumbling block is that you can't use __class__ to convert from stack types to heap types in CPython. My workaround for this would be to have a factory that creates generic "List" classes, modifying the bases to produce the correct behavior. Then, converting from a typed list to a generic object list would just be a matter of removing a member from the bases for a class. This of course basically kills the ability to perform type specific list optimization in CPython, but that isn't necessarily true for other implementations. The additional type information would be preserved for code analysis in any case. The case would be even simpler for generators and other iterators, as you don't have to worry about mutation. I'd like to hear people's thoughts on the subject. Currently we are throwing away useful information in many cases that could be used for code analysis, optimization and simpler interfaces. I believe that "typed" lists that get "demoted" to normal lists with a warning on out of type operations preserve this information while providing complete backwards compatibility and freedom. Nathan From ian.g.kelly at gmail.com Fri Dec 16 12:57:06 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Dec 2011 10:57:06 -0700 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: <4EEB81B3.6020600@mrabarnett.plus.com> References: <4EEB81B3.6020600@mrabarnett.plus.com> Message-ID: On Fri, Dec 16, 2011 at 10:36 AM, MRAB wrote: > On 16/12/2011 16:49, John Gordon wrote: >> >> According to the documentation on re.sub(), it replaces the leftmost >> matching pattern. >> >> However, I want to replace the *longest* matching pattern, which is >> not necessarily the leftmost match. ?Any suggestions? >> >> I'm working with IPv6 CIDR strings, and I want to replace the longest >> match of "(0000:|0000$)+" with ":". ?But when I use re.sub() it replaces >> the leftmost match, even if there is a longer match later in the string. >> >> I'm also looking for a regexp that will remove leading zeroes in each >> four-digit group, but will leave a single zero if the group was all >> zeroes. >> > How about this: > > result = re.sub(r"\b0+(\d)\b", r"\1", string) Close. pattern = r'\b0+([1-9a-f]+|0)\b' re.sub(pattern, r'\1', string, flags=re.IGNORECASE) Cheers, Ian From fabiofz at gmail.com Fri Dec 16 12:57:33 2011 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Fri, 16 Dec 2011 15:57:33 -0200 Subject: Python Interpreter Error with PyDev (Eclipse IDE) In-Reply-To: <1323994849.34934.YahooMailNeo@web120504.mail.ne1.yahoo.com> References: <1323994849.34934.YahooMailNeo@web120504.mail.ne1.yahoo.com> Message-ID: It seems you have a version of Python 2.5 in your system and when starting up Python 3.2.2 it's getting things from 2.5 (this would probably also happen on the command line). The usual suspect is that you defined a PYTHONROOT variable which is pointing to Python 2.5 and not to the python you're using... Cheers, Fabio On Thu, Dec 15, 2011 at 10:20 PM, Joshua Jodrey wrote: > Hi, > This is my first email to this list, so I apologize if there's a better > mailing-list for this type of question. > > I'm looking to get started with python development, so I downloaded Python > 3.2.2 and PyDev IDE for Eclipse. ?When I go to configure my interpreter for > a new python project, Windows gives me the following error: > > "python.exe has stopped working" > > and then Eclipse shows > > "Error getting info on interpreter > "See error log for details. > No output was in the standard output when trying to create the interpreter > info. > The error output contains:>>Fatal Python error: Py_Initialize: unable to > load the file system codec > ? File "C:\csvn\Python25\\lib\encodings\__init__.py", line 120 > ? ? raise CodecRegistryError,\ > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^ > SyntaxError: invalid syntax > > This application has requested the Runtime to terminate it in an unusual > way. > Please contact the application's support team for more information. > <<" > > Does anybody have any idea what this means or what I'm doing wrong? ?I've > tried?re-installing?Python, but still can't get it to work. > And again, if I'm in the wrong place, please direct me to the proper > mailing-list or forum. > > Thanks! > Josh > > P.S. I'm not?subscribed?to this mailer, so please include me in your > replies! > > -- > http://mail.python.org/mailman/listinfo/python-list > From ian.g.kelly at gmail.com Fri Dec 16 12:59:57 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Dec 2011 10:59:57 -0700 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: <4EEB81B3.6020600@mrabarnett.plus.com> Message-ID: On Fri, Dec 16, 2011 at 10:57 AM, Ian Kelly wrote: > On Fri, Dec 16, 2011 at 10:36 AM, MRAB wrote: >> On 16/12/2011 16:49, John Gordon wrote: >>> >>> According to the documentation on re.sub(), it replaces the leftmost >>> matching pattern. >>> >>> However, I want to replace the *longest* matching pattern, which is >>> not necessarily the leftmost match. ?Any suggestions? >>> >>> I'm working with IPv6 CIDR strings, and I want to replace the longest >>> match of "(0000:|0000$)+" with ":". ?But when I use re.sub() it replaces >>> the leftmost match, even if there is a longer match later in the string. >>> >>> I'm also looking for a regexp that will remove leading zeroes in each >>> four-digit group, but will leave a single zero if the group was all >>> zeroes. >>> >> How about this: >> >> result = re.sub(r"\b0+(\d)\b", r"\1", string) > > Close. > > pattern = r'\b0+([1-9a-f]+|0)\b' > re.sub(pattern, r'\1', string, flags=re.IGNORECASE) Doh, that's still not quite right. pattern = r'\b0{1,3}([1-9a-f][0-9a-f]*|0)\b' re.sub(pattern, r'\1', string, flags=re.IGNORECASE) From roy at panix.com Fri Dec 16 13:05:37 2011 From: roy at panix.com (Roy Smith) Date: Fri, 16 Dec 2011 13:05:37 -0500 Subject: Making the case for "typed" lists/iterators in python References: Message-ID: In article , Nathan Rice wrote: > I'd like to hear people's thoughts on the subject. Currently we are > throwing away useful information in many cases that could be used for > code analysis, optimization and simpler interfaces. Most of this was TL:DNR, but I will admit I often wish for a better way to log intermediate values. For example, a common pattern in the code I'm working with now is functions that end in: return [Foo(x) for x in bunch_of_x_thingies] When something goes amiss and I want to debug the problem, I often transform that into: temp = [Foo(x) for x in bunch_of_x_thingies] logger.debug(temp) return temp It would be convenient to be able to get at and log the intermediate value without having to pull it out to an explicit temporary. From python at mrabarnett.plus.com Fri Dec 16 13:19:52 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 16 Dec 2011 18:19:52 +0000 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: <4EEB81B3.6020600@mrabarnett.plus.com> Message-ID: <4EEB8BC8.6040000@mrabarnett.plus.com> On 16/12/2011 17:57, Ian Kelly wrote: > On Fri, Dec 16, 2011 at 10:36 AM, MRAB wrote: >> On 16/12/2011 16:49, John Gordon wrote: >>> >>> According to the documentation on re.sub(), it replaces the leftmost >>> matching pattern. >>> >>> However, I want to replace the *longest* matching pattern, which is >>> not necessarily the leftmost match. Any suggestions? >>> >>> I'm working with IPv6 CIDR strings, and I want to replace the longest >>> match of "(0000:|0000$)+" with ":". But when I use re.sub() it replaces >>> the leftmost match, even if there is a longer match later in the string. >>> >>> I'm also looking for a regexp that will remove leading zeroes in each >>> four-digit group, but will leave a single zero if the group was all >>> zeroes. >>> >> How about this: >> >> result = re.sub(r"\b0+(\d)\b", r"\1", string) > > Close. > > pattern = r'\b0+([1-9a-f]+|0)\b' > re.sub(pattern, r'\1', string, flags=re.IGNORECASE) > Ah, OK. The OP said "digit" instead of "hex digit". That's my excuse. :-) From stefan_ml at behnel.de Fri Dec 16 13:23:27 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 16 Dec 2011 19:23:27 +0100 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: Nathan Rice, 16.12.2011 18:48: > I realize this has been discussed in the past, I hope that I am > presenting a slightly different take on the subject that will prove > interesting. This is primarily motivated by my annoyance with using > comprehensions in certain circumstances. > > Currently, if you want to perform successive transformations on the > elements of a list, a couple of options: > > 1. Successive comprehensions: > > L2 = [X(e) for e in L1] > L3 = [Y(e) for e in L2] > L4 = [Z(e) for e in L3] > or > L2 = [e.X() for e in L1] > > This gets the job done and gives you access to all the intermediate > values, but isn't very succinct, particularly if you are in the habit > of using informative identifiers. > > 2. One comprehension: > > L2 = [Z(X(Y(e))) for e in L1] > or > L2 = [e.X().Y().Z() for e in L1] > > This gets the job done, but doesn't give you access to all the > intermediate values, and tends to be pretty awful to read. > > Having "typed" lists let you take preexisting string/int/etc methods > and expose them in a vectorized context and provides an easy way for > developers to support both vectors and scalars in a single function > (you could easily "fix" other people's functions dynamically to > support both). Additionally, "typed" lists/iterators will allow > improved code analysis and optimization. The PyPy people have already > stated that they are working on implementing different strategies for > lists composed of a single type, so clearly there is already community > movement in this direction. > > Just compare the above examples to their type-aware counterparts: > > L2 = X(L1) > L2 = L1.X() > > L2 = Z(Y(X(L1))) > L2 = L1.X().Y().Z() What keeps you from implementing this? You don't need to change the language for it, just wrap the list in a class that overrides __getattr__() to return something that does the appropriate transformation for each element. I would be surprised if you needed more than a couple of lines of Python code for that. Stefan From rosuav at gmail.com Fri Dec 16 13:25:12 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 17 Dec 2011 05:25:12 +1100 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: On Sat, Dec 17, 2011 at 5:05 AM, Roy Smith wrote: > Most of this was TL:DNR, but I will admit I often wish for a better way > to log intermediate values. ?For example, a common pattern in the code > I'm working with now is functions that end in: > > ? return [Foo(x) for x in bunch_of_x_thingies] > > When something goes amiss and I want to debug the problem, I often > transform that into: > > ? ?temp = [Foo(x) for x in bunch_of_x_thingies] > ? ?logger.debug(temp) > ? ?return temp > > It would be convenient to be able to get at and log the intermediate > value without having to pull it out to an explicit temporary. tee = lambda func,arg: (func(arg),arg)[1] return tee(logger.debug,[Foo(x) for x in bunch_of_x_thingies]) ChrisA From roy at panix.com Fri Dec 16 13:36:17 2011 From: roy at panix.com (Roy Smith) Date: Fri, 16 Dec 2011 13:36:17 -0500 Subject: re.sub(): replace longest match instead of leftmost match? References: Message-ID: In article , John Gordon wrote: > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(0000:|0000$)+" with ":". But when I use re.sub() it replaces > the leftmost match, even if there is a longer match later in the string. > > I'm also looking for a regexp that will remove leading zeroes in each > four-digit group, but will leave a single zero if the group was all > zeroes. Having done quite a bit of IPv6 work, my opinion here is that you're trying to do The Wrong Thing. What you want is an IPv6 class which represents an address in some canonical form. It would have constructors which accept any of the RFC-2373 defined formats. It would also have string formatting methods to convert the internal form into any of these formats. Then, instead of attempting to regex your way directly from one string representation to another, you would do something like: addr_string = "FEDC:BA98:7654:3210:FEDC:BA98:7654:321" print IPv6(addr_string).to_short_form() From arnodel at gmail.com Fri Dec 16 13:38:05 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Fri, 16 Dec 2011 18:38:05 +0000 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: On 16 December 2011 18:25, Chris Angelico wrote: > tee = lambda func,arg: (func(arg),arg)[1] What a strange way to spell it! def tee(func, arg): func(arg) return arg -- Arnaud From rosuav at gmail.com Fri Dec 16 13:43:17 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 17 Dec 2011 05:43:17 +1100 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: On Sat, Dec 17, 2011 at 5:38 AM, Arnaud Delobelle wrote: > On 16 December 2011 18:25, Chris Angelico wrote: > >> tee = lambda func,arg: (func(arg),arg)[1] > > What a strange way to spell it! > > def tee(func, arg): > ? ?func(arg) > ? ?return arg I started with that version and moved to the lambda for compactness. But either way works. It's no more strange than the way some people omit the u from colour. :) ChrisA From nathan.alexander.rice at gmail.com Fri Dec 16 13:51:08 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Fri, 16 Dec 2011 13:51:08 -0500 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: Nothing stops me from implementing it, in fact it is VERY trivial to wrap member class methods onto a list subclass, and wrap functions to support vectorized behavior. The problem is that as soon as you hit anything outside your code that returns a list or iterator, everything gets blown away unless you explicitly wrap the return value, which entirely defeats the point. On Fri, Dec 16, 2011 at 1:23 PM, Stefan Behnel wrote: > Nathan Rice, 16.12.2011 18:48: > >> I realize this has been discussed in the past, I hope that I am >> presenting a slightly different take on the subject that will prove >> interesting. ?This is primarily motivated by my annoyance with using >> comprehensions in certain circumstances. >> >> Currently, if you want to perform successive transformations on the >> elements of a list, a couple of options: >> >> 1. Successive comprehensions: >> >> L2 = [X(e) for e in L1] >> L3 = [Y(e) for e in L2] >> L4 = [Z(e) for e in L3] >> or >> L2 = [e.X() for e in L1] >> >> This gets the job done and gives you access to all the intermediate >> values, but isn't very succinct, particularly if you are in the habit >> of using informative identifiers. >> >> 2. One comprehension: >> >> L2 = [Z(X(Y(e))) for e in L1] >> or >> L2 = [e.X().Y().Z() for e in L1] >> >> This gets the job done, but doesn't give you access to all the >> intermediate values, and tends to be pretty awful to read. >> >> Having "typed" lists let you take preexisting string/int/etc methods >> and expose them in a vectorized context and provides an easy way for >> developers to support both vectors and scalars in a single function >> (you could easily "fix" other people's functions dynamically to >> support both). ?Additionally, "typed" lists/iterators will allow >> improved code analysis and optimization. ?The PyPy people have already >> stated that they are working on implementing different strategies for >> lists composed of a single type, so clearly there is already community >> movement in this direction. >> >> Just compare the above examples to their type-aware counterparts: >> >> L2 = X(L1) >> L2 = L1.X() >> >> L2 = Z(Y(X(L1))) >> L2 = L1.X().Y().Z() > > > What keeps you from implementing this? You don't need to change the language > for it, just wrap the list in a class that overrides __getattr__() to return > something that does the appropriate transformation for each element. I would > be surprised if you needed more than a couple of lines of Python code for > that. > > Stefan > > -- > http://mail.python.org/mailman/listinfo/python-list From jeanpierreda at gmail.com Fri Dec 16 14:03:44 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 16 Dec 2011 14:03:44 -0500 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: > 2. One comprehension: > > L2 = [Z(X(Y(e))) for e in L1] > or > L2 = [e.X().Y().Z() for e in L1] I want to say that maybe this is better done by functions like map() (although, Python is missing a composition operator which might be handy, and mapping method calls isn't pretty). I don't know that it belongs as a method on lists, especially since that means you can't do the same thing on, for example, generators. Or I guess, the easy answer to that lattermost objection is to make it typed iterators rather than typed lists. -- Devin On Fri, Dec 16, 2011 at 12:48 PM, Nathan Rice wrote: > I realize this has been discussed in the past, I hope that I am > presenting a slightly different take on the subject that will prove > interesting. ?This is primarily motivated by my annoyance with using > comprehensions in certain circumstances. > > Currently, if you want to perform successive transformations on the > elements of a list, a couple of options: > > 1. Successive comprehensions: > > L2 = [X(e) for e in L1] > L3 = [Y(e) for e in L2] > L4 = [Z(e) for e in L3] > or > L2 = [e.X() for e in L1] > > This gets the job done and gives you access to all the intermediate > values, but isn't very succinct, particularly if you are in the habit > of using informative identifiers. > > 2. One comprehension: > > L2 = [Z(X(Y(e))) for e in L1] > or > L2 = [e.X().Y().Z() for e in L1] > > This gets the job done, but doesn't give you access to all the > intermediate values, and tends to be pretty awful to read. > > Having "typed" lists let you take preexisting string/int/etc methods > and expose them in a vectorized context and provides an easy way for > developers to support both vectors and scalars in a single function > (you could easily "fix" other people's functions dynamically to > support both). ?Additionally, "typed" lists/iterators will allow > improved code analysis and optimization. ?The PyPy people have already > stated that they are working on implementing different strategies for > lists composed of a single type, so clearly there is already community > movement in this direction. > > Just compare the above examples to their type-aware counterparts: > > L2 = X(L1) > L2 = L1.X() > > L2 = Z(Y(X(L1))) > L2 = L1.X().Y().Z() > > Also, this would provide a way to clean up stuff like: > > "\n".join(l.capitalize() for l in my_string.split("\n")) > > into: > > my_string.split("\n").capitalize().join_this("\n") > > Before anyone gets up in arms at the idea of statically typed python, > what I am suggesting here would be looser than that. ?Basically, I > believe it would be a good idea in instances where it is known that a > list of single type is going to be returned, to return a list subclass > (for example, StringList, IntegerList, etc). ?To avoid handcuffing > people with types, the standard list modification methods could be > hooked so that if an object of an incorrect type is placed in the > list, a warning is raised and the list converts to a generic object > list. ?The only stumbling block is that you can't use __class__ to > convert from stack types to heap types in CPython. ?My workaround for > this would be to have a factory that creates generic "List" classes, > modifying the bases to produce the correct behavior. ?Then, converting > from a typed list to a generic object list would just be a matter of > removing a member from the bases for a class. ?This of course > basically kills the ability to perform type specific list optimization > in CPython, but that isn't necessarily true for other implementations. > ?The additional type information would be preserved for code analysis > in any case. ?The case would be even simpler for generators and other > iterators, as you don't have to worry about mutation. > > I'd like to hear people's thoughts on the subject. ?Currently we are > throwing away useful information in many cases that could be used for > code analysis, optimization and simpler interfaces. ?I believe that > "typed" lists that get "demoted" to normal lists with a warning on out > of type operations preserve this information while providing complete > backwards compatibility and freedom. > > Nathan > -- > http://mail.python.org/mailman/listinfo/python-list From using4business at gmail.com Fri Dec 16 14:15:59 2011 From: using4business at gmail.com (using business) Date: Fri, 16 Dec 2011 11:15:59 -0800 (PST) Subject: ANGELINA JOLIE New SEX Tape RELEASED ( Must See Very EROTIC ) Message-ID: Angelina Jolie New SEX Tape. Just Download it and open it to see her new video http://www.fileserve.com/file/H6BqSbr/Angelina Jolie.rar From hoogendoorn.eelco at gmail.com Fri Dec 16 14:40:11 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Fri, 16 Dec 2011 11:40:11 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> Message-ID: <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> On 16 dec, 18:38, rusi wrote: > On Dec 16, 3:25?pm, Eelco wrote: > > > Pseudo-backwards compatibility with other > > languages, I couldnt not care less for. > > Double negations n Goedelian situations have interesting implications > (tho here its triple) Heh. Well at least my extra (unintended) negation is semantically consistent with the actual english usage of the phrase, which omits the negation completely :). (I could care less) But ill stick with trying to change one language at a time :). From mwilson at the-wire.com Fri Dec 16 15:14:39 2011 From: mwilson at the-wire.com (Mel Wilson) Date: Fri, 16 Dec 2011 15:14:39 -0500 Subject: Making the case for "typed" lists/iterators in python References: Message-ID: Chris Angelico wrote: > It's no more strange than the way some people omit the u from colour. :) Bonum Petronio Arbiteri, bonum mihi. Mel. From stefan_ml at behnel.de Fri Dec 16 15:16:22 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 16 Dec 2011 21:16:22 +0100 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: Nathan Rice, 16.12.2011 19:51: > Nothing stops me from implementing it, in fact it is VERY trivial to > wrap member class methods onto a list subclass, and wrap functions to > support vectorized behavior. The problem is that as soon as you hit > anything outside your code that returns a list or iterator, everything > gets blown away unless you explicitly wrap the return value, which > entirely defeats the point. The point you are trying to make, maybe. Stefan PS: note that you top-posted in your reply, you should try to avoid that. From ben+python at benfinney.id.au Fri Dec 16 15:17:15 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 17 Dec 2011 07:17:15 +1100 Subject: Making the case for "typed" lists/iterators in python References: Message-ID: <87hb10z2yc.fsf@benfinney.id.au> Roy Smith writes: > When something goes amiss and I want to debug the problem, I often > transform that into: > > temp = [Foo(x) for x in bunch_of_x_thingies] > logger.debug(temp) > return temp > > It would be convenient to be able to get at and log the intermediate > value without having to pull it out to an explicit temporary. What's wrong with that though? You're not ?pulling it out to? anything; you're binding a name to the value in order to do several things with it. Exactly what you say you want to do. It's explicit and clear. -- \ ?What we usually pray to God is not that His will be done, but | `\ that He approve ours.? ?Helga Bergold Gross | _o__) | Ben Finney From timothy.c.delaney at gmail.com Fri Dec 16 15:47:09 2011 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Sat, 17 Dec 2011 07:47:09 +1100 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: On 17 December 2011 02:05, Brad Tilley wrote: > On Dec 16, 9:36 am, Tim Wintle wrote: > > > should be: > > def run(t): > > with lock: > > shared_container.append(t.name) > > > > (or lock.acquire() and lock.release() as you mentioned) > > > Thanks Tim. The with statement is closer to the C++ code (IMO) more so > than the explicit acquire() and release() so I'll use that approach. I > appreciate your advice. Most definitely. The acquire/release code shown was erroneous, since it would not release if an exception was thrown. The with code is effectively equivalent to: lock.acquire() try: ... finally: lock.release() Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From gordon at panix.com Fri Dec 16 16:04:49 2011 From: gordon at panix.com (John Gordon) Date: Fri, 16 Dec 2011 21:04:49 +0000 (UTC) Subject: re.sub(): replace longest match instead of leftmost match? References: Message-ID: In Devin Jeanpierre writes: > You could use re.finditer to find the longest match, and then replace > it manually by hand (via string slicing). > (a match is the longest if (m.end() - m.start()) is the largest -- > so, max(re.finditer(...), key=3Dlambda m: (m.end() =3D m.start())) I ended up doing something similar: # find the longest match longest_match = '' for word in re.findall('((0000:?)+)', ip6): if len(word[0]) > len(longest_match): longest_match = word[0] # if we found a match, replace it with a colon if longest_match: ip6 = re.sub(longest_match, ':', ip6, 1) Thanks! -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From gordon at panix.com Fri Dec 16 16:06:06 2011 From: gordon at panix.com (John Gordon) Date: Fri, 16 Dec 2011 21:06:06 +0000 (UTC) Subject: re.sub(): replace longest match instead of leftmost match? References: <4EEB81B3.6020600@mrabarnett.plus.com> Message-ID: In Ian Kelly writes: > >>> I'm also looking for a regexp that will remove leading zeroes in each > >>> four-digit group, but will leave a single zero if the group was all > >>> zeroes. > pattern = r'\b0{1,3}([1-9a-f][0-9a-f]*|0)\b' > re.sub(pattern, r'\1', string, flags=re.IGNORECASE) Perfect. Thanks Ian! -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From gordon at panix.com Fri Dec 16 16:07:30 2011 From: gordon at panix.com (John Gordon) Date: Fri, 16 Dec 2011 21:07:30 +0000 (UTC) Subject: re.sub(): replace longest match instead of leftmost match? References: Message-ID: In Roy Smith writes: > Having done quite a bit of IPv6 work, my opinion here is that you're > trying to do The Wrong Thing. > What you want is an IPv6 class which represents an address in some > canonical form. It would have constructors which accept any of the > RFC-2373 defined formats. It would also have string formatting methods > to convert the internal form into any of these formats. > Then, instead of attempting to regex your way directly from one string > representation to another, you would do something like: > addr_string = "FEDC:BA98:7654:3210:FEDC:BA98:7654:321" > print IPv6(addr_string).to_short_form() This does sound like a more robust solution. I'll give it some thought. Thanks Roy! -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From python at mrabarnett.plus.com Fri Dec 16 16:36:34 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 16 Dec 2011 21:36:34 +0000 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: Message-ID: <4EEBB9E2.1070301@mrabarnett.plus.com> On 16/12/2011 21:04, John Gordon wrote: > In Devin Jeanpierre writes: > >> You could use re.finditer to find the longest match, and then replace >> it manually by hand (via string slicing). > >> (a match is the longest if (m.end() - m.start()) is the largest -- >> so, max(re.finditer(...), key=3Dlambda m: (m.end() =3D m.start())) > > I ended up doing something similar: > > # find the longest match > longest_match = '' > for word in re.findall('((0000:?)+)', ip6): > if len(word[0])> len(longest_match): > longest_match = word[0] > > # if we found a match, replace it with a colon > if longest_match: > ip6 = re.sub(longest_match, ':', ip6, 1) > For a simple replace, using re is probably overkill. The .replace method is a better solution: ip6 = longest_match.replace(ip6, ':', 1) From tjreedy at udel.edu Fri Dec 16 17:05:57 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 16 Dec 2011 17:05:57 -0500 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/16/2011 4:22 AM, Steven D'Aprano wrote: > On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: > [...] > > After reading your post, I think I have worked out where our disagreement > lines: you think that bound methods and instance methods are not the same > thing, Do you agree that an unbound method and a bound method are different? In Python, as indicated by the glossary entry, an unspecified 'method' is usually meant to be an unbound method. It is am important distinction and I do not see why you insist on confusing the two. > and that a function defined inside a class is different from a > function outside of a class. That, and your repetition of the same claim further on, is a insulting lie. Def statements always create functions. I have known that for 14 years since the first day I started with Python. I have never thought differently. If you actually think that I have, you are wrong. What I have said from my first response is that a function that is an attribute of a class, *whether defined in or outside the class*, gets special treatment when accessed via an instance of the class. And that is the truth. If you are ever interested in learning anything from me on this subject, re=read what I already wrote with a more open mind than you have so far. Otherwise, I am done. -- Terry Jan Reedy From lie.1296 at gmail.com Fri Dec 16 17:08:04 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 17 Dec 2011 09:08:04 +1100 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: On 12/17/2011 01:30 AM, Brad Tilley wrote: > Or perhaps run should look like this instead: > > def run(t): > lock.acquire() > shared_container.append(t.name ) > lock.release() > > That seems a bit barbaric to me, not sure. change that to: def run(t): with lock: shared_container.append(t.name ) the `with-statement` will call lock.acquire() and lock.release(). From tjreedy at udel.edu Fri Dec 16 17:21:14 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 16 Dec 2011 17:21:14 -0500 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: On 12/16/2011 1:05 PM, Roy Smith wrote: > I'm working with now is functions that end in: > > return [Foo(x) for x in bunch_of_x_thingies] > > When something goes amiss and I want to debug the problem, I often > transform that into: > > temp = [Foo(x) for x in bunch_of_x_thingies] > logger.debug(temp) > return temp > > It would be convenient to be able to get at and log the intermediate > value without having to pull it out to an explicit temporary. Decorate the function with @logreturn and you do not have to touch the function code. If the logging module does not now have such a decorator predefined (I simply do not know), perhaps it should. -- Terry Jan Reedy From tjreedy at udel.edu Fri Dec 16 17:26:03 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 16 Dec 2011 17:26:03 -0500 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: Message-ID: On 12/16/2011 1:36 PM, Roy Smith wrote: > What you want is an IPv6 class which represents an address in some > canonical form. It would have constructors which accept any of the > RFC-2373 defined formats. It would also have string formatting methods > to convert the internal form into any of these formats. > > Then, instead of attempting to regex your way directly from one string > representation to another, you would do something like: > > addr_string = "FEDC:BA98:7654:3210:FEDC:BA98:7654:321" > print IPv6(addr_string).to_short_form() There are at least 2 third-party IP classes in use. I would not be surprised if at least one of them does this. -- Terry Jan Reedy From jdjodrey at yahoo.com Fri Dec 16 17:40:04 2011 From: jdjodrey at yahoo.com (Joshua Jodrey) Date: Fri, 16 Dec 2011 14:40:04 -0800 (PST) Subject: Python Interpreter Error with PyDev (Eclipse IDE) In-Reply-To: References: <1323994849.34934.YahooMailNeo@web120504.mail.ne1.yahoo.com> Message-ID: <1324075204.65234.YahooMailNeo@web120506.mail.ne1.yahoo.com> Thanks Fabio! I looked at my environment variables, and the only one python related was PYTHONHOME, and that was set to C:\csvn\Python25. ?So I changed that to C:\Python32\ and I added an environment variable for PYTHONROOT and set that to the same value. ?It now seems to work! I must have had a some python directories laying around from an odd svn installation or something... Thanks again! ________________________________ From: Fabio Zadrozny To: Joshua Jodrey Cc: "python-list at python.org" Sent: Friday, December 16, 2011 12:57 PM Subject: Re: Python Interpreter Error with PyDev (Eclipse IDE) It seems you have a version of Python 2.5 in your system and when starting up Python 3.2.2 it's getting things from 2.5 (this would probably also happen on the command line). The usual suspect is that you defined a PYTHONROOT variable which is pointing to Python 2.5 and not to the python you're using... Cheers, Fabio On Thu, Dec 15, 2011 at 10:20 PM, Joshua Jodrey wrote: > Hi, > This is my first email to this list, so I apologize if there's a better > mailing-list for this type of question. > > I'm looking to get started with python development, so I downloaded Python > 3.2.2 and PyDev IDE for Eclipse. ?When I go to configure my interpreter for > a new python project, Windows gives me the following error: > > "python.exe has stopped working" > > and then Eclipse shows > > "Error getting info on interpreter > "See error log for details. > No output was in the standard output when trying to create the interpreter > info. > The error output contains:>>Fatal Python error: Py_Initialize: unable to > load the file system codec > ? File "C:\csvn\Python25\\lib\encodings\__init__.py", line 120 > ? ? raise CodecRegistryError,\ > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^ > SyntaxError: invalid syntax > > This application has requested the Runtime to terminate it in an unusual > way. > Please contact the application's support team for more information. > <<" > > Does anybody have any idea what this means or what I'm doing wrong? ?I've > tried?re-installing?Python, but still can't get it to work. > And again, if I'm in the wrong place, please direct me to the proper > mailing-list or forum. > > Thanks! > Josh > > P.S. I'm not?subscribed?to this mailer, so please include me in your > replies! > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Fri Dec 16 18:26:30 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 16 Dec 2011 15:26:30 -0800 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EEBD3A6.4030700@stoneleaf.us> Terry Reedy wrote: > On 12/16/2011 4:22 AM, Steven D'Aprano wrote: >> On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: >> [...] >> >> After reading your post, I think I have worked out where our disagreement >> lies: you think that bound methods and instance methods are not the same >> thing, > > Do you agree that an unbound method and a bound method are different? In > Python, as indicated by the glossary entry, an unspecified 'method' is > usually meant to be an unbound method. I think you two are in violent agreement as far as how Python is functioning, and the conflict is in the names given to the various pieces... I think a glossary would help (please correct me): function: callable code suite method: function that lives in a class unbound method: function that lives in a class bound method: callable wrapper around function that has been extracted from class that will supply the instance object to the function (note: Python does not save these, they are recreated at each lookup) and here is where I think you two diverge: instance method (Steven): a bound method that has been saved into the instance __dict__ (no matter how created) instance method (Terry): a function that must be looked up in the class Have I missed anything? Honestly-trying-learn-the-distinctions-ly yours, ~Ethan~ From greg.ewing at canterbury.ac.nz Fri Dec 16 18:49:17 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sat, 17 Dec 2011 12:49:17 +1300 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> Message-ID: <9l23ntFsibU1@mid.individual.net> Eelco wrote: > the actual english usage of the phrase, which omits > the negation completely :). (I could care less) No, that's the American usage. The English usage is "I couldn't care less", which has the advantage of actually making sense. -- Greg From hoogendoorn.eelco at gmail.com Fri Dec 16 19:00:23 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Fri, 16 Dec 2011 16:00:23 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> <9l23ntFsibU1@mid.individual.net> Message-ID: <2420abd7-7d91-4bc9-bb3b-d8ec1680ed0c@u32g2000yqe.googlegroups.com> On Dec 17, 12:49?am, Gregory Ewing wrote: > Eelco wrote: > > the actual english usage of the phrase, which omits > > the negation completely :). (I could care less) > > No, that's the American usage. The English usage is > "I couldn't care less", which has the advantage of > actually making sense. > > -- > Greg Oh thanks for clearing that up, never noticed a division along these lines. And yes, I agree; 'I couldnt care less' makes much more sense. 'I could care less' can only make sense if you interpret it sarcastically, as if omitting an 'oh wait, I cant', but that does not seem congruent with how its typically pronounced. Just another case of suboptimal language design; but where can you submit EEP's? From roy at panix.com Fri Dec 16 19:03:23 2011 From: roy at panix.com (Roy Smith) Date: Fri, 16 Dec 2011 19:03:23 -0500 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> <9l23ntFsibU1@mid.individual.net> <2420abd7-7d91-4bc9-bb3b-d8ec1680ed0c@u32g2000yqe.googlegroups.com> Message-ID: In article <2420abd7-7d91-4bc9-bb3b-d8ec1680ed0c at u32g2000yqe.googlegroups.com>, Eelco wrote: > And yes, I agree; 'I couldnt care less' makes much more sense. 'I > could care less' can only make sense if you interpret it > sarcastically, as if omitting an 'oh wait, I cant', but that does not > seem congruent with how its typically pronounced. "I care so little about the subject that I am unwilling to spend one of my precious apostrophes to properly express the sentiment" From ethan at stoneleaf.us Fri Dec 16 19:34:49 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 16 Dec 2011 16:34:49 -0800 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4EEBD3A6.4030700@stoneleaf.us> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEBD3A6.4030700@stoneleaf.us> Message-ID: <4EEBE3A9.9010706@stoneleaf.us> Ethan Furman wrote: > Terry Reedy wrote: >> On 12/16/2011 4:22 AM, Steven D'Aprano wrote: >>> On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: >>> [...] >>> >>> After reading your post, I think I have worked out where our >>> disagreement >>> lies: you think that bound methods and instance methods are not the same >>> thing, >> >> Do you agree that an unbound method and a bound method are different? >> In Python, as indicated by the glossary entry, an unspecified 'method' >> is usually meant to be an unbound method. > > I think you two are in violent agreement as far as how Python is > functioning, and the conflict is in the names given to the various > pieces... I think a glossary would help (please correct me): > > function: callable code suite > > method: function that lives in a class > > unbound method: function that lives in a class > > bound method: callable wrapper around function that has been extracted > from class that will supply the instance object to the function (note: > Python does not save these, they are recreated at each lookup) I think the above 'bound method' definition should be attributed to Terry, and Steven's follows: bound method: callable wrapper around any function that will accept an instance object as the first parameter, and the wrapper will supply said instance object when calling the function (and where/how function was created is irrelevent, as is where the wrapper is stored) > > > and here is where I think you two diverge: > > instance method (Steven): a bound method that has been saved into the > instance __dict__ (no matter how created) > > instance method (Terry): a function that must be looked up in the class > > > Have I missed anything? > > Honestly-trying-learn-the-distinctions-ly yours, > > ~Ethan~ From steve+comp.lang.python at pearwood.info Fri Dec 16 19:54:23 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2011 00:54:23 GMT Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> Message-ID: <4eebe83f$0$29979$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Dec 2011 11:40:11 -0800, Eelco wrote: > On 16 dec, 18:38, rusi wrote: >> On Dec 16, 3:25?pm, Eelco wrote: >> >> > Pseudo-backwards compatibility with other languages, I couldnt not >> > care less for. >> >> Double negations n Goedelian situations have interesting implications >> (tho here its triple) > > Heh. Well at least my extra (unintended) negation is semantically > consistent with the actual english usage of the phrase, which omits the > negation completely :). (I could care less) Oh please. "I could care less" is not English. That's American. Here in Australia, we follow the English practice of saying that we couldn't care less. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 16 20:26:24 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2011 01:26:24 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eebefc0$0$29979$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Dec 2011 17:05:57 -0500, Terry Reedy wrote: > On 12/16/2011 4:22 AM, Steven D'Aprano wrote: >> On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: [...] >> >> After reading your post, I think I have worked out where our >> disagreement lines: you think that bound methods and instance methods >> are not the same thing, > > Do you agree that an unbound method and a bound method are different? "Are different" or "are different types"? Bound and unbound methods in Python 2 are the same type. They are different in the sense that one is effectively a curry of the other, but they are implemented as a single type. So presumably the difference is a bound method has a slot filled and an unbound method doesn't, or some other implementation detail. In Python 3, unbound methods don't exist. > In > Python, as indicated by the glossary entry, an unspecified 'method' is > usually meant to be an unbound method. I think you are badly confused here. The glossary talks about calling methods as an attribute of an instance, e.g. instance.method(), and the method receiving the instance as the first argument. That's a bound method, not unbound. How do you conclude that this indicates that an unspecified "method" is unbound? Your conclusion is especially nonsensical because unbound methods don't exist in Python 3 -- you're alleging that the usual meaning of "method" is something which no longer exists in the language! method A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self). See function and nested scope. http://docs.python.org/glossary.html > It is am important distinction > and I do not see why you insist on confusing the two. It is not an important distinction, and I am not confusing the two. Bound or unbound, it is still an instance method. >> and that a function defined inside a class is different from a function >> outside of a class. > > That, and your repetition of the same claim further on, is a insulting > lie. If you can't assume I'm carrying on this discussion in good faith, then we have a serious problem. This is not like you Terry -- I've been reading your posts for many years, and you're not usually so obstreperous. > Def statements always create functions. I have known that for 14 years > since the first day I started with Python. I have never thought > differently. If you actually think that I have, you are wrong. I'm glad to hear it. But nevertheless you have made statements (which I quoted, and you deleted from your reply) that suggest the opposite. If I have misinterpreted them, or if you had worded them badly, there's no need to attribute malice to me. Calling me a liar for something said in good faith and which I prefixed with "I think" is simply not cool. -- Steven From drobinow at gmail.com Fri Dec 16 21:11:27 2011 From: drobinow at gmail.com (David Robinow) Date: Fri, 16 Dec 2011 21:11:27 -0500 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <4eebe83f$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> <4eebe83f$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 16, 2011 at 7:54 PM, Steven D'Aprano wrote: > On Fri, 16 Dec 2011 11:40:11 -0800, Eelco wrote: > >> On 16 dec, 18:38, rusi wrote: >>> On Dec 16, 3:25?pm, Eelco wrote: >>> >>> > Pseudo-backwards compatibility with other languages, I couldnt not >>> > care less for. >>> >>> Double negations n Goedelian situations have interesting implications >>> (tho here its triple) >> >> Heh. Well at least my extra (unintended) negation is semantically >> consistent with the actual english usage of the phrase, which omits the >> negation completely :). (I could care less) > > Oh please. "I could care less" is not English. That's American. > > Here in Australia, we follow the English practice of saying that we > couldn't care less. Well the phrase is still somewhat controversial in the US. I never heard it until age 19 (in 1966) and have always been somewhat disdainful of those using it. But it appears to be hopeless. http://articles.boston.com/2010-10-24/lifestyle/29303907_1_care-peeves-decades From nagle at animats.com Fri Dec 16 21:18:32 2011 From: nagle at animats.com (John Nagle) Date: Fri, 16 Dec 2011 18:18:32 -0800 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: <4eebfbff$0$1679$742ec2ed@news.sonic.net> On 12/16/2011 2:08 PM, Lie Ryan wrote: > On 12/17/2011 01:30 AM, Brad Tilley wrote: >> Or perhaps run should look like this instead: >> >> def run(t): >> lock.acquire() >> shared_container.append(t.name ) >> lock.release() >> >> That seems a bit barbaric to me, not sure. > > change that to: > > def run(t): > with lock: > shared_container.append(t.name ) > > > the `with-statement` will call lock.acquire() and lock.release(). > And, importantly, if you leave the "with" via an exception, the lock will be unlocked. John Nagle From gosefan at gmail.com Fri Dec 16 21:40:19 2011 From: gosefan at gmail.com (YAN HUA) Date: Sat, 17 Dec 2011 10:40:19 +0800 Subject: root[:]=[root,root] Message-ID: Hi,all. Could anybody tell how this code works? >>> root = [None, None] >>> root[:] = [root, root] >>> root [[...], [...]] >>> root[0] [[...], [...]] >>> root[0][0][1][1][0][0][0][1][1] [[...], [...]] >>> Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Fri Dec 16 22:05:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2011 03:05:59 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eec0715$0$29979$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Dec 2011 15:26:30 -0800, Ethan Furman wrote: > Terry Reedy wrote: >> On 12/16/2011 4:22 AM, Steven D'Aprano wrote: [...] > I think you two are in violent agreement as far as how Python is > functioning, and the conflict is in the names given to the various > pieces... I think a glossary would help (please correct me): > > function: callable code suite Sure, why not? Informally: a function is some object which can be called and returns a result. Hence we sometimes call str() and int() functions even though they are actually implemented as types. "Callable" would be better for such informal use, but newbies may not recognise the term. To be more precise: a function is a particular type of callable object created by the def and lambda statements. > method: function that lives in a class Informally, I'm happy with that definition, and use it myself. Under 99% of circumstances, it would be overly pedantic to describe spam in the following as anything other than a method: class C: def spam(self): pass Call it the duck-type principle: since spam quacks like a duck, we might as well call it a duck. If you only care about the easy cases, then spam is a method and we're done. But as a formal definition, it fails on two fronts: functions that live inside classes are not actually methods, they are functions, and while methods are commonly found in classes, they can also be found in instances, or anywhere else for that matter. Functions inside classes are still functions -------------------------------------------- I said that since spam quacks like a method, we might as well call it a method. But it doesn't swim like a method: it's actually still a function with a remarkable power of mimicry. There are places and times where the fact that spam is not actually a method comes through. The three most important examples in my opinion are: * inside the class suite, during class definition time, if you use or inspect spam, you will see it is a function and not a method; * if you bypass the getattr mechanism for looking up attributes, e.g. use C.__dict__['spam'] instead of C().spam, you will see that spam is stored in the dict as a function object, not a method object; * (Python 3 only) if use the standard getattr mechanism, but call it on the class instead of an instance, you will get the function object instead of a method. So, to be precise: spam is actually a function. It is stored as a function inside C.__dict__, but when you retrieve it using C().spam Python automagically builds a method object wrapping spam and gives you that instead. This distinction is important, well, hardly ever, but on the rare occasion it is important, you need to know it. One example of when it may be important: because methods are created as needed, you can't assume that identity checks will necessarily pass for methods: `inst.spam is inst.spam` may not be true. Methods can be created outside of classes ----------------------------------------- The normal way to get a method is by defining it inside a class. In Java, I believe that is the *only* way to get methods, but this is Python, and we have other options. Methods are first class objects, which means they have a type and a constructor, so if you can get hold of their type, you can make them by hand. You can retrieve the method type two ways: * call type on a method you made the usual way: type(C().spam); * or import types and use types.MethodType. Confusingly, the method type calls itself but is exported via the types module as MethodType. Oh well. MethodType requires two arguments, In Python 2, it accepts an optional third argument. Since Python 2 is the past, I'll only talk about the Python 3 version, where the signature might look something like this: MethodType(callable, instance) => instancemethod object This creates a wrapper around callable (usually, but not necessarily a function) and binds instance to that wrapper so the first argument (conventionally called self) can be automatically provided. Another way to create methods is by calling the function object __get__ method by hand. That's what gets used in the normal C().spam case. So you can create methods without putting them inside a class. One trick is that you can customise behaviour on a per-instance basis by overriding the normal method living inside the class with one which lives inside the instance: class C: def spam(self): return "Spam spam spam, lovely SPAM!!!" c = C() c.spam = types.MethodType(lambda self: "Bloody vikings!", c) Instance c now has a custom method which overrides C.spam. [Aside #1: I think this was the original point of contention between Terry and I. For reasons I still don't understand, it seems to me that he denies that the per-instance method spam is actually a method, since it doesn't live inside the class.] [Aside #2: this per-instance method trick may not work for double- underscore methods like __exit__, since CPython and Jython at least skip the instance __dict__ when using dunder methods. In Python 2, it will work for classic classes, but they're gone in Python 3. If you go all the way back to the initial post in this thread, that was the OP's problem.] > unbound method: function that lives in a class An unbound method is merely a method that doesn't know what instance to use, that is, it cannot automatically supply "self". In Python 2, the easiest way to get an unbound method is by retrieving the method from the class instead of an instance: C.spam => returns method which is not bound ("unbound method") C().spam => returns method which is bound ("bound method") Since unbound methods are more or less functionally identical to the underlying function (the only difference is they do a type-check on the argument), Python 3 gets rid of them and and just uses the function object itself. So all the reasons for why methods are not *strictly* functions living inside classes apply equally to unbound methods, since unbound methods are merely a particular form of method. > bound method: callable wrapper around function that has been extracted > from class that will supply the instance object to the function (note: > Python does not save these, they are recreated at each lookup) Bound methods are not *necessarily* extracted from a class, although they *usually* are. But wherever it comes from, a bound method knows which instance to provide as first argument ("self") to the function; otherwise we'd call it an unbound method. > and here is where I think you two diverge: > > instance method (Steven): a bound method that has been saved into the > instance __dict__ (no matter how created) Certainly not! If I gave that impression, I apologize. Every thing I have said above refers to *instance* methods, or just "methods" for lazy people. Instance methods get their name from the fact that they supply the instance as first argument to the function ("self"). If they instead supply the class, we call them a class method; if they don't supply anything, we call them a static method (much to the confusion of Java programmers, who use the same names for different things). You can even create your own method types: http://code.activestate.com/recipes/577030-dualmethod-descriptor/ -- Steven From lie.1296 at gmail.com Fri Dec 16 23:24:50 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 17 Dec 2011 15:24:50 +1100 Subject: root[:]=[root,root] In-Reply-To: References: Message-ID: On 12/17/2011 01:40 PM, YAN HUA wrote: > Hi,all. Could anybody tell how this code works? > >>> root = [None, None] First, you're creating a list of two None, let's say it's list-1. Then you bind the name 'root' to list-1. > >>> root[:] = [root, root] Next, you assign list-1's first member with list-1 and list-1's second member with list-1. > >>> root > [[...], [...]] The result is a recursive list, both list-1's first and second member is list-1 itself. > >>> root[0] > [[...], [...]] > >>> root[0][0][1][1][0][0][0][1][1] > [[...], [...]] > >>> > > Thanks. > > From nukeymusic at gmail.com Sat Dec 17 05:19:44 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sat, 17 Dec 2011 02:19:44 -0800 (PST) Subject: calculate difference between two timestamps [newbie] Message-ID: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> I'm trying to calculate the difference in seconds between two timestamps, but I'm totally stuck: date1="Dec-13-09:47:12" date2="Dec-13-09:47:39" >>> diff=datetime.date(date2)-datetime.date(date1) Traceback (most recent call last): File "", line 1, in TypeError: an integer is required struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S") struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S") >>> diff=datetime.date(struct_date2)-datetime.date(struct_date1) Traceback (most recent call last): File "", line 1, in TypeError: an integer is required thanks in advance nukey From d at davea.name Sat Dec 17 06:09:50 2011 From: d at davea.name (Dave Angel) Date: Sat, 17 Dec 2011 06:09:50 -0500 Subject: calculate difference between two timestamps [newbie] In-Reply-To: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> Message-ID: <4EEC787E.2080703@davea.name> On 12/17/2011 05:19 AM, nukeymusic wrote: > I'm trying to calculate the difference in seconds between two > timestamps, but I'm totally stuck: > date1="Dec-13-09:47:12" > date2="Dec-13-09:47:39" >>>> diff=datetime.date(date2)-datetime.date(date1) > Traceback (most recent call last): > File "", line 1, in > TypeError: an integer is required > > struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S") > struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S") >>>> diff=datetime.date(struct_date2)-datetime.date(struct_date1) > Traceback (most recent call last): > File "", line 1, in > TypeError: an integer is required > > thanks in advance > nukey You should post the full code; you omitted your import line(s) That last approach was closer, try this one: import datetime struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") struct_date2=datetime.datetime.strptime(date2, "%b-%d-%H:%M:%S") diff = struct_date2 - struct_date1 print diff diff is of type datetime.timedelta -- DaveA From vincent.vandevyvre at swing.be Sat Dec 17 06:13:37 2011 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Sat, 17 Dec 2011 12:13:37 +0100 Subject: calculate difference between two timestamps [newbie] In-Reply-To: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> Message-ID: <4EEC7961.7020408@swing.be> An HTML attachment was scrubbed... URL: From vince at vince.ca Sat Dec 17 06:19:09 2011 From: vince at vince.ca (Vince) Date: Sat, 17 Dec 2011 06:19:09 -0500 Subject: calculate difference between two timestamps [newbie] In-Reply-To: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> Message-ID: <20111217111909.GA16443@derelicts.org> On Sat, Dec 17, 2011 at 02:19:44AM -0800, nukeymusic wrote: > I'm trying to calculate the difference in seconds between two > timestamps, but I'm totally stuck: > date1="Dec-13-09:47:12" > date2="Dec-13-09:47:39" > >>> diff=datetime.date(date2)-datetime.date(date1) > Traceback (most recent call last): > File "", line 1, in > TypeError: an integer is required > > struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S") > struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S") > >>> diff=datetime.date(struct_date2)-datetime.date(struct_date1) > Traceback (most recent call last): > File "", line 1, in > TypeError: an integer is required You're trying to compare two time.struct_time types when they need to be datetime.datetime types. This will do the conversion for you: import datetime,time date1="Dec-13-09:47:12" date2="Dec-13-09:47:39" struct_date1=datetime.datetime(*time.strptime(date1, "%b-%d-%H:%M:%S")[:6]) struct_date2=datetime.datetime(*time.strptime(date2, "%b-%d-%H:%M:%S")[:6]) print struct_date2 - struct_date1 From gd.usenet at spamfence.net Sat Dec 17 06:20:55 2011 From: gd.usenet at spamfence.net (=?UTF-8?Q?G=C3=BCnther?= Dietrich) Date: Sat, 17 Dec 2011 12:20:55 +0100 Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> Message-ID: nukeymusic wrote: >I'm trying to calculate the difference in seconds between two [...] >>> import datetime >>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", "%b-%d-%H:%M:%S") >>> date2 = datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") >>> delta = date2 - date1 >>> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000) For very big time differences you should consider to use the Decimal arithmetics (standard module Decimal) instead of integer arithmetics for the last line. If you are sure, that you don't use fractional seconds, you can omit the part with 'delta.microseconds'. Best regards, G?nther From prabhadigi2 at gmail.com Sat Dec 17 07:11:55 2011 From: prabhadigi2 at gmail.com (prabhakaran k) Date: Sat, 17 Dec 2011 04:11:55 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From hoogendoorn.eelco at gmail.com Sat Dec 17 09:38:22 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 17 Dec 2011 06:38:22 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax Message-ID: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> This is a follow-up discussion on my earlier PEP-suggestion. Ive integrated the insights collected during the previous discussion, and tried to regroup my arguments for a second round of feedback. Thanks to everybody who gave useful feedback the last time. PEP Proposal: Pythonification of the asterisk-based collection packing/ unpacking syntax. This proposal intends to expand upon the currently existing collection packing and unpacking syntax. Thereby we mean the following related python constructs: head, *tail = somesequence #pack the remainder of the unpacking of somesequence into a list called tail def foo(*args): pass #pack the unbound positional arguments into a tuple calls args def foo(**kwargs): pass #pack the unbound keyword arguments into a dict calls kwargs foo(*args) #unpack the sequence args into positional arguments foo(**kwargs) #unpack the mapping kwargs into keyword arguments We suggest that these constructs have the following shortcomings that could be remedied. It is unnecessarily cryptic, and out of line with Pythons preference for an explicit syntax. One can not state in a single line what the asterisk operator does; this is highly context dependent, and is devoid of that ?for line in file? pythonic obviousness. From the perspective of a Python outsider, the only hint as to what *args means is by loose analogy with the C-way of handling variable arguments. The current syntax, in its terseness, leaves to be desired in terms of flexibility. While a tuple might be the logical choice to pack positional arguments in the vast majority of cases, it need not be true that a list is always the preferred choice to repack an unpacked sequence, for instance. Type constraints: In case the asterisk is not used to signal unpacking, but rather to signal packing, its semantics is essentially that of a type constraint. The statement: head, tail = sequence Signifies regular unpacking. However, if we add an asterisk, as in: head, *tail = sequence We demand that tail not be just any python object, but rather a list. This changes the semantics from normal unpacking, to unpacking and then repacking all but the head into a list. It may be somewhat counter-intuitive to think of this as a type constraint, since python is after all a weakly-typed language. But the current usage of askeriskes is an exception to that rule. For those who are unconvinced, please consider the analogy to the following simple C# code: var foo = 3; An ?untyped? object foo is created (actually, its type will be inferred from its rhs as an integer). float foo = 3; By giving foo a type-constraint of float instead, the semantics are modified; foo is no longer the integer 3, but gets silently cast to 3.0. This is a simple example, but conceptually entirely analogous to what happens when one places an asterisk before an lvalue in Python. It means ?be a list, and adjust your behavior accordingly?, versus ?be a float, and adjust your behavior accordingly?. The aim of this PEP, is that this type-constraint syntax is expanded upon. We should be careful here to distinguish with providing optional type constraints throughout python as a whole; this is not our aim. This concept has been considered before, but the costs have not been found to out-weight the benefits. http://www.artima.com/weblogs/viewpost.jsp?thread=86641 Our primary aim is the niche of collection packing/unpacking, but if further generalizations can be made without increasing the cost, those are most welcome. To reiterate: what is proposed is nothing radical; merely to replace the asterisk-based type constraints with a more explicit type constraint. Currently favored alternative syntax: Both for the sake of explicitness and flexibility, we consider it desirable that the name of the collection type is used directly in any collection packing statement. Annotating a variable declaration with a collection type name should signal collection packing. This association between a collection type name and a variable declaration can be accomplished in many ways; for now, we suggest collectionname::collectiontype for packing, and ::collectionname for unpacking. Examples of use: head, tail::tuple = ::sequence def foo(args::list, kwargs::dict): pass foo(::args, ::kwargs) The central idea is to replace annotations with asteriskes by annotations with collection type names, but note that we have opted for several other minor alterations of the existing syntax that seem natural given the proposed changes. First of all, explicitly mentioning the type of the collection involved eliminates the need to have two symbols, * and **. Which variable captures the positional arguments and which captures the keyword arguments can be inferred from the collection type they model, mapping or sequence. The rare case of collections that both model a sequence and a mapping can either be excluded or handled by assigning precedence for one type or the other. A double semicolon before a collection type signals unpacking. As with declarations, there is no genuine need to have a different operator for sequence and mapping types, although if such a demand exists, it would not be hard to accommodate. A double semicolon in front of the collection is congruent with the asterisk syntax, and nicely emphasizes this unpacking operation being the symmetric counterpart of the packing operation, which is signalled by the same symbols to the right of the identifier. Since we are going to make the double semicolon (or whatever the symbol) a general collection packing/ unpacking marker, we feel it makes sense to allow it to be used to explicitly signify unpacking, even when as much is implied by the syntax on the left hand side, to preserve symmetry with the syntax inside function calls. Summarizing, what this syntax achieves, in loose order of perceived importance: Simplicity: we have reduced a set of rather arbitrary rules concerning the syntax and semantics of the asterisk (does it construct a list or a tuple?) to a single general symbol: the double semicolon is the collection packing/unpacking annotation symbol, and that is all there is to know about it. Readability: the proposed syntax reads like a book: args-list and kwargs-dict, unlike the more cryptic asterisk syntax. We avoid extra lines of code in the event another sequence or mapping type than the one returned by default is required. Efficiency: by declaring the desired collection type, it can be constructed in the optimal way from the given input, rather than requiring a conversion after the default collection type is constructed. A double semicolon is suggested, since the single colon is already taken by the function annotation syntax in Python 3. This is somewhat unfortunate: programming should come before meta-programming, and it should rather be the other way around. On the one hand having both : and :: as variable declaration annotation symbols is a nice unification, on the other hand, a syntax more easily visually distinguished from function annotations can be defended. For increased backwards compatibility the asterisk could be used, but sandwiched between two identifiers it looks like a multiplication. But many others symbols would do, such as @ or !. From alex.kapps at web.de Sat Dec 17 10:22:32 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sat, 17 Dec 2011 16:22:32 +0100 Subject: Help about Xlib Library in Python In-Reply-To: References: Message-ID: <4EECB3B8.7000808@web.de> On 16.12.2011 05:55, ?? wrote: > Hi, > > A question about Xlib Library in Python troubled me for several days > and I finally found this email list. I hope someone could answer my > question. I think it is easy for experienced user. > > I would like to write a small script to response my mouse click in > root screen and write something in the terminal. My script is like > this, but it does not work in my computer. > > from Xlib import X > import Xlib.display > > def main(): > display = Xlib.display.Display() > root = display.screen().root > root.change_attributes(event_mask= > X.ButtonPressMask | > X.ButtonReleaseMask) > while True: > event = root.display.next_event() > print "1" > if __name__ == "__main__": > main() > > > Any hints are welcome > Thank you! > > Ruan zheng > You haven't said *how* your script is not working and what distro/desktop-environment you use, but I suspect, that your script collides with the window manager. Most window managers occupy the root window for them self, and even display another window above the root window (for the wallpaper, the desktop icons, etc.), so your clicks never reach the root window, but whatever the window manager draws as the background. If you run your script in a plain and naked X "session" with nothing but an xterm *and no window manager*, your script will probably work. From dihedral88888 at googlemail.com Sat Dec 17 11:34:59 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 17 Dec 2011 08:34:59 -0800 (PST) Subject: Help about Xlib Library in Python In-Reply-To: References: Message-ID: <18311531.104.1324139699498.JavaMail.geo-discussion-forums@prdr27> On Saturday, December 17, 2011 11:22:32 PM UTC+8, Alexander Kapps wrote: > On 16.12.2011 05:55, ?? wrote: > > Hi, > > > > A question about Xlib Library in Python troubled me for several days > > and I finally found this email list. I hope someone could answer my > > question. I think it is easy for experienced user. > > > > I would like to write a small script to response my mouse click in > > root screen and write something in the terminal. My script is like > > this, but it does not work in my computer. > > > > from Xlib import X > > import Xlib.display > > > > def main(): > > display = Xlib.display.Display() > > root = display.screen().root > > root.change_attributes(event_mask= > > X.ButtonPressMask | > > X.ButtonReleaseMask) > > while True: > > event = root.display.next_event() > > print "1" > > if __name__ == "__main__": > > main() > > > > > > Any hints are welcome > > Thank you! > > > > Ruan zheng > > > > You haven't said *how* your script is not working and what > distro/desktop-environment you use, but I suspect, that your script > collides with the window manager. > > Most window managers occupy the root window for them self, and even > display another window above the root window (for the wallpaper, the > desktop icons, etc.), so your clicks never reach the root window, > but whatever the window manager draws as the background. > > If you run your script in a plain and naked X "session" with nothing > but an xterm *and no window manager*, your script will probably work. Check co-linux and VNC and X-windows. It's trivial to play with PYTHON under co-linux. NT, WindowsXP, and Windows7 are excellent in the heap manager of a single process with multiple threads. Linux is multiple-process not multiple threads. Also the brain damaged heap walker is inferior to $$$MS. From dihedral88888 at googlemail.com Sat Dec 17 11:34:59 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 17 Dec 2011 08:34:59 -0800 (PST) Subject: Help about Xlib Library in Python In-Reply-To: References: Message-ID: <18311531.104.1324139699498.JavaMail.geo-discussion-forums@prdr27> On Saturday, December 17, 2011 11:22:32 PM UTC+8, Alexander Kapps wrote: > On 16.12.2011 05:55, ?? wrote: > > Hi, > > > > A question about Xlib Library in Python troubled me for several days > > and I finally found this email list. I hope someone could answer my > > question. I think it is easy for experienced user. > > > > I would like to write a small script to response my mouse click in > > root screen and write something in the terminal. My script is like > > this, but it does not work in my computer. > > > > from Xlib import X > > import Xlib.display > > > > def main(): > > display = Xlib.display.Display() > > root = display.screen().root > > root.change_attributes(event_mask= > > X.ButtonPressMask | > > X.ButtonReleaseMask) > > while True: > > event = root.display.next_event() > > print "1" > > if __name__ == "__main__": > > main() > > > > > > Any hints are welcome > > Thank you! > > > > Ruan zheng > > > > You haven't said *how* your script is not working and what > distro/desktop-environment you use, but I suspect, that your script > collides with the window manager. > > Most window managers occupy the root window for them self, and even > display another window above the root window (for the wallpaper, the > desktop icons, etc.), so your clicks never reach the root window, > but whatever the window manager draws as the background. > > If you run your script in a plain and naked X "session" with nothing > but an xterm *and no window manager*, your script will probably work. Check co-linux and VNC and X-windows. It's trivial to play with PYTHON under co-linux. NT, WindowsXP, and Windows7 are excellent in the heap manager of a single process with multiple threads. Linux is multiple-process not multiple threads. Also the brain damaged heap walker is inferior to $$$MS. From steve+comp.lang.python at pearwood.info Sat Dec 17 12:00:46 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2011 17:00:46 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> Message-ID: <4eeccabe$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 06:38:22 -0800, Eelco wrote: > One can not state in a single line what the asterisk > operator does; Multiplication, exponentiation, sequence packing/unpacking, and varargs. -- Steven From roy at panix.com Sat Dec 17 12:14:12 2011 From: roy at panix.com (Roy Smith) Date: Sat, 17 Dec 2011 12:14:12 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eeccabe$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4eeccabe$0$29979$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > On Sat, 17 Dec 2011 06:38:22 -0800, Eelco wrote: > > > One can not state in a single line what the asterisk > > operator does; > > Multiplication, exponentiation, sequence packing/unpacking, and varargs. Import wildcarding? From rosuav at gmail.com Sat Dec 17 12:18:15 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 04:18:15 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eeccabe$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 18, 2011 at 4:14 AM, Roy Smith wrote: > Import wildcarding? That's not an operator, any more than it is when used in filename globbing. The asterisk _character_ has many meanings beyond those of the operators * and **. ChrisA From nagle at animats.com Sat Dec 17 14:24:24 2011 From: nagle at animats.com (John Nagle) Date: Sat, 17 Dec 2011 11:24:24 -0800 Subject: Python install regression test fail Message-ID: <4eecec6e$0$1707$742ec2ed@news.sonic.net> Here's a result from "make", "make test" of a fresh download of Python 2.7.2 on Linux 2.6.18-1.2239.fc5smp: 350 tests OK. 2 tests failed: test_os test_site 35 tests skipped: test_aepack test_al test_applesingle test_bsddb185 test_bsddb3 test_cd test_cl test_codecmaps_cn test_codecmaps_hk test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw test_curses test_gdb test_gl test_imgfile test_kqueue test_linuxaudiodev test_macos test_macostools test_msilib test_ossaudiodev test_scriptpackages test_smtpnet test_socketserver test_startfile test_sunaudiodev test_timeout test_tk test_ttk_guionly test_urllib2net test_urllibnet test_winreg test_winsound test_zipfile64 3 skips unexpected on linux2: test_gdb test_tk test_ttk_guionly make: *** [test] Error 1 -bash-3.1$ Here are the fails: test_os test test_os failed -- Traceback (most recent call last): File "/var/www/vhosts/sitetruth.com/private/downloads/python/Python-2.7.2/Lib/test/test_os.py", line 563, in helper self.check(getattr(os, f)) File "/var/www/vhosts/sitetruth.com/private/downloads/python/Python-2.7.2/Lib/test/test_os.py", line 572, in check self.assertEqual(e.errno, errno.EBADF) AssertionError: 25 != 9 (This test expected EBADF and got ENOTTY on making a bad file descriptor. Not clear why it got ENOTTY, but that's "Inappropriate IOCTL", as if the file descriptor number was in use but not valid for the test.) test_site test test_site crashed -- : [Errno 13] Permission denied: '/var/www/vhosts/sitetruth.com/.local' (That looks like a bug in the test suite. It shouldn't be storing into the user's home directory.) John Nagle From yasar11732 at gmail.com Sat Dec 17 14:33:01 2011 From: yasar11732 at gmail.com (=?ISO-8859-9?Q?Ya=FEar_Arabac=FD?=) Date: Sat, 17 Dec 2011 21:33:01 +0200 Subject: Bug in multiprocessing.reduction? Message-ID: You can see my all code below, theoritically that code should work I guess. But I keep getting this error: [SUBWARNING/MainProcess] thread for sharing handles raised exception : ------------------------------------------------------------------------------- Traceback (most recent call last): File "/usr/lib/python2.7/multiprocessing/reduction.py", line 127, in _serve send_handle(conn, handle_wanted, destination_pid) File "/usr/lib/python2.7/multiprocessing/reduction.py", line 80, in send_handle _multiprocessing.sendfd(conn.fileno(), handle) OSError: [Errno 9] Bad file descriptor ------------------------------------------------------------------------------- Do you see an error in my side, or is this a bug in Python? import multiprocessing as mp import logging import socket import time from random import randint from multiprocessing.reduction import reduce_socket, rebuild_socket logger = mp.log_to_stderr(logging.DEBUG) def worker(queue): while True: reduced_socket = queue.get() # This is the line where I get Bad file descriptor error: print reduced_socket[0](*reduced_socket[1]) if __name__ == '__main__': serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind(('',9090)) serversocket.listen(5) num_workers = 5 socket_queue = mp.Queue() worker_processes = [mp.Process(target=worker,args=(socket_queue,)) for i in range(num_workers)] for worker_process in worker_processes: worker_process.daemon = True worker_process.start() while True: client, address = serversocket.accept() client_handle = reduce_socket(client) socket_queue.put(client_handle) -- http://yasar.serveblog.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid.invalid Sat Dec 17 15:02:44 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Sat, 17 Dec 2011 20:02:44 +0000 (UTC) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> <9l23ntFsibU1@mid.individual.net> Message-ID: On 2011-12-16, Gregory Ewing wrote: > Eelco wrote: >> the actual english usage of the phrase, which omits >> the negation completely :). (I could care less) > > No, that's the American usage. That's the _ignorant_ American usage. Americans with a clue use the "couldn't" version. I won't comment on the relative sizes of the two groups. > The English usage is "I couldn't care less", which has the advantage > of actually making sense. Indeed. -- Grant Edwards grant.b.edwards Yow! HUGH BEAUMONT died at in 1982!! gmail.com From hoogendoorn.eelco at gmail.com Sat Dec 17 15:11:04 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 17 Dec 2011 12:11:04 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eeccabe$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 17, 6:18?pm, Chris Angelico wrote: > On Sun, Dec 18, 2011 at 4:14 AM, Roy Smith wrote: > > Import wildcarding? > > That's not an operator, any more than it is when used in filename > globbing. The asterisk _character_ has many meanings beyond those of > the operators * and **. > > ChrisA To cut short this line of discussion; I meant the asterisk symbol purely in the context of collection packing/unpacking. Of course it has other uses too. Even that single use requires a whole paragraph to explain completely; when does it result in a tuple or a list, when is unpacking implicit and when not, why * versus **, and so on. From vinay_sajip at yahoo.co.uk Sat Dec 17 15:44:45 2011 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Sat, 17 Dec 2011 12:44:45 -0800 (PST) Subject: logging issues References: <4EE77279.10104@gmail.com> <4EE77933.2060705@sequans.com> <4EE77B7B.9090408@gmail.com> Message-ID: <1f2ca0bf-4c47-416d-b2d4-e526c56db400@f1g2000yqi.googlegroups.com> On Dec 13, 5:03?pm, Andrea Crotti wrote: > The set_verbosity seems to do its job, the second is also > called and the filters are added but I don't see the output > formatted as it should.. Generally speaking, you should only do logging configuration once, as Jean-Michel said - from your main() function or "if __name__ == '__main__'" suite. Until you show more detail about what you're getting and what you're expecting, it's hard to provide more help. Regards, Vinay Sajip From steve+comp.lang.python at pearwood.info Sat Dec 17 18:20:10 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2011 23:20:10 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eeccabe$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eed23a9$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 12:11:04 -0800, Eelco wrote: > > One can not state in a single line what the asterisk > > operator does; ... > To cut short this line of discussion; I meant the asterisk symbol purely > in the context of collection packing/unpacking. Of course it has other > uses too. > > Even that single use requires a whole paragraph to explain completely; > when does it result in a tuple or a list, when is unpacking implicit and > when not, why * versus **, and so on. Do you think that this paragraph will become shorter if you change the spelling * to something else? It takes more than one line to explain list comprehensions, content managers, iterators, range(), and import. Why should we care if * and ** also take more than one paragraph? Even if you could get it down to a single line, what makes you think that such extreme brevity is a good thing? You might not be able to explain them in a single line, but you can explain them pretty succinctly: Varags: Inside a function parameter list, * collects an arbitrary number of positional arguments into a tuple. When calling functions, * expands any iterator into positional arguments. In both cases, ** does the same thing for keyword arguments. Extended iterator unpacking: On the left hand side of an assignment, * collects multiple values from the right hand side into a list. Let's see you do better with your suggested syntax. How concisely can you explain the three functions? Don't forget the new type coercions (not constraints, as you keep calling them) you're introducing. It boggles my mind that you complain about the complexity of existing functionality, and your solution involves *increasing* the complexity with more functionality. -- Steven From rbanffy at gmail.com Sat Dec 17 18:53:43 2011 From: rbanffy at gmail.com (=?ISO-8859-1?Q?Ricardo_B=E1nffy?=) Date: Sat, 17 Dec 2011 21:53:43 -0200 Subject: PyCon US 2012 sprints Message-ID: Hi folks. Next March I'm planning to attend PyCon US (for the first time) and stay for the sprints. I am not sure how they work, however. Are there any "first-timer guide to PyCon sprints"? -- Ricardo B?nffy http://www.dieblinkenlights.com http://twitter.com/rbanffy From steve+comp.lang.python at pearwood.info Sat Dec 17 19:59:45 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 00:59:45 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> Message-ID: <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 06:38:22 -0800, Eelco wrote: > Type constraints: > > In case the asterisk is not used to signal unpacking, but rather to > signal packing, its semantics is essentially that of a type constraint. "Type constraint" normally refers to type restrictions on *input*: it is a restriction on what types are accepted. When it refers to output, it is not normally a restriction, therefore "constraint" is inappropriate. Instead it is normally described as a coercion, cast or conversion. Automatic type conversions are the opposite of a constraint: it is a loosening of restrictions. "I don't have to use a list, I can use any sequence or iterator". In iterator unpacking, it is the *output* which is a list, not a restriction on input: in the statement: head, *tail = sequence tail may not exist before the assignment, and so describing this as a constraint on the type of tail is completely inappropriate. > The statement: > > head, tail = sequence > > Signifies regular unpacking. However, if we add an asterisk, as in: > > head, *tail = sequence > > We demand that tail not be just any python object, but rather a list. We don't demand anything, any more than when we say: for x in range(1, 100): we "demand" that x is not just any python object, but rather an int. Rather, we accept what we're given: in case of range and the for loop, we are given an int. In the case of extended tuple unpacking, we are given a list. > This changes the semantics from normal unpacking, to unpacking and then > repacking all but the head into a list. Aside: iterator unpacking is more general than just head/tail unpacking. >>> a, b, *var, c, d, e = range(10) >>> print(a, b, c, d, e, var) 0 1 7 8 9 [2, 3, 4, 5, 6] You are jumping to conclusions about implementation details which aren't supported by the visible behaviour. What evidence do you have that iterator unpacking creates a tuple first and then converts it to a list? > It may be somewhat counter-intuitive to think of this as a type > constraint, since python is after all a weakly-typed language. The usual test of a weakly-typed language is that "1"+1 succeeds (and usually gives 2), as in Perl but not Python. I believe you are confusing weak typing with dynamic typing, a common mistake. [...] > The aim of this PEP, is that this type-constraint syntax is expanded > upon. We should be careful here to distinguish with providing optional > type constraints throughout python as a whole; this is not our aim. Iterator unpacking is no more about type constraints than is len(). -- Steven From edriscoll at wisc.edu Sat Dec 17 20:29:53 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 19:29:53 -0600 Subject: Parsing stream of JSON objects incrementally Message-ID: <4EED4211.5080308@wisc.edu> I'm interested in writing two programs, A and B, which communicate using JSON. At a high level, A wants to transfer an array to B. However, I would very much like to make it possible for A and B to run in parallel, so my current plan is to have A output and B read a *sequence* of JSON objects. In other words, instead of [ {"a": 0}, {"b":0}, {"c": 0} ] it would just send {"a": 0} {"b": 0} {"c": 0} I know about the raw_decode() object inside the json.JSONParser class, and that gets me most of the way there there. However, what I'm *not* sure about is the best way to get the input to the raw_decode() function, which expects a "string or buffer": >>> d = json.JSONDecoder() >>> d.raw_decode(sys.stdin) Traceback (most recent call last): ... File "json\scanner.py", line 42, in iterscan match = self.scanner.scanner(string, idx).match TypeError: expected string or buffer Now I'm not very familiar with the buffer and how it could be used (and whether a file or stdin could be used as one in an incremental fashion), but the best way I can come up with is the following: 1. Read a line of input 2. Try to decode it 3. If not, read another line, concatenate it to the end, and try again 4. etc. That seems... inelegant at least. Some other information: * I'm looking for a 2.7 solution ideally * I'd prefer not to use a different JSON library entirely * As suggested, I *am* willing to wait for a newline to do processing * However, I don't want to require exactly one object per line (and want to allow both multiple objects on one line and newlines within an object) Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From tjreedy at udel.edu Sat Dec 17 21:09:36 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Dec 2011 21:09:36 -0500 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4eebefc0$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eebefc0$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/16/2011 8:26 PM, Steven D'Aprano wrote: > On Fri, 16 Dec 2011 17:05:57 -0500, Terry Reedy wrote: >> It is am important distinction [unbound versus bound] > It is not an important distinction, and I am not confusing the two. So we agree on the distinction but disagree on its importance. Let us leave it at that. > Bound or unbound, it is still an instance method. OK. So 'instance method' is a bit ambiguous (more than I thought, or would prefer) in that it can refer to unbound methods, bounds methods, or both. So be it. >>> and that a function defined inside a class is different from a function >>> outside of a class. >> >> That, and your repetition of the same claim further on, is a insulting >> lie. > > If you can't assume I'm carrying on this discussion in good faith, If you can't assume that I am intelligent and experienced enough to know the meaning of def, one on the most basic aspects of Python, and you are unwilling to give me the benefit of any doubt you might have on that score, but instead go on to 'correct' me publicly, then no, I can't. >> Def statements always create functions. I have known that for 14 years >> since the first day I started with Python. I have never thought >> differently. If you actually think that I have, you are wrong. > > I'm glad to hear it. But nevertheless you have made statements (which I > quoted, and you deleted from your reply) that suggest the opposite. OK, let us look at the my statement and your 'repetition of the same claim further on' that I previously deleted. I wrote >> These are bound methods. The instance methods are the functions wrapped. As I indicated in response to Ethan, I would now revised the second sentence now to "The unbound methods are the function wrapped" or "The instance-requiring methods are the functions wrapped." But that is not important here. In my opinion, there is no way that anyone reading that in good faith could conclude that I do not know the meaning of def statements. They are not the subject of discussion in that sentence or the rest of this thread. But in response you wrote. > I am afraid you are mistaken. About what? You go on to explain. > What you say may very well apply to other languages, > but in Python, def creates functions no matter where you > execute it. Always and without exception. So that makes twice that you said or implied that I think the location of a def statement changes what it creates, even though I explicitly said the opposite when I suggested that the glossary entry might be revised. What am I to think at such a tactic. You are normally much more careful in what you write. > If I have misinterpreted them, or if you had worded them badly, > there's no need to attribute malice to me. I did not do that. I gave my opinion of your statement, just as you have given your opinions of mine. I really did not and do not know why you misrepresented my knowledge of Python. I actually consider overt intentional malice much less likely than other possibilities. > Calling me a liar I did not do that, any more than you have been calling me things. I believe you are asking for the same 'benefit of the doubt' that I believe you denied to me. -- Terry Jan Reedy From rosuav at gmail.com Sat Dec 17 21:45:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 13:45:35 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 18, 2011 at 11:59 AM, Steven D'Aprano wrote: > The usual test of a weakly-typed language is that "1"+1 succeeds (and > usually gives 2), as in Perl but not Python. I believe you are confusing > weak typing with dynamic typing, a common mistake. I'd go stronger than "usually" there. If "1"+1 results in "11", then that's not weak typing but rather a convenient syntax for stringification - if every object can (or must) provide a to-string method, and concatenating anything to a string causes it to be stringified, then it's still strongly typed. Or is a rich set of automated type-conversion functions evidence of weak typing? And if so, then where is the line drawn - is upcasting of int to float weak? ChrisA From edriscoll at wisc.edu Sat Dec 17 22:03:01 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 21:03:01 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EED57E5.2020900@wisc.edu> On 12/17/2011 20:45, Chris Angelico wrote: > I'd go stronger than "usually" there. If "1"+1 results in "11", then > that's not weak typing but rather a convenient syntax for > stringification - if every object can (or must) provide a to-string > method, and concatenating anything to a string causes it to be > stringified, then it's still strongly typed. > > Or is a rich set of automated type-conversion functions evidence of > weak typing? And if so, then where is the line drawn - is upcasting of > int to float weak? > > ChrisA Sorry, I just subscribed to the list so am stepping in mid-conversation, but "strong" vs "weak" typing does not have a particularly well-defined meaning. There are at least three very different definitions you'll find people use which are almost pairwise orthogonal in theory, if less so in practice. There's a great mail to a Perl mailing list I've seen [1] where someone lists *eight* definitions (albeit with a couple pairs of definitions that are only slightly different). I like to use it in the "automated conversion" sense, because I feel like the other possible definitions are covered by other terms (static/dynamic, and safe/unsafe). And in that sense, I think that thinking of languages as "strong" *or* "weak" is a misnomer; it's a spectrum. (Actually even a spectrum is simplifying things -- it's more like a partial order.) Something like ML or Haskell, which does not even allow integer to double promotions, is very strong typing. Something like Java, which allows some arithmetic conversion and also automatic stringification (a la "1" + 1) is somewhere in the middle of the spectrum. Personally I'd put Python even weaker on account of things such as '[1,2]*2' and '1 < True' being allowed, but on the other hand it doesn't allow "1"+1. Evan [1] http://groups.google.com/group/comp.lang.perl.moderated/msg/89b5f256ea7bfadb (though I don't think I've seen all of those) -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From edriscoll at wisc.edu Sat Dec 17 22:08:04 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 21:08:04 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4EED57E5.2020900@wisc.edu> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EED57E5.2020900@wisc.edu> Message-ID: <4EED5914.6030903@wisc.edu> On 12/17/2011 21:03, Evan Driscoll wrote: > Personally I'd put Python even weaker on account of things such as > '[1,2]*2' and '1 < True' being allowed, but on the other hand it > doesn't allow "1"+1. Not to mention duck typing, which under my definition I'd argue is pretty much the weakest of typing that you can apply to structure-like types which I can think of. Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From tjreedy at udel.edu Sat Dec 17 22:08:24 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Dec 2011 22:08:24 -0500 Subject: root[:]=[root,root] In-Reply-To: References: Message-ID: On 12/16/2011 9:40 PM, YAN HUA wrote: > Hi,all. Could anybody tell how this code works? > >>> root = [None, None] > >>> root[:] = [root, root] > >>> root > [[...], [...]] > >>> root[0] > [[...], [...]] > >>> root[0][0][1][1][0][0][0][1][1] > [[...], [...]] A simpler example: >>> l = [] >>> l.append(l) >>> l [[...]] Python is (now) smart enough to recognize a recursive list and print '...' instead of going into an infinite loop printing '['s (as it once did, I believe). -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Sat Dec 17 22:33:03 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 03:33:03 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eed5eef$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Dec 2011 13:45:35 +1100, Chris Angelico wrote: > On Sun, Dec 18, 2011 at 11:59 AM, Steven D'Aprano > wrote: >> The usual test of a weakly-typed language is that "1"+1 succeeds (and >> usually gives 2), as in Perl but not Python. I believe you are >> confusing weak typing with dynamic typing, a common mistake. > > I'd go stronger than "usually" there. If "1"+1 results in "11", then > that's not weak typing but rather a convenient syntax for > stringification - if every object can (or must) provide a to-string > method, and concatenating anything to a string causes it to be > stringified, then it's still strongly typed. For what it's worth, Wikipedia's article on type systems gives Javascript as an example of weak typing because it converts "1"+1 to "11". I agree with them. http://en.wikipedia.org/wiki/Type_system I think that weak and strong typing aren't dichotomies, but extremes in a continuum. Assembly languages are entirely weak, since everything is bytes and there are no types to check; some academic languages may be entire strong; but most real-world languages include elements of both. Most commonly coercing ints to floats. Chris Smith's influence article "What To Know Before Debating Type Systems" goes further, suggesting that weak and strong typing are meaningless terms. I don't go that far, but you should read his article: http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/ > Or is a rich set of automated type-conversion functions evidence of weak > typing? And if so, then where is the line drawn - is upcasting of int to > float weak? To my mind, the distinction that should be drawn is that if two types are in some sense the same *kind* of thing, then automatic conversions or coercions are weak evidence of weak typing. Since we consider both ints and floats to be kinds of numbers, mixed int/float arithmetic is not a good example of weak typing. But since numbers and strings are quite different kinds of things, mixed str/int operations is a good example of weak typing. But not *entirely* different: numbers can be considered strings of digits; and non-digit strings can have numeric values. I don't know of any language that allows 1 + "one" to return 2, but such a thing wouldn't be impossible. -- Steven From kmshafique at yahoo.com Sat Dec 17 22:35:33 2011 From: kmshafique at yahoo.com (Shafique, M. (UNU-MERIT)) Date: Sat, 17 Dec 2011 19:35:33 -0800 (PST) Subject: Newman's community structure algorithms Message-ID: <1324179333.70741.YahooMailNeo@web65405.mail.ac4.yahoo.com> Hello, I am in need of the python or matlab implementation of following algorithms: Newman, M. E. J. 2006. Modularity and community structure in networks. PNAS 103(23): 8577-8582. Newman, M. E. J. and Leicht, E. A. 2007. Mixture models and exploratory analysis in networks. PNAS 104(23): 9564-9569. If anyone could share that or guide me to a possible source, I shall be immensely grateful. Best regards,? --- Muhammad Shafique? Ph.D. Fellow? United Nations University, UNU-MERIT? Keizer Kareplein 19, 6211 TC Maastricht? The Netherlands http://www.merit.unu.edu/about/profile.php?id=1058 -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sat Dec 17 22:42:05 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 14:42:05 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4EED57E5.2020900@wisc.edu> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EED57E5.2020900@wisc.edu> Message-ID: On Sun, Dec 18, 2011 at 2:03 PM, Evan Driscoll wrote: > Sorry, I just subscribed to the list so am stepping in mid-conversation, Welcome to the list! If you're curious as to what's happened, check the archives: http://mail.python.org/pipermail/python-list/ > Something like ML or Haskell, which does not even allow integer to > double promotions, is very strong typing. Something like Java, which > allows some arithmetic conversion and also automatic stringification (a > la "1" + 1) is somewhere in the middle of the spectrum. Personally I'd > put Python even weaker on account of things such as '[1,2]*2' and '1 < > True' being allowed, but on the other hand it doesn't allow "1"+1. But [1,2]*2 is operator overloading. The language doesn't quietly convert [1,2] into a number and multiply that by 2, it keeps it as a list and multiplies the list by 2. Allowing 1 < True is weaker typing. It should be noted, however, that "1 < True" is False, and "1 > True" is also False. The comparison doesn't make much sense, but it's not an error. ChrisA From roy at panix.com Sat Dec 17 22:59:17 2011 From: roy at panix.com (Roy Smith) Date: Sat, 17 Dec 2011 22:59:17 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eed5eef$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4eed5eef$0$29979$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > some academic languages may be > entire strong; but most real-world languages include elements of both. > Most commonly coercing ints to floats. Early Fortran compilers did not automatically promote ints to floats. > But not *entirely* different: numbers can be considered strings of > digits; and non-digit strings can have numeric values. I don't know of > any language that allows 1 + "one" to return 2, but such a thing wouldn't > be impossible. It is possible for 1 + "one" to be equal to 2 in C or C++. All it takes is for the string literal to be located at memory location 1. Not likely, but nothing in the language prevents it. From rosuav at gmail.com Sat Dec 17 23:05:33 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 15:05:33 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eed5eef$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 18, 2011 at 2:59 PM, Roy Smith wrote: > It is possible for 1 + "one" to be equal to 2 in C or C++. ?All it takes > is for the string literal to be located at memory location 1. ?Not > likely, but nothing in the language prevents it. Not quite; 1 + "one" will be "ne", which might happen to be at memory location 2. The data type is going to be char* (or, in a modern compiler, const char*), not int. That said, though, I think that (in this obscure circumstance) it would compare equal with 2. For what that's worth. :) ChrisA From workitharder at gmail.com Sat Dec 17 23:17:35 2011 From: workitharder at gmail.com (buck) Date: Sat, 17 Dec 2011 20:17:35 -0800 (PST) Subject: Debugging a difficult refcount issue. Message-ID: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. How should I go about debugging this further? I've been completely stuck on this for two days now :( [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload From edriscoll at wisc.edu Sat Dec 17 23:43:16 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 22:43:16 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EED57E5.2020900@wisc.edu> Message-ID: <4EED6F64.5020906@wisc.edu> On 12/17/2011 21:42, Chris Angelico wrote: > Welcome to the list! If you're curious as to what's happened, check > the archives: > http://mail.python.org/pipermail/python-list/ Thanks! Incidentally, is there a good way to respond to the original post in this thread, considering it wasn't delivered to me? > But [1,2]*2 is operator overloading. The language doesn't quietly > convert [1,2] into a number and multiply that by 2, it keeps it as a > list and multiplies the list by 2. > > Allowing 1 < True is weaker typing. It should be noted, however, that > "1 < True" is False, and "1 > True" is also False. The comparison > doesn't make much sense, but it's not an error. I see where you're coming from, especially as I wouldn't consider overloading a function on types (in a language where that phrase makes sense) moving towards weak typing either. Or at least I wouldn't have before this discussion... At the same time, it seems a bit implementationy. I mean, suppose '1' were an object and implemented __lt__. Does it suddenly become not weak typing because of that? (The other thing is that I think strong vs weak is more subjective than many of the other measures. Is '1 < True' or '"1"+1' weaker? I think it has a lot to do with how the operations provided by the language play to your expectations.) On 12/17/2011 22:05, Chris Angelico wrote: > Not quite; 1 + "one" will be "ne", which might happen to be at memory > location 2. The data type is going to be char* (or, in a modern > compiler, const char*), not int. I'm not quite sure I'd say that it could be 2, exactly, but I definitely disagree with this... after running 'int x = 5, *p = &x' would you say that "p is 5"? (Assume &x != 5.) 1+"one" *points to* "ne", but it's still a pointer. Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From workitharder at gmail.com Sat Dec 17 23:52:32 2011 From: workitharder at gmail.com (buck) Date: Sat, 17 Dec 2011 20:52:32 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> Message-ID: <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> I like the spirit of this. Let's look at your examples. > Examples of use: > head, tail::tuple = ::sequence > def foo(args::list, kwargs::dict): pass > foo(::args, ::kwargs) My initial reaction was "nonono!", but this is simply because of the ugliness. The double-colon is very visually busy. I find that your second example is inconsistent with the others. If we say that the variable-name is always on the right-hand-side, we get: > def foo(list::args, dict::kwargs): pass This nicely mirrors other languages (such as in your C# example: "float foo") as well as the old python behavior (prefixing variables with */** to modify the assignment). As for the separator, let's examine the available ascii punctuation. Excluding valid variable characters, whitespace, and operators, we have: ! -- ok. " -- can't use this. Would look like a string. # -- no. Would looks like a comment. $ -- ok. ' -- no. Would look like a string. ( -- no. Would look like a function. ) -- no. Would look like ... bad syntax. , -- no. Would indicate a separate item in the variable list. . -- no. Would look like an attribute. : -- ok, maybe. Seems confusing in a colon-terminated statement. ; -- no, just no. ? -- ok. @ -- ok. [ -- no. Would look like indexing. ] -- no. ` -- no. Would look like a string? { -- too strange } -- too strange ~ -- ok. That leaves these. Which one looks least strange? float ! x = 1 float $ x = 1 float ? x = 1 float @ x = 1 The last one looks decorator-ish, but maybe that's proper. The implementation of this would be quite decorator-like: take the "normal" value of x, pass it through the indicated function, assign that value back to x. Try these on for size. head, @tuple tail = sequence def foo(@list args, @dict kwargs): pass foo(@args, @kwargs) For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. -buck From rosuav at gmail.com Sun Dec 18 00:00:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 16:00:46 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4EED6F64.5020906@wisc.edu> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EED57E5.2020900@wisc.edu> <4EED6F64.5020906@wisc.edu> Message-ID: On Sun, Dec 18, 2011 at 3:43 PM, Evan Driscoll wrote: > On 12/17/2011 21:42, Chris Angelico wrote: >> Welcome to the list! If you're curious as to what's happened, check >> the archives: >> http://mail.python.org/pipermail/python-list/ > Thanks! Incidentally, is there a good way to respond to the original > post in this thread, considering it wasn't delivered to me? I don't know of a way, but this works. It's all part of the same thread. > I mean, suppose '1' were an object and implemented > __lt__. Does it suddenly become not weak typing because of that? Is it weak typing to overload a function? //C++ likes this a lot. int foo(int x,int y) {return x*3+y;} double foo(double x,double y) {return x*3+y;} This is definitely making the term "strongly typed language" fairly useless. > (The other thing is that I think strong vs weak is more subjective than > many of the other measures. Is '1 < True' or '"1"+1' weaker? I think it > has a lot to do with how the operations provided by the language play to > your expectations.) +1. My expectations are: 1) The Boolean value "True" might be the same as a nonzero integer, or might not; it would make sense to use inequality comparisons with zero, MAYBE, but not with 1. So I don't particularly care what the language does with "1 < True", because it's not something that I would normally do. 2) "1"+1, in any high level language with an actual string type, I would expect to produce "11". It makes the most sense this way; having it return 2 means there's a special case where the string happens to look like a number - meaning that " 1"+1 is different from "1"+1. That just feels wrong to me... but I'm fully aware that many other people will disagree. Yep, it's pretty subjective. > On 12/17/2011 22:05, Chris Angelico wrote: >> Not quite; 1 + "one" will be "ne", which might happen to be at memory >> location 2. The data type is going to be char* (or, in a modern >> compiler, const char*), not int. > I'm not quite sure I'd say that it could be 2, exactly, but I definitely > disagree with this... after running 'int x = 5, *p = &x' would you say > that "p is 5"? (Assume &x != 5.) 1+"one" *points to* "ne", but it's > still a pointer. Point. I stand corrected. I tend to think of a char* as "being" the string, even though technically it only points to the beginning of it; it's the nearest thing C has to a string type. (To be honest, it's still a lot better than many high level languages' string types for certain common operations - eg trimming leading whitespace is pretty efficient on a PSZ.) In your example, p would be some integer value that is the pointer, but *p is 5. However, there's really no syntax in C to say what the "string value" is. ChrisA From rosuav at gmail.com Sun Dec 18 00:21:00 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 16:21:00 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: On Sun, Dec 18, 2011 at 3:52 PM, buck wrote: > The last one looks decorator-ish, but maybe that's proper. The implementation of this would be quite decorator-like: take the "normal" value of x, pass it through the indicated function, assign that value back to x. > > Try these on for size. > > ? ? head, @tuple tail = sequence > ? ? def foo(@list args, @dict kwargs): pass > ? ? foo(@args, @kwargs) That's reasonably clean as a concept, but it's not really quite the same. None of these examples is the way a decorator works; each of them requires a fundamental change to the way Python handles the rest of the statement. head, @tuple tail = sequence -- Does this mean "take the second element of a two-element sequence, pass it through tuple(), and store the result in tail"? Because, while that might be useful, and would make perfect sense as a decorator, it's insufficient as a replacement for current "*tail" syntax. ChrisA From edriscoll at wisc.edu Sun Dec 18 00:33:27 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 23:33:27 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: <4EED7B27.6000208@wisc.edu> On 12/17/2011 22:52, buck wrote: > Try these on for size. > > head, @tuple tail = sequence > def foo(@list args, @dict kwargs): pass > foo(@args, @kwargs) > > For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. > I like this idea much more than the original one. In addition to the arguments buck puts forth, which I find compelling, I have one more: you go to great length to say "this isn't really type checking in any sense" (which is true)... but then you go forth and pick a syntax that looks almost exactly like how you name types in many languages! (In fact, except for the fact that it's inline, the 'object :: type' syntax is *exactly* how you name types in Haskell.) buck's syntax still has some of the feel of "I wonder if this is type checking" to a newbie, but much much less IMO. I have a bigger objection with the general idea, however.It seems very strange that you should have to specify types to use it. If the */** syntax were removed, that would make the proposed syntax very very unusual for Python. I could be missing something, but I can think of any other place where you have to name a type except where the type is an integral part of what you're trying to do. (I would not say that choosing between tuples and lists are an integral part of dealing with vararg functions.) If */** were to stick around, I could see 99% of users continuing to use them. And then what has the new syntax achieved? You can fix this if you don't require the types and just allow the user to say "def foo(@args)" and "foo(@args)". Except... that's starting to look pretty familiar... (Not to mention if you just omit the type from the examples above you need another way to distinguish between args and kwargs.) I have one more suggestion. I do have one more thing to point out, which is that currently the Python vararg syntax is very difficult to Google for. In the first pages of the four searches matching "python (function)? (star | asterisk)", there was just one relevant hit on python.org which wasn't a bug report. I certainly remember having a small amount of difficulty figuring out what the heck * and ** did the first time I encountered them. This would suggest perhaps some keywords might be called for instead of operators. In the grand scheme of things the argument packing and unpacking are not *all* that common, so I don't think the syntactic burden would be immense. The bigger issue, of course, would be picking good words. This also helps with the issue above. Let's say we'll use 'varargs' and 'kwargs', though the latter too well-ingrained in code to steal. (I don't want to get too much into the debate over *what* word to choose. Also these don't match the 'head, *tail = l' syntax very well.) Then we could say: def foo(varargs l, kwargs d): bar(varargs l, kwargs d) and varargs would be equivalent to * and kwargs would be equivalent to **. But then you could also say def foo(varargs(list) l, kwargs(dict) d) Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From edriscoll at wisc.edu Sun Dec 18 00:54:35 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 23:54:35 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4EED7B27.6000208@wisc.edu> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4EED7B27.6000208@wisc.edu> Message-ID: <4EED801B.2070208@wisc.edu> On 12/17/2011 23:33, Evan Driscoll wrote: > I do have one more thing to point out, which is that currently the > Python vararg syntax is very difficult to Google for. In the first pages > of the four searches matching "python (function)? (star | asterisk)", > there was just one relevant hit on python.org which wasn't a bug report. Though in the interest of full disclosure, and I should have said this before, the first hit for all of those searches except "python star" *is* relevant and reasonably helpful; just from someone's blog post instead of "from the horse's mouth", so to speak. Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From steve+comp.lang.python at pearwood.info Sun Dec 18 02:31:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 07:31:21 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 23:33:27 -0600, Evan Driscoll wrote: > I do have one more thing to point out, which is that currently the > Python vararg syntax is very difficult to Google for. You're coming in late to the conversation, but that was literally the second thing pointed out by the original poster: On Sun, 11 Dec 2011 11:49:23 +0100, Eelco Hoogendoorn wrote: > Throwing an idea for a PEP out there: > > It strikes me that the def func(*args, **kwargs) syntax is rather > unpytonic. It certainly did not have that 'for line in file' > pythonic obviousness for me as a beginner. Plus, asterikses are > impossible to google for, so finding out what exactly they do more > or less forces you to write a forum post about it. And rebutted. Modesty[1] prevents me from quoting myself, but here are some links to searches: http://duckduckgo.com/?q=python+asterisk http://duckduckgo.com/?q=python+* My normal first place to look for something is Wikipedia. Enjoy it before SOPA kills it. http://en.wikipedia.org/wiki/Asterisk#Programming_languages > In the first pages > of the four searches matching "python (function)? (star | asterisk)", Your search looks overly complicated to me. I'm not an expert on Google's syntax, but if you search for "python, optionally with function", isn't that the same as just searching for "python" since it will return hits either with or without "function"? > I certainly remember having a small amount of difficulty figuring out > what the heck * and ** did the first time I encountered them. Answering that sort of question is what the interactive interpreter excels at. Suppose you see a function declared with *args as an argument, and you have no idea what it means. Try it and find out! py> def spam(*args): ... print(args, type(args)) ... py> spam(42) ((42,), ) py> spam(42, 23) ((42, 23), ) Never underestimate the power of Python's introspection tools, especially the two simplest ones: print and type. Often you will learn more in 10 minutes experimentation than in an hour googling. [1] Eh, who am I fooling? -- Steven From no.email at nospam.invalid Sun Dec 18 02:55:13 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Sat, 17 Dec 2011 23:55:13 -0800 Subject: Debugging a difficult refcount issue. References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: <7xpqfm48m6.fsf@ruckus.brouhaha.com> buck writes: > I tried to pinpoint this intermediate allocation with a similar > PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. Did you try a gdb watchpoint? From steve+comp.lang.python at pearwood.info Sun Dec 18 03:36:12 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 08:36:12 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 23:33:27 -0600, Evan Driscoll wrote: > This would suggest perhaps some keywords might be called for instead of > operators. The barrier to new keywords in Python is very high. Not going to happen for something that already has perfectly good syntax already familiar to Python and Ruby programmers. Might else well try to get C and Java to stop using "..." (ellipses). > In the grand scheme of things the argument packing and > unpacking are not *all* that common, so I don't think the syntactic > burden would be immense. The burden is that you complicate the compiler and reduce the pool of useful names available to the programmer. To be useful, the keywords need to be short, meaningful and memorable -- which means they're already used in code, which you will break needlessly. With operators that otherwise would be illegal, the programmer doesn't need to learn about varargs until they need to. With keywords, the programmer needs to learn "you can't use varargs or kwargs as variable names" practically from day 1. > But then you could also say > def foo(varargs(list) l, kwargs(dict) d) So you're not just adding keywords, you're adding new syntax: something which looks like a function call, but isn't a function call. Another feature which the programmer has to learn just to be able to read Python source code -- and one which almost certainly is going to clash with function annotations as people start using them. I'm going to pose the same question to you I have already posed to Eelco: in your proposal, what happens if the caller has shadowed the list built- in? Does argument l become a built-in list, or does it become whatever type list is currently bound to? Some more questions: Can varargs accepted arbitrary callables, or only a fixed set of known types? How does this differ from what can be done with annotations? -- Steven From rosuav at gmail.com Sun Dec 18 03:43:09 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 19:43:09 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 18, 2011 at 6:31 PM, Steven D'Aprano wrote: > My normal first place to look for something is Wikipedia. Enjoy it before > SOPA kills it. > > http://en.wikipedia.org/wiki/Asterisk#Programming_languages I would never expect to find this sort of thing in the article on the asterisk; maybe I'd look on the language's article, but more likely I would be looking for info on the language's own web site. > On Sat, 17 Dec 2011 23:33:27 -0600, Evan Driscoll wrote: >> In the first pages >> of the four searches matching "python (function)? (star | asterisk)", > > Your search looks overly complicated to me. I understand this to mean that he did four searches: * python star * python function star * python asterisk * python function asterisk > Never underestimate the power of Python's introspection tools, especially > the two simplest ones: print and type. Often you will learn more in 10 > minutes experimentation than in an hour googling. +1 QOTW. I refer to this as "IIDPIO debugging" (Eyed Pio) - If In Doubt, Print It Out. ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 18 03:46:57 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 08:46:57 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eeda881$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 21:03:01 -0600, Evan Driscoll wrote: > Something like ML or Haskell, which does not even allow integer to > double promotions, is very strong typing. Something like Java, which > allows some arithmetic conversion and also automatic stringification (a > la "1" + 1) is somewhere in the middle of the spectrum. Personally I'd > put Python even weaker on account of things such as '[1,2]*2' and '1 < > True' being allowed, They are not examples of weak typing. The first is an example of operator overloading, the second is a side-effect of a compromise made for backwards compatibility. If the first example were written: [1, 2].repeat(2) I expect you'd be perfectly happy with it as an unexceptional example of a list method: it takes an integer count, and returns a new list consisting of the elements of the original list repeated twice. If it were written: [1, 2].__mul__(2) you'd probably raise an eyebrow at the ugliness of the method name, but you would be okay with the concept. Well, that's exactly what it is in Python: the list __mul__ method performs sequence multiplication (repeating the contents), which overloads the * operator. No automatic type conversions needed, so not an example of weak typing. As for comparisons between ints and True or False, that's not weak typing either, because True and False *are* ints: bool is a subclass of int. This was done purely for historical reasons: originally Python didn't have a bool type, and you used 0 and 1 by convention for boolean values. When it was decided to add a bool type, the least disruptive way to do this was to define it as a subclass of int. -- Steven From gustavo.cordova at gmail.com Sun Dec 18 03:52:09 2011 From: gustavo.cordova at gmail.com (RangerElf) Date: Sun, 18 Dec 2011 00:52:09 -0800 (PST) Subject: Make a small function thread safe In-Reply-To: <4eebfbff$0$1679$742ec2ed@news.sonic.net> References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> <4eebfbff$0$1679$742ec2ed@news.sonic.net> Message-ID: <17238811.518.1324198329306.JavaMail.geo-discussion-forums@yqio4> Which is why the original .acquire() ... .release() idiom was wrong, this would better express the intent: try: lock.acquire() shared_container.append(...) finally: lock.release() But like everyone mentions, the with statement takes care of all that in a more readable and compact fashion. From kevin.p.dwyer at gmail.com Sun Dec 18 03:55:49 2011 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Sun, 18 Dec 2011 08:55:49 +0000 Subject: Bug in multiprocessing.reduction? References: Message-ID: Ya?ar Arabac? wrote: > You can see my all code below, theoritically that code should work I > guess. But I keep getting this error: > [SUBWARNING/MainProcess] thread for sharing handles raised exception : > ------------------------------------------------------------------------------- > Traceback (most recent call last): > File "/usr/lib/python2.7/multiprocessing/reduction.py", line 127, in > _serve send_handle(conn, handle_wanted, destination_pid) > File "/usr/lib/python2.7/multiprocessing/reduction.py", line 80, in > send_handle > _multiprocessing.sendfd(conn.fileno(), handle) > OSError: [Errno 9] Bad file descriptor > ------------------------------------------------------------------------------- > > Do you see an error in my side, or is this a bug in Python? > Hello, I don't know much about the multiprocessing module, so I won't speculate about bugs ;) however I can tell you that your code works with error on my machine: kev at pluto:~> python mtest.py [DEBUG/MainProcess] created semlock with handle 140252275732480 [DEBUG/MainProcess] created semlock with handle 140252275728384 [DEBUG/MainProcess] created semlock with handle 140252275724288 [DEBUG/MainProcess] Queue._after_fork() [DEBUG/Process-1] Queue._after_fork() [INFO/Process-1] child process calling self.run() [DEBUG/Process-2] Queue._after_fork() [INFO/Process-2] child process calling self.run() [DEBUG/Process-3] Queue._after_fork() [INFO/Process-3] child process calling self.run() [DEBUG/Process-4] Queue._after_fork() [INFO/Process-4] child process calling self.run() [DEBUG/Process-5] Queue._after_fork() [INFO/Process-5] child process calling self.run() [DEBUG/MainProcess] starting listener and thread for sending handles [INFO/MainProcess] created temp directory /tmp/pymp-J3UxCe [DEBUG/MainProcess] Queue._start_thread() [DEBUG/MainProcess] doing self._thread.start() [DEBUG/MainProcess] starting thread to feed data to pipe [DEBUG/MainProcess] ... done self._thread.start() Here "Here" was added to your print line by me. Otherwise I ran your code as is, and sent the string "Hello world" to port 9090 from another console. Hope that helps, Kev From vs at it.uu.se Sun Dec 18 05:31:33 2011 From: vs at it.uu.se (Virgil Stokes) Date: Sun, 18 Dec 2011 11:31:33 +0100 Subject: Module msvcrt for Python Message-ID: <4EEDC105.9010506@it.uu.se> I am running Python 2.6.6 on a Windows Vista platform and for some reason the module msvcrt is not present. How can I install the msvcrt module in my Python 2.6.6? God Jul :-) From nukeymusic at gmail.com Sun Dec 18 05:37:57 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 02:37:57 -0800 (PST) Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> Message-ID: <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> On 17 dec, 12:20, "G?nther Dietrich" wrote: > nukeymusic wrote: > >I'm trying to calculate the difference in seconds between two > > [...] > > >>> import datetime > >>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", "%b-%d-%H:%M:%S") > >>> date2 = datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") > >>> delta = date2 - date1 > >>> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000) > > For very big time differences you should consider to use the Decimal > arithmetics (standard module Decimal) instead of integer arithmetics > for the last line. > If you are sure, that you don't use fractional seconds, you can omit > the part with 'delta.microseconds'. > > Best regards, > > G?nther That can very much G?nther, this helped me a lot further, I'm only struggling with one more problem to finish my first python-program. Could you tell me why I can't write to the outputfile as I do in the code below:? #!/usr/bin/python #version 16/12/2011 #Example of testfile #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 import datetime f = open('testfile','r') g = open('outputfile','w') #get line 1 from input file: line1=f.readline() #get first element in line 1: date1=line1.rsplit()[0] #convert first element tot structured date time struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") for line in f: temp=line.rsplit() delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000) temp[0]=delta_seconds #the following line is wrong, but I don't know how to fix it: g.write(temp) #Close files f.close() g.close() thanks in advance nukey From nukeymusic at gmail.com Sun Dec 18 06:00:26 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 03:00:26 -0800 (PST) Subject: how to run python-script from the python promt? [absolute newbie] Message-ID: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> How can I load a python-script after starting python in the interactive mode? I tried with >>>load 'myscript.py' >>>myscript.py >>>myscript but none of these works, so the only way I could work further until now was copy/paste line per line of my python-script to the interactive mode prompt I do know how to run the script non-interactively, but what I want to do is adding lines to the code I have written thus far in interactive mode. thanks in advance nukey From rosuav at gmail.com Sun Dec 18 06:29:49 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 22:29:49 +1100 Subject: how to run python-script from the python promt? [absolute newbie] In-Reply-To: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: On Sun, Dec 18, 2011 at 10:00 PM, nukeymusic wrote: > How can I load a python-script after starting python in the > interactive mode? > I tried with >>>>load 'myscript.py' >>>>myscript.py >>>>myscript > > but none of these works, so the only way I could work further until > now was copy/paste line per line of my python-script to the > interactive mode prompt The easiest way to load a file is to import it. However, this is not quite identical to loading the script into the current session. If your script mainly defines functions, you can either: import myscript or from myscript import * and it'll do more or less what you expect; however, it will execute in its own module context, so globals from your current session won't be available to it. Tip for pasting: Indent every line of your code at least one space/tab, prefix it with "if True:", and then you can paste it all at once. ChrisA From __peter__ at web.de Sun Dec 18 06:43:44 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 18 Dec 2011 12:43:44 +0100 Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> Message-ID: nukeymusic wrote: > On 17 dec, 12:20, "G?nther Dietrich" wrote: >> nukeymusic wrote: >> >I'm trying to calculate the difference in seconds between two >> >> [...] >> >> >>> import datetime >> >>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", >> >>> "%b-%d-%H:%M:%S") date2 = >> >>> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta >> >>> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + >> >>> delta.seconds + ((delta.microseconds + 500000) / 1000000) >> >> For very big time differences you should consider to use the Decimal >> arithmetics (standard module Decimal) instead of integer arithmetics >> for the last line. >> If you are sure, that you don't use fractional seconds, you can omit >> the part with 'delta.microseconds'. >> >> Best regards, >> >> G?nther > That can very much G?nther, this helped me a lot further, I'm only > struggling with one more problem to finish my first python-program. > Could you > tell me why I can't write to the outputfile as I do in the code > below:? > #!/usr/bin/python > #version 16/12/2011 > #Example of testfile > #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 > #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 > import datetime > f = open('testfile','r') > g = open('outputfile','w') > #get line 1 from input file: > line1=f.readline() > #get first element in line 1: > date1=line1.rsplit()[0] > #convert first element tot structured date time > struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > for line in f: > temp=line.rsplit() > delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- > datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + > ((delta.microseconds + 500000) / 1000000) > temp[0]=delta_seconds > #the following line is wrong, but I don't know how to fix it: > g.write(temp) > #Close files > f.close() > g.close() The write() method only accepts strings; you have to convert the temp list to a string before passing it on. The minimal change: for line in f: temp = line.rsplit() delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") -datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")) delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000)) temp[0] = str(delta_seconds) g.write(" ".join(temp) + "\n") Other observations: - you are repeating calculations in the loop that you can do (and did) outside. - use four-space indent for better readability - there's no need to use rsplit(); use split() After a few other modifications: import datetime def parse_line(line): date, rest = line.split(None, 1) date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") return date, rest with open('testfile','r') as f: with open('outputfile','w') as g: first_date, first_rest = parse_line(next(f)) for line in f: cur_date, rest = parse_line(line) delta = cur_date - first_date delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000)) g.write("%s %s" % (delta_seconds, rest)) From kevin.p.dwyer at gmail.com Sun Dec 18 06:45:01 2011 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Sun, 18 Dec 2011 11:45:01 +0000 Subject: how to run python-script from the python promt? [absolute newbie] References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: nukeymusic wrote: > How can I load a python-script after starting python in the > interactive mode? > I tried with >>>>load 'myscript.py' >>>>myscript.py >>>>myscript > > but none of these works, so the only way I could work further until > now was copy/paste line per line of my python-script to the > interactive mode prompt > I do know how to run the script non-interactively, but what I want to > do is adding lines to the code I have written thus far in interactive > mode. > > thanks in advance > nukey Hello, You can make the code in your script available to the interpreter by typing import myscript (assuming that you are running the interpreter in the same directory that contains myscript.py) You can access functions, classes and other top-level objects in your script by prefixing their names with "myscript" and a dot (".") e.g. myscript.myfunc, myscript.MyClass, myscript.myvar You can't really edit your script in the interpreter, but you can edit and save in a text editor and then type reload(myscript) in the interpreter to refresh its version of the myscript code. N.B. when you import/reload your script the interpreter will immediately execute any code that is not enclosed in a function or class definition. Cheers, Kev From vs at it.uu.se Sun Dec 18 06:52:51 2011 From: vs at it.uu.se (Virgil Stokes) Date: Sun, 18 Dec 2011 12:52:51 +0100 Subject: Module msvcrt for Python In-Reply-To: <4EEDC105.9010506@it.uu.se> References: <4EEDC105.9010506@it.uu.se> Message-ID: <4EEDD413.2040302@it.uu.se> On 18-Dec-2011 11:31, Virgil Stokes wrote: > I am running Python 2.6.6 on a Windows Vista platform and for some reason the > module msvcrt is not present. > > How can I install the msvcrt module in my Python 2.6.6? > > God Jul :-) I found the problem! My code was using Python 2.5 (inside cygwin) by default and there it was unable to import msvcrt. But, when I ran the same code in my Python 2.6 installation it worked as it should (msvcrt module was imported). Sorry for the "noise". Best :-) From lie.1296 at gmail.com Sun Dec 18 07:39:58 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 18 Dec 2011 23:39:58 +1100 Subject: how to run python-script from the python promt? [absolute newbie] In-Reply-To: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: On 12/18/2011 10:00 PM, nukeymusic wrote: > How can I load a python-script after starting python in the > interactive mode? > I tried with >>>> load 'myscript.py' >>>> myscript.py >>>> myscript > > but none of these works, so the only way I could work further until > now was copy/paste line per line of my python-script to the > interactive mode prompt > I do know how to run the script non-interactively, but what I want to > do is adding lines to the code I have written thus far in interactive > mode. > > thanks in advance > nukey The normal python shell doesn't directly support doing that, although there are several workaround with (ab)using the 'import' statement, it had several subtleties with how module are cached. Try the ipython shell; in ipython you can load a file into the current interpreter session using the %run magic command. From lie.1296 at gmail.com Sun Dec 18 07:58:21 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 18 Dec 2011 23:58:21 +1100 Subject: calculate difference between two timestamps [newbie] In-Reply-To: References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> Message-ID: On 12/18/2011 10:43 PM, Peter Otten wrote: > nukeymusic wrote: > >> On 17 dec, 12:20, "G?nther Dietrich" wrote: >>> nukeymusic wrote: >>>> I'm trying to calculate the difference in seconds between two >>> >>> [...] >>> >>>>>> import datetime >>>>>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", >>>>>> "%b-%d-%H:%M:%S") date2 = >>>>>> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta >>>>>> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + >>>>>> delta.seconds + ((delta.microseconds + 500000) / 1000000) >>> >>> For very big time differences you should consider to use the Decimal >>> arithmetics (standard module Decimal) instead of integer arithmetics >>> for the last line. >>> If you are sure, that you don't use fractional seconds, you can omit >>> the part with 'delta.microseconds'. >>> >>> Best regards, >>> >>> G?nther >> That can very much G?nther, this helped me a lot further, I'm only >> struggling with one more problem to finish my first python-program. >> Could you >> tell me why I can't write to the outputfile as I do in the code >> below:? >> #!/usr/bin/python >> #version 16/12/2011 >> #Example of testfile >> #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 >> #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 >> #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 >> import datetime >> f = open('testfile','r') >> g = open('outputfile','w') >> #get line 1 from input file: >> line1=f.readline() >> #get first element in line 1: >> date1=line1.rsplit()[0] >> #convert first element tot structured date time >> struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") >> for line in f: >> temp=line.rsplit() >> delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- >> datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") >> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + >> ((delta.microseconds + 500000) / 1000000) >> temp[0]=delta_seconds >> #the following line is wrong, but I don't know how to fix it: >> g.write(temp) >> #Close files >> f.close() >> g.close() > > The write() method only accepts strings; you have to convert the temp list > to a string before passing it on. The minimal change: > > for line in f: > temp = line.rsplit() > delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") > -datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")) > delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > + ((delta.microseconds + 500000) / 1000000)) > temp[0] = str(delta_seconds) > g.write(" ".join(temp) + "\n") > > Other observations: > > - you are repeating calculations in the loop that you can do (and did) > outside. > > - use four-space indent for better readability > > - there's no need to use rsplit(); use split() > > After a few other modifications: > > import datetime > > def parse_line(line): > date, rest = line.split(None, 1) > date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > return date, rest > > with open('testfile','r') as f: > with open('outputfile','w') as g: > first_date, first_rest = parse_line(next(f)) > for line in f: > cur_date, rest = parse_line(line) > delta = cur_date - first_date > delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > + ((delta.microseconds + 500000) / 1000000)) > g.write("%s %s" % (delta_seconds, rest)) > minor improvement, you can do: with open('testfile','r') as f, open('outputfile','w') as g: ... instead of the nested with-block. Also, you can use `delta.total_seconds()` instead of `delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000))` Therefore (untested): import datetime def parse_line(line): date, rest = line.split(None, 1) date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") return date, rest with open('testfile','r') as f, open('outputfile','w') as g: first_date, first_rest = parse_line(next(f)) for line in f: cur_date, rest = parse_line(line) delta = cur_date - first_date g.write("%s %s" % (int(round(delta.total_seconds())), rest)) From nukeymusic at gmail.com Sun Dec 18 08:16:49 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 05:16:49 -0800 (PST) Subject: how to run python-script from the python promt? [absolute newbie] References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: On 18 dec, 13:39, Lie Ryan wrote: > On 12/18/2011 10:00 PM, nukeymusic wrote: > > > > > > > > > > > How can I load a python-script after starting python in the > > interactive mode? > > I tried with > >>>> load 'myscript.py' > >>>> myscript.py > >>>> myscript > > > but none of these works, so the only way I could work further until > > now was copy/paste line per line of my python-script to the > > interactive mode prompt > > I do know how to run the script non-interactively, but what I want to > > do is adding lines to the code I have written thus far in interactive > > mode. > > > thanks in advance > > nukey > > The normal python shell doesn't directly support doing that, although > there are several workaround with (ab)using the 'import' statement, it > had several subtleties with how module are cached. Try the ipython > shell; in ipython you can load a file into the current interpreter > session using the %run magic command. I guess you mean the following command? %run 'myscript.py' is this correct? thanks nukey From roy at panix.com Sun Dec 18 08:58:28 2011 From: roy at panix.com (Roy Smith) Date: Sun, 18 Dec 2011 08:58:28 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Chris Angelico wrote: > > Never underestimate the power of Python's introspection tools, especially > > the two simplest ones: print and type. Often you will learn more in 10 > > minutes experimentation than in an hour googling. > > +1 QOTW. I refer to this as "IIDPIO debugging" (Eyed Pio) - If In > Doubt, Print It Out. I always knew it by the name "Ask the computer". As in, "WTF is foo? Let's ask the computer!". In addition to print and type, I'm a big fan of dir(). Often, I know an object has a method to do what I want, but I can't remember the name. For example, the other day, I was using a set (which I don't use very often). I needed the method to remove an item from the set. Faster than finding the right place in the docs, I just fired up an interpreter and typed dir(set()) at it. From nukeymusic at gmail.com Sun Dec 18 09:03:18 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 06:03:18 -0800 (PST) Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> Message-ID: <8d1ec984-9786-4b0c-924b-01f661c1374c@q11g2000vbq.googlegroups.com> On 18 dec, 13:58, Lie Ryan wrote: > On 12/18/2011 10:43 PM, Peter Otten wrote: > > > > > > > > > > > nukeymusic wrote: > > >> On 17 dec, 12:20, "G?nther Dietrich" ?wrote: > >>> nukeymusic ?wrote: > >>>> I'm trying to calculate the difference in seconds between two > > >>> [...] > > >>>>>> import datetime > >>>>>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", > >>>>>> "%b-%d-%H:%M:%S") date2 = > >>>>>> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta > >>>>>> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + > >>>>>> delta.seconds + ((delta.microseconds + 500000) / 1000000) > > >>> For very big time differences you should consider to use the Decimal > >>> arithmetics (standard module Decimal) instead of integer arithmetics > >>> for the last line. > >>> If you are sure, that you don't use fractional seconds, you can omit > >>> the part with 'delta.microseconds'. > > >>> Best regards, > > >>> G?nther > >> That can very much G?nther, this helped me a lot further, I'm only > >> struggling with one more problem to finish my first python-program. > >> Could you > >> tell me why I can't write to the outputfile as I do in the code > >> below:? > >> #!/usr/bin/python > >> #version 16/12/2011 > >> #Example of testfile > >> #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > >> #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 > >> #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 > >> import datetime > >> f = open('testfile','r') > >> g = open('outputfile','w') > >> #get line 1 from input file: > >> line1=f.readline() > >> #get first element in line 1: > >> date1=line1.rsplit()[0] > >> #convert first element tot structured date time > >> struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > >> for line in f: > >> ? temp=line.rsplit() > >> ? delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- > >> datetime.datetime.strptime(date1, ?"%b-%d-%H:%M:%S") > >> ? delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + > >> ((delta.microseconds + 500000) / 1000000) > >> ? temp[0]=delta_seconds > >> #the following line is wrong, but I don't know how to fix it: > >> ? g.write(temp) > >> #Close files > >> f.close() > >> g.close() > > > The write() method only accepts strings; you have to convert the temp list > > to a string before passing it on. The minimal change: > > > for line in f: > > ? ? ? temp = line.rsplit() > > ? ? ? delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") > > ? ? ? ? ? ? ? ?-datetime.datetime.strptime(date1, ?"%b-%d-%H:%M:%S")) > > ? ? ? delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > ? ? ? ? ? ? ? ? ? ? ? ?+ ((delta.microseconds + 500000) / 1000000)) > > ? ? ? temp[0] = str(delta_seconds) > > ? ? ? g.write(" ".join(temp) + "\n") > > > Other observations: > > > - you are repeating calculations in the loop that you can do (and did) > > outside. > > > - use four-space indent for better readability > > > - there's no need to use rsplit(); use split() > > > After a few other modifications: > > > import datetime > > > def parse_line(line): > > ? ? ?date, rest = line.split(None, 1) > > ? ? ?date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > > ? ? ?return date, rest > > > with open('testfile','r') as f: > > ? ? ?with open('outputfile','w') as g: > > ? ? ? ? ?first_date, first_rest = parse_line(next(f)) > > ? ? ? ? ?for line in f: > > ? ? ? ? ? ? ?cur_date, rest = parse_line(line) > > ? ? ? ? ? ? ?delta = cur_date - first_date > > ? ? ? ? ? ? ?delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? + ((delta.microseconds + 500000) / 1000000)) > > ? ? ? ? ? ? ?g.write("%s %s" % (delta_seconds, rest)) > > minor improvement, you can do: > > with open('testfile','r') as f, open('outputfile','w') as g: > ? ? ?... > > instead of the nested with-block. > > Also, you can use `delta.total_seconds()` instead of `delta_seconds = > ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + > 500000) / 1000000))` > > Therefore (untested): > > import datetime > > def parse_line(line): > ? ? ?date, rest = line.split(None, 1) > ? ? ?date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > ? ? ?return date, rest > > with open('testfile','r') as f, open('outputfile','w') as g: > ? ? ?first_date, first_rest = parse_line(next(f)) > ? ? ?for line in f: > ? ? ? ? ?cur_date, rest = parse_line(line) > ? ? ? ? ?delta = cur_date - first_date > ? ? ? ? ?g.write("%s %s" % (int(round(delta.total_seconds())), rest)) thanks and also thanks to all the others who were so kind to help me out with my first python-script. I tested your alternatives and they work, the only a minor inconvenience is that the first line of the inputfile gets lost i.e. the first timestamp should become zero (seconds) best regards, nukey From rosuav at gmail.com Sun Dec 18 09:06:34 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Dec 2011 01:06:34 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 19, 2011 at 12:58 AM, Roy Smith wrote: > In addition to print and type, I'm a big fan of dir(). ?Often, I know an > object has a method to do what I want, but I can't remember the name. > For example, the other day, I was using a set (which I don't use very > often). ?I needed the method to remove an item from the set. ?Faster > than finding the right place in the docs, I just fired up an interpreter > and typed dir(set()) at it. That's excellent when all you need is the name. I usually have IDLE running (all the time - which sometimes produces oddities after I experiment with monkey-patching some module or other, and then oddly enough, things don't work properly!!), and will snap off "help(foo)" for any foo. Unfortunately help() is at times unhelpful, and even at its best it's very spammy. There's no one best solution; the successful programmer will usually have a large toolset at his disposal. ChrisA From hoogendoorn.eelco at gmail.com Sun Dec 18 09:13:37 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 18 Dec 2011 06:13:37 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9c808e14-fde9-49e7-b051-84ff45d663d5@i8g2000vbh.googlegroups.com> On Dec 18, 1:59?am, Steven D'Aprano wrote: > On Sat, 17 Dec 2011 06:38:22 -0800, Eelco wrote: > > Type constraints: > > > In case the asterisk is not used to signal unpacking, but rather to > > signal packing, its semantics is essentially that of a type constraint. > > "Type constraint" normally refers to type restrictions on *input*: it is > a restriction on what types are accepted. When it refers to output, it is > not normally a restriction, therefore "constraint" is inappropriate. > Instead it is normally described as a coercion, cast or conversion. > Automatic type conversions are the opposite of a constraint: it is a > loosening of restrictions. "I don't have to use a list, I can use any > sequence or iterator". Casts or conversions are a runtime concept; im talking about declarations. That seems to be the source of your confusion. > In iterator unpacking, it is the *output* which is a list, not a > restriction on input: in the statement: > > head, *tail = sequence > > tail may not exist before the assignment, and so describing this as a > constraint on the type of tail is completely inappropriate. Yes, the variable tail is being (re)declared here. Thats exactly why I call it a type constraint. Im not sure what the CS books have to say on the matter, I guess this use of the term entered my lexicon through the C# developer team. Either way, you seem to be the only one who does not grok my intended meaning, so I suggest you try reading it again. > > The statement: > > > ? ? head, tail = sequence > > > Signifies regular unpacking. However, if we add an asterisk, as in: > > > ? ? head, *tail = sequence > > > We demand that tail not be just any python object, but rather a list. > > We don't demand anything, any more than when we say: > > for x in range(1, 100): > > we "demand" that x is not just any python object, but rather an int. > > Rather, we accept what we're given: in case of range and the for loop, we > are given an int. In the case of extended tuple unpacking, we are given a > list. for x in range is syntactic sugar for a series of assignments to x; x is an unconstrained variable that will indeed take anything it gets; the semantics of what comes after 'x' does in no way depend on x itself. head, tail = l and head, *tail = l mean something completely different, and the only difference is a constraint placed on tail, which forces the semantics to be different; the righthand side, or what is to be assigned, is identical. Of course one can just regard it as syntactic sugar for head, tail = unpackheadandtailaslist(l); but the syntactic sugar achieves that same end through a type constraint on tail. Really. > You are jumping to conclusions about implementation details which aren't > supported by the visible behaviour. What evidence do you have that > iterator unpacking creates a tuple first and then converts it to a list? You are jumping to conclusions about my opinion which aren't supported by my visible behaviour. What evidence do you have that I ever even said any such thing? > > The aim of this PEP, is that this type-constraint syntax is expanded > > upon. We should be careful here to distinguish with providing optional > > type constraints throughout python as a whole; this is not our aim. > > Iterator unpacking is no more about type constraints than is len(). Because you wish to keep nitpicking about my usage of the term 'type constraint' (even though you have not introduced an alternative term yourself), or because you actually disagree with the content of my message? From hoogendoorn.eelco at gmail.com Sun Dec 18 09:23:51 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 18 Dec 2011 06:23:51 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: On Dec 18, 5:52?am, buck wrote: > I like the spirit of this. Let's look at your examples. Glad to see an actual on-topic reply; thanks. > > Examples of use: > > ? ? head, tail::tuple = ::sequence > > ? ? def foo(args::list, kwargs::dict): pass > > ? ? foo(::args, ::kwargs) > > My initial reaction was "nonono!", but this is simply because of the ugliness. The double-colon is very visually busy. I would have preferred the single colon, but its taken. But exactly this syntax is used in other languages (for what that is worth...) > I find that your second example is inconsistent with the others. If we say that the variable-name is always on the right-hand-side, we get: > > > ? ? def foo(list::args, dict::kwargs): pass > > This nicely mirrors other languages (such as in your C# example: ?"float foo") as well as the old python behavior (prefixing variables with */** to modify the assignment). The link in my OP has the BDFL discussing type constraints; he also prefers identifier:type. Many modern languages have something similar. How is my second example inconsistent? > As for the separator, let's examine the available ascii punctuation. Excluding valid variable characters, whitespace, and operators, we have: > > ! -- ok. > " -- can't use this. Would look like a string. > # -- no. Would looks like a comment. > $ -- ok. > ' -- no. Would look like a string. > ( -- no. Would look like a function. > ) -- no. Would look like ... bad syntax. > , -- no. Would indicate a separate item in the variable list. > . -- no. Would look like an attribute. > : -- ok, maybe. Seems confusing in a colon-terminated statement. > ; -- no, just no. > ? -- ok. > @ -- ok. > [ -- no. Would look like indexing. > ] -- no. > ` -- no. Would look like a string? > { -- too strange} -- too strange > > ~ -- ok. > > That leaves these. Which one looks least strange? > > float ! x = 1 > float $ x = 1 > float ? x = 1 > float @ x = 1 > > The last one looks decorator-ish, but maybe that's proper. The implementation of this would be quite decorator-like: take the "normal" value of x, pass it through the indicated function, assign that value back to x. I dont think thats too proper an analogy. The type constraint does not just coerce the content that is bound to the variable, it influences the semantics of the whole statement; so insofar there is a suggested relation with decorators, id rather not have it. > > Try these on for size. > > ? ? ?head, @tuple tail = sequence > ? ? ?def foo(@list args, @dict kwargs): pass > ? ? ?foo(@args, @kwargs) > > For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. Yes, maximum backwards compatibility would be great. From nukeymusic at gmail.com Sun Dec 18 09:26:25 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 06:26:25 -0800 (PST) Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> Message-ID: <35ff07d2-540c-483e-b66b-edfa66540bc6@z17g2000vbe.googlegroups.com> On 18 dec, 13:58, Lie Ryan wrote: > On 12/18/2011 10:43 PM, Peter Otten wrote: > > > > > > > > > > > nukeymusic wrote: > > >> On 17 dec, 12:20, "G?nther Dietrich" ?wrote: > >>> nukeymusic ?wrote: > >>>> I'm trying to calculate the difference in seconds between two > > >>> [...] > > >>>>>> import datetime > >>>>>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", > >>>>>> "%b-%d-%H:%M:%S") date2 = > >>>>>> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta > >>>>>> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + > >>>>>> delta.seconds + ((delta.microseconds + 500000) / 1000000) > > >>> For very big time differences you should consider to use the Decimal > >>> arithmetics (standard module Decimal) instead of integer arithmetics > >>> for the last line. > >>> If you are sure, that you don't use fractional seconds, you can omit > >>> the part with 'delta.microseconds'. > > >>> Best regards, > > >>> G?nther > >> That can very much G?nther, this helped me a lot further, I'm only > >> struggling with one more problem to finish my first python-program. > >> Could you > >> tell me why I can't write to the outputfile as I do in the code > >> below:? > >> #!/usr/bin/python > >> #version 16/12/2011 > >> #Example of testfile > >> #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > >> #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 > >> #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 > >> import datetime > >> f = open('testfile','r') > >> g = open('outputfile','w') > >> #get line 1 from input file: > >> line1=f.readline() > >> #get first element in line 1: > >> date1=line1.rsplit()[0] > >> #convert first element tot structured date time > >> struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > >> for line in f: > >> ? temp=line.rsplit() > >> ? delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- > >> datetime.datetime.strptime(date1, ?"%b-%d-%H:%M:%S") > >> ? delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + > >> ((delta.microseconds + 500000) / 1000000) > >> ? temp[0]=delta_seconds > >> #the following line is wrong, but I don't know how to fix it: > >> ? g.write(temp) > >> #Close files > >> f.close() > >> g.close() > > > The write() method only accepts strings; you have to convert the temp list > > to a string before passing it on. The minimal change: > > > for line in f: > > ? ? ? temp = line.rsplit() > > ? ? ? delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") > > ? ? ? ? ? ? ? ?-datetime.datetime.strptime(date1, ?"%b-%d-%H:%M:%S")) > > ? ? ? delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > ? ? ? ? ? ? ? ? ? ? ? ?+ ((delta.microseconds + 500000) / 1000000)) > > ? ? ? temp[0] = str(delta_seconds) > > ? ? ? g.write(" ".join(temp) + "\n") > > > Other observations: > > > - you are repeating calculations in the loop that you can do (and did) > > outside. > > > - use four-space indent for better readability > > > - there's no need to use rsplit(); use split() > > > After a few other modifications: > > > import datetime > > > def parse_line(line): > > ? ? ?date, rest = line.split(None, 1) > > ? ? ?date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > > ? ? ?return date, rest > > > with open('testfile','r') as f: > > ? ? ?with open('outputfile','w') as g: > > ? ? ? ? ?first_date, first_rest = parse_line(next(f)) > > ? ? ? ? ?for line in f: > > ? ? ? ? ? ? ?cur_date, rest = parse_line(line) > > ? ? ? ? ? ? ?delta = cur_date - first_date > > ? ? ? ? ? ? ?delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? + ((delta.microseconds + 500000) / 1000000)) > > ? ? ? ? ? ? ?g.write("%s %s" % (delta_seconds, rest)) > > minor improvement, you can do: > > with open('testfile','r') as f, open('outputfile','w') as g: > ? ? ?... > > instead of the nested with-block. > > Also, you can use `delta.total_seconds()` instead of `delta_seconds = > ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + > 500000) / 1000000))` > > Therefore (untested): > > import datetime > > def parse_line(line): > ? ? ?date, rest = line.split(None, 1) > ? ? ?date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > ? ? ?return date, rest > > with open('testfile','r') as f, open('outputfile','w') as g: > ? ? ?first_date, first_rest = parse_line(next(f)) > ? ? ?for line in f: > ? ? ? ? ?cur_date, rest = parse_line(line) > ? ? ? ? ?delta = cur_date - first_date > ? ? ? ? ?g.write("%s %s" % (int(round(delta.total_seconds())), rest)) thanks to you and all the others for helping me out with my first python-script. I tested your alternatives and they do work. The only minor inconvenience with the scripts is that the output-file misses the first line i.e. the first timestamp should be reset to zero (seconds), but as these log-files are very large, missing one measurement is not too bad best regards nukey From hoogendoorn.eelco at gmail.com Sun Dec 18 09:35:36 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 18 Dec 2011 06:35:36 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> On Dec 18, 6:33?am, Evan Driscoll wrote: > On 12/17/2011 22:52, buck wrote:> Try these on for size. > > > ? ? ?head, @tuple tail = sequence > > ? ? ?def foo(@list args, @dict kwargs): pass > > ? ? ?foo(@args, @kwargs) > > > For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. > > I like this idea much more than the original one. In addition to the > arguments buck puts forth, which I find compelling, I have one more: you > go to great length to say "this isn't really type checking in any sense" > (which is true)... but then you go forth and pick a syntax that looks > almost exactly like how you name types in many languages! (In fact, > except for the fact that it's inline, the 'object :: type' syntax is > *exactly* how you name types in Haskell.) No, its not type *checking*, its type *declaration*. I tried to go to great lengths to point that out, but it appears I did not do a very good job :). Type declaration is exactly what I want, and insofar this syntax has already found adoptation elsewhere, ill consider that a plus. > I have a bigger objection with the general idea, however.It seems very > strange that you should have to specify types to use it. If the */** > syntax were removed, that would make the proposed syntax very very > unusual for Python. I could be missing something, but I can think of any > other place where you have to name a type except where the type is an > integral part of what you're trying to do. (I would not say that > choosing between tuples and lists are an integral part of dealing with > vararg functions.) If */** were to stick around, I could see 99% of > users continuing to use them. And then what has the new syntax achieved? Good point; technically the askeriskes could be kept for backwards compatibility, but that would break 'there should only be one way to do it'. Indeed it would be unusual for python to have to explicitly name a type, but implicitly ** does very much the same. Its just a cryptic way of saying 'dict please'. > You can fix this if you don't require the types and just allow the user > to say "def foo(@args)" and "foo(@args)". Except... that's starting to > look pretty familiar... (Not to mention if you just omit the type from > the examples above you need another way to distinguish between args and > kwargs.) Yes, one could opt for a syntax where the collection type is optional and a sensible default is chosen, But to me that would largely defeat the point; I very much like the added verbosity and explicitness. args- tuple and kwargs-dict; that just has a much better ring to it than star-star-kwargs, or whatever other cryptic symbol you use. > I have one more suggestion. > > I do have one more thing to point out, which is that currently the > Python vararg syntax is very difficult to Google for. In the first pages > of the four searches matching "python (function)? (star | asterisk)", > there was just one relevant hit on python.org which wasn't a bug report. > I certainly remember having a small amount of difficulty figuring out > what the heck * and ** did the first time I encountered them. > > This would suggest perhaps some keywords might be called for instead of > operators. In the grand scheme of things the argument packing and > unpacking are not *all* that common, so I don't think the syntactic > burden would be immense. The bigger issue, of course, would be picking > good words. > > This also helps with the issue above. Let's say we'll use 'varargs' and > 'kwargs', though the latter too well-ingrained in code to steal. (I > don't want to get too much into the debate over *what* word to choose. > Also these don't match the 'head, *tail = l' syntax very well.) Then we > could say: > ? def foo(varargs l, kwargs d): > ? ? ? bar(varargs l, kwargs d) > and varargs would be equivalent to * and kwargs would be equivalent to > **. But then you could also say > ? def foo(varargs(list) l, kwargs(dict) d) I agree; I had the same experience. But according to others our opinion is wrong, and we should just become better at using google. From __peter__ at web.de Sun Dec 18 10:01:25 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 18 Dec 2011 16:01:25 +0100 Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> <8d1ec984-9786-4b0c-924b-01f661c1374c@q11g2000vbq.googlegroups.com> Message-ID: nukeymusic wrote: > thanks and also thanks to all the others who were so kind to help me > out with my first python-script. > I tested your alternatives and they work, the only a minor > inconvenience is that the first line of the inputfile gets lost i.e. > the first timestamp should become zero (seconds) That should be easy to fix: >> with open('testfile','r') as f, open('outputfile','w') as g: >> first_date, first_rest = parse_line(next(f)) g.write("0 %s" % first_rest) >> for line in f: >> cur_date, rest = parse_line(line) >> delta = cur_date - first_date >> g.write("%s %s" % (int(round(delta.total_seconds())), rest)) From nukeymusic at gmail.com Sun Dec 18 10:22:23 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 07:22:23 -0800 (PST) Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> <8d1ec984-9786-4b0c-924b-01f661c1374c@q11g2000vbq.googlegroups.com> Message-ID: <5b86f74b-df94-462e-b022-3119f022995d@z17g2000vbe.googlegroups.com> On 18 dec, 16:01, Peter Otten <__pete... at web.de> wrote: > nukeymusic wrote: > > thanks and also thanks to all the others who were so kind to help me > > out with my first python-script. > > I tested your alternatives and they work, the only a minor > > inconvenience is that the first line of the inputfile gets lost i.e. > > the first timestamp should become zero (seconds) > > That should be easy to fix: > > >> with open('testfile','r') as f, open('outputfile','w') as g: > >> ? ? ?first_date, first_rest = parse_line(next(f)) > > ? ? ? ? g.write("0 %s" % first_rest) > > > > > > > > >> ? ? ?for line in f: > >> ? ? ? ? ?cur_date, rest = parse_line(line) > >> ? ? ? ? ?delta = cur_date - first_date > >> ? ? ? ? ?g.write("%s %s" % (int(round(delta.total_seconds())), rest)) thanks, that solves it, you're too kind nukey From joshua.landau.ws at gmail.com Sun Dec 18 11:04:58 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Sun, 18 Dec 2011 16:04:58 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> Message-ID: I'm just going to throw myself in the conversation obtusely.I felt we needed some real code. These are literally the first two samples I had on hand. !!! OUT OF CONTEXT REAL CODE ALERT !!! ################################### formatter = formatter.format(**color).replace("(","{").replace(")","}") print("\n\n".join(formatter.format(**d) for d in db)) # vs # formatter = formatter.format(::(), ::color).replace("(","{").replace(")","}") print("\n\n".join(formatter.format(::(), ::d) for d in db)) # What do we do for "format(**kwargs)"? # With the current guess of mine, I'm thinking "what does ::() do?" and "why are there two double-colons?". # Aside from that it looks not much different/worse. # and # def checkLoad(self, *args, **kwargs): if getattr(self, x) == None: getattr(self, loadx)() if fakeself: return function(getattr(self, x), *args, **kwargs) else: return function(self, *args, **kwargs) return checkLoad # vs # def checkLoad(self, tuple::args, dict::kwargs): if getattr(self, x) == None: getattr(self, loadx)() if fakeself: return function(getattr(self, x), ::args, ::kwargs) else: return function(self, ::args, ::kwargs) return checkLoad # It looks much the same, but how does it benefit me? # "*args" is very distinct from "**kwargs" in my opinion. "tuple"/"dict" doesn't have that. # That said, the second works better with my syntax highlighter (although pylint then swears at me). ############################################################# Before I actually put the syntax in context I hated this. Now I just really dislike it. There is one big thing bugging me: How do I do: "foo(**kwargs)"? My solution was to use "foo(::(), ::kwargs)", but it's obviously not a great alternative. There is another, smaller one. Wrapper functions often just want to pass values straight through them. They don't care that it's a tuple. They don't care that it's a dict. They're just going to throw it across. Without "def foo(::args)" syntax, that's just a waste of a lookup. With "def foo(::args)" syntax you lose the discrimination of args and kwargs, resulting in the same problem above. Finally, it's uglier when without. I do have one suggestion: foo(*args, **kwargs): pass foo(tuple(*) args, dict(**) kwargs): pass foo(dict(**) kwargs): pass foo(*, bar="spam"): pass # this is illegal because unbound "catchalls" don't catch anything, as they do today foo(tuple(*), bar="spam"): pass I'll leave you to guess how it works. If you can't it's obviously not obvious enough. Advantages: "*" means a catchall, as it should. "*" is distinct from "**". With syntax highlighting, it's pretty obvious what is going on. It's backwards compatible (the main reason it's not "tuple(*):args"). *My main problem is: use case? I've had none at all.* If only 1% of users want it, it's not right to make the 99% change. Saving *one line and 50 microseconds* per hundred uses of (*/**) is not a valid argument.And don't make us all change all our code just to satisfy that. The only argument you can make is readability, but (*/**) is more explicit than a lookup, just because it's not dependant on position (args/kwargs argument) and it's always a real tuple and dict. def tuple(): 1/0 -------------- next part -------------- An HTML attachment was scrubbed... URL: From workitharder at gmail.com Sun Dec 18 11:06:56 2011 From: workitharder at gmail.com (buck) Date: Sun, 18 Dec 2011 08:06:56 -0800 (PST) Subject: Debugging a difficult refcount issue. In-Reply-To: <7xpqfm48m6.fsf@ruckus.brouhaha.com> References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> <7xpqfm48m6.fsf@ruckus.brouhaha.com> Message-ID: <812938.821.1324224416576.JavaMail.geo-discussion-forums@prfi34> On Saturday, December 17, 2011 11:55:13 PM UTC-8, Paul Rubin wrote: > buck writes: > > I tried to pinpoint this intermediate allocation with a similar > > PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > Did you try a gdb watchpoint? I didn't try that, since that piece of code is run millions of times, and I don't know the dict-id I'm looking for until after the problem has occurred. From steve+comp.lang.python at pearwood.info Sun Dec 18 12:03:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 17:03:05 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <9c808e14-fde9-49e7-b051-84ff45d663d5@i8g2000vbh.googlegroups.com> Message-ID: <4eee1cc8$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Dec 2011 06:13:37 -0800, Eelco wrote: > Casts or conversions are a runtime concept; im talking about > declarations. That seems to be the source of your confusion. Everything in Python happens at runtime, apart from compilation of source code into byte code. Python doesn't have declarations. Even class and def are statements which happen at runtime, not declarations which happen at compile time. Your proposal will be no different: if it happens, it will happen at runtime. [...] > I guess this use of the term entered my lexicon through the > C# developer team. Either way, you seem to be the only one who does not > grok my intended meaning, so I suggest you try reading it again. I grok your *intended* meaning. I also grok that you are using the wrong words to explain your intended meaning. "head, *tail = iterable" is not a declaration in Python. It is not a type conversion, or a constraint, or a cast. It is an iterator unpacking operation. It is syntactic sugar for something similar to this: tmp = iter(iterable) head = next(tmp) tail = [] try: while True: tail.append(next(tmp)) except StopIteration: pass del tmp The only constraint is on the *input* to the operation: the right hand side object must obey the iterator protocol. The only conversion (if we want to call it such) is that an iterator object is created by the right hand side object. [...] > head, tail = l and head, *tail = l mean something completely different, They are both iterator unpacking, and therefore by definition not "completely different". The first one is syntactic sugar for something like this: tmp = iter(iterable) head = next(tmp) tail = next(tmp) if tmp is not empty: raise exception del tmp and the second as shown earlier. Clearly they are quite similar: the only difference is that in the first case, the right hand side must have exactly two items, while in the second case, the right hand side can have an arbitrary number of items. > and the only difference is a constraint placed on tail, which forces the > semantics to be different; the righthand side, or what is to be > assigned, is identical. Of course one can just regard it as syntactic > sugar for head, tail = unpackheadandtailaslist(l); but the syntactic > sugar achieves that same end through a type constraint on tail. Really. All the words are in English, but I have no understanding of what you are trying to say here. What is unpackheadandtailaslist and how does it differ from actual unpacking in Python? >> You are jumping to conclusions about implementation details which >> aren't supported by the visible behaviour. What evidence do you have >> that iterator unpacking creates a tuple first and then converts it to a >> list? > > You are jumping to conclusions about my opinion which aren't supported > by my visible behaviour. What evidence do you have that I ever even said > any such thing? My evidence was your actual words, quoted in my post, which you deleted in your response. Here they are again: [quote] This changes the semantics from normal unpacking, to unpacking and then repacking all but the head into a list. [end quote] In context, "this changes..." refers to extended iterator unpacking in the form "head, *tail" contrasted with "head, tail =...", and that it generates a list. It may very well be that I have misunderstood you. If you do not intend the meaning that extended tuple unpacking first unpacks to a tuple and then re-packs to a list, then please explain what you did mean. >> > The aim of this PEP, is that this type-constraint syntax is expanded >> > upon. We should be careful here to distinguish with providing >> > optional type constraints throughout python as a whole; this is not >> > our aim. >> >> Iterator unpacking is no more about type constraints than is len(). > > Because you wish to keep nitpicking about my usage of the term 'type > constraint' (even though you have not introduced an alternative term > yourself), or because you actually disagree with the content of my > message? I don't think much about your proposal. I think it is unnecessary, based on a profound misunderstanding of how Python actually operates, badly explained using inappropriate terms, and the syntax you propose is ugly. -- Steven From hoogendoorn.eelco at gmail.com Sun Dec 18 12:40:06 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 18 Dec 2011 09:40:06 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <9c808e14-fde9-49e7-b051-84ff45d663d5@i8g2000vbh.googlegroups.com> <4eee1cc8$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <600d1e90-c8b5-4dd1-831d-922372bc8808@q16g2000yqn.googlegroups.com> On 18 dec, 18:03, Steven D'Aprano wrote: > On Sun, 18 Dec 2011 06:13:37 -0800, Eelco wrote: > > Casts or conversions are a runtime concept; im talking about > > declarations. That seems to be the source of your confusion. > > Everything in Python happens at runtime, apart from compilation of source > code into byte code. Python doesn't have declarations. Even class and def > are statements which happen at runtime, not declarations which happen at > compile time. Of course it will have runtime effects; and compile/interpret-time effects too. You said as much yourself: 'Everything in Python happens at runtime, apart from compilation of source'. Python *does* have declarations. Even though they may not be called such in the documentation (dunno, havnt checked) the current use of * and ** when not signifying collectiong UNpacking, is an annotation to the declaration of the symbol that comes after it. In this case, a constraint-on / specification-of the type of the object that the variable symbol may bind; a list or tuple, depending on the context. > Your proposal will be no different: if it happens, it will happen at > runtime. At least something we agree upon; all im proposing is an alternative syntax for something thats already there. Lets be pragmatic and agree to disagree on what it is that is already there, or what it ought to be called. Because frankly, this has detracted enough already from what it is I do want to discuss. > [snipped staements of the obvious] > > and the second as shown earlier. Clearly they are quite similar: the only > difference is that in the first case, the right hand side must have > exactly two items, while in the second case, the right hand side can have > an arbitrary number of items. Oh great, you found another semantic hair to split. Yes, they are quite similar. Nonetheless, any python implementation that would confuse the two behaviors would be considered badly bugged. > > and the only difference is a constraint placed on tail, which forces the > > semantics to be different; the righthand side, or what is to be > > assigned, is identical. Of course one can just regard it as syntactic > > sugar for head, tail = unpackheadandtailaslist(l); but the syntactic > > sugar achieves that same end through a type constraint on tail. Really. > > All the words are in English, but I have no understanding of what you are > trying to say here. What is unpackheadandtailaslist and how does it > differ from actual unpacking in Python? I have the impression you are not even trying then. Google 'syntactic sugar'. It means 'semantically identical way of putting things', in short. That tells you everything you need to know about unpackheadandtailaslist: nothing. > >> You are jumping to conclusions about implementation details which > >> aren't supported by the visible behaviour. What evidence do you have > >> that iterator unpacking creates a tuple first and then converts it to a > >> list? > > > You are jumping to conclusions about my opinion which aren't supported > > by my visible behaviour. What evidence do you have that I ever even said > > any such thing? > > My evidence was your actual words, quoted in my post, which you deleted > in your response. > > Here they are again: > > ? ? [quote] > ? ? This changes the semantics from normal unpacking, to unpacking > ? ? and then repacking all but the head into a list. > ? ? [end quote] > > In context, "this changes..." refers to extended iterator unpacking in > the form "head, *tail" contrasted with "head, tail =...", and that it > generates a list. > > It may very well be that I have misunderstood you. If you do not intend > the meaning that extended tuple unpacking first unpacks to a tuple and > then re-packs to a list, then please explain what you did mean. Again, where did I say it first unpacks to a tuple? As far as im aware that only happens in the context of a function call. > >> > The aim of this PEP, is that this type-constraint syntax is expanded > >> > upon. We should be careful here to distinguish with providing > >> > optional type constraints throughout python as a whole; this is not > >> > our aim. > > >> Iterator unpacking is no more about type constraints than is len(). > > > Because you wish to keep nitpicking about my usage of the term 'type > > constraint' (even though you have not introduced an alternative term > > yourself), or because you actually disagree with the content of my > > message? > > I don't think much about your proposal. I think it is unnecessary, based > on a profound misunderstanding of how Python actually operates, badly > explained using inappropriate terms, and the syntax you propose is ugly. Which is not an aswer to the question I posed; just an expression of frustration. Its mutual. From jackdied at gmail.com Sun Dec 18 12:52:46 2011 From: jackdied at gmail.com (Jack Diederich) Date: Sun, 18 Dec 2011 12:52:46 -0500 Subject: Debugging a difficult refcount issue. In-Reply-To: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: I don't have any great advice, that kind of issue is hard to pin down. That said, do try using a python compile with --with-debug enabled, with that you can turn your unit tests on and off to pinpoint where the refcounts are getting messed up. It also causes python to use plain malloc()s so valgrind becomes useful. Worst case add assertions and printf()s in the places you think are most janky. -Jack On Sat, Dec 17, 2011 at 11:17 PM, buck wrote: > I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. > > Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. > > I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. > > I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > How should I go about debugging this further? I've been completely stuck on this for two days now :( > > [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 > [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload > -- > http://mail.python.org/mailman/listinfo/python-list From traveller3141 at gmail.com Sun Dec 18 13:33:32 2011 From: traveller3141 at gmail.com (traveller3141) Date: Sun, 18 Dec 2011 13:33:32 -0500 Subject: Question about Reading from text file with Python's Array class Message-ID: I've been trying to use the Array class to read 32-bit integers from a file. There is one integer per line and the integers are stored as text. For problem specific reasons, I only am allowed to read 2 lines (2 32-bit integers) at a time. To test this, I made a small sample file (sillyNums.txt) as follows; 109 345 2 1234556 To read this file I created the following test script (trying to copy something I saw on Guido's blog - http://neopythonic.blogspot.com/2008/10/sorting-million-32-bit-integers-in-2mb.html ): import array assert array.array('i').itemsize == 4 bufferSize = 2 f=open("sillyNums.txt","r") data = array.array('i') data.fromstring(f.read(data.itemsize* bufferSize)) print data The output was nonsense: array('i', [171520049, 171258931]) I assume this has something to do with my incorrectly specifying how the various bit/bytes line up. Does anyone know if there's a simple explanation of how I can do this correctly, or why I can't do it at all? Thanks, Steven -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sun Dec 18 14:24:28 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 18 Dec 2011 20:24:28 +0100 Subject: Question about Reading from text file with Python's Array class References: Message-ID: traveller3141 wrote: > I've been trying to use the Array class to read 32-bit integers from a > file. There is one integer per line and the integers are stored as text. > For problem specific reasons, I only am allowed to read 2 lines (2 32-bit > integers) at a time. > > To test this, I made a small sample file (sillyNums.txt) as follows; > 109 > 345 > 2 > 1234556 These are numbers in ascii not in binary. To read these you don't have to use the array class: from itertools import islice data = [] with open("sillyNums.txt") as f: for line in islice(f, 2): data.append(int(line)) (If you insist on using an array you of course can) "Binary" denotes the numbers as they are stored in memory and typically used by lowlevel languages like C. A binary 32 bit integer always consists of 4 bytes. With your code > To read this file I created the following test script (trying to copy > something I saw on Guido's blog - > http://neopythonic.blogspot.com/2008/10/sorting-million-32-bit-integers- in-2mb.html > ): > > import array > assert array.array('i').itemsize == 4 > > bufferSize = 2 > > f=open("sillyNums.txt","r") > data = array.array('i') > data.fromstring(f.read(data.itemsize* bufferSize)) > print data > > > The output was nonsense: > array('i', [171520049, 171258931]) you are telling Python to interpret the first 8 bytes in the file as two integers. The first 4 bytes are "1", "0", "9", "\n" (newline) when interpreted as characters or 49, 48, 57, 10 when looking at the bytes' numerical values. A 32 bit integer is calculated with >>> 49+48*2**8+57*2**16+10*2**24 171520049 Looks familiar... From paka-en at tumia.org Sun Dec 18 14:26:33 2011 From: paka-en at tumia.org (Paka Small) Date: Sun, 18 Dec 2011 11:26:33 -0800 (PST) Subject: Tumia the first Object-Oriented Internet Directory Message-ID: <3309e437-d9cd-47dc-b257-a62d30ae3c93@f1g2000yqi.googlegroups.com> Hi, To my knowledge Tumia (http://www.tumia.org) is the first internet directory structured according to the object-oriented paradigm. Currently Tumia is in its baby years. To illustrate the object- oriented concept consider below "Navigate from Earth Global Warming 2000 AD to Mitigating Solar Energy and Back" which is a specific scenario of the use case "Navigate to Related Tumia Instances". Scenario Navigate from Earth Global Warming 2000 AD to Mitigating Solar Energy and Back Precondition: The user has opened http://www.tumia.org . Scenario: 1. The user selects the Tumia Class Climate Change. 2. The system displays http://www.tumia.org/en/directory/en/earth_global_warming_2000_ad_p1.html 3. The user selects the Related Instance Solar Energy. 4. The system displays http://www.tumia.org/en/directory/en/solar_energy_p1.html 5. The user selects the Related Instance Earth Global Warming 2000 AD. 6. The system displays http://www.tumia.org/en/directory/en/earth_global_warming_2000_ad_p1.html I hope as software developers and OO and use case believers you'll find this example to be a promising use of object-orientation and of use cases in the functional analysis discipline of software development. I also hope it shows how important OO is not only in software design but in first instance in functional analysis and thus determining the domain classes of your software design and thus realising low representational gap. Kind Regards, Paka Small From python.list at tim.thechases.com Sun Dec 18 14:33:13 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 18 Dec 2011 13:33:13 -0600 Subject: Question about Reading from text file with Python's Array class In-Reply-To: References: Message-ID: <4EEE3FF9.1010308@tim.thechases.com> On 12/18/11 12:33, traveller3141 wrote: > To test this, I made a small sample file (sillyNums.txt) as follows; > 109 > 345 > 2 > 1234556 > > f=open("sillyNums.txt","r") > data = array.array('i') > data.fromstring(f.read(data.itemsize* bufferSize)) > print data > > The output was nonsense: > array('i', [171520049, 171258931]) > > I assume this has something to do with my incorrectly specifying how the > various bit/bytes line up. Does anyone know if there's a simple explanation > of how I can do this correctly, or why I can't do it at all? It reads the bytes directly as if using struct.unpack() as in >>> data = '109\n345\n2\n123456' >>> print ' '.join(hex(ord(c))[2:] for c in data) 31 30 39 a 33 34 35 a 32 a 31 32 33 34 35 36 >>> 0x0a393031 # 4 bytes 171520049 It sounds like you want something like from itertools import islice a = array.array('i') a.fromlist(map(int, islice(f, bufferSize))) which will read the first 2 lines of the file (using islice() on the file-object and slicing the first 2 items), and map the string representations into integers, then pass the resulting list of integer data to the array.array.fromlist() method. -tkc From someone at someplace.invalid Sun Dec 18 14:41:37 2011 From: someone at someplace.invalid (HoneyMonster) Date: Sun, 18 Dec 2011 19:41:37 +0000 (UTC) Subject: Newbie Question: Obtain element from list of tuples Message-ID: Hi, I'm just starting to learn Python, so please bear with me. I have in my program an object (recs) which is a list of tuples (returned as such by a database query). My question is doubtless a very easy one to answer: Say I want the ninth element in the twentieth tuple put into variable PID, I can do this, bearing in mind that numbering starts at zero: tup = recs[19] PID = tup[8] But there must be an easier way; i.e. to do it in one go without the extra variable. How do I achieve that please? Secondly, I am more than happy to read the fine manuals. If someone could point me in the direction of one or two "for idiots" guides, that would be greatly appreciated. From edriscoll at wisc.edu Sun Dec 18 14:47:46 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sun, 18 Dec 2011 13:47:46 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EEE4362.8060103@wisc.edu> On 12/18/2011 2:36, Steven D'Aprano wrote: > The barrier to new keywords in Python is very high. Not going to > happen for something that already has perfectly good syntax already > familiar to Python and Ruby programmers. Might else well try to get C > and Java to stop using "..." (ellipses). I agree to some extent; I did say my suggestion was somewhat an effort in brainstorming. (Though I would point out that, if you accept the premise of this PEP suggestion, "perfectly good" only sort of applies.) > The burden is that you complicate the compiler and reduce the pool of > useful names available to the programmer. To be useful, the keywords need > to be short, meaningful and memorable -- which means they're already used > in code, which you will break needlessly. OTOH, it wouldn't be the first time it was done. For instance, 'with'. > With operators that otherwise would be illegal, the programmer doesn't > need to learn about varargs until they need to. With keywords, the > programmer needs to learn "you can't use varargs or kwargs as variable > names" practically from day 1. Yes, but it's not exactly a hard lesson. I can't think of any time where you'd have ambiguous syntax, so misuse could always produce a good error message. And keyword highlighting is something that basically all editors can do well... so unless you're opposed to using an editor more advanced than Notepad, you'll type in "varargs", it'll turn purple or whatever, and you'll go "oh that's a keyword." > So you're not just adding keywords, you're adding new syntax: something > which looks like a function call, but isn't a function call. Well, almost by definition these proposals consider adding syntax. > I'm going to pose the same question to you I have already posed to Eelco: > in your proposal, what happens if the caller has shadowed the list built- > in? Does argument l become a built-in list, or does it become whatever > type list is currently bound to? > > Some more questions: > > Can varargs accepted arbitrary callables, or only a fixed set of known > types? > > How does this differ from what can be done with annotations? These are questions I don't feel I can answer very well. I was giving my thoughts on what I would do or consider doing with the proposal assuming the fundamental idea is sound. Whether it should be accepted at all is a matter for someone else. :-) Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From arnodel at gmail.com Sun Dec 18 14:49:34 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sun, 18 Dec 2011 19:49:34 +0000 Subject: Newbie Question: Obtain element from list of tuples In-Reply-To: References: Message-ID: On 18 December 2011 19:41, HoneyMonster wrote: > Hi, > > I'm just starting to learn Python, so please bear with me. I have in my > program an object (recs) which is a list of tuples (returned as such by a > database query). > > My question is doubtless a very easy one to answer: Say I want the ninth > element in the twentieth tuple put into variable PID, I can do this, > bearing in mind that numbering starts at zero: > > tup = recs[19] > PID = tup[8] > > But there must be an easier way; i.e. to do it in one go without the > extra variable. How do I achieve that please? Well, you were almost there: pid = recs[19][8] Note: all caps is usually reserved for constants in Python. -- Arnaud From edriscoll at wisc.edu Sun Dec 18 14:56:17 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sun, 18 Dec 2011 13:56:17 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EEE4561.10705@wisc.edu> On 12/18/2011 1:31, Steven D'Aprano wrote: > And rebutted. Modesty[1] prevents me from quoting myself, but here are > some links to searches: > > http://duckduckgo.com/?q=python+asterisk > http://duckduckgo.com/?q=python+* OK, so if you search using the right search engine, you *might* get a link to the Python docs. (As I pointed out in a reply to myself before, you *do* get relevant hits with my Google searches as well, but virtually nothing to the relevant Python documentation.) My thinking is borne out; I asked a friend who didn't know what it did to find out, and he went to Google with the search "python 'starred argument'", which he said led to moderately helpful results, but again none from the actual Python docs. > My normal first place to look for something is Wikipedia. Enjoy it before > SOPA kills it. > > http://en.wikipedia.org/wiki/Asterisk#Programming_languages I might think to look at the Wikipedia article for *Python* (which, by the way, doesn't help); looking at the wikipedia page for *asterisk* wouldn't even enter my mind. > Your search looks overly complicated to me. > > I'm not an expert on Google's syntax, but if you search for "python, > optionally with function", isn't that the same as just searching for > "python" since it will return hits either with or without "function"? Chris Angelico's interpretation is correct: I did four searches, "python function star", "python function asterisk", "python star", and "python asterisk". Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From roy at panix.com Sun Dec 18 15:04:13 2011 From: roy at panix.com (Roy Smith) Date: Sun, 18 Dec 2011 15:04:13 -0500 Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: In article , HoneyMonster wrote: > Hi, > > I'm just starting to learn Python, so please bear with me. I have in my > program an object (recs) which is a list of tuples (returned as such by a > database query). Sounds like a standard database interface -- each tuple represents one row in the query result. Very common. > My question is doubtless a very easy one to answer: Say I want the ninth > element in the twentieth tuple put into variable PID, I can do this, > bearing in mind that numbering starts at zero: > > tup = recs[19] > PID = tup[8] Sure, you could do: PID = recs[19][8] That's shorter, but it probably not better. Presumably, the tuple elements represent columns in the database. It would make the code easier to read if you unpacked the tuple into something with human-readable names: col_1, col_2, col_3, ...... col_n = recs[19] and then use the name for the column of interest. A common convention is that when you're unpacking a tuple (or a list, I suppose) and are only interested in some of the elements, you unpack the others into "_". Thus: _, _, _, _, pid, _, _, _ = recs[19] Of course, if you're going to do all that, you probably could have written a better query which returned just the column you were interested in. Instead of "select * from foo", you could have done "select pid from foo" (assuming you're using some SQL-based database). > Secondly, I am more than happy to read the fine manuals. If someone could > point me in the direction of one or two "for idiots" guides, that would > be greatly appreciated. The on-line tutorial is pretty good. http://docs.python.org/tutorial/index.html From edriscoll at wisc.edu Sun Dec 18 15:20:20 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sun, 18 Dec 2011 14:20:20 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> Message-ID: <4EEE4B04.1040505@wisc.edu> On 12/18/2011 8:35, Eelco wrote: > No, its not type *checking*, its type *declaration*. I tried to go to > great lengths to point that out, but it appears I did not do a very > good job :). Type declaration is exactly what I want, and insofar this > syntax has already found adoptation elsewhere, ill consider that a > plus. You say it's found adoption elsewhere, but I think it's that adoption which makes it a *bad* idea, because it does something entirely different in those situations. Other languages are using that syntax for something which is statically checked -- you are proposing that syntax for a dynamic conversion. look pretty familiar... (Not to mention if you just omit the type from the examples above you need another way to distinguish between args and kwargs.) > Yes, one could opt for a syntax where the collection type is optional > and a sensible default is chosen, But to me that would largely defeat > the point; I very much like the added verbosity and explicitness. args- > tuple and kwargs-dict; that just has a much better ring to it than > star-star-kwargs, or whatever other cryptic symbol you use. My problem with it is that it in some sense is forcing me to make a decision I don't care about. Yes, what we have now is less flexible, but I have *never* said "man, I wish this *args parameter were a list instead of a tuple". So at least for me, making me say "args::tuple" or "@tuple args" or whatever is just changing the syntax a bit and adding several more characters for me to type. Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From someone at someplace.invalid Sun Dec 18 15:22:20 2011 From: someone at someplace.invalid (HoneyMonster) Date: Sun, 18 Dec 2011 20:22:20 +0000 (UTC) Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: On Sun, 18 Dec 2011 15:04:13 -0500, Roy Smith wrote: > In article , > HoneyMonster wrote: > >> Hi, >> >> I'm just starting to learn Python, so please bear with me. I have in my >> program an object (recs) which is a list of tuples (returned as such by >> a database query). > > Sounds like a standard database interface -- each tuple represents one > row in the query result. Very common. > >> My question is doubtless a very easy one to answer: Say I want the >> ninth element in the twentieth tuple put into variable PID, I can do >> this, bearing in mind that numbering starts at zero: >> >> tup = recs[19] >> PID = tup[8] > > Sure, you could do: > > PID = recs[19][8] > > That's shorter, but it probably not better. Presumably, the tuple > elements represent columns in the database. It would make the code > easier to read if you unpacked the tuple into something with > human-readable names: > > col_1, col_2, col_3, ...... col_n = recs[19] > > and then use the name for the column of interest. A common convention > is that when you're unpacking a tuple (or a list, I suppose) and are > only interested in some of the elements, you unpack the others into "_". > Thus: > > _, _, _, _, pid, _, _, _ = recs[19] > > Of course, if you're going to do all that, you probably could have > written a better query which returned just the column you were > interested in. Instead of "select * from foo", you could have done > "select pid from foo" (assuming you're using some SQL-based database). > >> Secondly, I am more than happy to read the fine manuals. If someone >> could point me in the direction of one or two "for idiots" guides, that >> would be greatly appreciated. > > The on-line tutorial is pretty good. > http://docs.python.org/tutorial/index.html Thanks, Arnaud and Roy. I had no idea it could be so easy. I think I rather like Python! OK, I'll change PID to pid if that's best practice. Yes, it's an SQL-based database, PostgreSQL actually. I'm using psycopg2 to access it and wxGlade to construct the GUI interface. It all seems to be progressing very smoothly. Thanks again. From roy at panix.com Sun Dec 18 15:25:06 2011 From: roy at panix.com (Roy Smith) Date: Sun, 18 Dec 2011 15:25:06 -0500 Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: In article , HoneyMonster wrote: > I think I rather like Python! +1 QOTD? From rosuav at gmail.com Sun Dec 18 15:51:08 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Dec 2011 07:51:08 +1100 Subject: Newbie Question: Obtain element from list of tuples In-Reply-To: References: Message-ID: On Mon, Dec 19, 2011 at 6:41 AM, HoneyMonster wrote: > My question is doubtless a very easy one to answer: Say I want the ninth > element in the twentieth tuple put into variable PID, I can do this, > bearing in mind that numbering starts at zero: > > tup = recs[19] > PID = tup[8] > > But there must be an easier way; i.e. to do it in one go without the > extra variable. How do I achieve that please? The specific answer has already been given, but I'd like to fill in the generality. In Python, everything's an object; if you can do something with a variable, you can do it with an expression too. I don't know what your function call is to obtain your record list, but imagine it's: recs = conn.query("SELECT * FROM some_table") Then you can actually do all of this in a single statement. It's not usually what you'll want to do, but this is legal: pid = conn.query("SELECT * FROM some_table")[19][8] If you're absolutely certain that you'll always get precisely one value from a query, this becomes rather more useful: mode = conn.query("SELECT mode FROM config WHERE id=5")[0][0] In any case: You can work with a function's return value directly, without first storing it in a temporary variable. Hope that helps! Chris Angelico From roy at panix.com Sun Dec 18 16:10:25 2011 From: roy at panix.com (Roy Smith) Date: Sun, 18 Dec 2011 16:10:25 -0500 Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: In article , Chris Angelico wrote: > If you're absolutely certain that you'll always get precisely one > value from a query, this becomes rather more useful: > > mode = conn.query("SELECT mode FROM config WHERE id=5")[0][0] Although, if you're going to do that, you might as well take advantage of the fetchone() method that most Python database APIs have and eliminate one of the indexes. Typical code would be something like: cursor.execute("SELECT mode FROM config WHERE id=5") mode = cursor.fetchone()[0] If you're going to be doing any database work in Python, you should be familiar with the Python Database API Specification, http://www.python.org/dev/peps/pep-0249/. Most of the vendor-specific database modules have interfaces which hold pretty close to the style described there. You might also want to explore SQL_Alchemy. I personally find it difficult and painful to work with, but it does have the powerful advantage that it abstracts away all the vendor-specific differences between databases. From someone at someplace.invalid Sun Dec 18 17:55:38 2011 From: someone at someplace.invalid (HoneyMonster) Date: Sun, 18 Dec 2011 22:55:38 +0000 (UTC) Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: On Mon, 19 Dec 2011 07:51:08 +1100, Chris Angelico wrote: > On Mon, Dec 19, 2011 at 6:41 AM, HoneyMonster > wrote: >> My question is doubtless a very easy one to answer: Say I want the >> ninth element in the twentieth tuple put into variable PID, I can do >> this, bearing in mind that numbering starts at zero: >> >> tup = recs[19] >> PID = tup[8] >> >> But there must be an easier way; i.e. to do it in one go without the >> extra variable. How do I achieve that please? > > The specific answer has already been given, but I'd like to fill in the > generality. In Python, everything's an object; if you can do something > with a variable, you can do it with an expression too. I don't know what > your function call is to obtain your record list, but imagine it's: > > recs = conn.query("SELECT * FROM some_table") > > Then you can actually do all of this in a single statement. It's not > usually what you'll want to do, but this is legal: > > pid = conn.query("SELECT * FROM some_table")[19][8] > > If you're absolutely certain that you'll always get precisely one value > from a query, this becomes rather more useful: > > mode = conn.query("SELECT mode FROM config WHERE id=5")[0][0] > > In any case: You can work with a function's return value directly, > without first storing it in a temporary variable. > > Hope that helps! Thanks, Chris. Actually I had simplified the question somewhat. The actuality is indeed - as you suspected - that recs = conn.query("SELECT * FROM some_table") but I am then using the recs object to populate a (non editable) wxGrid. When the user selects a row and clicks a button, I am using: pos = self.grid_1.GetGridCursorRow() to establish which tuple in recs is involved, and then pid = recs[pos][4] to determine the key value (I suspected that accessing recs directly would be more efficient that trying to determine the wxGrid column value, and in any case the pid is in recs but not in the wxGrid). I trust this all makes sense. Thanks again. From rosuav at gmail.com Sun Dec 18 18:40:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Dec 2011 10:40:06 +1100 Subject: Newbie Question: Obtain element from list of tuples In-Reply-To: References: Message-ID: On Mon, Dec 19, 2011 at 9:55 AM, HoneyMonster wrote: > When the user selects a row and clicks a button, I am using: > pos = self.grid_1.GetGridCursorRow() to establish which tuple in recs is > involved, and then pid = recs[pos][4] to determine the key value (I > suspected that accessing recs directly would be more efficient that > trying to determine the wxGrid column value, and in any case the pid is > in recs but not in the wxGrid). Yep, this looks like a sensible way to do it! I would recommend, though, that you avoid "magic numbers" - the process ID might happen to be in cell 4, but what if your column list changes? This is especially problematic when you use "select * from table", because changes outside your code can break things. There's two solutions. One is to carefully match your SELECT statement to a set of constants: exec("SELECT FOO,BAR,QUUX,ASDF,PID,WHATEVER FROM table") FOO,BAR,QUUX,ASDF,PID,WHATEVER,*_=range(20) # Cool trick to avoid having to count the columns But a better way is to let the database handle it. Check your interface library to see if you can have it return dictionaries or objects instead of tuples - then you could use: pid = recs[pos]["pid"] # or pid = recs[pos]->pid which avoids the need to count columns at all. Other than that, though, yep - your code concept looks fine. ChrisA From rantingrickjohnson at gmail.com Sun Dec 18 19:57:56 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 18 Dec 2011 16:57:56 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: On Dec 17, 10:52?pm, buck wrote: > [...] > As for the separator, let's examine the available ascii punctuation. Excluding valid variable characters, whitespace, and operators, we have: > > ! -- ok. No, there are better uses for that char. > " -- can't use this. Would look like a string. > # -- no. Would looks like a comment. > $ -- ok. No, there are better uses for that char. > : -- ok, maybe. Seems confusing in a colon-terminated statement. dear god no! > ; -- no, just no. > ? -- ok. No, there are better uses for that char. > @ -- ok. YES! From rantingrickjohnson at gmail.com Sun Dec 18 19:59:22 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 18 Dec 2011 16:59:22 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> On Dec 17, 11:33?pm, Evan Driscoll wrote: > On 12/17/2011 22:52, buck wrote:> Try these on for size. > > > ? ? ?head, @tuple tail = sequence > > ? ? ?def foo(@list args, @dict kwargs): pass > > ? ? ?foo(@args, @kwargs) > > > For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. > > I like this idea much more than the original one. +1. I will second that! Eelco has the CORRECT idea, but the WRONG syntax! From rantingrickjohnson at gmail.com Sun Dec 18 20:04:42 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 18 Dec 2011 17:04:42 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> Message-ID: <8bc7f444-24f4-4179-9eaa-8dafbe541c14@f1g2000yqi.googlegroups.com> On Dec 18, 8:35?am, Eelco wrote: > On Dec 18, 6:33?am, Evan Driscoll wrote: > Good point; technically the askeriskes could be kept for backwards > compatibility, but that would break 'there should only be one way to > do it'. I believe it's high time for the upper society of this community to realize that the small breaks in backward compatibility in Python 3000 were only the beginning. Python is evolving past even what Guido could have dreamed. He can not hold the reins of this unwieldy beast any longer. From steve+comp.lang.python at pearwood.info Sun Dec 18 20:26:02 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Dec 2011 01:26:02 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eee92aa$0$11091$c3e8da3@news.astraweb.com> On Sun, 18 Dec 2011 13:47:46 -0600, Evan Driscoll wrote: [...] > so unless you're opposed to using an editor more > advanced than Notepad, you'll type in "varargs", it'll turn purple or > whatever, and you'll go "oh that's a keyword." Not everybody uses editors more advanced than Notepad. Even those who do may not have an editor that understands Python keywords, or has an obsolete list of keywords (my editor of choice doesn't know about NotImplementedError). Or they're ssh'ed into a customer's server half-way across the world and using `ed` because that's the only thing available. The point is not that there can never been more keywords in Python, but that the burden of proof is on those asking for new keywords. By default, the status quo wins: http://www.boredomandlaziness.org/2011/02/status-quo-wins-stalemate.html -- Steven From timothy.c.delaney at gmail.com Sun Dec 18 20:27:24 2011 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Mon, 19 Dec 2011 12:27:24 +1100 Subject: Make a small function thread safe In-Reply-To: <17238811.518.1324198329306.JavaMail.geo-discussion-forums@yqio4> References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> <4eebfbff$0$1679$742ec2ed@news.sonic.net> <17238811.518.1324198329306.JavaMail.geo-discussion-forums@yqio4> Message-ID: On 18 December 2011 19:52, RangerElf wrote: > Which is why the original .acquire() ... .release() idiom was wrong, this > would better express the intent: > > try: > lock.acquire() > shared_container.append(...) > finally: > lock.release() > No - this is very bad. The lock must be acquired outside the try: - otherwise if an exception is thrown while acquiring, you will try to release a lock that you have not acquired. Which again is why using with is a much better option - you can't make this kind of mistake. Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From workitharder at gmail.com Sun Dec 18 21:01:50 2011 From: workitharder at gmail.com (buck) Date: Sun, 18 Dec 2011 18:01:50 -0800 (PST) Subject: Debugging a difficult refcount issue. In-Reply-To: References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: <4953582.389.1324260110614.JavaMail.geo-discussion-forums@pruu23> Thanks Jack. I think printf is what it will come down to. I plan to put a little code into PyDict_New to print the id and the line at which it was allocated. Hopefully this will show me all the possible suspects and I can figure it out from there. I hope figuring out the file and line-number from within that code isn't too hard. On Sunday, December 18, 2011 9:52:46 AM UTC-8, Jack Diederich wrote: > I don't have any great advice, that kind of issue is hard to pin down. > That said, do try using a python compile with --with-debug enabled, > with that you can turn your unit tests on and off to pinpoint where > the refcounts are getting messed up. It also causes python to use > plain malloc()s so valgrind becomes useful. Worst case add assertions > and printf()s in the places you think are most janky. > > -Jack > > On Sat, Dec 17, 2011 at 11:17 PM, buck wrote: > > I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. > > > > Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. > > > > I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. > > > > I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > > > How should I go about debugging this further? I've been completely stuck on this for two days now :( > > > > [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 > > [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload > > -- > > http://mail.python.org/mailman/listinfo/python-list From workitharder at gmail.com Sun Dec 18 21:01:50 2011 From: workitharder at gmail.com (buck) Date: Sun, 18 Dec 2011 18:01:50 -0800 (PST) Subject: Debugging a difficult refcount issue. In-Reply-To: References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: <4953582.389.1324260110614.JavaMail.geo-discussion-forums@pruu23> Thanks Jack. I think printf is what it will come down to. I plan to put a little code into PyDict_New to print the id and the line at which it was allocated. Hopefully this will show me all the possible suspects and I can figure it out from there. I hope figuring out the file and line-number from within that code isn't too hard. On Sunday, December 18, 2011 9:52:46 AM UTC-8, Jack Diederich wrote: > I don't have any great advice, that kind of issue is hard to pin down. > That said, do try using a python compile with --with-debug enabled, > with that you can turn your unit tests on and off to pinpoint where > the refcounts are getting messed up. It also causes python to use > plain malloc()s so valgrind becomes useful. Worst case add assertions > and printf()s in the places you think are most janky. > > -Jack > > On Sat, Dec 17, 2011 at 11:17 PM, buck wrote: > > I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. > > > > Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. > > > > I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. > > > > I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > > > How should I go about debugging this further? I've been completely stuck on this for two days now :( > > > > [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 > > [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload > > -- > > http://mail.python.org/mailman/listinfo/python-list From rantingrickjohnson at gmail.com Sun Dec 18 21:16:00 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 18 Dec 2011 18:16:00 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> Message-ID: <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> On Dec 18, 7:26?pm, Steven D'Aprano wrote: > Not everybody uses editors more advanced than Notepad. And they have no excuse for NOT using a better one. Well, except for a "foolish consistency" that is! > Even those who do > may not have an editor that understands Python keywords, or has an > obsolete list of keywords (my editor of choice doesn't know about > NotImplementedError). Are you kidding me? Any editor that is not smart enough to do... >>> import keyword >>> pyKeyWords = keyword.kwlist ... needs to be thrown out the window. And let's not forget that Python even ships with an IDE called IDLE. As poorly written as IDLE's code may be, at least it is smart enough to find the current keyword list. From wuwei23 at gmail.com Sun Dec 18 21:23:41 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 18 Dec 2011 18:23:41 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> Message-ID: <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> Evan Driscoll wrote: > My problem with it is that it in some sense is forcing me to make a > decision I don't care about. Yes, what we have now is less flexible, but > I have *never* said "man, I wish this *args parameter were a list > instead of a tuple". And if you _did_, then one of the first lines in your function would be: args = list(args) Which is obvious to everyone, doesn't modify existing behaviour, doesn't force everyone without a fetish for change to add unnecessary cruft to their function signature... Except, OMG, list() is RETURNING A LIST, which is an OBVIOUS type constraint. I propose that: args = @set list(args) Will coerce args into a list and then give me a set in return. From wuwei23 at gmail.com Sun Dec 18 21:35:47 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 18 Dec 2011 18:35:47 -0800 (PST) Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: <2c286dea-efba-45d5-85a7-a7503733575a@i6g2000vbe.googlegroups.com> Roy Smith wrote: > A common convention > is that when you're unpacking a tuple (or a list, I suppose) and are > only interested in some of the elements, you unpack the others into "_". > Thus: > > _, _, _, _, pid, _, _, _ = recs[19] Pre-namedtuple, I used to like using named slices for this: cPID = slice(19) pid = recs[cPID] cHostPort = slice(99,100) host, port = recs[cHostPort] etc. The suggestions of having your query return a dictionary where possible are the most ideal, but if it's not possible you can always wrap the result tuple in a namedtuple: from collections import namedtuple Staff = namedtuple('Staff', ['firstname','lastname','age','position']) sql_result = ('John', 'Example', '30', 'Dummy') staff_record = Staff(sql_result) print staff_record.firstname, staff_record.age From cs at zip.com.au Sun Dec 18 21:45:03 2011 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 19 Dec 2011 13:45:03 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4EED6F64.5020906@wisc.edu> References: <4EED6F64.5020906@wisc.edu> Message-ID: <20111219024502.GA10048@cskk.homeip.net> On 17Dec2011 22:43, Evan Driscoll wrote: | On 12/17/2011 21:42, Chris Angelico wrote: | > Welcome to the list! If you're curious as to what's happened, check | > the archives: | > http://mail.python.org/pipermail/python-list/ | Thanks! Incidentally, is there a good way to respond to the original | post in this thread, considering it wasn't delivered to me? Personally, when I join a mailing list I download the archives and unpack them into my mail folder for that list. Then I can reply! (I actually do this for search purposes). Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ You can't have everything... where would you put it? - Charles Robinson, cr0100 at medtronic.com From ian.g.kelly at gmail.com Sun Dec 18 22:00:59 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 18 Dec 2011 20:00:59 -0700 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4eee92aa$0$11091$c3e8da3@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Sun, Dec 18, 2011 at 6:26 PM, Steven D'Aprano wrote: > On Sun, 18 Dec 2011 13:47:46 -0600, Evan Driscoll wrote: > > [...] >> so unless you're opposed to using an editor more >> advanced than Notepad, you'll type in "varargs", it'll turn purple or >> whatever, and you'll go "oh that's a keyword." > > Not everybody uses editors more advanced than Notepad. Even those who do > may not have an editor that understands Python keywords, or has an > obsolete list of keywords (my editor of choice doesn't know about > NotImplementedError). Probably because NotImplementedError is not a keyword. From steve+comp.lang.python at pearwood.info Sun Dec 18 22:00:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Dec 2011 03:00:59 GMT Subject: Newbie Question: Obtain element from list of tuples References: <2c286dea-efba-45d5-85a7-a7503733575a@i6g2000vbe.googlegroups.com> Message-ID: <4eeea8eb$0$11091$c3e8da3@news.astraweb.com> On Sun, 18 Dec 2011 18:35:47 -0800, alex23 wrote: > Pre-namedtuple, I used to like using named slices for this: > > cPID = slice(19) > pid = recs[cPID] You know, that is an incredibly simple thing and yet it never dawned on me before now. Thank you for sharing that. -- Steven From wuwei23 at gmail.com Sun Dec 18 22:15:34 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 18 Dec 2011 19:15:34 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <4EED6F64.5020906@wisc.edu> Message-ID: <4a93e19b-edf6-479b-af0f-37561e9c46f8@q11g2000vbq.googlegroups.com> Cameron Simpson wrote: > Personally, when I join a mailing list I download the archives and > unpack them into my mail folder for that list. Would it be more useful to you if you could unpack them into a tuple? From steve+comp.lang.python at pearwood.info Sun Dec 18 22:36:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Dec 2011 03:36:05 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4eeeb125$0$11091$c3e8da3@news.astraweb.com> On Sun, 18 Dec 2011 20:00:59 -0700, Ian Kelly wrote: > On Sun, Dec 18, 2011 at 6:26 PM, Steven D'Aprano > wrote: >> On Sun, 18 Dec 2011 13:47:46 -0600, Evan Driscoll wrote: >> >> [...] >>> so unless you're opposed to using an editor more advanced than >>> Notepad, you'll type in "varargs", it'll turn purple or whatever, and >>> you'll go "oh that's a keyword." >> >> Not everybody uses editors more advanced than Notepad. Even those who >> do may not have an editor that understands Python keywords, or has an >> obsolete list of keywords (my editor of choice doesn't know about >> NotImplementedError). > > Probably because NotImplementedError is not a keyword. Poor choice of words on my part. My editor colours built-ins like ValueError, TypeError, len, sum, etc., but not NotImplementedError. -- Steven From rosuav at gmail.com Sun Dec 18 23:35:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Dec 2011 15:35:06 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> Message-ID: On Mon, Dec 19, 2011 at 1:23 PM, alex23 wrote: > Except, OMG, list() is RETURNING A LIST, which is an OBVIOUS type > constraint. I propose that: > > ? ?args = @set list(args) > > Will coerce args into a list and then give me a set in return. Point to note: list,set = set,list # Request a death sentence from the next maintainer is perfectly legal code. Now, what does your "args=" line do? ChrisA From brian at python.org Mon Dec 19 00:05:58 2011 From: brian at python.org (Brian Curtin) Date: Sun, 18 Dec 2011 23:05:58 -0600 Subject: PyCon US 2012 sprints In-Reply-To: References: Message-ID: 2011/12/17 Ricardo B?nffy : > Hi folks. > > Next March I'm planning to attend PyCon US (for the first time) and > stay for the sprints. I am not sure how they work, however. Are there > any "first-timer guide to PyCon sprints"? I don't know of any such guide, but here's what usually happens: 1. Right after the closing of the conference days on Sunday, late in the afternoon, there will be a chance for all projects who plan to host sprints to get on stage and talk for a minute about who they are, what their project is, and what they'll be sprinting on. You might hear something like this: "Hi I'm Brian and I'll be hosting a sprint on foo project. We're going to be working on x, y, and z functionality using a lot of a, b, and c libraries. Come check it out of you're interested. We've got plenty of stuff for first timers" 2. At the entrance to wherever the sprints will be there will be a board listing what projects are sprinting in what areas. The last few years we've taken over numerous rooms, so that board acted as a map to know what was going on in what room. 3. I don't yet know how the sprints will be laid out this year since it's a new hotel and location, but if its anything like last year, each room will have several projects working inside it. It's quite normal to just walk in and say "hey where's the blah blah sprint at?", then see they're over in the corner. Everyone has been really friendly in my experience, and a lot of people bounce back and forth between rooms for a lot of reasons, so ask if you don't know where you're going or who you're looking for. 4. Hack on some fun stuff and meet some new people. It's a bit early for the 2012 sprint page to be up and populated, but it'll happen soon enough. The 2011 page is here (http://us.pycon.org/2011/sprints/) and the list of some (but not all) of the sprints and their attendees here: http://us.pycon.org/2011/sprints/projects/ It's really relaxed and a lot of fun. It's one of my favorite parts of the PyCon experience. From frank at chagford.com Mon Dec 19 01:46:28 2011 From: frank at chagford.com (Frank Millman) Date: Mon, 19 Dec 2011 08:46:28 +0200 Subject: Newbie Question: Obtain element from list of tuples References: <2c286dea-efba-45d5-85a7-a7503733575a@i6g2000vbe.googlegroups.com> <4eeea8eb$0$11091$c3e8da3@news.astraweb.com> Message-ID: "Steven D'Aprano" wrote in message news:4eeea8eb$0$11091$c3e8da3 at news.astraweb.com... > On Sun, 18 Dec 2011 18:35:47 -0800, alex23 wrote: > >> Pre-namedtuple, I used to like using named slices for this: >> >> cPID = slice(19) >> pid = recs[cPID] > > You know, that is an incredibly simple thing and yet it never dawned on > me before now. Thank you for sharing that. > I also like it, but it does not work quite so simply for me. >>> row = tuple(range(20)) >>> cPID = slice(15) >>> pid = row[cPID] >>> pid (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) >>> This works - >>> row = tuple(range(20)) >>> cPID = slice(15, 16) >>> pid, = row[cPID] # note the trailing comma >>> pid 15 >>> Still nice, but not quite so elegant. Am I missing something? Frank Millman From ian.g.kelly at gmail.com Mon Dec 19 01:57:43 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 18 Dec 2011 23:57:43 -0700 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> <4eebfbff$0$1679$742ec2ed@news.sonic.net> <17238811.518.1324198329306.JavaMail.geo-discussion-forums@yqio4> Message-ID: On Sun, Dec 18, 2011 at 6:27 PM, Tim Delaney wrote: > On 18 December 2011 19:52, RangerElf wrote: >> >> Which is why the original .acquire() ... .release() idiom was wrong, this >> would better express the intent: >> >> try: >> ?lock.acquire() >> ?shared_container.append(...) >> finally: >> ?lock.release() > > > No - this is very bad. The lock must be acquired outside the try: - > otherwise if an exception is thrown while acquiring, you will try to release > a lock that you have not acquired. > > Which again is why using with is a much better option - you can't make this > kind of mistake. Well, not unless you make the same mistake in the context manager itself. from contextlib import contextmanager @contextmanager def bad_context_manager(lock): try: lock.acquire() yield finally: lock.release() class Lock(object): def __init__(self): self.is_locked = False def acquire(self): assert False def release(self): assert self.is_locked, "Tried to release lock without acquiring it" with bad_context_manager(Lock()): pass Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__ return self.gen.next() File "", line 7, in bad_context_manager File "", line 7, in release AssertionError: Tried to release lock without acquiring it Perhaps a (small) reason to avoid the contextmanager decorator and implement __enter__ and __exit__ instead. From workitharder at gmail.com Mon Dec 19 03:09:19 2011 From: workitharder at gmail.com (buck) Date: Mon, 19 Dec 2011 00:09:19 -0800 (PST) Subject: Debugging a difficult refcount issue. In-Reply-To: References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: <13061655.1156.1324282159733.JavaMail.geo-discussion-forums@prkz16> This is what I came up with: https://gist.github.com/1496028 We'll see if it helps, tomorrow. On Sunday, December 18, 2011 6:01:50 PM UTC-8, buck wrote: > Thanks Jack. I think printf is what it will come down to. I plan to put a little code into PyDict_New to print the id and the line at which it was allocated. Hopefully this will show me all the possible suspects and I can figure it out from there. > > I hope figuring out the file and line-number from within that code isn't too hard. > > > On Sunday, December 18, 2011 9:52:46 AM UTC-8, Jack Diederich wrote: > > I don't have any great advice, that kind of issue is hard to pin down. > > That said, do try using a python compile with --with-debug enabled, > > with that you can turn your unit tests on and off to pinpoint where > > the refcounts are getting messed up. It also causes python to use > > plain malloc()s so valgrind becomes useful. Worst case add assertions > > and printf()s in the places you think are most janky. > > > > -Jack > > > > On Sat, Dec 17, 2011 at 11:17 PM, buck wrote: > > > I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. > > > > > > Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. > > > > > > I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. > > > > > > I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > > > > > How should I go about debugging this further? I've been completely stuck on this for two days now :( > > > > > > [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 > > > [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload > > > -- > > > http://mail.python.org/mailman/listinfo/python-list From workitharder at gmail.com Mon Dec 19 03:09:19 2011 From: workitharder at gmail.com (buck) Date: Mon, 19 Dec 2011 00:09:19 -0800 (PST) Subject: Debugging a difficult refcount issue. In-Reply-To: References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: <13061655.1156.1324282159733.JavaMail.geo-discussion-forums@prkz16> This is what I came up with: https://gist.github.com/1496028 We'll see if it helps, tomorrow. On Sunday, December 18, 2011 6:01:50 PM UTC-8, buck wrote: > Thanks Jack. I think printf is what it will come down to. I plan to put a little code into PyDict_New to print the id and the line at which it was allocated. Hopefully this will show me all the possible suspects and I can figure it out from there. > > I hope figuring out the file and line-number from within that code isn't too hard. > > > On Sunday, December 18, 2011 9:52:46 AM UTC-8, Jack Diederich wrote: > > I don't have any great advice, that kind of issue is hard to pin down. > > That said, do try using a python compile with --with-debug enabled, > > with that you can turn your unit tests on and off to pinpoint where > > the refcounts are getting messed up. It also causes python to use > > plain malloc()s so valgrind becomes useful. Worst case add assertions > > and printf()s in the places you think are most janky. > > > > -Jack > > > > On Sat, Dec 17, 2011 at 11:17 PM, buck wrote: > > > I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. > > > > > > Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. > > > > > > I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. > > > > > > I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > > > > > How should I go about debugging this further? I've been completely stuck on this for two days now :( > > > > > > [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 > > > [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload > > > -- > > > http://mail.python.org/mailman/listinfo/python-list From franck at ditter.org Mon Dec 19 04:15:52 2011 From: franck at ditter.org (Franck Ditter) Date: Mon, 19 Dec 2011 10:15:52 +0100 Subject: Can't get tilde character with IDLE 3.2.2 on French Mac Lion Message-ID: Hi ! All is in the subject. I'm starting to use Python with Idle 3.2.2 on MacOS-X Lion (French). I can't get "Option-N space" to provide the ~ char. I tried to go into the Keys preferences but I can't find "Option-N space" to modify its meaning. Its actual behavior is to merge lines of a paragraph. Thanks for help ! franck From fperez.net at gmail.com Mon Dec 19 04:49:02 2011 From: fperez.net at gmail.com (Fernando Perez) Date: Mon, 19 Dec 2011 09:49:02 +0000 (UTC) Subject: [ANN] IPython 0.12 is out! Message-ID: Hi all, on behalf of the IPython development team, I'm thrilled to announce, after an intense 4 1/2 months of work, the official release of IPython 0.12. This is a very important release for IPython, for several reasons. First and foremost, we have a major new feature, our interactive web-based notebook, that has been in our sights for a very long time. We tried to build one years ago (with WX) as a Google SoC project in 2005, had other prototypes later on, but things never quite worked. Finally the refactoring effort started two years ago, the communications architecture we built in 2010, and the advances of modern browsers, gave us all the necessary pieces. With this foundation in place, while part of the team worked on the 0.11 release, Brian Granger had already started quietly building the web notebook, which we demoed in early-alpha mode at the SciPy 2011 conference (http://www.archive.org/details/Wednesday-203-6- IpythonANewArchitectureForInteractiveAndParallel). By the EuroScipy conference in August we had merged Brian's amazing effort into our master branch, and after that multiple people (old and new) jumped in to make all kinds of improvements, leaving us today with something that is an excellent foundation. It's still the first release of the notebook, and as such we know it has a number of rough edges, but several of us have been using it as a daily research tool for the last few months. Do not hesitate to file issues for any problems you encounter with it, and we even have an 'open issue' for general discussion of ideas and features for the notebook at: https://github.com/ipython/ipython/issues/977. Furthermore, it is clear that our big refactoring work, combined with the amazing facilities at Github, are paying off. The 0.11 series was a major amount of work, with 511 issues closed over almost two years. But that pales in comparison to this cycle: in only 4 1/2 months we closed 515 issues, with 50% being Pull Requests. And very importantly, our list of contributors includes many new faces (see the credits section in our release notes for full details), which is the best thing that can happen to an open source project. We hope you will find the new features (the notebook isn't the only one! see below) compelling, and that many more will not only use IPython but will join the project; there's plenty to do and now there are tasks for many different skill sets (web, javascript, gui work, low-level networking, parallel machinery, console apps, etc). *Downloads* Download links and instructions are at: http://ipython.org/download.html And IPython is also on PyPI: http://pypi.python.org/pypi/ipython Those contain a built version of the HTML docs; if you want pure source downloads with no docs, those are available on github: Tarball: https://github.com/ipython/ipython/tarball/rel-0.12 Zipball: https://github.com/ipython/ipython/zipball/rel-0.12 * Features * Here is a quick listing of the major new features: - An interactive browser-based Notebook with rich media support - Two-process terminal console - Tabbed QtConsole - Full Python 3 compatibility - Standalone Kernel - PyPy support And many more... We closed over 500 tickets, merged over 200 pull requests, and more than 45 people contributed commits for the final release. Please see our release notes for the full details on everything about this release: http://ipython.org/ipython-doc/stable/whatsnew/version0.12.html * IPython tutorial at PyCon 2012 * Those of you attending (or planning on it) PyCon 2012 in Santa Clara, CA, may be interested in attending a hands-on tutorial we will be presenting on the many faces of IPython. See https://us.pycon.org/2012/schedule/ presentation/121/ for full details. * Errata * This was caught by Matthias Bussionnier's (one of our great new contributors) sharp eyes while I was writing these release notes: In the example notebook called display_protocol, the first cell starts with: from IPython.lib.pylabtools import print_figure which should instead be: from IPython.core.pylabtools import print_figure This has already been fixed on master, but since the final 0.12 files have been uploaded to github and PyPI, we'll let them be. As usual, if you find any other problem, please file a ticket --or even better, a pull request fixing it-- on our github issues site (https:// github.com/ipython/ipython/issues/). Many thanks to all who contributed! Fernando, on behalf of the IPython development team. http://ipython.org From hoogendoorn.eelco at gmail.com Mon Dec 19 05:15:11 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 19 Dec 2011 02:15:11 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> Message-ID: On Dec 19, 3:23?am, alex23 wrote: > Evan Driscoll wrote: > > My problem with it is that it in some sense is forcing me to make a > > decision I don't care about. Yes, what we have now is less flexible, but > > I have *never* said "man, I wish this *args parameter were a list > > instead of a tuple". > > And if you _did_, then one of the first lines in your function would > be: > > ? ? args = list(args) > > Which is obvious to everyone, doesn't modify existing behaviour, > doesn't force everyone without a fetish for change to add unnecessary > cruft to their function signature... Its obvious you end up with a list (assuming args is an iterable); knowing what args was to begin with suffers from the same problems. > Except, OMG, list() is RETURNING A LIST, which is an OBVIOUS type > constraint. I propose that: > > ? ? args = @set list(args) > > Will coerce args into a list and then give me a set in return. ? What does that have to do with collection packing/unpacking? From hoogendoorn.eelco at gmail.com Mon Dec 19 05:20:33 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 19 Dec 2011 02:20:33 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> Message-ID: <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> On Dec 19, 1:59?am, Rick Johnson wrote: > On Dec 17, 11:33?pm, Evan Driscoll wrote: > > > On 12/17/2011 22:52, buck wrote:> Try these on for size. > > > > ? ? ?head, @tuple tail = sequence > > > ? ? ?def foo(@list args, @dict kwargs): pass > > > ? ? ?foo(@args, @kwargs) > > > > For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. > > > I like this idea much more than the original one. > > +1. I will second that! Eelco has the CORRECT idea, but the WRONG > syntax! Thanks, your opinion is noted. Personally im impartial between identifier::collectiontype and identifier at collectiontype, but that order is something I think is rather important. Having two seperate symbols seperated by whitespace, as in @list args strikes me as a terrible break of normal python lexical rules. Plus, identifier at collectiontype as a collection-type annotation would be consistent with the existing general function annotation syntax. Many non-C-family languages postfix the type declaration. Also, we want to use the same symbol for collection unpacking as we use for collection packing. Saying foo(@argslist) really does look a tad much like a decorator, even though it can be unambigiously distinguished from it by context. From ralf at systemexit.de Mon Dec 19 05:26:00 2011 From: ralf at systemexit.de (Ralf Schmitt) Date: Mon, 19 Dec 2011 11:26:00 +0100 Subject: [ANNOUNCE] greenlet 0.3.2 Message-ID: <87wr9skgcn.fsf@winserver.brainbot.com> Hi, I have uploaded greenlet 0.3.2 to PyPI: http://pypi.python.org/pypi/greenlet What is it? ----------- The greenlet module provides coroutines for python. coroutines allow suspending and resuming execution at certain locations. concurrence[1], eventlet[2] and gevent[3] use the greenlet module in order to implement concurrent network applications. Documentation can be found here: http://packages.python.org/greenlet/ The code is hosted on github: https://github.com/python-greenlet/greenlet Changes in version 0.3.2 ------------------------ The NEWS file lists these changes for release 0.3.2: * fix various crashes with recent gcc versions and VC90 * Try to fix stack save/restore on arm32 * store and restore the threadstate on exceptions like pypy/stackless do * Switch to using PyCapsule for Python 2.7 and 3.1 * Port for AIX on PowerPC * Fix the sparc/solaris header * Improved build dependencies patch from flub. * #21 - Can't pass parent=None to greenlet.greenlet() [1] http://opensource.hyves.org/concurrence/ [2] http://eventlet.net/ [3] http://www.gevent.org/ -- Cheers Ralf Schmitt From lie.1296 at gmail.com Mon Dec 19 05:54:37 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 19 Dec 2011 21:54:37 +1100 Subject: how to run python-script from the python promt? [absolute newbie] In-Reply-To: References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: On 12/19/2011 12:16 AM, nukeymusic wrote: > On 18 dec, 13:39, Lie Ryan wrote: >> On 12/18/2011 10:00 PM, nukeymusic wrote: >> >> >> >> >> >> >> >> >> >>> How can I load a python-script after starting python in the >>> interactive mode? >>> I tried with >>>>>> load 'myscript.py' >>>>>> myscript.py >>>>>> myscript >> >>> but none of these works, so the only way I could work further until >>> now was copy/paste line per line of my python-script to the >>> interactive mode prompt >>> I do know how to run the script non-interactively, but what I want to >>> do is adding lines to the code I have written thus far in interactive >>> mode. >> >>> thanks in advance >>> nukey >> >> The normal python shell doesn't directly support doing that, although >> there are several workaround with (ab)using the 'import' statement, it >> had several subtleties with how module are cached. Try the ipython >> shell; in ipython you can load a file into the current interpreter >> session using the %run magic command. > > I guess you mean the following command? > %run 'myscript.py' > > is this correct? yes From ulrich.eckhardt at dominolaser.com Mon Dec 19 08:16:42 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Mon, 19 Dec 2011 14:16:42 +0100 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: Am 16.12.2011 18:48, schrieb Nathan Rice: > I realize this has been discussed in the past, I hope that I am > presenting a slightly different take on the subject that will prove > interesting. This is primarily motivated by my annoyance with using > comprehensions in certain circumstances. [...] > Having "typed" lists let you take preexisting string/int/etc methods > and expose them in a vectorized context and provides an easy way for > developers to support both vectors and scalars in a single function > (you could easily "fix" other people's functions dynamically to > support both). Additionally, "typed" lists/iterators will allow > improved code analysis and optimization. The PyPy people have > already stated that they are working on implementing different > strategies for lists composed of a single type, so clearly there is > already community movement in this direction. > > Just compare the above examples to their type-aware counterparts: > > L2 = X(L1) L2 = L1.X() > > L2 = Z(Y(X(L1))) L2 = L1.X().Y().Z() > > Also, this would provide a way to clean up stuff like: > > "\n".join(l.capitalize() for l in my_string.split("\n")) > > into: > > my_string.split("\n").capitalize().join_this("\n") > > Before anyone gets up in arms at the idea of statically typed > python, what I am suggesting here would be looser than that. > Basically, I believe it would be a good idea in instances where it is > known that a list of single type is going to be returned, to return a > list subclass (for example, StringList, IntegerList, etc). To avoid > handcuffing people with types, the standard list modification methods > could be hooked so that if an object of an incorrect type is placed > in the list, a warning is raised and the list converts to a generic > object list. The only stumbling block is that you can't use > __class__ to convert from stack types to heap types in CPython. My > workaround for this would be to have a factory that creates generic > "List" classes, modifying the bases to produce the correct behavior. > Then, converting from a typed list to a generic object list would > just be a matter of removing a member from the bases for a class. > This of course basically kills the ability to perform type specific > list optimization in CPython, but that isn't necessarily true for > other implementations. The additional type information would be > preserved for code analysis in any case. The case would be even > simpler for generators and other iterators, as you don't have to > worry about mutation. > > I'd like to hear people's thoughts on the subject. Currently we are > throwing away useful information in many cases that could be used > for code analysis, optimization and simpler interfaces. I think there are two aspects to your idea: 1. collections that share a single type 2. accessing multiple elements via a common interface Both are things that should be considered and I think both are useful in some contexts. The former would provide additional guarantees, for example, you could savely look up an attribute of the type only once while iterating over the sequence and use it for all elements. Also, I believe you could save some storage. The second aspect would mean that you have a single function call that targets multiple objects, which is syntactic sugar, but that's a good thing. To some extent, this looks like a C++ valarray, see e.g. [1] and [2] (note that I don't trust [1] and that [2] is perhaps a bit outdated), in case you know C++ and want to draw some inspiration from this. Anyway, I believe something like that would already be possible today, which would give people something they could actually try out instead of just musing about: class ValarrayWrapper(object): def __init__(self, elements): self._elements = elements def map(self, function): tmp = [function(x) for x in self._elements] return ValarrayWrapper(tmp) def apply(self, function) self._elements[:] = [function(x) for x in self._elements] I could even imagine this to implement "generic" attribute lookup by looking at the first element. If it contains the according attribute, return a proxy that allows calls to member functions or property access, depending on the type of the attribute. > I believe that "typed" lists that get "demoted" to normal lists with > a warning on out of type operations preserve this information while > providing complete backwards compatibility and freedom. I don't think that a warning helps people write correct code, a meaningful error does. Otherwise, with the same argument you could convert a tuple on the fly to a list when someone tries to change an element of it. Cheers! Uli [1] http://www.cplusplus.com/reference/std/valarray/abs/ [2] http://drdobbs.com/184403620 From lancee.ware at gmail.com Mon Dec 19 09:26:36 2011 From: lancee.ware at gmail.com (Overo) Date: Mon, 19 Dec 2011 06:26:36 -0800 (PST) Subject: Pycrypto AES CTR Message-ID: <58e00ba7-d4cc-4eb3-9325-94256aa37ee0@u5g2000vbd.googlegroups.com> Hello, I am writing an application that will decrypt an AES file with a counter attached at the beginning offset. I see that in the CTR mode. Pycrypto Seems to use the top 16-bytes as random data and perpends zero's at the bottom end. For the sake of more security, I would like to provide a counter with a 32-bits of unique's in order to give it more security. I wanted to know if 1) Will I be able to use the Counter class provided with pycrypto? 2) If not what call does pycrpto use to increment the counter so I can provide my own implementation. Thanks From duncan.booth at invalid.invalid Mon Dec 19 10:23:14 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 19 Dec 2011 15:23:14 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: Evan Driscoll wrote: >> I'm not an expert on Google's syntax, but if you search for "python, >> optionally with function", isn't that the same as just searching for >> "python" since it will return hits either with or without "function"? > Chris Angelico's interpretation is correct: I did four searches, "python > function star", "python function asterisk", "python star", and "python > asterisk". > Using www.google.com: "python function star": first hit is: http://farmdev.com/thoughts/24/what- does-the-def-star-variable-or-def-asterisk-parameter-syntax-do-in-python-/ "python function asterisk": first hit is: http://www.technovelty.org/code/python/asterisk.html "python asterisk": the first hit I get is http://www.technovelty.org/code/python/asterisk.html "python star" is the only useless one of those giving me two hits for star pathfinding algorithms, one about the BNF used in Python's documentation, and a Daily Mail article about John Cleese and Eric Idle. -- Duncan Booth http://kupuguy.blogspot.com From devplayer at gmail.com Mon Dec 19 10:37:58 2011 From: devplayer at gmail.com (DevPlayer) Date: Mon, 19 Dec 2011 07:37:58 -0800 (PST) Subject: Newbie Question: Obtain element from list of tuples References: <2c286dea-efba-45d5-85a7-a7503733575a@i6g2000vbe.googlegroups.com> <4eeea8eb$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Dec 19, 1:46?am, "Frank Millman" wrote: > "Steven D'Aprano" wrote in message > > news:4eeea8eb$0$11091$c3e8da3 at news.astraweb.com... > > > On Sun, 18 Dec 2011 18:35:47 -0800, alex23 wrote: > > >> Pre-namedtuple, I used to like using named slices for this: > > >> ? ? cPID = slice(19) > >> ? ? pid = recs[cPID] > > > You know, that is an incredibly simple thing and yet it never dawned on > > me before now. Thank you for sharing that. > > I also like it, but it does not work quite so simply for me. > > >>> row = tuple(range(20)) > >>> cPID = slice(15) > >>> pid = row[cPID] > >>> pid > > (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) > > > > This works - > > > > >>> row = tuple(range(20)) > >>> cPID = slice(15, 16) > >>> pid, = row[cPID] ?# note the trailing comma > >>> pid > 15 > > Still nice, but not quite so elegant. > > Am I missing something? > > Frank Millman if you're going to do: > >>> cPID = slice(15, 16) > >>> pid, = row[cPID] # note the trailing comma Why not just: >>>row = tuple(range(20)) >>>cPID = 15 # a proposed constant >>>pid = row[cPID] >>>pid 15 It might be better for other's to debug if you use >>> pid, _ = row[cPID] as a little tiny comma like that is easy to miss in some text editors. Not everyone has large LCD screens. Not that I like the "_" for this purpose. Personally I'd love to see the "_" used instead of "self" as a common practice (useful when you use lots of "self...." on a line). From duncan.booth at invalid.invalid Mon Dec 19 10:46:12 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 19 Dec 2011 15:46:12 GMT Subject: re.sub(): replace longest match instead of leftmost match? References: Message-ID: MRAB wrote: > On 16/12/2011 21:04, John Gordon wrote: >> In Devin >> Jeanpierre writes: >> >>> You could use re.finditer to find the longest match, and then >>> replace it manually by hand (via string slicing). >> >>> (a match is the longest if (m.end() - m.start()) is the largest -- >>> so, max(re.finditer(...), key=3Dlambda m: (m.end() =3D m.start())) >> >> I ended up doing something similar: >> >> # find the longest match >> longest_match = '' >> for word in re.findall('((0000:?)+)', ip6): >> if len(word[0])> len(longest_match): >> longest_match = word[0] >> >> # if we found a match, replace it with a colon >> if longest_match: >> ip6 = re.sub(longest_match, ':', ip6, 1) >> > For a simple replace, using re is probably overkill. The .replace > method is a better solution: > > ip6 = longest_match.replace(ip6, ':', 1) I think you got longest_match/ip6 backwards there. Anyway, for those who like brevity: try: ip6 = ip6.replace(max(re.findall('((?:0000:?)+)', ip6), key=len), ':', 1) except ValueError: pass -- Duncan Booth http://kupuguy.blogspot.com From nathan.alexander.rice at gmail.com Mon Dec 19 10:56:21 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Mon, 19 Dec 2011 10:56:21 -0500 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: > I think there are two aspects to your idea: > 1. collections that share a single type > 2. accessing multiple elements via a common interface You are correct, and I now regret posing them in a coupled manner. > Both are things that should be considered and I think both are useful in > some contexts. The former would provide additional guarantees, for example, > you could savely look up an attribute of the type only once while iterating > over the sequence and use it for all elements. Also, I believe you could > save some storage. > > The second aspect would mean that you have a single function call that > targets multiple objects, which is syntactic sugar, but that's a good thing. > To some extent, this looks like a C++ valarray, see e.g. [1] and [2] (note > that I don't trust [1] and that [2] is perhaps a bit outdated), in case you > know C++ and want to draw some inspiration from this. > > Anyway, I believe something like that would already be possible today, which > would give people something they could actually try out instead of just > musing about: > > ? class ValarrayWrapper(object): > ? ? ? def __init__(self, elements): > ? ? ? ? ? self._elements = elements > ? ? ? def map(self, function): > ? ? ? ? ? tmp = [function(x) for x in self._elements] > ? ? ? ? ? return ValarrayWrapper(tmp) > ? ? ? def apply(self, function) > ? ? ? ? ? self._elements[:] = [function(x) for x in self._elements] > > I could even imagine this to implement "generic" attribute lookup by looking > at the first element. If it contains the according attribute, return a proxy > that allows calls to member functions or property access, depending on the > type of the attribute. Thank you for the references, I am always interested to see how other languages solve problems. I have received the "code please" comment repeatedly, I will have to take some time after work today to deliver. >> I believe that "typed" lists that get "demoted" to normal lists with >> a warning on out of type operations preserve this information while >> providing complete backwards compatibility and freedom. > > > I don't think that a warning helps people write correct code, a meaningful > error does. Otherwise, with the same argument you could convert a tuple on > the fly to a list when someone tries to change an element of it. I do agree errors are more normative than warnings. The problem with an error in these circumstances is it will certainly break code somewhere. Perhaps a warning that becomes an error at some point in the future would be the prudent way to go. Thanks! Nathan From wanderer at dialup4less.com Mon Dec 19 12:12:17 2011 From: wanderer at dialup4less.com (Wanderer) Date: Mon, 19 Dec 2011 09:12:17 -0800 (PST) Subject: IPython 0.12 is out! References: Message-ID: <1331f71b-b8c6-475c-af22-13c79cfa0471@o9g2000yqa.googlegroups.com> On Dec 19, 4:49?am, Fernando Perez wrote: > Hi all, > > on behalf of the IPython development team, I'm thrilled to announce, after > an intense 4 1/2 months of work, the official release of IPython 0.12. > > This is a very important release for IPython, for several reasons. First > and foremost, we have a major new feature, our interactive web-based > notebook, that has been in our sights for a very long time. ?We tried to > build one years ago (with WX) as a Google SoC project in 2005, had other > prototypes later on, but things never quite worked. ?Finally the > refactoring effort started two years ago, the communications architecture > we built in 2010, and the advances of modern browsers, gave us all the > necessary pieces. > > With this foundation in place, while part of the team worked on the 0.11 > release, Brian Granger had already started quietly building the web > notebook, which we demoed in early-alpha mode at the SciPy 2011 conference > (http://www.archive.org/details/Wednesday-203-6- > IpythonANewArchitectureForInteractiveAndParallel). ?By the EuroScipy > conference in August we had merged Brian's amazing effort into our master > branch, and after that multiple people (old and new) jumped in to make all > kinds of improvements, leaving us today with something that is an > excellent foundation. ?It's still the first release of the notebook, and > as such we know it has a number of rough edges, but several of us have > been using it as a daily research tool for the last few months. ?Do not > hesitate to file issues for any problems you encounter with it, and we > even have an 'open issue' for general discussion of ideas and features for > the notebook at:https://github.com/ipython/ipython/issues/977. > > Furthermore, it is clear that our big refactoring work, combined with the > amazing facilities at Github, are paying off. ?The 0.11 series was a major > amount of work, with 511 issues closed over almost two years. But that > pales in comparison to this cycle: in only 4 1/2 months we closed 515 > issues, with 50% being Pull Requests. And very importantly, our list of > contributors includes many new faces (see the credits section in our > release notes for full details), which is the best thing that can happen > to an open source project. > > We hope you will find the new features (the notebook isn't the only one! > see below) compelling, and that many more will not only use IPython but > will join the project; there's plenty to do and now there are tasks for > many different skill sets (web, javascript, gui work, low-level > networking, parallel machinery, console apps, etc). > > *Downloads* > > Download links and instructions are at:http://ipython.org/download.html > And IPython is also on PyPI:http://pypi.python.org/pypi/ipython > > Those contain a built version of the HTML docs; if you want pure source > downloads with no docs, those are available on github: > > Tarball:https://github.com/ipython/ipython/tarball/rel-0.12 > Zipball:https://github.com/ipython/ipython/zipball/rel-0.12 > > * Features * > Here is a quick listing of the major new features: > > - An interactive browser-based Notebook with rich media support > - Two-process terminal console > - Tabbed QtConsole > - Full Python 3 compatibility > - Standalone Kernel > - PyPy support > > And many more... > > We closed over 500 tickets, merged over 200 pull requests, and more than > 45 people contributed commits for the final release. > > Please see our release notes for the full details on everything about this > release:http://ipython.org/ipython-doc/stable/whatsnew/version0.12.html > > * IPython tutorial at PyCon 2012 * > > Those of you attending (or planning on it) PyCon 2012 in Santa Clara, CA, > may be interested in attending a hands-on tutorial we will be presenting > on the many faces of IPython. ?Seehttps://us.pycon.org/2012/schedule/ > presentation/121/ for full details. > > * Errata * > This was caught by Matthias Bussionnier's (one of our great new > contributors) sharp eyes while I was writing these release notes: In the > example notebook called display_protocol, the first cell starts with: > > from IPython.lib.pylabtools import print_figure > > which should instead be: > > from IPython.core.pylabtools import print_figure > > This has already been fixed on master, but since the final 0.12 files have > been uploaded to github and PyPI, we'll let them be. > > As usual, if you find any other problem, please file a ticket --or even > better, a pull request fixing it-- on our github issues site (https:// > github.com/ipython/ipython/issues/). > > Many thanks to all who contributed! > > Fernando, on behalf of the IPython development team. > > http://ipython.org Doesn't work for me. I installed it over my IPython 0.11 which did work for me. I get the following complaint. Traceback (most recent call last): File "C:\Python27\scripts\ipython.py", line 26, in (module) import Ipython.Shell File "C:\Python27\lib\site-packages\Ipython\Shell.py", line 38 in (module) from Ipython.Magic import Magic File "C:\Python27\lib\site-packages\Ipython\Magic.py", line 536, in Magic @testdec.skip_doctest AttributeError: 'module' object has no attribute 'skip_doctest' From tycho at tycho.ws Mon Dec 19 12:13:19 2011 From: tycho at tycho.ws (Tycho Andersen) Date: Mon, 19 Dec 2011 11:13:19 -0600 Subject: Getting a patch accepted Message-ID: <20111219171319.GB16551@smitten> Hi all, A couple months ago I found a bug in a corner of the curses library (http://bugs.python.org/issue13051) and filed it. Unfortunately, there was nobody listed to cc on the noisy list, so it probably got lost in the shuffle. (There is even previous mention of this bug elsewhere on the tracker, and on stackoverflow, so it does affect some small number of people.) Is there anyone else I can bother to get this patch applied? Thanks! \t From wanderer at dialup4less.com Mon Dec 19 13:07:21 2011 From: wanderer at dialup4less.com (Wanderer) Date: Mon, 19 Dec 2011 10:07:21 -0800 (PST) Subject: IPython 0.12 is out! References: <1331f71b-b8c6-475c-af22-13c79cfa0471@o9g2000yqa.googlegroups.com> Message-ID: On Dec 19, 12:12?pm, Wanderer wrote: > On Dec 19, 4:49?am, Fernando Perez wrote: > > > > > > > > > > > Hi all, > > > on behalf of the IPython development team, I'm thrilled to announce, after > > an intense 4 1/2 months of work, the official release of IPython 0.12. > > > This is a very important release for IPython, for several reasons. First > > and foremost, we have a major new feature, our interactive web-based > > notebook, that has been in our sights for a very long time. ?We tried to > > build one years ago (with WX) as a Google SoC project in 2005, had other > > prototypes later on, but things never quite worked. ?Finally the > > refactoring effort started two years ago, the communications architecture > > we built in 2010, and the advances of modern browsers, gave us all the > > necessary pieces. > > > With this foundation in place, while part of the team worked on the 0.11 > > release, Brian Granger had already started quietly building the web > > notebook, which we demoed in early-alpha mode at the SciPy 2011 conference > > (http://www.archive.org/details/Wednesday-203-6- > > IpythonANewArchitectureForInteractiveAndParallel). ?By the EuroScipy > > conference in August we had merged Brian's amazing effort into our master > > branch, and after that multiple people (old and new) jumped in to make all > > kinds of improvements, leaving us today with something that is an > > excellent foundation. ?It's still the first release of the notebook, and > > as such we know it has a number of rough edges, but several of us have > > been using it as a daily research tool for the last few months. ?Do not > > hesitate to file issues for any problems you encounter with it, and we > > even have an 'open issue' for general discussion of ideas and features for > > the notebook at:https://github.com/ipython/ipython/issues/977. > > > Furthermore, it is clear that our big refactoring work, combined with the > > amazing facilities at Github, are paying off. ?The 0.11 series was a major > > amount of work, with 511 issues closed over almost two years. But that > > pales in comparison to this cycle: in only 4 1/2 months we closed 515 > > issues, with 50% being Pull Requests. And very importantly, our list of > > contributors includes many new faces (see the credits section in our > > release notes for full details), which is the best thing that can happen > > to an open source project. > > > We hope you will find the new features (the notebook isn't the only one! > > see below) compelling, and that many more will not only use IPython but > > will join the project; there's plenty to do and now there are tasks for > > many different skill sets (web, javascript, gui work, low-level > > networking, parallel machinery, console apps, etc). > > > *Downloads* > > > Download links and instructions are at:http://ipython.org/download.html > > And IPython is also on PyPI:http://pypi.python.org/pypi/ipython > > > Those contain a built version of the HTML docs; if you want pure source > > downloads with no docs, those are available on github: > > > Tarball:https://github.com/ipython/ipython/tarball/rel-0.12 > > Zipball:https://github.com/ipython/ipython/zipball/rel-0.12 > > > * Features * > > Here is a quick listing of the major new features: > > > - An interactive browser-based Notebook with rich media support > > - Two-process terminal console > > - Tabbed QtConsole > > - Full Python 3 compatibility > > - Standalone Kernel > > - PyPy support > > > And many more... > > > We closed over 500 tickets, merged over 200 pull requests, and more than > > 45 people contributed commits for the final release. > > > Please see our release notes for the full details on everything about this > > release:http://ipython.org/ipython-doc/stable/whatsnew/version0.12.html > > > * IPython tutorial at PyCon 2012 * > > > Those of you attending (or planning on it) PyCon 2012 in Santa Clara, CA, > > may be interested in attending a hands-on tutorial we will be presenting > > on the many faces of IPython. ?Seehttps://us.pycon.org/2012/schedule/ > > presentation/121/ for full details. > > > * Errata * > > This was caught by Matthias Bussionnier's (one of our great new > > contributors) sharp eyes while I was writing these release notes: In the > > example notebook called display_protocol, the first cell starts with: > > > from IPython.lib.pylabtools import print_figure > > > which should instead be: > > > from IPython.core.pylabtools import print_figure > > > This has already been fixed on master, but since the final 0.12 files have > > been uploaded to github and PyPI, we'll let them be. > > > As usual, if you find any other problem, please file a ticket --or even > > better, a pull request fixing it-- on our github issues site (https:// > > github.com/ipython/ipython/issues/). > > > Many thanks to all who contributed! > > > Fernando, on behalf of the IPython development team. > > >http://ipython.org > > Doesn't work for me. I installed it over my IPython 0.11 which did > work for me. I get the following complaint. > > Traceback (most recent call last): > ?File "C:\Python27\scripts\ipython.py", line 26, in (module) > ? import Ipython.Shell > ?File "C:\Python27\lib\site-packages\Ipython\Shell.py", line 38 in > (module) > ? from Ipython.Magic import Magic > ?File "C:\Python27\lib\site-packages\Ipython\Magic.py", line 536, in > Magic > ? @testdec.skip_doctest > AttributeError: 'module' object has no attribute 'skip_doctest' The windows installer didn't work but installing from the tar file did. But installing from the tar file doesn't install Ipython in the site-packages directory. It installs it wherever you untar the tar file. I don't remember ever having to deal with this before. Most things just install in the site-packages directory without me having to do anything special. From bex.lewis at gmail.com Mon Dec 19 14:05:16 2011 From: bex.lewis at gmail.com (becky_lewis) Date: Mon, 19 Dec 2011 11:05:16 -0800 (PST) Subject: IPython 0.12 is out! References: Message-ID: Thanks and congratulations! Installed via pip --upgrade and everything seems to be working just fine (python 2.7.0+ on Linux) I look forward to investigating all of the new features! Becky Lewis On Dec 19, 9:49?am, Fernando Perez wrote: > Hi all, > > on behalf of the IPython development team, I'm thrilled to announce, after > an intense 4 1/2 months of work, the official release of IPython 0.12. > ... snip ... From nad at acm.org Mon Dec 19 14:19:02 2011 From: nad at acm.org (Ned Deily) Date: Mon, 19 Dec 2011 14:19:02 -0500 Subject: Can't get tilde character with IDLE 3.2.2 on French Mac Lion References: Message-ID: In article , Franck Ditter wrote: > All is in the subject. I'm starting to use Python with Idle 3.2.2 > on MacOS-X Lion (French). I can't get "Option-N space" to provide > the ~ char. > I tried to go into the Keys preferences but I can't find "Option-N space" > to modify its meaning. Its actual behavior is to merge lines of a > paragraph. You are likely running into a current problem in the OS X Cocoa version of Tcl/Tk 8.5 as included with Lion and as shipped by ActiveState. Previously, if you tried to type composite characters, like Option N, the Cocoa Tcl/Tk would crash. Pending a real fix, a patch was made to Tcl/Tk 8.5 to discard composite characters rather than crash. You should be able to get a tilde by using the post-composite keyboard sequence: try typing "space" followed by "Shift-Option-N". http://sourceforge.net/tracker/index.php?func=detail&aid=2907388&group_id =12997&atid=112997 -- Ned Deily, nad at acm.org From irmen at -NOSPAM-xs4all.nl Mon Dec 19 14:39:25 2011 From: irmen at -NOSPAM-xs4all.nl (Irmen de Jong) Date: Mon, 19 Dec 2011 20:39:25 +0100 Subject: Getting a patch accepted In-Reply-To: References: Message-ID: <4eef92ed$0$6932$e4fe514c@news2.news.xs4all.nl> On 19-12-11 18:13, Tycho Andersen wrote: > Hi all, > > A couple months ago I found a bug in a corner of the curses library > (http://bugs.python.org/issue13051) and filed it. Unfortunately, there > was nobody listed to cc on the noisy list, so it probably got lost in > the shuffle. (There is even previous mention of this bug elsewhere on > the tracker, and on stackoverflow, so it does affect some small number > of people.) > > Is there anyone else I can bother to get this patch applied? > Maybe the folks over at #python-dev on freenode can help you out? Also the dev guide has some hints on the 'lifecycle of a patch' http://docs.python.org/devguide/patch.html Irmen From rantingrickjohnson at gmail.com Mon Dec 19 16:24:11 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Mon, 19 Dec 2011 13:24:11 -0800 (PST) Subject: Getting a patch accepted References: Message-ID: On Dec 19, 11:13?am, Tycho Andersen wrote: > Hi all, > > A couple months ago I found a bug in a corner of the curses library > (http://bugs.python.org/issue13051) and filed it. Unfortunately, there > was nobody listed to cc on the noisy list, so it probably got lost in > the shuffle. (There is even previous mention of this bug elsewhere on > the tracker, and on stackoverflow, so it does affect some small number > of people.) > > Is there anyone else I can bother to get this patch applied? My friend, it seems as though you've exhausted all avenues and explored all options. Now you need to contact upper management. I'd dare say you should go further! Yes. I'd say you should contact the great Guido van Rossum himself and bring this atrocious patching system to his attention. If no one in the community has the time to patch a bug in the source, then by god GvR needs to step forth and earn that "Benevolent" scout patch. I mean, he has the 20% time from Google for which he can do whatever he likes. What did he think, we were going to let him veg out on the couch watching re-runs of Monty Pythons Flying Circus! He'll end up like Shatner for christ sakes! I want to see him mingling with the masses and manning the trenches. I want to see him shaking hands and kissing babies! From brian at python.org Mon Dec 19 16:37:43 2011 From: brian at python.org (Brian Curtin) Date: Mon, 19 Dec 2011 15:37:43 -0600 Subject: Getting a patch accepted In-Reply-To: <4eef92ed$0$6932$e4fe514c@news2.news.xs4all.nl> References: <4eef92ed$0$6932$e4fe514c@news2.news.xs4all.nl> Message-ID: On 19-12-11 18:13, Tycho Andersen wrote: > > Hi all, > > A couple months ago I found a bug in a corner of the curses library > (http://bugs.python.org/issue13051) and filed it. Unfortunately, there > was nobody listed to cc on the noisy list, so it probably got lost in > the shuffle. (There is even previous mention of this bug elsewhere on > the tracker, and on stackoverflow, so it does affect some small number > of people.) > > Is there anyone else I can bother to get this patch applied? Part of the issue with this taking a while is that there's no one on the committer side of things that deals with curses. For one thing, I see that the patch has no tests, so you might want to add unit tests to ensure that your changes don't break anything else. I left a comment saying that on the issue, so I'll get any updates you make and can look at them. Rick Johnson's response is, as always, pure trolling. I'd suggest you ignore him. From bahamutzero8825 at gmail.com Mon Dec 19 16:57:59 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Mon, 19 Dec 2011 15:57:59 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: <4EEFB367.7030408@gmail.com> On 12/18/2011 8:16 PM, Rick Johnson wrote: > On Dec 18, 7:26 pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> Not everybody uses editors more advanced than Notepad. > And they have no excuse for NOT using a better one. Well, except for a > "foolish consistency" that is! But what about the example he gave about being logged into a customer's machine with only ed available? I suppose such fools would not be worthy of your business. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From Juan.Declet-Barreto at MesaAZ.gov Mon Dec 19 17:02:50 2011 From: Juan.Declet-Barreto at MesaAZ.gov (Juan Declet-Barreto) Date: Mon, 19 Dec 2011 15:02:50 -0700 Subject: the slash & Windows paths Message-ID: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> All, I have a Windows-style path that I need to modify so Python functions can find it. For example, the path "C:\Projects\Population_Pyramids\charts\test.xls" is being interpreted as pointing to a file called "est.xls" since the "t" is being escaped by the preceding slash as a tab. I know this is straightforward to handle using raw literals by prefixing the literal with an 'r', but my path is stored in a variable. My questions are: 1. How do I prevent single slashes from being escaped in a string variable (NOT literal)? 2. Is it possible to do a string.replace to substitute all instances of '\' for '\\'? I tried this but it did not give me the desired result. Juan Declet-Barreto [cid:image004.png at 01CCBE5F.46845530] GIS Specialist, Information Technology Dept. City of Mesa Office: 480.644.4751 juan.declet-barreto at mesaaz.gov [cid:image003.png at 01CCBE5E.C7AA1EC0] -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image003.png Type: image/png Size: 11402 bytes Desc: image003.png URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image004.png Type: image/png Size: 1454 bytes Desc: image004.png URL: From bahamutzero8825 at gmail.com Mon Dec 19 17:03:14 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Mon, 19 Dec 2011 16:03:14 -0600 Subject: Getting a patch accepted In-Reply-To: References: Message-ID: <4EEFB4A2.5030205@gmail.com> On 12/19/2011 3:24 PM, Rick Johnson wrote: > ... > I want to see him mingling with > the masses and manning the trenches. I want to see him shaking hands > and kissing babies! I prefer these over-the-top responses over the more subtle ones. Much more entertaining IMO. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From brian at python.org Mon Dec 19 17:26:52 2011 From: brian at python.org (Brian Curtin) Date: Mon, 19 Dec 2011 16:26:52 -0600 Subject: the slash & Windows paths In-Reply-To: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> References: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> Message-ID: On Mon, Dec 19, 2011 at 16:02, Juan Declet-Barreto < Juan.Declet-Barreto at mesaaz.gov> wrote: > All,**** > > ** ** > > I have a Windows-style path that I need to modify so Python functions can > find it. For example, the path > ?C:\Projects\Population_Pyramids\charts\test.xls? is being interpreted as > pointing to a file called ?est.xls? since the ?t? is being escaped by the > preceding slash as a tab. **** > > ** ** > > I know this is straightforward to handle using raw literals by prefixing > the literal with an ?r?, but my path is stored in a variable. **** > > ** ** > > My questions are: 1. How do I prevent single slashes from being escaped in > a string variable (NOT literal)? **** > > 2. Is it possible to do a > string.replace to substitute all instances of ?\? for ?\\?? I tried this > but it did not give me the desired result. > Use os.path.normpath(your_path). -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Mon Dec 19 17:32:13 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Mon, 19 Dec 2011 16:32:13 -0600 Subject: the slash & Windows paths In-Reply-To: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> References: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> Message-ID: <4EEFBB6D.8020900@gmail.com> On 12/19/2011 4:02 PM, Juan Declet-Barreto wrote: > I have a Windows-style path that I need to modify so Python functions > can find it. For example, the path > ?C:\Projects\Population_Pyramids\charts\test.xls? is being interpreted > as pointing to a file called ?est.xls? since the ?t? is being escaped by > the preceding slash as a tab. Whatever code is getting the path and storing it into a variable needs to escape the path. Otherwise, you'll have to replace each tab/newline/etc. with literal \t/\n/\r/etc., and that's ugly and could lead to problems (you may have special characters you want to keep). The reason your replace code didn't work is because the \t is a tab character, not a literal "\t". The replace function doesn't see "\t", but rather a tab. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From ting at thsu.org Mon Dec 19 18:15:06 2011 From: ting at thsu.org (ting at thsu.org) Date: Mon, 19 Dec 2011 15:15:06 -0800 (PST) Subject: re.sub(): replace longest match instead of leftmost match? References: Message-ID: On Dec 16, 11:49?am, John Gordon wrote: > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(0000:|0000$)+" with ":". ?But when I use re.sub() it replaces > the leftmost match, even if there is a longer match later in the string. Typically this means that your regular expression is not specific enough. That is, if you get multiple matches, and you need to sort through those matches before performing a replace, it usually means that you should rewrite your expression to get a single match. Invariably this happens when you try to take short cuts. I can't blame you for using a short cut, as sometimes short cuts just work, but once you find that your short cut fails, you need to step back and rethink the problem, rather than try to hack your short cut. I don't know what you are doing, but off the top of my head, I'd check to see if the CIDR string is wrapped in a header message and include the header as part of the search pattern, or if you know the IPv6 strings are interspersed with IPv4 strings, I would rewrite the regex to exclude IPv4 strings. -- // T.Hsu From miki.tebeka at gmail.com Mon Dec 19 18:18:19 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Mon, 19 Dec 2011 15:18:19 -0800 (PST) Subject: Parsing stream of JSON objects incrementally In-Reply-To: References: Message-ID: <22477795.145.1324336699742.JavaMail.geo-discussion-forums@yqir6> You probably need to accumulate a buffer and try to decode it, when succeeded return the object and read more. See example below (note that for sockets select might be a better option for reading data). import json from time import sleep def json_decoder(fo): buff = '' decode = json.JSONDecoder().raw_decode while True: line = fo.readline() if not line: break buff += line print('BUFF: {}'.format(buff)) try: obj, i = decode(buff) buff = buff[i:].lstrip() yield obj except ValueError as e: print('ERR: {}'.format(e)) sleep(0.01) # select will probably be a better option :) def main(): import sys for obj in json_decoder(sys.stdin): print(obj) if __name__ == '__main__': main() From miki.tebeka at gmail.com Mon Dec 19 18:18:19 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Mon, 19 Dec 2011 15:18:19 -0800 (PST) Subject: Parsing stream of JSON objects incrementally In-Reply-To: References: Message-ID: <22477795.145.1324336699742.JavaMail.geo-discussion-forums@yqir6> You probably need to accumulate a buffer and try to decode it, when succeeded return the object and read more. See example below (note that for sockets select might be a better option for reading data). import json from time import sleep def json_decoder(fo): buff = '' decode = json.JSONDecoder().raw_decode while True: line = fo.readline() if not line: break buff += line print('BUFF: {}'.format(buff)) try: obj, i = decode(buff) buff = buff[i:].lstrip() yield obj except ValueError as e: print('ERR: {}'.format(e)) sleep(0.01) # select will probably be a better option :) def main(): import sys for obj in json_decoder(sys.stdin): print(obj) if __name__ == '__main__': main() From jerome at jolimont.fr Mon Dec 19 18:50:17 2011 From: jerome at jolimont.fr (=?UTF-8?B?SsOpcsO0bWU=?=) Date: Tue, 20 Dec 2011 00:50:17 +0100 Subject: Threading issue (using alsaaudio) Message-ID: <20111220005017.7f713e14@bouzin.lan> Hi all. I am currently learning python (and pyGTK) and I'm having a hard time understanding some threading stuff. I'm writing here hoping I can get - pointers to some documentation that could help me - a lead concerning the specific problem described in the message - a hint about which library to use for audio playback As an exercise, I am trying to write an application that plays sounds through the speakers. For that, I use alsaaudio (http://pyalsaaudio.sourceforge.net/). The sound is just a sine wave. I guess using threading is the good way to go. So I create a thread. The run() method calls PCM.write() from alsaudio to send the sound to a playback buffer. Once the thread is started, the button is disabled, and it is enabled again when the thread is dead. This is done via GObject.idle_add(), which triggers a regular call to the thread is_alive() function. There's a design flow, here, as the run() method that calls pcm.write() exits when the sound data is buffered (this is quick, say 0.1s), not when it is actualy finished playing (this is long : 1s). Therefore, the thread dies long before the sound is finished playing and the button is going to be enabled too soon. But this is not what happens, and before finding another way to proceed (this is likely to be a timer), I would like to understand what actually happens (remember, this is just an exercise). What happens is that the idle_add polling occurs as long as the run() method is running, but then, it gets stuck until the sound has actually finished playing. The button is enabled at the end of the playback (which is what I wanted in the first place, except I'd like to understand why...). Besides, a second click (on the then disabled button) during the playback is not ignored, but generates another sound after the first. It seems that while the thread is dead (or should be, as the last print has been reached and executed), as long as the sound is being played, the Gtk.main() is stuck and does not process either gtk events or idle tasks. I don't understand that. I wonder what happens and in which thread lies my program while the sound is playing, between the thread run() end and the Gtk.main(). (There's a note in alsaaudio documentation about pcm.write normal and non-block modes but I don't think it is relevant here because, as far as I understand, the data sent does not fill the buffer so in both modes, pcm.write() should exit without blocking. (I might be wrong about this.)) I tried to remove from the code all that has nothing to do with this issue, and I left the prints that reveal what happens. Here it is : --------------------------------------------------------------------------- #!/usr/bin/env python # -*- coding: UTF8 -*- from gi.repository import Gtk, GObject import alsaaudio from numpy import arange, sin, ceil, pi, float32, hstack import threading import time ######################################################################### # audio functions ######################################################################### class Alsa_play_note(threading.Thread): def run(self): print "Entering thread run() method" length = 1 #?1 second freq = 440 channels = 1 sample_size = 1 # bytes per sample frame_size = channels * sample_size # bytes per frame frame_rate = 44000 # frames per second period_size = int(frame_rate * length) pcm = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, alsaaudio.PCM_NORMAL) #pcm = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, alsaaudio.PCM_NONBLOCK) pcm.setchannels(channels) pcm.setrate(frame_rate) pcm.setformat(alsaaudio.PCM_FORMAT_FLOAT_LE) pcm.setperiodsize(period_size) nb_samp = int(ceil(frame_rate / freq)) sample = arange(0, nb_samp, dtype=float32) sample *= pi * 2 / 44100 * freq sample = sin(sample) (nw, ex) = divmod(period_size * frame_size, nb_samp) wave_data = sample for i in range(nw-2): wave_data = hstack((wave_data, sample)) wave_data = hstack((wave_data, sample[:ex])) print "run() -> entering pcm.write" #time.sleep(1) a = pcm.write(wave_data.tostring()) #print a print "run() -> pcm.write done" print "Leaving thread run() method" def __init__(self): threading.Thread.__init__(self) ######################################################################### # class Keyboard ######################################################################### class Keyboard: ##################################################################### # Play note ##################################################################### def play_note(self, widget): print "Entering play_note() callback" self._button.set_sensitive(False) print "Creating thread" self._alsa_thread = Alsa_play_note() print "Calling thread.start()" self._alsa_thread.start() print "Back from thread.start()" GObject.idle_add(self._poll_alsa_play_in_progress) print "GObject.idle_add() done" ##################################################################### # Poll alsa play in progress ##################################################################### def _poll_alsa_play_in_progress (self): print "." if (self._alsa_thread.is_alive()): return True print "Button enabled" self._button.set_sensitive(True) return False ##################################################################### # Init ##################################################################### def __init__(self): # Declare thread handler self._alsa_thread = 0 # Create window ############### self._window = Gtk.Window() self._window.connect("destroy", Gtk.main_quit) self._window.connect("destroy", Gtk.main_quit) # Key button ############ self._button = Gtk.Button() self._button.set_size_request(400, 100) self._button.set_label("A") self._button.show() # Connect handler self._handler = self._button.connect("clicked", self.play_note) # Show everything ################# self._window.add(self._button) self._window.show() ######################################################################### # main ######################################################################### def main(): GObject.threads_init() Gtk.main() return 0 if __name__ == "__main__": Keyboard() main() --------------------------------------------------------------------------- I'd be thankful for any tip. I've been pulling my hair quite some time with these threading issues. Regarding python and pyGTK, I'm generally refering to : Python 2.7 documentation http://docs.python.org/ python GTK+3 tutorial (not really complete) http://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/index.html python GTK2.0 tutorial http://pygtk.org/pygtk2tutorial/index.html I may be missing some documentation / tutorials. Anything else I should read extensively before asking here ? Regarding the playback library, I've been searching before choosing alsaaudio. I chose ALSA because I understand OSS is deprecated, or on its way to be. And alsaaudio seemed to be the project with the most recent updates. Yet it does not pretend to be (or aim at being) complete. Was that a sensible choice ? Would you recommend something else ? The learning curve is sometimes steep... I would be thankful for any lead. -- J?r?me From ting at thsu.org Mon Dec 19 18:56:43 2011 From: ting at thsu.org (ting at thsu.org) Date: Mon, 19 Dec 2011 15:56:43 -0800 (PST) Subject: Make a small function thread safe References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> Message-ID: <13838f06-58a2-4f77-8279-25e414d9c64c@z19g2000vbe.googlegroups.com> On Dec 16, 8:21?am, Brad Tilley wrote: > A thread locks the function on entrance and then releases it on exit. > What is the equivalent way to do this in Python? I'm not sure if this applies in your case, but much of the time, you can use thread local storage, rather thread locking, in order to make your code thread safe. You tend to run into far less bottleneck and race condition issues by using thread local storage rather than thread locking, whenever possible. -- // T.Hsu From d at davea.name Mon Dec 19 19:15:23 2011 From: d at davea.name (Dave Angel) Date: Mon, 19 Dec 2011 19:15:23 -0500 Subject: the slash & Windows paths In-Reply-To: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> References: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> Message-ID: <4EEFD39B.5010705@davea.name> On 12/19/2011 05:02 PM, Juan Declet-Barreto wrote: > All, > > I have a Windows-style path that I need to modify so Python functions can find it. For example, the path "C:\Projects\Population_Pyramids\charts\test.xls" is being interpreted as pointing to a file called "est.xls" since the "t" is being escaped by the preceding slash as a tab. > > I know this is straightforward to handle using raw literals by prefixing the literal with an 'r', but my path is stored in a variable. > > My questions are: 1. How do I prevent single slashes from being escaped in a string variable (NOT literal)? > 2. Is it possible to do a string.replace to substitute all instances of '\' for '\\'? I tried this but it did not give me the desired result. > So far you haven't told us how you got this weird pattern into your variable. For example, if it was typed in by the user, in the form: mypath = raw_input("what's yourpath?") then the question is nonsensical. There's no escaping needed, as it has never been a literal string. if it came from a text file, there's no escaping needed. So give us a real scenario, and we'll probably be able to figure out where you're going wrong. -- DaveA From roy at panix.com Mon Dec 19 19:30:53 2011 From: roy at panix.com (Roy Smith) Date: Mon, 19 Dec 2011 19:30:53 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: In article , Andrew Berg wrote: > But what about the example he gave about being logged into a customer's > machine with only ed available? I suppose such fools would not be worthy > of your business. The customer is always right. Especially when the support contract is big enough to make or break your quarter. From someone at someplace.invalid Mon Dec 19 19:59:32 2011 From: someone at someplace.invalid (HoneyMonster) Date: Tue, 20 Dec 2011 00:59:32 +0000 (UTC) Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: On Mon, 19 Dec 2011 10:40:06 +1100, Chris Angelico wrote: > On Mon, Dec 19, 2011 at 9:55 AM, HoneyMonster > wrote: >> When the user selects a row and clicks a button, I am using: >> pos = self.grid_1.GetGridCursorRow() to establish which tuple in recs >> is involved, and then pid = recs[pos][4] to determine the key value (I >> suspected that accessing recs directly would be more efficient that >> trying to determine the wxGrid column value, and in any case the pid is >> in recs but not in the wxGrid). > > Yep, this looks like a sensible way to do it! > > I would recommend, though, that you avoid "magic numbers" - the process > ID might happen to be in cell 4, but what if your column list changes? > This is especially problematic when you use "select * from table", > because changes outside your code can break things. > > There's two solutions. One is to carefully match your SELECT statement > to a set of constants: > > exec("SELECT FOO,BAR,QUUX,ASDF,PID,WHATEVER FROM table") > FOO,BAR,QUUX,ASDF,PID,WHATEVER,*_=range(20) # Cool trick to avoid having > to count the columns > > But a better way is to let the database handle it. Check your interface > library to see if you can have it return dictionaries or objects instead > of tuples - then you could use: > > pid = recs[pos]["pid"] > # or pid = recs[pos]->pid > > which avoids the need to count columns at all. > > Other than that, though, yep - your code concept looks fine. Thanks again, Chris. I'm completely new to Python, but am an old had at databases. I *never* use "select *" in a production environment; rather an explicit list of columns. Thus it doesn't matter if a column is added to the table, nor if the column order in the table is changed. But I have used this at the start if my code to make it more readable anyway: # Define constants (position in tuple) Title = 0 Episode = 1 Categories = 2 Channel = 3 PID = 4 Index = 5 so that in the bit where I populate the grid, the code is now: cnt = 0 for tup in recs: self.grid_1.SetCellValue(cnt, Title, tup[Title]) self.grid_1.SetCellValue(cnt, Episode, tup[Episode]) self.grid_1.SetCellValue(cnt, Categories, tup[Categories]) self.grid_1.SetCellValue(cnt, Channel, tup[Channel]) cnt = cnt + 1 just so that is more legible. Cheers, HM From joshua.landau.ws at gmail.com Mon Dec 19 20:10:59 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Dec 2011 01:10:59 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: On 20 December 2011 00:30, Roy Smith wrote: > In article , > Andrew Berg wrote: > > > But what about the example he gave about being logged into a customer's > > machine with only ed available? I suppose such fools would not be worthy > > of your business. > > The customer is always right. Especially when the support contract is > big enough to make or break your quarter. > -- > http://mail.python.org/mailman/listinfo/python-list > This whole conversation is ridiculous. Do we really want a syntax that's worse just to satisfy the 1/10,000 cases* where someone will be using ed or notepad? Correct Answer: no. * Probably less, as I use vim many_times_a_day * many_days_a_year - and even less if we consider that no-one'll be doing as much coding in an hour of ed as in an hour of -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Mon Dec 19 20:18:14 2011 From: roy at panix.com (Roy Smith) Date: Mon, 19 Dec 2011 20:18:14 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: Sorry, I wasn't meaning to imply support for the syntax proposal. Just reacting to the (seemingly unrelated) comment that a customer with foolish access policies would not be worthy of your business. Only because I've been in the situation of having to provide remote support to major customers with similar absurd policies :-) On Dec 19, 2011, at 8:10 PM, Joshua Landau wrote: > On 20 December 2011 00:30, Roy Smith wrote: > In article , > Andrew Berg wrote: > > > But what about the example he gave about being logged into a customer's > > machine with only ed available? I suppose such fools would not be worthy > > of your business. > > The customer is always right. Especially when the support contract is > big enough to make or break your quarter. > -- > http://mail.python.org/mailman/listinfo/python-list > > This whole conversation is ridiculous. Do we really want a syntax that's worse just to satisfy the 1/10,000 cases* where someone will be using ed or notepad? > > Correct Answer: no. > > * Probably less, as I use vim many_times_a_day * many_days_a_year - and even less if we consider that no-one'll be doing as much coding in an hour of ed as in an hour of -- Roy Smith roy at panix.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Mon Dec 19 20:22:52 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Dec 2011 01:22:52 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: On 20 December 2011 01:18, Roy Smith wrote: > Sorry, I wasn't meaning to imply support for the syntax proposal. Just > reacting to the (seemingly unrelated) comment that a customer with foolish > access policies would not be worthy of your business. Only because I've > been in the situation of having to provide remote support to major > customers with similar absurd policies :-) > Nononono! I wasn't referring to you! I was referring to the whole topic. You just happened to be that last person to say something. Sorry about that, I should have been clearer... > On Dec 19, 2011, at 8:10 PM, Joshua Landau wrote: > > On 20 December 2011 00:30, Roy Smith wrote: > >> In article , >> Andrew Berg wrote: >> >> > But what about the example he gave about being logged into a customer's >> > machine with only ed available? I suppose such fools would not be worthy >> > of your business. >> >> The customer is always right. Especially when the support contract is >> big enough to make or break your quarter. >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > This whole conversation is ridiculous. Do we really want a syntax that's > worse just to satisfy the 1/10,000 cases* where someone will be using ed or > notepad? > > Correct Answer: no. > > * Probably less, as I use vim many_times_a_day * many_days_a_year - and > even less if we consider that no-one'll be doing as much coding in an hour > of ed as in an hour of > > > > -- > Roy Smith > roy at panix.com > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Mon Dec 19 20:34:25 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Dec 2011 01:34:25 +0000 Subject: Grammar for classes Message-ID: In reading thorough the syntax defined in the reference, the class statement has surprised me. It says that the inheritance part of the class can accept comprehensions. What does this mean? I've tried: "class A(x for x in ()): pass" but this doesn't need the extra clause as "x for x in ()" is an expression, and thus this evaluates: "class A(x for x in (),): pass" although again it won't be a valid class anytime soon. So what is this clause for? -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Mon Dec 19 20:41:42 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Mon, 19 Dec 2011 19:41:42 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: <4EEFE7D6.2090903@gmail.com> On 12/19/2011 7:18 PM, Roy Smith wrote: > Sorry, I wasn't meaning to imply support for the syntax proposal. Just > reacting to the (seemingly unrelated) comment that a customer with > foolish access policies would not be worthy of your business. It was directed at Rick, and by "your", I was referring to him specifically, as that is the kind of attitude I might expect him to show. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From gnarlodious at gmail.com Mon Dec 19 21:04:15 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Mon, 19 Dec 2011 18:04:15 -0800 (PST) Subject: Transform two tuples item by item Message-ID: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> What is the best way to operate on a tuple of values transforming them against a tuple of operations? Result can be a list or tuple: tup=(35, '34', 0, 1, 31, 0, '既濟') from cgi import escape [tup[0], "{}".format(tup[1]), bool(tup[2]), bool(tup[3]), tup[4], bool(tup[5]), escape(tup[6])] -> [35, "34", False, True, 31, False, '&#26082;&#28639;'] But I want to loop rather than subscripting. -- Gnarlie From python.list at tim.thechases.com Mon Dec 19 21:42:13 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 19 Dec 2011 20:42:13 -0600 Subject: Transform two tuples item by item In-Reply-To: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> References: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> Message-ID: <4EEFF605.8040100@tim.thechases.com> On 12/19/11 20:04, Gnarlodious wrote: > What is the best way to operate on a tuple of values > transforming them against a tuple of operations? Result can be > a list or tuple: > > tup=(35, '34', 0, 1, 31, 0, '既濟') > > from cgi import escape > [tup[0], " class='H'>{}".format(tup[1]), bool(tup[2]), > bool(tup[3]), tup[4], bool(tup[5]), escape(tup[6])] > > -> [35, "34", False, True, 31, False, > '&#26082;&#28639;'] > > But I want to loop rather than subscripting. Well, you can do something like >>> from cgi import escape >>> nop = lambda x: x >>> tup = (35, '34', 0, 1, 31, 0, '既濟') >>> ops = [nop, "{0}".format, bool, bool, nop, bool, escape] >>> [f(x) for f, x in zip(ops,tup)] [35, "34", False, True, 31, False, '&#26082;&#28639;'] Note #1: I had to change your format from "{}" to "{0}", at least in 2.6 I've got here) Note #2: it's spelled ".format" not ".format()" which puts the function reference in the "ops" list, not the results of calling it. -tkc From 1011_wxy at 163.com Mon Dec 19 21:44:07 2011 From: 1011_wxy at 163.com (1011_wxy) Date: Tue, 20 Dec 2011 10:44:07 +0800 (CST) Subject: About pysvn for x64 system In-Reply-To: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> References: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> Message-ID: <4b4ac11a.12356.134595ac321.Coremail.1011_wxy@163.com> Hi: I want to use svn to backup my oracle db object daily,so i want to use pysvn module. server: windows server 2008 64bit python:Python 2.6.5 (r265:79096, Mar 19 2010, 18:02:59) [MSC v.1500 64 bit (AMD64)] on win32 I have downloaded pysvn from "http://pysvn.tigris.org/files/documents/1233/48844/py26-pysvn-svn1615-1.7.5-1360.exe",and It can't be installed unless I copy [HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.6\InstallPath] to [HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\2.6\InstallPath]. After I installed with no error occured >>> import pysvn Traceback (most recent call last): File "", line 1, in import pysvn File "C:\Python26\lib\site-packages\pysvn\__init__.py", line 99, in import _pysvn_2_6 ImportError: DLL load failed: %1 is not valid Win32 application. So, I think there is no pysvn for 64bit windows,is that right? -------------- next part -------------- An HTML attachment was scrubbed... URL: From pytom at yahoo.cn Mon Dec 19 21:53:13 2011 From: pytom at yahoo.cn (Tom Zhou) Date: Tue, 20 Dec 2011 10:53:13 +0800 (CST) Subject: Columnize in module "cmd" Message-ID: <1324349593.51908.YahooMailNeo@web92411.mail.cnh.yahoo.com> Hi~ alls recently, i focus on the module "cmd", and find some confused things-- the function named "columnize". Why we need a multiloop as "for nrows .. ? ? ? ? for col .. ? ? ? ? ? ? ? ? ? ?for row.." ?? i think we can make a easier method, for example, first, find out the maxlen str in list, and use its length as the standard size to format the list. Ok, maybe i ignore something, so please give me some hints. --- thanks tom -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Mon Dec 19 21:58:11 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 19 Dec 2011 19:58:11 -0700 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: Message-ID: On Mon, Dec 19, 2011 at 4:15 PM, wrote: > On Dec 16, 11:49?am, John Gordon wrote: >> I'm working with IPv6 CIDR strings, and I want to replace the longest >> match of "(0000:|0000$)+" with ":". ?But when I use re.sub() it replaces >> the leftmost match, even if there is a longer match later in the string. > > Typically this means that your regular expression is not specific > enough. > > That is, if you get multiple matches, and you need to sort through > those matches before performing a replace, it usually means that you > should rewrite your expression to get a single match. > > Invariably this happens when you try to take short cuts. I can't blame > you for using a short cut, as sometimes short cuts just work, but once > you find that your short cut fails, you need to step back and rethink > the problem, rather than try to hack your short cut. The problem isn't short cuts. To narrow down multiple matches to a single longest match here, two additional criteria need to be met: there must be no other match anywhere in the search string that is longer than the match being considered, and there must be no match of equal length preceding the match being considered. Note that in the general case, the language is not regular and it would not even be possible to get a single longest match using a regular expression. For IPv6, it is possible only because IPv6 addresses are bounded in length. In English, that regular expression would be constructed like this: Any block of four or more groups of zeroes OR Zero or more blocks containing (zero to two groups of zero followed by a non-zero group) followed by a block of three or more groups of zeroes followed by zero or more blocks containing (a non-zero group followed by zero to three groups of zeroes) OR Zero or more blocks containing (an optional zero group followed by a non-zero group) followed by a block of two or more groups of zeroes followed by zero or more blocks containing (a non-zero group followed by zero to to two groups of zeroes) OR Zero or more non-zero groups followed by a group of zeroes followed by zero or more blocks containing (a non-zero group followed by an optional zero group). If anyone wants to give a crack at translating that to an actual regular expression, I'd be interested in seeing it. The added complexity is so great, though, that I for one would vastly prefer the simple solution already proposed of getting all the matches and iterating to find the longest. Cheers, Ian From rantingrickjohnson at gmail.com Mon Dec 19 22:15:33 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Mon, 19 Dec 2011 19:15:33 -0800 (PST) Subject: The ever-expanding bubble of GREED Message-ID: <24b782e6-6320-4f97-ad54-a0609450e4d0@x7g2000yqb.googlegroups.com> I just read an article[1] reporting that Apple has won a court case against Andoid (and others) over a patent infringement. Before i was able to see what all the hub-bub was about my mind started to wonder what "advanced technology" Google has stolen from Apple. To my great UNSURPRISE the so-called "advanced tech" the apple-tards managed to patent was... wait for it....HYPERLINKS! WHAT? They got a patent for hyperlinks? When will the REAL inventor get due credit? Are you trying to tell us that YOU invented the hyperlink, Apple? Folks, this whole intellectual property thing has approached a point of madness. I can see two spoiled kids fighting over a toy iterating for eternity ... while throat_not__too_terribly_sore(): kid_1.say( "i had it first") kid_2.say("No, I had it first") ...meanwhile WE suffer the consequences. And the consequences are two fold. 1. Increased cost 2. decreased advancement of tech But is Apple the REAL problem here? No, the *real* problem is GREED. We have corporations who's sole purpose is to make a very few people rich off the backs of the slaves. In this environment, the only way you can prosper is through GREED. I don't blame the corporations, no, i blame the system. Greed is the disease, and Cooperatives[2] are the cure. [1] http://www.nytimes.com/2011/12/20/technology/apple-wins-partial-victory-on-patent-claim-over-android-features.html [2] http://en.wikipedia.org/wiki/Cooperative From ian.g.kelly at gmail.com Mon Dec 19 22:26:30 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 19 Dec 2011 20:26:30 -0700 Subject: The ever-expanding bubble of GREED In-Reply-To: <24b782e6-6320-4f97-ad54-a0609450e4d0@x7g2000yqb.googlegroups.com> References: <24b782e6-6320-4f97-ad54-a0609450e4d0@x7g2000yqb.googlegroups.com> Message-ID: On Mon, Dec 19, 2011 at 8:15 PM, Rick Johnson wrote: > I just read an article[1] reporting that Apple has won a court case > against Andoid (and others) over a patent infringement. Before i was > able to see what all the hub-bub was about my mind started to wonder > what "advanced technology" Google has stolen from Apple. None of which has anything to do with Python. Congratulations, you're back in my message filters again. From steve+comp.lang.python at pearwood.info Mon Dec 19 22:29:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 03:29:00 GMT Subject: the slash & Windows paths References: Message-ID: <4ef000fc$0$11091$c3e8da3@news.astraweb.com> On Mon, 19 Dec 2011 15:02:50 -0700, Juan Declet-Barreto wrote: > xmlns:o="urn:schemas-microsoft-com:office:office" > xmlns:w="urn:schemas-microsoft-com:office:word" > xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" > xmlns="http://www.w3.org/TR/REC-html40"> http-equiv=Content-Type content="text/html; charset=us-ascii"> name=Generator content="Microsoft Word 14 (filtered medium)">> UG everyone! db = %d'%self.db ## TODO: meretezhetoseg def createWidgets(self): top = self.winfo_toplevel() top.rowconfigure(0,weight = 1) top.columnconfigure(0,weight = 1) self.rowconfigure(0,weight = 1) self.columnconfigure(0,weight = 1) self.QUIT = Button(self) self.QUIT["text"] = "QUIT" self.QUIT["fg"] = "red" self.QUIT["command"] = self.quit self.QUIT.pack({"side": "left"}) self.hi_there = Button(self) self.hi_there["text"] = "Hello", self.hi_there["command"] = self.say_hi self.hi_there.pack({"side": "left"}) def __init__(self, master=None): Frame.__init__(self, master) self.pack() self.createWidgets() self.db = 0 app = Application() app.master.title('UG test') app.mainloop() Thx in advance... Gabor -- Linux is like a wigwam: no Gates, no Windows and an Apache inside. From franck at ditter.org Tue Dec 20 07:56:20 2011 From: franck at ditter.org (Franck Ditter) Date: Tue, 20 Dec 2011 13:56:20 +0100 Subject: Can't get tilde character with IDLE 3.2.2 on French Mac Lion References: Message-ID: Nope, "space" followed by "Shift-Option-N" gives a greek iota... I tried other combinations, unsuccessfully. IDLE 3 (French) seems to be unusable as we use many ~ in web applications :-( Should we hope a fix soon, or leave IDLE ? Thanks, franck In article , Ned Deily wrote: > In article , > Franck Ditter wrote: > > All is in the subject. I'm starting to use Python with Idle 3.2.2 > > on MacOS-X Lion (French). I can't get "Option-N space" to provide > > the ~ char. > > I tried to go into the Keys preferences but I can't find "Option-N space" > > to modify its meaning. Its actual behavior is to merge lines of a > > paragraph. > > You are likely running into a current problem in the OS X Cocoa version > of Tcl/Tk 8.5 as included with Lion and as shipped by ActiveState. > Previously, if you tried to type composite characters, like Option N, > the Cocoa Tcl/Tk would crash. Pending a real fix, a patch was made to > Tcl/Tk 8.5 to discard composite characters rather than crash. You > should be able to get a tilde by using the post-composite keyboard > sequence: try typing "space" followed by "Shift-Option-N". > > http://sourceforge.net/tracker/index.php?func=detail&aid=2907388&group_id=12997&atid=112997 From ulrich.eckhardt at dominolaser.com Tue Dec 20 08:33:37 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Tue, 20 Dec 2011 14:33:37 +0100 Subject: check for a certain Python version Message-ID: Hi! I'd like to give my users a meaningful error message when they are using obsolete Python versions. Is there some kind of best practice or recipe for that? Specifically, the case is a bunch of unittests that use features new to Python 2.7. Thank you! Uli From saqib.ali.75 at gmail.com Tue Dec 20 08:52:30 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Tue, 20 Dec 2011 08:52:30 -0500 Subject: Need help with really elementary pexpect fragment In-Reply-To: <11996.1324359117@alphaville.dokosmarshall.org> References: <93ad02cb-68c3-44c4-ac9b-74a6e87d3f70@y7g2000vbe.googlegroups.com> <11996.1324359117@alphaville.dokosmarshall.org> Message-ID: Oops! Good call. Thank you. You pointed out my mistake. - Saqib On Tue, Dec 20, 2011 at 12:31 AM, Nick Dokos wrote: > Saqib Ali wrote: > > > > > I want to write a pexpect script that simply cd's into a directory ("~/ > > install") and then runs a command from there. It should be so easy. > > But even my cd command is failing. Can't figure out what the problem > > is. The command line prompt is "[my machine name here] % " > > > > Here is the code fragment: > > > > print "Spawning Expect" > > p = pexpect.spawn ('/bin/tcsh',) > > > > If you execute /bin/tcsh by hand, do you get a "%" prompt? > > Nick > > > print "Sending cd command" > > i = p.expect([pexpect.TIMEOUT, "%",]) > > assert i != 0, "Time-Out.... exiting" > > p.sendline("cd ~/install") > > > > Here is the output: > > > > Spawning Expect > > Sending cd command > > Time-Out.... exiting > > > > > > How could I be screwing something so simple up?? > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From arnodel at gmail.com Tue Dec 20 09:02:31 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Tue, 20 Dec 2011 14:02:31 +0000 Subject: Performing a number of substitutions on a unicode string Message-ID: Hi all, I've got to escape some unicode text according to the following map: escape_map = { u'\n': u'\\n', u'\t': u'\\t', u'\r': u'\\r', u'\f': u'\\f', u'\\': u'\\\\' } The simplest solution is to use str.replace: def escape_text(text): return text.replace('\\', '\\\\').replace('\n', '\\n').replace('\t', '\\t').replace('\r', '\\r').replace('\f', '\\f') But it creates 4 intermediate strings, which is quite inefficient (I've got 10s of MB's worth of unicode strings to escape) I can think of another way using regular expressions: escape_ptn = re.compile(r"[\n\t\f\r\\]") # escape_map is defined above def escape_match(m, map=escape_map): return map[m.group(0)] def escape_text(text, sub=escape_match): return escape_ptn.sub(sub, text) Is there a better way? Thanks, -- Arnaud From ulrich.eckhardt at dominolaser.com Tue Dec 20 09:15:06 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Tue, 20 Dec 2011 15:15:06 +0100 Subject: nesting context managers Message-ID: Hi! Let us assume I had a class HTTPClient that has a socket for HTTP and a logfile for filing logs. I want to make this HTTPClient a context manager, so that I can write with HTTPClient(url) as client: pass and reliably have both the socket and the logfile closed. The easy way is wrong def __enter__(self): with self._mysock, self._myfile: return self because it closes the file and socket as part of the return statement. Of course I can go the hard way, code everything myself, but I wonder if there isn't some useful tool that would help me achieve this (common?) task. Thanks! Uli From roy at panix.com Tue Dec 20 09:21:41 2011 From: roy at panix.com (Roy Smith) Date: Tue, 20 Dec 2011 09:21:41 -0500 Subject: check for a certain Python version References: Message-ID: In article , Ulrich Eckhardt wrote: > Hi! > > I'd like to give my users a meaningful error message when they are using > obsolete Python versions. Is there some kind of best practice or recipe > for that? Specifically, the case is a bunch of unittests that use > features new to Python 2.7. >>> sys.version_info (2, 6, 1, 'final', 0) major, minor, _, _, _ = sys.version_info self.assertGreaterEqual(minor, 7, "You, dude, fix your python") From rantingrickjohnson at gmail.com Tue Dec 20 09:37:58 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 20 Dec 2011 06:37:58 -0800 (PST) Subject: Set initial size in TKinter References: Message-ID: <1deb41c4-d511-4a54-a34d-90b0566d0f9e@q9g2000yqe.googlegroups.com> On Dec 20, 5:09?am, Gabor Urban wrote: > Hi, > > I am quite newbie with Tkinter and I could not find the way to set the > size of the application. Probably due to this haphazard coding style; why would you name an object "Application" that is an instance of Tkinter.Frame? That is just going to confuse people, including yourself. Choosing proper names is very important. Would you call a cat a hat? Or a dog a frog? Also, why did you post so much non-applicable code? Here is what you should have posted... from Tkinter import * # app = Tk() app.title('This is an App, not a frame or a button') app.config(width=50, height=400) #frame1 = Frame() # Proper Naming Example. #frame2 = Frame() # Proper Naming Example. app.mainloop() YWATF From rantingrickjohnson at gmail.com Tue Dec 20 09:45:47 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 20 Dec 2011 06:45:47 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <0b479489-8dab-488c-81e8-f15ae2804234@z17g2000vbe.googlegroups.com> On Dec 20, 2:14?am, Stefan Behnel wrote: > For teaching, I think it's better to come around with something simpler > than a full-blown IDE, so that you can show off interactive development, > help() and other introspection features. IMHO much better than hiding all > that behind an IDE, That is a damn good point Stefan. Too many noobs step passed the introspection features of Python without knowing what happened. help, dir, type, isinstance, id... to name a few. Heck for most things, considering you have at least basic programming experience, the help function is all you need to learn the language. From python.list at tim.thechases.com Tue Dec 20 09:54:16 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 20 Dec 2011 08:54:16 -0600 Subject: Performing a number of substitutions on a unicode string In-Reply-To: References: Message-ID: <4EF0A198.6020309@tim.thechases.com> On 12/20/11 08:02, Arnaud Delobelle wrote: > Hi all, > > I've got to escape some unicode text according to the following map: > > escape_map = { > u'\n': u'\\n', > u'\t': u'\\t', > u'\r': u'\\r', > u'\f': u'\\f', > u'\\': u'\\\\' > } > > The simplest solution is to use str.replace: > > def escape_text(text): > return text.replace('\\', '\\\\').replace('\n', > '\\n').replace('\t', '\\t').replace('\r', '\\r').replace('\f', '\\f') > > But it creates 4 intermediate strings, which is quite inefficient > (I've got 10s of MB's worth of unicode strings to escape) You might try def escape_text(text): return text.encode("string_escape") as documented at[1] -tkc [1] http://docs.python.org/release/2.5.2/lib/standard-encodings.html#standard-encodings From arnodel at gmail.com Tue Dec 20 10:04:35 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Tue, 20 Dec 2011 15:04:35 +0000 Subject: Performing a number of substitutions on a unicode string In-Reply-To: <4EF0A198.6020309@tim.thechases.com> References: <4EF0A198.6020309@tim.thechases.com> Message-ID: On 20 December 2011 14:54, Tim Chase wrote: > On 12/20/11 08:02, Arnaud Delobelle wrote: >> >> Hi all, >> >> I've got to escape some unicode text according to the following map: >> >> escape_map = { >> ? ? u'\n': u'\\n', >> ? ? u'\t': u'\\t', >> ? ? u'\r': u'\\r', >> ? ? u'\f': u'\\f', >> ? ? u'\\': u'\\\\' >> } >> >> The simplest solution is to use str.replace: >> >> def escape_text(text): >> ? ? return text.replace('\\', '\\\\').replace('\n', >> '\\n').replace('\t', '\\t').replace('\r', '\\r').replace('\f', '\\f') >> >> But it creates 4 intermediate strings, which is quite inefficient >> (I've got 10s of MB's worth of unicode strings to escape) > > > You might try > > ?def escape_text(text): > ? ?return text.encode("string_escape") > I don't think this kind of approach would work as I'm not decoding unicode: both the input and the output of the "escape_text" function are unicode. -- Arnaud From eric.brunel at pragmadev.nospam.com Tue Dec 20 10:20:08 2011 From: eric.brunel at pragmadev.nospam.com (Eric Brunel) Date: Tue, 20 Dec 2011 16:20:08 +0100 Subject: Set initial size in TKinter References: Message-ID: In article , Gabor Urban wrote: > Hi, > > I am quite newbie with Tkinter and I could not find the way to set the > size of the application. (I could find the method to make it > resizeable, though :-)) ) Any ideas, suggestions or links to > references are wellcome. Usually, the best way is to use the geometry method on instances of Tk or Toplevel. For example, if you have a variable named root which is the instance of Tk, you can do: root.geometry('500x400') This will make the window 500 pixels wide and 400 pixels high. > Here is my code: > > from Tkinter import * > > class Application(Frame): > def say_hi(self): > self.db += 1 > print 'hi there, -->> UG everyone! db = %d'%self.db > > ## TODO: meretezhetoseg > def createWidgets(self): > top = self.winfo_toplevel() > top.rowconfigure(0,weight = 1) > top.columnconfigure(0,weight = 1) > self.rowconfigure(0,weight = 1) > self.columnconfigure(0,weight = 1) > self.QUIT = Button(self) > self.QUIT["text"] = "QUIT" > self.QUIT["fg"] = "red" > self.QUIT["command"] = self.quit > > self.QUIT.pack({"side": "left"}) > > self.hi_there = Button(self) > self.hi_there["text"] = "Hello", > self.hi_there["command"] = self.say_hi > > self.hi_there.pack({"side": "left"}) > > def __init__(self, master=None): > Frame.__init__(self, master) > self.pack() > self.createWidgets() > self.db = 0 > > app = Application() > app.master.title('UG test') > app.mainloop() Where did you find an example code looking like this? This looks like veeeeeery old conventions for Tkinter programs? For example, there's no need at all to do: self.QUIT = Button(self) self.QUIT["text"] = "QUIT" self.QUIT["fg"] = "red" self.QUIT["command"] = self.quit This can be done in a single line: self.QUIT = Button(self, text='QUIT', fg='red', command=self.quit) The same goes for self.QUIT.pack({"side": "left"}). Nowadays, this is always written self.QUIT.pack(side="left"). And you should avoid creating only an instance of Frame. This actually creates a window, but it's a side-effect. Windows are created by instantiating Tk for the main one, and Toplevel for all others. Having only a Frame will cause problems later, for example if you want to add a menu to the window: You can do so on instances of Tk or Toplevel, but not on frames? HTH - Eric - From rantingrickjohnson at gmail.com Tue Dec 20 10:30:50 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 20 Dec 2011 07:30:50 -0800 (PST) Subject: Set initial size in TKinter References: Message-ID: <8fd4083e-ae80-40bf-824e-cc38aa5cddde@i8g2000vbh.googlegroups.com> On Dec 20, 9:20?am, Eric Brunel wrote: > Where did you find an example code looking like this? This looks like > veeeeeery old conventions for Tkinter programs? >[...] > And you should avoid creating only an instance of Frame. This actually > creates a window, but it's a side-effect. Two major problems with Tkinter exposed here. 1. The tutorials are VERY old and need to be updated. There needs to be one, and preferably only one site for Tkinter tutorials. One that is through and complete! 2. Tkinter is designed to "auto-magically" create a root window if the user does not do so explicitly. This is a major flaw in the design of Tkinter and needs to be removed yesterday! From __peter__ at web.de Tue Dec 20 10:35:21 2011 From: __peter__ at web.de (Peter Otten) Date: Tue, 20 Dec 2011 16:35:21 +0100 Subject: Performing a number of substitutions on a unicode string References: Message-ID: Arnaud Delobelle wrote: > I've got to escape some unicode text according to the following map: > > escape_map = { > u'\n': u'\\n', > u'\t': u'\\t', > u'\r': u'\\r', > u'\f': u'\\f', > u'\\': u'\\\\' > } > > The simplest solution is to use str.replace: > > def escape_text(text): > return text.replace('\\', '\\\\').replace('\n', > '\\n').replace('\t', '\\t').replace('\r', '\\r').replace('\f', '\\f') > > But it creates 4 intermediate strings, which is quite inefficient > (I've got 10s of MB's worth of unicode strings to escape) > > I can think of another way using regular expressions: > > escape_ptn = re.compile(r"[\n\t\f\r\\]") > > # escape_map is defined above > def escape_match(m, map=escape_map): > return map[m.group(0)] > > def escape_text(text, sub=escape_match): > return escape_ptn.sub(sub, text) > > Is there a better way? >>> escape_map = { ... u'\n': u'\\n', ... u'\t': u'\\t', ... u'\r': u'\\r', ... u'\f': u'\\f', ... u'\\': u'\\\\' ... } >>> escape_map = dict((ord(k), v) for k, v in escape_map.items()) >>> print u"the quick\n brown\tfox jumps\\over\\the\\lazy\\dog".translate(escape_map) the quick\n brown\tfox jumps\\over\\the\\lazy\\dog From rosuav at gmail.com Tue Dec 20 10:51:42 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Dec 2011 02:51:42 +1100 Subject: Python education survey In-Reply-To: <0b479489-8dab-488c-81e8-f15ae2804234@z17g2000vbe.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <0b479489-8dab-488c-81e8-f15ae2804234@z17g2000vbe.googlegroups.com> Message-ID: On Wed, Dec 21, 2011 at 1:45 AM, Rick Johnson wrote: > Heck for most things, > considering you have at least basic programming experience, the help > function is all you need to learn the language. I know I shouldn't feed the troll, but this is more general. You cannot learn the _language_ from help() - it's more like a dictionary. You still need something that explains the grammar, and you also need to have some ideas of what names to look up. The help() function in Python is actually not as helpful as could be desired, in many cases. I think "help()" on its own is probably a good thing, but... I didn't know about it until I tried it right while typing up this email. (That's probably my fault more than Python's, though.) help(functionname) is usually fairly helpful - but most of that is just from the function's docstring. Unfortunately help(classname) and help(modulename) are way too spammy to be much use. Look for instance at help(decimal.Decimal) - quite a few dunder methods are listed, uselessly. What does "__deepcopy__(self, memo)" do? What about "__ge__(self, other, context=None)"? Unless you happen to know that __ge__ is the greater-than-or-equal function, it's not going to do you much good to see the method listed. They're just spam, forcing you to read through more screed to figure out what's going on. Same, and even worse, with modules - help(decimal) is pages and pages of text, detailing the exceptions supported etc. Unlike the class's, though, the module's docstring is actually quite helpful. (It also hints at what the context=None arguments are on a lot of the methods.) Please note that this is not a criticism of the decimal module specifically; and the help() function can't really be written any other way, short of having it emit ONLY docstrings, and then demand that module authors maintain perfect class and module docstrings (like that's gonna happen). Hmm. Another feature I didn't know about help() - instead of passing it an object, you can pass it a string. This gets around the fact that "help(class)" doesn't work - "help('class')" does. I think I should reword this from "possible feature request" to "does this already exist", because it probably does... so... Is there a less spammy documentation utility, in-built into Python? Chris Angelico From wanderer at dialup4less.com Tue Dec 20 11:14:42 2011 From: wanderer at dialup4less.com (Wanderer) Date: Tue, 20 Dec 2011 08:14:42 -0800 (PST) Subject: IPython 0.12 is out! References: <1331f71b-b8c6-475c-af22-13c79cfa0471@o9g2000yqa.googlegroups.com> <56bc8947-781e-41b6-a4aa-cdb1acd929ab@n6g2000vbz.googlegroups.com> Message-ID: <0a968f0a-3940-4d53-b151-7b43f4bc067d@p9g2000vbb.googlegroups.com> On Dec 19, 11:00?pm, alex23 wrote: > On Dec 20, 4:07?am, Wanderer wrote: > > > The windows installer didn't work but installing from the tar file > > did. But installing from the tar file doesn't install Ipython in the > > site-packages directory. It installs it wherever you untar the tar > > file. I don't remember ever having to deal with this before. Most > > things just install in the site-packages directory without me having > > to do anything special. > > You read the installation instructions and did a 'python setup.py > install' as it states, yes? > > Installed that way for Python 2.7.2 under Win64 with no issues > whatsoever. No I scrolled down to the Windows directions and used setuptools,'python setupegg.py install' It works but it is installed in 'C:Python27\Lib\site-packages\ipython-0.12.tar\dist\ipython-0.12' which is weird. I used 7zip to unzip and untar it maybe that is the issue. I'm using winXP. I should be getting my system upgraded to Win7 any day now, so I'm going to leave it where it is for now. Thanks From rami.chowdhury at gmail.com Tue Dec 20 11:25:58 2011 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Tue, 20 Dec 2011 16:25:58 +0000 Subject: nesting context managers In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 14:15, Ulrich Eckhardt wrote: > Hi! > > Let us assume I had a class HTTPClient that has a socket for HTTP and a > logfile for filing logs. I want to make this HTTPClient a context manager, > so that I can write > > ?with HTTPClient(url) as client: > ? ? ?pass > > and reliably have both the socket and the logfile closed. The easy way is > wrong > > ?def __enter__(self): > ? ? ?with self._mysock, self._myfile: > ? ? ? ? ?return self > It seems like some of the functions in the contextlib module might help? You could try and reorganize your code so that you can use the @contextmanager decorator, for instance? That having been said, it doesn't seem that difficult to me to code your own simple __exit__ method if you're already coding up __enter__ ? HTH, Rami -- Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 From ulrich.eckhardt at dominolaser.com Tue Dec 20 11:56:54 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Tue, 20 Dec 2011 17:56:54 +0100 Subject: nesting context managers In-Reply-To: References: Message-ID: Am 20.12.2011 15:15, schrieb Ulrich Eckhardt: > Let us assume I had a class HTTPClient that has a socket for HTTP and a > logfile for filing logs. I want to make this HTTPClient a context > manager, so that I can write > > with HTTPClient(url) as client: > pass Actually, I overestimated the task: class HTTPClient(url): def __enter__(self): return self def __exit__(self, ctx_type, ctx_val, ctx_tb): self.close() def close(self): self._myfile.close() self._mysocket.close() I'll simply ignore the fact that closing a file can fail if you can't flush buffers. Now, my error was that I have to somehow invoke the file's and socket's enter function in my client's enter function. The default for all files and sockets is that they are already open, they are not opened in the enter function! That way, the client remains usable outside a with context but gives me the desired guarantees inside one. For the case of on-demand allocated stuff, I could write the enter function like this: def __enter__(self): # allocate resources f = ... # open file s = ... # connect socket # attach after completion self._myfile = f self._mysocket = s To be extra safe or in more complicated scenarios, I could wrap this in a try-except and explicitly close those that were already created, but normally I'd expect the garbage collector to do that for me ... or am I then implicitly assuming a specific implementation? Uli From arnodel at gmail.com Tue Dec 20 12:04:48 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Tue, 20 Dec 2011 17:04:48 +0000 Subject: Performing a number of substitutions on a unicode string In-Reply-To: References: Message-ID: On 20 December 2011 15:35, Peter Otten <__peter__ at web.de> wrote: >>>> escape_map = { > ... ? ? u'\n': u'\\n', > ... ? ? u'\t': u'\\t', > ... ? ? u'\r': u'\\r', > ... ? ? u'\f': u'\\f', > ... ? ? u'\\': u'\\\\' > ... } >>>> escape_map = dict((ord(k), v) for k, v in escape_map.items()) >>>> print u"the quick\n brown\tfox > jumps\\over\\the\\lazy\\dog".translate(escape_map) > the quick\n brown\tfox jumps\\over\\the\\lazy\\dog Thanks. I think I've got a blind spot about unicode.translate. Actually, I know why: it's not properly documented where I look [1]. -- Arnaud [1] http://docs.python.org/library/stdtypes.html#str.translate From joshua.landau.ws at gmail.com Tue Dec 20 12:05:05 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Dec 2011 17:05:05 +0000 Subject: Grammar for classes In-Reply-To: References: Message-ID: On 20 December 2011 10:55, Robert Kern wrote: > On 12/20/11 1:34 AM, Joshua Landau wrote: > >> In reading thorough the syntax defined in the reference >> > class-definitions >> >, >> >> the class statement has surprised me. >> >> It says that the inheritance part of the class can accept comprehensions. >> What >> does this mean? >> I've tried: >> "class A(x for x in ()): pass" >> but this doesn't need the extra clause as "x for x in ()" is an >> expression, and >> thus this evaluates: >> "class A(x for x in (),): pass" >> although again it won't be a valid class anytime soon. >> >> So what is this clause for? >> > > I suspect that it's harder to make a grammar rule that allows every kind > of expression except for generator expressions than it is just to reuse the > "testlist" rule and let the runtime reject the generator object when it > goes to construct the class. > The thing is, as far as I can tell, the grammar would be the same without the added clause. That is because it's a valid grammar anyway. The only exception I can think of is if generators were treated exceptionally. Thus, while I agree it makes complete sense to make it a runtime error, the clause in the specification seems to be fluff. Also - what is this "testlist" rule thee speaks of? I think I'm just going to ignore it for now, as long as it doesn't mean I ignore valid code. Thanks Ian and Robert for the input. Should I file a documentation bug report? > -- > 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From robert.kern at gmail.com Tue Dec 20 12:20:12 2011 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 20 Dec 2011 17:20:12 +0000 Subject: Grammar for classes In-Reply-To: References: Message-ID: On 12/20/11 5:05 PM, Joshua Landau wrote: > On 20 December 2011 10:55, Robert Kern > wrote: > > On 12/20/11 1:34 AM, Joshua Landau wrote: > > In reading thorough the syntax defined in the reference > >, > > the class statement has surprised me. > > It says that the inheritance part of the class can accept > comprehensions. What > does this mean? > I've tried: > "class A(x for x in ()): pass" > but this doesn't need the extra clause as "x for x in ()" is an > expression, and > thus this evaluates: > "class A(x for x in (),): pass" > although again it won't be a valid class anytime soon. > > So what is this clause for? > > > I suspect that it's harder to make a grammar rule that allows every kind of > expression except for generator expressions than it is just to reuse the > "testlist" rule and let the runtime reject the generator object when it goes > to construct the class. > > > The thing is, as far as I can tell, the grammar would be the same without the > added clause. That is because it's a valid grammar anyway. The only exception I > can think of is if generators were treated exceptionally. Thus, while I agree it > makes complete sense to make it a runtime error, the clause in the specification > seems to be fluff. > > Also - what is this "testlist" rule thee speaks of? Sorry, I was looking at the actual 2.7 Grammar file. "testlist" has been renamed to "arglist" in the 3.2 version of the Grammar, but the substantive point I made remains (to the extent that it is relevant at all). That said, the documentation for both versions differs from a simple restatement of the Grammar rules, so I'm not sure exactly why the documentation is written the way it is. -- 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 From invalid at invalid.invalid Tue Dec 20 12:40:22 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 20 Dec 2011 17:40:22 +0000 (UTC) Subject: [OT] Quick intro to C++ for a Python and C user? Message-ID: Would anybody care to recommend online C++ resources for a long time C and Python user? (I'm also familiar with Smalltalk, Scheme, FORTRAN, bash, Javascript, and a variety of assembly languages.) I have a C++ library to which I need to add a couple minor wrappers/extensions. I've already done the same for the C version of the library. Writing test suites for C libraries using Python/ctypes is pretty cool. :) After googling a bit, I found several recommendations for the book "Thinking in C++" by Bruce Eckel. I've skimmed through it, and it's _way_ too long-winded. It takes him 200+ pages before he gets to data encapsulation and 600+ pages before he introduces inheritence. The writing is also a bit too condesending for my taste: You literally take the form of the existing class and add code to it, without modifying the existing class. This magical act is called _inheritance_, and most of the work is done by the compiler. Magical act? Seriously? -- Grant Edwards grant.b.edwards Yow! Are you the at self-frying president? gmail.com From rami.chowdhury at gmail.com Tue Dec 20 12:46:09 2011 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Tue, 20 Dec 2011 17:46:09 +0000 Subject: nesting context managers In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 16:56, Ulrich Eckhardt wrote: > To be extra safe or in more complicated scenarios, I could wrap this in a > try-except and explicitly close those that were already created, but > normally I'd expect the garbage collector to do that for me ... or am I then > implicitly assuming a specific implementation? I'm no expert but I believe the basic garbage collection behavior is part of the language, and it's only details of breaking cycles that are specific to CPython -- can anyone correct me if I'm wrong? -- Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 From ethan at stoneleaf.us Tue Dec 20 13:02:35 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 20 Dec 2011 10:02:35 -0800 Subject: nesting context managers In-Reply-To: References: Message-ID: <4EF0CDBB.1080904@stoneleaf.us> Rami Chowdhury wrote: > On Tue, Dec 20, 2011 at 16:56, Ulrich Eckhardt > wrote: >> To be extra safe or in more complicated scenarios, I could wrap this in a >> try-except and explicitly close those that were already created, but >> normally I'd expect the garbage collector to do that for me ... or am I then >> implicitly assuming a specific implementation? > > I'm no expert but I believe the basic garbage collection behavior is > part of the language, and it's only details of breaking cycles that > are specific to CPython -- can anyone correct me if I'm wrong? Garbage collection is part of the language, but how, and when, is implementation specific. Resource management (beyond basic memory allocation) should be handled directly. Python 3 encourages this by complaining if there were still open files when it shuts down. ~Ethan~ From miki.tebeka at gmail.com Tue Dec 20 13:07:31 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 20 Dec 2011 10:07:31 -0800 (PST) Subject: Python education survey In-Reply-To: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <21978406.608.1324404451230.JavaMail.geo-discussion-forums@yqgn9> I've tried several things. So far vim (with line numbers) to show the code and then ipython to run it works great. Another option I tried once was Aptana, since most people in my company know eclipse this was good for them. It has most (all?) of the features you mentioned above. From Admin at no.mail.to Tue Dec 20 13:19:14 2011 From: Admin at no.mail.to (Admin) Date: Tue, 20 Dec 2011 19:19:14 +0100 Subject: how to run python-script from the python promt? [absolute newbie] In-Reply-To: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: <4ef0d1a2$0$13515$4d5ecec7@news.xsusenet.com> Am 18.12.2011 12:00, schrieb nukeymusic: > How can I load a python-script after starting python in the > interactive mode? > I tried with >>>> load 'myscript.py' >>>> myscript.py >>>> myscript > > but none of these works, so the only way I could work further until > now was copy/paste line per line of my python-script to the > interactive mode prompt > I do know how to run the script non-interactively, but what I want to > do is adding lines to the code I have written thus far in interactive > mode. > > thanks in advance > nukey just go to the folder there there script is located and then do "scriptname.py" From rzantow at gmail.com Tue Dec 20 13:24:23 2011 From: rzantow at gmail.com (DZantow) Date: Tue, 20 Dec 2011 10:24:23 -0800 (PST) Subject: Test plans and "refined test plans" Message-ID: First, a disclaimer. I know this group is not the appropriate venue for the question I have. However, I am hoping some of you with experience can point me in the right direction, either by addressing the question or by suggesting a more appropriate group. The question is: A contract we are bidding on calls for, among other things, 2 deliverables: an "initial test plan" and a "refined test plan". We are not allowed to speak directly to the agency at this point, so it isn't clear how to accommodate this request. Have any of you had experience with similar requirements? If so, how did you differentiate the two plans? Thank you for any guidance. rzed From kimma77 at gmail.com Tue Dec 20 13:31:56 2011 From: kimma77 at gmail.com (kimma) Date: Tue, 20 Dec 2011 19:31:56 +0100 Subject: Learning Python 2.4 Message-ID: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Hi there, I am about to learn python with "how to think like a computer scientist". This book is just available for python 2.4. Does it matter for programming? Greetz From neilc at norwich.edu Tue Dec 20 13:36:53 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 20 Dec 2011 18:36:53 GMT Subject: [OT] Quick intro to C++ for a Python and C user? References: Message-ID: <9lc2u5FilrU1@mid.individual.net> On 2011-12-20, Grant Edwards wrote: > Would anybody care to recommend online C++ resources for a long > time C and Python user? (I'm also familiar with Smalltalk, > Scheme, FORTRAN, bash, Javascript, and a variety of assembly > languages.) The best book I know of to get you writing useful C++ quickly is Accelerated C++ by Koenig/Moo. It's not free online, though. It starts with an excellent introduction to using the STL and works it's way slowly down the abstraction ladder to using pointers and inheritance last of all. Iterators turn out to be an excellent starting point for learning pointers, though since you already know C that won't do you as much good. -- Neil Cerutti From ian.g.kelly at gmail.com Tue Dec 20 13:46:40 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Dec 2011 11:46:40 -0700 Subject: nesting context managers In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 9:56 AM, Ulrich Eckhardt wrote: > Am 20.12.2011 15:15, schrieb Ulrich Eckhardt: > >> Let us assume I had a class HTTPClient that has a socket for HTTP and a >> logfile for filing logs. I want to make this HTTPClient a context >> manager, so that I can write >> >> with HTTPClient(url) as client: >> ? ?pass > > > Actually, I overestimated the task: > > ?class HTTPClient(url): > ? ? ?def __enter__(self): > ? ? ? ? ?return self > ? ? ?def __exit__(self, ctx_type, ctx_val, ctx_tb): > ? ? ? ? ?self.close() > ? ? ?def close(self): > ? ? ? ? ?self._myfile.close() > ? ? ? ? ?self._mysocket.close() > > I'll simply ignore the fact that closing a file can fail if you can't flush > buffers. Now, my error was that I have to somehow invoke the file's and > socket's enter function in my client's enter function. The default for all > files and sockets is that they are already open, they are not opened in the > enter function! That way, the client remains usable outside a with context > but gives me the desired guarantees inside one. > > For the case of on-demand allocated stuff, I could write the enter function > like this: > > ?def __enter__(self): > ? ? ?# allocate resources > ? ? ?f = ... # open file > ? ? ?s = ... # connect socket > ? ? ?# attach after completion > ? ? ?self._myfile = f > ? ? ?self._mysocket = s > > To be extra safe or in more complicated scenarios, I could wrap this in a > try-except and explicitly close those that were already created, but > normally I'd expect the garbage collector to do that for me ... or am I then > implicitly assuming a specific implementation? For full generality, copy the code for the contextlib.nested function in Python 2.7 or 3.1 and use that. Don't use contextlib.nested itself, though, because it's deprecated and does not exist in Python 3.2. Cheers, Ian From ian.g.kelly at gmail.com Tue Dec 20 13:55:59 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Dec 2011 11:55:59 -0700 Subject: nesting context managers In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 11:46 AM, Ian Kelly wrote: > On Tue, Dec 20, 2011 at 9:56 AM, Ulrich Eckhardt > wrote: >> Am 20.12.2011 15:15, schrieb Ulrich Eckhardt: >> >>> Let us assume I had a class HTTPClient that has a socket for HTTP and a >>> logfile for filing logs. I want to make this HTTPClient a context >>> manager, so that I can write >>> >>> with HTTPClient(url) as client: >>> ? ?pass >> >> >> Actually, I overestimated the task: >> >> ?class HTTPClient(url): >> ? ? ?def __enter__(self): >> ? ? ? ? ?return self >> ? ? ?def __exit__(self, ctx_type, ctx_val, ctx_tb): >> ? ? ? ? ?self.close() >> ? ? ?def close(self): >> ? ? ? ? ?self._myfile.close() >> ? ? ? ? ?self._mysocket.close() >> >> I'll simply ignore the fact that closing a file can fail if you can't flush >> buffers. Now, my error was that I have to somehow invoke the file's and >> socket's enter function in my client's enter function. The default for all >> files and sockets is that they are already open, they are not opened in the >> enter function! That way, the client remains usable outside a with context >> but gives me the desired guarantees inside one. >> >> For the case of on-demand allocated stuff, I could write the enter function >> like this: >> >> ?def __enter__(self): >> ? ? ?# allocate resources >> ? ? ?f = ... # open file >> ? ? ?s = ... # connect socket >> ? ? ?# attach after completion >> ? ? ?self._myfile = f >> ? ? ?self._mysocket = s >> >> To be extra safe or in more complicated scenarios, I could wrap this in a >> try-except and explicitly close those that were already created, but >> normally I'd expect the garbage collector to do that for me ... or am I then >> implicitly assuming a specific implementation? > > For full generality, copy the code for the contextlib.nested function > in Python 2.7 or 3.1 and use that. ?Don't use contextlib.nested > itself, though, because it's deprecated and does not exist in Python > 3.2. Also note that in versions that do support the nested with syntax, you can do something like: class HTTPClient(url): @contextlib.contextmanager def _contextmanager(file, socket): with file, socket: yield def __enter__(self): self._cm = self._contextmanager(self._myfile, self._mysocket) return self._cm.__enter__() def __exit__(self, exc_type, exc_value, traceback): try: return self._cm.__exit__(exc_type, exc_value, traceback) finally: del self._cm Cheers, Ian From y.turgut at gmail.com Tue Dec 20 14:17:15 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Tue, 20 Dec 2011 11:17:15 -0800 (PST) Subject: Text Processing Message-ID: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> Hi all, I have a text file containing such data ; A B C ------------------------------------------------------- -2.0100e-01 8.000e-02 8.000e-05 -2.0000e-01 0.000e+00 4.800e-04 -1.9900e-01 4.000e-02 1.600e-04 But I only need Section B, and I need to change the notation to ; 8.000e-02 = 0.08 0.000e+00 = 0.00 4.000e-02 = 0.04 Text file is approximately 10MB in size. I looked around to see if there is a quick and dirty workaround but there are lots of modules, lots of options.. I am confused. Which module is most suitable for this task ? From no.email at nospam.invalid Tue Dec 20 14:43:09 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 20 Dec 2011 11:43:09 -0800 Subject: [OT] Quick intro to C++ for a Python and C user? References: Message-ID: <7xvcpb9ghe.fsf@ruckus.brouhaha.com> Grant Edwards writes: > Would anybody care to recommend online C++ resources for a long time C > and Python user? (I'm also familiar with Smalltalk, Scheme, FORTRAN, > bash, Javascript, and a variety of assembly languages.) Stroustrup's web site has lots of good stuff on it. I also like his C++ book "The C++ Programming Language" in preference to the many me-too books that are out there. He also has another C++ book that I haven't looked at but which sounded interesting. C++ is quite a bit different from those other languages you mention in that contemporary C++ style revolves a lot more around a reasonably serious static type system with typed generics. By comparison, C and Fortran have minimal static type systems and Smalltalk, Python, etc. have none at all. From nathan.alexander.rice at gmail.com Tue Dec 20 14:45:06 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Tue, 20 Dec 2011 14:45:06 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. Message-ID: Elementwise provides a proxy object for iterables which supports chained method calls, as well as elementwise expressions and some built-in functions. Example: class ExampleList(ElementwiseProxyMixin, list): def __new__(cls, iterable): return list.__new__(cls, iterable) foo = ExampleList([1, 2, 3, 4]) # You could also do: efoo = ElementwiseProxy(foo) efoo = foo.each assert list(efoo.bit_length()) == [1, 2, 2, 3] print "bit length: ", list(efoo.bit_length()) assert list(efoo + 1) == [2, 3, 4, 5] print "with addition of 1: ", list(efoo + 1) assert list(efoo * 2) == [2, 4, 6, 8] print "with multiplication by 2: ", list(efoo * 2) assert list(efoo == 2) == [False, True, False, False] print "testing equality: ", efoo == 2 assert list((efoo + 1) * 2 + 3) == [7, 9, 11, 13] print "chaining addition and multiplication: ", (efoo + 1) * 2 + 3 Each ElementwiseProxy also has a "parent" attribute so you can backtrack in the chain as needed rather than store each intermediate value, if you think you might need them. There are still some issues with proper support of things like bool() and int(), which refuse to return things that are not of the correct type. Get it: PyPi: http://pypi.python.org/pypi/elementwise/0.111220 GitHub: https://github.com/nathan-rice/Elementwise This was developed as a proof of concept for expanding the role of element-wise syntax in python, and to that end I welcome comments. Nathan Rice From ian.g.kelly at gmail.com Tue Dec 20 14:47:29 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Dec 2011 12:47:29 -0700 Subject: Learning Python 2.4 In-Reply-To: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Message-ID: On Tue, Dec 20, 2011 at 11:31 AM, kimma wrote: > Hi there, > > I am about to learn python with "how to think like a computer scientist". > This book is just available for python 2.4. Does it matter for programming? > > Greetz Python 2.4 is pretty old at this point. A lot has been added to the language since then, and Python 3 introduced a number of large, backward-incompatible changes. I don't have any specific book recommendations, but you should look for a book written for Python 3 if possible, since that is the future of Python. If not, then look for something that at least targets Python 2.6, which I would expect to include sections about coding with an eye toward Python 3 compatibility. Cheers, Ian From steve+comp.lang.python at pearwood.info Tue Dec 20 14:53:57 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 19:53:57 GMT Subject: check for a certain Python version References: Message-ID: <4ef0e7d5$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 14:33:37 +0100, Ulrich Eckhardt wrote: > Hi! > > I'd like to give my users a meaningful error message when they are using > obsolete Python versions. Is there some kind of best practice or recipe > for that? Specifically, the case is a bunch of unittests that use > features new to Python 2.7. if sys.version < "2.7": ... Or test for features and either provide your own, or disable functionality not available: class MyTest: try: @unittest.FancyFeature def testFancy(self): self.assertFancy(self, ...) except AttributeError: pass -- Steven From d at davea.name Tue Dec 20 14:57:15 2011 From: d at davea.name (Dave Angel) Date: Tue, 20 Dec 2011 14:57:15 -0500 Subject: Text Processing In-Reply-To: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> Message-ID: <4EF0E89B.4050907@davea.name> On 12/20/2011 02:17 PM, Yigit Turgut wrote: > Hi all, > > I have a text file containing such data ; > > A B C > ------------------------------------------------------- > -2.0100e-01 8.000e-02 8.000e-05 > -2.0000e-01 0.000e+00 4.800e-04 > -1.9900e-01 4.000e-02 1.600e-04 > > But I only need Section B, and I need to change the notation to ; > > 8.000e-02 = 0.08 > 0.000e+00 = 0.00 > 4.000e-02 = 0.04 > > Text file is approximately 10MB in size. I looked around to see if > there is a quick and dirty workaround but there are lots of modules, > lots of options.. I am confused. > > Which module is most suitable for this task ? You probably don't need anything but sys (to parse the command options) and os (maybe). open the file for eachline if one of the header lines, continue separate out the part you want print it, formatted as you like Then just run the script with its stdout redirected, and you've got your new file The details depend on what your experience with Python is, and what version of Python you're running. -- DaveA From jerome at jolimont.fr Tue Dec 20 15:03:21 2011 From: jerome at jolimont.fr (=?UTF-8?B?SsOpcsO0bWU=?=) Date: Tue, 20 Dec 2011 21:03:21 +0100 Subject: Text Processing In-Reply-To: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> Message-ID: <20111220210321.77451e19@bouzin.lan> Tue, 20 Dec 2011 11:17:15 -0800 (PST) Yigit Turgut a ?crit: > Hi all, > > I have a text file containing such data ; > > A B C > ------------------------------------------------------- > -2.0100e-01 8.000e-02 8.000e-05 > -2.0000e-01 0.000e+00 4.800e-04 > -1.9900e-01 4.000e-02 1.600e-04 > > But I only need Section B, and I need to change the notation to ; > > 8.000e-02 = 0.08 > 0.000e+00 = 0.00 > 4.000e-02 = 0.04 > > Text file is approximately 10MB in size. I looked around to see if > there is a quick and dirty workaround but there are lots of modules, > lots of options.. I am confused. > > Which module is most suitable for this task ? You could try to do it yourself. You'd need to know what seperates the datas. Tabulation character ? Spaces ? Exemple : Input file ---------- A B C ------------------------------------------------------- -2.0100e-01 8.000e-02 8.000e-05 -2.0000e-01 0.000e+00 4.800e-04 -1.9900e-01 4.000e-02 1.600e-04 Python code ----------- # Open file with open('test1.plt','r') as f: b_values = [] # skip as many lines as needed line = f.readline() line = f.readline() line = f.readline() while line: #start = line.find(u"\u0009", 0) + 1 #seek Tab start = line.find(" ", 0) + 4 #seek 4 spaces #end = line.find(u"\u0009", start) end = line.find(" ", start) b_values.append(float(line[start:end].strip())) line = f.readline() print b_values It gets trickier if the amount of spaces is not constant. I would then try with regular expressions. Perhaps would regexp be more efficient in any case. -- J?r?me From steve+comp.lang.python at pearwood.info Tue Dec 20 15:21:03 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 20:21:03 GMT Subject: Learning Python 2.4 References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Message-ID: <4ef0ee2f$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 19:31:56 +0100, kimma wrote: > Hi there, > > I am about to learn python with "how to think like a computer > scientist". This book is just available for python 2.4. Does it matter > for programming? Python 2.4 is pretty old, and missing a lot of really cool features that came out in 2.5 and above. Having tried to support 2.4 through 2.7 in one code base, I can tell you that the features missing from 2.4 really hurt. But for just learning the language basics, 2.4 should be fine. Just make sure you use one of the 2.x series Pythons (that is, 2.4 up to 2.7) and NOT Python 3. Python 3 intentionally breaks some backwards compatibility in ways that will confuse a beginner to the language unless you learn from a book designed for Python 3. -- Steven From steve+comp.lang.python at pearwood.info Tue Dec 20 15:29:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 20:29:49 GMT Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: Message-ID: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 14:45:06 -0500, Nathan Rice wrote: > PyPi: http://pypi.python.org/pypi/elementwise/0.111220 Version 0.111220? What do you do, bump the version number after every keystroke? -- Steven From invalid at invalid.invalid Tue Dec 20 15:41:24 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 20 Dec 2011 20:41:24 +0000 (UTC) Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> Message-ID: On 2011-12-20, Neil Cerutti wrote: > On 2011-12-20, Grant Edwards wrote: >> Would anybody care to recommend online C++ resources for a long >> time C and Python user? (I'm also familiar with Smalltalk, >> Scheme, FORTRAN, bash, Javascript, and a variety of assembly >> languages.) > > The best book I know of to get you writing useful C++ quickly is > Accelerated C++ by Koenig/Moo. It's not free online, though. It > starts with an excellent introduction to using the STL and works > it's way slowly down the abstraction ladder to using pointers and > inheritance last of all. Oops. I should have mentioned this is for embedded systems programming so templates in general (and STL in particular) are probably off the table. > Iterators turn out to be an excellent starting point for learning > pointers, though since you already know C that won't do you as > much good. -- Grant Edwards grant.b.edwards Yow! Boy, am I glad it's at only 1971... gmail.com From benjamin.kaplan at case.edu Tue Dec 20 15:42:31 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Tue, 20 Dec 2011 15:42:31 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 20, 2011 3:32 PM, "Steven D'Aprano" < steve+comp.lang.python at pearwood.info> wrote: > > On Tue, 20 Dec 2011 14:45:06 -0500, Nathan Rice wrote: > > > PyPi: http://pypi.python.org/pypi/elementwise/0.111220 > > Version 0.111220? What do you do, bump the version number after every > keystroke? > I would just like to point out that today is 2011-12-20. That may be relevant here. > Steven > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Tue Dec 20 15:45:07 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Tue, 20 Dec 2011 15:45:07 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: If you take a moment and examine the version number, you will notice that it is a date code. In my opinion that is far more informative than an arbitrary number. I use the major version number to signify... Wait for it... Major changes :) From python at mrabarnett.plus.com Tue Dec 20 15:47:09 2011 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 20 Dec 2011 20:47:09 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EF0F44D.2050403@mrabarnett.plus.com> On 20/12/2011 20:29, Steven D'Aprano wrote: > On Tue, 20 Dec 2011 14:45:06 -0500, Nathan Rice wrote: > >> PyPi: http://pypi.python.org/pypi/elementwise/0.111220 > > Version 0.111220? What do you do, bump the version number after every > keystroke? > It looks like it's based on the date. I do something similar with the regex module, except that _my_ version number is Y2K compliant. :-) From kimma77 at gmail.com Tue Dec 20 15:48:09 2011 From: kimma77 at gmail.com (kimma) Date: Tue, 20 Dec 2011 21:48:09 +0100 Subject: how to run python-script from the python promt? [absolute newbie] In-Reply-To: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: <4ef0f488$0$10783$4d5ecec7@news.xsusenet.com> Am 18.12.2011 12:00, schrieb nukeymusic: > How can I load a python-script after starting python in the > interactive mode? > I tried with >>>> load 'myscript.py' >>>> myscript.py >>>> myscript > > but none of these works, so the only way I could work further until > now was copy/paste line per line of my python-script to the > interactive mode prompt > I do know how to run the script non-interactively, but what I want to > do is adding lines to the code I have written thus far in interactive > mode. > > thanks in advance > nukey In console, do "python script.py" (after cd'ing into the right directory) From steve+comp.lang.python at pearwood.info Tue Dec 20 15:51:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 20:51:21 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: <4ef0f548$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Dec 2011 19:38:52 -0800, alex23 wrote: > On Dec 20, 7:57?am, Andrew Berg wrote: >> But what about the example he gave about being logged into a customer's >> machine with only ed available? I suppose such fools would not be >> worthy of your business. > > Do you mean directly editing the source code on a production machine? > Because that's pretty much the only scenario I can come up with where > that's plausible. > > If I was doing that, _I_ wouldn't be worth doing business with. > > So you only have ssh & ed: at the very least you should be making > changes against your local copy, TESTING THEM, and then copy&paste > directly onto the remote box. Come on guys, you're treating a throw-away example *way* too seriously. If we're going to hypothesis a customer so insane to only have ed installed on their system[1], chances are they've specified some crazy rule like "You May Not Copy Our Precious, Precious Source Code Onto Another Machine Because That's Copyright Theft". Or whatever. Spend 15 minutes on The Daily WTF and you'll see war stories far more crazy than that. The point I was making is that in the real world, sometimes you don't have the luxury of using the right tool for the job, but have to make do with whatever you have. Languages shouldn't depend on advanced editor features or special keyboards -- that way leads to ColorForth and APL. [1] Possibly because ed is the standard Unix editor. http://www.gnu.org/fun/jokes/ed.msg.html -- Steven From invalid at invalid.invalid Tue Dec 20 15:51:29 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 20 Dec 2011 20:51:29 +0000 (UTC) Subject: [OT] Quick intro to C++ for a Python and C user? References: Message-ID: On 2011-12-20, Grant Edwards wrote: > Would anybody care to recommend online C++ resources for a long time C > and Python user? (I'm also familiar with Smalltalk, Scheme, FORTRAN, > bash, Javascript, and a variety of assembly languages.) > > I have a C++ library to which I need to add a couple minor > wrappers/extensions. I've already done the same for the C version of > the library. Writing test suites for C libraries using Python/ctypes > is pretty cool. :) I stumbled across an undergrad CSci lesson that showed in a couple pages how to subclass something and add a couple methods. All I needed to do was add four methods containing two lines of code each. Unfortunately, I had to muck about with the original library's code to change a couple things from "private" to "protected" to allow me to extend the class to do what needed to be done. Every time I have to do anything with C++ (once every handfull of years) it feels like swimming against the current... It probably would have been simpler to just add the code to the original library's implementation, but that would have introduced a "backwards" dependency in the system's libraries. -- Grant Edwards grant.b.edwards Yow! A dwarf is passing out at somewhere in Detroit! gmail.com From no.email at nospam.invalid Tue Dec 20 16:00:22 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 20 Dec 2011 13:00:22 -0800 Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> Message-ID: <7xr4zzx8k9.fsf@ruckus.brouhaha.com> Grant Edwards writes: > Oops. I should have mentioned this is for embedded systems programming > so templates in general (and STL in particular) are probably off the > table. Templates are how C++ does generics and I'd expect them to appear in be used in embedded programming as well as elsewhere. They can bloat up the code if you're not careful (if function f has several parameters, you can end up with a separate, specialized copy of f for every combination of types that f is called with in the program), but in the typical monomorphic situation they don't add any overhead. I'm not sure about the situation with STL. Anyway, templates aren't terribly hard to understand. From steve+comp.lang.python at pearwood.info Tue Dec 20 16:00:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 21:00:49 GMT Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 15:45:07 -0500, Nathan Rice wrote: > If you take a moment and examine the version number, you will notice > that it is a date code. Not any date code I'm familiar with. 0.111220 doesn't look anything like a date to me. Possibly if the last release was two thousand years ago. I'd rather stick to actively maintained software, if it's all the same with you. > In my opinion that is far more informative than > an arbitrary number. I use the major version number to signify... Wait > for it... Major changes :) Well, that's one opinion. Another opinion is that nobody cares what specific day you release a new version, and that versions 0.191231 and 0.200101 probably aren't that big a difference. -- Steven From nicholas.dokos at hp.com Tue Dec 20 16:04:45 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Tue, 20 Dec 2011 16:04:45 -0500 Subject: Text Processing In-Reply-To: Message from =?us-ascii?Q?=3D=3FUTF-8=3FB=3FSsOpcsO0bWU=3D=3F?= =?us-ascii?Q?=3D?= of "Tue\, 20 Dec 2011 21\:03\:21 +0100." <20111220210321.77451e19@bouzin.lan> References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> <20111220210321.77451e19@bouzin.lan> Message-ID: <7895.1324415085@alphaville.americas.hpqcorp.net> J?r?me wrote: > Tue, 20 Dec 2011 11:17:15 -0800 (PST) > Yigit Turgut a ?crit: > > > Hi all, > > > > I have a text file containing such data ; > > > > A B C > > ------------------------------------------------------- > > -2.0100e-01 8.000e-02 8.000e-05 > > -2.0000e-01 0.000e+00 4.800e-04 > > -1.9900e-01 4.000e-02 1.600e-04 > > > > But I only need Section B, and I need to change the notation to ; > > > > 8.000e-02 = 0.08 > > 0.000e+00 = 0.00 > > 4.000e-02 = 0.04 > > > > Text file is approximately 10MB in size. I looked around to see if > > there is a quick and dirty workaround but there are lots of modules, > > lots of options.. I am confused. > > > > Which module is most suitable for this task ? > > You could try to do it yourself. > Does it have to be python? If not, I'd go with something similar to sed 1,2d foo.data | awk '{printf("%.2f\n", $2);}' Nick From no.email at nospam.invalid Tue Dec 20 16:06:49 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 20 Dec 2011 13:06:49 -0800 Subject: [OT] Quick intro to C++ for a Python and C user? References: Message-ID: <7xmxanx89i.fsf@ruckus.brouhaha.com> Grant Edwards writes: > Unfortunately, I had to muck about with the original library's code to > change a couple things from "private" to "protected" to allow me to > extend the class to do what needed to be done. Every time I have to > do anything with C++ (once every handfull of years) it feels like > swimming against the current... I think this is what's called "the expression problem" and it's a perennial sore point in statically typed PL design. Different languages have different approaches to dealing with it. I'm no C++ expert so I don't know what the gurus would say for your particular situation. But, I do get the impression that PL mavens now tend to think that OO and inheritance was a 1990's thing that didn't work out as well as expected, that Java is a painful wreck because of it, and that C++ culture has moved somewhat away from OO in favor of template-based generics. From no.email at nospam.invalid Tue Dec 20 16:08:09 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 20 Dec 2011 13:08:09 -0800 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <7xiplbx87a.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Not any date code I'm familiar with. 0.111220 doesn't look anything like > a date to me. 0 is the major version number. 111220 (the minor version number) is the date code, 2011-12-20 being today. From rosuav at gmail.com Tue Dec 20 16:10:26 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Dec 2011 08:10:26 +1100 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4EF0F44D.2050403@mrabarnett.plus.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4EF0F44D.2050403@mrabarnett.plus.com> Message-ID: On Wed, Dec 21, 2011 at 7:47 AM, MRAB wrote: > It looks like it's based on the date. I do something similar with the > regex module, except that _my_ version number is Y2K compliant. :-) The eight-digit date is a recognized entity. The truncated six-digit form, much less so. But either is better known than what PPWizard uses, which involves the day-of-year... ChrisA From nathan.alexander.rice at gmail.com Tue Dec 20 16:20:37 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Tue, 20 Dec 2011 16:20:37 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Dec 20, 2011 at 4:00 PM, Steven D'Aprano wrote: > On Tue, 20 Dec 2011 15:45:07 -0500, Nathan Rice wrote: > >> If you take a moment and examine the version number, you will notice >> that it is a date code. > > Not any date code I'm familiar with. 0.111220 doesn't look anything like > a date to me. > > Possibly if the last release was two thousand years ago. I'd rather stick > to actively maintained software, if it's all the same with you. Date code != date. >> In my opinion that is far more informative than >> an arbitrary number. ?I use the major version number to signify... Wait >> for it... Major changes :) > > Well, that's one opinion. Another opinion is that nobody cares what > specific day you release a new version, and that versions 0.191231 and > 0.200101 probably aren't that big a difference. Nobody cares about version numbers in general, except as a way to fulfill dependencies. By using a date code, your versions are guaranteed to sort in release order (which is nice, say if someone was to download your software via FTP), you can tell what release has what ticket fixes in an issue tracker, stuff like that. It also gives me an easy way to be nostalgic about releases.... As for the extra "20" that I exclude, if I haven't updated the major version number by the time 2020 rolls around I deserve any trouble it causes :) From wimpybobcat at gmail.com Tue Dec 20 17:15:43 2011 From: wimpybobcat at gmail.com (abhishek bhat) Date: Tue, 20 Dec 2011 14:15:43 -0800 (PST) Subject: Cross compiling Python 3.2 for PPC (MPC 8641D) Message-ID: I am trying to compile Python 3.2 to a PPC platform running a lesser known OS called OSE (by ENEA). The problem is that there is no shell which can be used to run the configure script which means that I need to manually port the pyconfig.h and the makefile for the given platform. Any suggestions on the best way to achieve this is much appreciated? From joshua.landau.ws at gmail.com Tue Dec 20 17:16:30 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Dec 2011 22:16:30 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4ef0f548$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> <4ef0f548$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 20 December 2011 20:51, Steven D'Aprano < steve+comp.lang.python at pearwood.info> wrote: > Languages shouldn't depend on advanced editor > features or special keyboards -- that way leads to ColorForth and APL. > True, but dependency on a tool is not the same as being crafted to work best on the tool that 99.9% of programmers use. It's not like it's an ide-*only *feature we were discussing. It's just an observation that this feature* works well with syntax highlighting. *That's a good thing.* * * *What feature are we discussing anyway? I can't remember... Oh! It's actually that "vargs" will be a highlighted keyword! Well, someone using ed will obviously have to stop using python on the basis that "vargs" isn't highlighted, then, which makes it a bad idea. *No*, what makes it a bad idea is the concept itself. As I've said, the 1/10,000 using ed have bigger problems. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ladasky at my-deja.com Tue Dec 20 17:43:16 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 20 Dec 2011 14:43:16 -0800 (PST) Subject: Upgraded Ubuntu -> 11.10, Python -> 2.7.2. Now psyco doesn't work? Message-ID: <222051f5-35b7-44ae-857f-bb819bae0a3e@z22g2000prd.googlegroups.com> I'm chasing down a bug that I think may be in my own code. But a fellow Pythoneer suspected that I might actually be looking at a bug in Python itself. http://groups.google.com/group/comp.lang.python/browse_thread/thread/f4b5843e2b06d1a6 Because I was using Python 2.6.6, and I was told that only bugs which affect language security will be fixed in older revisions, I was encouraged to try my code on Python 2.7.2. I was running Ubuntu Linux 10.10, on which Python 2.6.6 was the standard distribution. I succeeded in doing an "alternate install" of Python 2.7.2, over Ubuntu 10.10. But I use so many packages in my code. Dealing with the package dependencies, and the intricacies of invoking the right version of Python at the command prompt, were making me dizzy. After a few hours of struggle, I just decided to upgrade my OS to Ubuntu 11.10, on which Python 2.7.2 is the standard distribution. I then installed numpy, scipy, and matplotlib, and confirmed that they work. I needed just one more package to try my code: psyco. According to the Synaptic Package Manager of Ubuntu 11.10, there is a supported version of psyco for this OS revision, python-psyco 1.6-2ubuntu1. I downloaded it. It installed. But an attempt to "import psyco" gives me an ImportError at the python prompt. I'm not sure whether my OS upgrade is responsible for this, or whether the accompanying Python revision bump is responsible. I can remove the psyco dependency for now, as it's not essential for me to test my bug. But I will eventually want to do some JIT compilation on a critical inner loop in my code. Applying psyco to that loop nearly doubled my execution speed. Can I fix my import problem? Is psyco being superseded by PyPy? From what I've seen of PyPy, it's pretty radically different than psyco. Thanks for any help! From alex.kapps at web.de Tue Dec 20 19:01:41 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Wed, 21 Dec 2011 01:01:41 +0100 Subject: Text Processing In-Reply-To: <7895.1324415085@alphaville.americas.hpqcorp.net> References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> <20111220210321.77451e19@bouzin.lan> <7895.1324415085@alphaville.americas.hpqcorp.net> Message-ID: <4EF121E5.9020300@web.de> On 20.12.2011 22:04, Nick Dokos wrote: >>> I have a text file containing such data ; >>> >>> A B C >>> ------------------------------------------------------- >>> -2.0100e-01 8.000e-02 8.000e-05 >>> -2.0000e-01 0.000e+00 4.800e-04 >>> -1.9900e-01 4.000e-02 1.600e-04 >>> >>> But I only need Section B, and I need to change the notation to ; >>> >>> 8.000e-02 = 0.08 >>> 0.000e+00 = 0.00 >>> 4.000e-02 = 0.04 > Does it have to be python? If not, I'd go with something similar to > > sed 1,2d foo.data | awk '{printf("%.2f\n", $2);}' > Why sed and awk: awk 'NR>2 {printf("%.2f\n", $2);}' data.txt And in Python: f = open("data.txt") f.readline() # skip header f.readline() # skip header for line in f: print "%02s" % float(line.split()[1]) From ian.g.kelly at gmail.com Tue Dec 20 19:03:26 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Dec 2011 17:03:26 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 12:45 PM, Nathan Rice wrote: > There are still some issues with proper support of things like bool() > and int(), which refuse to return things that are not of the correct > type. And that's a good thing. As type conversion functions, bool(x) and int(x) should *always* return bools and ints respectively (or raise an exception), no matter what you pass in for x. If I do "list(efoo)", where efoo is an ElementwiseProxy object, should I expect to get the efoo collection converted to a list, or another ElementwiseProxy where each element has been converted to a list? I would hope the former. > This was developed as a proof of concept for expanding the role of > element-wise syntax in python, and to that end I welcome comments. The examples you gave are all numerical in nature. If I might inquire, what might I use this for that I can't already do with numpy? Cheers, Ian From malaclypse2 at gmail.com Tue Dec 20 19:08:13 2011 From: malaclypse2 at gmail.com (Jerry Hill) Date: Tue, 20 Dec 2011 19:08:13 -0500 Subject: Upgraded Ubuntu -> 11.10, Python -> 2.7.2. Now psyco doesn't work? In-Reply-To: <222051f5-35b7-44ae-857f-bb819bae0a3e@z22g2000prd.googlegroups.com> References: <222051f5-35b7-44ae-857f-bb819bae0a3e@z22g2000prd.googlegroups.com> Message-ID: On Tue, Dec 20, 2011 at 5:43 PM, John Ladasky wrote: > I was running Ubuntu Linux 10.10, on which Python 2.6.6 was the > standard distribution. ?I succeeded in doing an "alternate install" of > Python 2.7.2, over Ubuntu 10.10. ?But I use so many packages in my > code. ?Dealing with the package dependencies, and the intricacies of > invoking the right version of Python at the command prompt, were > making me dizzy. ?After a few hours of struggle, I just decided to > upgrade my OS to Ubuntu 11.10, on which Python 2.7.2 is the standard > distribution. > > I then installed numpy, scipy, and matplotlib, and confirmed that they > work. ?I needed just one more package to try my code: psyco. > According to the Synaptic Package Manager of Ubuntu 11.10, there is a > supported version of psyco for this OS revision, python-psyco > 1.6-2ubuntu1. ?I downloaded it. ?It installed. ?But an attempt to > "import psyco" gives me an ImportError at the python prompt. >From the psyco homepage ( http://psyco.sourceforge.net/ ) "Python 2.7 is unsupported so far. Anyone interested in porting Psyco to it is welcome. I started the work in a branch but it needs finishing." That's the most recent news item on the home page, and it's dated July 16, 2010, about a year and a half ago. I don't think psyco supports python 2.7. -- Jerry From steve+comp.lang.python at pearwood.info Tue Dec 20 19:12:08 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Dec 2011 00:12:08 GMT Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> <7xiplbx87a.fsf@ruckus.brouhaha.com> Message-ID: <4ef12458$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 13:08:09 -0800, Paul Rubin wrote: > Steven D'Aprano writes: >> Not any date code I'm familiar with. 0.111220 doesn't look anything >> like a date to me. > > 0 is the major version number. 111220 (the minor version number) is the > date code, 2011-12-20 being today. Not here it isn't. It is yesterday. Writing-from-Down-Under-ly y'rs, -- Steven From nathan.alexander.rice at gmail.com Tue Dec 20 19:24:49 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Tue, 20 Dec 2011 19:24:49 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 7:03 PM, Ian Kelly wrote: > On Tue, Dec 20, 2011 at 12:45 PM, Nathan Rice > wrote: >> There are still some issues with proper support of things like bool() >> and int(), which refuse to return things that are not of the correct >> type. > > And that's a good thing. ?As type conversion functions, bool(x) and > int(x) should *always* return bools and ints respectively (or raise an > exception), no matter what you pass in for x. I was hoping to have the proxy be completely transparent. I don't disagree with your statement though. > If I do "list(efoo)", where efoo is an ElementwiseProxy object, should > I expect to get the efoo collection converted to a list, or another > ElementwiseProxy where each element has been converted to a list? ?I > would hope the former. Iterators are how you go from an ElementwiseProxy back to a regular collection. Thus list/set/etc or anything that takes an iterator will work. >> This was developed as a proof of concept for expanding the role of >> element-wise syntax in python, and to that end I welcome comments. > > The examples you gave are all numerical in nature. ?If I might > inquire, what might I use this for that I can't already do with numpy? efoo2 = ElementwiseProxy(["one", "two", "three", "four"]) efoo_res = ((efoo2.capitalize() + " little indian").split(" ").apply(reversed) * 2).apply("_".join) # note that you could do reversed(...) instead, I just like to read left to right efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " little indian").split(" ") in case you need to debug something and want to look at intermediate values The idea is to provide a syntax that lets you do very complex things on collections in a more readable manner, without having 5 or 6 lines of generator expressions. Nathan From steve+comp.lang.python at pearwood.info Tue Dec 20 19:28:33 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Dec 2011 00:28:33 GMT Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef12831$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 16:20:37 -0500, Nathan Rice wrote: >> Well, that's one opinion. Another opinion is that nobody cares what >> specific day you release a new version, and that versions 0.191231 and >> 0.200101 probably aren't that big a difference. > > Nobody cares about version numbers in general, except as a way to > fulfill dependencies. By using a date code, your versions are > guaranteed to sort in release order (which is nice, say if someone was > to download your software via FTP), you can tell what release has what > ticket fixes in an issue tracker, stuff like that. *blink* It must be nice to have software with so few bugs that you can remember the date you fixed each and every one just from the ticket number. I don't actually care what version numbering scheme you use. I had just never come across using part of a date as the version number before, and I don't think that it is any more recognisable or sensible than any other arbitrary numbering scheme. And it is arbitrary: the fact that you happened to make a release on Tuesday rather than Wednesday doesn't have any meaning (particularly if you're a night owl who is coding at midnight) and yet you'll assign a different version number to it. To my mind, the version number should encode (in some sense) the feature set, not the date you release it. It also has the serious disadvantage that you're limited to a single release per day, unless you add an extra field to the version string. But whatever floats your boat. I'm just glad that you've put your money where your mouth is, and released the package, instead of demanding others do the work. Thank you. -- Steven From nathan.alexander.rice at gmail.com Tue Dec 20 19:31:02 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Tue, 20 Dec 2011 19:31:02 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: > efoo2 = ElementwiseProxy(["one", "two", "three", "four"]) > > efoo_res = ((efoo2.capitalize() + " little indian").split(" > ").apply(reversed) * 2).apply("_".join) # note that you could do > reversed(...) instead, I just like to read left to right > efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " > little indian").split(" ") in case you need to debug something and > want to look at intermediate values Just a quick note, I realized after I sent this that reversed returns an iterator, so * will not work. Otherwise everything is kosher. From rantingrickjohnson at gmail.com Tue Dec 20 19:31:42 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 20 Dec 2011 16:31:42 -0800 (PST) Subject: Learning Python 2.4 References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Message-ID: <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> On Dec 20, 12:31?pm, kimma wrote: > Hi there, > > I am about to learn python with "how to think like a computer > scientist". This book is just available for python 2.4. Does it matter > for programming? > > Greetz Kimma, don't listen to either of these guys. Anything before Python 3.0 is now obsolete. We are currently at 3.2.2 for a stable release. If a tutorial writer is too lazy to update his tutorial to AT LEAST Python 3.0, THEN he is a fool and should be banished from not only this community, but from the Internets also. The ONLY reason to use any Python version LESS than Python 3.0 is if you have NO OTHER CHOICE! You don't have that problem kimma because you are new to Python. You have a choice. And the choice should be the MOST CURRENT VERSION OF PYTHON. If, at a later time, you find that you need to use some module that is not 3.0 compliant, then by all means download the appropriate interpreter. BUT DON"T WRITE CODE THAT IS NOT 3.0 COMPLIANT UNLESS YOU HAVE NO OTHER CHOICE!!! Don't be a part of slowing the transition that is happening with Python. We need to look forward and forget about the past. The past is old and dumpy, however, the future is bright! Good luck! From ian.g.kelly at gmail.com Tue Dec 20 19:47:47 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Dec 2011 17:47:47 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 12:45 PM, Nathan Rice wrote: > Get it: > > ? ?PyPi: http://pypi.python.org/pypi/elementwise/0.111220 > ? ?GitHub: https://github.com/nathan-rice/Elementwise > > This was developed as a proof of concept for expanding the role of > element-wise syntax in python, and to that end I welcome comments. A couple of notes on the code: @property def iterable(self): iterable = object.__getattribute__(self, "_iterable") if isinstance(iterable, types.GeneratorType): # Should probably look into a better way to do this return itertools.tee(iterable, 1)[0] else: return iterable I don't think tee is going to do what you think it does here. The semantics of tee are that once you tee off an iterable, you're no longer supposed to use the original iterable. "itertools.tee(iterable, 1)[0]" is more or less equivalent to "iter(iterable)". A more correct approach might be: @property def iterable(self): iterable = object.__getattribute__(self, "_iterable") if isinstance(iterable, collections.Iterator): iterator, self.iterable = itertools.tee(iterable, 2) return iterator else: return iterable That also checks for iterators specifically rather than just generators. Alternatively, you could just check for a "__next__" or "next" method, depending on your Python version, if you don't want to introduce a dependency on Python 2.6. ---------- def __str__(self): return ", ".join(e.__str__() for e in object.__getattribute__(self, "iterable")) ... def __unicode__(self): return object.__unicode__(str(self)) So "unicode(efoo)" will first construct a str from each object in the iterable, and then attempt to decode that str using the default ASCII codec, which may or may not be correct. The attempt at code reuse is admirable, but really __unicode__ should not rely on __str__ if the encoding is unknown. This ought to be: def __unicode__(self): return u", ".join(unicode(e) for e in object.__getattribute__(self, "iterable")) If the objects in question don't have __unicode__ methods, then Python will fall back on __str__ and the default encoding for you. That's all that jumps out at me. Also, are you aware that your README is empty? Although maybe that's just some weird github thing. Cheers, Ian From ladasky at my-deja.com Tue Dec 20 20:16:53 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 20 Dec 2011 17:16:53 -0800 (PST) Subject: Upgraded Ubuntu -> 11.10, Python -> 2.7.2. Now psyco doesn't work? References: <222051f5-35b7-44ae-857f-bb819bae0a3e@z22g2000prd.googlegroups.com> Message-ID: <0825ebed-b501-42b7-975b-cc0e1aaec195@37g2000prc.googlegroups.com> On Dec 20, 4:08?pm, Jerry Hill wrote: > From the psyco homepage (http://psyco.sourceforge.net/) "Python 2.7 > is unsupported so far. Anyone interested in porting Psyco to it is > welcome. I started the work in a branch but it needs finishing." > That's the most recent news item on the home page, and it's dated July > 16, 2010, about a year and a half ago. > > I don't think psyco supports python 2.7. > > -- > Jerry Thanks, Jerry. It's funny that the package tree of Ubuntu 11.10 doesn't specify a dependency on an older revision of Python, then. From joshua.landau.ws at gmail.com Tue Dec 20 20:37:23 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 01:37:23 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 00:24, Nathan Rice wrote: > efoo_res = ((efoo2.capitalize() + " little indian").split(" > ").apply(reversed) * 2).apply("_".join) # note that you could do > reversed(...) instead, I just like to read left to right > efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " > little indian").split(" ") in case you need to debug something and > want to look at intermediate values > How is any of this better than the elementwise operators ("~")? People should be able to expect len(x) to *always* return a number or raise an error. I know it's not part of the spec, but a lot breaks without these guarantees. When "str(x)" isn't a string, *all the formatting code breaks**. And when the other suggestion ("~str(x)" or "str~(x)" or something similar) has all the benifits and none of the drawbacks, why should I use this? Upon this implementation I take back my comment on the whole typing thing. Your title just really confused me. * You can't just make functions non-elementwise unless called through ".apply" either: def str(x): return x.__str__() Also note that I'm ignoring that "~" has a predefined meaning, making "~func(x)" valid already. That's a minor syntactic point, not a point of the proposal, that i stuck with just 'cause of the PEP. The idea is to provide a syntax that lets you do very complex things > on collections in a more readable manner, without having 5 or 6 lines > of generator expressions. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fperez.net at gmail.com Tue Dec 20 20:40:54 2011 From: fperez.net at gmail.com (Fernando Perez) Date: Wed, 21 Dec 2011 01:40:54 +0000 (UTC) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: On Mon, 19 Dec 2011 19:51:00 -0800, Raymond Hettinger wrote: > Do you use IDLE when teaching Python? If not, what is the tool of > choice? I'm obviously biased (I started IPython years ago), but I've done a lot of teaching and I still do like the combination of IPython plus an editor. Sometimes I use IDLE configured to only open the editor and not the shell, but I recommend that users learn a 'real' editor for the long run (aka emacs/vim), as it's an investment that will pay off many times over. But if nothing else, there's at least an OK free editor for each OS that does work, and I keep a 'starter kit' page with those resources for my students: http://fperez.org/py4science/starter_kit.html Cheers, f From mavjs01 at gmail.com Tue Dec 20 20:42:03 2011 From: mavjs01 at gmail.com (Maverick JS) Date: Tue, 20 Dec 2011 17:42:03 -0800 (PST) Subject: Learning Python 2.4 In-Reply-To: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Message-ID: <612275.52.1324431723390.JavaMail.geo-discussion-forums@pril5> I'd recommend starting with http://learnpythonthehardway.org/book/ by "Zed Shaw" or Beginning Python Using Python 2.6 and Python 3.1 by "James Payne" I'd say learning Python 2.6 would be better than 2.4, since it's really old. From ashton at fagg.id.au Tue Dec 20 21:06:33 2011 From: ashton at fagg.id.au (Ashton Fagg) Date: Wed, 21 Dec 2011 12:06:33 +1000 Subject: Python education survey In-Reply-To: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: On 20 December 2011 13:51, Raymond Hettinger wrote: > Students may not be experienced with the command-line and may be > running Windows, Linux, or Macs. ?Ideally, the tool or IDE will be > easy to install and configure (startup directory, path, associated > with a particular version of Python etc). I tutor people (usually fellow students) in programming occasionally, and I've always recommended a simple text editor and a command line (this correlates with most languages, not just Python). My personal set up (using Linux) is vim (with line numbers and syntax highlighting) + shell, no matter which language I'm working with. However for people I'm tutoring, particularly if they're new to programming in general and would find vim intimidating, I recommend gedit (for Linux) or Notepad++ (for Windows), executing/compiling from the command line. As long as the text editor has line numbers and syntax highlighting it's sufficient in my book. I don't like obfuscating what's going on in the background (i.e. interacting with the Python/C/ interpreter/compiler/whatever) with a fancy IDE. However that is my personal (strong) opinion. Hope that helps. -- Ashton Fagg E-mail: ashton at fagg.id.au Web: http://www.fagg.id.au/~ashton/ Keep calm and call Batman. From ashton at fagg.id.au Tue Dec 20 21:13:07 2011 From: ashton at fagg.id.au (Ashton Fagg) Date: Wed, 21 Dec 2011 12:13:07 +1000 Subject: Learning Python 2.4 In-Reply-To: <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On 21 December 2011 10:31, Rick Johnson wrote: > Kimma, don't listen to either of these guys. Anything before Python > 3.0 is now obsolete. We are currently at 3.2.2 for a stable release. > If a tutorial writer is too lazy to update his tutorial to AT LEAST > Python 3.0, THEN he is a fool and should be banished from not only > this community, but from the Internets also. I got the impression the OP was learning programming in general (i.e. from scratch) and not merely "learning Python". If this is the case it shouldn't matter if they're merely learning the concepts as you can always get up to speed on the differences later on as they get more experienced. If they are "learning Python" (i.e. have programmed previously and are learning this as a new language), learning the basics from the book would be fine and it shouldn't be too hard to get up to speed on the newer stuff later on. Although I'd recommend starting on the latest iteration if possible, as it will alleviate some of the leg work of catching up on the newer features. Cheers. -- Ashton Fagg E-mail: ashton at fagg.id.au Web: http://www.fagg.id.au/~ashton/ Keep calm and call Batman. From roy at panix.com Tue Dec 20 21:34:38 2011 From: roy at panix.com (Roy Smith) Date: Tue, 20 Dec 2011 21:34:38 -0500 Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: In article , Ashton Fagg wrote: > As long as the text editor has line numbers and syntax highlighting > it's sufficient in my book. I agree with the syntax highlighting. I resisted for many years, then somebody turned me on to it a few years ago and I've been addicted ever since. As for line numbers, for working alone, I don't see much point. But for any kind of interaction with other people, it's essential. It's just SO much easier to say, "line 417" as opposed to "OK, scroll up a couple more lines, no, no, not that far, go down a little. There! See that print statement, now go down three lines below that, ..." By the time the two of you are on the same page about which line of code you're talking about, you will have forgotten what you wanted to say. From rosuav at gmail.com Tue Dec 20 21:44:27 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Dec 2011 13:44:27 +1100 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: On Wed, Dec 21, 2011 at 1:34 PM, Roy Smith wrote: > As for line numbers, for working alone, I don't see much point. ?But for > any kind of interaction with other people, it's essential. ?It's just SO > much easier to say, "line 417" as opposed to "OK, scroll up a couple > more lines, no, no, not that far, go down a little. ?There! ?See that > print statement, now go down three lines below that, ..." ?By the time > the two of you are on the same page about which line of code you're > talking about, you will have forgotten what you wanted to say. Ctrl-G in many editors will let you GOTO (sorry, I'll try to keep this PG-13) a specific line by number. Extremely handy, especially when you're debugging across computers - the error message comes up on your test computer and cites the line number, and you jump to it on your dev easily. ChrisA From rantingrickjohnson at gmail.com Tue Dec 20 21:50:29 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 20 Dec 2011 18:50:29 -0800 (PST) Subject: Learning Python 2.4 References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On Dec 20, 8:13?pm, Ashton Fagg wrote: > On 21 December 2011 10:31, Rick Johnson wrote: > I got the impression the OP was learning programming in general (i.e. > from scratch) and not merely "learning Python". If this is the case it > shouldn't matter if they're merely learning the concepts as you can > always get up to speed on the differences later on as they get more > experienced. I say that assumption is wrong; read on for details... > If they are "learning Python" (i.e. have programmed previously and are > learning this as a new language), learning the basics from the book > would be fine and it shouldn't be too hard to get up to speed on the > newer stuff later on. Although I'd recommend starting on the latest > iteration if possible, as it will alleviate some of the leg work of > catching up on the newer features. I don't see how "experienced" or "un-experienced" has anything to do with this. The fact is... Python 2.4 is dead. Python 2.5 is dead. Python 2.6 is dead, and yes, even python 2.7 is bereft of life! The only Python version that matters at this moment in "time space" is Python 3.2.2. The only exception to that rule is if you need an old module that 3.0 does not support. AND THAT IS THE ONLY EXCEPTION! I currently have Python 2.7 and Python 3.2.2 installed on my machine. I don't use Python 2.7 because i am a member of some secret "Python Historical Commission", NO. I use Python 2.7 because i have no other choice. Much of my old code bases requires modules that DO NOT work in Python 3.0. Some 3rd party devs are even suggesting that they will NEVER upgrade...HOWEVER... I write ALL new code with Python 3.0 compliance. Anyone who would refuse to use Python 3.0 for new code is a fool. You may not realize it now, but you will be kicking yourself later. There are individual losses and collective losses from each person who thinks they should keep some old run down building from the wrecking ball. Progress requires tearing down old, worn out structures, and replacing them with cleaner, safer, and shiny-er structures. Structures that are NOT infested with vermin. Structures that are NOT a fire hazard. Structures that are NOT insulated with asbestos! Remember, back in the day, asbestos was the "wonder material". Look, it does not burn! Look, it does not rot! Look, it will even wipe your back side! Oh by the way, you might get lung cancer and die from one breath of it's dust. But no need to worry about that little hiccup for many years to come. Collectively we as a community suffer when even one new lad chooses to use an old version of Python. Why? Because that is one less person pushing the masses of 3rd party developers to get off their lazy bums and upgrade to 3000 compliance. Avoid Python<3.X like the plague -- and stop propagating stagnation! From rosuav at gmail.com Tue Dec 20 22:41:38 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Dec 2011 14:41:38 +1100 Subject: Learning Python 2.4 In-Reply-To: References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On Wed, Dec 21, 2011 at 1:50 PM, Rick Johnson wrote: > I currently have Python 2.7 and Python 3.2.2 installed on my machine. > I don't use Python 2.7 because i am a member of some secret "Python > Historical Commission" Shun 3.2.2, it's way too old. The ONLY version of Python worth using is the bleeding-edge from source control! Otherwise you ARE on the Historical Commission. Seriously... to the OP, ignore Rick's doom-and-gloom about old Pythons. If you have a book that teaches 2.4, you can almost certainly use what it teaches with a more recent 2.X interpreter (eg 2.7.2). You may have problems trying to use that book with a 3.X interpreter, though. ChrisA From wuwei23 at gmail.com Tue Dec 20 23:27:07 2011 From: wuwei23 at gmail.com (alex23) Date: Tue, 20 Dec 2011 20:27:07 -0800 (PST) Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: Message-ID: On Dec 21, 10:24?am, Nathan Rice wrote: > The idea is to provide a syntax that lets you do very complex things > on collections in a more readable manner, without having 5 or 6 lines > of generator expressions. Have you seen PyLINQ? It has a similar approach to operating on collections, returning a PyLINQ object after each call to facilitate chaining. https://github.com/kalessin/PyLINQ/blob/master/pylinq/linq.py This is a personal opinion on the code, but I'd move instantiating the new ElementwiseProxy out of each method and into its own decorator: # declare this outside of the class def chainable(fn): def _(self, *args, **kwargs): return ElementwiseProxy(fn(self, *args, **kwargs), self) return _ This way, each method that is chainable is a little more obvious without inspecting the code, and the method body itself is only doing what the method says it does: @chainable def __add__(self, other): return (e + other for e in object.__getattribute__(self, "iterable")) Incidentally, displaying an ElementwiseProxy instance doesn't go down well with iPython: In [1]: from elementwise import * In [2]: e = ElementwiseProxy(['one','two','three']) In [3]: e Out[3]: ERROR: An unexpected error occurred while tokenizing input The following traceback may be corrupted or invalid The error message is: ('EOF in multi-line statement', (6, 0)) From python at mrabarnett.plus.com Tue Dec 20 23:30:51 2011 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 21 Dec 2011 04:30:51 +0000 Subject: [Python-ideas] =?UTF-8?B?5Zue5aSN77yaICBDb2x1bW5pemUgaW4gbW9k?= =?UTF-8?B?dWxlICJjbWQi?= In-Reply-To: <1324438134.11446.YahooMailNeo@web92408.mail.cnh.yahoo.com> References: <1324349593.51908.YahooMailNeo@web92411.mail.cnh.yahoo.com> <1324350440.30993.YahooMailNeo@web92413.mail.cnh.yahoo.com> <1324438134.11446.YahooMailNeo@web92408.mail.cnh.yahoo.com> Message-ID: <4EF160FB.50500@mrabarnett.plus.com> On 21/12/2011 03:28, Tom Zhou wrote: > Thanks a lot for your reply! > I've reservations, maybe code looks like: > """ > colwidth = max(map(len, list)) > ncols = displaywidth/(colwidth+2)+1 > nrows = size/ncols+1 > for nrows > for ncols > texts = list[...] > output texts > """ > is clear, and just waste some space of stdout. > A couple of points: 1. Trying not to use the names of built-in classes or functions for your variable names, for example "list". 2. In Python 2 int/int returns an int, but in Python 3 int/int returns a float. In both cases int//int returns an int (at least for recent versions of Python 2), so it's a good idea to always use // when you want to do integer division. From emekamicro at gmail.com Tue Dec 20 23:39:43 2011 From: emekamicro at gmail.com (Emeka) Date: Wed, 21 Dec 2011 06:39:43 +0200 Subject: Please explain this for me Message-ID: Hello All, v = [] def add_to_list(plist): u = plist.append(90) return u add_to_list(v) # This function call returns nothing Could someone explain why this function call will return nothing? v = [90] # Object values are passed by reference This one is clear to me add_to_list([]) This one returns nothing, why? Regards, Emeka -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From ironfroggy at gmail.com Tue Dec 20 23:54:36 2011 From: ironfroggy at gmail.com (Calvin Spealman) Date: Tue, 20 Dec 2011 23:54:36 -0500 Subject: Please explain this for me In-Reply-To: References: Message-ID: On Dec 20, 2011 11:41 PM, "Emeka" wrote: > > > Hello All, > > v = [] > > def add_to_list(plist): > u = plist.append(90) > return u > > add_to_list(v) # This function call returns nothing > Could someone explain why this function call will return nothing? > > v = [90] # Object values are passed by reference > This one is clear to me > > add_to_list([]) > This one returns nothing, why? Because list.append() returns nothing, it only modifies the list in place. > > Regards, > Emeka > -- > Satajanus Nig. Ltd > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From enalicho at gmail.com Tue Dec 20 23:57:26 2011 From: enalicho at gmail.com (Noah Hall) Date: Wed, 21 Dec 2011 04:57:26 +0000 Subject: Please explain this for me In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 4:39 AM, Emeka wrote: > > Hello All, > v = [] > > def add_to_list(plist): > ? ? u = plist.append(90) > ? ? return u > > add_to_list(v) ?# This function call returns nothing > Could someone explain why this function call will return nothing? It's because add_to_list returns the value returned from plist.append stored in u. append changes a list in place and returns nothing. Functions that return nothing return None. This is why it'll be None - u is None because append returns None. > add_to_list([]) > This one returns nothing, why? It's because the object [] here has no name, so that you have no way to refer to it after the function changes it, since it changes it in place. It gets eaten by Python, never to be seen again. From emekamicro at gmail.com Wed Dec 21 00:12:40 2011 From: emekamicro at gmail.com (Emeka) Date: Wed, 21 Dec 2011 07:12:40 +0200 Subject: Please explain this for me In-Reply-To: References: Message-ID: Noah, Calvin Thanks so much! Regards, Emeka On Wed, Dec 21, 2011 at 6:57 AM, Noah Hall wrote: > On Wed, Dec 21, 2011 at 4:39 AM, Emeka wrote: > > > > Hello All, > > > v = [] > > > > def add_to_list(plist): > > u = plist.append(90) > > return u > > > > add_to_list(v) # This function call returns nothing > > Could someone explain why this function call will return nothing? > > It's because add_to_list returns the value returned from plist.append > stored in u. > append changes a list in place and returns nothing. Functions that > return nothing return None. This is why it'll be None - u is None > because append returns None. > > > > add_to_list([]) > > This one returns nothing, why? > > It's because the object [] here has no name, so that you have no way > to refer to it after the function changes it, since it changes it in > place. It gets eaten by Python, never to be seen again. > -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From nad at acm.org Wed Dec 21 01:42:05 2011 From: nad at acm.org (Ned Deily) Date: Wed, 21 Dec 2011 01:42:05 -0500 Subject: Can't get tilde character with IDLE 3.2.2 on French Mac Lion References: Message-ID: In article , Franck Ditter wrote: > In article , > Ned Deily wrote: > > In article , > > Franck Ditter wrote: > > > All is in the subject. I'm starting to use Python with Idle 3.2.2 > > > on MacOS-X Lion (French). I can't get "Option-N space" to provide > > > the ~ char. > > > I tried to go into the Keys preferences but I can't find "Option-N space" > > > to modify its meaning. Its actual behavior is to merge lines of a > > > paragraph. > > You are likely running into a current problem in the OS X Cocoa version > > of Tcl/Tk 8.5 as included with Lion and as shipped by ActiveState. > > Previously, if you tried to type composite characters, like Option N, > > the Cocoa Tcl/Tk would crash. Pending a real fix, a patch was made to > > Tcl/Tk 8.5 to discard composite characters rather than crash. You > > should be able to get a tilde by using the post-composite keyboard > > sequence: try typing "space" followed by "Shift-Option-N". > > > > http://sourceforge.net/tracker/index.php?func=detail&aid=2907388&group_id=12 > > 997&atid=112997 > Nope, "space" followed by "Shift-Option-N" gives a greek iota... > I tried other combinations, unsuccessfully. > IDLE 3 (French) seems to be unusable as we use many ~ in web applications :-( > Should we hope a fix soon, or leave IDLE ? Yes, I see now that that won't work with the French input method. Unfortunately, there is nothing that IDLE or Python can do to workaround the issue as the problem is in Cocoa Tcl/Tk and I don't know that any fix is being worked on. One approach that *might* work is to create a modified French keyboard layout that substitutes tilde for some other key combination without using a dead key (Option-N is a dead key). Ukelele.app seems to be the most frequently recommended keyboard layout editor for OS X and comes with extensive documentation. I don't have time right now to experiment with it but I would be interested to hear if someone is able to make it work. http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=ukelele -- Ned Deily, nad at acm.org From chris at simplistix.co.uk Wed Dec 21 02:15:46 2011 From: chris at simplistix.co.uk (Chris Withers) Date: Wed, 21 Dec 2011 07:15:46 +0000 Subject: Anyone still using Python 2.5? Message-ID: <4EF187A2.6070909@simplistix.co.uk> Hi All, What's the general consensus on supporting Python 2.5 nowadays? Do people still have to use this in commercial environments or is everyone on 2.6+ nowadays? I'm finally getting some continuous integration set up for my packages and it's highlighting some 2.5 compatibility issues. I'm wondering whether to fix those (lots of ugly "from __future__ import with_statement" everywhere) or just to drop Python 2.5 support. What do people feel? cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk From stefan_ml at behnel.de Wed Dec 21 03:35:35 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 21 Dec 2011 09:35:35 +0100 Subject: Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: Chris Withers, 21.12.2011 08:15: > What's the general consensus on supporting Python 2.5 nowadays? From my own (recent) polls, it appears that people want continued support for Python 2.4 and later for a couple of years to come, mainly because RHEL5 uses that by default and has official support until 2014. Similar considerations apply to many Solaris installations. The general consensus seems to be that support for Python 2.3 can easily be dropped, but that support for Py2.4 and later would be helpful. Stefan From pierre-yves.david at logilab.fr Wed Dec 21 04:21:28 2011 From: pierre-yves.david at logilab.fr (Pierre-Yves David) Date: Wed, 21 Dec 2011 10:21:28 +0100 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: <20111221092128.GA27175@crater1.logilab.fr> On Wed, Dec 21, 2011 at 07:15:46AM +0000, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > > Do people still have to use this in commercial environments or is > everyone on 2.6+ nowadays? > > I'm finally getting some continuous integration set up for my > packages and it's highlighting some 2.5 compatibility issues. I'm > wondering whether to fix those (lots of ugly "from __future__ import > with_statement" everywhere) or just to drop Python 2.5 support. > > What do people feel? Most linux distribution went directly from 2.4 to 2.5 Debian: old stable (lenny) 2.4 stable (squeeze) 2.5 Red Hat REHL5 2.4 REHL6 2.6 The most notable exception is Ubuntu Hardy and LTS release from april 2008 with 2.5. But this LTS is out of support for almost 1 year now and current LTS (Lucid) ship 2.6. If you don't plan to support 2.4, supporting 2.5 does not seems a priority. -- Pierre-Yves David http://www.logilab.fr/ -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 198 bytes Desc: Digital signature URL: From saqib.ali.75 at gmail.com Wed Dec 21 04:23:43 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Wed, 21 Dec 2011 01:23:43 -0800 (PST) Subject: Can I get use pexpect to control this process? Message-ID: <1b3ff657-53d3-40d9-b7a8-6246b4d27f31@n6g2000vbz.googlegroups.com> I have a program X that constantly spews output, hundreds of lines per minute. X is not an interactive program. IE: it doesn't take any user input. It just produces a lot of textual output to STDOUT. I would like to save the output produced by X into a different file every 5 seconds regardless of the actual content. I want one file to contain the output from seconds 0-5, another file should contain 6-10, etc. etc. Can I do this with pexpect? I'm not sure I can because the "before" argument gives me EVERYTHING upto the most recent match. What I really need is something that will give me what was produced between the last 2 calls to pexpect.expect(). From djc at news.invalid Wed Dec 21 05:06:59 2011 From: djc at news.invalid (DJC) Date: Wed, 21 Dec 2011 10:06:59 +0000 Subject: Learning Python 2.4 In-Reply-To: References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On 21/12/11 02:13, Ashton Fagg wrote: > I got the impression the OP was learning programming in general (i.e. > from scratch) and not merely "learning Python". If this is the case it > shouldn't matter if they're merely learning the concepts as you can > always get up to speed on the differences later on as they get more > experienced. In which case the most important thing is the quality of the book as a text on Programming. If you find the the author's style to your taste, then use that book rather than struggle with a text based on a recent version that you personally find unreadable. From DStaple at nds.com Wed Dec 21 05:09:05 2011 From: DStaple at nds.com (Staple, Daniel (BSKYB)) Date: Wed, 21 Dec 2011 10:09:05 +0000 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: We've a bunch of windows servers stuck on python 2.5 since an API we use on them is shipped to us pyc's only - forcing us to stay with that version. Most of our other machines are on 2.6 or 2.7. -----Original Message----- From: testing-in-python-bounces at lists.idyll.org [mailto:testing-in-python-bounces at lists.idyll.org] On Behalf Of Chris Withers Sent: 21 December 2011 07:16 To: Python List; testing-in-python at lists.idyll.org; simplistix at googlegroups.com Subject: [TIP] Anyone still using Python 2.5? Hi All, What's the general consensus on supporting Python 2.5 nowadays? Do people still have to use this in commercial environments or is everyone on 2.6+ nowadays? I'm finally getting some continuous integration set up for my packages and it's highlighting some 2.5 compatibility issues. I'm wondering whether to fix those (lots of ugly "from __future__ import with_statement" everywhere) or just to drop Python 2.5 support. What do people feel? cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk _______________________________________________ testing-in-python mailing list testing-in-python at lists.idyll.org http://lists.idyll.org/listinfo/testing-in-python ************************************************************************************** This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postmaster at nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary. NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00 ************************************************************************************** From steve+comp.lang.python at pearwood.info Wed Dec 21 05:50:34 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Dec 2011 10:50:34 GMT Subject: Anyone still using Python 2.5? References: Message-ID: <4ef1b9fa$0$29973$c3e8da3$5496439d@news.astraweb.com> On Wed, 21 Dec 2011 07:15:46 +0000, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > > Do people still have to use this in commercial environments or is > everyone on 2.6+ nowadays? Centos and Red Hat production systems still use Python 2.4, so yes, absolutely, 2.5 and 2.4 still need to be supported. Not necessarily by package authors though -- that's a matter for them to decide. I'm presently writing a small library which will support 2.4 through 3.2, which isn't as hard as it sounds like, but still isn't exactly fun. If the project were much bigger, I'd drop support for 2.4 and only support 2.5. At least then I could use conditional expressions and __future__ imports. > I'm finally getting some continuous integration set up for my packages > and it's highlighting some 2.5 compatibility issues. I'm wondering > whether to fix those (lots of ugly "from __future__ import > with_statement" everywhere) or just to drop Python 2.5 support. > > What do people feel? It really depends on *your* users, not arbitrary developers. How many of your users are using 2.5? -- Steven From jml at mumak.net Wed Dec 21 05:55:29 2011 From: jml at mumak.net (Jonathan Lange) Date: Wed, 21 Dec 2011 10:55:29 +0000 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <20111221092128.GA27175@crater1.logilab.fr> References: <4EF187A2.6070909@simplistix.co.uk> <20111221092128.GA27175@crater1.logilab.fr> Message-ID: On Wed, Dec 21, 2011 at 9:21 AM, Pierre-Yves David wrote: ... > The most notable exception is Ubuntu Hardy and LTS release from april 2008 with > 2.5. But this LTS is out of support for almost 1 year now and current LTS > (Lucid) ship 2.6. Not quite. Ubuntu 8.04 LTS is supported on the server until April 2013. jml From jim at zope.com Wed Dec 21 05:57:26 2011 From: jim at zope.com (Jim Fulton) Date: Wed, 21 Dec 2011 05:57:26 -0500 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: On Wed, Dec 21, 2011 at 2:15 AM, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > > Do people still have to use this in commercial environments or is everyone > on 2.6+ nowadays? > > I'm finally getting some continuous integration set up for my packages and > it's highlighting some 2.5 compatibility issues. I'm wondering whether to > fix those (lots of ugly "from __future__ import with_statement" everywhere) > or just to drop Python 2.5 support. > > What do people feel? Google app engine is still Python 2.5, as is Jython. Jim -- Jim Fulton http://www.linkedin.com/in/jimfulton From ashton at fagg.id.au Wed Dec 21 07:16:42 2011 From: ashton at fagg.id.au (Ashton Fagg) Date: Wed, 21 Dec 2011 22:16:42 +1000 Subject: Learning Python 2.4 In-Reply-To: References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On 21 December 2011 20:06, DJC wrote: > > In which case the most important thing is the quality of the book as a > text on Programming. If you find the the author's style to your taste, > then use that book rather than struggle with a text based on a recent > version that you personally find unreadable. This is very good advice. -- Ashton Fagg E-mail: ashton at fagg.id.au Web: http://www.fagg.id.au/~ashton/ Keep calm and call Batman. From cosmo_general at yahoo.com Wed Dec 21 07:59:59 2011 From: cosmo_general at yahoo.com (Muddy Coder) Date: Wed, 21 Dec 2011 04:59:59 -0800 (PST) Subject: Why widgets become 'NoneType'? Message-ID: <2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com> Hi Folks, I was driven nuts by this thing: widgets lost their attributes, then I can't configure them. Please take a look at the codes below: from Tkinter import * canvas = Canvas(width=300, height=400, bg='white') canvas.pack(expand=NO, fill=BOTH) pic = PhotoImage(file=img) canvas.create_image(0, 0, image=pic) al = PhotoImage(file='a.gif') lbl = Label(canvas, text=x, fg='red').pack(side=LEFT) canvas.create_text(40,20, text='Howdy') the last line of code got error message, says canvas object has no attribute of config. The same problem happened again, the code is below: def make_labels(win, astr): labels = [] for i in range(len(astr)): lbl = Label(win, text=astr[i]).pack(side=LEFT ) labels.append(lbl) return tuple(labels) def config_labels(atuple, func): for lbl in atuple: lbl.config('%s') % func root = Tk() lbls = make_labels(root, 'foobar') config_labels(lbls, "fg='red'") The config for Label was picked up: No attributes of config. I tried to print type(lbls), and found Python interpreter reported as 'NoneType'. I also tried to print out dir(lbls), fount there was no attributes I familiar with such as config and such. What is wrong with them? Both of the codes above WORKED, since I saw my widgets displayed them as I wanted, but I just can't configure them as I usually did. Can somebody help me out? I never experience such a weird thing. Thanks! Cosmo From georger.silva at gmail.com Wed Dec 21 08:04:13 2011 From: georger.silva at gmail.com (George R. C. Silva) Date: Wed, 21 Dec 2011 11:04:13 -0200 Subject: Anyone still using Python 2.5? In-Reply-To: <4ef1b9fa$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef1b9fa$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EF1D94D.4000004@gmail.com> Em quarta-feira, 21 de dezembro de 2011 08:50:34, Steven D'Aprano escreveu: > On Wed, 21 Dec 2011 07:15:46 +0000, Chris Withers wrote: > >> Hi All, >> >> What's the general consensus on supporting Python 2.5 nowadays? >> >> Do people still have to use this in commercial environments or is >> everyone on 2.6+ nowadays? > > Centos and Red Hat production systems still use Python 2.4, so yes, > absolutely, 2.5 and 2.4 still need to be supported. > > Not necessarily by package authors though -- that's a matter for them to > decide. I'm presently writing a small library which will support 2.4 > through 3.2, which isn't as hard as it sounds like, but still isn't > exactly fun. If the project were much bigger, I'd drop support for 2.4 > and only support 2.5. At least then I could use conditional expressions > and __future__ imports. > > >> I'm finally getting some continuous integration set up for my packages >> and it's highlighting some 2.5 compatibility issues. I'm wondering >> whether to fix those (lots of ugly "from __future__ import >> with_statement" everywhere) or just to drop Python 2.5 support. >> >> What do people feel? > > It really depends on *your* users, not arbitrary developers. How many of > your users are using 2.5? > > > There are still people on 2.5. ESRIs customers (www.esri.com) that rely heavily on Python 2.5, because it ships with a popular ArcGIS release (9.31). The new ArcGIS release uses 2.6, but I can see 9.31 lurking around for another year, at least. Cheers. From roy at panix.com Wed Dec 21 08:07:24 2011 From: roy at panix.com (Roy Smith) Date: Wed, 21 Dec 2011 08:07:24 -0500 Subject: Anyone still using Python 2.5? References: <4ef1b9fa$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4ef1b9fa$0$29973$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Centos and Red Hat production systems still use Python 2.4, so yes, > absolutely, 2.5 and 2.4 still need to be supported. Is Python 2.4 destined to be the next IE-6? From d at davea.name Wed Dec 21 08:22:04 2011 From: d at davea.name (Dave Angel) Date: Wed, 21 Dec 2011 08:22:04 -0500 Subject: Why widgets become 'NoneType'? In-Reply-To: <2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com> References: <2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com> Message-ID: <4EF1DD7C.4020900@davea.name> On 12/21/2011 07:59 AM, Muddy Coder wrote: > Hi Folks, > > I was driven nuts by this thing: widgets lost their attributes, then I > can't configure them. Please take a look at the codes below: > > from Tkinter import * > canvas = Canvas(width=300, height=400, bg='white') > canvas.pack(expand=NO, fill=BOTH) > pic = PhotoImage(file=img) > canvas.create_image(0, 0, image=pic) > al = PhotoImage(file='a.gif') > > lbl = Label(canvas, text=x, fg='red').pack(side=LEFT) > > canvas.create_text(40,20, text='Howdy') > > the last line of code got error message, says canvas object has no > attribute of config. The same problem happened again, the code is > below: > > def make_labels(win, astr): > > labels = [] > for i in range(len(astr)): > lbl = Label(win, text=astr[i]).pack(side=LEFT ) > labels.append(lbl) > return tuple(labels) > def config_labels(atuple, func): > for lbl in atuple: > lbl.config('%s') % func > > root = Tk() > lbls = make_labels(root, 'foobar') > config_labels(lbls, "fg='red'") > > The config for Label was picked up: No attributes of config. I tried > to print type(lbls), and found Python interpreter reported as > 'NoneType'. I also tried to print out dir(lbls), fount there was no > attributes I familiar with such as config and such. What is wrong with > them? Both of the codes above WORKED, since I saw my widgets displayed > them as I wanted, but I just can't configure them as I usually did. > Can somebody help me out? I never experience such a weird thing. > Thanks! > > Cosmo When posting error messages, post it exactly and completely; do not paraphrase. -- DaveA From tom at recursivedream.com Wed Dec 21 08:38:21 2011 From: tom at recursivedream.com (Tom Davis) Date: Wed, 21 Dec 2011 08:38:21 -0500 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: On Dec 21, 2011, at 2:15 AM, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > > Do people still have to use this in commercial environments or is everyone on 2.6+ nowadays? For those of us living the nightmare of AppEngine *and* working on an app old enough to not be using the newer Datastore, we're stuck on 2.5 until we can justify a data migration. Frankly I don't know how many such apps exist these days that are still actively developed, though. > > I'm finally getting some continuous integration set up for my packages and it's highlighting some 2.5 compatibility issues. I'm wondering whether to fix those (lots of ugly "from __future__ import with_statement" everywhere) or just to drop Python 2.5 support. > > What do people feel? > > cheers, > > Chris > > -- > Simplistix - Content Management, Batch Processing & Python Consulting > - http://www.simplistix.co.uk > > _______________________________________________ > testing-in-python mailing list > testing-in-python at lists.idyll.org > http://lists.idyll.org/listinfo/testing-in-python From kw at codebykevin.com Wed Dec 21 08:46:07 2011 From: kw at codebykevin.com (Kevin Walzer) Date: Wed, 21 Dec 2011 08:46:07 -0500 Subject: Learning Python 2.4 In-Reply-To: <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On 12/20/11 7:31 PM, Rick Johnson wrote: > Anything before Python > 3.0 is now obsolete. We are currently at 3.2.2 for a stable release. 2.7 is still a supported production release of Python. It has not been end-of-lifed. -- Kevin Walzer Code by Kevin http://www.codebykevin.com From roy at panix.com Wed Dec 21 08:51:14 2011 From: roy at panix.com (Roy Smith) Date: Wed, 21 Dec 2011 08:51:14 -0500 Subject: [TIP] Anyone still using Python 2.5? References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: In article , Tom Davis wrote: > For those of us living the nightmare of AppEngine I've never used AppEngine, just read a little about it. Could you explain why it's a nightmare? From neilc at norwich.edu Wed Dec 21 09:04:54 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 21 Dec 2011 14:04:54 GMT Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> Message-ID: <9le7c5F1lsU1@mid.individual.net> On 2011-12-20, Paul Rubin wrote: > Grant Edwards writes: >> Oops. I should have mentioned this is for embedded systems >> programming so templates in general (and STL in particular) >> are probably off the table. > > Templates are how C++ does generics and I'd expect them to > appear in be used in embedded programming as well as elsewhere. > They can bloat up the code if you're not careful (if function f > has several parameters, you can end up with a separate, > specialized copy of f for every combination of types that f is > called with in the program), but in the typical monomorphic > situation they don't add any overhead. I'm not sure about the > situation with STL. Anyway, templates aren't terribly hard to > understand. Moreover, if you don't plan to take advantage of templates or inheritance, then you could as well write C++ compatible C and be pretty happy with the results. -- Neil Cerutti From roy at panix.com Wed Dec 21 09:26:04 2011 From: roy at panix.com (Roy Smith) Date: Wed, 21 Dec 2011 09:26:04 -0500 Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> Message-ID: In article <9le7c5F1lsU1 at mid.individual.net>, Neil Cerutti wrote: > On 2011-12-20, Paul Rubin wrote: > > Grant Edwards writes: > >> Oops. I should have mentioned this is for embedded systems > >> programming so templates in general (and STL in particular) > >> are probably off the table. > > > > Templates are how C++ does generics and I'd expect them to > > appear in be used in embedded programming as well as elsewhere. > > They can bloat up the code if you're not careful (if function f > > has several parameters, you can end up with a separate, > > specialized copy of f for every combination of types that f is > > called with in the program), but in the typical monomorphic > > situation they don't add any overhead. I'm not sure about the > > situation with STL. Anyway, templates aren't terribly hard to > > understand. > > Moreover, if you don't plan to take advantage of templates or > inheritance, then you could as well write C++ compatible C and be > pretty happy with the results. Well, C++ still gives you constructors, destructors, and the ability to write class-specific operators. But, you'd be missing one of C++'s biggest selling points; safe containers. Even if you never explore anything in STL beyond std:string and std:vector, you will have saved yourself a world of buffer overflow pain. From nathan.alexander.rice at gmail.com Wed Dec 21 10:07:31 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 10:07:31 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 8:37 PM, Joshua Landau wrote: > On 21 December 2011 00:24, Nathan Rice > wrote: >> efoo_res = ((efoo2.capitalize() + " little indian").split(" >> ").apply(reversed) * 2).apply("_".join) # note that you could do >> reversed(...) instead, I just like to read left to right >> efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " >> little indian").split(" ") in case you need to debug something and >> want to look at intermediate values > > How is any of this better than the elementwise operators ("~")? People > should be able to expect len(x) to always?return a number or raise an error. > I know it's not part of the spec, but a lot breaks without these guarantees. > When "str(x)" isn't a string, all the formatting code breaks*. And when the > other suggestion ("~str(x)" or "str~(x)" or something similar) has all the > benifits and none of the drawbacks, why should I use this? len() will always return a number or raise an error, just like the type functions (bool/int/etc) return that type or raise an error. The interpreter guarantees that for you. This has a couple of advantages over element-wise operators: 1. Because everything is handled in terms of generator chains, all operations on an ElementwiseProxy are evaluated lazily. With element-wise operator overloading you would need to perform each operation immediately. 2. As a result of #1, you can "undo" operations you perform on an ElementwiseProxy with the parent property. 3. This still works if the person who created the class you're working with doesn't add support for element-wise operators. Sure, you could monkey patch their code, but that can lead to other problems down the line. 4. There isn't an obvious/intuitive character for element-wise versions of operators, and fewer symbols is better than more IMHO (see: Perl). Also, if you use the ElementwiseProxyMixin, you can sprinkle element-wise stuff in neatly just by using "variable.each" where you would use "~" in your examples. > Upon this implementation I take back my comment on the whole typing thing. > Your title just really confused me. > > * You can't just make functions non-elementwise unless called through > ".apply" either: > def str(x): return x.__str__() str is one of the special cases (along with repr, unicode, int, float, long, bool, etc). These can't ever be elementwise in CPython (I don't know that this holds in other interpreters). The idea is that you use ElementwiseProxy(x) or preferably x.each, work with your data in an element-wise capacity as needed, then list() or iter() back out. I think it should be a deliberate change of context. Interleaving the proxy (say via x.each) in mostly scalar expressions works, but wasn't the use case I designed it for, and in those circumstances it doesn't really offer anything above the map function. Nathan From nathan.alexander.rice at gmail.com Wed Dec 21 10:15:38 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 10:15:38 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: > Have you seen PyLINQ? It has a similar approach to operating on > collections, returning a PyLINQ object after each call to facilitate > chaining. https://github.com/kalessin/PyLINQ/blob/master/pylinq/linq.py I hadn't seen it previously. I am a VERY heavy user of SQL Alchemy though, and I am sure chaining generative ClauseElements/Queries/etc, has burned some patterns into my subconscious at this point. > This is a personal opinion on the code, but I'd move instantiating the > new ElementwiseProxy out of each method and into its own decorator: > > ? ?# declare this outside of the class > ? ?def chainable(fn): > ? ? ? ?def _(self, *args, **kwargs): > ? ? ? ? ? ?return ElementwiseProxy(fn(self, *args, **kwargs), self) > ? ? ? ?return _ > > This way, each method that is chainable is a little more obvious > without inspecting the code, and the method body itself is only doing > what the method says it does: > > ? ?@chainable > ? ?def __add__(self, other): > ? ? ? ?return (e + other for e in object.__getattribute__(self, > "iterable")) This is a reasonable suggestion and I will play with something along those lines soon. > Incidentally, displaying an ElementwiseProxy instance doesn't go down > well with iPython: > > In [1]: from elementwise import * > > In [2]: e = ElementwiseProxy(['one','two','three']) > > In [3]: e > Out[3]: ERROR: An unexpected error occurred while tokenizing input > The following traceback may be corrupted or invalid > The error message is: ('EOF in multi-line statement', (6, 0)) I love IPython, but it has had known problems with iterators for years. A long time ago, I agonized over what I thought was a bug in my code where itertools.count would skip numbers in IPython, but my unit tests all passed. Everything should work fine if you tuple() it first. Nathan From invalid at invalid.invalid Wed Dec 21 10:37:02 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 21 Dec 2011 15:37:02 +0000 (UTC) Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> Message-ID: On 2011-12-21, Neil Cerutti wrote: > On 2011-12-20, Paul Rubin wrote: >> Grant Edwards writes: >>> Oops. I should have mentioned this is for embedded systems >>> programming so templates in general (and STL in particular) >>> are probably off the table. >> >> Templates are how C++ does generics and I'd expect them to >> appear in be used in embedded programming as well as elsewhere. I've only worked on the perphery of a couple embedded projects that used C++, but it seems to be farily common practice to prohibit the use of templates in embedded code. The problem seems to be that people writing C++ code don't really understand the language, how templates work, or (in general) the implications of the code they write. So, you have to prohibit anything that can be easily misused or cause non-obvious problems. For exaple (quoting from http://en.wikipedia.org/wiki/Embedded_C%2B%2B): Embedded C++ is a proper subset of C++. The following language features have been removed: Multiple inheritance Virtual base classes Run-time type information (typeid) New style casts (static_cast, dynamic_cast, reinterpret_cast and const_cast) The mutable type qualifier Namespaces Exceptions Templates Some people go further and prohibit any run-time object creation. >> They can bloat up the code if you're not careful Bingo. In my experience, many (if not most) C++ programmers don't understand enough of the language to realize what "being careful" means. >> (if function f has several parameters, you can end up with a >> separate, specialized copy of f for every combination of types that f >> is called with in the program), but in the typical monomorphic >> situation they don't add any overhead. I'm not sure about the >> situation with STL. Anyway, templates aren't terribly hard to >> understand. > > Moreover, if you don't plan to take advantage of templates or > inheritance, then you could as well write C++ compatible C and be > pretty happy with the results. Well, there are other C++ features that C doesn't have (e.g. exceptions -- but they're often prohibitted in embedded programming as well). Personally, I think C++ is an awful language in general and a _really_ awful language for embedded systems work. -- Grant Edwards grant.b.edwards Yow! PEGGY FLEMMING is at stealing BASKET BALLS to gmail.com feed the babies in VERMONT. From ndbecker2 at gmail.com Wed Dec 21 10:48:36 2011 From: ndbecker2 at gmail.com (Neal Becker) Date: Wed, 21 Dec 2011 10:48:36 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> Message-ID: Clarification: where can packing/unpacking syntax be used? It would be great if it were valid essentially anywhere (not limited to parameter passing). What about constructs like: a, @tuple tail, b = sequence? From neilc at norwich.edu Wed Dec 21 11:16:26 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 21 Dec 2011 16:16:26 GMT Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> Message-ID: <9lef2pFgggU1@mid.individual.net> On 2011-12-21, Grant Edwards wrote: > On 2011-12-21, Neil Cerutti wrote: >> On 2011-12-20, Paul Rubin wrote: >>> Grant Edwards writes: >>>> Oops. I should have mentioned this is for embedded systems >>>> programming so templates in general (and STL in particular) >>>> are probably off the table. >>> >>> Templates are how C++ does generics and I'd expect them to >>> appear in be used in embedded programming as well as elsewhere. > > I've only worked on the perphery of a couple embedded projects that > used C++, but it seems to be farily common practice to prohibit the > use of templates in embedded code. The problem seems to be that > people writing C++ code don't really understand the language, how > templates work, or (in general) the implications of the code they > write. So, you have to prohibit anything that can be easily misused > or cause non-obvious problems. I cheerfully agree that programmers ignorant of C++ should not write programs in it. But furthermore, they should also not define a subset of C++ for use in embedded programming. ;) -- Neil Cerutti From robert.kern at gmail.com Wed Dec 21 11:29:33 2011 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 21 Dec 2011 16:29:33 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 12/21/11 3:15 PM, Nathan Rice wrote: >> Incidentally, displaying an ElementwiseProxy instance doesn't go down >> well with iPython: >> >> In [1]: from elementwise import * >> >> In [2]: e = ElementwiseProxy(['one','two','three']) >> >> In [3]: e >> Out[3]: ERROR: An unexpected error occurred while tokenizing input >> The following traceback may be corrupted or invalid >> The error message is: ('EOF in multi-line statement', (6, 0)) > > I love IPython, but it has had known problems with iterators for > years. A long time ago, I agonized over what I thought was a bug in > my code where itertools.count would skip numbers in IPython, but my > unit tests all passed. Everything should work fine if you tuple() it > first. This is a different problem, actually. The problem is that the recently added (by me, actually) pretty-printing system tries to dispatch based on the type. In order to handle old-style classes, it checks for the type using .__class__ first. ElementProxy's __getattribute__() gets in the way here by returning a generator instead of the ElementProxy class. -- 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 From ethan at stoneleaf.us Wed Dec 21 11:33:41 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 21 Dec 2011 08:33:41 -0800 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> Message-ID: <4EF20A65.3050507@stoneleaf.us> Neal Becker wrote: > Clarification: where can packing/unpacking syntax be used? > > It would be great if it were valid essentially anywhere (not limited to > parameter passing). > > What about constructs like: > > a, @tuple tail, b = sequence? You mean like Python 3's: a, *middle, b = sequence ? From nathan.alexander.rice at gmail.com Wed Dec 21 11:43:49 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 11:43:49 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 11:29 AM, Robert Kern wrote: > On 12/21/11 3:15 PM, Nathan Rice wrote: > >>> Incidentally, displaying an ElementwiseProxy instance doesn't go down >>> well with iPython: >>> >>> In [1]: from elementwise import * >>> >>> In [2]: e = ElementwiseProxy(['one','two','three']) >>> >>> In [3]: e >>> Out[3]: ERROR: An unexpected error occurred while tokenizing input >>> The following traceback may be corrupted or invalid >>> The error message is: ('EOF in multi-line statement', (6, 0)) >> >> >> I love IPython, but it has had known problems with iterators for >> years. ?A long time ago, I agonized over what I thought was a bug in >> my code where itertools.count would skip numbers in IPython, but my >> unit tests all passed. ?Everything should work fine if you tuple() it >> first. > > > This is a different problem, actually. The problem is that the recently > added (by me, actually) pretty-printing system tries to dispatch based on > the type. In order to handle old-style classes, it checks for the type using > .__class__ first. ElementProxy's __getattribute__() gets in the way here by > returning a generator instead of the ElementProxy class. Thanks for the heads up Robert. Given that IPython is awesome and people should probably be doing an '.apply(type)' in that instance anyhow, I will corner case that on __getattribute__. Nathan From joshua.landau.ws at gmail.com Wed Dec 21 11:48:44 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 16:48:44 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 15:07, Nathan Rice wrote: > On Tue, Dec 20, 2011 at 8:37 PM, Joshua Landau > wrote: > > On 21 December 2011 00:24, Nathan Rice > > wrote: > >> efoo_res = ((efoo2.capitalize() + " little indian").split(" > >> ").apply(reversed) * 2).apply("_".join) # note that you could do > >> reversed(...) instead, I just like to read left to right > >> efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " > >> little indian").split(" ") in case you need to debug something and > >> want to look at intermediate values > > > > How is any of this better than the elementwise operators ("~")? People > > should be able to expect len(x) to always return a number or raise an > error. > > I know it's not part of the spec, but a lot breaks without these > guarantees. > > When "str(x)" isn't a string, all the formatting code breaks*. And when > the > > other suggestion ("~str(x)" or "str~(x)" or something similar) has all > the > > benifits and none of the drawbacks, why should I use this? > > len() will always return a number or raise an error, just like the > type functions (bool/int/etc) return that type or raise an error. The > interpreter guarantees that for you. The point wasn't that either way was better, but that with this implementation you get neither choice ("len(x)" vs "len~(x)") or reliability. The reliability point works like this: You want to elementwise a few functions, that before you were doing on a single item. BEFORE: item = foreignfunc1(item) item = foreignfunc2(item) item = foreignfunc3(item) NOW (your method): item = ElementwiseProxy(item) item = foreignfunc1(item) item = foreignfunc2(item) item = foreignfunc3(item) item = list(item) NOW (the PEP): item = foreignfunc1~(item) item = foreignfunc2~(item) item = foreignfunc3~(item) NOW (map): # or imap to do it lazily item = map(foreignfunc1, (item)) item = map(foreignfunc2, (item)) item = map(foreignfunc3, (item)) The PEP is short and quaint, and makes lots of things shorter than map ("[1,2,3]~+[5,6,7]"), so you can see the advantages. That's why I like the PEP as "syntatic sugar". The mapping method works fine. *You might think your method works. *But what if foreignfunc is "str"? *And you can't blacklist functions.* You can't say everything works bar A, B and C. What if you get: "lambda x:str(x)"? You can't blacklist that. That makes the ElementwiseProxy version buggy and prone to unstable operation. If it's consistent, fine. But it's not. This has a couple of advantages over element-wise operators: > Couple == 2 :P > 1. Because everything is handled in terms of generator chains, all > operations on an ElementwiseProxy are evaluated lazily. With > element-wise operator overloading you would need to perform each > operation immediately. > I agree this can be a preferred advantage.But you still have to choose one. "~" could be lazy, or it could be eager. But in both implementations you have to choose. That said, you have map and imap, and so you could have ElemetwiseProxy and eElementwiseProxy (eager), and you can have "~" and "i~". Remember that the syntax I'm using is just for PEP consistency. Some more creative people can find a syntax that works. > 2. As a result of #1, you can "undo" operations you perform on an > ElementwiseProxy with the parent property. > Use case? If this is actually a wanted feature, "parent" could be made a general property of iterators. (x for x in foo).parent == foo I think that's a separate proposal that isn't intrinsic to this idea. 3. This still works if the person who created the class you're working > with doesn't add support for element-wise operators. Sure, you could > monkey patch their code, but that can lead to other problems down the > line. > Example? I'm struggling to think of a case where you could do this only with one. > 4. There isn't an obvious/intuitive character for element-wise > versions of operators, and fewer symbols is better than more IMHO > (see: Perl). Also, if you use the ElementwiseProxyMixin, you can > sprinkle element-wise stuff in neatly just by using "variable.each" > where you would use "~" in your examples. Obvious/intuitive is meaningless for something so potentially common. "**" is far more obscure ("*" is obscure enough). Why is a double star the exponent character? Because it works. "~" would be easy to learn and consistent, once a standard was agreed. And again, I'm only using that syntax because it's the one in the PEP. We're not going to get to Perl, I hope, with this addition. Explicit > Implicit. If someone passes you a "variable.each", things go haywire (see: first section of reply). "~" is explicit. It doesn't have these problems. I also think explicit looks nicer. 'Cause it is, y'know :P > > Upon this implementation I take back my comment on the whole typing > thing. > > Your title just really confused me. > > > > * You can't just make functions non-elementwise unless called through > > ".apply" either: > > def str(x): return x.__str__() > > str is one of the special cases (along with repr, unicode, int, float, > long, bool, etc). These can't ever be elementwise in CPython (I don't > know that this holds in other interpreters). > I'll hammer my point in again. "foo~(x)" is elementwise. "typeofx(foo(ElementwiseProxy(x)))" *may* be elementwise, depending on whether foo is a special case, or uses a special case, or is an obscure bug if it uses both a special case and a non special case. "x~.foo" is an elementwise "getattr" on a list of things. "ElementwiseProxy(x).foo" is elementwise or a *ramble ramble* *exceptions* *special cases*. --------------------------------------------------- To wrap this up, one of your points was style and I disagree, two were implementation agnostic and thus not advantages to yours, and one was a plausible but rare use-case where I feel I need an example. Additionally, your implementation technique has several seemingly unavoidable yet inexplicable behaviours. The idea is that you use ElementwiseProxy(x) or preferably x.each, > work with your data in an element-wise capacity as needed, then list() > or iter() back out. I think it should be a deliberate change of > context. Interleaving the proxy (say via x.each) in mostly scalar > expressions works, but wasn't the use case I designed it for, and in > those circumstances it doesn't really offer anything above the map > function. > > Nathan > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Wed Dec 21 11:55:58 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 16:55:58 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> Message-ID: On 21 December 2011 15:48, Neal Becker wrote: > Clarification: where can packing/unpacking syntax be used? > > It would be great if it were valid essentially anywhere (not limited to > parameter passing). > > What about constructs like: > > a, @tuple tail, b = sequence? >>> a, *b, c = range(10) >>> a, b, c (0, [1, 2, 3, 4, 5, 6, 7, 8], 9) It's just a proposed syntax change, effectively, so yes. However, http://bugs.python.org/issue2292 covers the ones we don't have (that we should :P). This discussion doesn't cover that. > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Wed Dec 21 11:57:57 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 11:57:57 -0500 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: +1 for IPython/%edit using the simplest editor that supports syntax highlighting and line numbers. I have found that Exploring/Prototyping in the interpreter has the highest ROI of anything I teach people. Nathan From pfdubois at gmail.com Wed Dec 21 12:07:28 2011 From: pfdubois at gmail.com (Paul Dubois) Date: Wed, 21 Dec 2011 09:07:28 -0800 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: You're reinventing Numeric Python. On Tue, Dec 20, 2011 at 11:45 AM, Nathan Rice < nathan.alexander.rice at gmail.com> wrote: > Elementwise provides a proxy object for iterables which supports > chained method calls, as well as elementwise expressions and some > built-in functions. > > Example: > > class ExampleList(ElementwiseProxyMixin, list): > def __new__(cls, iterable): > return list.__new__(cls, iterable) > foo = ExampleList([1, 2, 3, 4]) > > # You could also do: efoo = ElementwiseProxy(foo) > efoo = foo.each > > assert list(efoo.bit_length()) == [1, 2, 2, 3] > print "bit length: ", list(efoo.bit_length()) > assert list(efoo + 1) == [2, 3, 4, 5] > print "with addition of 1: ", list(efoo + 1) > assert list(efoo * 2) == [2, 4, 6, 8] > print "with multiplication by 2: ", list(efoo * 2) > assert list(efoo == 2) == [False, True, False, False] > print "testing equality: ", efoo == 2 > assert list((efoo + 1) * 2 + 3) == [7, 9, 11, 13] > print "chaining addition and multiplication: ", (efoo + 1) * 2 + 3 > > Each ElementwiseProxy also has a "parent" attribute so you can > backtrack in the chain as needed rather than store each intermediate > value, if you think you might need them. > > There are still some issues with proper support of things like bool() > and int(), which refuse to return things that are not of the correct > type. > > Get it: > > PyPi: http://pypi.python.org/pypi/elementwise/0.111220 > GitHub: https://github.com/nathan-rice/Elementwise > > This was developed as a proof of concept for expanding the role of > element-wise syntax in python, and to that end I welcome comments. > > Nathan Rice > -- > http://mail.python.org/mailman/listinfo/python-announce-list > > Support the Python Software Foundation: > http://www.python.org/psf/donations/ > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Wed Dec 21 12:22:20 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 12:22:20 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 12:07 PM, Paul Dubois wrote: > You're reinventing Numeric Python. I prefer to think of it in terms of paying homage to NumPy (and functional programming). A big part of the motivation for this was bringing the elegance of NumPy to other areas of python besides numerical/scientific programming. Nathan From ethan at stoneleaf.us Wed Dec 21 12:22:28 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 21 Dec 2011 09:22:28 -0800 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4ef12831$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef12831$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EF215D4.70309@stoneleaf.us> Steven D'Aprano wrote: > I'm just glad that you've put your money > where your mouth is, and released the package, instead of demanding > others do the work. Thank you. +1000! ~Ethan~ From python.list at tim.thechases.com Wed Dec 21 12:28:46 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 21 Dec 2011 11:28:46 -0600 Subject: Anyone still using Python 2.5? In-Reply-To: References: <4ef1b9fa$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EF2174E.8040900@tim.thechases.com> On 12/21/11 07:07, Roy Smith wrote: > In article<4ef1b9fa$0$29973$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> Centos and Red Hat production systems still use Python 2.4, so yes, >> absolutely, 2.5 and 2.4 still need to be supported. > > Is Python 2.4 destined to be the next IE-6? No...unlike IE6, 2.4 backwards compatibility has a foreseeable death when RHEL+2.6 eventually fall out of support ;-) -tkc From andreas.perstinger at gmx.net Wed Dec 21 12:42:22 2011 From: andreas.perstinger at gmx.net (Andreas Perstinger) Date: Wed, 21 Dec 2011 18:42:22 +0100 Subject: Learning Python 2.4 In-Reply-To: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Message-ID: <4EF21A7E.4010602@gmx.net> On 2011-12-20 19:31, kimma wrote: > I am about to learn python with "how to think like a computer > scientist". This book is just available for python 2.4. Does it matter > for programming? There is also a online-version for Python 3 but there are some differences: http://openbookproject.net/thinkcs/python/english3e/preface3-rle.html Bye, Andreas From woooee at gmail.com Wed Dec 21 12:45:46 2011 From: woooee at gmail.com (woooee) Date: Wed, 21 Dec 2011 09:45:46 -0800 (PST) Subject: Why widgets become 'NoneType'? References: <2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com> Message-ID: The error is with the labels not the canvas. All labels will have an id of "None" as that is what pack() returns. ? ? ? ? lbl = Label(win, text=astr[i]).pack(side=LEFT ) ? ? ? ? labels.append(lbl) The error will come up in the config_labels function when the program tries to config a tuple of "None", if not before. Other than that, the previous suggestion, post the entire error message so we know where an what is happening, is required to debug further. From ian.g.kelly at gmail.com Wed Dec 21 12:51:37 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Dec 2011 10:51:37 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 9:48 AM, Joshua Landau wrote: > functions, that before you were doing on a single item. > BEFORE: > item = foreignfunc1(item) > item = foreignfunc2(item) > item = foreignfunc3(item) > > NOW (your method): > item = ElementwiseProxy(item) > item = foreignfunc1(item) > item = foreignfunc2(item) > item = foreignfunc3(item) > item = list(item) Shouldn't that be: item = ElementwiseProxy(item) item = item.apply(foreignfunc1) item = item.apply(foreignfunc2) item = item.apply(foreignfunc3) item = list(item) > You might think your method works. But what if foreignfunc is "str"? And you > can't blacklist functions.?You can't say everything works bar A, B and C. > What if you get: "lambda x:str(x)"? You can't blacklist that. That makes the > ElementwiseProxy version buggy and prone to unstable operation. If it's > consistent, fine. But it's not. I'm not sure I understand this criticism. There is no "blacklisting" going on that I am aware of. The behavior seems consistent to me: Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from elementwise import * >>> ewp = ElementwiseProxy(range(5)) >>> str(ewp) '0, 1, 2, 3, 4' >>> (lambda x: str(x))(ewp) '0, 1, 2, 3, 4' >>> ewp.apply(str) '0', '1', '2', '3', '4' >>> ewp.apply(lambda x: str(x)) '0', '1', '2', '3', '4' All of which is exactly what I expected to get. Cheers, Ian From arnodel at gmail.com Wed Dec 21 12:53:06 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Wed, 21 Dec 2011 17:53:06 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 00:24, Nathan Rice wrote: > efoo2 = ElementwiseProxy(["one", "two", "three", "four"]) > > efoo_res = ((efoo2.capitalize() + " little indian").split(" > ").apply(reversed) * 2).apply("_".join) # note that you could do > reversed(...) instead, I just like to read left to right > efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " > little indian").split(" ") in case you need to debug something and > want to look at intermediate values You can already do: efoo2 = ["one", "two", "three", "four"] ["_".join(reversed((x.capitalize() + " little indian").split(" ")) * 2) for x in efoo2] Note 1: I've ignored the fact that reversed(...)*2 is erroneous Note 2: I wouldn't such code myself, in either form What's the advantage of your elementwise stuff? -- Arnaud From ian.g.kelly at gmail.com Wed Dec 21 12:54:40 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Dec 2011 10:54:40 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 12:45 PM, Nathan Rice wrote: > Elementwise provides a proxy object for iterables which supports > chained method calls, as well as elementwise expressions and some > built-in functions. It doesn't seem to work correctly when both operands are Elementwise: >>> numerator = ElementwiseProxy(range(5)) >>> denominator = ElementwiseProxy([2, 2, 3, 3, 3]) >>> remainder = numerator % denominator >>> print remainder Traceback (most recent call last): File "", line 1, in File "elementwise.py", line 72, in __repr__ return ", ".join(e.__repr__() for e in object.__getattribute__(self, "iterable")) TypeError: sequence item 0: expected string, ElementwiseProxy found >>> list(remainder) [] Cheers, Ian From nathan.alexander.rice at gmail.com Wed Dec 21 13:09:01 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 13:09:01 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: >> On Tue, Dec 20, 2011 at 8:37 PM, Joshua Landau >> wrote: >> > On 21 December 2011 00:24, Nathan Rice >> > wrote: >> >> efoo_res = ((efoo2.capitalize() + " little indian").split(" >> >> ").apply(reversed) * 2).apply("_".join) # note that you could do >> >> reversed(...) instead, I just like to read left to right >> >> efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " >> >> little indian").split(" ") in case you need to debug something and >> >> want to look at intermediate values >> > >> > How is any of this better than the elementwise operators ("~")? People >> > should be able to expect len(x) to always?return a number or raise an >> > error. >> > I know it's not part of the spec, but a lot breaks without these >> > guarantees. >> > When "str(x)" isn't a string, all the formatting code breaks*. And when >> > the >> > other suggestion ("~str(x)" or "str~(x)" or something similar) has all >> > the >> > benifits and none of the drawbacks, why should I use this? >> >> len() will always return a number or raise an error, just like the >> type functions (bool/int/etc) return that type or raise an error. ?The >> interpreter guarantees that for you. > > > The point wasn't that either way was better, but that with this > implementation you get neither choice ("len(x)" vs "len~(x)") or > reliability. If len didn't have the hard coded behavior, you would have the choice of len(x) or len(x.each). Since a lot of code relies on len() returning an it, I think it is fine to accept that you have to use x.each.apply(len). I agree that this is a case where some kind of elementwise designation in the syntax would be better; if there was a ? character on the keyboard and more people knew what it meant I would have fewer reservations. > > The?reliability?point works like this:?You want to elementwise a few > functions, that before you were doing on a single item. > BEFORE: > item = foreignfunc1(item) > item = foreignfunc2(item) > item = foreignfunc3(item) > > NOW (your method): > item = ElementwiseProxy(item) > item = foreignfunc1(item) > item = foreignfunc2(item) > item = foreignfunc3(item) > item = list(item) well, I would say it more like: item.each.apply(foreignfunc1).apply(foreignfunc2).apply(foreignfunc3) # I like to read left to right, what can I say? and I wouldn't list() it right away, since it is nice and lazy. > You might think your method works. But what if foreignfunc is "str"? And you > can't blacklist functions.?You can't say everything works bar A, B and C. > What if you get: "lambda x:str(x)"? You can't blacklist that. That makes the > ElementwiseProxy version buggy and prone to unstable operation. If it's > consistent, fine. But it's not. I don't need to blacklist anything. Everything that has funny behavior like str goes through special methods on the class (that I know of), and isn't hooked through __getattribute__, so I just handle it somewhat normally. >> 1. Because everything is handled in terms of generator chains, all >> operations on an ElementwiseProxy are evaluated lazily. ?With >> element-wise operator overloading you would need to perform each >> operation immediately. > > > I agree this can be a preferred advantage.But you still have to choose one. > "~" could be lazy, or it could be eager. But in both implementations you > have to choose. That said, you have map and imap, and so you could have > ElemetwiseProxy and eElementwiseProxy (eager), and you can have "~" and > "i~". Remember that the syntax I'm using is just for PEP consistency. Some > more creative people can find a syntax that works. >From my perspective the strength of operators is that they are intuitive, since we use them constantly in other areas; I can make a good guess about what X * Y or X + Y means in various contexts. When you introduce operators that don't have any well ingrained, standard meaning, you just make the syntax cryptic. >> >> 2. As a result of #1, you can "undo" operations you perform on an >> ElementwiseProxy with the parent property. > > > Use case? If this is actually a wanted feature, "parent" could be made a > general property of iterators. > (x for x in foo).parent == foo > I think that's a?separate?proposal that isn't intrinsic to this idea. One quick use case: I want to debug something big, slow and nasty; . I set breakpoints with conditions where it seems like the issue lies from the stack trace. Unfortunately I missed the root cause, and the variables that would help me debug it have been garbage collected. I can go through the entire process again, try to set better breakpoints and cross my fingers, or iterate over some subset of parent operations right there. I think I'll take the latter. >> 3. This still works if the person who created the class you're working >> with doesn't add support for element-wise operators. ?Sure, you could >> monkey patch their code, but that can lead to other problems down the >> line. As I understood it, the elementwise operators in the PEP weren't language magic, but new hooks to special methods. If it is language magic, that will probably have very far reaching implications in the code. >> >> 4. There isn't an obvious/intuitive character for element-wise >> versions of operators, and fewer symbols is better than more IMHO >> (see: Perl). ?Also, if you use the ElementwiseProxyMixin, you can >> sprinkle element-wise stuff in neatly just by using "variable.each" >> where you would use "~" in your examples. > > > Obvious/intuitive is meaningless for something so potentially common. "**" > is far more obscure ("*" is obscure enough). Why is a double star the > exponent character? Because it works. "~" would be easy to learn and > consistent, once a standard was agreed. And again, I'm only using that > syntax because it's the one in the PEP. We're not going to get to Perl, I > hope, with this addition. I agree ** is pretty bad (xor should not have gotten first dibs on ^). The only saving grace for this scheme is that it is to some degree a convention in programming. > Explicit > Implicit. If someone passes you a "variable.each", things go > haywire (see: first section of reply). "~" is explicit. It doesn't have > these problems. I also think explicit looks nicer. 'Cause it is, y'know :P Sure, I can't prevent people from doing bad things though. People will always find ways to shoot themselves (and potentially others around them) in the foot. > I'll hammer my point in again. > > "foo~(x)" is elementwise. > "typeofx(foo(ElementwiseProxy(x)))" may?be elementwise, depending on whether > foo is a special case, or uses a special case, or is an obscure bug if it > uses both a special case and a non special case. > > "x~.foo" is an elementwise "getattr" on a list of things. > "ElementwiseProxy(x).foo" is elementwise or a *ramble ramble* *exceptions* > *special cases*. I agree that having elementwise functionality at a deeper level in python would avoid some of the inconsistencies (of which there are really not very many!) Nathan From nathan.alexander.rice at gmail.com Wed Dec 21 13:16:28 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 13:16:28 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 12:53 PM, Arnaud Delobelle wrote: > > You can already do: > > efoo2 = ["one", "two", "three", "four"] > ["_".join(reversed((x.capitalize() + " little indian").split(" ")) * 2) > ? ? for x in efoo2] > > Note 1: I've ignored the fact that reversed(...)*2 is erroneous > Note 2: I wouldn't such code myself, in either form > > What's the advantage of your elementwise stuff? Sit two theoretically identical programmers who have a decent grasp of python, understand the basics of ElementwiseProxy and the components of your version down side by side, and see which one can figure out what the output should be first. I will bet real money it is the one working with the ElementwiseProxy code. We have all had a lot of practice reading inside out and backwards in the python community with comprehensions and such, but it is in no way natural or easy. Additionally, everything done via an ElementwiseProxy is lazy, so you only pay for what you eat :) Nathan From joshua.landau.ws at gmail.com Wed Dec 21 13:24:19 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 18:24:19 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 17:51, Ian Kelly wrote: > On Wed, Dec 21, 2011 at 9:48 AM, Joshua Landau > wrote: > > functions, that before you were doing on a single item. > > BEFORE: > > item = foreignfunc1(item) > > item = foreignfunc2(item) > > item = foreignfunc3(item) > > > > NOW (your method): > > item = ElementwiseProxy(item) > > item = foreignfunc1(item) > > item = foreignfunc2(item) > > item = foreignfunc3(item) > > item = list(item) > > Shouldn't that be: > > item = ElementwiseProxy(item) > item = item.apply(foreignfunc1) > item = item.apply(foreignfunc2) > item = item.apply(foreignfunc3) > item = list(item) I was under the impression that these were meant to be interchangeable. This is because functions are just wrappers to non-functions, really. >>> from elementwise import ElementwiseProxy as P >>> (lambda x:x+[1])(P([[0],[0],[0]])) [0, 1], [0, 1], [0, 1] If we have to use .apply, we might as well use map :P. Note that "len" and "dir" crash. *Here is a perfect example:* >>> int(P(["1","2","3"])) Traceback (most recent call last): File "", line 1, in *TypeError: __int__ returned non-int (type ElementwiseProxy)* It's elementwise, *but that breaks it.* An elementwise operator wouldn't have these problems. > You might think your method works. But what if foreignfunc is "str"? And > you > > can't blacklist functions. You can't say everything works bar A, B and C. > > What if you get: "lambda x:str(x)"? You can't blacklist that. That makes > the > > ElementwiseProxy version buggy and prone to unstable operation. If it's > > consistent, fine. But it's not. > > I'm not sure I understand this criticism. There is no "blacklisting" > going on that I am aware of. The behavior seems consistent to me: > > Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> from elementwise import * > >>> ewp = ElementwiseProxy(range(5)) > >>> str(ewp) > '0, 1, 2, 3, 4' > >>> (lambda x: str(x))(ewp) > '0, 1, 2, 3, 4' > >>> ewp.apply(str) > '0', '1', '2', '3', '4' > >>> ewp.apply(lambda x: str(x)) > '0', '1', '2', '3', '4' > > All of which is exactly what I expected to get. > Again, str(x) in this context is an outlier. It's one of the exceptions to the rule of applying functions = elementwise. -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Wed Dec 21 13:27:28 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 13:27:28 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: > It doesn't seem to work correctly when both operands are Elementwise: > >>>> numerator = ElementwiseProxy(range(5)) >>>> denominator = ElementwiseProxy([2, 2, 3, 3, 3]) >>>> remainder = numerator % denominator >>>> print remainder > Traceback (most recent call last): > ?File "", line 1, in > ?File "elementwise.py", line 72, in __repr__ > ? ?return ", ".join(e.__repr__() for e in > object.__getattribute__(self, "iterable")) > TypeError: sequence item 0: expected string, ElementwiseProxy found >>>> list(remainder) > [] > > Cheers, > Ian Ian, can you clarify the expected output in that case? My initial guess would be that you want to do a modulo on the cartesian product of parameters from the proxies, sort of like: import itertools numerator = range(5) denominator = [2, 2, 3, 3, 3] remainder = [n % d for (n, d) in itertools.product(numerator, denominator)] Nathan From nathan.alexander.rice at gmail.com Wed Dec 21 13:32:03 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 13:32:03 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 1:24 PM, Joshua Landau wrote: > I was under the impression that these were meant to be interchangeable. This > is because functions are just wrappers to non-functions, really. > >>>> from elementwise import ElementwiseProxy as P >>>> (lambda x:x+[1])(P([[0],[0],[0]])) > [0, 1], [0, 1], [0, 1] > > If we have to use .apply, we might as well use map :P. Apply is not required, you can use functions. I just hate reading inside out and backwards. compare: func3(func2(func1(x))) # Fine if the chain is short, but quickly gets unreadable x.apply(func1).apply(func2).apply(func3) # More verbose when working with short chains, but infinitely clearer and more readable when things start getting complicated, or for less experienced coders > Note that "len" and "dir" crash. > > Here is a perfect example: >>>> int(P(["1","2","3"])) > Traceback (most recent call last): > ? File "", line 1, in > TypeError: __int__ returned non-int (type ElementwiseProxy) It is alpha software :P Easily fixed though. Nathan From ian.g.kelly at gmail.com Wed Dec 21 13:39:07 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Dec 2011 11:39:07 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 11:24 AM, Joshua Landau wrote: > I was under the impression that these were meant to be interchangeable. This > is because functions are just wrappers to non-functions, really. > >>>> from elementwise import ElementwiseProxy as P >>>> (lambda x:x+[1])(P([[0],[0],[0]])) > [0, 1], [0, 1], [0, 1] > > If we have to use .apply, we might as well use map :P. Agreed. The advantage of .apply here is that when the functions are nested they are written left-to-right. But I wouldn't be using multiple maps or applys on a single line anyway -- my goal is to write clear code, not great one-liners. :-D From ian.g.kelly at gmail.com Wed Dec 21 13:41:37 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Dec 2011 11:41:37 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 11:27 AM, Nathan Rice wrote: > Ian, can you clarify the expected output in that case? ?My initial > guess would be that you want to do a modulo on the cartesian product > of parameters from the proxies, sort of like: > > import itertools > numerator = range(5) > denominator = [2, 2, 3, 3, 3] > > remainder = [n % d for (n, d) in itertools.product(numerator, denominator)] I expected the equivalent of: remainder = [n % d for (n, d) in zip(numerator, denominator)] which is what numpy does. From ethan at stoneleaf.us Wed Dec 21 13:48:41 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 21 Dec 2011 10:48:41 -0800 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: <4EF22A09.2070908@stoneleaf.us> Ian Kelly wrote: > my goal is to write clear code, not great one-liners. :-D +1 QOTW From garrickp at gmail.com Wed Dec 21 13:48:49 2011 From: garrickp at gmail.com (Falcolas) Date: Wed, 21 Dec 2011 10:48:49 -0800 (PST) Subject: Multiprocessing.Pipe in a daemon Message-ID: So, I'm running into a somewhat crazy bug. I am running several workers using multiprocessing to handle gearman requests. I'm using pipes to funnel log messages from each of the workers back to the controlling process, which iterates over the other end of the pipe, looking for messages with poll(), and logging them using the parent's log handler. This works really well when I'm not daemonizing the entire thing. However, when I daemonize the process (which happens well prior to any setup of the pipes & multiprocess.Process'es), a pipe which has nothing in it return True for the poll(), and then blocks on the pipe.recv() command. The gearman workers are still operational and responsive, and only starting one worker resolves the problem. Has anybody seen anything like this? #Trim # Create all of the end points endpoints = [] log_pipes = [] for w_num in range(5): (recv, snd) = multiprocessing.Pipe(False) # Start the worker logger.debug("Creating the worker {0}".format(w_num)) worker = Worker(w_num, job_name, gm_servers, snd) # Add the worker to the list of endpoints so it can be started endpoints.append(worker) log_pipes.append(recv) # Trim starting endpoints try: while True: time.sleep(1) pipe_logger(logger, log_pipes) except (KeyboardInterrupt, SignalQuit): pass # Trim cleanup def pipe_logger(logger_obj, pipes): done = False while not done: done = True for p in pipes: if p.poll(): # <-- Returning true after a previous pipe actually had data try: log_level, log_msg = p.recv() # <-- Hanging here except EOFError: continue done = False From joshua.landau.ws at gmail.com Wed Dec 21 13:52:37 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 18:52:37 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 18:32, Nathan Rice wrote: > On Wed, Dec 21, 2011 at 1:24 PM, Joshua Landau > wrote: > > I was under the impression that these were meant to be interchangeable. > This > > is because functions are just wrappers to non-functions, really. > > > >>>> from elementwise import ElementwiseProxy as P > >>>> (lambda x:x+[1])(P([[0],[0],[0]])) > > [0, 1], [0, 1], [0, 1] > > > > If we have to use .apply, we might as well use map :P. > > Apply is not required, you can use functions. I just hate reading > inside out and backwards. compare: > > func3(func2(func1(x))) # Fine if the chain is short, but quickly gets > unreadable > > x.apply(func1).apply(func2).apply(func3) # More verbose when working > with short chains, but infinitely clearer and more readable when > things start getting complicated, or for less experienced coders I wan't disagreeing with that, hence the word "if". I was just making a counter-counter point to a misunderstanding of my post. > > Note that "len" and "dir" crash. > > > > Here is a perfect example: > >>>> int(P(["1","2","3"])) > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: __int__ returned non-int (type ElementwiseProxy) > > It is alpha software :P Easily fixed though. > If fix means "return number" then no. This inconsistency between elementwise operations and 'normal' ones is the problem. Again, explicit elementwise-ifying through "~" fixes that. You need to tell me why this is worth the confusion over that. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Wed Dec 21 13:57:05 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Dec 2011 11:57:05 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 9:48 AM, Joshua Landau wrote: > NOW (the PEP): > item = foreignfunc1~(item) > item = foreignfunc2~(item) > item = foreignfunc3~(item) Just a note: PEP 225 does not actually include this syntax for arbitrary function calls. It only proposes to augment the arithmetic and assignment operators. From bahamutzero8825 at gmail.com Wed Dec 21 13:59:45 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 21 Dec 2011 12:59:45 -0600 Subject: Why does this launch an infinite loop of new processes? Message-ID: <4EF22CA1.1070200@gmail.com> I am trying to understand the multiprocessing module, and I tried some simple code: import multiprocessing def f(): print('bla bla') p = multiprocessing.Process(target=f) p.start() p.join() And the result is a new process that spawns a new process that spawns a new process ad infinitum until I log out and the OS forcefully terminates all my user processes. I have no idea what is happening; I expected it to just print the string and terminate. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From joshua.landau.ws at gmail.com Wed Dec 21 14:00:38 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 19:00:38 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 18:57, Ian Kelly wrote: > On Wed, Dec 21, 2011 at 9:48 AM, Joshua Landau > wrote: > > NOW (the PEP): > > item = foreignfunc1~(item) > > item = foreignfunc2~(item) > > item = foreignfunc3~(item) > > Just a note: PEP 225 does not actually include this syntax for > arbitrary function calls. It only proposes to augment the arithmetic > and assignment operators. > Not quite. They never seem to have come to an agreement (the whole thing is deferred) but they do touch upon "~f(x)": 5. Using ~ as generic elementwise meta-character to replace map ~f(a, b) # map(f, a, b) ~~f(a, b) # map(lambda *x:map(f, *x), a, b) More generally, def ~f(*x): return map(f, *x) def ~~f(*x): return map(~f, *x) ... I used "f~(x)" because it's not ambiguous with the current binary not operator. -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Wed Dec 21 14:03:08 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 19:03:08 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 19:00, Joshua Landau wrote: > On 21 December 2011 18:57, Ian Kelly wrote: > >> On Wed, Dec 21, 2011 at 9:48 AM, Joshua Landau >> wrote: >> > NOW (the PEP): >> > item = foreignfunc1~(item) >> > item = foreignfunc2~(item) >> > item = foreignfunc3~(item) >> >> Just a note: PEP 225 does not actually include this syntax for >> arbitrary function calls. It only proposes to augment the arithmetic >> and assignment operators. >> > > Not quite. They never seem to have come to an agreement (the whole thing > is deferred) but they do touch upon "~f(x)": > > > 5. Using ~ as generic elementwise meta-character to replace map > > ~f(a, b) # map(f, a, b) > ~~f(a, b) # map(lambda *x:map(f, *x), a, b) > > More generally, > > def ~f(*x): return map(f, *x) > def ~~f(*x): return map(~f, *x) > ... > > I used "f~(x)" because it's not ambiguous with the current binary not operator. > > Apologies for the double post but I think it's worth mentioning that I did make up the "list~.method" syntax. -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Wed Dec 21 14:04:16 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 14:04:16 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 1:41 PM, Ian Kelly wrote: > > I expected the equivalent of: > > remainder = [n % d for (n, d) in zip(numerator, denominator)] > > which is what numpy does. I do want to come up with a nice way to do that... However: if numerator and denominator are plain lists that magically have an each method... each_numerator = numerator.each each_denominator = denominator.each each_numerator % each_denominator see where I'm going with the mismatch on the semantics? I know the example is a bit contrived, sorry. Expanding iterators by default might be a worth a shot though, since passing around iterators like that is pretty rare. Nathan From joshua.landau.ws at gmail.com Wed Dec 21 14:07:31 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 19:07:31 +0000 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: <4EF22CA1.1070200@gmail.com> References: <4EF22CA1.1070200@gmail.com> Message-ID: On 21 December 2011 18:59, Andrew Berg wrote: > I am trying to understand the multiprocessing module, and I tried some > simple code: > > import multiprocessing > def f(): > print('bla bla') > p = multiprocessing.Process(target=f) > p.start() > p.join() > > And the result is a new process that spawns a new process that spawns a > new process ad infinitum until I log out and the OS forcefully > terminates all my user processes. I have no idea what is happening; I > expected it to just print the string and terminate. Eh? It works for me. Python 3.2 + 2.7 Is this the full code? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dean at red-spanner.com Wed Dec 21 14:12:10 2011 From: dean at red-spanner.com (Dean Richardson P.Eng) Date: Wed, 21 Dec 2011 12:12:10 -0700 Subject: Newbie OOP Question & Code Snippet Message-ID: Hi All, I'm a newbie just learning Python, using a couple of books to learn the language. (Books: "Visual Quickstart Guide - Python, 2nd Ed", "Practical Programming - An Intro to Computer Science using Python"). I'm just now learning OOP material as presented in both books -- I'm new to this approach -- the last formal language I learned was Fortran77 -- :o) I am running Python 3.2.1 on Mac OS/X 10.6.8. My problem stems from a simple example in the Visual Quickstart book. The code is: ---------------------------- #person.py class Person: """Class to represent a person""" def __init__(self): self.name=' ' self.age=0 def display(self): print("Person('%s', age)" % (self.name, self.age)) ------------------------- When I run this, the shell presents thus: >>> ================================ RESTART ================================ >>> >>> p=Person() >>> p.name='Bob' >>> p.age=24 >>> p.display() Traceback (most recent call last): File "", line 1, in p.display() File "/Volumes/dean_richardson/GoFlex Home Personal/Dean's Code/Python3.x/Person.py", line 9, in display (self.name, self.age)) TypeError: not all arguments converted during string formatting >>> --------------- I'm sure this is something simple, but I can't see it. Any help appreciated! Regards, Dean -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Wed Dec 21 14:13:53 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 21 Dec 2011 13:13:53 -0600 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: References: <4EF22CA1.1070200@gmail.com> Message-ID: <4EF22FF1.5040709@gmail.com> On 12/21/2011 1:07 PM, Joshua Landau wrote: > Eh? It works for me. Python 3.2 + 2.7 > Is this the full code? That is the full code. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From happybrowndog at hotmail.com Wed Dec 21 14:19:59 2011 From: happybrowndog at hotmail.com (hbd666) Date: Wed, 21 Dec 2011 11:19:59 -0800 Subject: Need advice on the design of my application Message-ID: I am writing a hull-wave simulator for the design of boat hulls as they are affected by waves. This application is composed of 2 main parts, the part that renders the waves and its impact on the hull, and a GUI that controls the hull shape, waves, and other factors. The two parts of the application will need to exchange data, for example, the GUI will set the hull shape and this will be displayed by the renderer. The renderer will show the waves and it will tell the GUI statistics about the waves as they are being animated. This application will run on Windows. The Renderer part of the application must be written in C because it needs to do very fast numerical calculations and requires the use of OpenGL and must render very quickly. The GUI part of the application will be wxPython. The options for constructing the application that I have thought of are: 1) The wxPython part launches the Renderer on a thread, and the Renderer is a DLL (provided a DLL can display graphics). 2) The Renderer starts a thread where the Python interpreter is embedded, which launches wxPython. 3) The Renderer and the GUI are actually two separate applications that communicate with each other over a socket. My last resort is Option 3 because communications can be a hassle. Option 1 seems to be the easiest but will be very difficult to debug the Renderer using my IDE because it is a DLL, if a DLL can display graphics at all. Option 2 seems to be the most feasible both in terms of debugging and displaying graphics. I have concerns about the Python GIL and how that will affect the way this program will work. The Renderer is going to be running a tight loop. The GUI under wxPython also runs a main loop internally, but has the ability to put function calls on a sort of stack to be executed without interrupting its main loop. In my experience implementing Option 1 in another project, I know that Python suspends execution until the DLL function calls return, but I did not launch the DLL on a thread. I expect that if the DLL were launched on a thread, a function call into the DLL will still suspend Python. Maybe someone can tell me if this is true? Option 2 is of most interest to me, but how shall I handle the Python GIL when the Renderer runs its main loop? Will the main loop be unaffected because Python interpreter is embedded in a thread? From ethan at stoneleaf.us Wed Dec 21 14:29:43 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 21 Dec 2011 11:29:43 -0800 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: <4EF22CA1.1070200@gmail.com> References: <4EF22CA1.1070200@gmail.com> Message-ID: <4EF233A7.6020907@stoneleaf.us> Andrew Berg wrote: > I am trying to understand the multiprocessing module, and I tried some > simple code: > > import multiprocessing > def f(): > print('bla bla') > p = multiprocessing.Process(target=f) > p.start() > p.join() > > And the result is a new process that spawns a new process that spawns a > new process ad infinitum until I log out and the OS forcefully > terminates all my user processes. I have no idea what is happening; I > expected it to just print the string and terminate. > Anything that runs at import time should be protected by the `if __name__ == '__main__'` idiom as the children will import the __main__ module. 8<----------------------------------------------- import multiprocessing def f(): print('bla bla') if __name__ == '__main__': p = multiprocessing.Process(target=f) p.start() p.join() 8<----------------------------------------------- ~Ethan~ From python at mrabarnett.plus.com Wed Dec 21 14:31:05 2011 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 21 Dec 2011 19:31:05 +0000 Subject: Newbie OOP Question & Code Snippet In-Reply-To: References: Message-ID: <4EF233F9.8000902@mrabarnett.plus.com> On 21/12/2011 19:12, Dean Richardson P.Eng wrote: > Hi All, > I'm a newbie just learning Python, using a couple of books to learn the > language. (Books: "Visual Quickstart Guide - Python, 2nd Ed", "Practical > Programming - An Intro to Computer Science using Python"). I'm just now > learning OOP material as presented in both books -- I'm new to this > approach -- the last formal language I learned was Fortran77 -- :o) I > am running Python 3.2.1 on Mac OS/X 10.6.8. > > My problem stems from a simple example in the Visual Quickstart book. > The code is: > ---------------------------- > #person.py > class Person: > """Class to represent a person""" > def __init__(self): > self.name=' ' > self.age=0 > def display(self): > print("Person('%s', age)" % > (self.name, self.age)) > ------------------------- > When I run this, the shell presents thus: > >>> ================================ RESTART > ================================ > >>> > >>> p=Person() > >>> p.name ='Bob' > >>> p.age=24 > >>> p.display() > Traceback (most recent call last): > File "", line 1, in > p.display() > File "/Volumes/dean_richardson/GoFlex Home Personal/Dean's > Code/Python3.x/Person.py", line 9, in display > (self.name , self.age)) > TypeError: not all arguments converted during string formatting > >>> > --------------- > I'm sure this is something simple, but I can't see it. Any help appreciated! > It should be: print("Person('%s', %s)" % (self.name, self.age)) As it is, you're giving it 2 values but only %s placeholder. From ckaynor at zindagigames.com Wed Dec 21 14:37:34 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Wed, 21 Dec 2011 11:37:34 -0800 Subject: Need advice on the design of my application In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 11:19 AM, hbd666 wrote: > > > In my experience implementing Option 1 in another project, I know that > Python suspends > execution until the DLL function calls return, but I did not launch the > DLL on a thread. > I expect that if the DLL were launched on a thread, a function call into > the DLL will still > suspend Python. Maybe someone can tell me if this is true? > > Option 2 is of most interest to me, but how shall I handle the Python GIL > when the Renderer > runs its main loop? Will the main loop be unaffected because Python > interpreter is embedded > in a thread? When extending Python (as option 1 would do), the extension would block all Python threads if called from Python (though c-threads created by the module would not block Python). You can explicitly release the GIL during your C code's execution, which would cause only the thread actually making the call to be blocked (as it is actually running the C code), but other Python threads would still be able to run. When embedding Python (as option 2 would do), the embedding program will not be affected by the GIL unless it explicitly acquires the GIL (which is must do before accessing Python). Via the Python API functions you can acquire and release the Python GIL as needed. The only rule is that you must hold the Python GIL should you wish to access any part of the Python interpreter (function calls, variables, etc) [note: technically you don't HAVE to, but you run a high risk of data corruption and segfaults from the race conditions which would exist]. Either option should work fine. In case of option 1: 1. Import the C module. 2. The C module sets up some defaults for the render. 3. The C module creates its render thread (which is unaffected by the GIL, but probably needs other locks around global variables). 4. The C module returns to Python. 5. The Python GUI calls into the C module to change settings (which probably need to acquire short-term locks) An alternative would be: 1. Import the C module. 2. The C module setups up some defaults. 3. Create a Python thread to call the C module's render loop. 4. The render loop thread releases the GIL and continues as above. 5. The main Python thread continues as above. For option 2: 1. Initialize any needed variables for the system. 2. Start a new thread for loading and executing Python. 3. Have the main thread start the render loop. 4. Have the Python thread startup up Python. 5. Continue as in the first case above. > > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Wed Dec 21 14:38:11 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 19:38:11 +0000 Subject: Newbie OOP Question & Code Snippet In-Reply-To: References: Message-ID: On 21 December 2011 19:12, Dean Richardson P.Eng wrote: > Hi All, > I'm a newbie just learning Python, using a couple of books to learn the > language. (Books: "Visual Quickstart Guide - Python, 2nd Ed", "Practical > Programming - An Intro to Computer Science using Python"). I'm just now > learning OOP material as presented in both books -- I'm new to this > approach -- the last formal language I learned was Fortran77 -- :o) I am > running Python 3.2.1 on Mac OS/X 10.6.8. > > My problem stems from a simple example in the Visual Quickstart book. The > code is: > ---------------------------- > #person.py > class Person: > """Class to represent a person""" > def __init__(self): > self.name=' ' > self.age=0 > def display(self): > print("Person('%s', age)" % > (self.name, self.age)) > The "% (self.name, self.age)" means that two arguments are given in. You only have one "%s" in the string (untested, so I may be completely off). Additionally, use str.format now: "Person({}, {})".format(self.name, self.age) > ------------------------- > When I run this, the shell presents thus: > >>> ================================ RESTART > ================================ > >>> > >>> p=Person() > >>> p.name='Bob' > >>> p.age=24 > >>> p.display() > Traceback (most recent call last): > File "", line 1, in > p.display() > File "/Volumes/dean_richardson/GoFlex Home Personal/Dean's > Code/Python3.x/Person.py", line 9, in display > (self.name, self.age)) > TypeError: not all arguments converted during string formatting > >>> > --------------- > I'm sure this is something simple, but I can't see it. Any help > appreciated! > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Wed Dec 21 15:11:03 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 21 Dec 2011 14:11:03 -0600 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: <4EF233A7.6020907@stoneleaf.us> References: <4EF22CA1.1070200@gmail.com> <4EF233A7.6020907@stoneleaf.us> Message-ID: <4EF23D57.6010305@gmail.com> On 12/21/2011 1:29 PM, Ethan Furman wrote: > Anything that runs at import time should be protected by the `if > __name__ == '__main__'` idiom as the children will import the __main__ > module. So the child imports the parent and runs the spawn code again? That makes sense. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From Chris.Wesseling at cwi.nl Wed Dec 21 15:28:38 2011 From: Chris.Wesseling at cwi.nl (Chris.Wesseling at cwi.nl) Date: Wed, 21 Dec 2011 21:28:38 +0100 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: <20111221202838.GU532@cwi.nl> On 2011-12-21T07:15:46+0000, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > > Do people still have to use this in commercial environments or is > everyone on 2.6+ nowadays? 2.5, how modern. SUSE Linux Enterprise Server 10 comes with 2.4.2 Will be moving to a RHEL derivative running 2.6, though. The conservative enterprise will probably run py3k by y3k. :-( -- Chris -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 836 bytes Desc: not available URL: From invalid at invalid.invalid Wed Dec 21 16:02:27 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 21 Dec 2011 21:02:27 +0000 (UTC) Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> <9lef2pFgggU1@mid.individual.net> Message-ID: On 2011-12-21, Neil Cerutti wrote: > On 2011-12-21, Grant Edwards wrote: >> >>>> Templates are how C++ does generics and I'd expect them to >>>> appear in be used in embedded programming as well as elsewhere. >> >> I've only worked on the perphery of a couple embedded projects that >> used C++, but it seems to be farily common practice to prohibit the >> use of templates in embedded code. The problem seems to be that >> people writing C++ code don't really understand the language, how >> templates work, or (in general) the implications of the code they >> write. So, you have to prohibit anything that can be easily misused >> or cause non-obvious problems. > > I cheerfully agree that programmers ignorant of C++ should not > write programs in it. But furthermore, they should also not > define a subset of C++ for use in embedded programming. ;) I fully agree that programmers ignorant of C++ should not write programs in it. However C++ is a vast, complex, and dangerous language -- and industry doesn't seem to be willing to limit itself to using the seven people on the planet who understand it. I'm only half joking... :) It seems to me that you need to know far more about C++ to use it safely than you need to know about most other languages to use them safely. -- Grant Edwards grant.b.edwards Yow! Hey, waiter! I want at a NEW SHIRT and a PONY TAIL gmail.com with lemon sauce! From ethan at stoneleaf.us Wed Dec 21 16:15:35 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 21 Dec 2011 13:15:35 -0800 Subject: [OT] Quick intro to C++ for a Python and C user? In-Reply-To: References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> <9lef2pFgggU1@mid.individual.net> Message-ID: <4EF24C77.2060607@stoneleaf.us> Grant Edwards wrote: > On 2011-12-21, Neil Cerutti wrote: >> I cheerfully agree that programmers ignorant of C++ should not >> write programs in it. But furthermore, they should also not >> define a subset of C++ for use in embedded programming. ;) > > I fully agree that programmers ignorant of C++ should not write > programs in it. However C++ is a vast, complex, and dangerous > language -- and industry doesn't seem to be willing to limit itself to > using the seven people on the planet who understand it. > > I'm only half joking... :) Ah -- so there's actually 14? ;) ~Ethan~ From hansmu at xs4all.nl Wed Dec 21 17:08:19 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Wed, 21 Dec 2011 23:08:19 +0100 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: <4ef258d4$0$6981$e4fe514c@news2.news.xs4all.nl> On 21/12/11 01:03:26, Ian Kelly wrote: > As type conversion functions, bool(x) and > int(x) should *always* return bools and ints respectively > (or raise an exception), no matter what you pass in for x. That doesn't always happen in 2.x: >>> type(int(1e42)) This was fixed in 3.0. -- HansM From brett at yvrsfo.ca Wed Dec 21 17:08:31 2011 From: brett at yvrsfo.ca (Brett Cannon) Date: Wed, 21 Dec 2011 17:08:31 -0500 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: On Wed, Dec 21, 2011 at 05:57, Jim Fulton wrote: > On Wed, Dec 21, 2011 at 2:15 AM, Chris Withers > wrote: > > Hi All, > > > > What's the general consensus on supporting Python 2.5 nowadays? > > > > Do people still have to use this in commercial environments or is > everyone > > on 2.6+ nowadays? > > > > I'm finally getting some continuous integration set up for my packages > and > > it's highlighting some 2.5 compatibility issues. I'm wondering whether to > > fix those (lots of ugly "from __future__ import with_statement" > everywhere) > > or just to drop Python 2.5 support. > > > > What do people feel? > > Google app engine is still Python 2.5, as is Jython. But App Engine also supports Python 2.7: http://code.google.com/appengine/docs/python/python27/ (currently experimental, but then again App Engine itself was in preview mode until just this past month). -------------- next part -------------- An HTML attachment was scrubbed... URL: From miki.tebeka at gmail.com Wed Dec 21 17:10:10 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 21 Dec 2011 14:10:10 -0800 (PST) Subject: Need advice on the design of my application In-Reply-To: References: Message-ID: <32438051.312.1324505410308.JavaMail.geo-discussion-forums@yqdj19> I'd say go with option 1. Pass the window handler (or DC) to the rendered thread. To overcome the GIL problem, you can use Py_BEGIN_ALLOW_THREADS (see http://docs.python.org/release/1.5.2/api/threads.html) From miki.tebeka at gmail.com Wed Dec 21 17:10:10 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 21 Dec 2011 14:10:10 -0800 (PST) Subject: Need advice on the design of my application In-Reply-To: References: Message-ID: <32438051.312.1324505410308.JavaMail.geo-discussion-forums@yqdj19> I'd say go with option 1. Pass the window handler (or DC) to the rendered thread. To overcome the GIL problem, you can use Py_BEGIN_ALLOW_THREADS (see http://docs.python.org/release/1.5.2/api/threads.html) From ckaynor at zindagigames.com Wed Dec 21 17:17:23 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Wed, 21 Dec 2011 14:17:23 -0800 Subject: Need advice on the design of my application In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 2:10 PM, carlos choy wrote: > Thank you for your great advice. It is detailed and tells me what I need > to know, I wasn't expecting such an accurate response from anyone for some > time. > > I think Option 2 is the way I will go. Having never embedded before, I > think it will be interesting, besides then my IDE can help me debug more > easily than Option 1. > > Either way is a bit of a mixed bag. Going with extending, it will (generally) be harder to debug and test the C side of the code, as it is more difficult to get the C debugger attached. Depending on what IDE you are using (I am most used to Visual Studio 2010), you can probably set it up to execute a Python instance with command-line arguments for automatic attachment. You can also manually attach to the debugger that way. If you go with embedding, you will likely have a harder time debugging the Python side, unless you're very careful in your implementation (which you will need to be anyways) of the embedding. It will also, likely, be harder to actually step into and debug the code (I use Wing, and at work we have it setup to be attachable from any process via 2 lines of code). In either case, it should not be too hard to setup the IDEs to make debugging easy, but either way, you do not really get automatic support for one of the two languages. Note that this is generally a problem anytime you mix multiple languages. -------------- next part -------------- An HTML attachment was scrubbed... URL: From einazaki668 at yahoo.com Wed Dec 21 17:25:17 2011 From: einazaki668 at yahoo.com (Eric) Date: Wed, 21 Dec 2011 14:25:17 -0800 (PST) Subject: what does 'a=b=c=[]' do Message-ID: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Is it true that if I want to create an array or arbitrary size such as: for a in range(n): x.append() I must do this instead? x=[] for a in range(n): x.append() Now to my actual question. I need to do the above for multiple arrays (all the same, arbitrary size). So I do this: x=y=z=[] for a in range(n): x.append() y.append() z.append() Except it seems that I didn't create three different arrays, I created one array that goes by three different names (i.e. x[], y[] and z[] all reference the same pile of numbers, no idea which pile). This surprises me, can someone tell me why it shouldn't? I figure if I want to create and initialize three scalars the just do "a=b=c=7", for example, so why not extend it to arrays. Also, is there a more pythonic way to do "x=[], y=[], z=[]"? It's a slick language but I still have trouble wrapping my brain around some of the concepts. TIA, eric From joshua.landau.ws at gmail.com Wed Dec 21 18:06:11 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 23:06:11 +0000 Subject: what does 'a=b=c=[]' do In-Reply-To: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: On 21 December 2011 22:25, Eric wrote: > Is it true that if I want to create an array or arbitrary size such > as: > for a in range(n): > x.append() > > I must do this instead? > x=[] > for a in range(n): > x.append() > > Now to my actual question. I need to do the above for multiple arrays > (all the same, arbitrary size). So I do this: > x=y=z=[] > for a in range(n): > x.append() > y.append() > z.append() > > Except it seems that I didn't create three different arrays, I created > one array that goes by three different names (i.e. x[], y[] and z[] > all reference the same pile of numbers, no idea which pile). > > This surprises me, can someone tell me why it shouldn't? I figure if > I want to create and initialize three scalars the just do "a=b=c=7", > 7 is 7 => True They're the same "7". You won't notice it though, as numbers are immutable. for example, so why not extend it to arrays. Also, is there a more > pythonic way to do "x=[], y=[], z=[]"? > a, b, c = [], [], [] It's a slick language but I still have trouble wrapping my brain > around some of the concepts. > > TIA, > eric > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ckaynor at zindagigames.com Wed Dec 21 18:12:53 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Wed, 21 Dec 2011 15:12:53 -0800 Subject: what does 'a=b=c=[]' do In-Reply-To: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: On Wed, Dec 21, 2011 at 2:25 PM, Eric wrote: > > Is it true that if I want to create an array or arbitrary size such > as: > for a in range(n): > x.append() > > I must do this instead? > x=[] > for a in range(n): > x.append() You can also use a list comprehension: x = [ for a in range(n)] Or extend and a generator expression: x = [] x.extend( for a in range(n)) Or map and a generator function: map(x.append, ( for a in range(n))) I would recommend either your way, the first of, or the second of my ways, depending on the full context. > > > Now to my actual question. I need to do the above for multiple arrays > (all the same, arbitrary size). So I do this: > x=y=z=[] This creates a new object, then assigns the labels x, y, and z to that object. > for a in range(n): > x.append() > y.append() > z.append() Then this appends the items to each of those labels, which, as they pointing to the same object, appends to all of the labels. The "variables" in Python are merely labels, and assigning to different labels does not automatically copy the object. Consider: a = [] b = a a.append(1) print b [1] > > Except it seems that I didn't create three different arrays, I created > one array that goes by three different names (i.e. x[], y[] and z[] > all reference the same pile of numbers, no idea which pile). > > This surprises me, can someone tell me why it shouldn't? I figure if > I want to create and initialize three scalars the just do "a=b=c=7", > for example, so why not extend it to arrays. Also, is there a more > pythonic way to do "x=[], y=[], z=[]"? The above rules apply in all cases, however are generally invisible on immutable objects (strings, ints, floats, tuples). In the case of a=b=c=7, you will find that all of a, b, and c point to the same object (try the "id" function or "is" operator). Doing the operation a += 1 after a=7 will create a new int* with the value 7+1 and assign it to the label a. * In CPython, there exists an optimization where small ints are cached, namely from -7 to 255 (the lower bound I stated may be wrong). This improved performance in most cases, but is CPython specific - other implementations such as PyPy or IronPython may behave differently. > > It's a slick language but I still have trouble wrapping my brain > around some of the concepts. > > TIA, > eric > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Dec 21 18:44:53 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Dec 2011 23:44:53 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: <4ef26f74$0$29973$c3e8da3$5496439d@news.astraweb.com> On Wed, 21 Dec 2011 14:25:17 -0800, Eric wrote: > Is it true that if I want to create an array or arbitrary size such as: > for a in range(n): > x.append() x is not defined, so you will get a NameError unless by some lucky fluke something else has created x AND it happens to be a list. Either way, it is unlikely to do what you want. > I must do this instead? > x=[] > for a in range(n): > x.append() Yes, you should create your lists before trying to append to them. But you aren't forced to use a for-loop. You can use a list comprehension: x = [some_function(a) for a in range(n)] Notice that here you don't need x to pre-exist, because the list comp creates a brand new list, which then gets assigned directly to x. > Now to my actual question. I need to do the above for multiple arrays > (all the same, arbitrary size). So I do this: > x=y=z=[] This creates one empty list object, and gives it three names, x, y and z. Every time you append to the list, all three names see the same change, because they refer to a single list. [...] > Except it seems that I didn't create three different arrays, I created > one array that goes by three different names (i.e. x[], y[] and z[] all > reference the same pile of numbers, no idea which pile). Exactly. > This surprises me, can someone tell me why it shouldn't? Because that's the way Python works. Python is an object-oriented, name binding language. This is how OO name binding works: you have a single object, with three names bound to it. The above line is short-cut for: a = [] b = a c = a Python does not make a copy of the list unless you specifically instruct it to. > I figure if I > want to create and initialize three scalars the just do "a=b=c=7", That creates a single integer object with value 7, and binds three names to it, *exactly* the same as the above. If you could modify int objects in place, like you can modify lists in place, you would see precisely the same effect. But ints are immutable: all operations on ints create new ints. Lists are mutable, and can be changed in place. > for > example, so why not extend it to arrays. Also, is there a more pythonic > way to do "x=[], y=[], z=[]"? Well that literally won't work, you can't separate them by commas. Newlines or semicolons will work. Or: x, y, z = [], [], [] Either is pretty Pythonic. -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 21 18:48:30 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Dec 2011 23:48:30 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: <4ef2704d$0$29973$c3e8da3$5496439d@news.astraweb.com> On Wed, 21 Dec 2011 18:20:16 -0500, Dennis Lee Bieber wrote: > For the amount of typing, it's easier to just do a straight line > tuple unpack > >>>> a,b,c = ([],[],[]) Note that tuples are created by the comma, not the round brackets (or parentheses for any Americans reading). So the round brackets there are strictly redundant: a, b, c = [], [], [] The only times you need the brackets around a tuple is to control the precedence of operations, or for an empty tuple. -- Steven From wuwei23 at gmail.com Wed Dec 21 19:50:40 2011 From: wuwei23 at gmail.com (alex23) Date: Wed, 21 Dec 2011 16:50:40 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> On Dec 22, 8:25?am, Eric wrote: > This surprises me, can someone tell me why it shouldn't? ?I figure if > I want to create and initialize three scalars the just do "a=b=c=7", > for example, so why not extend it to arrays. The thing to remember is that everything is an object, and that it's better to think of variables as labels on an object. So: a=b=c=7 means that _one_ integer object with the value of 7 can be referenced using any of the labels a, b or c. x=y=z=[] means that _one_ empty list can be referenced using x, y or z. The difference is that the value of a number object _cannot be changed_ ('immutable') while a list can be modified to add or remove items ('mutable'). a=10 just reassigns the label a to an integer object of value 10. x.append("foo") _modifies_ the list referred to by x, which is the same list known as y & z. > Also, is there a more pythonic way to do "x=[], y=[], z=[]"? I'd say that _is_ the most pythonic way, it's very obvious in its intent (or would be with appropriate names). If it bothers you that much: def listgen(count, default=[]): for _ in xrange(count): yield default[:] x, y, z = listgen(3) From roy at panix.com Wed Dec 21 20:02:14 2011 From: roy at panix.com (Roy Smith) Date: Wed, 21 Dec 2011 20:02:14 -0500 Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> <9lef2pFgggU1@mid.individual.net> Message-ID: In article , Grant Edwards wrote: > C++ is a vast, complex, and dangerous language -- and industry > doesn't seem to be willing to limit itself to using the seven people > on the planet who understand it. > I'm only half joking... :) Half joking, indeed. I happen to know for a fact that there are *fourteen* people on the planet who understand it. From greg at krypto.org Wed Dec 21 20:48:37 2011 From: greg at krypto.org (Gregory P. Smith) Date: Wed, 21 Dec 2011 17:48:37 -0800 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: On Wed, Dec 21, 2011 at 2:57 AM, Jim Fulton wrote: > On Wed, Dec 21, 2011 at 2:15 AM, Chris Withers > wrote: > > Hi All, > > > > What's the general consensus on supporting Python 2.5 nowadays? > > > > Do people still have to use this in commercial environments or is > everyone > > on 2.6+ nowadays? > > > > I'm finally getting some continuous integration set up for my packages > and > > it's highlighting some 2.5 compatibility issues. I'm wondering whether to > > fix those (lots of ugly "from __future__ import with_statement" > everywhere) > > or just to drop Python 2.5 support. > > > > What do people feel? > > Google app engine is still Python 2.5, as is Jython. > There's work being done to change that on the app engine front: http://code.google.com/appengine/docs/python/python27/newin27.html -gps -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Wed Dec 21 21:15:31 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 21:15:31 -0500 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: Just because the default python version on a server is 2.4 doesn't mean you can't install 2.7.2... If the admins that run the machine are too lazy/stupid to install a second copy of Python let them rot. Of course, if by some nightmare scenario you have code that can't be upgraded for whatever reason, I'm so sorry. Nathan From rantingrickjohnson at gmail.com Wed Dec 21 22:43:43 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Wed, 21 Dec 2011 19:43:43 -0800 (PST) Subject: Why widgets become 'NoneType'? References: <2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com> Message-ID: <118d4919-6df3-457d-9409-d95a45b989ec@k10g2000yqk.googlegroups.com> On Dec 21, 6:59?am, Muddy Coder wrote: > Hi Folks, > > I was driven nuts by this thing: widgets lost their attributes, then I > can't configure them. Please take a look at the codes below: The problem is here... > ? ? labels = [] > ? ? for i in range(len(astr)): > ? ? ? ? lbl = Label(win, text=astr[i]).pack(side=LEFT ) label.pack() returns None. So you are creating a variable named "lbl" the value of which is None. Don't believe me? Try this... >>> from Tkinter import * >>> root = Tk() >>> label = Label(root).pack() >>> label['bg'] Traceback (most recent call last): File "", line 1, in label['bg'] TypeError: 'NoneType' object is not subscriptable >>> type(label) # # Now we separate the packing from the instancing. # >>> label = Label(root) >>> label.pack() >>> type(label) >>> label['bg'] 'SystemButtonFace' Same with list.sort. >>> lst = range(5) >>> lst [0, 1, 2, 3, 4] >>> var = lst.sort() >>> repr(var) 'None' From steve+comp.lang.python at pearwood.info Thu Dec 22 00:09:27 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 Dec 2011 05:09:27 GMT Subject: [TIP] Anyone still using Python 2.5? References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: <4ef2bb87$0$13942$c3e8da3$76491128@news.astraweb.com> On Wed, 21 Dec 2011 21:15:31 -0500, Nathan Rice wrote: > Just because the default python version on a server is 2.4 doesn't mean > you can't install 2.7.2... If the admins that run the machine are too > lazy/stupid to install a second copy of Python let them rot. If any of my sys admins installed non-supported software on one of my production servers without permission, they'd be looking for a new job. Just because some guy with root privileges can install software doesn't mean that he should. Having vendor support and security patches is far more important than the ability to write one-liner if statements. -- Steven From zyzhu2000 at gmail.com Thu Dec 22 00:45:13 2011 From: zyzhu2000 at gmail.com (GZ) Date: Wed, 21 Dec 2011 21:45:13 -0800 (PST) Subject: Generator Question Message-ID: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> Hi, I am wondering what would be the best way to return an iterator that has zero items. I just noticed the following two are different: def f(): pass def g(): if 0: yield 0 pass for x in f(): print x Traceback (most recent call last): File "", line 1, in TypeError: 'NoneType' object is not iterable for x in g(): print x #loop exits without any errors Now the question here is this: def h(): if condition=true: #I would like to return an itereator with zero length else: for ...: yield x In other words, when certain condition is met, I want to yield nothing. How to do? Thanks, gz From steve+comp.lang.python at pearwood.info Thu Dec 22 00:52:25 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 Dec 2011 05:52:25 GMT Subject: Generator Question References: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> Message-ID: <4ef2c599$0$13942$c3e8da3$76491128@news.astraweb.com> On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: > Hi, > > I am wondering what would be the best way to return an iterator that has > zero items. return iter([]) > I just noticed the following two are different: > > def f(): > pass That creates a function that does nothing, and then returns None (because Python doesn't have Pascal procedures or C void function). > def g(): > if 0: yield 0 > pass The pass is redundant. This creates a generator function which, when called, doesn't yield anything, then raises StopIteration. -- Steven From rosuav at gmail.com Thu Dec 22 01:08:12 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Dec 2011 17:08:12 +1100 Subject: Generator Question In-Reply-To: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> References: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> Message-ID: On Thu, Dec 22, 2011 at 4:45 PM, GZ wrote: > def h(): > ? ?if condition=true: > ? ? ? #I would like to return an itereator with zero length > ? ?else: > ? ? ? for ...: yield x Easy. Just 'return' in the conditional. >>> def h(): if condition: return for i in range(4): yield i >>> condition=False >>> h() >>> for i in h(): print(i) 0 1 2 3 >>> condition=True >>> h() >>> for i in h(): print(i) >>> A generator object is returned since the function contains a 'yield'. On one of the branches, nothing will ever be yielded and StopIteration will be raised immediately. You could probably also raise StopIteration directly, but it's not necessary. ChrisA From steve+comp.lang.python at pearwood.info Thu Dec 22 01:35:30 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 Dec 2011 06:35:30 GMT Subject: Generator Question References: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> Message-ID: <4ef2cfb2$0$13942$c3e8da3$76491128@news.astraweb.com> On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: > Now the question here is this: > > def h(): > if condition=true: > #I would like to return an itereator with zero length > else: > for ...: yield x > > In other words, when certain condition is met, I want to yield nothing. > How to do? Actually, there's an even easier way. >>> def h(): ... if not condition: ... for c in "abc": ... yield c ... >>> >>> condition = False >>> list(h()) ['a', 'b', 'c'] >>> condition = True >>> list(h()) [] -- Steven From stefan_ml at behnel.de Thu Dec 22 02:05:10 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 22 Dec 2011 08:05:10 +0100 Subject: Anyone still using Python 2.5? In-Reply-To: References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: Dennis Lee Bieber, 21.12.2011 17:48: > On Wed, 21 Dec 2011 07:15:46 +0000, Chris Withers wrote: >> What's the general consensus on supporting Python 2.5 nowadays? >> >> Do people still have to use this in commercial environments or is >> everyone on 2.6+ nowadays? > > I was recently laid-off from a program that is still using Python 2.3 [...] That reminds me: we shouldn't forget about embedded Python installations. They are usually somewhere between "very hard" and "impossible" to upgrade, also because they often use vendor supplied binary packages for plugin APIs. I've recently seen that in a FrontArena installation (basically a trading platform) that had an embedded Py2.3 for scripting. It wasn't exactly the cutting-edge release, but the users of these platforms tend to be pretty conservative with their upgrades, and the time it takes the vendor to upgrade to a new embedded Python version can be similarly long. That means it can take several years before an embedded 2.7 hits the end users, during which anything can happen, from "vendor switches from Python to Lua" to "vendor goes bankrupt" (or maybe just one after the other). Stefan From speeze.pearson at gmail.com Thu Dec 22 03:21:41 2011 From: speeze.pearson at gmail.com (Spencer Pearson) Date: Thu, 22 Dec 2011 00:21:41 -0800 (PST) Subject: Adding an interface to existing classes Message-ID: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> I'm writing a geometry package, with Points and Lines and Circles and so on, and eventually I want to be able to draw these things on the screen. I have two options so far for how to accomplish this, but neither of them sits quite right with me, and I'd like the opinion of comp.lang.python's wizened elders. Option 1. Subclassing. The most Pythonic way would seem to be writing subclasses for the things I want to display, adding a ".draw(...)" method to each one, like this: class DrawablePoint( geometry.Point ): class draw( self, ... ): ... When the time comes to draw things, I'll have some list of objects I want drawn, and say for x in to_draw: x.draw(...) I see a problem with this, though. The intersection of two lines is (usually) an object of type Point. Since DrawableLine inherits from Line, this means that unless I redefine the "intersect" method in DrawableLine, the intersection of two DrawableLines will be a Point object, not a DrawablePoint. I don't want to saddle the user with the burden of converting every method output into its corresponding Drawable subclass, and I don't want to redefine every method to return an instance of said subclass. I see no other options if I continue down the subclassing path. Am I missing something? Option 2. A "draw" function, with a function dictionary. This feels weird, but is fairly simple to write, use, and extend. We have a module with a "draw_functions" dictionary that maps types onto functions, and a "draw" function that just looks up the proper type in the dictionary and calls the corresponding function. If you create your own object, you can just add a new entry to the dictionary. The implementation is simple enough to outline here: In file "geometry/gui.py": def draw_point(...): ... def draw_line(...): ... draw_functions = {geometry.Point: draw_point, geometry.Line: draw_line, ...} def draw( x, *args, **kwargs ): for type, callback in draw_functions.iteritems(): if isinstance(x, type): callback(x, *args, **kwargs) else: raise TypeError("don't know how to draw things of type " "{0}".format(type(x))) In the file that uses this: # Drawing a predefined type of object: geometry.gui.draw(some_point, ...) # Here we define a new kind of object and tell the package how to draw it. class MyObject(GeometricObject): ... def draw_my_object(...): ... geometry.gui.draw_functions[MyObject] = draw_my_object # And now we draw it. geometry.gui.draw(MyObject(...), ...) If I feel fancy, I might use a decorator for adding entries to draw_functions, but this is basically how it'd work. The second way feels kludgey to me, but I'm leaning towards it because it seems like so much less work and I'm out of ideas. Can anyone help, explaining either a different way to do it or why one of these isn't as bad as I think? Thanks for your time! -Spencer From rolf at roce.be Thu Dec 22 03:51:54 2011 From: rolf at roce.be (Rolf Camps) Date: Thu, 22 Dec 2011 09:51:54 +0100 Subject: what does 'a=b=c=[]' do In-Reply-To: <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: <1324543914.2075.82.camel@puppy> alex23 schreef op wo 21-12-2011 om 16:50 [-0800]: > On Dec 22, 8:25 am, Eric wrote: > > This surprises me, can someone tell me why it shouldn't? I figure if > > I want to create and initialize three scalars the just do "a=b=c=7", > > for example, so why not extend it to arrays. > > The thing to remember is that everything is an object, and that it's > better to think of variables as labels on an object. > > So: a=b=c=7 means that _one_ integer object with the value of 7 can be > referenced using any of the labels a, b or c. x=y=z=[] means that > _one_ empty list can be referenced using x, y or z. > > The difference is that the value of a number object _cannot be > changed_ ('immutable') while a list can be modified to add or remove > items ('mutable'). a=10 just reassigns the label a to an integer > object of value 10. x.append("foo") _modifies_ the list referred to by > x, which is the same list known as y & z. > > > Also, is there a more pythonic way to do "x=[], y=[], z=[]"? > > I'd say that _is_ the most pythonic way, it's very obvious in its > intent (or would be with appropriate names). If it bothers you that > much: > > def listgen(count, default=[]): > for _ in xrange(count): > yield default[:] > > x, y, z = listgen(3) > I'm afraid it's dangerous to encourage the use of '[]' as assignment to a parameter in a function definition. If you use the function several times 'default' always points to the same list. >>> def return_list(list_ = []): >>> return list_ >>> a_list = return_list() >>> a_list [] >>> a_list.append(3) >>> a_list [3] >>> b_list = return_list() >>> b_list >>> [3] # !!?? >>> def return_list(): >>> return [] >>> a_list = return_list() >>> a_list [] >>> a_list.append(3) >>> a_list [3] >>> b_list = return_list() >>> b_list >>> [] # OK! I only use python3 so I don't know how these things work in other versions. No problem in your function since you yield a copy, but I've already seen long threads about this. I would change your function to (Python3.x): def empty_lists(count): for _ in range(count): yield [] Regards, Rolf From praveen.venkata at gmail.com Thu Dec 22 06:04:02 2011 From: praveen.venkata at gmail.com (Ven) Date: Thu, 22 Dec 2011 03:04:02 -0800 (PST) Subject: Item Checking not possible with UltimateListCtrl in ULC_VIRTUAL mode Message-ID: Following is the system and software info Platforms: Windows XP and OSX Lion Activestate Python 2.7.2 wxPython2.9-osx-cocoa-py2.7 (for OSX) wxPython2.9-win32-py27 (for Windows XP) I am trying to create a UltimateListCtrl using ULC_VIRTUAL and ULC_REPORT mode. I would like to know how can I put a checkbox beside the first column of every row and catch the event when a user checks the box. I was able to do the same using UltimateListCtrl without VIRTUAL mode. But, with the ULC_VIRTUAL flag ON, I don't know how to proceed. Following is the code I created, but this still doesn't allow me to check the boxes associated with the first column. Please help. import wx import images import random import os, sys from wx.lib.agw import ultimatelistctrl as ULC class TestUltimateListCtrl(ULC.UltimateListCtrl): def __init__(self, parent, log): ULC.UltimateListCtrl.__init__(self, parent, -1, agwStyle=ULC.ULC_AUTO_CHECK_CHILD|ULC.ULC_VIRTUAL|ULC.ULC_REPORT| ULC.ULC_SINGLE_SEL|ULC.ULC_VRULES|ULC.ULC_HRULES) self.SetItemCount(1000) self.table_fields=['First','Second','Third'] field_index=0 for field in self.table_fields: info = ULC.UltimateListItem() info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT | ULC.ULC_MASK_CHECK info._image = [] info._format = wx.LIST_FORMAT_CENTER info._kind = 1 info._text = field info._font= wx.Font(13, wx.ROMAN, wx.NORMAL, wx.BOLD) self.InsertColumnInfo(field_index, info) self.SetColumnWidth(field_index,175) field_index += 1 def getColumnText(self, index, col): item = self.GetItem(index, col) return item.GetText() def OnGetItemText(self, item, col): return "Item %d, Column %d" % (item,col) def OnGetItemColumnImage(self, item, col): return [] def OnGetItemImage(self, item): return [] def OnGetItemAttr(self, item): return None def OnGetItemTextColour(self, item, col): return None #def OnGetItemColumnCheck(self, item, col): #return True #def OnGetItemCheck(self, item): #item=self.GetItem(item) #return item.IsChecked() def OnGetItemToolTip(self, item, col): return None def OnGetItemKind(self, item): return 1 def OnGetItemColumnKind(self, item, col): if col==0: return self.OnGetItemKind(item) return 0 class TestFrame(wx.Frame): def __init__(self, parent, log): wx.Frame.__init__(self, parent, -1, "UltimateListCtrl in wx.LC_VIRTUAL mode", size=(700, 600)) panel = wx.Panel(self, -1) sizer = wx.BoxSizer(wx.VERTICAL) listCtrl = TestUltimateListCtrl(panel, log) sizer.Add(listCtrl, 1, wx.EXPAND) panel.SetSizer(sizer) sizer.Layout() self.CenterOnScreen() self.Show() if __name__ == '__main__': import sys app = wx.PySimpleApp() frame = TestFrame(None, sys.stdout) frame.Show(True) app.MainLoop() Btw, following is the code I used to create the same thing without the VIRTUAL mode. And in this case, I can check the boxes beside the first column data in every row. But, I will be working with tens of thousands of items and I cannot rely on loading the items like below because it is very slow. Hence, I want to use the Virtual List, but I don't know how to get the same functionality in it. import wx import images import random import os, sys from wx.lib.agw import ultimatelistctrl as ULC class TestUltimateListCtrl(ULC.UltimateListCtrl): def __init__(self, parent, log): ULC.UltimateListCtrl.__init__(self, parent, -1, agwStyle=ULC.ULC_REPORT|ULC.ULC_SINGLE_SEL|ULC.ULC_VRULES| ULC.ULC_HRULES) self.table_fields=['First','Second','Third'] field_index=0 for field in self.table_fields: info = ULC.UltimateListItem() info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT | ULC.ULC_MASK_CHECK info._image = [] info._format = wx.LIST_FORMAT_CENTER info._kind = 1 info._text = field info._font= wx.Font(13, wx.ROMAN, wx.NORMAL, wx.BOLD) self.InsertColumnInfo(field_index, info) self.SetColumnWidth(field_index,175) field_index += 1 for record_index in range(0,1000): for field in self.table_fields: if self.table_fields.index(field)==0: self.InsertStringItem(record_index, 'Item %d, Column %d' % (record_index,self.table_fields.index(field)),it_kind=1) else: self.SetStringItem(record_index, self.table_fields.index(field), 'Item %d, Column %d' % (record_index,self.table_fields.index(field))) class TestFrame(wx.Frame): def __init__(self, parent, log): wx.Frame.__init__(self, parent, -1, "UltimateListCtrl in wx.LC_VIRTUAL mode", size=(700, 600)) panel = wx.Panel(self, -1) sizer = wx.BoxSizer(wx.VERTICAL) listCtrl = TestUltimateListCtrl(panel, log) sizer.Add(listCtrl, 1, wx.EXPAND) panel.SetSizer(sizer) sizer.Layout() self.CenterOnScreen() self.Show() if __name__ == '__main__': import sys app = wx.PySimpleApp() frame = TestFrame(None, sys.stdout) frame.Show(True) app.MainLoop() From y.turgut at gmail.com Thu Dec 22 06:11:13 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Thu, 22 Dec 2011 03:11:13 -0800 (PST) Subject: Text Processing References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> <20111220210321.77451e19@bouzin.lan> <7895.1324415085@alphaville.americas.hpqcorp.net> Message-ID: <6ff3a578-5a77-4e31-8e54-e29c46299e3c@f1g2000yqi.googlegroups.com> On Dec 21, 2:01?am, Alexander Kapps wrote: > On 20.12.2011 22:04, Nick Dokos wrote: > > > > > > > > > > >>> I have a text file containing such data ; > > >>> ? ? ? ? ?A ? ? ? ? ? ? ? ?B ? ? ? ? ? ? ? ?C > >>> ------------------------------------------------------- > >>> -2.0100e-01 ? ?8.000e-02 ? ?8.000e-05 > >>> -2.0000e-01 ? ?0.000e+00 ? 4.800e-04 > >>> -1.9900e-01 ? ?4.000e-02 ? ?1.600e-04 > > >>> But I only need Section B, and I need to change the notation to ; > > >>> 8.000e-02 = 0.08 > >>> 0.000e+00 = 0.00 > >>> 4.000e-02 = 0.04 > > Does it have to be python? If not, I'd go with something similar to > > > ? ? sed 1,2d foo.data | awk '{printf("%.2f\n", $2);}' > > Why sed and awk: > > awk 'NR>2 {printf("%.2f\n", $2);}' data.txt > > And in Python: > > f = open("data.txt") > f.readline() ? ?# skip header > f.readline() ? ?# skip header > for line in f: > ? ? ?print "%02s" % float(line.split()[1]) @Jerome ; Your suggestion provided floating point error, it might need some slight modificiation. @Nick ; Sorry mate, it needs to be in Python. But I noted solution in case if I need for another case. @Alexander ; Works as expected. Thank you all for the replies. From robert.kern at gmail.com Thu Dec 22 06:15:11 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 22 Dec 2011 11:15:11 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 12/21/11 6:52 PM, Joshua Landau wrote: > If fix means "return number" then no. This inconsistency between elementwise > operations and 'normal' ones is the problem. Again, explicit elementwise-ifying > through "~" fixes that. You need to tell me why this is worth the confusion over > that. I understand that you are a supporter of PEP 225, but please have some courtesy towards other developers. We can explore alternatives in parallel. If we all worked on exactly the same thing (much less the one thing that only you get to choose), we would forgo a lot of valuable experience. You can point out the flaws of Elementwise's implementation and compare it to the virtues of PEP 225 without being so demanding. He doesn't "need to tell" you a damn thing. -- 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 From hansmu at xs4all.nl Thu Dec 22 06:17:17 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 22 Dec 2011 12:17:17 +0100 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: References: <4EF22CA1.1070200@gmail.com> <4EF233A7.6020907@stoneleaf.us> Message-ID: <4ef311bd$0$6870$e4fe514c@news2.news.xs4all.nl> On 21/12/11 21:11:03, Andrew Berg wrote: > On 12/21/2011 1:29 PM, Ethan Furman wrote: >> Anything that runs at import time should be protected by the `if >> __name__ == '__main__'` idiom as the children will import the __main__ >> module. > So the child imports the parent and runs the spawn code again? That > makes sense. It's platform dependent. On Windows, the child imports the parent and you get the phenomenon your ran into. On Posix platforms, multiprocessing uses fork(), and your code runs without problems. It would still be a good idea to use the `if __name__ is '__main__'` idiom, even on Posix platforms. Hope this helps, -- HansM From robert.kern at gmail.com Thu Dec 22 06:18:38 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 22 Dec 2011 11:18:38 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 12/21/11 5:07 PM, Paul Dubois wrote: > You're reinventing Numeric Python. Actually, he's not. The numerical operators certainly overlap, but Elementwise supports quite a bit more generic operations than we ever bothered to implement with object arrays. -- 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 From robert.kern at gmail.com Thu Dec 22 06:24:21 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 22 Dec 2011 11:24:21 +0000 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: <4EF23D57.6010305@gmail.com> References: <4EF22CA1.1070200@gmail.com> <4EF233A7.6020907@stoneleaf.us> <4EF23D57.6010305@gmail.com> Message-ID: On 12/21/11 8:11 PM, Andrew Berg wrote: > On 12/21/2011 1:29 PM, Ethan Furman wrote: >> Anything that runs at import time should be protected by the `if >> __name__ == '__main__'` idiom as the children will import the __main__ >> module. > So the child imports the parent and runs the spawn code again? That > makes sense. This is a problem with multiprocessing on Windows. Your code would work fine on a UNIX system. The problem is that Windows does not have a proper fork() for multiprocessing to use. Consequently, it has to start a new Python process from scratch and then *import* the main module such that it can properly locate the rest of the code to start. If you do not guard the spawning code with a __main__ test, then this import will cause an infinite loop. Just as a further note on these lines, when older versions of setuptools and distribute install scripts, they generate stub scripts that do not use a __main__ guard, so installing a multiprocessing-using application with setuptools can cause this problem. I believe newer versions of distribute has this problem fixed, but I'm not sure. -- 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 From ndbecker2 at gmail.com Thu Dec 22 06:49:16 2011 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 22 Dec 2011 06:49:16 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> Message-ID: I agree with the OP that the current syntax is confusing. The issue is, the meaning of * is context-dependent. Why not this: Func (*args) == Func (unpack (args)) def Func (*args) == Func (pack (args)) That seems very clear IMO From steve+comp.lang.python at pearwood.info Thu Dec 22 08:12:57 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 Dec 2011 13:12:57 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> Message-ID: <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Dec 2011 06:49:16 -0500, Neal Becker wrote: > I agree with the OP that the current syntax is confusing. The issue is, > the meaning of * is context-dependent. Here you are complaining about an operator being "confusing" because it is context-dependent, in a post where you strip all context except the subject line and expect us to still understand what you're talking about. There's a lesson there, I'm sure. * is context dependent. You know what else is context dependent? Well, most things. But in particular, parentheses. Clearly they must be "confusing" too. So how about we say: class MyClass superclasslist A, B C: def method argumentlist self, x, y: t = tuple 1, 2 tuple 3, 4 endtuple endtuple return group x + y endgroup * group x - y endgroup Much less confusing! -- Steven From neilc at norwich.edu Thu Dec 22 08:19:21 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 22 Dec 2011 13:19:21 GMT Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> <9lef2pFgggU1@mid.individual.net> Message-ID: <9lgp2pFd2sU4@mid.individual.net> On 2011-12-22, Roy Smith wrote: > In article , > Grant Edwards wrote: >> C++ is a vast, complex, and dangerous language -- and industry >> doesn't seem to be willing to limit itself to using the seven >> people on the planet who understand it. > >> I'm only half joking... :) > > Half joking, indeed. I happen to know for a fact that there > are *fourteen* people on the planet who understand it. One of its greatest contributions to computer science were Glassbarrow's C++ puzzles. They likely couldn't have been as challenging in any other language. ;) -- Neil Cerutti From ethan at stoneleaf.us Thu Dec 22 08:20:10 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 22 Dec 2011 05:20:10 -0800 Subject: what does 'a=b=c=[]' do In-Reply-To: <1324543914.2075.82.camel@puppy> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <1324543914.2075.82.camel@puppy> Message-ID: <4EF32E8A.70900@stoneleaf.us> Rolf Camps wrote: > alex23 schreef op wo 21-12-2011 om 16:50 [-0800]: >> I'd say that _is_ the most pythonic way, it's very obvious in its >> intent (or would be with appropriate names). If it bothers you that >> much: >> >> def listgen(count, default=[]): >> for _ in xrange(count): >> yield default[:] >> >> x, y, z = listgen(3) >> > I would change your function to (Python3.x): > > def empty_lists(count): > for _ in range(count): > yield [] While it's good to be careful, default mutable arguments have their place. Alex's versioun allows one to use an already existing list and get shallow copies of it, yours will only create empty lists. a, b, c = listgen([1, 2, 3]) # a, b, & c are bound to different lists ~Ethan~ From rosuav at gmail.com Thu Dec 22 08:30:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 00:30:56 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 23, 2011 at 12:12 AM, Steven D'Aprano wrote: > class MyClass superclasslist A, B C: > ? ?def method argumentlist self, x, y: > ? ? ? ?t = tuple 1, 2 tuple 3, 4 endtuple endtuple > ? ? ? ?return group x + y endgroup * group x - y endgroup > > > Much less confusing! A definite improvement. However, I fear that the numerals "1", "2", etc are far too vague. In some contexts, the abuttal of two such numerals results in a larger number by a factor of 10, while in others, the factor is 8, or 16, or 2. This must not be. We need an unambiguous representation of integers. Also, I think we should adopt an existing standard [1]. This is generally a good idea. class MyClass superclasslist A, B C: def method argumentlist self, x, y: t = tuple summer's day, proud pony tuple sum of honest purse and fellow, large proud town endtuple endtuple return product of group sum of x and y endgroup and group difference between x and y endgroup You will find this a vast improvement. ChrisA [1] http://shakespearelang.sourceforge.net/report/shakespeare/shakespeare.html#SECTION00045000000000000000 or http://tinyurl.com/6sycv From hansmu at xs4all.nl Thu Dec 22 09:13:22 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 22 Dec 2011 15:13:22 +0100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef33b02$0$6952$e4fe514c@news2.news.xs4all.nl> On 22/12/11 14:12:57, Steven D'Aprano wrote: > On Thu, 22 Dec 2011 06:49:16 -0500, Neal Becker wrote: > >> I agree with the OP that the current syntax is confusing. The issue is, >> the meaning of * is context-dependent. > > Here you are complaining about an operator being "confusing" because it > is context-dependent, in a post where you strip all context except the > subject line and expect us to still understand what you're talking about. > There's a lesson there, I'm sure. > > > * is context dependent. You know what else is context dependent? Well, > most things. But in particular, parentheses. Clearly they must be > "confusing" too. So how about we say: > > class MyClass superclasslist A, B C: > def method argumentlist self, x, y: > t = tuple 1, 2 tuple 3, 4 endtuple endtuple > return group x + y endgroup * group x - y endgroup > > > Much less confusing! How about: 1 2 34 xx xx More more readable! And it's a standard! :-) -- HansM From rosuav at gmail.com Thu Dec 22 09:30:29 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 01:30:29 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4ef33b02$0$6952$e4fe514c@news2.news.xs4all.nl> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef33b02$0$6952$e4fe514c@news2.news.xs4all.nl> Message-ID: On Fri, Dec 23, 2011 at 1:13 AM, Hans Mulder wrote: > How about: > > > ... > > > More more readable! ?And it's a standard! Unfortunately it's not Pythonic, because indentation is insignificant. We need to adopt a more appropriate form. Eliminate all the tags and use indentation to mark the ends of elements. ChrisA PS. Brilliant, sir, brilliant! I take off my cap to you. From nikos.kouras at gmail.com Thu Dec 22 09:40:27 2011 From: nikos.kouras at gmail.com (=?ISO-8859-7?B?zenq/Ovh7/Igyu/98eHy?=) Date: Thu, 22 Dec 2011 06:40:27 -0800 (PST) Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error Message-ID: Hello when i try to visit my webpage i get the error it displays. Iam not posting it since you can see it by visiting my webpage at http://superhost.gr Please if you can tell me what might be wrong. From iversonstan at gmail.com Thu Dec 22 09:55:06 2011 From: iversonstan at gmail.com (Stan Iverson) Date: Thu, 22 Dec 2011 10:55:06 -0400 Subject: Spanish Accents Message-ID: Hi; If I write a python page to print to the web with Spanish accents all is well. However, if I read the data from a text file it prints diamonds with question marks wherever there are accented vowels. Please advise. TIA, Stan -------------- next part -------------- An HTML attachment was scrubbed... URL: From gslindstrom at gmail.com Thu Dec 22 09:58:34 2011 From: gslindstrom at gmail.com (Greg Lindstrom) Date: Thu, 22 Dec 2011 08:58:34 -0600 Subject: Overlaying PDF with data Message-ID: Hello, I would like to take an existing pdf form and overlay text "on top" (name, address, etc.). I'm looking at ReportLab v2.5 and see their "PLATYPUS" library might be what I crave, but would like to know if there's a more obvious way to do this? I'm working with health care forms which are quite complex (over 150 fields to potentially populate) and would like to do it as painlessly as possible. I'm running Python 2.7 on Windows. Thanks for any advice you may have, --greg -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu Dec 22 09:58:43 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 01:58:43 +1100 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Fri, Dec 23, 2011 at 1:55 AM, Stan Iverson wrote: > Hi; > If I write a python page to print to the web with Spanish accents all is > well. However, if I read the data from a text file it prints diamonds with > question marks wherever there are accented vowels. Please advise. Sounds like an encoding problem. Firstly, are you using Python 2 or Python 3? Things will be slightly different, since the default 'str' object in Py3 is Unicode. I would guess that your page is being output as UTF-8; you may find that the solution is as easy as declaring the encoding of your text file when you read it in. ChrisA From mwilson at the-wire.com Thu Dec 22 10:06:46 2011 From: mwilson at the-wire.com (Mel Wilson) Date: Thu, 22 Dec 2011 10:06:46 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef33b02$0$6952$e4fe514c@news2.news.xs4all.nl> Message-ID: Chris Angelico wrote: > On Fri, Dec 23, 2011 at 1:13 AM, Hans Mulder wrote: >> How about: >> >> >> ... >> >> >> More more readable! And it's a standard! > > Unfortunately it's not Pythonic, because indentation is insignificant. Easy-peasy: Mel. > We need to adopt a more appropriate form. Eliminate all the > tags and use indentation to mark the ends of elements. > > ChrisA > PS. Brilliant, sir, brilliant! I take off my cap to you. From iversonstan at gmail.com Thu Dec 22 10:25:46 2011 From: iversonstan at gmail.com (Stan Iverson) Date: Thu, 22 Dec 2011 11:25:46 -0400 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 10:58 AM, Chris Angelico wrote: > Firstly, are you using Python 2 or Python 3? Things will be slightly > different, since the default 'str' object in Py3 is Unicode. > 2 > > I would guess that your page is being output as UTF-8; you may find > that the solution is as easy as declaring the encoding of your text > file when you read it in. > So I tried this: file = open(p + "2.txt") for line in file: print unicode(line, 'utf-8') and got this error: 142 print unicode(line, 'utf-8') 143 144 print '''

*builtin* *unicode* = , *line* = '\r\n' /usr/lib64/python2.4/encodings/utf_8.pyin *decode*(input=, errors='strict') 14 15 def decode(input, errors='strict'): 16 return codecs.utf_16_decode(input, errors, True) 17 18 class StreamWriter(codecs.StreamWriter): *global* *codecs* = , codecs.*utf_16_decode* = , *input* = , *errors* = 'strict', *builtin* *True* = True *UnicodeDecodeError*: 'utf16' codec can't decode byte 0x0a in position 20: truncated data args = ('utf16', '\r\n', 20, 21, 'truncated data') encoding = 'utf16' end = 21 object = '\r\n' reason = 'truncated data' start = 20 Tried it with utf-16 with same results. TIA, Stan -------------- next part -------------- An HTML attachment was scrubbed... URL: From rami.chowdhury at gmail.com Thu Dec 22 10:30:21 2011 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Thu, 22 Dec 2011 15:30:21 +0000 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 15:25, Stan Iverson wrote: > On Thu, Dec 22, 2011 at 10:58 AM, Chris Angelico wrote: > >> Firstly, are you using Python 2 or Python 3? Things will be slightly >> different, since the default 'str' object in Py3 is Unicode. >> > > 2 > >> >> I would guess that your page is being output as UTF-8; you may find >> that the solution is as easy as declaring the encoding of your text >> file when you read it in. >> > > So I tried this: > > file = open(p + "2.txt") > for line in file: > print unicode(line, 'utf-8') > Could you try using the 'open' function from the 'codecs' module? file = codecs.open(p + "2.txt", "utf-8") # or whatever encoding your file is written in for line in file: print line > > and got this error: > > 142 print unicode(line, 'utf-8') > 143 > 144 print '''

> *builtin* *unicode* = , *line* = '\r\n > ' /usr/lib64/python2.4/encodings/utf_8.py in *decode*(input= buffer ptr 0x2b197e378454, size 21>, errors='strict') 14 > 15 def decode(input, errors='strict'): > 16 return codecs.utf_16_decode(input, errors, True) > 17 > 18 class StreamWriter(codecs.StreamWriter): > *global* *codecs* = '/usr/lib64/python2.4/codecs.pyc'>, codecs.*utf_16_decode* = function utf_16_decode>, *input* = size 21>, *errors* = 'strict', *builtin* *True* = True > > *UnicodeDecodeError*: 'utf16' codec can't decode byte 0x0a in position > 20: truncated data > args = ('utf16', '\r\n', 20, 21, 'truncated > data') > encoding = 'utf16' > end = 21 > object = '\r\n' > reason = 'truncated data' > start = 20 > > Tried it with utf-16 with same results. > > TIA, > > Stan > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 -------------- next part -------------- An HTML attachment was scrubbed... URL: From rami.chowdhury at gmail.com Thu Dec 22 10:32:34 2011 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Thu, 22 Dec 2011 15:32:34 +0000 Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error In-Reply-To: References: Message-ID: 2011/12/22 ???????? ?????? : > Hello when i try to visit my webpage i get the error it displays. Iam > not posting it since you can see it by visiting my webpage at > http://superhost.gr > > Please if you can tell me what might be wrong. I can't see any errors on that page -- can you please post the complete traceback so we can all see it? -- Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 From bex.lewis at gmail.com Thu Dec 22 10:52:39 2011 From: bex.lewis at gmail.com (becky_lewis) Date: Thu, 22 Dec 2011 07:52:39 -0800 (PST) Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error References: Message-ID: <21f682a5-fc62-4820-9cc5-f5dfd064ec6a@t16g2000vba.googlegroups.com> On Dec 22, 2:40?pm, ???????? ?????? wrote: > Hello when i try to visit my webpage i get the error it displays. Iam > not posting it since you can see it by visiting my webpage athttp://superhost.gr > > Please if you can tell me what might be wrong. It doesn't seem entirely clear but if I had to guess I'd think that for some reason os.environ['REMOTE_ADDR'] is not returning a good value (os.environ is a dictionary holding all of the os evironment variables). According to the socket docs, this error gets raised for address related errors. From iversonstan at gmail.com Thu Dec 22 11:22:52 2011 From: iversonstan at gmail.com (Stan Iverson) Date: Thu, 22 Dec 2011 12:22:52 -0400 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 11:30 AM, Rami Chowdhury wrote: > Could you try using the 'open' function from the 'codecs' module? > I believe this is what you meant: file = codecs.open(p + "2.txt", "r", "utf-8") for line in file: print line but got this error: 141 file = codecs.open(p + "2.txt", "r", "utf-8") 142 for line in file: 143 print line 144 *line* = '\r\n', *file* = /usr/lib64/python2.4/codecs.py in *next*(self=) 492 493 """ Return the next decoded line from the input stream.""" 494 return self.reader.next() 495 496 def __iter__(self): *self* = , self.*reader* = , self.reader.*next* = > /usr/lib64/python2.4/codecs.py in *next*(self=) 429 430 """ Return the next decoded line from the input stream.""" 431 line = self.readline() 432 if line: 433 return line line *undefined*, *self* = , self.* readline* = > /usr/lib64/python2.4/codecs.py in *readline*(self=, size=None, keepends=True) 344 # If size is given, we call read() only once 345 while True: 346 data = self.read(readsize, firstline=True) 347 if data: 348 # If we're at a "\r" read one extra character (which might data *undefined*, *self* = , self.*read* = >, * readsize* = 72, firstline *undefined*, *builtin* *True* = True /usr/lib64/python2.4/codecs.py in *read*(self=, size=72, chars=-1, firstline=True) 291 data = self.bytebuffer + newdata 292 try: 293 newchars, decodedbytes = self.decode(data, self.errors) 294 except UnicodeDecodeError, exc: 295 if firstline: *newchars* = u'', *decodedbytes* = 0, *self* = , self.*decode* = , *data* = '\xe1intentado para ellos bastante sabios para discernir lo obvio. Tales perso', self.*errors* = 'strict' *UnicodeDecodeError*: 'utf8' codec can't decode bytes in position 0-2: invalid data args = ('utf8', '\xe1 intentado para ellos bastante sabios para discernir lo obvio. Tales perso', 0, 3, 'invalid data') encoding = 'utf8' end = 3 object = '\xe1 intentado para ellos bastante sabios para discernir lo obvio. Tales perso' reason = 'invalid data' start = 0 which is the letter ? (a with accent). So I tried with utf-16 and got this error: 141 file = codecs.open(p + "2.txt", "r", "utf-16") 142 for line in file: 143 print line 144 *line* = '\r\n', *file* = /usr/lib64/python2.4/codecs.py in *next*(self=) 492 493 """ Return the next decoded line from the input stream.""" 494 return self.reader.next() 495 496 def __iter__(self): *self* = , self.*reader* = , self.reader.*next* = > /usr/lib64/python2.4/codecs.py in *next*(self=) 429 430 """ Return the next decoded line from the input stream.""" 431 line = self.readline() 432 if line: 433 return line line *undefined*, *self* = , self.* readline* = > /usr/lib64/python2.4/codecs.py in *readline*(self=, size=None, keepends=True) 344 # If size is given, we call read() only once 345 while True: 346 data = self.read(readsize, firstline=True) 347 if data: 348 # If we're at a "\r" read one extra character (which might data *undefined*, *self* = , self.*read* = >, * readsize* = 72, firstline *undefined*, *builtin* *True* = True /usr/lib64/python2.4/codecs.py in *read*(self=, size=72, chars=-1, firstline=True) 291 data = self.bytebuffer + newdata 292 try: 293 newchars, decodedbytes = self.decode(data, self.errors) 294 except UnicodeDecodeError, exc: 295 if firstline: newchars *undefined*, decodedbytes *undefined*, *self* = , self.*decode* = >, *data* = '\r\nNoticia: Este sitio web entre este portal est\xe1 i', self.*errors* = 'strict' /usr/lib64/python2.4/encodings/utf_16.py in *decode*(self=, input='\r\nNoticia: Este sitio web entre este portal est\xe1 i', errors='strict') 47 self.decode = codecs.utf_16_be_decode 48 elif consumed>=2: 49 raise UnicodeError,"UTF-16 stream does not start with BOM" 50 return (object, consumed) 51 *builtin* *UnicodeError* = *UnicodeError*: UTF-16 stream does not start with BOM args = ('UTF-16 stream does not start with BOM',) -------------- next part -------------- An HTML attachment was scrubbed... URL: From nikos.kouras at gmail.com Thu Dec 22 11:29:37 2011 From: nikos.kouras at gmail.com (=?ISO-8859-7?B?zenq/Ovh7/Igyu/98eHy?=) Date: Thu, 22 Dec 2011 08:29:37 -0800 (PST) Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error References: Message-ID: <920839e5-58fa-4fdf-9c88-7bed9a7448fd@d10g2000vbh.googlegroups.com> On 22 ???, 17:32, Rami Chowdhury wrote: > 2011/12/22 ???????? ?????? : > > > Hello when i try to visit my webpage i get the error it displays. Iam > > not posting it since you can see it by visiting my webpage at > >http://superhost.gr > > > Please if you can tell me what might be wrong. > > I can't see any errors on that page -- can you please post the > complete traceback so we can all see it? Yes of course. Its the following: A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred. /home/nikos/public_html/cgi-bin/counter.py 15 16 hits = 0 17 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] 18 date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S' ) 19 agent = os.environ['HTTP_USER_AGENT'] host undefined, socket = , socket.gethostbyaddr = , os = , os.environ = {'REDIRECT_QUERY_STRING': 'page=index.html', 'HT...e,sdch', 'UNIQUE_ID': 'TvNasbAJGaoAABtUy at gAAAAI'} herror: (1, '\xb6\xe3\xed\xf9\xf3\xf4\xef \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2') args = (1, '\xb6\xe3\xed\xf9\xf3\xf4\xef \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2') From rosuav at gmail.com Thu Dec 22 11:35:54 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 03:35:54 +1100 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Fri, Dec 23, 2011 at 3:22 AM, Stan Iverson wrote: > > On Thu, Dec 22, 2011 at 11:30 AM, Rami Chowdhury wrote: >> >> Could you try using the 'open' function from the 'codecs' module? > > I believe this is what you meant: > > file = codecs.open(p + "2.txt", "r", "utf-8") > > but got this error: > > ?/usr/lib64/python2.4/codecs.py in next(self=) Your file almost certainly isn't UTF-16, so don't bother trying that. I think you may have ISO-8859-1 encoding, also called Latin-1, but it's not easy to be sure. Try: file = codecs.open(p + "2.txt", "r", "iso-8859-1") ChrisA From __peter__ at web.de Thu Dec 22 11:42:09 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 22 Dec 2011 17:42:09 +0100 Subject: Spanish Accents References: Message-ID: Stan Iverson wrote: > On Thu, Dec 22, 2011 at 11:30 AM, Rami Chowdhury > wrote: > >> Could you try using the 'open' function from the 'codecs' module? >> > > I believe this is what you meant: > > file = codecs.open(p + "2.txt", "r", "utf-8") > for line in file: > print line > > but got this error: > > *UnicodeDecodeError*: 'utf8' codec can't decode bytes in position 0-2: > invalid data > args = ('utf8', '\xe1 intentado para ellos bastante sabios para > discernir lo obvio. Tales perso', 0, 3, 'invalid data') > which is the letter ? (a with accent). The file is probably encoded in ISO-8859-1, ISO-8859-15, or cp1252 then: >>> print "\xe1".decode("iso-8859-1") ? >>> print "\xe1".decode("iso-8859-15") ? >>> print "\xe1".decode("cp1252") ? Try codecs.open() with one of these encodings. From iversonstan at gmail.com Thu Dec 22 11:54:50 2011 From: iversonstan at gmail.com (Stan Iverson) Date: Thu, 22 Dec 2011 12:54:50 -0400 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 12:42 PM, Peter Otten <__peter__ at web.de> wrote: > The file is probably encoded in ISO-8859-1, ISO-8859-15, or cp1252 then: > > >>> print "\xe1".decode("iso-8859-1") > ? > >>> print "\xe1".decode("iso-8859-15") > ? > >>> print "\xe1".decode("cp1252") > ? > > Try codecs.open() with one of these encodings. > I'm baffled. I duplicated your print statements but when I run this code (or any of the 3 encodings): file = codecs.open(p + "2.txt", "r", "cp1252") #file = codecs.open(p + "2.txt", "r", "utf-8") for line in file: print line I get this error: *UnicodeEncodeError*: 'ascii' codec can't encode character u'\xe1' in position 48: ordinal not in range(128) args = ('ascii', u'Noticia: Este sitio web entre este portal est...r\xe1pidamente va a salir de aqu\xed.

\r\n', 48, 49, 'ordinal not in range(128)') encoding = 'ascii' end = 49 object = u'Noticia: Este sitio web entre este portal est...r\xe1pidamente va a salir de aqu\xed.

\r\n' reason = 'ordinal not in range(128)' start = 48 Please advise. TIA, Stan -------------- next part -------------- An HTML attachment was scrubbed... URL: From robert.kern at gmail.com Thu Dec 22 12:01:14 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 22 Dec 2011 17:01:14 +0000 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: References: <4EF22CA1.1070200@gmail.com> <4EF233A7.6020907@stoneleaf.us> <4EF23D57.6010305@gmail.com> Message-ID: On 12/22/11 11:24 AM, Robert Kern wrote: > Just as a further note on these lines, when older versions of setuptools and > distribute install scripts, they generate stub scripts that do not use a > __main__ guard, so installing a multiprocessing-using application with > setuptools can cause this problem. I believe newer versions of distribute has > this problem fixed, but I'm not sure. Just checked. No, distribute has not fixed this bug. -- 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 From wolftracks at invalid.com Thu Dec 22 12:53:04 2011 From: wolftracks at invalid.com (W. eWatson) Date: Thu, 22 Dec 2011 09:53:04 -0800 Subject: Cannot Remove Python Libs from XP Message-ID: I have three py libs and the python program itself, 2.52, installed on an 6 year old HP Laptop. I decided to remove them, and took removed Python with the Control Panel ?Add/Remove icon. Worked fine. When I try to remove any of the remaining libs, press the add/remove button does nothing but blink. How do I get around this problem? From __peter__ at web.de Thu Dec 22 13:17:30 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 22 Dec 2011 19:17:30 +0100 Subject: Spanish Accents References: Message-ID: Stan Iverson wrote: > On Thu, Dec 22, 2011 at 12:42 PM, Peter Otten <__peter__ at web.de> wrote: > >> The file is probably encoded in ISO-8859-1, ISO-8859-15, or cp1252 then: >> >> >>> print "\xe1".decode("iso-8859-1") >> ? >> >>> print "\xe1".decode("iso-8859-15") >> ? >> >>> print "\xe1".decode("cp1252") >> ? >> >> Try codecs.open() with one of these encodings. >> > > I'm baffled. I duplicated your print statements but when I run this code > (or any of the 3 encodings): > > file = codecs.open(p + "2.txt", "r", "cp1252") > #file = codecs.open(p + "2.txt", "r", "utf-8") > for line in file: > print line > > I get this error: > > *UnicodeEncodeError*: 'ascii' codec can't encode character u'\xe1' in > position 48: ordinal not in range(128) You are now one step further, you have successfully* decoded the file. The remaining step is to encode the resulting unicode lines back into bytes. The encoding implicitly used by the print statement is sys.stdout.encoding which is either "ascii" or None in your case. Try to encode explicitly to UTF-8 with f = codecs.open(p + "2.txt", "r", "iso-8859-1") for line in f: print line.encode("utf-8") (*) This is however no big achievement as two (ISO-8859-1, ISO-8859-15) of the above codecs would not even balk on a binary file, e.g. a jpeg. They offer a character for every possible byte value. From kumar.mcmillan at gmail.com Thu Dec 22 13:24:13 2011 From: kumar.mcmillan at gmail.com (Kumar McMillan) Date: Thu, 22 Dec 2011 12:24:13 -0600 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: On Wed, Dec 21, 2011 at 1:15 AM, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > If you compile mod_wsgi with Apache you are stuck on the version of Python you compiled with. I had an old server stuck on Python 2.5 for this reason but I finally got a new box where I will be stuck on Python 2.7 for a while. There's probably a better way with gunicorn or something but Apache is pretty sweet when you configure it right. btw, tox is great for developing a project that supports multiple Pythons: http://tox.testrun.org/latest/ > > Do people still have to use this in commercial environments or is everyone > on 2.6+ nowadays? > > I'm finally getting some continuous integration set up for my packages and > it's highlighting some 2.5 compatibility issues. I'm wondering whether to > fix those (lots of ugly "from __future__ import with_statement" everywhere) > or just to drop Python 2.5 support. > > What do people feel? > > cheers, > > Chris > > -- > Simplistix - Content Management, Batch Processing & Python Consulting > - http://www.simplistix.co.uk > > ______________________________**_________________ > testing-in-python mailing list > testing-in-python at lists.idyll.**org > http://lists.idyll.org/**listinfo/testing-in-python > -------------- next part -------------- An HTML attachment was scrubbed... URL: From raymond.hettinger at gmail.com Thu Dec 22 15:01:25 2011 From: raymond.hettinger at gmail.com (Raymond Hettinger) Date: Thu, 22 Dec 2011 12:01:25 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <28c19da6-8ad8-42e9-bb67-964a39672c0b@s10g2000prs.googlegroups.com> On Dec 21, 9:57?am, Nathan Rice wrote: > +1 for IPython/%edit using the simplest editor that supports syntax > highlighting and line numbers. ?I have found that > Exploring/Prototyping in the interpreter has the highest ROI of > anything I teach people. Thank you Nathan and all the other respondents for your thoughtful answers. Raymond From bahamutzero8825 at gmail.com Thu Dec 22 15:27:31 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 22 Dec 2011 14:27:31 -0600 Subject: IPC with multiprocessing.connection Message-ID: <4EF392B3.2050005@gmail.com> I'm trying to set up a system where my main program launches external programs and then establishes connections to them via named pipes or Unix domain sockets (depending on platform) with multiprocessing.connection.Listener. The issue I'm having is with the accept() method. If there is nothing on the other side of the pipe/socket, it just hangs. Connection objects have a poll() method that can timeout, but I see no way to continue the program if there's no initial connection. What I'd like to do is let it time out if there's nothing on the other side. I'm pretty new to the multiprocessing module (and IPC in general), so I could've easily missed something. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From saqib.ali.75 at gmail.com Thu Dec 22 15:53:41 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Thu, 22 Dec 2011 12:53:41 -0800 (PST) Subject: Can't I define a decorator in a separate file and import it? Message-ID: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> I'm using this decorator to implement singleton class in python: http://stackoverflow.com/posts/7346105/revisions The strategy described above works if and only if the Singleton is declared and defined in the same file. If it is defined in a different file and I import that file, it doesn't work. Why can't I import this Singleton decorator from a different file? What's the best work around? From ethan at stoneleaf.us Thu Dec 22 16:07:34 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 22 Dec 2011 13:07:34 -0800 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> Message-ID: <4EF39C16.4090803@stoneleaf.us> Saqib Ali wrote: > > I'm using this decorator to implement singleton class in python: > > http://stackoverflow.com/posts/7346105/revisions > > The strategy described above works if and only if the Singleton is > declared and defined in the same file. If it is defined in a different > file and I import that file, it doesn't work. > > Why can't I import this Singleton decorator from a different file? > What's the best work around? Post the code, and the traceback. ~Ethan~ From saqib.ali.75 at gmail.com Thu Dec 22 16:09:20 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Thu, 22 Dec 2011 13:09:20 -0800 (PST) Subject: Can't I define a decorator in a separate file and import it? References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> Message-ID: <7fb2b7f5-12a2-4724-88e2-53991004d079@d8g2000vbb.googlegroups.com> BTW Here is the traceback: >>> import myClass Traceback (most recent call last): File "", line 1, in File "myClass.py", line 6, in @Singleton TypeError: 'module' object is not callable Here is Singleton.py: class Singleton: def __init__(self, decorated): self._decorated = decorated def Instance(self): try: return self._instance except AttributeError: self._instance = self._decorated() return self._instance def __call__(self): raise TypeError( 'Singletons must be accessed through the `Instance` method.') Here is myClass.py: #!/usr/bin/env python import os, sys, string, time, re, subprocess import Singleton @Singleton class myClass: def __init__(self): print 'Constructing myClass' def __del__(self): print 'Destructing myClass' From saqib.ali.75 at gmail.com Thu Dec 22 16:11:11 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Thu, 22 Dec 2011 16:11:11 -0500 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: <4EF39C16.4090803@stoneleaf.us> References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <4EF39C16.4090803@stoneleaf.us> Message-ID: MYCLASS.PY: #!/usr/bin/env python import os, sys, string, time, re, subprocess import Singleton @Singleton class myClass: def __init__(self): print 'Constructing myClass' def __del__(self): print 'Destructing myClass' SINGLETON.PY: #!/usr/bin/env python import os, sys, string, time, re, subprocess import Singleton @Singleton class myClass: def __init__(self): print 'Constructing myClass' def __del__(self): print 'Destructing myClass' TRACEBACK: >>> import myClass Traceback (most recent call last): File "", line 1, in File "myClass.py", line 6, in @Singleton TypeError: 'module' object is not callable - Saqib >> > Post the code, and the traceback. > > ~Ethan~ -------------- next part -------------- An HTML attachment was scrubbed... URL: From ckaynor at zindagigames.com Thu Dec 22 16:15:32 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Thu, 22 Dec 2011 13:15:32 -0800 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <4EF39C16.4090803@stoneleaf.us> Message-ID: On Thu, Dec 22, 2011 at 1:11 PM, Saqib Ali wrote: > MYCLASS.PY: > > #!/usr/bin/env python > import os, sys, string, time, re, subprocess > import Singleton > This imports the module Singleton, not the class or function. There are two options to deal with this: from Singleton import Singleton imports the name Singleton from the module Singleton, which will be what you are probably expecting. If you wish to keep the namespace, you can also change your call from @Singleton to @Singleton.Singleton > > @Singleton > class myClass: > > def __init__(self): > print 'Constructing myClass' > > def __del__(self): > print 'Destructing myClass' > > > SINGLETON.PY: > > > #!/usr/bin/env python > import os, sys, string, time, re, subprocess > import Singleton > > > @Singleton > class myClass: > > def __init__(self): > print 'Constructing myClass' > > def __del__(self): > print 'Destructing myClass' > > TRACEBACK: > > >>> import myClass > Traceback (most recent call last): > File "", line 1, in > File "myClass.py", line 6, in > @Singleton > TypeError: 'module' object is not callable > > > > - Saqib > > > > > >>> >> Post the code, and the traceback. >> >> ~Ethan~ > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Thu Dec 22 16:20:02 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 22 Dec 2011 15:20:02 -0600 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <4EF39C16.4090803@stoneleaf.us> Message-ID: <4EF39F02.8040201@gmail.com> You have the same code for both files... From joshua.landau.ws at gmail.com Thu Dec 22 16:22:53 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 22 Dec 2011 21:22:53 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 22 December 2011 11:15, Robert Kern wrote: > On 12/21/11 6:52 PM, Joshua Landau wrote: > > If fix means "return number" then no. This inconsistency between >> elementwise >> operations and 'normal' ones is the problem. Again, explicit >> elementwise-ifying >> through "~" fixes that. You need to tell me why this is worth the >> confusion over >> that. >> > > I understand that you are a supporter of PEP 225, but please have some > courtesy towards other developers. We can explore alternatives in parallel. > If we all worked on exactly the same thing (much less the one thing that > only you get to choose), we would forgo a lot of valuable experience. You > can point out the flaws of Elementwise's implementation and compare it to > the virtues of PEP 225 without being so demanding. He doesn't "need to > tell" you a damn thing. OK. Sorry. I had a feeling my language was a bit blunt. -------------- next part -------------- An HTML attachment was scrubbed... URL: From usenet at solar-empire.de Thu Dec 22 16:26:02 2011 From: usenet at solar-empire.de (Marc Christiansen) Date: Thu, 22 Dec 2011 22:26:02 +0100 Subject: Can't I define a decorator in a separate file and import it? References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> Message-ID: Saqib Ali wrote: > I'm using this decorator to implement singleton class in python: > > http://stackoverflow.com/posts/7346105/revisions > > The strategy described above works if and only if the Singleton is > declared and defined in the same file. If it is defined in a different > file and I import that file, it doesn't work. > > Why can't I import this Singleton decorator from a different file? Maybe you're doing something wrong. But since you didn't provide any code, it's impossible to tell what. > What's the best work around? As far as I can tell with the information you have given, none is needed. See below: 0:> python2.7 singleton_test.py # works also with python3.2 Foo created True #singleton_test.py from singleton import Singleton # Singleton is the class from Paul Manta from the SO page # and singleton.py contains nothing else @Singleton class Foo: def __init__(self): print('Foo created') f = Foo.Instance() g = Foo.Instance() print(f is g) Gr??e Marc From saqib.ali.75 at gmail.com Thu Dec 22 16:29:04 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Thu, 22 Dec 2011 16:29:04 -0500 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: <4EF3A1C9.705@stoneleaf.us> References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <4EF39C16.4090803@stoneleaf.us> <4EF3A1C9.705@stoneleaf.us> Message-ID: Thanks for pointing out the mistake! Works. - Saqib On Thu, Dec 22, 2011 at 4:31 PM, Ethan Furman wrote: > Saqib Ali wrote: > >> MYCLASS.PY: >> >> #!/usr/bin/env python >> import os, sys, string, time, re, subprocess >> import Singleton >> > > This should be 'from Singleton import Singleton' > > > > @Singleton >> class myClass: >> def __init__(self): >> print 'Constructing myClass' >> > > At this point, the *instance* of myClass has already been constructed and > it is now being initialized. The __new__ method is where the instance is > actually created. > > > >> def __del__(self): >> print 'Destructing myClass' >> >> >> > class Singleton: > > > > def __init__(self, decorated): > > self._decorated = decorated > > > > def Instance(self): > > try: > > return self._instance > > except AttributeError: > > self._instance = self._decorated() > > return self._instance > > > > def __call__(self): > > raise TypeError( > > 'Singletons must be accessed through the `Instance` > > method.') > > > ~Ethan~ > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Thu Dec 22 16:31:53 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 22 Dec 2011 13:31:53 -0800 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <4EF39C16.4090803@stoneleaf.us> Message-ID: <4EF3A1C9.705@stoneleaf.us> Saqib Ali wrote: > MYCLASS.PY: > > #!/usr/bin/env python > import os, sys, string, time, re, subprocess > import Singleton This should be 'from Singleton import Singleton' > @Singleton > class myClass: > > def __init__(self): > print 'Constructing myClass' At this point, the *instance* of myClass has already been constructed and it is now being initialized. The __new__ method is where the instance is actually created. > > def __del__(self): > print 'Destructing myClass' > > > class Singleton: > > def __init__(self, decorated): > self._decorated = decorated > > def Instance(self): > try: > return self._instance > except AttributeError: > self._instance = self._decorated() > return self._instance > > def __call__(self): > raise TypeError( > 'Singletons must be accessed through the `Instance` > method.') ~Ethan~ From ben+python at benfinney.id.au Thu Dec 22 17:33:22 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 23 Dec 2011 09:33:22 +1100 Subject: Can't I define a decorator in a separate file and import it? References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <7fb2b7f5-12a2-4724-88e2-53991004d079@d8g2000vbb.googlegroups.com> Message-ID: <87d3bgkzil.fsf@benfinney.id.au> Saqib Ali writes: > BTW Here is the traceback: > > >>> import myClass > Traceback (most recent call last): > File "", line 1, in > File "myClass.py", line 6, in > @Singleton > TypeError: 'module' object is not callable Yes. When you ?import foo?, you have a module bound to the name ?foo?. A module is not callable. > Here is Singleton.py: You should name the file ?singleton.py? instead; modules should be named in all lower-case, as PEP 8 recommends. > Here is myClass.py: Which should be named ?my_class.py?, instead, by the same rationale. Neither of those is necessary, but it will help dispel the confusion you're experiencing, and help your code be more easily comprehensible to other Python programmers. > #!/usr/bin/env python > import os, sys, string, time, re, subprocess > import Singleton So, you should (after the renames suggested) do either:: import singleton @singleton.Singleton class my_class: or:: from singleton import Singleton @Singleton class my_class: Notice how naming the module in lower-case makes it more easily distinguishable from the class name. More generally, remember that Python is not Java , and you should be grouping your classes into logically coherent files, not one-file-per-class. -- \ ?Reality must take precedence over public relations, for nature | `\ cannot be fooled.? ?Richard P. Feynman, _Rogers' Commission | _o__) Report into the Challenger Crash_, 1986-06 | Ben Finney From tjreedy at udel.edu Thu Dec 22 18:13:42 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 22 Dec 2011 18:13:42 -0500 Subject: Adding an interface to existing classes In-Reply-To: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> Message-ID: On 12/22/2011 3:21 AM, Spencer Pearson wrote: > I'm writing a geometry package, with Points and Lines and Circles and > so on, and eventually I want to be able to draw these things on the > screen. I have two options so far for how to accomplish this, but > neither of them sits quite right with me, and I'd like the opinion of > comp.lang.python's wizened elders. > > Option 1. Subclassing. > The most Pythonic way would seem to be writing subclasses for the > things I want to display, adding a ".draw(...)" method to each one, > like this: > Option 2. A "draw" function, with a function dictionary. Option 3? Add a draw method to existing classes, rather than subclassing? -- Terry Jan Reedy From iversonstan at gmail.com Thu Dec 22 18:14:56 2011 From: iversonstan at gmail.com (Stan Iverson) Date: Thu, 22 Dec 2011 19:14:56 -0400 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 2:17 PM, Peter Otten <__peter__ at web.de> wrote: > You are now one step further, you have successfully* decoded the file. > The remaining step is to encode the resulting unicode lines back into > bytes. > The encoding implicitly used by the print statement is sys.stdout.encoding > which is either "ascii" or None in your case. Try to encode explicitly to > UTF-8 with > > f = codecs.open(p + "2.txt", "r", "iso-8859-1") > for line in f: > print line.encode("utf-8") > > > OOEEE! Thanks! Stan -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Thu Dec 22 18:39:34 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 22 Dec 2011 18:39:34 -0500 Subject: Grammar for classes In-Reply-To: References: Message-ID: On 12/20/2011 12:05 PM, Joshua Landau wrote: > On 20 December 2011 10:55, Robert Kern > wrote: > > On 12/20/11 1:34 AM, Joshua Landau wrote: > > In reading thorough the syntax defined in the reference > >, > > the class statement has surprised me. > > It says that the inheritance part of the class can accept > comprehensions. What > does this mean? Beats me. The 2.7 doc says inheritance ::= "(" [expression_list] ")" and I no on no 3.x change/addition. My experiments with both genexp and listcomp versions gave errors. > I ignore valid code. Thanks Ian and Robert for the input. Should I file > a documentation bug report? Please do. It the addition means something, it needs to be explained in the text. If it is wrong, it should go. -- Terry Jan Reedy From macsmith.us at gmail.com Thu Dec 22 19:33:12 2011 From: macsmith.us at gmail.com (Mac Smith) Date: Fri, 23 Dec 2011 06:03:12 +0530 Subject: reading multiline output References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> Message-ID: Hi, I have started HandBrakeCLI using subprocess.popen but the output is multiline and not terminated with \n so i am not able to read it using readline() while the HandBrakeCLI is running. kindly suggest some alternative. i have attached the output in a file. -------------- next part -------------- A non-text attachment was scrubbed... Name: output Type: application/octet-stream Size: 4541 bytes Desc: not available URL: -------------- next part -------------- -- Thanks Mac From python at mrabarnett.plus.com Thu Dec 22 19:47:07 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 23 Dec 2011 00:47:07 +0000 Subject: reading multiline output In-Reply-To: References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> Message-ID: <4EF3CF8B.4060005@mrabarnett.plus.com> On 23/12/2011 00:33, Mac Smith wrote: > Hi, > > > I have started HandBrakeCLI using subprocess.popen but the output is > multiline and not terminated with \n so i am not able to read it > using readline() while the HandBrakeCLI is running. kindly suggest > some alternative. i have attached the output in a file. > The lines are terminated with \r, so read with read() and then split on "\r". From macsmith.us at gmail.com Thu Dec 22 20:07:12 2011 From: macsmith.us at gmail.com (Mac Smith) Date: Fri, 23 Dec 2011 06:37:12 +0530 Subject: reading multiline output In-Reply-To: <4EF3CF8B.4060005@mrabarnett.plus.com> References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> Message-ID: On 23-Dec-2011, at 6:17 AM, MRAB wrote: > On 23/12/2011 00:33, Mac Smith wrote: >> Hi, >> >> >> I have started HandBrakeCLI using subprocess.popen but the output is >> multiline and not terminated with \n so i am not able to read it >> using readline() while the HandBrakeCLI is running. kindly suggest >> some alternative. i have attached the output in a file. >> > The lines are terminated with \r, so read with read() and then split on > "\r". read() will read the complete output and than i will be able to parse it, i want to read the output of the command in realtime. From python at mrabarnett.plus.com Thu Dec 22 20:18:48 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 23 Dec 2011 01:18:48 +0000 Subject: reading multiline output In-Reply-To: References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> Message-ID: <4EF3D6F8.3020203@mrabarnett.plus.com> On 23/12/2011 01:07, Mac Smith wrote: > > On 23-Dec-2011, at 6:17 AM, MRAB wrote: > >> On 23/12/2011 00:33, Mac Smith wrote: >>> Hi, >>> >>> >>> I have started HandBrakeCLI using subprocess.popen but the output >>> is multiline and not terminated with \n so i am not able to read >>> it using readline() while the HandBrakeCLI is running. kindly >>> suggest some alternative. i have attached the output in a file. >>> >> The lines are terminated with \r, so read with read() and then >> split on "\r". > > read() will read the complete output and than i will be able to parse > it, i want to read the output of the command in realtime. > Try telling it how much to read with read(size): def read_lines(output, line_ending="\n"): buffer = "" while True: chunk = output.read(1024) if not chunk: break buffer += chunk while True: pos = buffer.find(line_ending) if pos < 0: break pos += len(line_ending) yield buffer[ : pos] buffer = buffer[pos : ] if buffer: yield buffer From cs at zip.com.au Thu Dec 22 20:21:40 2011 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 23 Dec 2011 12:21:40 +1100 Subject: reading multiline output In-Reply-To: References: Message-ID: <20111223012140.GA29153@cskk.homeip.net> On 23Dec2011 06:37, Mac Smith wrote: | On 23-Dec-2011, at 6:17 AM, MRAB wrote: | > On 23/12/2011 00:33, Mac Smith wrote: | >> I have started HandBrakeCLI using subprocess.popen but the output is | >> multiline and not terminated with \n so i am not able to read it | >> using readline() while the HandBrakeCLI is running. kindly suggest | >> some alternative. i have attached the output in a file. | >> | > The lines are terminated with \r, so read with read() and then split on | > "\r". | | read() will read the complete output and than i will be able to parse | it, i want to read the output of the command in realtime. You can read a limited number of bytes - for your purposes this needs to be 1 byte at a time. Very inefficient, but you can do what you need. Write a little function that gathers single bytes until "\r" and then returns them joined into a string. Traditionally the way to work with "interactive" facilities like this is often the "expect" command, and I gather there's a third party Python module named "pexpect" with that functionality. This: http://www.noah.org/wiki/pexpect And then there's all sorts of read-with-timeout games you can play I guess... Note that all this relies on HandBrakeCLI emitting its progress messages in a timely (== unbuffered) manner when its output is a pipe... Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ It's better to be DoD and cool, than to get a life and be uncool! - _Harley Davidson & The Marlboro Man_ (slightly paraphrased by Roar Larsen, DoD#463 ) From ian.g.kelly at gmail.com Thu Dec 22 20:27:54 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 22 Dec 2011 18:27:54 -0700 Subject: Grammar for classes In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 4:39 PM, Terry Reedy wrote: > Beats me. The 2.7 doc says inheritance ::= ?"(" [expression_list] ")" and I > no on no 3.x change/addition. Well, there is one change in 3.x which is that the inheritance list now accepts keyword parameters (PEP 3115) -- the "metaclass" keyword specifies the metaclass instead of using a bogus __metaclass__ attribute, and any other keywords are passed along to the metaclass. That's why the parsing of the inheritance list was generalized to look more like a function call, but it doesn't explain why the class declaration might wish to receive a comprehension. Cheers, Ian From macsmith.us at gmail.com Thu Dec 22 21:05:23 2011 From: macsmith.us at gmail.com (Mac Smith) Date: Fri, 23 Dec 2011 07:35:23 +0530 Subject: reading multiline output In-Reply-To: <4EF3D6F8.3020203@mrabarnett.plus.com> References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> <4EF3D6F8.3020203@mrabarnett.plus.com> Message-ID: <2D5264F3-A1EB-4C68-85E2-A39C5C67E699@gmail.com> On 23-Dec-2011, at 6:48 AM, MRAB wrote: > On 23/12/2011 01:07, Mac Smith wrote: >> >> On 23-Dec-2011, at 6:17 AM, MRAB wrote: >> >>> On 23/12/2011 00:33, Mac Smith wrote: >>>> Hi, >>>> >>>> >>>> I have started HandBrakeCLI using subprocess.popen but the output >>>> is multiline and not terminated with \n so i am not able to read >>>> it using readline() while the HandBrakeCLI is running. kindly >>>> suggest some alternative. i have attached the output in a file. >>>> >>> The lines are terminated with \r, so read with read() and then >>> split on "\r". >> >> read() will read the complete output and than i will be able to parse >> it, i want to read the output of the command in realtime. >> > Try telling it how much to read with read(size): > > def read_lines(output, line_ending="\n"): > buffer = "" > > while True: > chunk = output.read(1024) > if not chunk: > break > > buffer += chunk > > while True: > pos = buffer.find(line_ending) > if pos < 0: > break > > pos += len(line_ending) > yield buffer[ : pos] > buffer = buffer[pos : ] > > if buffer: > yield buffer > -- > http://mail.python.org/mailman/listinfo/python-list thanks, this helped. just need to correct line_ending="\n" should be line_ending="\r" From wuwei23 at gmail.com Thu Dec 22 21:10:46 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 22 Dec 2011 18:10:46 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: On Dec 22, 6:51?pm, Rolf Camps wrote: > I'm afraid it's dangerous to encourage the use of '[]' as assignment to > a parameter in a function definition. If you use the function several > times 'default' always points to the same list. I appreciate the concern, but adding a default argument guard would not only obscure the code. It's irrelevant, as you recognise, because no matter what, it's going to make copies of the default argument. You know what the say about foolish consistencies :) From python at mrabarnett.plus.com Thu Dec 22 21:14:36 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 23 Dec 2011 02:14:36 +0000 Subject: reading multiline output In-Reply-To: <2D5264F3-A1EB-4C68-85E2-A39C5C67E699@gmail.com> References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> <4EF3D6F8.3020203@mrabarnett.plus.com> <2D5264F3-A1EB-4C68-85E2-A39C5C67E699@gmail.com> Message-ID: <4EF3E40C.6060705@mrabarnett.plus.com> On 23/12/2011 02:05, Mac Smith wrote: > > On 23-Dec-2011, at 6:48 AM, MRAB wrote: > >> On 23/12/2011 01:07, Mac Smith wrote: >>> >>> On 23-Dec-2011, at 6:17 AM, MRAB wrote: >>> >>>> On 23/12/2011 00:33, Mac Smith wrote: >>>>> Hi, >>>>> >>>>> >>>>> I have started HandBrakeCLI using subprocess.popen but the output >>>>> is multiline and not terminated with \n so i am not able to read >>>>> it using readline() while the HandBrakeCLI is running. kindly >>>>> suggest some alternative. i have attached the output in a file. >>>>> >>>> The lines are terminated with \r, so read with read() and then >>>> split on "\r". >>> >>> read() will read the complete output and than i will be able to parse >>> it, i want to read the output of the command in realtime. >>> >> Try telling it how much to read with read(size): >> >> def read_lines(output, line_ending="\n"): >> buffer = "" >> >> while True: >> chunk = output.read(1024) >> if not chunk: >> break >> >> buffer += chunk >> >> while True: >> pos = buffer.find(line_ending) >> if pos< 0: >> break >> >> pos += len(line_ending) >> yield buffer[ : pos] >> buffer = buffer[pos : ] >> >> if buffer: >> yield buffer > > thanks, this helped. just need to correct line_ending="\n" should be line_ending="\r" > I wrote the default as "\n" because that's the normal line ending in Python. If, as in your case, the line ending is different, just pass the appropriate string as the second argument. From ian.g.kelly at gmail.com Thu Dec 22 21:59:06 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 22 Dec 2011 19:59:06 -0700 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: On Thu, Dec 22, 2011 at 7:10 PM, alex23 wrote: > On Dec 22, 6:51?pm, Rolf Camps wrote: >> I'm afraid it's dangerous to encourage the use of '[]' as assignment to >> a parameter in a function definition. If you use the function several >> times 'default' always points to the same list. > > I appreciate the concern, but adding a default argument guard would > not only obscure the code. It's irrelevant, as you recognise, because > no matter what, it's going to make copies of the default argument. It's only irrelevant in the immediate context of the code you posted. But when Joe Novice sees your code and likes it and duplicates it a million times without receiving any warning about it, he's eventually going to write a function that modifies its default list argument, and he'll be in for a nasty surprise when he does. From rustompmody at gmail.com Thu Dec 22 22:05:54 2011 From: rustompmody at gmail.com (rusi) Date: Thu, 22 Dec 2011 19:05:54 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <643c3931-db20-44ce-80b4-5f4677cc8da3@18g2000prn.googlegroups.com> On Dec 21, 9:57?pm, Nathan Rice wrote: > +1 for IPython/%edit using the simplest editor that supports syntax > highlighting and line numbers. ?I have found that > Exploring/Prototyping in the interpreter has the highest ROI of > anything I teach people. > > Nathan It seems to me that we are not distinguishing different 'scales'. For example: In principle one can use any unit to measure length. In practice it is inconvenient to use kilometers when microns or angstroms are more appropriate. Clearly there is a need, when learning a language, to explore the basics with a minimum of interference from the environment ie introspection features of python with as little extra interference as possible. As a teacher I generally demonstrate with python-mode in emacs. I guess IDLE, ipython etc should be equivalent. But then someone in the class comes with a 'question' of this sort: We have this project in eclipse+python. But the path is not being searched properly. etc Now for me eclipse is more of pizazz on the outside and nightmare on the inside. [Or maybe its just that I am particularly bad at solving these kinds of problems]. Nevertheless this scale or granularity of work is also important in python education and is sorely underrepresented. Here are some of my earlier attempts to canvass for this orientation: http://mail.python.org/pipermail/python-list/2011-May/1271749.html http://mail.python.org/pipermail/python-list/2011-November/1283634.html From wuwei23 at gmail.com Thu Dec 22 22:40:11 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 22 Dec 2011 19:40:11 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: On Dec 23, 12:59?pm, Ian Kelly wrote: > It's only irrelevant in the immediate context of the code you posted. > But when Joe Novice sees your code and likes it and duplicates it a > million times I'm sorry, but I'm not going to modify my coding style for the sake of bad programmers. The context is _important_. Why should I guard against default argument mutability when its not going to occur in the function body? From einazaki668 at yahoo.com Thu Dec 22 22:46:09 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 22 Dec 2011 19:46:09 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: <1ec8f662-f4ef-4f99-b476-5e5ae2a3b412@p41g2000yqm.googlegroups.com> On Dec 21, 6:50?pm, alex23 wrote: > On Dec 22, 8:25?am, Eric wrote: > > > This surprises me, can someone tell me why it shouldn't? ?I figure if > > I want to create and initialize three scalars the just do "a=b=c=7", > > for example, so why not extend it to arrays. > > The thing to remember is that everything is an object, and that it's > better to think of variables as labels on an object. > > So: a=b=c=7 means that _one_ integer object with the value of 7 can be > referenced using any of the labels a, b or c. x=y=z=[] means that > _one_ empty list can be referenced using x, y or z. > > The difference is that the value of a number object _cannot be > changed_ ('immutable') while a list can be modified to add or remove > items ('mutable'). a=10 just reassigns the label a to an integer > object of value 10. x.append("foo") _modifies_ the list referred to by > x, which is the same list known as y & z. > > > Also, is there a more pythonic way to do "x=[], y=[], z=[]"? > > I'd say that _is_ the most pythonic way, it's very obvious in its > intent (or would be with appropriate names). If it bothers you that > much: > Thanks for the explanation. I guess from what I've seen of Python so far I was expecting something more, I don't know, compact. Anyway, it doesn't bother me, at least not enough to go and do something like this: > ? ? def listgen(count, default=[]): > ? ? ? ? for _ in xrange(count): > ? ? ? ? ? ? yield default[:] > > ? ? x, y, z = listgen(3) Thanks, eric From python.list at tim.thechases.com Thu Dec 22 23:16:30 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 22 Dec 2011 22:16:30 -0600 Subject: Idiom for shelling out to $EDITOR/$PAGER? Message-ID: <4EF4009E.8010109@tim.thechases.com> After a little searching, I've not been able to come up with what I'd consider canonical examples of consider calling an external editor/pager on a file and reading the results back in. (most of my results are swamped by people asking about editors written in Python, or what the best editors for Python code are) The pseudocode would be something like def edit_text(data): temp_fname = generate_temp_name() try: f = file(temp_fname, 'w') f.write(data) f.close() before = info(temp_fname) # maybe stat+checksum? editor = find_sensible_editor() subprocess.call([editor, temp_fname]) if before == info(temp_fname): return None else: return file(temp_fname).read() finally: delete_if_exists(temp_fname) However there are things to watch out for in this lousy code: -race conditions, unique naming, and permissions on the temp file -proper & efficient detection of file-change, to know whether the user actually did anything -cross-platform determination of a sensible editor (that blocks rather than spawns), using platform conventions like os.environ['EDITOR'] -cleanup deletion of the temp-file I presume the code for spawning $PAGER on some content would look pretty similar. Any good example code (or blog posts, or other links) that has been battle-tested? Thanks, -tkc From rosuav at gmail.com Thu Dec 22 23:25:51 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 15:25:51 +1100 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: On Fri, Dec 23, 2011 at 2:40 PM, alex23 wrote: > I'm sorry, but I'm not going to modify my coding style for the sake of > bad programmers. And there, folks, you have one of the eternal dilemmas. The correct decision depends on myriad factors; if you're writing code to go into the documentation as an example, you want it to be able to handle idiots hacking on it - but on the other hand, that same situation demands simplicity, which is why a lot of examples omit huge slabs of error checking. ChrisA From einazaki668 at yahoo.com Thu Dec 22 23:27:57 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 22 Dec 2011 20:27:57 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <4ef26f74$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <845617ed-2e20-410f-90c7-ddafac2867bf@v13g2000yqc.googlegroups.com> On Dec 21, 5:44?pm, Steven D'Aprano wrote: > Yes, you should create your lists before trying to append to them. > > But you aren't forced to use a for-loop. You can use a list comprehension: > > x = [some_function(a) for a in range(n)] > > Notice that here you don't need x to pre-exist, because the list comp > creates a brand new list, which then gets assigned directly to x. > > > Now to my actual question. ?I need to do the above for multiple arrays > > (all the same, arbitrary size). ?So I do this: > > ? ?x=y=z=[] > > This creates one empty list object, and gives it three names, x, y and z. > Every time you append to the list, all three names see the same change, > because they refer to a single list. > > [...] > > > Except it seems that I didn't create three different arrays, I created > > one array that goes by three different names (i.e. x[], y[] and z[] all > > reference the same pile of numbers, no idea which pile). > > Exactly. > > > This surprises me, can someone tell me why it shouldn't? > > Because that's the way Python works. Python is an object-oriented, name > binding language. This is how OO name binding works: you have a single > object, with three names bound to it. The above line is short-cut for: > > a = [] > b = a > c = a > > Python does not make a copy of the list unless you specifically instruct > it to. > > > I figure if I > > want to create and initialize three scalars the just do "a=b=c=7", > > That creates a single integer object with value 7, and binds three names > to it, *exactly* the same as the above. > > If you could modify int objects in place, like you can modify lists in > place, you would see precisely the same effect. But ints are immutable: > all operations on ints create new ints. Lists are mutable, and can be > changed in place. > > > for > > example, so why not extend it to arrays. ?Also, is there a more pythonic > > way to do "x=[], y=[], z=[]"? > > Well that literally won't work, you can't separate them by commas. > Newlines or semicolons will work. > > Or: x, y, z = [], [], [] > > Either is pretty Pythonic. > > -- > Steven Thanks to you and Dennis for the quick lesson and tips. Very helpful and illuminating. From ian.g.kelly at gmail.com Fri Dec 23 00:22:49 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 22 Dec 2011 22:22:49 -0700 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: On Thu, Dec 22, 2011 at 8:40 PM, alex23 wrote: > On Dec 23, 12:59?pm, Ian Kelly wrote: >> It's only irrelevant in the immediate context of the code you posted. >> But when Joe Novice sees your code and likes it and duplicates it a >> million times > > I'm sorry, but I'm not going to modify my coding style for the sake of > bad programmers. Nobody is asking you to modify your coding style. The request is that you not throw it up as an example without mentioning the important caveats. Also, novice programmer == bad programmer? From wuwei23 at gmail.com Fri Dec 23 01:00:51 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 22 Dec 2011 22:00:51 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: <7d519515-e278-4159-a35e-36bcafa81c73@c42g2000prb.googlegroups.com> On Dec 23, 3:22?pm, Ian Kelly wrote: > Nobody is asking you to modify your coding style. ?The request is that > you not throw it up as an example without mentioning the important > caveats. No, 100% no. It's not my responsibility to mention every potentially relevant gotcha when providing example code. > Also, novice programmer == bad programmer? If they're wholly learning how to code by throwaway examples on mailing lists, then yes. Object mutability is a _major_ aspect of Python; I'm simply not going to inject an essay explaining what that implies every time I choose to use a mutable default argument. From cs at zip.com.au Fri Dec 23 01:02:18 2011 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 23 Dec 2011 17:02:18 +1100 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: <4EF4009E.8010109@tim.thechases.com> References: <4EF4009E.8010109@tim.thechases.com> Message-ID: <20111223060218.GA28472@cskk.homeip.net> On 22Dec2011 22:16, Tim Chase wrote: | After a little searching, I've not been able to come up with what | I'd consider canonical examples of consider calling an external | editor/pager on a file and reading the results back in. (most of my | results are swamped by people asking about editors written in | Python, or what the best editors for Python code are) | | The pseudocode would be something like | | def edit_text(data): | temp_fname = generate_temp_name() | try: | f = file(temp_fname, 'w') | f.write(data) | f.close() | before = info(temp_fname) # maybe stat+checksum? | editor = find_sensible_editor() | subprocess.call([editor, temp_fname]) | if before == info(temp_fname): | return None | else: | return file(temp_fname).read() | finally: | delete_if_exists(temp_fname) | | However there are things to watch out for in this lousy code: | | -race conditions, unique naming, and permissions on the temp file NamedTemporaryFile is your friend. | -proper & efficient detection of file-change, to know whether the | user actually did anything Wait for the editor to exit? In that scenario I go for: - wait for edit to exit - if exit status 0 and file non-empty, trust it (subject to parse issues afterwards of course) | -cross-platform determination of a sensible editor (that blocks | rather than spawns), using platform conventions like | os.environ['EDITOR'] os.environment.get('EDITOR', 'vi')? Some platforms have an executable called "editor" that solves that problem (absent $EDITOR); MacOSX has "open", though it won't be running a blocking editor, alas. You may need some knowledge of the local system. On a terminal? Easy, use $EDITOR. No terminal, but $DISPLAY? xterm -e "$EDITOR temp_file", or the like. No terminal, no $DISPLAY, macosx? I have an incantation somewhere... | -cleanup deletion of the temp-file NamedTemporaryFile is your friend. | I presume the code for spawning $PAGER on some content would look | pretty similar. Yep. | Any good example code (or blog posts, or other links) that has been | battle-tested? Hmm. Nothing directly to had, but regarding the temp file: # write contents of the file `fp` into a temp file with NamedTemporaryFile('w', dir=os.path.join(self.dir, 'tmp')) as T: T.write(fp.read()) ... do stuff with T.name (the temp file name). Anyway, look it up; it has an autodelete mode etc. Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ I have always been a welly man myself. They are superb in wet grass, let alone lagoons of pig shit. - Julian Macassey From ben+python at benfinney.id.au Fri Dec 23 01:12:20 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 23 Dec 2011 17:12:20 +1100 Subject: Idiom for shelling out to $EDITOR/$PAGER? References: <4EF4009E.8010109@tim.thechases.com> Message-ID: <877h1nn7ej.fsf@benfinney.id.au> Cameron Simpson writes: > On 22Dec2011 22:16, Tim Chase wrote: > | -proper & efficient detection of file-change, to know whether the > | user actually did anything > > Wait for the editor to exit? > In that scenario I go for: > - wait for edit to exit > - if exit status 0 and file non-empty, trust it > (subject to parse issues afterwards of course) That doesn't address the concern Tim raised: did the user actually do anything, did the file change? The exit status of text editors are not bound to distinguish ?buffer was modified?, and certainly don't do so in any standard way. My advice: * Compute a before-edit hash of the text (MD5 or SHA-1 would be fine). * Invoke the editor on that text. * Wait for (or detect) the exit of the editor process. * Compute an after-edit hash of the text resulting from the editor. * Compare the hashes to see whether the text changed. -- \ ?Come on Milhouse, there's no such thing as a soul! It's just | `\ something they made up to scare kids, like the Boogie Man or | _o__) Michael Jackson.? ?Bart, _The Simpsons_ | Ben Finney From cs at zip.com.au Fri Dec 23 01:32:16 2011 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 23 Dec 2011 17:32:16 +1100 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: <877h1nn7ej.fsf@benfinney.id.au> References: <877h1nn7ej.fsf@benfinney.id.au> Message-ID: <20111223063216.GA32188@cskk.homeip.net> On 23Dec2011 17:12, Ben Finney wrote: | Cameron Simpson writes: | > On 22Dec2011 22:16, Tim Chase wrote: | > | -proper & efficient detection of file-change, to know whether the | > | user actually did anything | > | > Wait for the editor to exit? | > In that scenario I go for: | > - wait for edit to exit | > - if exit status 0 and file non-empty, trust it | > (subject to parse issues afterwards of course) | | That doesn't address the concern Tim raised: did the user actually do | anything, did the file change? I'm not sure it matters. It's _quicker_ to do nothing if the file is unchanged, but is it bad to act on it anyway? | The exit status of text editors are not bound to distinguish ?buffer was | modified?, and certainly don't do so in any standard way. Indeed not; my 0 above is just "did you user exit the editor or did it crash"? | My advice: | * Compute a before-edit hash of the text (MD5 or SHA-1 would be fine). | * Invoke the editor on that text. | * Wait for (or detect) the exit of the editor process. | * Compute an after-edit hash of the text resulting from the editor. | * Compare the hashes to see whether the text changed. Sure. But still, if you're able to act on a changed file, why not also act on an unchanged file? The user asked to change things; take what you got back and proceed! And personally, as a user, I've often rewritten an unchanged file to force a recompute of something. Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ "Don't you know the speed limit is 55 miles per hour???" "Yeah, but I wasn't going to be out that long." - Steven Wright From jeanpierreda at gmail.com Fri Dec 23 02:21:01 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 23 Dec 2011 02:21:01 -0500 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: <20111223060218.GA28472@cskk.homeip.net> References: <4EF4009E.8010109@tim.thechases.com> <20111223060218.GA28472@cskk.homeip.net> Message-ID: > Anyway, look it up; it has an autodelete mode etc. The autodelete isn't useful in this context. The file needs to be closed and then reopened again; with the autodelete option, the closing would delete the file, preventing it from being opened by the text editor or reopened to check contents. (As far as I know, the race condition inherent in this is unavoidable. At least on windows, but I couldn't figure out any other way on Linux either.) -- Devin On Fri, Dec 23, 2011 at 1:02 AM, Cameron Simpson wrote: > On 22Dec2011 22:16, Tim Chase wrote: > | After a little searching, I've not been able to come up with what > | I'd consider canonical examples of consider calling an external > | editor/pager on a file and reading the results back in. ?(most of my > | results are swamped by people asking about editors written in > | Python, or what the best editors for Python code are) > | > | The pseudocode would be something like > | > | ? def edit_text(data): > | ? ? temp_fname = generate_temp_name() > | ? ? try: > | ? ? ? f = file(temp_fname, 'w') > | ? ? ? f.write(data) > | ? ? ? f.close() > | ? ? ? before = info(temp_fname) # maybe stat+checksum? > | ? ? ? editor = find_sensible_editor() > | ? ? ? subprocess.call([editor, temp_fname]) > | ? ? ? if before == info(temp_fname): > | ? ? ? ? return None > | ? ? ? else: > | ? ? ? ? return file(temp_fname).read() > | ? ? finally: > | ? ? ? delete_if_exists(temp_fname) > | > | However there are things to watch out for in this lousy code: > | > | -race conditions, unique naming, and permissions on the temp file > > NamedTemporaryFile is your friend. > > | -proper & efficient detection of file-change, to know whether the > | user actually did anything > > Wait for the editor to exit? > In that scenario I go for: > ?- wait for edit to exit > ?- if exit status 0 and file non-empty, trust it > ? ?(subject to parse issues afterwards of course) > > | -cross-platform determination of a sensible editor (that blocks > | rather than spawns), using platform conventions like > | os.environ['EDITOR'] > > os.environment.get('EDITOR', 'vi')? > > Some platforms have an executable called "editor" that solves that > problem (absent $EDITOR); MacOSX has "open", though it won't be running > a blocking editor, alas. > > You may need some knowledge of the local system. On a terminal? Easy, > use $EDITOR. No terminal, but $DISPLAY? xterm -e "$EDITOR temp_file", > or the like. No terminal, no $DISPLAY, macosx? I have an incantation > somewhere... > > | -cleanup deletion of the temp-file > > NamedTemporaryFile is your friend. > > | I presume the code for spawning $PAGER on some content would look > | pretty similar. > > Yep. > > | Any good example code (or blog posts, or other links) that has been > | battle-tested? > > Hmm. Nothing directly to had, but regarding the temp file: > > ?# write contents of the file `fp` into a temp file > ?with NamedTemporaryFile('w', dir=os.path.join(self.dir, 'tmp')) as T: > ? ?T.write(fp.read()) > ?... do stuff with T.name (the temp file name). > > Anyway, look it up; it has an autodelete mode etc. > > Cheers, > -- > Cameron Simpson DoD#743 > http://www.cskk.ezoshosting.com/cs/ > > I have always been a welly man myself. They are superb in wet grass, let > alone lagoons of pig shit. ? ? ?- Julian Macassey > -- > http://mail.python.org/mailman/listinfo/python-list From yasar11732 at gmail.com Fri Dec 23 02:34:39 2011 From: yasar11732 at gmail.com (=?ISO-8859-9?Q?Ya=FEar_Arabac=FD?=) Date: Fri, 23 Dec 2011 09:34:39 +0200 Subject: Recommend blogs for me to follow Message-ID: Hi, I am looking for some recommendations about blogs that I may like to follow. I am interested in Python (that goes without saying), django and server side web development in general, linux tools, and git scm. I am looking for blogs which shares short examples of codes, and hints about various topics that I might be interested with. My favorite blog so far is http://www.saltycrane.com/blog/ as it gives quick tips aboug various topics that I am interested in. So can you suggest other blogs like that? I am considering this thread to become a reference point for people like me who wants to extent their knowledge about Python and world of programming in general by reading fellow programmers. -- http://yasar.serveblog.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri Dec 23 03:28:52 2011 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 Dec 2011 09:28:52 +0100 Subject: Idiom for shelling out to $EDITOR/$PAGER? References: <4EF4009E.8010109@tim.thechases.com> Message-ID: Tim Chase wrote: > After a little searching, I've not been able to come up with what > I'd consider canonical examples of consider calling an external > editor/pager on a file and reading the results back in. (most of > my results are swamped by people asking about editors written in > Python, or what the best editors for Python code are) > > The pseudocode would be something like > > def edit_text(data): > temp_fname = generate_temp_name() > try: > f = file(temp_fname, 'w') > f.write(data) > f.close() > before = info(temp_fname) # maybe stat+checksum? > editor = find_sensible_editor() > subprocess.call([editor, temp_fname]) > if before == info(temp_fname): > return None > else: > return file(temp_fname).read() > finally: > delete_if_exists(temp_fname) > > However there are things to watch out for in this lousy code: > > -race conditions, unique naming, and permissions on the temp file > > -proper & efficient detection of file-change, to know whether the > user actually did anything Just read the whole thing back into memory and compare the string to the original data. The file has to be quite long for the checksum calculation to be worthwhile. > -cross-platform determination of a sensible editor (that blocks > rather than spawns), using platform conventions like > os.environ['EDITOR'] > > -cleanup deletion of the temp-file > > I presume the code for spawning $PAGER on some content would look > pretty similar. > > Any good example code (or blog posts, or other links) that has > been battle-tested? You could look into mercurial to see what it does to let you edit its commit messages. A quick look into mercurial/ui.py (http://selenic.com/hg/file/9cf1620e1e75/mercurial/ui.py, start with the ui.edit() method) suggests that it uses the same approach as your pseudocode. From ethan at stoneleaf.us Fri Dec 23 03:38:04 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 23 Dec 2011 00:38:04 -0800 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: <4EF43DEC.6040100@stoneleaf.us> Ian Kelly wrote: > On Thu, Dec 22, 2011 at 7:10 PM, alex23 wrote: >> On Dec 22, 6:51 pm, Rolf Camps wrote: >>> I'm afraid it's dangerous to encourage the use of '[]' as assignment to >>> a parameter in a function definition. If you use the function several >>> times 'default' always points to the same list. >> >> I appreciate the concern, but adding a default argument guard would >> not only obscure the code. It's irrelevant, as you recognise, because >> no matter what, it's going to make copies of the default argument. > > It's only irrelevant in the immediate context of the code you posted. > But when Joe Novice sees your code and likes it and duplicates it a > million times without receiving any warning about it, he's eventually > going to write a function that modifies its default list argument, and > he'll be in for a nasty surprise when he does. And then he will learn about it and not make the mistake again (or if he does, it take much less time to figure it out). ~Ethan~ From rustompmody at gmail.com Fri Dec 23 03:38:07 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 00:38:07 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> On Dec 23, 7:10?am, alex23 wrote: > On Dec 22, 6:51?pm, Rolf Camps wrote: > > > I'm afraid it's dangerous to encourage the use of '[]' as assignment to > > a parameter in a function definition. If you use the function several > > times 'default' always points to the same list. > > I appreciate the concern, but adding a default argument guard would > not only obscure the code. It's irrelevant, as you recognise, because > no matter what, it's going to make copies of the default argument. > > You know what the say about foolish consistencies :) Programming languages can have bugs as much as programs can. A classic example is the precedence table of C which Kernighan or Ritchie (dont remember which) admitted was wrong. Likewise function arguments that default to mutable entities is a known gotcha of python which is best treated as a bug in python. It should be avoided with the suitable additional circumspection that a language bug deserves over a program bug. [Just my rephrasing of what Ian is saying] From bahamutzero8825 at gmail.com Fri Dec 23 03:46:51 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 23 Dec 2011 02:46:51 -0600 Subject: IPC with multiprocessing.connection In-Reply-To: References: <4EF392B3.2050005@gmail.com> Message-ID: <4EF43FFB.8060609@gmail.com> On 12/23/2011 2:32 AM, Deepak Babu wrote: > See if http://www.rsdcbabu.com/2011/02/multiprocessing-with-python.htmlhelps > you. It doesn't. There is not even a mention of the connection submodule (plus it's Python 2, and I'm familiar enough with Python 2 to do a good job of porting to py3k). I don't even want to spawn new processes with the multiprocessing module; I want processes which may not even be written in Python to communicate over a named pipe or Unix domain socket. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From ethan at stoneleaf.us Fri Dec 23 03:49:45 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 23 Dec 2011 00:49:45 -0800 Subject: what does 'a=b=c=[]' do In-Reply-To: <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> Message-ID: <4EF440A9.7030101@stoneleaf.us> rusi wrote: > On Dec 23, 7:10 am, alex23 wrote: >> On Dec 22, 6:51 pm, Rolf Camps wrote: >> >>> I'm afraid it's dangerous to encourage the use of '[]' as assignment to >>> a parameter in a function definition. If you use the function several >>> times 'default' always points to the same list. >> >> I appreciate the concern, but adding a default argument guard would >> not only obscure the code. It's irrelevant, as you recognise, because >> no matter what, it's going to make copies of the default argument. >> >> You know what the say about foolish consistencies :) > > Programming languages can have bugs as much as programs can. > A classic example is the precedence table of C which Kernighan or > Ritchie (dont remember which) admitted was wrong. > > Likewise function arguments that default to mutable entities is a > known gotcha of python which is best treated as a bug in python. It > should be avoided with the suitable additional circumspection that a > language bug deserves over a program bug. That is the most ridiculous thing I have heard in a while. Mutable default arguments are *not* a bug in Python. Reminds me of a bug report a couple years back claiming multiple inheritence was a bug and asking it to be removed. ~Ethan~ From steve+comp.lang.python at pearwood.info Fri Dec 23 04:39:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 09:39:59 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> Message-ID: <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: > Likewise function arguments that default to mutable entities is a known > gotcha of python which is best treated as a bug in python. Nonsense. It is a feature, not a bug. Some people might argue that it is a mistake, a minor feature which allegedly causes more difficulties than benefits. I do not hold with that idea. But either way, it is not a bug to be fixed, but a deliberate consequence of intended semantics. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 23 04:44:31 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 09:44:31 GMT Subject: Idiom for shelling out to $EDITOR/$PAGER? References: <4EF4009E.8010109@tim.thechases.com> Message-ID: <4ef44d7f$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 09:28:52 +0100, Peter Otten wrote: >> -proper & efficient detection of file-change, to know whether the user >> actually did anything > > Just read the whole thing back into memory and compare the string to the > original data. The file has to be quite long for the checksum > calculation to be worthwhile. I don't think so. No matter how long the file is, the checksum is always going to do far more work than a simple string comparison. A string comparison can short-circuit on the first difference; the checksum must always process both files in their entirety. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 23 04:47:07 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 09:47:07 GMT Subject: Idiom for shelling out to $EDITOR/$PAGER? References: <4EF4009E.8010109@tim.thechases.com> <4ef44d7f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef44e1a$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 09:44:31 +0000, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 09:28:52 +0100, Peter Otten wrote: > >>> -proper & efficient detection of file-change, to know whether the user >>> actually did anything >> >> Just read the whole thing back into memory and compare the string to >> the original data. The file has to be quite long for the checksum >> calculation to be worthwhile. > > I don't think so. No matter how long the file is, the checksum is always > going to do far more work than a simple string comparison. A string > comparison can short-circuit on the first difference; the checksum must > always process both files in their entirety. Wait... the above only holds assuming you keep both the before-editing and after-editing versions. If you don't, then Peter is right, there comes a point where keeping a checksum saves enough memory for the additional effort to be worth while. Sorry for the noise. -- Steven From rosuav at gmail.com Fri Dec 23 04:59:16 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 20:59:16 +1100 Subject: what does 'a=b=c=[]' do In-Reply-To: <4EF440A9.7030101@stoneleaf.us> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> Message-ID: On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman wrote: > That is the most ridiculous thing I have heard in a while. ?Mutable default > arguments are *not* a bug in Python. > > Reminds me of a bug report a couple years back claiming multiple inheritence > was a bug and asking it to be removed. Both of these could arguably be called misfeaures, but not bugs. ChrisA From steve+comp.lang.python at pearwood.info Fri Dec 23 04:59:51 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 09:59:51 GMT Subject: Idiom for shelling out to $EDITOR/$PAGER? References: Message-ID: <4ef45117$0$29973$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Dec 2011 22:16:30 -0600, Tim Chase wrote: > I presume the code for spawning $PAGER on some content would look pretty > similar. Have a look at pydoc in the standard library, which implements pager-like functionality. -- Steven From rustompmody at gmail.com Fri Dec 23 05:22:33 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 02:22:33 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 23, 2:39?pm, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: > > Likewise function arguments that default to mutable entities is a known > > gotcha of python which is best treated as a bug in python. > > Nonsense. It is a feature, not a bug. Tsk Tsk How can python have a bug? And that too on the python mailing list? Others however feel differently. See Chris Rebert's http://mail.python.org/pipermail/python-ideas/2007-January/000073.html and the links cited there. > > Some people might argue that it is a mistake, a minor feature which > allegedly causes more difficulties than benefits. I do not hold with that > idea. But either way, it is not a bug to be fixed, but a deliberate > consequence of intended semantics. I did not ask or imply that it should be 'fixed', just that language misfeatures should be treated with extra care. Windows uses CRLF where Unix uses LF. Nobody could argue that this discrepancy is of any use and nobody is about to fix it. Of course if "bug" means "must-fix-else-unusable" then sure you are right but then we would not be able to use most of the hardware or software that we do. To restate what (I think) Ian is saying: Exploit language misfeatures cleverly in your code if you want. But please red-flag them in mailing list posts. From rustompmody at gmail.com Fri Dec 23 05:31:46 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 02:31:46 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> Message-ID: <77861e74-c4fa-4f3f-8f28-853a1a319b1d@l16g2000prg.googlegroups.com> On Dec 23, 2:59?pm, Chris Angelico wrote: > On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman wrote: > > That is the most ridiculous thing I have heard in a while. ?Mutable default > > arguments are *not* a bug in Python. > > > Reminds me of a bug report a couple years back claiming multiple inheritence > > was a bug and asking it to be removed. > > Both of these could arguably be called misfeatures, but not bugs. > > ChrisA In Fortran, if the comma in the loop DO 10 I = 1,10 is misspelt as '.' it becomes the assignment DO10I = 1.0 Do you consider it a bug or a feature? Does Fortran consider it a bug or feature? From bex.lewis at gmail.com Fri Dec 23 05:41:48 2011 From: bex.lewis at gmail.com (becky_lewis) Date: Fri, 23 Dec 2011 02:41:48 -0800 (PST) Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error References: <920839e5-58fa-4fdf-9c88-7bed9a7448fd@d10g2000vbh.googlegroups.com> Message-ID: <1f45fa2a-0c4f-4362-bc75-e73ce16595ab@o9g2000vbc.googlegroups.com> Is there any possibility that you can tell us what the script actually is or provide a code listing (use pastebin if it's big)? From r32813 at freescale.com Fri Dec 23 06:20:36 2011 From: r32813 at freescale.com (Wong Wah Meng-R32813) Date: Fri, 23 Dec 2011 11:20:36 +0000 Subject: Timeout when calling COM objects on Windows In-Reply-To: <77861e74-c4fa-4f3f-8f28-853a1a319b1d@l16g2000prg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <77861e74-c4fa-4f3f-8f28-853a1a319b1d@l16g2000prg.googlegroups.com> Message-ID: <78E1273CA6E76A43BB8830A194FF709B01F48E@039-SN2MPN1-013.039d.mgd.msft.net> Hello there, I am converting my VB ActiveX application written sometime back in year 2000 with python 1.5.2 to run in python 2.7.1. My application is using RMI architecture and I am using a pythonOleRmi.py parser to exchange data/objects between the python and the VB exe. I have no issue when my VB EXE is not running as a COM server while making python API calls where my application server is written purely in python and runs on UNIX. However, when my VB EXE is running as a COM server, this code seems hangs when I make the python API same call to the application server. Is there something that I need to change or configure in order to run this successfully? Or to make some security setting on my Windows? (My VB EXE is run on Windows 7 now, will try out if the same issue is encountered on XP next week). Is the issue resided in the calling to the local daemon process (10.228.70.137:26000), or it is some security setting depicted in the policy.py file? Collecting Python Trace Output... Proxy called with: MatlMgr, getCompatibleFTCSVersions(), (), {} Dec 23 18:41:21 OleRmiClient Timeout of 300 seconds occurred on the invocati on of ('getAppAddress', (u'MatlMgr',), {}) to ('10.228.70.137', 26000) pythoncom error: Python error invoking COM method. Traceback (most recent call last): File "C:\genesis\Enablers\Python\lib\site-packages\win32com\server\policy.py", line 277, in _Invoke_ return self._invoke_(dispid, lcid, wFlags, args) File "C:\genesis\Enablers\Python\lib\site-packages\win32com\server\policy.py", line 282, in _invoke_ return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None) File "C:\genesis\Enablers\Python\lib\site-packages\win32com\server\policy.py", line 585, in _invokeex_ return func(*args) File "C:\genesis\Product\Lib\PythonOleRmi.py", line 240, in Proxy proxy = self._getProxyObj(pyserverString) File "C:\genesis\Product\Lib\PythonOleRmi.py", line 223, in _getProxyObj None, self._logWriter, rem_admin_addr = remAddr ) File "C:\genesis\Product\Lib\EComponent.py", line 710, in __init__ addr = self._getAppProxyAddress() File "C:\genesis\Product\Lib\EComponent.py", line 742, in _getAppProxyAddress addr = remAdmin.getAppAddress(self.server_name) File "C:\genesis\Product\Lib\EComponent.py", line 611, in __call__ self._method, args, kw ) File "C:\genesis\Product\Lib\RMI.py", line 1779, in _genericInvocation reply = self._requestReply( replyBit, (method_name, args, kw) ) File "C:\genesis\Product\Lib\RMI.py", line 1585, in _requestReply reply = self._receive() File "C:\genesis\Product\Lib\RMI.py", line 1677, in _receive raise ServerReplyTimeout( self.reply_timeout) ServerReplyTimeout: 300 Regards, Wah Meng From icanbob at gmail.com Fri Dec 23 06:50:19 2011 From: icanbob at gmail.com (bobicanprogram) Date: Fri, 23 Dec 2011 03:50:19 -0800 (PST) Subject: IPC with multiprocessing.connection References: Message-ID: On Dec 22, 3:27?pm, Andrew Berg wrote: > I'm trying to set up a system where my main program launches external > programs and then establishes connections to them via named pipes or > Unix domain sockets (depending on platform) with > multiprocessing.connection.Listener. The issue I'm having is with the > accept() method. If there is nothing on the other side of the > pipe/socket, it just hangs. Connection objects have a poll() method that > can timeout, but I see no way to continue the program if there's no > initial connection. What I'd like to do is let it time out if there's > nothing on the other side. > I'm pretty new to the multiprocessing module (and IPC in general), so I > could've easily missed something. > > -- > CPython 3.2.2 | Windows NT 6.1.7601.17640 You might want to check out the code samples at: http://www.icanprogram.com/06py/lesson1/lesson1.html bob From phil at riverbankcomputing.com Fri Dec 23 06:56:48 2011 From: phil at riverbankcomputing.com (Phil Thompson) Date: Fri, 23 Dec 2011 11:56:48 +0000 Subject: ANN: PyQt v4.9 Released (Python bindings for Qt) Message-ID: PyQt v4.9 has been released and is available from http://www.riverbankcomputing.com/software/pyqt/. PyQt is a comprehensive set of bindings for the Qt application and UI framework from Nokia. It supports the same platforms as Qt (Windows, Linux and MacOS/X). PyQt supports Python v3 and Python v2.3 and later. The highlight of this release is full support for Qt v4.8.0 and the inclusion of the QtDBus module (so providing support for DBus with Python v3). Windows installers are provided for the GPL version of PyQt which contains everything needed for PyQt development (including Qt, Qt Designer, QScintilla, and MySQL, PostgreSQL, SQLite and ODBC drivers) except Python itself. Installers are provided for the 32 and 64 bit versions of Python v2.5, v2.6, v2.7, v3.1 and v3.2. PyQt v4 is implemented as a set of 19 extension modules containing over 400 classes and over 6,000 functions and methods. QtCore The non-GUI infrastructure including event loops, threads, i18n, Unicode, signals and slots, user and application settings, mapped files and shared memory. QtDeclarative A set of classes that implement a framework for building highly dynamic user interfaces using the QML declarative language that can be integrated with Python. QtDBus A set of classes for accessing remote applications using the DBus protocol. QtDesigner A set of classes that allow the Qt Designer GUI design tool to be extended with PyQt. QtGui A rich collection of GUI widgets. QtHelp A set of classes for creating and viewing searchable documentation and being able to integrate online help with PyQt applications. It includes the C++ port of the Lucene text search engine. QtNetwork A set of classes to support TCP and UDP socket programming and higher level protocols (eg. HTTP, SSL). QtOpenGL A set of classes that allows PyOpenGL to render onto Qt widgets. QtScript A set of classes that implements a JavaScript interpreter. Python objects may be exposed in the interpreter as JavaScript objects. QtScriptTools A debugger for the JavaScript interpreter. QtSql A set of classes that implement SQL data models and interfaces to industry standard databases. The Windows installers include support for SQLite, MySQL, PostgreSQL and ODBC. QtSvg A set of classes to render SVG files onto Qt widgets. QtTest A set of classes to automate unit testing of PyQt applications and GUIs. QtWebKit This implements a web browser engine based on the WebKit engine used by Apple's Safari browser. It allows the methods and properties of Python objects to be published and appear as JavaScript objects to scripts embedded in HTML pages. QtXML A set of classes that implement DOM and SAX parsers. QtXMLPatterns A set of classes that implement XQuery and XPath support for XML and custom data models. QAxContainer A set of classes for Windows that allows the integration of ActiveX controls and COM objects. phonon A cross-platform multimedia framework that enables the use of audio and video content in PyQt applications. DirectX is used as the Windows backend, QuickTime as the MacOS/X backend, and GStreamer as the Linux backend. QtMultimedia A set of classes that provide low-level multimedia functions. Application developers would normally use the phonon module. PyQt includes the pyuic4 utility which generates Python code to implement user interfaces created with Qt Designer in the same way that the uic utility generates C++ code. It is also able to load Designer XML files dynamically. PyQt is available under the GPL and a commercial license. Unlike Qt, PyQt is not available under the LGPL. The commercial PyQt license allows GPL applications to be relicensed at any time. From ben+python at benfinney.id.au Fri Dec 23 07:06:03 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 23 Dec 2011 23:06:03 +1100 Subject: Idiom for shelling out to $EDITOR/$PAGER? References: <877h1nn7ej.fsf@benfinney.id.au> Message-ID: <87wr9nlcgk.fsf@benfinney.id.au> Cameron Simpson writes: > On 23Dec2011 17:12, Ben Finney wrote: > | That doesn't address the concern Tim raised: did the user actually do > | anything, did the file change? > > I'm not sure it matters. I know of numerous applications where it matters, spcifically ones which decide what to do with the edited text. Including ?submit a bug report? and ?submit a wiki page edit? and ?send an email message?. > Sure. But still, if you're able to act on a changed file, why not also > act on an unchanged file? The user asked to change things; take what > you got back and proceed! If the buffer was not edited, the user has most likely changed their mind about editing at all, and there are many applications where the correct default action differes depending on whether the user actually changed anything in the text. -- \ ?By instructing students how to learn, unlearn, and relearn, a | `\ powerful new dimension can be added to education.? ?Alvin | _o__) Toffler, _Future Shock_, 1970 | Ben Finney From bahamutzero8825 at gmail.com Fri Dec 23 07:27:12 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 23 Dec 2011 06:27:12 -0600 Subject: IPC with multiprocessing.connection In-Reply-To: References: Message-ID: <4EF473A0.9090209@gmail.com> On 12/23/2011 5:50 AM, bobicanprogram wrote: > You might want to check out the code samples at: > http://www.icanprogram.com/06py/lesson1/lesson1.html Let's see, I asked about a method in the multiprocessing module, and you recommend code snippets that run on Python versions that don't even have the module and that assume Linux when I state in my email signature the version of /Windows/ that I run. On top of all that, it's not even designed to teach Python; it's designed to explain how to use that library! I honestly don't mind someone pushing their website if it actually might help, but you clearly didn't read my post. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From python.list at tim.thechases.com Fri Dec 23 07:28:26 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 23 Dec 2011 06:28:26 -0600 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: <87wr9nlcgk.fsf@benfinney.id.au> References: <877h1nn7ej.fsf@benfinney.id.au> <87wr9nlcgk.fsf@benfinney.id.au> Message-ID: <4EF473EA.2040205@tim.thechases.com> On 12/23/11 06:06, Ben Finney wrote: > Cameron Simpson writes: >> On 23Dec2011 17:12, Ben Finney >> wrote: | That doesn't address the concern Tim raised: did >> the user actually do | anything, did the file change? >> >> I'm not sure it matters. > > I know of numerous applications where it matters, spcifically > ones which decide what to do with the edited text. Including > ?submit a bug report? and ?submit a wiki page edit? and ?send > an email message?. > > If the buffer was not edited, the user has most likely changed > their mind about editing at all, and there are many > applications where the correct default action differes > depending on whether the user actually changed anything in the > text. Yes, Ben's understanding is correct here, that for some of my use-cases, it's helpful to know if the user did anything. The application can then decide whether it should use the original data it passed to the function, or whether it should go some "abort what you're doing" route. -tkc From neilc at norwich.edu Fri Dec 23 08:10:53 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 23 Dec 2011 13:10:53 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> Message-ID: <9ljcutFemiU5@mid.individual.net> On 2011-12-23, Chris Angelico wrote: > On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman wrote: >> That is the most ridiculous thing I have heard in a while. >> ?Mutable default arguments are *not* a bug in Python. >> >> Reminds me of a bug report a couple years back claiming >> multiple inheritence was a bug and asking it to be removed. > > Both of these could arguably be called misfeaures, but not > bugs. Is the misfeature that Python doesn't evaluate the default argument expression every time you call the function? What would be the harm if it did? -- Neil Cerutti From robert.kern at gmail.com Fri Dec 23 08:10:54 2011 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 23 Dec 2011 13:10:54 +0000 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/23/11 10:22 AM, rusi wrote: > On Dec 23, 2:39 pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: >>> Likewise function arguments that default to mutable entities is a known >>> gotcha of python which is best treated as a bug in python. >> >> Nonsense. It is a feature, not a bug. > > Tsk Tsk How can python have a bug? And that too on the python mailing > list? > > Others however feel differently. See Chris Rebert's > http://mail.python.org/pipermail/python-ideas/2007-January/000073.html > and the links cited there. > >> >> Some people might argue that it is a mistake, a minor feature which >> allegedly causes more difficulties than benefits. I do not hold with that >> idea. But either way, it is not a bug to be fixed, but a deliberate >> consequence of intended semantics. > > I did not ask or imply that it should be 'fixed', just that language > misfeatures should be treated with extra care. "Bug" means, roughly, "something that should be fixed" not just any "thing that has some unwanted consequences". So yes, by calling it a bug you are asking and implying just that. If you don't mean that, don't use the word "bug". -- 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 From neilc at norwich.edu Fri Dec 23 08:13:38 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 23 Dec 2011 13:13:38 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> Message-ID: <9ljd41Fp3bU1@mid.individual.net> On 2011-12-23, Neil Cerutti wrote: > Is the misfeature that Python doesn't evaluate the default > argument expression every time you call the function? What > would be the harm if it did? ...you know, assuming it wouldn't break existing code. ;) -- Neil Cerutti From rustompmody at gmail.com Fri Dec 23 08:23:59 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 05:23:59 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> On Dec 23, 6:10?pm, Robert Kern wrote: > On 12/23/11 10:22 AM, rusi wrote: > > > > > > > > > > > On Dec 23, 2:39 pm, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> ?wrote: > >> On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: > >>> Likewise function arguments that default to mutable entities is a known > >>> gotcha of python which is best treated as a bug in python. > > >> Nonsense. It is a feature, not a bug. > > > Tsk Tsk How can python have a bug? And that too on the python mailing > > list? > > > Others however feel differently. See Chris Rebert's > >http://mail.python.org/pipermail/python-ideas/2007-January/000073.html > > and the links cited there. > > >> Some people might argue that it is a mistake, a minor feature which > >> allegedly causes more difficulties than benefits. I do not hold with that > >> idea. But either way, it is not a bug to be fixed, but a deliberate > >> consequence of intended semantics. > > > I did not ask or imply that it should be 'fixed', just that language > > misfeatures should be treated with extra care. > > "Bug" means, roughly, "something that should be fixed" not just any "thing that > has some unwanted consequences". So yes, by calling it a bug you are asking and > implying just that. If you don't mean that, don't use the word "bug". Of course it should be fixed. The repeated recurrence of it as a standard gotcha as well as the python ideas list testifies to that. Its not going to be fixed -- does not mean thats ideal, just that the best proposed solutions cost/benefit equations are not good enough at this point. Case in point: I think it was around python 2.2 that there were discussions for having a conditional operator. GvR did not put it in for a couple of releases not because it was a bad idea but because no syntax was good enough. When he finally did it, he chose a syntax which is arguably not ideal but is the best all-things-considered (eg no unnecessary new keywords, compatibility with existing code etc) From robert.kern at gmail.com Fri Dec 23 08:53:54 2011 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 23 Dec 2011 13:53:54 +0000 Subject: what does 'a=b=c=[]' do In-Reply-To: <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> Message-ID: On 12/23/11 1:23 PM, rusi wrote: > On Dec 23, 6:10 pm, Robert Kern wrote: >> On 12/23/11 10:22 AM, rusi wrote: >>> On Dec 23, 2:39 pm, Steven D'Aprano>> +comp.lang.pyt... at pearwood.info> wrote: >>>> Some people might argue that it is a mistake, a minor feature which >>>> allegedly causes more difficulties than benefits. I do not hold with that >>>> idea. But either way, it is not a bug to be fixed, but a deliberate >>>> consequence of intended semantics. >> >>> I did not ask or imply that it should be 'fixed', just that language >>> misfeatures should be treated with extra care. >> >> "Bug" means, roughly, "something that should be fixed" not just any "thing that >> has some unwanted consequences". So yes, by calling it a bug you are asking and >> implying just that. If you don't mean that, don't use the word "bug". > > Of course it should be fixed. The repeated recurrence of it as a > standard gotcha as well as the python ideas list testifies to that. So you were lying when you said that you did not ask or imply that it should be 'fixed'? Please make up your mind. It's quite rude to "defend" your position by constantly shifting it whenever you get challenged on it. -- 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 From rustompmody at gmail.com Fri Dec 23 09:57:02 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 06:57:02 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> Message-ID: <0d2fa782-21d1-416a-9756-bd833163c36a@t36g2000prj.googlegroups.com> On Dec 23, 6:53?pm, Robert Kern wrote: > On 12/23/11 1:23 PM, rusi wrote: > > > > > > > > > > > On Dec 23, 6:10 pm, Robert Kern ?wrote: > >> On 12/23/11 10:22 AM, rusi wrote: > >>> On Dec 23, 2:39 pm, Steven D'Aprano >>> +comp.lang.pyt... at pearwood.info> ? ?wrote: > >>>> Some people might argue that it is a mistake, a minor feature which > >>>> allegedly causes more difficulties than benefits. I do not hold with that > >>>> idea. But either way, it is not a bug to be fixed, but a deliberate > >>>> consequence of intended semantics. > > >>> I did not ask or imply that it should be 'fixed', just that language > >>> misfeatures should be treated with extra care. > > >> "Bug" means, roughly, "something that should be fixed" not just any "thing that > >> has some unwanted consequences". So yes, by calling it a bug you are asking and > >> implying just that. If you don't mean that, don't use the word "bug". > > > Of course it should be fixed. ?The repeated recurrence of it as a > > standard gotcha as well as the python ideas list testifies to that. > > So you were lying when you said that you did not ask or imply that it should be > 'fixed'? Please make up your mind. It's quite rude to "defend" your position by > constantly shifting it whenever you get challenged on it. Meanings of "should" http://www.englishpage.com/modals/should.html My first should was the 3rd one My second was the 1st one. [And we really should stop this argument (2nd one)] From steve+comp.lang.python at pearwood.info Fri Dec 23 10:33:09 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 15:33:09 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> <0d2fa782-21d1-416a-9756-bd833163c36a@t36g2000prj.googlegroups.com> Message-ID: <4ef49f34$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 06:57:02 -0800, rusi wrote: > On Dec 23, 6:53?pm, Robert Kern wrote: >> On 12/23/11 1:23 PM, rusi wrote: [...] >> > Of course it should be fixed. ?The repeated recurrence of it as a >> > standard gotcha as well as the python ideas list testifies to that. >> >> So you were lying when you said that you did not ask or imply that it >> should be 'fixed'? Please make up your mind. It's quite rude to >> "defend" your position by constantly shifting it whenever you get >> challenged on it. > > Meanings of "should" http://www.englishpage.com/modals/should.html My > first should was the 3rd one > My second was the 1st one. Rusi, are you a native English speaker? Because that excuse/defense does not excuse your comments at all, multiple meanings of "should" or not. Why not just admit to a mistake? After denying you were asking for Python to fix a so-called bug, you then called for Python to fix it. If this were a real argument rather than a friendly debate, people would be crying "Gotcha!" for sure. > [And we really should stop this argument (2nd one)] When the facts are against you, argue semantics; when semantics are against you, argue the facts; when both are against you, claim to be above arguing. -- Steven From joshua.landau.ws at gmail.com Fri Dec 23 10:33:15 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Fri, 23 Dec 2011 15:33:15 +0000 Subject: Grammar for classes In-Reply-To: References: Message-ID: On 22 December 2011 23:39, Terry Reedy wrote: > On 12/20/2011 12:05 PM, Joshua Landau wrote: > >> Should I file a documentation bug report? >> > > Please do. It the addition means something, it needs to be explained in > the text. If it is wrong, it should go. I have filed one at issue13658 . -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Fri Dec 23 10:49:34 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 15:49:34 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> Message-ID: <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 13:13:38 +0000, Neil Cerutti wrote: > On 2011-12-23, Neil Cerutti wrote: >> Is the misfeature that Python doesn't evaluate the default argument >> expression every time you call the function? What would be the harm if >> it did? > > ...you know, assuming it wouldn't break existing code. ;) It will. Python's default argument strategy has been in use for 20 years. Some code will rely on it. I know mine does. There are two strategies for dealing with default arguments that I know of: early binding and late binding. Python has early binding: the default argument is evaluated once, when the function is created. Late binding means the default argument is always re-evaluated each time it is needed. Both strategies are reasonable choices. Both have advantages and disadvantages. Both have use-cases, and both lead to confusion when the user expects one but gets the other. If you think changing from early to late binding will completely eliminate the default argument "gotcha", you haven't thought things through -- at best you might reduce the number of complaints, but only at the cost of shifting them from one set of use- cases to another. Early binding is simple to implement and simple to explain: when you define a function, the default value is evaluated once, and the result stored to be used whenever it is needed. The disadvantage is that it can lead to unexpected results for mutable arguments. Late binding is also simple to explain, but a little harder to implement. The function needs to store the default value as a piece of code (an expression) which can be re-evaluated as often as needed, not an object. The disadvantage of late binding is that since the expression is live, it needs to be calculated each time, even if it turns out to be the same result. But there's no guarantee that it will return the same result each time: consider a default value like x=time.time(), which will return a different value each time it is called; or one like x=a+b, which will vary if either a or b are changed. Or will fail altogether if either a or b are deleted. This will surprise some people some of the time and lead to demands that Python "fix" the "obviously buggy" default argument gotcha. If a language only offers one, I maintain it should offer early binding (the status quo). Why? Because it is more elegant to fake late binding in an early binding language than vice versa. To fake late binding in a language with early binding, use a sentinel value and put the default value inside the body of the function: def func(x, y=None): if y is None: y = [] ... All the important parts of the function are in one place, namely inside the function. To fake early binding when the language provides late binding, you still use a sentinel value, but the initialization code creating the default value is outside the body of the function, usually in a global variable: _DEFAULT_Y = [] # Private constant, don't touch. def func(x, y=None): if y is None: y = _DEFAULT_Y ... This separates parts of the code that should be together, and relies on a global, with all the disadvantages that implies. -- Steven From rosuav at gmail.com Fri Dec 23 10:55:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Dec 2011 02:55:41 +1100 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Dec 24, 2011 at 2:49 AM, Steven D'Aprano wrote: > To fake early binding when the language provides late binding, you still > use a sentinel value, but the initialization code creating the default > value is outside the body of the function, usually in a global variable: > > ? ?_DEFAULT_Y = [] ?# Private constant, don't touch. > > ? ?def func(x, y=None): > ? ? ? ?if y is None: > ? ? ? ? ? ?y = _DEFAULT_Y > ? ? ? ?... > > This separates parts of the code that should be together, and relies on a > global, with all the disadvantages that implies. A static variable (in the C sense) would make this just as clean as the alternative. In Python, that could be implemented as an attribute of the function object. Oh looky here... that's how default arguments are implemented. :) Tim Toady. ChrisA From hoogendoorn.eelco at gmail.com Fri Dec 23 10:56:06 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Fri, 23 Dec 2011 07:56:06 -0800 (PST) Subject: Need advice on the design of my application References: Message-ID: My first suggestion would be to keep the rendering in Python, if at all feasible, and do only the actual simulation/computation in C. Rasterizing a heightfield and rigid body plus some plash effects is nothing that couldnt be done in PyOpenGL, or even something higher- level like visvis or mayavi. (visvis would be my first suggestion) I would run the simulation in a python subprocess that calls into the C dll; that should give you at least one core fully focussed on your computations, without competition/blocking from any other modules. Marshalling the simulation data between subprocesses should be a small performance hurdle relative to getting it on your GPU; either way, the python subprocessing module makes it entirely painless. And I would start with an implementation in numpy; probably, you will be doing some fft's and/or boundary element stuff. This can all be done fairly efficiently in numpy; at minimum it will give you a reference implementation, and its not unlikely it will work well enough that you dont even want to bother with the C implementation anymore. From rustompmody at gmail.com Fri Dec 23 10:59:27 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 07:59:27 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> <0d2fa782-21d1-416a-9756-bd833163c36a@t36g2000prj.googlegroups.com> <4ef49f34$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <17da5f49-5e48-48d5-aa67-026bffc7b805@a31g2000pre.googlegroups.com> On Dec 23, 8:33?pm, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 06:57:02 -0800, rusi wrote: > > On Dec 23, 6:53?pm, Robert Kern wrote: > >> On 12/23/11 1:23 PM, rusi wrote: > [...] > >> > Of course it should be fixed. ?The repeated recurrence of it as a > >> > standard gotcha as well as the python ideas list testifies to that. > > >> So you were lying when you said that you did not ask or imply that it > >> should be 'fixed'? Please make up your mind. It's quite rude to > >> "defend" your position by constantly shifting it whenever you get > >> challenged on it. > > > Meanings of "should"http://www.englishpage.com/modals/should.htmlMy > > first should was the 3rd one > > My second was the 1st one. > > Rusi, are you a native English speaker? Because that excuse/defense does > not excuse your comments at all, multiple meanings of "should" or not. > Why not just admit to a mistake? After denying you were asking for Python > to fix a so-called bug, you then called for Python to fix it. If this > were a real argument rather than a friendly debate, people would be > crying "Gotcha!" for sure. Ok You got me! Does that help the OP's? > It's a slick language but I still have trouble wrapping my brain around some of the concepts. From gnarlodious at gmail.com Fri Dec 23 11:01:54 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 23 Dec 2011 08:01:54 -0800 (PST) Subject: Get named module's file location Message-ID: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> Given a module's name, how do I get the file path without importing it? Searched all over, can't find any such info. Is it possible to ask if a named module exists before attempting an import? Or are we forced to import first and catch any failure? -- Gnarlie From roy at panix.com Fri Dec 23 11:15:33 2011 From: roy at panix.com (Roy Smith) Date: Fri, 23 Dec 2011 11:15:33 -0500 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4ef4a30d$0$29973$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > The disadvantage of late binding is that since the expression is live, it > needs to be calculated each time, even if it turns out to be the same > result. But there's no guarantee that it will return the same result each > time: consider a default value like x=time.time(), which will return a > different value each time it is called I know this is not quite the same thing, but it's interesting to look at what django (and mongoengine) do in their model definitions, prompted by your time.time() example. You can do declare a model field something like: class Foo(models.Model): timestamp = DateTimeField(default=datetime.utcnow) Now, when you create a Foo, if you don't supply a timestamp, it generates one by calling utcnow() for you. Very handy. I'm not arguing that Python should do that, just pointing out an example of where late binding is nice. Technically, that's probably not really late binding. You always get the same function bound, it's just called each time it's used because django notices that you passed a callable. Maybe sort of "early binding, late calling" would be a more apt description, but given that python has descriptors, the line between data and function sometimes gets a little blurry anyway. From yasar11732 at gmail.com Fri Dec 23 11:16:37 2011 From: yasar11732 at gmail.com (=?ISO-8859-9?Q?Ya=FEar_Arabac=FD?=) Date: Fri, 23 Dec 2011 18:16:37 +0200 Subject: Get named module's file location In-Reply-To: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> Message-ID: >>> import imp >>> imp.find_module("os") (, '/usr/lib/python2.7/os.py', ('.py', 'U', 1)) >>> 2011/12/23 Gnarlodious > Given a module's name, how do I get the file path without importing it? > Searched all over, can't find any such info. > > Is it possible to ask if a named module exists before attempting an import? > > Or are we forced to import first and catch any failure? > > -- Gnarlie > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://yasar.serveblog.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Fri Dec 23 11:18:12 2011 From: roy at panix.com (Roy Smith) Date: Fri, 23 Dec 2011 11:18:12 -0500 Subject: Get named module's file location References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> Message-ID: In article <32472953.855.1324656114851.JavaMail.geo-discussion-forums at prix23>, Gnarlodious wrote: > Given a module's name, how do I get the file path without importing it? > Searched all over, can't find any such info. > > Is it possible to ask if a named module exists before attempting an import? > > Or are we forced to import first and catch any failure? > > -- Gnarlie import imp imp.find_module() Why do you want to do this? From mwilson at the-wire.com Fri Dec 23 11:27:01 2011 From: mwilson at the-wire.com (Mel Wilson) Date: Fri, 23 Dec 2011 11:27:01 -0500 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 23 Dec 2011 13:13:38 +0000, Neil Cerutti wrote: >> On 2011-12-23, Neil Cerutti wrote: >> ...you know, assuming it wouldn't break existing code. ;) > > It will. Python's default argument strategy has been in use for 20 years. > Some code will rely on it. I know mine does. In a tool that's meant for other people to use to accomplish work of their own, breaking workflow is a cardinal sin. In a research language that's meant always to be up-to-date with the concept of the week, not so much. Mel. From gnarlodious at gmail.com Fri Dec 23 11:51:13 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 23 Dec 2011 08:51:13 -0800 (PST) Subject: Get named module's file location In-Reply-To: References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> Message-ID: <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> Roy Smith wrote: > import imp > imp.find_module() Oh yeah that works. I am getting a list of modtimes using List Comprehension, from a list of modules, which will be compared to an older list to see if mod_wsgi needs to be restarted. Maybe thee is an easy way to get the modtimes, I'd be grateful. -- Gnarlie From roy at panix.com Fri Dec 23 11:56:47 2011 From: roy at panix.com (Roy Smith) Date: Fri, 23 Dec 2011 11:56:47 -0500 Subject: Get named module's file location References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> Message-ID: In article <4946660.379.1324659073535.JavaMail.geo-discussion-forums at prez5>, Gnarlodious wrote: > Roy Smith wrote: > > > import imp > > imp.find_module() > > Oh yeah that works. I am getting a list of modtimes using List Comprehension, > from a list of modules, which will be compared to an older list to see if > mod_wsgi needs to be restarted. Ah, I see. Django's runserver does this. You might want to look to see how they implement it (https://www.djangoproject.com/download/). From neilc at norwich.edu Fri Dec 23 12:03:11 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 23 Dec 2011 17:03:11 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9ljqifFfj9U3@mid.individual.net> On 2011-12-23, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 13:13:38 +0000, Neil Cerutti wrote: >> On 2011-12-23, Neil Cerutti wrote: >>> Is the misfeature that Python doesn't evaluate the default >>> argument expression every time you call the function? What >>> would be the harm if it did? >> >> ...you know, assuming it wouldn't break existing code. ;) > > It will. Python's default argument strategy has been in use for > 20 years. Some code will rely on it. I know mine does. I'm aware of that. I should have put the question differently, but you did guess what I meant to ask. Thanks for the dicussion. > Early binding is simple to implement and simple to explain: > when you define a function, the default value is evaluated > once, and the result stored to be used whenever it is needed. > The disadvantage is that it can lead to unexpected results for > mutable arguments. > > Late binding is also simple to explain, but a little harder to > implement. The function needs to store the default value as a > piece of code (an expression) which can be re-evaluated as > often as needed, not an object. > > The disadvantage of late binding is that since the expression > is live, it needs to be calculated each time, even if it turns > out to be the same result. But there's no guarantee that it > will return the same result each time: That's its main *advantage*. > consider a default value like x=time.time(), which will return > a different value each time it is called; or one like x=a+b, > which will vary if either a or b are changed. Or will fail > altogether if either a or b are deleted. This will surprise > some people some of the time and lead to demands that Python > "fix" the "obviously buggy" default argument gotcha. It's hard to see anyone being confused by the resultant exception. It's much harder to figure out what's going wrong with an early-bound mutable. > If a language only offers one, I maintain it should offer early > binding (the status quo). Why? Because it is more elegant to > fake late binding in an early binding language than vice versa. > > To fake late binding in a language with early binding, use a > sentinel value and put the default value inside the body of the > function: > > def func(x, y=None): > if y is None: > y = [] > ... > > All the important parts of the function are in one place, > namely inside the function. > > To fake early binding when the language provides late binding, > you still use a sentinel value, but the initialization code > creating the default value is outside the body of the function, > usually in a global variable: > > _DEFAULT_Y = [] # Private constant, don't touch. > > def func(x, y=None): > if y is None: > y = _DEFAULT_Y > ... > > This separates parts of the code that should be together, and > relies on a global, with all the disadvantages that implies. I'd use a function attribute. def func(x, y=None): if y is None: y = func.default_y ... func.default_y = [] That's awkward only if you believe function attributes are awkward. Even if common practice were instead to use a global variable, as you did, it wouldn't be considered bad if it were a common idiom. In case, a global that's not meant to be rebound or mutated is the best possible variety. However, I also wouldn't base the decision of late versus early binding on how confusing it would be if you assume wrongly which one you are getting. Most default function arguments are literals, so in Python the question just doesn't come up until you get mutabled. The greater efficiency was probably what decided this question for Python, right? Since late-binding is so easy to fake, is hardly ever what you want, and would make all code slower, why do it? -- Neil Cerutti From nikos.kouras at gmail.com Fri Dec 23 12:14:14 2011 From: nikos.kouras at gmail.com (=?ISO-8859-7?B?zenq/Ovh7/Igyu/98eHy?=) Date: Fri, 23 Dec 2011 09:14:14 -0800 (PST) Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error References: <920839e5-58fa-4fdf-9c88-7bed9a7448fd@d10g2000vbh.googlegroups.com> <1f45fa2a-0c4f-4362-bc75-e73ce16595ab@o9g2000vbc.googlegroups.com> Message-ID: <144a4387-e80e-4782-a6eb-aecc1e742dc6@v14g2000yqh.googlegroups.com> On 23 ???, 12:41, becky_lewis wrote: > Is there any possibility that you can tell us what the script actually > is or provide a code listing (use pastebin if it's big)? The script is about retrieving and storing the visitros hostnames to mysql database creating a log file. I dont know why this line host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] fails sometimes and some other times works ok retrieving the hostnames correctly. What do you understand from the traceback?! From torriem at gmail.com Fri Dec 23 12:23:30 2011 From: torriem at gmail.com (Michael Torrie) Date: Fri, 23 Dec 2011 10:23:30 -0700 Subject: what does 'a=b=c=[]' do In-Reply-To: <77861e74-c4fa-4f3f-8f28-853a1a319b1d@l16g2000prg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <77861e74-c4fa-4f3f-8f28-853a1a319b1d@l16g2000prg.googlegroups.com> Message-ID: <4EF4B912.4090702@gmail.com> On 12/23/2011 03:31 AM, rusi wrote: > In Fortran, if the comma in the loop > DO 10 I = 1,10 > is misspelt as '.' it becomes the assignment > DO10I = 1.0 > > Do you consider it a bug or a feature? > Does Fortran consider it a bug or feature? Non sequitor. Nothing at all to do with the issue at hand. Furthermore, you are talking about a parser "feature" not a runtime behavior. From gnarlodious at gmail.com Fri Dec 23 14:40:48 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 23 Dec 2011 11:40:48 -0800 (PST) Subject: Get named module's file location In-Reply-To: References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> Message-ID: <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> I am rolling my own, and learning Python at the same time. One more question. Say I want to assemble a list of tuples like this: modules = ['wsgiref', 'http'] import imp [(imp.find_module(module)[1], os.path.getmtime(imp.find_module(module)[1])) for module in modules] Can I in some way assign imp.find_module(module)[1] to a variable and reuse it? Is this a job for lambda? Thanks anyone. -- Gnarlie From cs at zip.com.au Fri Dec 23 14:47:53 2011 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 24 Dec 2011 06:47:53 +1100 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: References: Message-ID: <20111223194753.GA23781@cskk.homeip.net> On 23Dec2011 02:21, Devin Jeanpierre wrote: | > Anyway, look it up; it has an autodelete mode etc. | | The autodelete isn't useful in this context. The file needs to be | closed and then reopened again; with the autodelete option, the | closing would delete the file, preventing it from being opened by the | text editor or reopened to check contents. | | (As far as I know, the race condition inherent in this is unavoidable. | At least on windows, but I couldn't figure out any other way on Linux | either.) You can fire off the editor from inside the with statement. Write contents, flush, edit, open T.name for read, read, close, exit with statement. Windows may hate you for this with its locking behaviour for open files, but it will work for UNIX. Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ The elliptical cam has gradually slid up the beam shaft and caught on the flange rebate dislodging the gripley, with disastrous results. - Death, 'Reaper Man' (Pratchett) From wolftracks at invalid.com Fri Dec 23 14:51:29 2011 From: wolftracks at invalid.com (W. eWatson) Date: Fri, 23 Dec 2011 11:51:29 -0800 Subject: Cannot Remove Python Libs from XP In-Reply-To: References: Message-ID: On 12/22/2011 1:21 PM, Dennis Lee Bieber wrote: > On Thu, 22 Dec 2011 09:53:04 -0800, "W. eWatson" > wrote: > >> I have three py libs and the python program itself, 2.52, installed on >> an 6 year old HP Laptop. I decided to remove them, and took removed >> Python with the Control Panel ?Add/Remove icon. Worked fine. When I try >> to remove any of the remaining libs, press the add/remove button does >> nothing but blink. How do I get around this problem? > > It would probably have been better to remove the libraries first > (especially if their installers /used/ Python to do the install; the > uninstaller would then likely also have used that version of Python). In > short, uninstalling a set of related packages should be done in the > reverse order of how they were installed. > > If you don't have anything private stored in the remains of the > Python2.5.2 install directory... Just delete the entire directory tree > (which should get rid of any left-over files from the other libraries). > > Removing them from the Add/Remove control might be achieved by using > a registry cleaner; if not, manually scanning the registry for those > package names and deleting the registry keys may suffice (back up the > registry first -- export everything from the top down). Then run a > registry cleaner, as there may be stray GUID keys that eventually mapped > to the deleted keys. > > Lets see.... {booting even older WinXP laptop -- I'm using a Win7 > laptop with data-card for current online activity} > > HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\... > > Look for the particular packages (and anything else you may also > have removed just in case something got left behind) and delete the > keys. (Interesting -- I have an entry for wxPython, but no entry for > Python or ActiveState... They may be stuffed elsewhere -- I also seem to > have a corrupted search path; maybe happened last time I tried using the > laptop to debug one of your queries; Pythonwin isn't finding win32ui) > > I think this turns out to be something of a laugher. I did re-install Python 2.5.2; however, the action was similar when I tried to remove the libs. I happened to move the screen slightly, and noticed a small hidden dialog behind the Add/Remove screen with a message in it. "Are you sure you want to remove this program?". Argh. How foolish it was to hide that simple dialog. So hitting the add/remove button repeatedly really was useless. From rosuav at gmail.com Fri Dec 23 14:52:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Dec 2011 06:52:22 +1100 Subject: Get named module's file location In-Reply-To: <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> Message-ID: On Sat, Dec 24, 2011 at 6:40 AM, Gnarlodious wrote: > [(imp.find_module(module)[1], os.path.getmtime(imp.find_module(module)[1])) for module in modules] > > Can I in some way assign imp.find_module(module)[1] to a variable and reuse it? Is this a job for lambda? Well, you can use an additional comprehension to provide a temporary variable, if you really want to do it all as a single expression. [(m, os.path.getmtime(m)) for m in (imp.find_module(module)[1] for module in modules)] ChrisA From roy at panix.com Fri Dec 23 15:00:17 2011 From: roy at panix.com (Roy Smith) Date: Fri, 23 Dec 2011 15:00:17 -0500 Subject: Get named module's file location References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> Message-ID: In article <4652751.858.1324669248908.JavaMail.geo-discussion-forums at prj1>, Gnarlodious wrote: > I am rolling my own, and learning Python at the same time. Hmmm. The imp module is kind of deep magic for a first introduction to the language. But, whatever. > One more question. Say I want to assemble a list of tuples like this: > > modules = ['wsgiref', 'http'] > import imp > [(imp.find_module(module)[1], os.path.getmtime(imp.find_module(module)[1])) > for module in modules] > > Can I in some way assign imp.find_module(module)[1] to a variable and reuse > it? Is this a job for lambda? I think what you want to do is rewrite the list comprehension as a regular loop. my_list = [] for module in modules: m = imp.find_module(module)[1] my_list.append(m, os.path.getmtime(m)) From jeanpierreda at gmail.com Fri Dec 23 16:01:17 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 23 Dec 2011 16:01:17 -0500 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: <20111223194753.GA23781@cskk.homeip.net> References: <20111223194753.GA23781@cskk.homeip.net> Message-ID: > Windows may hate you for this with its locking behaviour for open files, > but it will work for UNIX. Windows won't let you open the file while NamedTemporaryFile has a hold on it, yeah. Your point is legitimate anyway. Even so, there's still the race conditions for "what if the tempfile is moved before the editor can get to it" and "what if the tempfile is moved after the editor can get to it but before I can open it again". -- Devin On Fri, Dec 23, 2011 at 2:47 PM, Cameron Simpson wrote: > On 23Dec2011 02:21, Devin Jeanpierre wrote: > | > Anyway, look it up; it has an autodelete mode etc. > | > | The autodelete isn't useful in this context. The file needs to be > | closed and then reopened again; with the autodelete option, the > | closing would delete the file, preventing it from being opened by the > | text editor or reopened to check contents. > | > | (As far as I know, the race condition inherent in this is unavoidable. > | At least on windows, but I couldn't figure out any other way on Linux > | either.) > > You can fire off the editor from inside the with statement. > Write contents, flush, edit, open T.name for read, read, close, exit > with statement. > > Windows may hate you for this with its locking behaviour for open files, > but it will work for UNIX. > > Cheers, > -- > Cameron Simpson DoD#743 > http://www.cskk.ezoshosting.com/cs/ > > The elliptical cam has gradually slid up the beam shaft and caught on the > flange rebate dislodging the gripley, with disastrous results. > ? ? ? ?- Death, 'Reaper Man' (Pratchett) From rosuav at gmail.com Fri Dec 23 16:21:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Dec 2011 08:21:56 +1100 Subject: Get named module's file location In-Reply-To: <32191702.960.1324675017428.JavaMail.geo-discussion-forums@prmw6> References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> <32191702.960.1324675017428.JavaMail.geo-discussion-forums@prmw6> Message-ID: I'm guessing you meant for this to be on-list, and am hoping you don't mind that I'm replying on-list. On Sat, Dec 24, 2011 at 8:16 AM, Gnarlodious wrote: > Chris Angelico wrote: >> [(m, os.path.getmtime(m)) for m in (imp.find_module(module)[1] for >> module in modules)] >> >> Yeah, a little hard to read. Tell me, does this formulation execute >> imp.find_module(module) once or twice for each modname? What this does is save a temporary list, more or less. (It's actually a generator expression, not a list comprehension, but that's immaterial.) temporary = [imp.find_module(module)[1] for module in modules] [(m, os.path.getmtime(m)) for m in temporary] It iterates over modules, calling find_module for each, and saving the results to a new list. Then separately iterates over the new list, pairing each with the getmtime. Since I used parentheses instead of square brackets in the original expression, Python won't actually build the full list. Other than that, it's equivalent to the two-statement version, and you can try those two in IDLE to see what they do. ChrisA From steve+comp.lang.python at pearwood.info Fri Dec 23 17:32:15 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 22:32:15 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef5016e$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 02:55:41 +1100, Chris Angelico wrote: > On Sat, Dec 24, 2011 at 2:49 AM, Steven D'Aprano > wrote: >> To fake early binding when the language provides late binding, you >> still use a sentinel value, but the initialization code creating the >> default value is outside the body of the function, usually in a global >> variable: >> >> ? ?_DEFAULT_Y = [] ?# Private constant, don't touch. >> >> ? ?def func(x, y=None): >> ? ? ? ?if y is None: >> ? ? ? ? ? ?y = _DEFAULT_Y >> ? ? ? ?... >> >> This separates parts of the code that should be together, and relies on >> a global, with all the disadvantages that implies. > > A static variable (in the C sense) would make this just as clean as the > alternative. In Python, that could be implemented as an attribute of the > function object. Oh looky here... that's how default arguments are > implemented. :) Yes. But having to manage it *by hand* is still unclean: * you still have to assign the default value to the function assignment outside the function, which is inelegant; * if you rename the function, the internal lookup func.default_value will fail. I'm not saying it can't be done. I'm just saying that the status quo is cleaner and more elegant, and if you want late binding, there is a simple, obvious, elegant idiom to get it. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 23 17:39:57 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 22:39:57 GMT Subject: Get named module's file location References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> Message-ID: <4ef5033c$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 15:00:17 -0500, Roy Smith wrote: >> Can I in some way assign imp.find_module(module)[1] to a variable and >> reuse it? Is this a job for lambda? > > I think what you want to do is rewrite the list comprehension as a > regular loop. > > my_list = [] > for module in modules: > m = imp.find_module(module)[1] > my_list.append(m, os.path.getmtime(m)) +1 List comprehensions are so cool that sometimes people forget that not every loop has to be a list comp. There is no shortage of newlines in the world, and not everything needs to be on a single line. -- Steven From rosuav at gmail.com Fri Dec 23 17:50:04 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Dec 2011 09:50:04 +1100 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: <4ef5016e$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef5016e$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Dec 24, 2011 at 9:32 AM, Steven D'Aprano wrote: > Yes. But having to manage it *by hand* is still unclean: Well, my point was that Python's current behaviour _is_ that. > * you still have to assign the default value to the function assignment > outside the function, which is inelegant; C's static variables are initialized inside the function. But since Python doesn't have that, it doesn't really work that way. (You might be able to use a decorator to do some cool tricks though.) > * if you rename the function, the internal lookup func.default_value will > fail. Python would benefit some from a "current-function" reference, if tricks like this were to be encouraged. It'd help with recursion too. hmmmm... >>> def Y(func): return lambda *args,**kwargs: func(func,*args,**kwargs) >>> @Y def foo(me,x): if x>5: return x return me(me,x+1),7,x >>> foo(3) (((6, 7, 5), 7, 4), 7, 3) Useful? Not very. Maybe as a language feature, but not as a parameter. But of curiosity value. ChrisA From jeanpierreda at gmail.com Fri Dec 23 19:24:44 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 23 Dec 2011 19:24:44 -0500 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: > To fake early binding when the language provides late binding, you still > use a sentinel value, but the initialization code creating the default > value is outside the body of the function, usually in a global variable: > > _DEFAULT_Y = [] # Private constant, don't touch. > > def func(x, y=None): > if y is None: > y = _DEFAULT_Y > ... > > This separates parts of the code that should be together, and relies on a > global, with all the disadvantages that implies. No, you can just do def func(x, y=_DEFAULT_Y): ... -- Devin On Fri, Dec 23, 2011 at 10:49 AM, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 13:13:38 +0000, Neil Cerutti wrote: > >> On 2011-12-23, Neil Cerutti wrote: >>> Is the misfeature that Python doesn't evaluate the default argument >>> expression every time you call the function? What would be the harm if >>> it did? >> >> ...you know, assuming it wouldn't break existing code. ;) > > It will. Python's default argument strategy has been in use for 20 years. > Some code will rely on it. I know mine does. > > There are two strategies for dealing with default arguments that I know > of: early binding and late binding. Python has early binding: the default > argument is evaluated once, when the function is created. Late binding > means the default argument is always re-evaluated each time it is needed. > > Both strategies are reasonable choices. Both have advantages and > disadvantages. Both have use-cases, and both lead to confusion when the > user expects one but gets the other. If you think changing from early to > late binding will completely eliminate the default argument "gotcha", you > haven't thought things through -- at best you might reduce the number of > complaints, but only at the cost of shifting them from one set of use- > cases to another. > > Early binding is simple to implement and simple to explain: when you > define a function, the default value is evaluated once, and the result > stored to be used whenever it is needed. The disadvantage is that it can > lead to unexpected results for mutable arguments. > > Late binding is also simple to explain, but a little harder to implement. > The function needs to store the default value as a piece of code (an > expression) which can be re-evaluated as often as needed, not an object. > > The disadvantage of late binding is that since the expression is live, it > needs to be calculated each time, even if it turns out to be the same > result. But there's no guarantee that it will return the same result each > time: consider a default value like x=time.time(), which will return a > different value each time it is called; or one like x=a+b, which will > vary if either a or b are changed. Or will fail altogether if either a or > b are deleted. This will surprise some people some of the time and lead > to demands that Python "fix" the "obviously buggy" default argument > gotcha. > > If a language only offers one, I maintain it should offer early binding > (the status quo). Why? Because it is more elegant to fake late binding in > an early binding language than vice versa. > > To fake late binding in a language with early binding, use a sentinel > value and put the default value inside the body of the function: > > ? ?def func(x, y=None): > ? ? ? ?if y is None: > ? ? ? ? ? ?y = [] > ? ? ? ?... > > All the important parts of the function are in one place, namely inside > the function. > > To fake early binding when the language provides late binding, you still > use a sentinel value, but the initialization code creating the default > value is outside the body of the function, usually in a global variable: > > ? ?_DEFAULT_Y = [] ?# Private constant, don't touch. > > ? ?def func(x, y=None): > ? ? ? ?if y is None: > ? ? ? ? ? ?y = _DEFAULT_Y > ? ? ? ?... > > This separates parts of the code that should be together, and relies on a > global, with all the disadvantages that implies. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From angrybaldguy at gmail.com Fri Dec 23 21:41:07 2011 From: angrybaldguy at gmail.com (Owen Jacobson) Date: Fri, 23 Dec 2011 21:41:07 -0500 Subject: Idiom for shelling out to $EDITOR/$PAGER? References: Message-ID: <2011122321410754142-angrybaldguy@gmailcom> On 2011-12-23 06:02:18 +0000, Cameron Simpson said: > MacOSX has "open", though it won't be running a blocking editor, alas. But it can be. From the man page: -t Causes the file to be opened with the default text editor, as deter- mined via LaunchServices and -W Causes open to wait until the applications it opens (or that were already open) have exited. Use with the -n flag to allow open to function as an appropriate app for the $EDITOR environment variable. and finally -n Open a new instance of the application(s) even if one is already run- ning. can be combined to make 'open' (the shell interface to OS X's LaunchServices system) into a workable $EDITOR. -o From jason at powerpull.net Sat Dec 24 00:34:46 2011 From: jason at powerpull.net (Jason Friedman) Date: Sat, 24 Dec 2011 05:34:46 +0000 Subject: merging argparse parsers In-Reply-To: <4EEB2D92.10200@gmail.com> References: <4EEB2D92.10200@gmail.com> Message-ID: > I would like to have something like > > merged_parser = LoggingParser() + OtherParser() > > Which should create an argument parser with all the options composed. > I have used parent parsers. http://docs.python.org/py3k/library/argparse.html#parents I think in your case merged_parser would become more of a child, like this: >>> mom_parser = argparse.ArgumentParser(add_help=False) >>> mom_parser.add_argument('--arg1', type=int) >>> dad_parser = argparse.ArgumentParser(add_help=False) >>> dad_parser.add_argument('--arg2', type=int) >>> child_parser = argparse.ArgumentParser(parents=[mom_parser, dad_parser]) From jason at powerpull.net Sat Dec 24 01:04:19 2011 From: jason at powerpull.net (Jason Friedman) Date: Sat, 24 Dec 2011 06:04:19 +0000 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: On Fri, Dec 16, 2011 at 10:44 AM, Chris Angelico wrote: > On Fri, Dec 16, 2011 at 8:45 PM, Ulrich Eckhardt > wrote: >> I'm trying to create a struct_time that is e.g. one year ahead or a month >> back in order to test some parsing/formatting code with different dates. > > Do you need it to be one exact calendar year, or would it make sense > to add/subtract integers from a Unix time? > > t = time.time() + 365*86400 ? # Not actually a year ahead, it's 365 days ahead > t = time.localtime(t) ?# if you want a struct_time > > ChrisA > -- Not particularly elegant, but I believe accurate and relying only on the stated struct_time contract: #!/usr/bin/env python # 2.7.2 import time, itertools def is_local_time_different_by_one_year(time1, time2): if abs(time1.tm_year - time2.tm_year) != 1: return False if abs(time1.tm_mon - time2.tm_mon ) != 0: return False if abs(time1.tm_mday - time2.tm_mday) != 0: return False if abs(time1.tm_hour - time2.tm_hour) != 0: return False if abs(time1.tm_min - time2.tm_min ) != 0: return False if abs(time1.tm_sec - time2.tm_sec ) != 0: return False return True t = time.time() time1 = time.localtime(t) print("Local time is {}.".format(time1)) for i in itertools.count(0): t += 1 # Add one second until we have reached next year time2 = time.localtime(t) if is_local_time_different_by_one_year(time1, time2): print("One year later is {}".format(time2)) break Not exactly a speed demon, either: $ time python timediff.py Local time is time.struct_time(tm_year=2011, tm_mon=12, tm_mday=24, tm_hour=5, tm_min=57, tm_sec=44, tm_wday=5, tm_yday=358, tm_isdst=0). One year later is time.struct_time(tm_year=2012, tm_mon=12, tm_mday=24, tm_hour=5, tm_min=57, tm_sec=44, tm_wday=0, tm_yday=359, tm_isdst=0) real 3m8.922s user 2m2.470s sys 1m1.760s From rosuav at gmail.com Sat Dec 24 02:13:13 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Dec 2011 18:13:13 +1100 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: On Sat, Dec 24, 2011 at 5:04 PM, Jason Friedman wrote: > Not particularly elegant, but I believe accurate and relying only on > the stated struct_time contract: Funny! But a binary search would be better, I think. t = time.time() time1 = time.localtime(t) print("Local time is {}.".format(time1)) est1 = t + 365*86400 est2 = est1 + 86400 # Assume that the year is between 365 and 366 days long (widen this if assumption not valid) while True: est = (est1 + est2) / 2 time2 = time.localtime(t) cmp = is_local_time_different_by_one_year(time1, time2): if cmp<0: est1 = est elif cmp>0: est2 = est else: print("One year later is {}".format(time2)) break Could do with a few more improvements. Also, it requires that the comparison function return -1, 0, or 1, in the same way that strcmp() does (not a difficult enhancement, but I'm off to Christmas dinner with the family so I'll leave it as an exercise for the reader). ChrisA From steve+comp.lang.python at pearwood.info Sat Dec 24 03:11:12 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Dec 2011 08:11:12 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef5016e$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef58920$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 09:50:04 +1100, Chris Angelico wrote: > On Sat, Dec 24, 2011 at 9:32 AM, Steven D'Aprano > wrote: >> Yes. But having to manage it *by hand* is still unclean: > > Well, my point was that Python's current behaviour _is_ that. Minus the managing it by hand part. >> * you still have to assign the default value to the function assignment >> outside the function, which is inelegant; > > C's static variables are initialized inside the function. But since > Python doesn't have that, it doesn't really work that way. (You might be > able to use a decorator to do some cool tricks though.) If Python were C, then static variables would be the right solution, but since it isn't, they aren't. -- Steven From steve+comp.lang.python at pearwood.info Sat Dec 24 03:25:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Dec 2011 08:25:05 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> Message-ID: <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 17:03:11 +0000, Neil Cerutti wrote: >> The disadvantage of late binding is that since the expression is live, >> it needs to be calculated each time, even if it turns out to be the >> same result. But there's no guarantee that it will return the same >> result each time: > > That's its main *advantage*. Ah yes, sorry, poor wording on my part. Whether calculating the default value *once* or *each time* is an advantage or disadvantage depends on what you're trying to do. Either way, it could be just what you want, or an annoying source of bugs. >> consider a default value like x=time.time(), which will return a >> different value each time it is called; or one like x=a+b, which will >> vary if either a or b are changed. Or will fail altogether if either a >> or b are deleted. This will surprise some people some of the time and >> lead to demands that Python "fix" the "obviously buggy" default >> argument gotcha. > > It's hard to see anyone being confused by the resultant exception. That's because you're coming at it from the perspective of somebody who knows what to expect, in the middle of a discussion about the semantics of late binding. Now imagine you're a newbie who has never thought about the details of when the default value is created, but has a function like "def foo(x, y=a+b)". He calls foo(x) seven times and it works, and on the eighth time it blows up, perhaps with a NameError. It's surprising behaviour, and newbies aren't good at diagnosing surprising bugs. Or worse, it doesn't blow up at all, but gives some invalid value that causes your calculations to be completely wrong. Exceptions are not the worst bug to have -- they are the best. > It's > much harder to figure out what's going wrong with an early-bound > mutable. Only for those who don't understand, or aren't thinking about, Python's object model. The behaviour of early-bound mutables is obvious and clear once you think about it, but it does require you to think about what's going on under the hood, so to speak. [...] >> To fake early binding when the language provides late binding, you >> still use a sentinel value, but the initialization code creating the >> default value is outside the body of the function, usually in a global >> variable: [...] > I'd use a function attribute. > > def func(x, y=None): > if y is None: > y = func.default_y > ... > func.default_y = [] > > That's awkward only if you believe function attributes are awkward. I do. All you've done is move the default from *before* the function is defined to *after* the function is defined, instead of keeping it in the function definition. It's still separate, and if the function is renamed your code stops working. In other words, it violates encapsulation of the function. That's not to say that you shouldn't do this. It's a perfectly reasonable technique, and I've used it myself, but it's not as elegant as the current Python default argument behaviour. [...] > The greater efficiency was probably what decided this question for > Python, right? Since late-binding is so easy to fake, is hardly ever > what you want, and would make all code slower, why do it? Excellent point. -- Steven From steve+comp.lang.python at pearwood.info Sat Dec 24 03:26:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Dec 2011 08:26:59 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef58cd3$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 19:24:44 -0500, Devin Jeanpierre wrote: >> To fake early binding when the language provides late binding, you >> still use a sentinel value, but the initialization code creating the >> default value is outside the body of the function, usually in a global >> variable: >> >> _DEFAULT_Y = [] # Private constant, don't touch. >> >> def func(x, y=None): >> if y is None: >> y = _DEFAULT_Y >> ... >> >> This separates parts of the code that should be together, and relies on >> a global, with all the disadvantages that implies. > > No, you can just do def func(x, y=_DEFAULT_Y): ... Point taken. Nevertheless, the semantics are still not the same as actual early binding: if the global name is deleted or changed, the function stops working. -- Steven From lie.1296 at gmail.com Sat Dec 24 05:30:04 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 24 Dec 2011 21:30:04 +1100 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: On 12/22/2011 10:20 AM, Dennis Lee Bieber wrote: > which is to define the names "a", "b", and "c", and connects the three > names to the single object (integer 7 or new empty list). note that this "connects" and "disconnecting" business is more commonly referred to in python parlance as "binding" a name to an object. From mmanns at gmx.net Sat Dec 24 07:03:36 2011 From: mmanns at gmx.net (Martin Manns) Date: Sat, 24 Dec 2011 13:03:36 +0100 Subject: [ANN] pyspread 0.2.0 Message-ID: ============== pyspread 0.2.0 ============== Pyspread 0.2.0 has been released. The new version is an almost complete rewrite for better stability and maintainability. About pyspread ============== Pyspread is a non-traditional spreadsheet application that is based on and written in Python. Pyspread expects Python expressions in its grid cells, which makes a spreadsheet specific language obsolete. Pyspread is free software. It is released under the GPL v3. The project website has changed to http://manns.github.com/pyspread/ What's new in 0.2.0 =================== + Windows support discontinued + New pys save file format + A lot of bug fixes and small changes to the user interface Enjoy Martin From wuwei23 at gmail.com Sat Dec 24 08:43:20 2011 From: wuwei23 at gmail.com (alex23) Date: Sat, 24 Dec 2011 05:43:20 -0800 (PST) Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 24, 2:15?am, Roy Smith wrote: > I know this is not quite the same thing, but it's interesting to look at > what django (and mongoengine) do in their model definitions, prompted by > your time.time() example. ?You can do declare a model field something > like: > > class Foo(models.Model): > ? ?timestamp = DateTimeField(default=datetime.utcnow) > > Now, when you create a Foo, if you don't supply a timestamp, it > generates one by calling utcnow() for you. ?Very handy. > > I'm not arguing that Python should do that, just pointing out an example > of where late binding is nice. There is absolutely nothing stopping you from writing functions now with that behaviour. All Python functions are "early binding, late calling" with their arguments, if you treat the arguments as callables within the function body. From wuwei23 at gmail.com Sat Dec 24 08:52:24 2011 From: wuwei23 at gmail.com (alex23) Date: Sat, 24 Dec 2011 05:52:24 -0800 (PST) Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <0fea2ea6-80df-42c4-8fbb-5b4314095672@h37g2000pri.googlegroups.com> On Dec 24, 2:27?am, Mel Wilson wrote: > In a tool that's meant for other people to use to accomplish work of their > own, breaking workflow is a cardinal sin. > > In a research language that's meant always to be up-to-date with the concept > of the week, not so much. What on earth gave you the impression Python was bleeding edge? As there's more to the language than its syntax, "breaking workflow" disrupts the core library as much as it does the code of everyone else. More importantly, you're talking pap. Research is as much about communication as programming; if you expect every single researcher in a discipline (or even in the same _building_) to remain in perfect lockstep with the version releases of a domain-relevant language, you're either not a researcher or not a very good one. You should get out to a conference occasionally and see what people think about your "concept of the week" idea. From wuwei23 at gmail.com Sat Dec 24 09:08:54 2011 From: wuwei23 at gmail.com (alex23) Date: Sat, 24 Dec 2011 06:08:54 -0800 (PST) Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 24, 6:25?pm, Steven D'Aprano wrote: > > It's > > much harder to figure out what's going wrong with an early-bound > > mutable. > > Only for those who don't understand, or aren't thinking about, Python's > object model. The behaviour of early-bound mutables is obvious and clear > once you think about it, but it does require you to think about what's > going on under the hood, so to speak. And here we've come full circle to the point of this thread. If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd see an increase in the number of questions like the OP's. From hoogendoorn.eelco at gmail.com Sat Dec 24 09:39:39 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 06:39:39 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> Message-ID: <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> On Dec 20, 4:30?am, alex23 wrote: > On Dec 19, 8:15?pm, Eelco wrote: > > > What does that have to do with collection packing/unpacking? > > It's mocking your insistance that collection unpacking is a type > constraint. This is really going to be the last time I waste any words on this: The sentence 'collection unpacking is a type constraint' is entirely nonsensical. A type constraint is a linguistical construct that can be applied in many ways; typically, to narrow down the semantics of use of the symbol to which the type constraint is applied. In case of python, collection PACKING (not unpacking) is signaled by a construct that can be viewed as a type constraint. But if you dont want to fortify your view of the syntax by looking at what it is actually going on, ill repeat again; lets keep things simple, and not analyze it in detail. So here it is again, in terms every 5 year old can understand. Id like to do the exact same thing python is already doing. Except with a few more, and different symbols, to enable one to express a few different variants of behavior. Clear enough? From hoogendoorn.eelco at gmail.com Sat Dec 24 09:41:46 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 06:41:46 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> Message-ID: <6b44b5b2-7e86-4f54-86bd-77ab4cbda9bd@i6g2000vbe.googlegroups.com> On Dec 20, 4:35?am, alex23 wrote: > Eelco wrote: > > Having two seperate symbols seperated by whitespace, as in @list args > > strikes me as a terrible break of normal python lexical rules. > > You mean like 'is not'? And the upcoming 'yield from'? Im not sure why, but this feels like something entirely different to me. I suppose because these are compositions of keywords. Can you give an example of a whitespaced composition of identifiers being a valid construct anywhere else in Python? From hoogendoorn.eelco at gmail.com Sat Dec 24 09:45:01 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 06:45:01 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Dec 20, 6:47?am, Steven D'Aprano wrote: > On Mon, 19 Dec 2011 19:35:20 -0800, alex23 wrote: > > Eelco wrote: > >> Having two seperate symbols seperated by whitespace, as in @list args > >> strikes me as a terrible break of normal python lexical rules. > > > You mean like 'is not'? And the upcoming 'yield from'? > > Also "not in". > > Space-delimited tokens are hardly rare in Python, e.g.: > > import module as name > for x in sequence > if flag > elif condition > while condition > with obj > del name > > Nevertheless, I think the suggested syntax "@list args" is awful. > > -- > Steven Can you give an example of a construct in python where two whitespace delimited identifiers are legal? From hoogendoorn.eelco at gmail.com Sat Dec 24 09:47:21 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 06:47:21 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> Message-ID: <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> On Dec 21, 4:48?pm, Neal Becker wrote: > Clarification: where can packing/unpacking syntax be used? > > It would be great if it were valid essentially anywhere (not limited to > parameter passing). > > What about constructs like: > > a, @tuple tail, b = sequence? This has come up many times in the thread, including in my OP. More closely unifying collection packing/unpacking is part of my goal, so indeed, I would like to be able to write something like: a, middle::tuple, b = ::sequence Where I would like the extra :: before the sequence to explicitly signal collection unpacking on the rhs, to maintain the symmetry with collection unpacking within a function call. From hoogendoorn.eelco at gmail.com Sat Dec 24 09:54:07 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 06:54:07 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 22, 2:12?pm, Steven D'Aprano wrote: > On Thu, 22 Dec 2011 06:49:16 -0500, Neal Becker wrote: > > I agree with the OP that the current syntax is confusing. ?The issue is, > > the meaning of * is context-dependent. > > Here you are complaining about an operator being "confusing" because it > is context-dependent, in a post where you strip all context except the > subject line and expect us to still understand what you're talking about. > There's a lesson there, I'm sure. > > * is context dependent. You know what else is context dependent? Well, > most things. But in particular, parentheses. Clearly they must be > "confusing" too. So how about we say: > > class MyClass superclasslist A, B C: > ? ? def method argumentlist self, x, y: > ? ? ? ? t = tuple 1, 2 tuple 3, 4 endtuple endtuple > ? ? ? ? return group x + y endgroup * group x - y endgroup > > Much less confusing! > > -- > Steven Context dependence is not something to be avoided at all costs, but all else being equal, less is certainly more. The general concept of grouping thing together which parenthesis is an extremely pervasive one in programming, and thus deserves its own set of context-dependent rules. Packing and unpacking collections is far, far more rare, and thus a form that requires you to write more but remember less is certainly relatively favorable. From rosuav at gmail.com Sat Dec 24 09:57:18 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Dec 2011 01:57:18 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> Message-ID: On Sun, Dec 25, 2011 at 1:47 AM, Eelco wrote: > a, middle::tuple, b = ::sequence Then it ought to be ::(a, middle::tuple, b) = ::sequence or something, because you're doing the same thing on both sides. ChrisA From rosuav at gmail.com Sat Dec 24 10:01:04 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Dec 2011 02:01:04 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Sun, Dec 25, 2011 at 1:45 AM, Eelco wrote: > Can you give an example of a construct in python where two whitespace > delimited identifiers are legal? What do you mean? Two identifiers, separated only by whitespace and no keyword or operator? def foo(): asdf qwer Perfectly legal, two statements that probably don't do anything useful though. ChrisA From tinnews at isbd.co.uk Sat Dec 24 10:26:21 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Sat, 24 Dec 2011 15:26:21 +0000 Subject: How to check for single character change in a string? Message-ID: Can anyone suggest a simple/easy way to count how many characters have changed in a string? E.g. giving results as follows:- abcdefg abcdefh 1 abcdefg abcdekk 2 abcdefg gfedcba 6 Note that position is significant, a character in a different position should not count as a match. Is there any simpler/neater way than just a for loop running through both strings and counting non-matching characters? -- Chris Green From ian.g.kelly at gmail.com Sat Dec 24 10:57:01 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 24 Dec 2011 08:57:01 -0700 Subject: How to check for single character change in a string? In-Reply-To: References: Message-ID: On Sat, Dec 24, 2011 at 8:26 AM, wrote: > Can anyone suggest a simple/easy way to count how many characters have > changed in a string? > > E.g. giving results as follows:- > > ? ?abcdefg ? ? abcdefh ? ? ? ? 1 > ? ?abcdefg ? ? abcdekk ? ? ? ? 2 > ? ?abcdefg ? ? gfedcba ? ? ? ? 6 > > > Note that position is significant, a character in a different position > should not count as a match. > > Is there any simpler/neater way than just a for loop running through > both strings and counting non-matching characters? No, but the loop approach is pretty simple: sum(a == b for a, b in zip(str1, str2)) From roy at panix.com Sat Dec 24 10:57:14 2011 From: roy at panix.com (Roy Smith) Date: Sat, 24 Dec 2011 10:57:14 -0500 Subject: How to check for single character change in a string? References: Message-ID: In article , tinnews at isbd.co.uk wrote: > Can anyone suggest a simple/easy way to count how many characters have > changed in a string? Depending on exactly how you define "changed", you're probably talking about either Hamming Distance or Levenshtein Distance. I would start with the wikipedia articles on both those topics and explore from there. There are python packages for computing many of these metrics. For example, http://pypi.python.org/pypi/python-Levenshtein/ > Is there any simpler/neater way than just a for loop running through > both strings and counting non-matching characters? If you don't care about insertions and deletions (and it sounds like you don't), then this is the way to do it. It's O(n), and you're not going to get any better than that. It's a one-liner in python: >>> s1 = 'abcdefg' >>> s2 = 'abcdekk' >>> len([x for x in zip(s1, s2) if x[0] != x[1]]) 2 But go read the wikipedia articles. Computing distance between sequences is an interesting, important, and well-studied topic. It's worth exploring a bit. From roy at panix.com Sat Dec 24 11:10:41 2011 From: roy at panix.com (Roy Smith) Date: Sat, 24 Dec 2011 11:10:41 -0500 Subject: How to check for single character change in a string? References: Message-ID: In article , Roy Smith wrote: > >>> len([x for x in zip(s1, s2) if x[0] != x[1]]) Heh, Ian Kelly's version: > sum(a == b for a, b in zip(str1, str2)) is cleaner than mine. Except that Ian's counts matches and the OP asked for non-matches, but that's an exercise for the reader :-) From arnodel at gmail.com Sat Dec 24 12:09:57 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sat, 24 Dec 2011 17:09:57 +0000 Subject: How to check for single character change in a string? In-Reply-To: References: Message-ID: On 24 December 2011 16:10, Roy Smith wrote: > In article , > ?Roy Smith wrote: > >> >>> len([x for x in zip(s1, s2) if x[0] != x[1]]) > > Heh, Ian Kelly's version: > >> sum(a == b for a, b in zip(str1, str2)) > > is cleaner than mine. ?Except that Ian's counts matches and the OP asked > for non-matches, but that's an exercise for the reader :-) Here's a variation on the same theme: sum(map(str.__ne__, str1, str2)) -- Arnaud From hoogendoorn.eelco at gmail.com Sat Dec 24 12:21:37 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 09:21:37 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> Message-ID: On Dec 24, 3:57?pm, Chris Angelico wrote: > On Sun, Dec 25, 2011 at 1:47 AM, Eelco wrote: > > a, middle::tuple, b = ::sequence > > Then it ought to be > > ::(a, middle::tuple, b) = ::sequence > > or something, because you're doing the same thing on both sides. > > ChrisA Thats a fair point; something to think about. It all depends on how you define the rules of course; im trying to stay as close as possible to the original python rules, where the (un)packing of comma-seperated identifiers is implicit. Probably best to keep it that way, from the looks of it :). From hoogendoorn.eelco at gmail.com Sat Dec 24 12:23:59 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 09:23:59 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Dec 24, 4:01?pm, Chris Angelico wrote: > On Sun, Dec 25, 2011 at 1:45 AM, Eelco wrote: > > Can you give an example of a construct in python where two whitespace > > delimited identifiers are legal? > > What do you mean? Two identifiers, separated only by whitespace and no > keyword or operator? > > def foo(): > ? ? asdf > ? ? qwer > > Perfectly legal, two statements that probably don't do anything useful though. > > ChrisA Thats not a fair point, but more nitpicking. Yes, I should have been more precise: in python, 'whitespace' is not a single beast like in most languages, but newlines have a special meaning. I was obviously not talking about those. Want to try again? From k.sahithi2862 at gmail.com Sat Dec 24 12:33:28 2011 From: k.sahithi2862 at gmail.com (SAHITHI) Date: Sat, 24 Dec 2011 09:33:28 -0800 (PST) Subject: YOUTH SPECIAL HOT PICS Message-ID: FOR GOOD JOBS SITES TO YOU http://goodjobssites.blogspot.com/ BUSINESSMAN MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/businessman-movie-stills.html HOT ACTRESS BIKINI STILLS IN 2012 CALENDAR http://actressgallery-kalyani.blogspot.com/2011/12/2012-actress-calendar-wallpapers.html SAMANTHA SOUTHSCOPE HOT PHOTOS http://actressgallery-kalyani.blogspot.com/2011/12/samantha-at-south-scope-magazine.html DEEPIKA PADUKONE SPICY WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/deepika-padukone.html KATRINA KAIF LATEST HOT WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/katrina-kaif-wallpapers.html LOVE FAILURE MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/love-failure-movie-stills.html 4FRIENDS MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/4-friends-movie-stills.html NANDEESWARUDU MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/nandeeswarudu-movie-stills.html HARI PRIYA HOT PHOTO STILLS http://actressgallery-kalyani.blogspot.com/2011/12/haripariya-actress.html SHRUTI HASSAN HOT IN 3 MOVIE http://actressgallery-kalyani.blogspot.com/2011/11/shruti-hassan-in-3-movie.html PANJA MOVIE LATEST STILLS http://actressgallery-kalyani.blogspot.com/2011/11/panja-movie-stills.html NIPPU MOVIE WORKING STILLS http://actressgallery-kalyani.blogspot.com/2011/11/nippu-movie-stills.html FOR FAST UPDATES IN FILM INDUSTRY KATRINA KAIF RARE PHOTOS http://allyouwants.blogspot.com/2011/12/katrina-kaif.html KAJAL AGARWAL LATEST STILLS http://allyouwants.blogspot.com/2011/03/kajal-latest-stills.html PRIYANKA CHOPRA LATEST HOT PHOTOSHOOT http://allyouwants.blogspot.com/2011/08/priyanka-chopra.html TAMIL ACTRESS HOT PHOTOS&VIDEOS http://allyouwants.blogspot.com/2011/08/tamil-actress.html FOR ONLY HOT GUYS SEE THIS LATEST HOT KATRINA KAIF PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/katrina-kaif-hot.html ANUSHKA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/anushka-hot.html PRIYANKA TIWARI HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/12/priyanka-tiwari-hot.html TAMANNA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/tamanna-hot.html PARUL HOT PHOTO STILLS http://hotactress-kalyani.blogspot.com/2011/12/parul-hot.html ADITI AGARWAL NEW ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/12/aditi-agarwal-hot.html PAYAL GHOSH HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/payal-ghosh-hot.html RAGINI DWIVEDI HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/12/ragini-dwivedi.html PARVATHI MELTON LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/parvathi-melton-hot.html SARAH JANE DIAS HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/sarah-jane-dias-hot.html KAJAL AGARWAL HOT SAREE STILLS http://hotactress-kalyani.blogspot.com/2011/11/kajal-agarwal-hot-in-saree.html POONAM KAUR HOT ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/11/poonam-kaur-hot.html From rosuav at gmail.com Sat Dec 24 12:51:32 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Dec 2011 04:51:32 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Sun, Dec 25, 2011 at 4:23 AM, Eelco wrote: > Thats not a fair point, but more nitpicking. Yes, I should have been > more precise: in python, 'whitespace' is not a single beast like in > most languages, but newlines have a special meaning. I was obviously > not talking about those. Want to try again? In that case I can't think of any, but at least now you've clarified the question (by not denying the rest of it). It was somewhat ambiguous. ChrisA From rantingrickjohnson at gmail.com Sat Dec 24 13:22:52 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sat, 24 Dec 2011 10:22:52 -0800 (PST) Subject: How to check for single character change in a string? References: Message-ID: On Dec 24, 11:09?am, Arnaud Delobelle wrote: > sum(map(str.__ne__, str1, str2)) Mirror, mirror, on the wall. Who's the cleanest of them all? From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Sat Dec 24 13:41:55 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Sat, 24 Dec 2011 19:41:55 +0100 Subject: what does 'a=b=c=[]' do In-Reply-To: <4ef2704d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <4ef2704d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 22.12.2011 00:48 schrieb Steven D'Aprano: > On Wed, 21 Dec 2011 18:20:16 -0500, Dennis Lee Bieber wrote: > >> For the amount of typing, it's easier to just do a straight line >> tuple unpack >> >>>>> a,b,c = ([],[],[]) > > Note that tuples are created by the comma, not the round brackets (or > parentheses for any Americans reading). So the round brackets there are > strictly redundant: > > a, b, c = [], [], [] > > The only times you need the brackets around a tuple is to control the > precedence of operations, or for an empty tuple. IBTD: a=((a, b) for a, b, c in some_iter) b=[(1, c) for ] Without the round brackets, it is a syntax error. Thomas From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Sat Dec 24 13:45:39 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Sat, 24 Dec 2011 19:45:39 +0100 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: Am 22.12.2011 00:20 schrieb Dennis Lee Bieber: > The key one is that lists ([] defines a list, not an array) are > "mutable". Your "7" is not mutable. Strictly spoken, that's only a "side show" where the effect is visible. The real key concept is that [] creates *one* object which is then assigned to the three names. That's the same for mutable and immutable objects. But only the modification which happens on mutable objects turns it into a problem. The rest o your explanation is 100% correct. Thomas From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Sat Dec 24 13:49:53 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Sat, 24 Dec 2011 19:49:53 +0100 Subject: what does 'a=b=c=[]' do In-Reply-To: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: Am 21.12.2011 23:25 schrieb Eric: > Is it true that if I want to create an array or arbitrary size such > as: > for a in range(n): > x.append() > > I must do this instead? > x=[] > for a in range(n): > x.append() Of course - your x must exist before using it. > Now to my actual question. I need to do the above for multiple arrays > (all the same, arbitrary size). So I do this: > x=y=z=[] > for a in range(n): > x.append() > y.append() > z.append() > Also, is there a more pythonic way to do "x=[], y=[], z=[]"? You could do: def create_xyz(n): for a in range(n): yield , , \ ) x, y, z = zip(*create_xyz(11)) or, if you want x, y, z to be lists, x, y, z = [list(i) for i in zip(*create_xyz(11))] . Thomas From wolftracks at invalid.com Sat Dec 24 13:55:48 2011 From: wolftracks at invalid.com (W. eWatson) Date: Sat, 24 Dec 2011 10:55:48 -0800 Subject: Is my IDLE problem caused by .idlerc? Permissions. Message-ID: I installed 64-bit Python 2.7.2 on Win 7. IDLE does appear as the second choice on right-click of a py file menu. However, nothing happens if I use it. If I try to execute idle from the cmd window, it complains about .idlerc\recent-files.lst IOError: [Errno 13] Permission denied: 'C:\\Users\\Wayne\\.idlerc\\recent-files.lst' These are permissions for it. Permissions as follows: SYSTEM: All. From Full control to write Account Unknown(S-1-5-21...): read&exec, Read Wayne: (normal use) All. From Full control to write Admin: All. From Full control to write WMPNetwork: Read Comments? .idlerc permissions are slightly different for Account Unknown. From zyzhu2000 at gmail.com Sat Dec 24 16:19:44 2011 From: zyzhu2000 at gmail.com (GZ) Date: Sat, 24 Dec 2011 13:19:44 -0800 (PST) Subject: Generator Question References: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> <4ef2cfb2$0$13942$c3e8da3$76491128@news.astraweb.com> Message-ID: <85113506-abcb-4008-877a-c2c2a3fae2e6@k10g2000yqk.googlegroups.com> I see. Thanks for the clarification. On Dec 22, 12:35?am, Steven D'Aprano wrote: > On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: > > Now the question here is this: > > > def h(): > > ? ? if condition=true: > > ? ? ? ?#I would like to return an itereator with zero length > > ? ? else: > > ? ? ? ?for ...:yieldx > > > In other words, when certain condition is met, I want toyieldnothing. > > How to do? > > Actually, there's an even easier way. > > >>> def h(): > > ... ? ? if not condition: > ... ? ? ? ? for c in "abc": > ... ? ? ? ? ? ?yieldc > ... > > >>> condition = False > >>> list(h()) > ['a', 'b', 'c'] > >>> condition = True > >>> list(h()) > > [] > > -- > Steven > > From jeanpierreda at gmail.com Sat Dec 24 18:25:24 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sat, 24 Dec 2011 18:25:24 -0500 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: > If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd > see an increase in the number of questions like the OP's. What makes you so sure? Both models do make sense and are equally valid, it's just that only one of them is true. Is it just because people already used to Python would get confused? -- Devin On Sat, Dec 24, 2011 at 9:08 AM, alex23 wrote: > On Dec 24, 6:25?pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> > It's >> > much harder to figure out what's going wrong with an early-bound >> > mutable. >> >> Only for those who don't understand, or aren't thinking about, Python's >> object model. The behaviour of early-bound mutables is obvious and clear >> once you think about it, but it does require you to think about what's >> going on under the hood, so to speak. > > And here we've come full circle to the point of this thread. > > If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd > see an increase in the number of questions like the OP's. > -- > http://mail.python.org/mailman/listinfo/python-list From speeze.pearson at gmail.com Sat Dec 24 18:49:59 2011 From: speeze.pearson at gmail.com (Spencer Pearson) Date: Sat, 24 Dec 2011 15:49:59 -0800 (PST) Subject: Adding an interface to existing classes References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> Message-ID: <256b9bcf-aa08-4d3e-9928-8fa9d335f1ff@e8g2000prb.googlegroups.com> On Dec 23, 9:13?am, Terry Reedy wrote: > On 12/22/2011 3:21 AM, Spencer Pearson wrote: > > > I'm writing a geometry package, with Points and Lines and Circles and > > so on, and eventually I want to be able to draw these things on the > > screen. I have two options so far for how to accomplish this, but > > neither of them sits quite right with me, and I'd like the opinion of > > comp.lang.python's wizened elders. > > > Option 1. Subclassing. > > The most Pythonic way would seem to be writing subclasses for the > > things I want to display, adding a ".draw(...)" method to each one, > > like this: > > Option 2. A "draw" function, with a function dictionary. > > Option 3? Add a draw method to existing classes, rather than subclassing? > > -- > Terry Jan Reedy Thanks for the response! Do you mean something like this? class Point(GeometricObject): def intersect(self, other): ... def union(self, other): ... def draw(self, ...): ... I'd like to avoid this, because... well, what I want is a geometry package, and it seems to me that whistles and bells like GUI support ought to be confined to subpackages. I'd look at this and think, "the rest of the Point class deals with fundamental geometric reality. What's this GUI method doing mixed in with my beautiful mathematical purity?" Is this the wrong attitude to take? Or did you mean this? In file "geometry/gui.py": def draw_point(point, ...): ... Point.draw = draw_point I've never modified an existing class before, and I fear the unfamiliar. If that's what you meant... it's really an acceptable thing to do? It seems like somebody might see "some_point.draw(...)" and be confused by the method's absence in the Point class definition. (A hybrid approach would be to say "draw = geometry.gui.draw_point" inside the body of the Point class, setting the attribute there instead of in gui.py, but that makes me uneasy for the same reason as putting the function's full definition in the class.) Thanks again, -Spencer From wuwei23 at gmail.com Sat Dec 24 19:10:35 2011 From: wuwei23 at gmail.com (alex23) Date: Sat, 24 Dec 2011 16:10:35 -0800 (PST) Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 25, 9:25?am, Devin Jeanpierre wrote: > > If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd > > see an increase in the number of questions like the OP's. > > What makes you so sure? Both models do make sense and are equally > valid, it's just that only one of them is true. Is it just because > people already used to Python would get confused? Because I believe that the source of confusion has far more to do with mutable/immutable objects than with early/late binding. Masking or 'correcting' an aspect of Python's behaviour because novices make the wrong assumption about it just pushes the problem elsewhere and potentially makes the language inconsistent at the same time. From wuwei23 at gmail.com Sat Dec 24 19:24:07 2011 From: wuwei23 at gmail.com (alex23) Date: Sat, 24 Dec 2011 16:24:07 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> Message-ID: On Dec 25, 12:39?am, Eelco wrote: > This is really going to be the last time I waste any words on this Oh hey, don't feel you actually have to justify the bullshit you're talking for my sake. > In case of python, collection PACKING (not unpacking) is signaled by a > construct that can be viewed as a type constraint. _But no one does view it that way because it isn't_. No more so than [] taking a string separated list of arguments and return a list is a type constraint. _That's it's behaviour_. We have a language construct that returns a tuple, because in the context of what tuples are in Python, that makes sense. There are _plenty_ of such constructs. You have still yet to show what adding all of this ridiculous shit to a function signature provides that coercing the resulting tuple to your own type doesn't. > So here it is again, in terms every 5 year old can understand. Id like > to do the exact same thing python is already doing. Except with a few > more, and different symbols, to enable one to express a few different > variants of behavior. Clear enough? That you're a condescending douchebag with nothing of value to contribute? Crystal. From jeanpierreda at gmail.com Sat Dec 24 19:32:06 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sat, 24 Dec 2011 19:32:06 -0500 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: > Because I believe that the source of confusion has far more to do with > mutable/immutable objects than with early/late binding. Masking or > 'correcting' an aspect of Python's behaviour because novices make the > wrong assumption about it just pushes the problem elsewhere and > potentially makes the language inconsistent at the same time. That seems fairly silly -- foo.append(bar) obviously mutates _something_ . Certainly it wasn't the source of my confusion when I got caught on this. What makes you believe that the fundamental confusion is about mutability? (Also, if the change is applied everywhere, the language would not be inconsistent.) -- Devin On Sat, Dec 24, 2011 at 7:10 PM, alex23 wrote: > On Dec 25, 9:25?am, Devin Jeanpierre wrote: >> > If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd >> > see an increase in the number of questions like the OP's. >> >> What makes you so sure? Both models do make sense and are equally >> valid, it's just that only one of them is true. Is it just because >> people already used to Python would get confused? > > Because I believe that the source of confusion has far more to do with > mutable/immutable objects than with early/late binding. Masking or > 'correcting' an aspect of Python's behaviour because novices make the > wrong assumption about it just pushes the problem elsewhere and > potentially makes the language inconsistent at the same time. > -- > http://mail.python.org/mailman/listinfo/python-list From rantingrickjohnson at gmail.com Sat Dec 24 20:01:27 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sat, 24 Dec 2011 17:01:27 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> Message-ID: On Dec 24, 6:24?pm, alex23 wrote: > That you're a condescending douchebag with nothing of value to > contribute? > > Crystal. Take it from me Eelco. Once Alex drops into your thread and starts name calling, it's over my friend. From ian.g.kelly at gmail.com Sat Dec 24 20:16:42 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 24 Dec 2011 18:16:42 -0700 Subject: Adding an interface to existing classes In-Reply-To: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> Message-ID: On Thu, Dec 22, 2011 at 1:21 AM, Spencer Pearson wrote: > I see a problem with this, though. The intersection of two lines is > (usually) an object of type Point. Since DrawableLine inherits from > Line, this means that unless I redefine the "intersect" method in > DrawableLine, the intersection of two DrawableLines will be a Point > object, not a DrawablePoint. I don't want to saddle the user with the > burden of converting every method output into its corresponding > Drawable subclass, and I don't want to redefine every method to return > an instance of said subclass. I see no other options if I continue > down the subclassing path. Am I missing something? You could solve this with a factory. Instead of having Line.intersection() create a Point directly, have it call self.factory.create_point(). In the drawing module, replace the factory for the subclasses with one that creates drawable classes instead. > Option 2. A "draw" function, with a function dictionary. > This feels weird, but is fairly simple to write, use, and extend. We > have a module with a "draw_functions" dictionary that maps types onto > functions, and a "draw" function that just looks up the proper type in > the dictionary and calls the corresponding function. If you create > your own object, you can just add a new entry to the dictionary. The > implementation is simple enough to outline here: This will also work, but inheritance complicates things a little. Do you do a type equality check or an isinstance() check when looking up the type in the dictionary? If the former, then somebody who subclasses geometry.Line to create a GroovyLine subclass must add an entry for GroovyLine to the dictionary, even if the drawing code is the same. The drawing code is not inherited. But if you do an isinstance() check, then you need to be very careful about how you check it. If somebody creates a WavyLine subclass and registers a different drawing method, then you need to make sure the isinstance() check for the WavyLine implementation happens before the regular Line implementation, or the wrong drawing code may get invoked for WavyLine. Probably the easiest way to do this correctly is to follow the MRO that Python has conveniently already built for you. Something like: def draw(x, *args, **kwargs ): for class_ in type(x).__mro__: if class_ in draw_functions: draw_functions[class_](*args, **kwargs) break else: raise TypeError("don't know how to draw things of type " "{0}".format(type(x))) Cheers, Ian From rantingrickjohnson at gmail.com Sat Dec 24 20:24:14 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sat, 24 Dec 2011 17:24:14 -0800 (PST) Subject: Adding an interface to existing classes References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> Message-ID: <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> On Dec 22, 2:21?am, Spencer Pearson wrote: > I'm writing a geometry package, with Points and Lines and Circles and > so on, and eventually I want to be able to draw these things on the > screen. ...which is the main reason for creating a geometry package in the first place!. I mean, imaginary points and lines are great if you are a theoretical mathematician, but for the rest of us earthlings, plotted pixels are the "in thang" now-a-days. > I have two options so far for how to accomplish this, but > neither of them sits quite right with me, and I'd like the opinion of > comp.lang.python's wizened elders. > > Option 1. Subclassing. > The most Pythonic way would seem to be writing subclasses for the > things I want to display, adding a ".draw(...)" method to each one, > like this: > class DrawablePoint( geometry.Point ): > ? ? class draw( self, ... ): > ? ? ? ? ... "DrawablePoint"? I suppose there is an "ImaginaryPoint" somewhere in this API? Geez > When the time comes to draw things, I'll have some list of objects I > want drawn, and say > for x in to_draw: > ? ? x.draw(...) Seems reasonable. Not the fastest approach, but a good start for a very high level interface. > I see a problem with this, though. The intersection of two lines is > (usually) an object of type Point. Since DrawableLine inherits from > Line, Why the hell is "Drawable" inheriting from Line? I would think that a "Line" would be "Drawable" object and NOT vice-versa? Am i wrong? > this means that unless I redefine the "intersect" method in > DrawableLine, the intersection of two DrawableLines will be a Point > object, not a DrawablePoint. OMFG! > I don't want to saddle the user with the > burden of converting every method output into its corresponding > Drawable subclass, and I don't want to redefine every method to return > an instance of said subclass. I see no other options if I continue > down the subclassing path. Am I missing something? Yes, a proper object hierarchy and API it seems @_ at . Spencer, i would re-think this entire project from the beginning. You are trying to make an object out of everything. You don't need to make an object of EVERYTHING. Ask yourself, what are the most basic objects of a geometry library, and then report back to us your findings. PS: I prefer option1 for these things as the OOP paradigm fits nicely. I just hate to have modules of loose functions just lying about. From tjreedy at udel.edu Sat Dec 24 20:58:24 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 24 Dec 2011 20:58:24 -0500 Subject: Adding an interface to existing classes In-Reply-To: <256b9bcf-aa08-4d3e-9928-8fa9d335f1ff@e8g2000prb.googlegroups.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <256b9bcf-aa08-4d3e-9928-8fa9d335f1ff@e8g2000prb.googlegroups.com> Message-ID: On 12/24/2011 6:49 PM, Spencer Pearson wrote: > On Dec 23, 9:13 am, Terry Reedy wrote: >> On 12/22/2011 3:21 AM, Spencer Pearson wrote: >> >>> I'm writing a geometry package, with Points and Lines and Circles and >>> so on, and eventually I want to be able to draw these things on the >>> screen. I have two options so far for how to accomplish this, but >>> neither of them sits quite right with me, and I'd like the opinion of >>> comp.lang.python's wizened elders. >> >>> Option 1. Subclassing. >>> The most Pythonic way would seem to be writing subclasses for the >>> things I want to display, adding a ".draw(...)" method to each one, >>> like this: There are people who would advocate a Drawable base class with a virtual or abstract .draw method and that DrawablePoint, etc, inherit from Drawable and Point. >>> Option 2. A "draw" function, with a function dictionary. >> >> Option 3? Add a draw method to existing classes, rather than subclassing? > Thanks for the response! Do you mean something like this? > class Point(GeometricObject): > def intersect(self, other): > ... I am interpreting this to mean that you have a world coordinate system for instances that have location and size. > def union(self, other): > ... > def draw(self, ...): > ... Yes. I would consider that Option 0, the default, unless you have good reason to choose another. I would certainly include it on a list of options. > I'd like to avoid this, because... well, what I want is a geometry > package, and it seems to me that whistles and bells like GUI support > ought to be confined to subpackages. I'd look at this and think, "the > rest of the Point class deals with fundamental geometric reality. > What's this GUI method doing mixed in with my beautiful mathematical > purity?" By default, all Python objects have a text representation method. I do not see that giving all concrete geometric objects (with a location and size) a visual representation is much different. I would use drawing functions that accept the coordinates and distances of your geometry world and translate to low-level pixel functions for a particular gui system. I agree that your geometrical objects should not know about pixels, screens, windows, and aspect ratios. > Is this the wrong attitude to take? It depends on *your* goal and values. > Or did you mean this? > In file "geometry/gui.py": > def draw_point(point, ...): > ... > Point.draw = draw_point > > I've never modified an existing class before, and I fear the > unfamiliar. If that's what you meant... it's really an acceptable > thing to do? Yes, in my opinion. The advantage of this is putting all the draw methods together, and possibly having more than one one set. On the other hand, one needs to know the data attributes of each class to understand its draw method. > It seems like somebody might see "some_point.draw(...)" > and be confused by the method's absence in the Point class definition. With either suboption, you should put an abstract .draw method in the GeometricObject base class. I would look at various game, graph, geometry, and gui packages handle drawing for more ideas. -- Terry Jan Reedy From rustompmody at gmail.com Sat Dec 24 22:22:37 2011 From: rustompmody at gmail.com (rusi) Date: Sat, 24 Dec 2011 19:22:37 -0800 (PST) Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5198e7e2-859b-4894-8170-16ad35d67625@h7g2000prn.googlegroups.com> On Dec 25, 5:32?am, Devin Jeanpierre wrote: > alex23 wrote: > > Because I believe that the source of confusion has far more to do with > > mutable/immutable objects than with early/late binding. Masking or > > 'correcting' an aspect of Python's behaviour because novices make the > > wrong assumption about it just pushes the problem elsewhere and > > potentially makes the language inconsistent at the same time. Thats hitting the nail on the head. > > That seems fairly silly -- foo.append(bar) obviously mutates > _something_ . Certainly it wasn't the source of my confusion when I > got caught on this. What makes you believe that the fundamental > confusion is about mutability? The confusion is not about mutability. Its about mutability and parameter passing being non-orthogonal. > > (Also, if the change is applied everywhere, the language would not be > inconsistent.) Obviously that would depend on what the change is. From lie.1296 at gmail.com Sat Dec 24 23:12:27 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 25 Dec 2011 15:12:27 +1100 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/24/2011 07:25 PM, Steven D'Aprano wrote: >> I'd use a function attribute. >> >> def func(x, y=None): >> if y is None: >> y = func.default_y >> ... >> func.default_y = [] >> >> That's awkward only if you believe function attributes are awkward. > > I do. All you've done is move the default from *before* the function is > defined to *after* the function is defined, instead of keeping it in the > function definition. It's still separate, and if the function is renamed > your code stops working. In other words, it violates encapsulation of the > function. Although we can solve that (default being after the function is defined) using a simple decorator: def funcargs(**args): def __decorate_with_args(func): for k,v in args.items(): setattr(func, k, v) return func return __decorate_with_args Usage: @funcargs(foo=4) def bar(baz): return baz + bar.foo et voila, we had just reinvented early binding default argument, with a much uglier syntax. From ater1980 at gmail.com Sun Dec 25 01:43:36 2011 From: ater1980 at gmail.com (Alex Ter-Sarkissov) Date: Sun, 25 Dec 2011 19:43:36 +1300 Subject: installing matplotlib in MacOs 10.6.8. Message-ID: hi everyone, I run python 2.7.2. in Eclipse (recently upgraded from 2.6). I have a problem with installing matplotlib (I found the version for python 2.7. MacOs 10.3, no later versions). If I run python in terminal using arch -i386 python, and then from matplotlib.pylab import * and similar stuff, everything works fine. If I run python in eclipse or just without arch -i386, I can import matplotlib as from matplotlib import * but actually nothing gets imported. If I do it in the same way as above, I get the message no matching architecture in universal wrapper which means there's conflict of versions or something like that. I tried reinstalling the interpreter and adding matplotlib to forced built-ins, but nothing helped. For some reason I didn't have this problem with numpy and tkinter. Any suggestions are appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From zyzhu2000 at gmail.com Sun Dec 25 02:09:50 2011 From: zyzhu2000 at gmail.com (GZ) Date: Sat, 24 Dec 2011 23:09:50 -0800 (PST) Subject: Test None for an object that does not implement == Message-ID: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Hi, I run into a weird problem. I have a piece of code that looks like the following: f(...., a=None, c=None): assert (a==None)==(c==None) The problem is that == is not implemented sometimes for values in a and c, causing an exception NotImplementedError. I ended up doing assert (not a)==(not c), but I think this code has other issues, for example, when a=[] and c=['a'], the assertion will fail, although a is not None. So how do I reliably test if a value is None or not? Thanks, gz From no.email at nospam.invalid Sun Dec 25 02:28:00 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Sat, 24 Dec 2011 23:28:00 -0800 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: <7xaa6h6rgf.fsf@ruckus.brouhaha.com> GZ writes: > assert (a==None)==(c==None)... > So how do I reliably test if a value is None or not? Equality is the wrong comparison to start with. Use "a is None". From nobody at nowhere.com Sun Dec 25 04:38:07 2011 From: nobody at nowhere.com (Nobody) Date: Sun, 25 Dec 2011 09:38:07 +0000 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On Sat, 24 Dec 2011 23:09:50 -0800, GZ wrote: > I run into a weird problem. I have a piece of code that looks like the > following: > > f(...., a=None, c=None): > assert (a==None)==(c==None) > > > The problem is that == is not implemented sometimes for values in a > and c, causing an exception NotImplementedError. I have no idea how that can happen. If a.__eq__(None) returns NotImplemented, the interpreter should flip the test and perform the equivalent of None.__eq__(a), which will return False. > So how do I reliably test if a value is None or not? As Paul says, use "is" to check whether a value _is_ None. Checking for equality is almost certainly the wrong thing to do; nothing should compare equal to None except for None itself, so "x is None" and "x == None" shouldn't produce different results unless there's a bug in the comparison method. From lie.1296 at gmail.com Sun Dec 25 06:10:28 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 25 Dec 2011 22:10:28 +1100 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On 12/25/2011 08:38 PM, Nobody wrote: > nothing should compare equal to None except for None itself, so "x is None" > and "x == None" shouldn't produce different results unless there's a > bug in the comparison method. not necessarily, for example: import random class OddClass: def __eq__(self, other): return [True, False][random.randint(0, 1)] x = OddClass() print x == None print x == None print x == None print x == None print x == None Now, whether doing something like that is advisable or not, that's a different question; however nothing in python states that you couldn't have something that compare equal to None whether there is a bug or not in the comparison method. From mrinalini at edss.co.in Sun Dec 25 06:28:55 2011 From: mrinalini at edss.co.in (Mrinalini Kulkarni) Date: Sun, 25 Dec 2011 16:58:55 +0530 Subject: help - obtaining the type of the object using =?UTF-8?Q?tp=5Fname?= Message-ID: <440b1f62ce9f82adf5e6f17d9e520b5b@edss.co.in> Hello, I have embedded python into a vc++ app. I need to obtain type of the variable defined in the python script, in my c++ code. PyObject *pMyObject; -> assume points to a variable defined in the python script Now I want to do something like this const char * typeName; typeName = pMyObject->ob_type->tp_name Is this possible ? I tried doing this but it crashes. What is the alternative way for getting the name of the type from python. Is there a function available for this purpose such that I can obtain the address of that function using GetProcAddress and then use it for determining the type. (I am required to link to python dynamically) thanks, MK From steve+comp.lang.python at pearwood.info Sun Dec 25 07:10:40 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 12:10:40 GMT Subject: Adding an interface to existing classes References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> Message-ID: <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 17:24:14 -0800, Rick Johnson wrote: >> class DrawablePoint( geometry.Point ): >> ? ? class draw( self, ... ): >> ? ? ? ? ... > > "DrawablePoint"? I suppose there is an "ImaginaryPoint" somewhere in > this API? Geez No, but there's an abstract Point, which presumably refers to the geometric concept (hence the module, geometry) without concerning itself with such things as pixels, raster and vector output devices, screen resolutions, and all the other stuff which is needed for drawing points but not needed for working with points. [...] >> I see a problem with this, though. The intersection of two lines is >> (usually) an object of type Point. Since DrawableLine inherits from >> Line, > > Why the hell is "Drawable" inheriting from Line? I would think that a > "Line" would be "Drawable" object and NOT vice-versa? Am i wrong? Probably. I think there's a case for Drawable to be an abstract mixin class, so that DrawableLine inherits from both Line and Drawable. >> this means that unless I redefine the "intersect" method in >> DrawableLine, the intersection of two DrawableLines will be a Point >> object, not a DrawablePoint. Not if you define intersect in Point correctly in the first place. class Point: # An abstract class. def intersect(self, other): blah; blah; blah return Point(x, y) # No, wrong, bad!!! Don't do this. Instead: return self.__class__(x, y) # Better. > Spencer, i would re-think this entire project from the beginning. You > are trying to make an object out of everything. You don't need to make > an object of EVERYTHING. Very true. -- Steven From rosuav at gmail.com Sun Dec 25 07:32:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Dec 2011 23:32:41 +1100 Subject: Adding an interface to existing classes In-Reply-To: <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 25, 2011 at 11:10 PM, Steven D'Aprano wrote: > class Point: ?# An abstract class. > ? ?def intersect(self, other): > ? ? ? ?blah; blah; blah > ? ? ? ?return Point(x, y) ?# No, wrong, bad!!! Don't do this. > > Instead: > > ? ? ? ?return self.__class__(x, y) ?# Better. This would work if you were dealing with the intersection of two points, but how do you use that sort of trick for different classes? ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 25 07:45:22 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 12:45:22 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 06:54:07 -0800, Eelco wrote: > Context dependence is not something to be avoided at all costs, but all > else being equal, less is certainly more. The general concept of > grouping thing together which parenthesis is an extremely pervasive one > in programming, and thus deserves its own set of context-dependent > rules. Packing and unpacking collections is far, far more rare, Not in Python, where it is a very common idiom. > and thus > a form that requires you to write more but remember less is certainly > relatively favorable. Not in Python, where iteration is a fundamental idiom and packing/ unpacking is a basic syntax construct precisely because the designer of the language expects it to be common and encourages its use. There are built-in functions designed to be used with unpacking operations, e.g. enumerate and zip: for i, obj in enumerate(sequence): ... for a, b in zip(obj, range(100)): ... -- Steven From yasar11732 at gmail.com Sun Dec 25 07:45:26 2011 From: yasar11732 at gmail.com (=?utf-8?B?WWHFn2FyIEFyYWJhY8Sx?=) Date: Sun, 25 Dec 2011 14:45:26 +0200 Subject: help - obtaining the type of the object using tp_name In-Reply-To: <440b1f62ce9f82adf5e6f17d9e520b5b@edss.co.in> References: <440b1f62ce9f82adf5e6f17d9e520b5b@edss.co.in> Message-ID: I am not sure if I understood your question correctly, but I would advice checking this: http://docs.python.org/c-api/object.html#PyObject_Type Sun, 25 Dec 2011 13:28:55 +0200 tarihinde Mrinalini Kulkarni ??yle yazm??: > Hello, > > I have embedded python into a vc++ app. I need to obtain type of the > variable defined in the python script, in my c++ code. > > PyObject *pMyObject; -> assume points to a variable defined in the > python script > > Now I want to do something like this > > const char * typeName; > > typeName = pMyObject->ob_type->tp_name > > Is this possible ? I tried doing this but it crashes. > > What is the alternative way for getting the name of the type from > python. Is there a function available for this purpose such that I can > obtain the address of that function using GetProcAddress and then use > it for determining the type. (I am required to link to python > dynamically) > > thanks, > MK -- Opera'n?n e-posta istemcisi ile g?nderildi: http://www.opera.com/mail From steve+comp.lang.python at pearwood.info Sun Dec 25 07:50:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 12:50:21 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4ef71c0d$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 06:45:01 -0800, Eelco wrote: > Can you give an example of a construct in python where two whitespace > delimited identifiers are legal? Not apart from the trivial case of two identifiers separated by newlines. What's your point? -- Steven From yasar11732 at gmail.com Sun Dec 25 07:53:55 2011 From: yasar11732 at gmail.com (=?ISO-8859-9?Q?Ya=FEar_Arabac=FD?=) Date: Sun, 25 Dec 2011 14:53:55 +0200 Subject: help - obtaining the type of the object using tp_name In-Reply-To: <440b1f62ce9f82adf5e6f17d9e520b5b@edss.co.in> References: <440b1f62ce9f82adf5e6f17d9e520b5b@edss.co.in> Message-ID: And by the way, I would advice asking these kinds of questions in #python-dev IRC channel (Freenode). I believe you can get much better help about your problems regarding C-api there. 2011/12/25 Mrinalini Kulkarni > Hello, > > I have embedded python into a vc++ app. I need to obtain type of the > variable defined in the python script, in my c++ code. > > PyObject *pMyObject; -> assume points to a variable defined in the python > script > > Now I want to do something like this > > const char * typeName; > > typeName = pMyObject->ob_type->tp_name > > Is this possible ? I tried doing this but it crashes. > > What is the alternative way for getting the name of the type from python. > Is there a function available for this purpose such that I can obtain the > address of that function using GetProcAddress and then use it for > determining the type. (I am required to link to python dynamically) > > thanks, > MK > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://yasar.serveblog.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sun Dec 25 08:12:41 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:12:41 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> Message-ID: <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 06:39:39 -0800, Eelco wrote: > On Dec 20, 4:30?am, alex23 wrote: >> On Dec 19, 8:15?pm, Eelco wrote: >> >> > What does that have to do with collection packing/unpacking? >> >> It's mocking your insistance that collection unpacking is a type >> constraint. > > This is really going to be the last time I waste any words on this: If only that were true, but after sending this post, you immediately followed up with FIVE more posts on this subject in less than half an hour. > The sentence 'collection unpacking is a type constraint' is entirely > nonsensical. How true. Given that you have now acknowledged this fact, can you please stop insisting that collection unpacking is a type constraint? > A type constraint is a linguistical construct that can be > applied in many ways; typically, to narrow down the semantics of use of > the symbol to which the type constraint is applied. Traceback (most recent call last): RuntimeError: maximum recursion depth exceeded > In case of python, collection PACKING (not unpacking) is signaled by a > construct that can be viewed as a type constraint. Only by doing sufficient violence to the concept of type constraint that it could mean *anything*. Earlier, I insisted that a constraint is a rule that applies to input, not output. I haven't seen anyone, including yourself, dispute that. Normally we would say that in the statement: y = list(x) there is a constraint on x, namely that it is some sort of iterable object (otherwise an exception will be raised), but it would be an abuse of language to say that there is a type constraint on y. y ends up being a list, true, but that isn't a constraint on y, it is an outcome. In normal usage, "constraint" refers to pre-conditions, not post- conditions. There are no pre-conditions on y in the above. It may not even exist. Contrast it with this example: y += list(x) where there are constraints on y: it must exist, and it must be a list, or something which can be added to a list. -- Steven From steve+comp.lang.python at pearwood.info Sun Dec 25 08:13:36 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:13:36 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> Message-ID: <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 06:47:21 -0800, Eelco wrote: > I would like to be able to write something like: > > a, middle::tuple, b = ::sequence > > Where I would like the extra :: before the sequence to explicitly signal > collection unpacking on the rhs, to maintain the symmetry with > collection unpacking within a function call. The :: on the right-hand side is redundant, because the left-hand side already explicitly signals collection unpacking of the RHS. Requiring :: on the RHS above is as unnecessary as it would be here: n = len(::sequence) -- Steven From steve+comp.lang.python at pearwood.info Sun Dec 25 08:16:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:16:00 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <4ef2704d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef72210$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 19:41:55 +0100, Thomas Rachel wrote: >> The only times you need the brackets around a tuple is to control the >> precedence of operations, or for an empty tuple. > > IBTD: > > a=((a, b) for a, b, c in some_iter) > b=[(1, c) for ] > > Without the round brackets, it is a syntax error. Correction noted. Nevertheless, the parentheses don't create the tuple, the comma operator does. -- Steven From roy at panix.com Sun Dec 25 08:17:28 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 08:17:28 -0500 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: In article , Lie Ryan wrote: > Now, whether doing something like that is advisable or not, that's a > different question; however nothing in python states that you couldn't > have something that compare equal to None whether there is a bug or not > in the comparison method. Just for fun, I tried playing around with subclassing NoneType and writing an __eq__ for my subclass. Turns out, you can't do that: Traceback (most recent call last): File "./none.py", line 5, in class Nihil(NoneType): TypeError: Error when calling the metaclass bases type 'NoneType' is not an acceptable base type From steve+comp.lang.python at pearwood.info Sun Dec 25 08:27:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:27:49 GMT Subject: Adding an interface to existing classes References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef724d4$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 23:32:41 +1100, Chris Angelico wrote: > On Sun, Dec 25, 2011 at 11:10 PM, Steven D'Aprano > wrote: >> class Point: ?# An abstract class. >> ? ?def intersect(self, other): >> ? ? ? ?blah; blah; blah >> ? ? ? ?return Point(x, y) ?# No, wrong, bad!!! Don't do this. >> >> Instead: >> >> ? ? ? ?return self.__class__(x, y) ?# Better. > > This would work if you were dealing with the intersection of two points, > but how do you use that sort of trick for different classes? There's nothing in the above that assumes that other has the same type as self. It's just that the type of other is ignored, and the type of self always wins. I find that a nice, clear rule: x.intersection(y) always returns a point with the same type as x. If you want a more complicated rule, you have to code it yourself: def intersection(self, other): if issubclass(type(other), type(self)): kind = type(other) elif issubclass(type(self), type(other)): kind = AbstractPoint elif other.__class__ is UserPoint: kind = UserPoint elif today is Tuesday: kind = BelgiumPoint else: kind = self.__class__ return kind(x, y) -- Steven From roy at panix.com Sun Dec 25 08:30:46 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 08:30:46 -0500 Subject: Random string of digits? Message-ID: I want to create a string of 20 random digits (I'm OK with leading zeros). The best I came up with is: ''.join(str(random.randint(0, 9)) for i in range(20)) Is there something better? From rosuav at gmail.com Sun Dec 25 08:35:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:35:46 +1100 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith wrote: > Just for fun, I tried playing around with subclassing NoneType and > writing an __eq__ for my subclass. ?Turns out, you can't do that: > > Traceback (most recent call last): > ?File "./none.py", line 5, in > ? ?class Nihil(NoneType): > TypeError: Error when calling the metaclass bases > ? ?type 'NoneType' is not an acceptable base type Yes; unfortunately quite a few Python built-in classes can't be subclassed. It's an unfortunate fact of implementation, I think, rather than a deliberate rule. But then, what would you ever need to subclass None for, other than toys and testing? ChrisA From rosuav at gmail.com Sun Dec 25 08:37:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:37:22 +1100 Subject: Adding an interface to existing classes In-Reply-To: <4ef724d4$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef724d4$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 12:27 AM, Steven D'Aprano wrote: > There's nothing in the above that assumes that other has the same type as > self. It's just that the type of other is ignored, and the type of self > always wins. I find that a nice, clear rule: x.intersection(y) always > returns a point with the same type as x. The intersection of DrawableLine and DrawableLine is DrawablePoint. That's not the same type as either of the inputs. Same if you seek the intersection of two planes, which is a line - or two spheres, which is a circle (with possible failure if they don't intersect). ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 25 08:46:15 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:46:15 GMT Subject: Adding an interface to existing classes References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef724d4$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef72927$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 00:37:22 +1100, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 12:27 AM, Steven D'Aprano > wrote: >> There's nothing in the above that assumes that other has the same type >> as self. It's just that the type of other is ignored, and the type of >> self always wins. I find that a nice, clear rule: x.intersection(y) >> always returns a point with the same type as x. > > The intersection of DrawableLine and DrawableLine is DrawablePoint. > That's not the same type as either of the inputs. Same if you seek the > intersection of two planes, which is a line - or two spheres, which is a > circle (with possible failure if they don't intersect). class Line: intersection_kind = Point def intersection(self, other): blah() return self.intersection_kind(a, b) class DrawableLine(Line): intersection_kind = DrawablePoint -- Steven From steve+comp.lang.python at pearwood.info Sun Dec 25 08:48:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:48:16 GMT Subject: Random string of digits? References: Message-ID: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 08:30:46 -0500, Roy Smith wrote: > I want to create a string of 20 random digits (I'm OK with leading > zeros). The best I came up with is: > > ''.join(str(random.randint(0, 9)) for i in range(20)) > > Is there something better? '%20d' % random.randint(0, 10**20-1) -- Steven From steve+comp.lang.python at pearwood.info Sun Dec 25 08:48:27 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:48:27 GMT Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: <4ef729aa$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 00:35:46 +1100, Chris Angelico wrote: [...] >> TypeError: Error when calling the metaclass bases >> ? ?type 'NoneType' is not an acceptable base type > > Yes; unfortunately quite a few Python built-in classes can't be > subclassed. I can't think of any other un-subclassable classes other than NoneType. Which ones are you thinking of? -- Steven From rosuav at gmail.com Sun Dec 25 08:50:44 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:50:44 +1100 Subject: Random string of digits? In-Reply-To: References: Message-ID: On Mon, Dec 26, 2011 at 12:30 AM, Roy Smith wrote: > I want to create a string of 20 random digits (I'm OK with leading > zeros). ?The best I came up with is: > > ''.join(str(random.randint(0, 9)) for i in range(20)) > > Is there something better? The simple option is: random.randint(0,99999999999999999999) or "%020d"%random.randint(0,99999999999999999999) (the latter gives you a string, padded with leading zeroes). But I'm assuming that you discarded that option due to lack of entropy (ie you can't trust randint() over that huge a range). The way I'd do it would be in chunks. The simple option is one chunk; your original technique is twenty. We can go somewhere in between. First thing to do though: ascertain how far randint() is properly random. The Python 2 docs [1] say that the underlying random() function uses 53-bit floats, so you can probably rely on about that much randomness; for argument's sake, let's say it's safe for up to 10,000 but no further (although 53 bits give you about 15 decimal digits). ''.join('%04d'%random.randint(0,9999) for i in range(5)) For your actual task, I'd be inclined to take ten digits, twice, and not bother with join(): '%010d%010d'%(random.randint(0,9999999999),random.randint(0,9999999999)) Looks a little ugly, but it works! And only two random number calls (which can be expensive). ChrisA From rosuav at gmail.com Sun Dec 25 08:51:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:51:41 +1100 Subject: Random string of digits? In-Reply-To: References: Message-ID: On Mon, Dec 26, 2011 at 12:50 AM, Chris Angelico wrote: > The Python 2 docs [1] Forgot to provide link. Python 2: http://docs.python.org/library/random.html And same in Python 3: http://docs.python.org/py3k/library/random.html ChrisA From rosuav at gmail.com Sun Dec 25 08:54:40 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:54:40 +1100 Subject: Random string of digits? In-Reply-To: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 12:48 AM, Steven D'Aprano wrote: > On Sun, 25 Dec 2011 08:30:46 -0500, Roy Smith wrote: > >> I want to create a string of 20 random digits (I'm OK with leading >> zeros). ?The best I came up with is: >> >> ''.join(str(random.randint(0, 9)) for i in range(20)) >> >> Is there something better? > > '%20d' % random.randint(0, 10**20-1) I should mention that this sort of thing is absolutely acceptable in situations where you don't actually need that level of randomness; if you want to assign 20-digit numbers to each of ten million objects, you can happily use something that's only really random to fifteen. I use this technique with base 36 numbers to generate arbitrary alphanumeric strings to use as message tags, for instance; fill out a specified field size, even though not every combination will actually be used. ChrisA From rosuav at gmail.com Sun Dec 25 08:55:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:55:22 +1100 Subject: Adding an interface to existing classes In-Reply-To: <4ef72927$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef724d4$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef72927$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 12:46 AM, Steven D'Aprano wrote: > class DrawableLine(Line): > ? ?intersection_kind = DrawablePoint Ha! That works. I was sure there'd be some simple way to do it! ChrisA From rosuav at gmail.com Sun Dec 25 09:04:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 01:04:06 +1100 Subject: Test None for an object that does not implement == In-Reply-To: <4ef729aa$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> <4ef729aa$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 12:48 AM, Steven D'Aprano wrote: > I can't think of any other un-subclassable classes other than NoneType. > Which ones are you thinking of? I don't remember, but it was mentioned in a thread a little while ago. Experimentation shows that 'bool' is one of them, though. This may shed some light: >>> class Foo(type(iter)): pass Traceback (most recent call last): File "", line 1, in class Foo(type(iter)): TypeError: type 'builtin_function_or_method' is not an acceptable base type I think there are certain types that are actually not implemented as classes, and hence cannot be subclassed. This is almost certainly an implementation detail though; my testing was done in Py3.2 (on Windows fwiw). ChrisA From mohandayalank at gmail.com Sun Dec 25 09:10:09 2011 From: mohandayalank at gmail.com (D. Mohan M. Dayalan) Date: Sun, 25 Dec 2011 06:10:09 -0800 (PST) Subject: ckjdlkfnl,ndf,nfd,fndfnkdnk mmdlssdlndnll; k; as; lkds sjdsljdlskjdsl; kdslksdl; ddlk Message-ID: http;//123maza.com/48/moon670/ From roy at panix.com Sun Dec 25 09:13:09 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 09:13:09 -0500 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: In article , Chris Angelico wrote: > On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith wrote: > > Just for fun, I tried playing around with subclassing NoneType and > > writing an __eq__ for my subclass. ?Turns out, you can't do that: > > > > Traceback (most recent call last): > > ?File "./none.py", line 5, in > > ? ?class Nihil(NoneType): > > TypeError: Error when calling the metaclass bases > > ? ?type 'NoneType' is not an acceptable base type > > Yes; unfortunately quite a few Python built-in classes can't be > subclassed. It's an unfortunate fact of implementation, I think, > rather than a deliberate rule. > > But then, what would you ever need to subclass None for, other than > toys and testing? You might be to differentiate between temporary and permanent failures. Let's say you have a WidgetPool, containing Widgets of various classes. class WidgetPool: def get_widget(class_name): """Return a Widget of a given class. If there are no such Widgets available, returns None.""" [...] You might want to return a None subclass to signify, "No such Widgets are currently available, but they might be if you try again in a little while", as opposed to "No such Widgets will ever be available". If you were designing the interface from scratch, you would probably represent that with an exception hierarchy. However, if this was an old interface that you were modifying, this might be a way to return a richer failure indication for new clients without breaking backwards compatibility for existing code. Of course, the existing code would probably be using "is None" tests, and break anyway. But at least that's a plausible scenario for None subclasses. From roy at panix.com Sun Dec 25 09:21:34 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 09:21:34 -0500 Subject: Random string of digits? References: Message-ID: In article , Chris Angelico wrote: > "%020d"%random.randint(0,99999999999999999999) > (the latter gives you a string, padded with leading zeroes). But I'm > assuming that you discarded that option due to lack of entropy (ie you > can't trust randint() over that huge a range). Actually, the only entropy involved here is the ever increasing amount of it between my ears. It never occurred to me to try that :-) > For your actual task, I'd be inclined to take ten digits, twice, and > not bother with join(): > > '%010d%010d'%(random.randint(0,9999999999),random.randint(0,9999999999)) > > Looks a little ugly, but it works! And only two random number calls > (which can be expensive). Hmmm. In my case, I was looking more to optimize clarity of code, not speed. This is being used during account creation on a web site, so it doesn't get run very often. It turns out, I don't really need 20 digits. If I can count on >>> "%020d" % random.randint(0,999999999999999) to give me 15-ish digits, that's good enough for my needs and I'll probably go with that. Thanks. From rosuav at gmail.com Sun Dec 25 09:23:29 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 01:23:29 +1100 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 1:13 AM, Roy Smith wrote: > If you were designing the interface from scratch, you would probably > represent that with an exception hierarchy Or possibly with "returns a False value", giving the option of None for none available, False for none will ever be available. Of course, you then have to guarantee that your live return values will always boolify as True. ChrisA From hoogendoorn.eelco at gmail.com Sun Dec 25 09:45:01 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 25 Dec 2011 06:45:01 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> Message-ID: On Dec 25, 2:01?am, Rick Johnson wrote: > On Dec 24, 6:24?pm, alex23 wrote: > > > That you're a condescending douchebag with nothing of value to > > contribute? > > > Crystal. > > Take it from me Eelco. Once Alex drops into your thread and starts > name calling, it's over my friend. Yes, he has quite worn out my patience; whats over is our (attempts at) two sided communication, but I hope to continue the constructive lines of argument in this thread. From rosuav at gmail.com Sun Dec 25 09:51:30 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 01:51:30 +1100 Subject: Random string of digits? In-Reply-To: References: Message-ID: On Mon, Dec 26, 2011 at 1:21 AM, Roy Smith wrote: > It turns out, I don't really need 20 digits. ?If I can count on > >>>> "%020d" % random.randint(0,999999999999999) > > to give me 15-ish digits, that's good enough for my needs and I'll > probably go with that. ?Thanks. I'd say you can. The information about the Mersenne Twister underlying the module's functions isn't marked as "CPython Implementation Note", so I would expect that every Python implementation will provide a minimum of 53-bit precision. (It does have the feeling of an implementation detail though; is there a guarantee that two Pythons will generate the same sequence of numbers from the same seed?) ChrisA From hoogendoorn.eelco at gmail.com Sun Dec 25 09:55:28 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 25 Dec 2011 06:55:28 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> On Dec 25, 1:45?pm, Steven D'Aprano wrote: > On Sat, 24 Dec 2011 06:54:07 -0800, Eelco wrote: > > Context dependence is not something to be avoided at all costs, but all > > else being equal, less is certainly more. The general concept of > > grouping thing together which parenthesis is an extremely pervasive one > > in programming, and thus deserves its own set of context-dependent > > rules. Packing and unpacking collections is far, far more rare, > > Not in Python, where it is a very common idiom. I know we are talking about python; it was me that put that in the title, after all. I know python makes more use of this than some languages (and less than others; I wouldnt suggest such a verbose syntax for a functional language for instance). Anyway, braces are used at least an order of magnitude more than collection packing/ unpacking in typical code. From hoogendoorn.eelco at gmail.com Sun Dec 25 09:59:44 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 25 Dec 2011 06:59:44 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> <4ef71c0d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <77ed8395-54df-44a6-92c1-16cd7c8362da@z1g2000vbx.googlegroups.com> On Dec 25, 1:50?pm, Steven D'Aprano wrote: > On Sat, 24 Dec 2011 06:45:01 -0800, Eelco wrote: > > Can you give an example of a construct in python where two whitespace > > delimited identifiers are legal? > > Not apart from the trivial case of two identifiers separated by newlines. > > What's your point? My point is as I originally stated it: that this construct, of two identifiers seperated by non-newline whitespace, as in 'list tail' does not occur anywhere else in python, so introducing that syntax, while i suppose technically possible, would be a break with existing expectations. Normally speaking, if two identifiers interact, they are explicitly 'joined' by an infixed operator of some sort, as in 3*4, rather than * 3 4. That seems a sensible rule to me, and I see no compelling reason to depart from it. From arnodel at gmail.com Sun Dec 25 10:07:21 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sun, 25 Dec 2011 15:07:21 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> Message-ID: On Dec 25, 2011 2:55 PM, "Eelco" wrote: > > On Dec 25, 2:01 am, Rick Johnson wrote: > > On Dec 24, 6:24 pm, alex23 wrote: > > > > > That you're a condescending douchebag with nothing of value to > > > contribute? > > > > > Crystal. > > > > Take it from me Eelco. Once Alex drops into your thread and starts > > name calling, it's over my friend. > > Yes, he has quite worn out my patience; whats over is our (attempts > at) two sided communication, but I hope to continue the constructive > lines of argument in this thread. The real kiss of death is when you find yourself on the same side of the "argument" as ranting Rick. -- Arnaud -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sun Dec 25 10:21:49 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 25 Dec 2011 16:21:49 +0100 Subject: Random string of digits? References: Message-ID: Chris Angelico wrote: > On Mon, Dec 26, 2011 at 12:30 AM, Roy Smith wrote: >> I want to create a string of 20 random digits (I'm OK with leading >> zeros). The best I came up with is: >> >> ''.join(str(random.randint(0, 9)) for i in range(20)) >> >> Is there something better? > > The simple option is: > random.randint(0,99999999999999999999) > or > "%020d"%random.randint(0,99999999999999999999) > (the latter gives you a string, padded with leading zeroes). But I'm > assuming that you discarded that option due to lack of entropy (ie you > can't trust randint() over that huge a range). > > The way I'd do it would be in chunks. The simple option is one chunk; > your original technique is twenty. We can go somewhere in between. > First thing to do though: ascertain how far randint() is properly > random. The Python 2 docs [1] say that the underlying random() > function uses 53-bit floats, so you can probably rely on about that > much randomness; for argument's sake, let's say it's safe for up to > 10,000 but no further (although 53 bits give you about 15 decimal > digits). > > ''.join('%04d'%random.randint(0,9999) for i in range(5)) > > For your actual task, I'd be inclined to take ten digits, twice, and > not bother with join(): > > '%010d%010d'%(random.randint(0,9999999999),random.randint(0,9999999999)) > > Looks a little ugly, but it works! And only two random number calls > (which can be expensive). Judging from a quick look into the code (method Random._randbelow()) I'd say you don't need to do that unless you override Random.random() and not Random.getrandbits(). Even if you roll your own random() you'll get a warning when you run into the limit: >>> import random >>> random.randrange(10**20) 27709407700486201379L >>> class R(random.Random): ... def random(self): return 4 # * ... >>> R().randrange(10**20) /usr/lib/python2.6/random.py:253: UserWarning: Underlying random() generator does not supply enough bits to choose from a population range this large _warn("Underlying random() generator does not supply \n" 400000000000000000000L (*) According to the literature 4 is the best random number, 9 being the runner-up: http://www.googlefight.com/index.php?lang=en_GB&word1=random+number+dilbert&word2=random+number+xkcd A quick sanity check: >>> from collections import Counter >>> import random >>> Counter(str(random.randrange(10**10000))) Counter({'9': 1060, '6': 1039, '3': 1036, '8': 1007, '7': 997, '4': 977, '1': 976, '5': 976, '2': 970, '0': 962}) From steve+comp.lang.python at pearwood.info Sun Dec 25 10:27:37 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 15:27:37 GMT Subject: Random string of digits? References: Message-ID: <4ef740e8$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 01:51:30 +1100, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 1:21 AM, Roy Smith wrote: >> It turns out, I don't really need 20 digits. ?If I can count on >> >>>>> "%020d" % random.randint(0,999999999999999) >> >> to give me 15-ish digits, that's good enough for my needs and I'll >> probably go with that. ?Thanks. > > I'd say you can. The information about the Mersenne Twister underlying > the module's functions isn't marked as "CPython Implementation Note", so > I would expect that every Python implementation will provide a minimum > of 53-bit precision. (It does have the feeling of an implementation > detail though; is there a guarantee that two Pythons will generate the > same sequence of numbers from the same seed?) Yes. http://docs.python.org/dev/library/random.html#notes-on-reproducibility I think that's a new guarantee. Previously, the answer was only yes-ish: in theory, no guarantee was made, but in practice, you could normally rely on it. For example, when the Mersenne Twister became the default random number generator, the old generator, Wichman-Hill, was moved into its own module whrandom (deprecated in 2.4; now gone) for those who needed backwards compatibility. -- Steven From y.turgut at gmail.com Sun Dec 25 10:33:23 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Sun, 25 Dec 2011 07:33:23 -0800 (PST) Subject: Plot seems weird Message-ID: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> Hi all, I have a text file as following; 0.200047 0.000000 0.200053 0.160000 0.200059 0.000000 0.200065 0.080000 0.200072 0.000000 0.200078 0.160000 And I am trying to plot it with ; filenames = sys.argv[1:] if len(filenames) == 0: filenames = [sys.stdin] for filename in filenames: t,y1 = numpy.genfromtxt(filename, unpack=True) pyplot.plot(t,y1) pyplot.show() But graph seems weird, not as it supposed to be. Any ideas ? From hoogendoorn.eelco at gmail.com Sun Dec 25 10:38:17 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 25 Dec 2011 07:38:17 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> On Dec 25, 2:12?pm, Steven D'Aprano wrote: > On Sat, 24 Dec 2011 06:39:39 -0800, Eelco wrote: > > On Dec 20, 4:30?am, alex23 wrote: > >> On Dec 19, 8:15?pm, Eelco wrote: > > >> > What does that have to do with collection packing/unpacking? > > >> It's mocking your insistance that collection unpacking is a type > >> constraint. > > > This is really going to be the last time I waste any words on this: > > If only that were true, but after sending this post, you immediately > followed up with FIVE more posts on this subject in less than half an > hour. Did I waste any more words on collection packing and type constraints? No, I did not. (though I am about to, and am willing to do so for every question that seems genuinely aimed at engaging me on the matter) Did I intend to say that I was going to let a single troll shut down my entire topic? No, I did not. > > In case of python, collection PACKING (not unpacking) is signaled by a > > construct that can be viewed as a type constraint. > > Only by doing sufficient violence to the concept of type constraint that > it could mean *anything*. > > Earlier, I insisted that a constraint is a rule that applies to input, > not output. I haven't seen anyone, including yourself, dispute that. > > Normally we would say that in the statement: > > ? ? y = list(x) > > there is a constraint on x, namely that it is some sort of iterable > object (otherwise an exception will be raised), but it would be an abuse > of language to say that there is a type constraint on y. y ends up being > a list, true, but that isn't a constraint on y, it is an outcome. > > In normal usage, "constraint" refers to pre-conditions, not post- > conditions. There are no pre-conditions on y in the above. It may not > even exist. Contrast it with this example: > > ? ? y += list(x) > > where there are constraints on y: it must exist, and it must be a list, > or something which can be added to a list. Yes, indeed it would be abuse of language to call this a type constraint, since the fact that y is a list is indeed an outcome of whatever happens to pop out at the right hand side. One could redefine the identifier list to return any kind of object. How is 'head, *tail = sequence' or semantically entirely equivalently, 'head, tail::list = sequence' any different then? Of course after interpretation/compilation, what it boils down to is that we are constructing a list and binding it to the identifier tail, but that is not how it is formulated in python as a language (all talk of types is meaningless after compilation; machine code is untyped). We dont have something of the form 'tail = list_tail(sequence)'. Rather, we annotate the identifier 'tail' with an attribute that unquestionably destinates it to become a list*. It is no longer that 'tail' will just take anything that pops out of the expression on the right hand side; rather, the semantics of what will go on at right hand side is coerced by the constraint placed on 'tail'. But again, if you dont wish to view this as a type constraint, I wont lose any sleep over that. In that case this line of argument was simply never directed at you. It was directed at people who would reasonably argue that 'tail::tuple is a type constraint and thats unpythonic / type constraints have been considered and rejected'. If you dont think it looks like a type constraint: fine. The simpler argument is that whatever it is, its just a more verbose and flexible variant of a construct that python already has. *(I call that a 'type constraint', because that is what it literally is; if you can make a case that this term has acquired a different meaning in practice, and that there is another term in common use for this kind of construct; please enlighten me. Until that time, im going to ask you to take 'type constraint' by its literal meaning; a coercion of the type of a symbol, rather than whatever particular meaning it has acquired for you (it might help if you explained that). Im not sure if it was you that brought that up, but let me reiterate that I dont mean a 'type cast', which is a runtime concept. A 'type constraint' is purely a linguistic construct that will be 'compiled out') From steve+comp.lang.python at pearwood.info Sun Dec 25 10:46:58 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 15:46:58 GMT Subject: Random string of digits? References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 00:54:40 +1100, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 12:48 AM, Steven D'Aprano > wrote: >> On Sun, 25 Dec 2011 08:30:46 -0500, Roy Smith wrote: >> >>> I want to create a string of 20 random digits (I'm OK with leading >>> zeros). ?The best I came up with is: >>> >>> ''.join(str(random.randint(0, 9)) for i in range(20)) >>> >>> Is there something better? >> >> '%20d' % random.randint(0, 10**20-1) > > I should mention that this sort of thing is absolutely acceptable in > situations where you don't actually need that level of randomness; Use the Source, Luke, er, Chris :) If I've read the source correctly, randint() will generate sufficient bits of randomness to ensure that the entire int is random. http://hg.python.org/cpython/file/default/Lib/random.py -- Steven From hoogendoorn.eelco at gmail.com Sun Dec 25 10:47:20 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 25 Dec 2011 07:47:20 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 25, 2:13?pm, Steven D'Aprano wrote: > On Sat, 24 Dec 2011 06:47:21 -0800, Eelco wrote: > > I would like to be able to write something like: > > > a, middle::tuple, b = ::sequence > > > Where I would like the extra :: before the sequence to explicitly signal > > collection unpacking on the rhs, to maintain the symmetry with > > collection unpacking within a function call. > > The :: on the right-hand side is redundant, because the left-hand side > already explicitly signals collection unpacking of the RHS. Requiring :: > on the RHS above is as unnecessary as it would be here: Yes, it is redundant; hence the word 'extra' in my post. Explicit and implicit are not well-defined terms, but I would say that at the moment the signal is implicit, in the sense that one cannot see what is going on by considering the rhs in isolation. Normally in python, an assignment just binds the rhs to the identifiers on the lhs, but in case of collection (un)packing, this rule that holds almost all of the time is broken, and the assignment statement implies a far more complicated construct, with a far more subtle meaning, and non-constant time complexity. Thats not a terrible thing, but a little extra explicitness there would not hurt, and like I argued many times before, it is a nice unification with the situation where the unpacking can not be implicit, like inside a function call rather than assignment. > ? ? n = len(::sequence) Now you are just discrediting yourself in terms of having any idea what you are talking about. From rosuav at gmail.com Sun Dec 25 11:11:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 03:11:56 +1100 Subject: Random string of digits? In-Reply-To: <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 2:46 AM, Steven D'Aprano wrote: > Use the Source, Luke, er, Chris :) > > If I've read the source correctly, randint() will generate sufficient > bits of randomness to ensure that the entire int is random. > > http://hg.python.org/cpython/file/default/Lib/random.py I prefer not to rely on the source. That tells me what happens, not what's guaranteed to happen. However... bit of poking around can't hurt. That file doesn't actually justify anything, because random.Random() does not define getrandbits() - that, it seems, comes from _random(); turns out that getrandbits is actually doing pretty much the same thing I suggested: http://hg.python.org/cpython/file/745f9fd9856d/Modules/_randommodule.c#l371 Need a 64-bit random number? Take two 32-bit numbers and concatenate. So, it's going to be easier and clearer to just take the simple option, since it's actually doing the same thing underneath anyway. ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 25 11:15:25 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 16:15:25 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> Message-ID: <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 06:55:28 -0800, Eelco wrote: > Anyway, braces are used at > least an order of magnitude more than collection packing/ unpacking in > typical code. That's a wild and unjustified claim. Here's a quick and dirty test, using the standard library as an example of typical idiomatic code: [steve at orac ~]$ cd /usr/lib/python2.6 [steve at orac python2.6]$ grep "[*]args" *.py | wc -l 270 [steve at orac python2.6]$ grep "{" *.py | wc -l 550 Doesn't look like a factor of 10 difference to me. And from one of my projects: [steve at orac src]$ grep "[*]args" *.py | wc -l 267 [steve at orac src]$ grep "{" *.py | wc -l 8 -- Steven From rosuav at gmail.com Sun Dec 25 11:23:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 03:23:06 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 2:38 AM, Eelco wrote: > Until that time, im going > to ask you to take 'type constraint' by its literal meaning; a > coercion of the type of a symbol, rather than whatever particular > meaning it has acquired for you (it might help if you explained that). > Im not sure if it was you that brought that up, but let me reiterate > that I dont mean a 'type cast', which is a runtime concept. A 'type > constraint' is purely a linguistic construct that will be 'compiled > out') The dictionary definition of constraint is "a limitation or restriction", and you're right that it can be "compiled out". In fact, that is probably the best definition. Assuming everything is written correctly, you should be able to eliminate all constraints and the code will still function correctly*; but having the constrains means that certain illegal operations will throw errors. Here's two examples of tuple unpacking, one with a type constraint, the other without: a, b = ('hello', [1,2,3] ) a, b::list = ('hello', [1,2,3] ) The constraint on the second line means that, if the second element is not a list, the interpreter should throw an error. It does NOT mean to completely change the meaning of the statement to _make_ the last argument into a list. That is not the job of a constraint. ChrisA * In databasing, it's not uncommon to have code depend on error responses for correct operation; for instance, one implementation of UPSERT is to attempt an INSERT, and if it fails due to a unique key constraint, do the UPDATE instead. The same is also done in Python - eg using an exception to terminate a loop - but in the context of this discussion, assume that errors indicate errors. From steve+comp.lang.python at pearwood.info Sun Dec 25 12:05:39 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 17:05:39 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> Message-ID: <4ef757e3$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 07:38:17 -0800, Eelco wrote: > On Dec 25, 2:12?pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Sat, 24 Dec 2011 06:39:39 -0800, Eelco wrote: >> > On Dec 20, 4:30?am, alex23 wrote: >> >> On Dec 19, 8:15?pm, Eelco wrote: >> >> >> > What does that have to do with collection packing/unpacking? >> >> >> It's mocking your insistance that collection unpacking is a type >> >> constraint. >> >> > This is really going to be the last time I waste any words on this: >> >> If only that were true, but after sending this post, you immediately >> followed up with FIVE more posts on this subject in less than half an >> hour. > > Did I waste any more words on collection packing and type constraints? > No, I did not. (though I am about to, and am willing to do so for every > question that seems genuinely aimed at engaging me on the matter) > > Did I intend to say that I was going to let a single troll shut down my > entire topic? No, I did not. Ah, well whatever you *intended* wasn't clear from your comment. At least not clear to *me*. [...] > Yes, indeed it would be abuse of language to call this a type > constraint, since the fact that y is a list is indeed an outcome of > whatever happens to pop out at the right hand side. One could redefine > the identifier list to return any kind of object. So far, we agree on this. > How is 'head, *tail = sequence' or semantically entirely equivalently, > 'head, tail::list = sequence' any different then? Of course after > interpretation/compilation, what it boils down to is that we are > constructing a list and binding it to the identifier tail, but that is > not how it is formulated in python as a language I'm afraid it is. Here's the definition of assignment in Python 3: http://docs.python.org/py3k/reference/simple_stmts.html#assignment- statements > (all talk of types is > meaningless after compilation; machine code is untyped). What does machine code have to do with Python? > We dont have > something of the form 'tail = list_tail(sequence)'. I'm afraid we do. See the definition of assignment again. > Rather, we annotate > the identifier 'tail' with an attribute that unquestionably destinates > it to become a list*. It is no longer that 'tail' will just take > anything that pops out of the expression on the right hand side; Of course it will. Python is a dynamically typed language. It doesn't suddenly develop static types to ensure that 'tail' becomes a list; 'tail' is bound to a list because that's what the assignment statement provides. > rather, > the semantics of what will go on at right hand side is coerced by the > constraint placed on 'tail'. But it isn't a constraint placed on 'tail'. It is a consequence of the definition of assignment in Python 3. 'tail' becomes bound to a list because that is what the assignment statement is defined to do in that circumstance, not because the identifier (symbol) 'tail' is constrained to only accept lists. 'tail' may not even exist before hand, so talking about constraints on 'tail' is an abuse of language, AS YOU AGREED ABOVE. [...] > *(I call that a 'type constraint', because that is what it literally is; No. It is literally a name binding of a dynamically typed, unconstrained name to an object which happens to be a list. > if you can make a case that this term has acquired a different meaning > in practice, and that there is another term in common use for this kind > of construct; please enlighten me. Until that time, im going to ask you > to take 'type constraint' by its literal meaning; a coercion of the type > of a symbol, But THERE IS NO COERCION OF THE TYPE OF THE SYMBOL. I am sorry for shouting, but you seem oblivious to the simple fact that Python is not statically typed, and the symbol 'tail' is NOT coerced to a specific type. 'tail' may not even exist before the assignment is made; if it does exist, it could be *any type at all* -- and after the assignment takes place, there are no restrictions on subsequent assignments. 'head, *tail = sequence' is no more a type constraint than 'x = 1' is. Whatever the virtues of your proposal, you are doing it incalculable harm by your insistence on this incorrect model of Python's behaviour. -- Steven From rantingrickjohnson at gmail.com Sun Dec 25 12:06:57 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 25 Dec 2011 09:06:57 -0800 (PST) Subject: Plot seems weird References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> Message-ID: On Dec 25, 9:33?am, Yigit Turgut wrote: > Hi all, > > I have a text file as following; > > 0.200047 ? ? ? ?0.000000 > 0.200053 ? ? ? ?0.160000 > 0.200059 ? ? ? ?0.000000 > 0.200065 ? ? ? ?0.080000 > 0.200072 ? ? ? ?0.000000 > 0.200078 ? ? ? ?0.160000 > > And I am trying to plot it with ; > > filenames = sys.argv[1:] > if len(filenames) == 0: > ? ? filenames = [sys.stdin] > for filename in filenames: > ? ? t,y1 = numpy.genfromtxt(filename, unpack=True) > ? ? pyplot.plot(t,y1) > ? ? pyplot.show() > > But graph seems weird, not as it supposed to be. Any ideas ? Interesting. Of course "weird" leaves a LOT to be desired. On a scale of 1-10, how "weird" is the result? But seriously. Have you tried debugging yet? If not, test these points: * What is the value of "filenames" BEFORE the loop? * What is the value of "t" and "y1" for each iteration? Also observe this wonderful phenomenon: py> [] or [1,2,3] [1, 2, 3] py> [] or None or '' or () or {} or [1,2,3] or "1,2,3" [1, 2, 3] From lie.1296 at gmail.com Sun Dec 25 12:13:53 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 26 Dec 2011 04:13:53 +1100 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On 12/26/2011 01:13 AM, Roy Smith wrote: > In article, > Chris Angelico wrote: > >> On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith wrote: >>> Just for fun, I tried playing around with subclassing NoneType and >>> writing an __eq__ for my subclass. Turns out, you can't do that: >>> >>> Traceback (most recent call last): >>> File "./none.py", line 5, in >>> class Nihil(NoneType): >>> TypeError: Error when calling the metaclass bases >>> type 'NoneType' is not an acceptable base type >> >> Yes; unfortunately quite a few Python built-in classes can't be >> subclassed. It's an unfortunate fact of implementation, I think, >> rather than a deliberate rule. >> >> But then, what would you ever need to subclass None for, other than >> toys and testing? > > You might be to differentiate between temporary and permanent failures. > Let's say you have a WidgetPool, containing Widgets of various classes. > > class WidgetPool: > def get_widget(class_name): > """Return a Widget of a given class. If there are no such > Widgets available, returns None.""" > [...] > > You might want to return a None subclass to signify, "No such Widgets > are currently available, but they might be if you try again in a little > while", as opposed to "No such Widgets will ever be available". > > If you were designing the interface from scratch, you would probably > represent that with an exception hierarchy. However, if this was an old > interface that you were modifying, this might be a way to return a > richer failure indication for new clients without breaking backwards > compatibility for existing code. > > Of course, the existing code would probably be using "is None" tests, > and break anyway. But at least that's a plausible scenario for None > subclasses. That scenario doesn't actually need subclassing if you duck type. From steve+comp.lang.python at pearwood.info Sun Dec 25 12:18:53 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 17:18:53 GMT Subject: Random string of digits? References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 03:11:56 +1100, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 2:46 AM, Steven D'Aprano > wrote: >> Use the Source, Luke, er, Chris :) >> >> If I've read the source correctly, randint() will generate sufficient >> bits of randomness to ensure that the entire int is random. >> >> http://hg.python.org/cpython/file/default/Lib/random.py > > I prefer not to rely on the source. That tells me what happens, not > what's guaranteed to happen. In this case, the source explicitly tells you that the API includes support for arbitrary large ranges if you include a getrandbits() method: Optionally, implement a getrandbits() method so that randrange() can cover arbitrarily large ranges. I call that a pretty strong guarantee. > However... bit of poking around can't hurt. > That file doesn't actually justify anything, because random.Random() > does not define getrandbits() - that, it seems, comes from _random(); > turns out that getrandbits is actually doing pretty much the same thing > I suggested: > > http://hg.python.org/cpython/file/745f9fd9856d/Modules/ _randommodule.c#l371 > > Need a 64-bit random number? Take two 32-bit numbers and concatenate. > So, it's going to be easier and clearer to just take the simple option, > since it's actually doing the same thing underneath anyway. Um, I'm not sure what you consider "the simple option" in this context. I would hope you mean to use the high level API of randint: # need a random number with exactly 20 decimal digits random.randint(10**20, 10**21-1) rather than manually assembling a 20 digit number from smaller pieces. -- Steven From storchaka at gmail.com Sun Dec 25 12:32:27 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Sun, 25 Dec 2011 19:32:27 +0200 Subject: Random string of digits? In-Reply-To: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: 25.12.11 15:48, Steven D'Aprano ???????(??): > On Sun, 25 Dec 2011 08:30:46 -0500, Roy Smith wrote: >> I want to create a string of 20 random digits (I'm OK with leading >> zeros). The best I came up with is: >> ''.join(str(random.randint(0, 9)) for i in range(20)) >> Is there something better? > '%20d' % random.randint(0, 10**20-1) '%020d' % random.randrange(10**20) From roy at panix.com Sun Dec 25 12:41:29 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 12:41:29 -0500 Subject: Random string of digits? References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, 26 Dec 2011 03:11:56 +1100, Chris Angelico wrote: > > I prefer not to rely on the source. That tells me what happens, not > > what's guaranteed to happen. Steven D'Aprano wrote: > In this case, the source explicitly tells you that the API includes > support for arbitrary large ranges if you include a getrandbits() method: > > Optionally, implement a getrandbits() method so that randrange() > can cover arbitrarily large ranges. > > I call that a pretty strong guarantee. I think you mis-understood Chris's point. The documentation is the specification of how something behaves. If the documentation doesn't say it, you can't rely on it. The user should never have to read the source to know how to use a function, or what they can depend on. Now, I'm not saying that reading the source isn't useful for a deeper understanding, but it should be understood that any insights you glean from doing that are strictly implementation details. If you're saying that there are guarantees made by the implementation of getrandbits() which are not documented, then one of two things are true: 1) It is intended that users can depend on that behavior, in which case it's a bug in the docs, and the docs should be updated. or 2) It is not intended that users can depend on that behavior, in which case they would be foolish to do so. From rantingrickjohnson at gmail.com Sun Dec 25 12:44:33 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 25 Dec 2011 09:44:33 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> On Dec 19, 9:51?pm, Raymond Hettinger wrote: > Do you use IDLE when teaching Python? > If not, what is the tool of choice? I believe IDLE has the potential to be a very useful teaching tool and even in it's current abysmal state, i find it to be quite useful. > Students may not be experienced with the command-line and may be > running Windows, Linux, or Macs. ?Ideally, the tool or IDE will be > easy to install and configure (startup directory, path, associated > with a particular version of Python etc). Why install an IDE when IDLE is already there? Oh, yes, IDLE SUCKS. I know that already. But this revelation begs the question... Why has this community allowed IDLE to rot? Why has guido NOT started a public discussion on the matter? > Though an Emacs user myself, I've been teaching with IDLE because it's > free; it runs on multiple OSes, it has tooltips and code colorization > and easy indent/dedent/comment/uncomment commands, it has tab > completion; it allows easy editing at the interactive prompt; it has > an easy run-script command (F5); it has direct access to source code > (File OpenModule) and a class browser (Cntl+B). Yes, IDLE has all the basic tools anyone would need. Some people complain about a debugger, but i never use a debugger anyway. I feel debuggers just wreaken your debugging skills. > On the downside, some python distros aren't built with the requisite > Tcl/Tk support; And who's fault is that? > some distros like the Mac OS ship with a broken Tcl/Tk > so users have to install a fix to that as well; and IDLE sometimes > just freezes for no reason. And who's fault is that? > ?[IDLE] also doesn't have an easy way to > specify the startup directory. Are you kidding me? That could be fixed so easily! > If your goal is to quickly get new users up and running in Python, > what IDE or editor do you recommend? IDLE, of course. But NOT in its current state. Why would myself (or anyone) go to the trouble of downloading third party IDEs when IDLE is just waiting there for us to use? I for one, like to use tools that have open source code. And what is a better Python IDE than a Python IDE written in PYTHON? I ask ya? Also, what is the purpose of this thread Raymond? Are you (and others) considering removing IDLE from the source distro? You know. Many folks in this community have known for a long time how much i love IDLE, but at the same time how much i loath it's atrocious code base. I also know for a fact, that many "movers and shakers" within this community simultaneously use IDLE, and want to see IDLE code improved. However. None of these fine folks have taken the time to contact me privately so we can discuss such an evolution. Why is that? It boggles the mind really. From joshua.landau.ws at gmail.com Sun Dec 25 12:45:09 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Sun, 25 Dec 2011 17:45:09 +0000 Subject: Random string of digits? In-Reply-To: References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 25 December 2011 17:32, Serhiy Storchaka wrote: > 25.12.11 15:48, Steven D'Aprano ???????(??): > > On Sun, 25 Dec 2011 08:30:46 -0500, Roy Smith wrote: >> >>> I want to create a string of 20 random digits (I'm OK with leading >>> zeros). The best I came up with is: >>> ''.join(str(random.randint(0, 9)) for i in range(20)) >>> Is there something better? >>> >> '%20d' % random.randint(0, 10**20-1) >> > > '%020d' % random.randrange(10**20) Do the docs not advise .format now? "{:20}".format(random.randrange(10**20)) -------------- next part -------------- An HTML attachment was scrubbed... URL: From y.turgut at gmail.com Sun Dec 25 13:27:20 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Sun, 25 Dec 2011 10:27:20 -0800 (PST) Subject: Plot seems weird References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> Message-ID: <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> On Dec 25, 7:06?pm, Rick Johnson wrote: > On Dec 25, 9:33?am, Yigit Turgut wrote: > > Hi all, > > > I have a text file as following; > > > 0.200047 ? ? ? ?0.000000 > > 0.200053 ? ? ? ?0.160000 > > 0.200059 ? ? ? ?0.000000 > > 0.200065 ? ? ? ?0.080000 > > 0.200072 ? ? ? ?0.000000 > > 0.200078 ? ? ? ?0.160000 > > > And I am trying to plot it with ; > > > filenames = sys.argv[1:] > > if len(filenames) == 0: > > ? ? filenames = [sys.stdin] > > for filename in filenames: > > ? ? t,y1 = numpy.genfromtxt(filename, unpack=True) > > ? ? pyplot.plot(t,y1) > > ? ? pyplot.show() > > > But graph seems weird, not as it supposed to be. Any ideas ? > > Interesting. Of course "weird" leaves a LOT to be desired. On a scale > of 1-10, how "weird" is the result? I apply a 1Khz test signal just to see if things run smoothly, but I see spikes at lower and higher ends (logic 0,1) where I should see a clean rectangle pwm signal. By the look of it I say weirdness is around 3/10. > > But seriously. Have you tried debugging yet? If not, test these > points: Yes I double checked it, there seems to be nothing wrong in debug. > ?* What is the value of "filenames" BEFORE the loop? Filename is argument 1 of the startup action. > ?* What is the value of "t" and "y1" for each iteration? I test with both low and mid frequency signals (50Hz - 1Khz), same inconsistency. > > Also observe this wonderful phenomenon: > > py> [] or [1,2,3] > [1, 2, 3] > py> [] or None or '' or () or {} or [1,2,3] or "1,2,3" > [1, 2, 3] Beautiful. I convert my arrays to string before writing to file. Original post contains a fragment of the whole file. Data is fluctuating, not a linear behavior. From dihedral88888 at googlemail.com Sun Dec 25 14:50:03 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 25 Dec 2011 11:50:03 -0800 (PST) Subject: Random string of digits? In-Reply-To: References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20158346.705.1324842603361.JavaMail.geo-discussion-forums@prfi34> Roy Smith? 2011?12?26????UTC+8??1?41?29???? > On Mon, 26 Dec 2011 03:11:56 +1100, Chris Angelico wrote: > > > I prefer not to rely on the source. That tells me what happens, not > > > what's guaranteed to happen. > > Steven D'Aprano wrote: > > In this case, the source explicitly tells you that the API includes > > support for arbitrary large ranges if you include a getrandbits() method: > > > > Optionally, implement a getrandbits() method so that randrange() > > can cover arbitrarily large ranges. > > > > I call that a pretty strong guarantee. > > I think you mis-understood Chris's point. The documentation is the > specification of how something behaves. If the documentation doesn't > say it, you can't rely on it. The user should never have to read the > source to know how to use a function, or what they can depend on. Now, > I'm not saying that reading the source isn't useful for a deeper > understanding, but it should be understood that any insights you glean > from doing that are strictly implementation details. > > If you're saying that there are guarantees made by the implementation of > getrandbits() which are not documented, then one of two things are true: > > 1) It is intended that users can depend on that behavior, in which case > it's a bug in the docs, and the docs should be updated. > > or > > 2) It is not intended that users can depend on that behavior, in which > case they would be foolish to do so. Random bit generations for RSA2048 encoding and cryptography applications in python is simple and elegant. From dihedral88888 at googlemail.com Sun Dec 25 14:58:28 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 25 Dec 2011 11:58:28 -0800 (PST) Subject: RSA and Cryptography in PYTHON Message-ID: <31261167.63.1324843108857.JavaMail.geo-discussion-forums@prdv33> Long integer is really excellent in Python. Encoding RSA 2048bits in a simple and elegant way in Python is almost trivial. How about the nontrivial decoding part ? From orgnut at yahoo.com Sun Dec 25 18:45:10 2011 From: orgnut at yahoo.com (Larry Hudson) Date: Sun, 25 Dec 2011 15:45:10 -0800 Subject: Test None for an object that does not implement == In-Reply-To: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On 12/24/2011 11:09 PM, GZ wrote: > Hi, > > I run into a weird problem. I have a piece of code that looks like the > following: > > f(...., a=None, c=None): > assert (a==None)==(c==None) > <...> At first glance this looked like it should be a simple boolean "and", but then I realized that when a and c are both unequal to None, the result would also be True. This implies the logical approach would be exclusive-or (^). Try this expression: not ((a==None) ^ (c==None)) OTOH, if what you really want is simply to check that both are None (my first impression), then it's simply: (a==None) and (c==None) Most of the replies you're getting here seem unnecessarily complicated. > Thanks, > gz -=- Larry -=- From jeanpierreda at gmail.com Sun Dec 25 19:06:50 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sun, 25 Dec 2011 19:06:50 -0500 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: > At first glance this looked like it should be a simple boolean "and", but > then I realized that when a and c are both unequal to None, the result would > also be True. This implies the logical approach would be exclusive-or (^). Among booleans, "!=" is exclusive or and "==" is its negation. I don't see the point of complicating the formula to use ^. (related: <= is implication. Which is sad, because the arrow points the wrong way!) The issue here is that "== None" is being used instead of "is None", but I believe that's been covered. Your response doesn't include it, so maybe it's worth restating. -- Devin On Sun, Dec 25, 2011 at 6:45 PM, Larry Hudson wrote: > On 12/24/2011 11:09 PM, GZ wrote: >> >> Hi, >> >> I run into a weird problem. I have a piece of code that looks like the >> following: >> >> f(...., a=None, c=None): >> ? ? assert ?(a==None)==(c==None) >> > <...> > > At first glance this looked like it should be a simple boolean "and", but > then I realized that when a and c are both unequal to None, the result would > also be True. ?This implies the logical approach would be exclusive-or (^). > ?Try this expression: > > ? ? not ((a==None) ^ (c==None)) > > OTOH, if what you really want is simply to check that both are None (my > first impression), then it's simply: > > ? ? (a==None) and (c==None) > > Most of the replies you're getting here seem unnecessarily complicated. > >> Thanks, >> gz > > > ? ?-=- Larry -=- > -- > http://mail.python.org/mailman/listinfo/python-list From roy at panix.com Sun Dec 25 19:27:35 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 19:27:35 -0500 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: In article , Devin Jeanpierre wrote: > The issue here is that "== None" is being used instead of "is None", > but I believe that's been covered. Your response doesn't include it, > so maybe it's worth restating. Which of course leads to a SillyPEP for a new keyword, "are", which would allow you to write: >>> a and c are None instead of the much more verbose >>> a is None and c is None From lists at cheimes.de Sun Dec 25 19:37:03 2011 From: lists at cheimes.de (Christian Heimes) Date: Mon, 26 Dec 2011 01:37:03 +0100 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> <4ef729aa$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 25.12.2011 15:04, schrieb Chris Angelico: > I think there are certain types that are actually not implemented as > classes, and hence cannot be subclassed. This is almost certainly an > implementation detail though; my testing was done in Py3.2 (on Windows > fwiw). Some extension types are not subclass-able on purpose. The feature isn't available to heap types. http://docs.python.org/c-api/typeobj.html#Py_TPFLAGS_BASETYPE Most people have never noticed the deliberate limitation because only a few special types are sub-classable. Christian From drsalists at gmail.com Sun Dec 25 19:49:56 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Sun, 25 Dec 2011 16:49:56 -0800 Subject: How safe is this way of getting a default that allows None? Message-ID: How safe is this? I like the idea. #!/usr/bin/python UNSPECIFIED = object() def fn(x, y=UNSPECIFIED): if y is UNSPECIFIED: print x, 'default' else: print x, y fn(0, 1) fn(0, None) fn(0) -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sun Dec 25 19:52:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 Dec 2011 00:52:16 GMT Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: <4ef7c540$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 15:45:10 -0800, Larry Hudson wrote: > On 12/24/2011 11:09 PM, GZ wrote: >> Hi, >> >> I run into a weird problem. I have a piece of code that looks like the >> following: >> >> f(...., a=None, c=None): >> assert (a==None)==(c==None) >> > <...> > > At first glance this looked like it should be a simple boolean "and", > but then I realized that when a and c are both unequal to None, the > result would also be True. This implies the logical approach would be > exclusive-or (^). Try this expression: > > not ((a==None) ^ (c==None)) ^ is *bitwise* xor, not boolean xor. Python doesn't offer boolean xor directly, although != comes close. > OTOH, if what you really want is simply to check that both are None (my > first impression), then it's simply: > > (a==None) and (c==None) Replace == with 'is'. > Most of the replies you're getting here seem unnecessarily complicated. == is a more complicated operator than the 'is' operator. That's why the is operator is to be preferred when testing for None -- it is guaranteed to do the right thing, while == is not. -- Steven From jeanpierreda at gmail.com Sun Dec 25 19:58:14 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sun, 25 Dec 2011 19:58:14 -0500 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: > Which of course leads to a SillyPEP for a new keyword, "are", which > would allow you to write: > >>>> a and c are None > > instead of the much more verbose > >>>> a is None and c is None How about: >>> a is b is None ;) -- Devin On Sun, Dec 25, 2011 at 7:27 PM, Roy Smith wrote: > In article , > ?Devin Jeanpierre wrote: > >> The issue here is that "== None" is being used instead of "is None", >> but I believe that's been covered. Your response doesn't include it, >> so maybe it's worth restating. > > Which of course leads to a SillyPEP for a new keyword, "are", which > would allow you to write: > >>>> a and c are None > > instead of the much more verbose > >>>> a is None and c is None > -- > http://mail.python.org/mailman/listinfo/python-list From pip.261 at gmail.com Sun Dec 25 20:04:54 2011 From: pip.261 at gmail.com (Felipe O) Date: Sun, 25 Dec 2011 20:04:54 -0500 Subject: Backslash Escapes Message-ID: Hi all, Whenever I take any input (raw_input, of course!) or I read from a file, etc., any backslashes get escaped automatically. Is there any elegant way of parsing the backslashes as though they were written in a python string. The best I have so far right now goes like this: def parse_backslash_escapes(input_string): parts = input_string.split("'''") # That's ' " " " ' without the spaces '"""'.join(eval + p + '"""') for p in parts) I'm not entirely convinced that it's safe on two accounts. + Is that eval statement safe? The input could be coming from an unfriendly source. + Are there any obscure backslash escapes or other tricks I should be aware of? I guess the alternative is to make a dictionary of all the escapes I want to support, but that sounds tedious and error-prone. Thanks, Felipe From python.list at tim.thechases.com Sun Dec 25 20:14:29 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 25 Dec 2011 19:14:29 -0600 Subject: How safe is this way of getting a default that allows None? In-Reply-To: References: Message-ID: <4EF7CA75.50400@tim.thechases.com> On 12/25/11 18:49, Dan Stromberg wrote: > How safe is this? I like the idea. > > UNSPECIFIED = object() > > def fn(x, y=UNSPECIFIED): > if y is UNSPECIFIED: > print x, 'default' > else: > print x, y safe? It's idiomatic :) -tkc From jeanpierreda at gmail.com Sun Dec 25 20:26:39 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sun, 25 Dec 2011 20:26:39 -0500 Subject: Backslash Escapes In-Reply-To: References: Message-ID: > Whenever I take any input (raw_input, of course!) or I read from a > file, etc., any backslashes get escaped automatically. They don't get escaped, they get... treated as the bytes that they are. If you want them to mean what they do in Python, use the 'string_escape' codec. >>> x = r'nyan\nyan' >>> print x nyan\nyan >>> print x.decode('string_escape') nyan yan -- Devin On Sun, Dec 25, 2011 at 8:04 PM, Felipe O wrote: > Hi all, > Whenever I take any input (raw_input, of course!) or I read from a > file, etc., any backslashes get escaped automatically. Is there any > elegant way of parsing the backslashes as though they were written in > a python string. The best I have so far right now goes like this: > > def parse_backslash_escapes(input_string): > ? ?parts = input_string.split("'''") ?# That's ' " " " ' without the spaces > ? ?'"""'.join(eval + p + '"""') for p in parts) > > I'm not entirely convinced that it's safe on two accounts. > + Is that eval statement safe? The input could be coming from an > unfriendly source. > + Are there any obscure backslash escapes or other tricks I should be aware of? > > I guess the alternative is to make a dictionary of all the escapes I > want to support, but that sounds tedious and error-prone. > > Thanks, > > Felipe > -- > http://mail.python.org/mailman/listinfo/python-list From python at mrabarnett.plus.com Sun Dec 25 20:26:52 2011 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 26 Dec 2011 01:26:52 +0000 Subject: Backslash Escapes In-Reply-To: References: Message-ID: <4EF7CD5C.2060501@mrabarnett.plus.com> On 26/12/2011 01:04, Felipe O wrote: > Hi all, > Whenever I take any input (raw_input, of course!) or I read from a > file, etc., any backslashes get escaped automatically. Is there any > elegant way of parsing the backslashes as though they were written in > a python string. The best I have so far right now goes like this: > > def parse_backslash_escapes(input_string): > parts = input_string.split("'''") # That's ' " " " ' without the spaces > '"""'.join(eval + p + '"""') for p in parts) > > I'm not entirely convinced that it's safe on two accounts. > + Is that eval statement safe? The input could be coming from an > unfriendly source. > + Are there any obscure backslash escapes or other tricks I should be aware of? > > I guess the alternative is to make a dictionary of all the escapes I > want to support, but that sounds tedious and error-prone. > Try input_string.decode("string-escape") if input_string is a bytestring or input_string.decode("unicode-escape") it's a Unicode string. From ian.g.kelly at gmail.com Sun Dec 25 21:02:16 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 25 Dec 2011 19:02:16 -0700 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On Sun, Dec 25, 2011 at 2:38 AM, Nobody wrote: > On Sat, 24 Dec 2011 23:09:50 -0800, GZ wrote: > >> I run into a weird problem. I have a piece of code that looks like the >> following: >> >> f(...., a=None, c=None): >> ? ? assert ?(a==None)==(c==None) >> >> >> The problem is that == is not implemented sometimes for values in a >> and c, causing an exception NotImplementedError. > > I have no idea how that can happen. If a.__eq__(None) returns > NotImplemented, the interpreter should flip the test and perform the > equivalent of None.__eq__(a), which will return False. Maybe the class has a misbehaved __eq__ that raises NotImplementedError directly instead of returning NotImplemented. From steve+comp.lang.python at pearwood.info Sun Dec 25 22:00:08 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 Dec 2011 03:00:08 GMT Subject: Random string of digits? References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef7e337$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 12:41:29 -0500, Roy Smith wrote: > On Mon, 26 Dec 2011 03:11:56 +1100, Chris Angelico wrote: >> > I prefer not to rely on the source. That tells me what happens, not >> > what's guaranteed to happen. > > Steven D'Aprano wrote: >> In this case, the source explicitly tells you that the API includes >> support for arbitrary large ranges if you include a getrandbits() >> method: >> >> Optionally, implement a getrandbits() method so that randrange() >> can cover arbitrarily large ranges. >> >> I call that a pretty strong guarantee. > > I think you mis-understood Chris's point. And I'm afraid that you have missed my point. The above comment from the source is from a docstring: it *is* public, official documentation. See help(random.Random). > The documentation is the > specification of how something behaves. If the documentation doesn't > say it, you can't rely on it. A nice platitude, but not true. Documentation is often incomplete or even inaccurate. We rely on many things that aren't documented anywhere. For example, we can rely on the fact that x = 3917 print x+1 will print 3918, even though that specific fact isn't documented anywhere. Nevertheless, we can absolutely bank on it -- if it happened to do something else, we would report it as a bug and not expect to be told "implementation detail, will not fix". We make a number of undocumented assumptions: * we assume that when the documentation talks about "adding" two numbers, it means the standard mathematical definition of addition and not some other meaning; * we assume that the result of such addition must be *correct*, without that assumption being guaranteed anywhere; * we assume that addition of two ints will return an int, as opposed to some other numerically equal value such as a float or Fraction; * we assume that not only will it be an int, but it will be *exactly* an int, and not a subclass of int; and no doubt there are others. And by the way, in case you think I'm being ridiculously pedantic, consider the first assumption listed above: the standard mathematical definition of addition. That assumption is violated for floats. py> 0.7 + 0.1 == 0.8 False It is rather amusing the trust we put in documentation, when documentation can be changed just as easily as code. Just because the Fine Manual says that x+1 performs addition today, doesn't mean that it will continue to say the same thing tomorrow. So if you trust the language designers not to arbitrarily change the documentation, why not trust them not to arbitrarily change the code? Hint: as far as I can tell, nowhere does Python promise that printing 3918 will show those exact digits, but it's a pretty safe bet that Python won't suddenly change to displaying integers in balanced-ternary notation instead of decimal. > The user should never have to read the > source to know how to use a function, or what they can depend on. Well, that's one school of thought. Another school of thought is that documentation and comments lie, and the only thing that you can trust is the code itself. At the end of the day, the only thing that matters is the mindset of the developer(s) of the software. You must try to get into their head and determine whether they are the sort of people whose API promises can be believed. > Now, > I'm not saying that reading the source isn't useful for a deeper > understanding, but it should be understood that any insights you glean > from doing that are strictly implementation details. I couldn't care less about the specific details of *how* getrandbits is used to put together an arbitrarily large random number. That "how" is what people mean when they talk about "mere implementation details". But the fact that getrandbits is used by randrange (as opposed to how it is used) is not a mere implementation detail, but a public part of the interface. > If you're saying that there are guarantees made by the implementation of > getrandbits() which are not documented, then one of two things are true: The implementation of getrandbits is not documented at all. You would have to read the C source of the _random module to find out how the default pseudo-random number generator produces random bits. But note the leading underscore: _random is a private implementation detail. However the existence and use of random.Random.getrandbits is public, documented, and guaranteed. -- Steven From rosuav at gmail.com Sun Dec 25 22:27:44 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 14:27:44 +1100 Subject: Python education survey In-Reply-To: <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 4:44 AM, Rick Johnson wrote: > Why install an IDE when IDLE is already there? Oh, yes, IDLE SUCKS. I > know that already. But this revelation begs the question... Why has > this community allowed IDLE to rot? Why has guido NOT started a public > discussion on the matter? Conversely, why write an IDE into IDLE when perfectly-good IDEs already exist? I don't use IDLE for development per se; it's for interactive Python execution, but not editing of source files. ChrisA From rosuav at gmail.com Sun Dec 25 22:43:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 14:43:35 +1100 Subject: Random string of digits? In-Reply-To: <4ef7e337$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef7e337$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 2:00 PM, Steven D'Aprano wrote: > The implementation of getrandbits is not documented at all. You would > have to read the C source of the _random module to find out how the > default pseudo-random number generator produces random bits. But note the > leading underscore: _random is a private implementation detail. > > However the existence and use of random.Random.getrandbits is public, > documented, and guaranteed. It's that last line where I find disagreement between documentation and source code. """Class Random can also be subclassed if you want to use a different basic generator of your own devising: in that case, override the random(), seed(), getstate(), and setstate() methods. Optionally, a new generator can supply a getrandbits() method ? this allows randrange() to produce selections over an arbitrarily large range.""" My reading of this is that, if I were to write my own generator, I could provide that method and gain perfection. It's not until you dig somewhat that you find out that the default generator actually does provide getrandbits, meaning that the default randrange can actually be used for arbitrarily large ranges. Yes, it IS documented, but in separate places and somewhat as an afterthought; in the 2.x documentation it's shown as "new in 2.4", which explains this. Unfortunately my brain isn't really running on all cylinders at the moment and I can't come up with a better wording, but is there some way this could be stated up in the same top paragraph that explains about 53-bit precision and 2**19937-1 period? That's where I got my original information from; I didn't go down to the individual function definitions, which is the only place that it's stated that the Mersenne Twister does include getrandbits (and which assumes that you've read the top section that states that Mersenne Twister is the default implementation). ChrisA From rosuav at gmail.com Sun Dec 25 22:58:36 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 14:58:36 +1100 Subject: Backslash Escapes In-Reply-To: References: Message-ID: On Mon, Dec 26, 2011 at 12:04 PM, Felipe O wrote: > Hi all, > Whenever I take any input (raw_input, of course!) or I read from a > file, etc., any backslashes get escaped automatically. Is there any > elegant way of parsing the backslashes as though they were written in > a python string... I guess the alternative is to make a dictionary of all the escapes I > want to support, but that sounds tedious and error-prone. The question really is: Are you looking to support some backslash escapes, or are you looking to imitate Python? If the former, I recommend making a specific list of what you support; every language has its own, usually including some obscurities, and if all you need is a few basics like \n, \t, \\, etc, then you probably don't want to support \123 and \u1234 notations for arbitrary insertion of characters. But if you actually want to imitate a Python string literal, eval is almost certainly the easiest way; and if you want a safer version: http://docs.python.org/library/ast.html#ast.literal_eval I'm not sure how well suited this is for parsing a string for its escapes, but if you're looking to imitate Python, it may be easier to tell people that the value must be enclosed in quotes - thus making it an expression. ChrisA From roy at panix.com Sun Dec 25 23:17:11 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 23:17:11 -0500 Subject: Random string of digits? References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef7e337$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4ef7e337$0$29973$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > And I'm afraid that you have missed my point. The above comment from the > source is from a docstring: it *is* public, official documentation. See > help(random.Random). When you wrote, "the source explicitly tells you..." the natural assumption I made was "something in the source that's not part of the documentation" (i.e. some comment). > > The documentation is the specification of how something behaves. > > If the documentation doesn't say it, you can't rely on it. > A nice platitude, but not true. Documentation is often incomplete or even > inaccurate. Of course it is. Those things constitute doc bugs which need to get fixed. The fact that the specification is flawed does not change the fact that it *is* the specification. From ethan at stoneleaf.us Sun Dec 25 23:26:06 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 25 Dec 2011 20:26:06 -0800 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: <4EF7F75E.1010909@stoneleaf.us> Nobody wrote: > nothing should compare > equal to None except for None itself, so "x is None" and "x == None" > shouldn't produce different results unless there's a bug in the comparison > method. > Why wouldn't you want other types that can compare equal to None? It could be useful for a Null type to == None. ~Ethan~ From rosuav at gmail.com Sun Dec 25 23:26:48 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 15:26:48 +1100 Subject: Random string of digits? In-Reply-To: References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef7e337$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 3:17 PM, Roy Smith wrote: > Of course it is. ?Those things constitute doc bugs which need to get > fixed. ?The fact that the specification is flawed does not change the > fact that it *is* the specification. Also, the specification is what can be trusted across multiple implementations of Python - the source can't. I don't intend to get the source code to five Pythons to ascertain whether getrandbits() is provided in the default random.Random() of each of them. Now, in this particular instance, it IS stated in the docs - just down in the individual function descriptions, which I hadn't read - so I almost certainly CAN trust that across all platforms. But in the general case, I would not want to make firm statements on the basis of implementation details. ChrisA From ethan at stoneleaf.us Sun Dec 25 23:27:24 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 25 Dec 2011 20:27:24 -0800 Subject: Test None for an object that does not implement == In-Reply-To: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: <4EF7F7AC.5000500@stoneleaf.us> GZ wrote: > Hi, > > I run into a weird problem. I have a piece of code that looks like the > following: > > f(...., a=None, c=None): > assert (a==None)==(c==None) Um -- if you don't want a and c being passed in, why put them in the function signature? ~Ethan~ From jeanpierreda at gmail.com Mon Dec 26 00:22:38 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Mon, 26 Dec 2011 00:22:38 -0500 Subject: Test None for an object that does not implement == In-Reply-To: <4EF7F7AC.5000500@stoneleaf.us> References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> <4EF7F7AC.5000500@stoneleaf.us> Message-ID: > Um -- if you don't want a and c being passed in, why put them in the > function signature? He wants both or neither to be passed in. -- Devin On Sun, Dec 25, 2011 at 11:27 PM, Ethan Furman wrote: > GZ wrote: >> >> Hi, >> >> I run into a weird problem. I have a piece of code that looks like the >> following: >> >> f(...., a=None, c=None): >> ? ?assert ?(a==None)==(c==None) > > > > Um -- if you don't want a and c being passed in, why put them in the > function signature? > > ~Ethan~ > -- > http://mail.python.org/mailman/listinfo/python-list From ananya14389 at gmail.com Mon Dec 26 00:30:06 2011 From: ananya14389 at gmail.com (ananya 7) Date: Sun, 25 Dec 2011 21:30:06 -0800 (PST) Subject: Best Online Jobs Message-ID: <7e3ab47d-c41b-4057-88ca-ab3c9a30f3de@r13g2000prr.googlegroups.com> Best Online Jobs http://gsonlinejobs.yolasite.com/ From memilanuk at gmail.com Mon Dec 26 01:02:05 2011 From: memilanuk at gmail.com (Monte Milanuk) Date: Sun, 25 Dec 2011 22:02:05 -0800 Subject: Python education survey In-Reply-To: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: Not a teacher here, but I'm curious why Komodo Edit never seems to get any love in the IDE debates... a free version for personal/non-profit use, pro versions for those that need the extra features, seems to work fairly well but then again I'm probably not the best judge... Thanks, Monte From saqib.ali.75 at gmail.com Mon Dec 26 03:03:57 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Mon, 26 Dec 2011 00:03:57 -0800 (PST) Subject: Changing the system clock with pexpect confuses pexpect! Message-ID: <1f342621-0c96-447c-ad5d-f8c9dc777883@i6g2000vbe.googlegroups.com> See my code below. I'm controlling a shell logged in as root with pexpect. The class below has a method (startProc) which spawns a shell and keeps it alive until told to destroy it (stopProc). The other 2 methods in this class allow me to change the system clock and to get the IP Address of this machine. They all work fine.... except when I advance the system clock, and then try to get the IP Address. In that case, I get an exception because pexpect incorrectly thinks the output it is getting from ifconfig is invalid. But it is not. Pexpect is just confused. This doesn't happen when I move the clock backwards. It only happens when I move the clock forward. I believe what is going on is that internally pexpect uses the system clock to keep track of when it receives data from spawned processes. When I mess with the clock, that messes up the internal workings of pexpect. Any suggestions what I should do? I have numerous concurrent pexpect processes running when I modify the clock. Is there anyway to prevent them all from getting totally screwed up?? ----------------------------------------------------------------------------------------------------- #!/usr/bin/env python import pexpect, os, time, datetime, re def reportPrint(string): print string def reportAssert(condition, string) if condition == False: print string raise Exception class rootManager: rootProc = None rootPrompt = "] % " myPrompt = "] % " def __init__(self): pass def startProc(self): if self.rootProc != None: reportPrint("\t\t- Root Process is already created") else: self.rootProc = pexpect.spawn ('/bin/tcsh',) i = self.rootProc.expect([pexpect.TIMEOUT, self.myPrompt,]) reportAssert(i != 0, "Time-Out.... exiting") reportPrint("\t\t- Sending su") self.rootProc.sendline("su") i = self.rootProc.expect([pexpect.TIMEOUT, "Password: ",]) reportAssert(i != 0, "Time-Out.... exiting") reportPrint("\t\t- Sending Password") self.rootProc.sendline(ROOT_PASSWORD) i = self.rootProc.expect([pexpect.TIMEOUT, self.rootPrompt,]) reportAssert(i != 0, "Time-Out.... exiting") reportPrint("\t\t- Root Process created") def getIPAddr(self): reportAssert(self.rootProc != None, "No active Root Process!") reportPrint("\t\t- Sending ifconfig -a") self.rootProc.sendline("ifconfig -a") i = self.rootProc.expect([pexpect.TIMEOUT, self.rootPrompt,]) reportAssert(i != 0, "Time-Out.... exiting") outputTxt = self.rootProc.before ipList = [i for i in re.compile("(?<=inet )\d{1,3}\.\d{1,3}\. \d{1,3}\.\d{1,3}").findall(outputTxt) if i != "127.0.0.1"] reportAssert(len(ipList) == 1, "Cannot determine IP Address from 'ifconfig -a': \n%s" % outputTxt) return ipList[0] def changeClock(self, secondsDelta): reportAssert(self.rootProc != None, "No active Root Process!") newTime = datetime.datetime.now() + datetime.timedelta(seconds=secondsDelta) dateStr = "%02d%02d%02d%02d%s" % (newTime.month, newTime.day, newTime.hour, newTime.minute, str(newTime.year)[-2:]) reportPrint("\t\t- Sending 'date %s' command" % dateStr) self.rootProc.sendline("date %s" % dateStr) #Remember, by changing the clock, you are confusing pexpect's timeout measurement! # so ignore timeouts in this case i = self.rootProc.expect([pexpect.TIMEOUT, self.rootPrompt,],) def stopProc(self): if self.rootProc == None: reportPrint("\t\t- Root Process is already destroyed") else: reportPrint("\t\t- Sending exit command") rootProc.sendline("exit") i = rootProc.expect([pexpect.TIMEOUT, self.myPrompt]) reportAssert(i != 0, "Time-Out.... exiting") reportPrint("\t\t- Sending exit command") rootProc.sendline("exit") i = rootProc.expect([pexpect.TIMEOUT, pexpect.EOF]) reportAssert(i != 0, "Time-Out.... exiting") self.rootProc.close() self.rootProc = None reportPrint("\t\t- Root Process Destroyed") From vivekimsit at gmail.com Mon Dec 26 03:37:02 2011 From: vivekimsit at gmail.com (vivek poddar) Date: Mon, 26 Dec 2011 00:37:02 -0800 (PST) Subject: Python Openings Message-ID: Hi, I have just completed my B.tech this year and have above six months experience in OpenERP framework as a technical consultant.In these six months I have completed nearly 5 modules and acted as an active member.Now, my company is going under a financial crisis and so, I think I have to leave this job.But I want to continue my career in OpenERP or Python. Thanks and Regards! Vivek Poddar From lie.1296 at gmail.com Mon Dec 26 04:28:14 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 26 Dec 2011 20:28:14 +1100 Subject: Plot seems weird In-Reply-To: <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> Message-ID: On 12/26/2011 05:27 AM, Yigit Turgut wrote: > On Dec 25, 7:06 pm, Rick Johnson wrote: >> On Dec 25, 9:33 am, Yigit Turgut wrote: >>> Hi all, >> >>> I have a text file as following; >> >>> 0.200047 0.000000 >>> 0.200053 0.160000 >>> 0.200059 0.000000 >>> 0.200065 0.080000 >>> 0.200072 0.000000 >>> 0.200078 0.160000 >> >>> And I am trying to plot it with ; >> >>> filenames = sys.argv[1:] >>> if len(filenames) == 0: >>> filenames = [sys.stdin] >>> for filename in filenames: >>> t,y1 = numpy.genfromtxt(filename, unpack=True) >>> pyplot.plot(t,y1) >>> pyplot.show() >> >>> But graph seems weird, not as it supposed to be. Any ideas ? >> >> Interesting. Of course "weird" leaves a LOT to be desired. On a scale >> of 1-10, how "weird" is the result? > > I apply a 1Khz test signal just to see if things run smoothly, but I > see spikes at lower and higher ends (logic 0,1) where I should see a > clean rectangle pwm signal. By the look of it I say weirdness is > around 3/10. What are you expecting? Your data produces something that looks like the plot on the right of this screenshot (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with that; if you are expecting a square-wave-like plot (like on the left), then you should use a square-wave-like data, pyplot wouldn't magically transform a spiked-plot to squared-plot. Here's what I use to convert the data on right plot to data on left plot, I don't know much about numpy so it might be possible to do it more efficiently or numpy might even have something like it already. from itertools import izip_longest def to_square(t, y1): sq_data = [[], []] for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): sq_data[0].append(x) sq_data[1].append(y) sq_data[0].append(xn) sq_data[1].append(y) return numpy.array(sq_data, dtype=float) From lie.1296 at gmail.com Mon Dec 26 04:36:39 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 26 Dec 2011 20:36:39 +1100 Subject: Backslash Escapes In-Reply-To: References: Message-ID: On 12/26/2011 12:04 PM, Felipe O wrote: > Hi all, > Whenever I take any input (raw_input, of course!) or I read from a > file, etc., any backslashes get escaped automatically. Python never escapes backslashes when reading from raw_input or files. Python only ever escapes backslashes when displaying data on the interactive shell since the interactive shell uses __repr__ by default. > Is there any > elegant way of parsing the backslashes as though they were written in > a python string. The best I have so far right now goes like this: You shouldn't ever need to unless your file are backslash-escaped. From briskb5 at gmail.com Mon Dec 26 05:33:56 2011 From: briskb5 at gmail.com (briskb5 briskb5) Date: Mon, 26 Dec 2011 02:33:56 -0800 (PST) Subject: Best Online Jobs Message-ID: <7bdff594-5561-484e-b251-42a89da9d498@e8g2000prb.googlegroups.com> Best Online Jobs http://gsonlinejobs.yolasite.com/ From roy at panix.com Mon Dec 26 08:43:34 2011 From: roy at panix.com (Roy Smith) Date: Mon, 26 Dec 2011 08:43:34 -0500 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> <4EF7F7AC.5000500@stoneleaf.us> Message-ID: In article , Devin Jeanpierre wrote: > > Um -- if you don't want a and c being passed in, why put them in the > > function signature? > > He wants both or neither to be passed in. assert sum(foo is None for foo in [a, c]) % 2 == 0 From roy at panix.com Mon Dec 26 08:51:24 2011 From: roy at panix.com (Roy Smith) Date: Mon, 26 Dec 2011 08:51:24 -0500 Subject: Changing the system clock with pexpect confuses pexpect! References: <1f342621-0c96-447c-ad5d-f8c9dc777883@i6g2000vbe.googlegroups.com> Message-ID: In article <1f342621-0c96-447c-ad5d-f8c9dc777883 at i6g2000vbe.googlegroups.com>, Saqib Ali wrote: > I believe what is going on is that internally pexpect uses the system > clock to keep track of when it receives data from spawned processes. > When I mess with the clock, that messes up the internal workings of > pexpect. > > Any suggestions what I should do? Yeah. Don't mess with the system clock. Seriously. There is pretty much no reason for anybody not to be running NTP these days. Every OS ships with it, and there's gazillions of open NTP servers to use as references. NTP will keep your system clock accurate to within a fraction of a second and avoid all the horrible problems such as the one you've discovered with pexpect when you reset your clock. From paul.nospam at rudin.co.uk Mon Dec 26 09:33:24 2011 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Mon, 26 Dec 2011 14:33:24 +0000 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: GZ writes: > I run into a weird problem. I have a piece of code that looks like the > following: > > f(...., a=None, c=None): > assert (a==None)==(c==None) > There is only one 'None' - so use 'a is None' rather than 'a == None'. (In common lisp there is a particular language construct that allows you do determine whether an argument was passed in or was defaulted.) From rantingrickjohnson at gmail.com Mon Dec 26 09:52:03 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Mon, 26 Dec 2011 06:52:03 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> Message-ID: <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> On Dec 25, 9:27?pm, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 4:44 AM, Rick Johnson > > [...] > Conversely, why write an IDE into IDLE when perfectly-good IDEs > already exist? I don't use IDLE for development per se; it's for > interactive Python execution, but not editing of source files. I believe the answer is two fold: 1. Including an IDE like IDLE into the Python distro helps noobs to get started quickly without needing to traverse a gauntlet of unknown IDEs on their own. If later they find something that they feel is more appropriate; so be it. 2. (and most important to me... IDLE is written in Python using the Tkinter GUI (which ships with python also). Therefore, the source code for IDLE can be a GREAT teaching resource for how to write professional Tkinter applications. I KNOW THE CURRENT SOURCE SUCKS! However, we could change that. So, with those points being covered, i believe IDLE is very important to the Python community and could be useful to more people IF we clean it up a bit. It's really a great little IDE with even greater potential. If Guido would just say something (or at least some of the top Pythionistas (Hettinger i am looking at you!)) this community might work together to fix this problem. From nad at acm.org Mon Dec 26 09:54:41 2011 From: nad at acm.org (Ned Deily) Date: Mon, 26 Dec 2011 09:54:41 -0500 Subject: installing matplotlib in MacOs 10.6.8. References: Message-ID: In article , Alex Ter-Sarkissov wrote: > hi everyone, I run python 2.7.2. in Eclipse (recently upgraded from 2.6). I > have a problem with installing matplotlib (I found the version for python > 2.7. MacOs 10.3, no later versions). If I run python in terminal using arch > -i386 python, and then > > from matplotlib.pylab import * > > and similar stuff, everything works fine. If I run python in eclipse or > just without arch -i386, I can import matplotlib as > > from matplotlib import * > > but actually nothing gets imported. If I do it in the same way as above, I > get the message > > no matching architecture in universal wrapper > > which means there's conflict of versions or something like that. I tried > reinstalling the interpreter and adding matplotlib to forced built-ins, but > nothing helped. For some reason I didn't have this problem with numpy and > tkinter. The message means almost certainly means that the Python you are using is a 64-bit/32-bit universal version. When you launch Python with -arch i386, you force the Python to run in 32-bit mode so it is compatible with the 32-bit-only version of matplotlib you've installed in it. When you launch Python by default, it is runnning in 64-bit mode so the 32-bit-only C extension modules in that version of matplotlib cannot be loaded. You might want to ask the matplotlib developers to make a 64-bit version available. In the mean time, you could try to configure Eclipse to invoke the python with `python2.7-32`, which is included with distributions like the python.org one to force Python to run in 32-bit mode on OS X. -- Ned Deily, nad at acm.org From nathan.alexander.rice at gmail.com Mon Dec 26 11:11:32 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Mon, 26 Dec 2011 11:11:32 -0500 Subject: Python education survey In-Reply-To: <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 9:52 AM, Rick Johnson wrote: > On Dec 25, 9:27?pm, Chris Angelico wrote: >> On Mon, Dec 26, 2011 at 4:44 AM, Rick Johnson >> > [...] >> Conversely, why write an IDE into IDLE when perfectly-good IDEs >> already exist? I don't use IDLE for development per se; it's for >> interactive Python execution, but not editing of source files. > > I believe the answer is two fold: > > 1. Including an IDE like IDLE into the Python distro helps noobs to > get started quickly without needing to traverse a gauntlet of unknown > IDEs on their own. If later they find something that they feel is more > appropriate; so be it. > > 2. (and most important to me... IDLE is written in Python using the > Tkinter GUI (which ships with python also). Therefore, the source code > for IDLE can be a GREAT teaching resource for how to write > professional Tkinter applications. I KNOW THE CURRENT SOURCE SUCKS! > However, we could change that. > > So, with those points being covered, i believe IDLE is very important > to the Python community and could be useful to more people IF we clean > it up a bit. It's really a great little IDE with even greater > potential. If Guido would just say something (or at least some of the > top Pythionistas (Hettinger i am looking at you!)) this community > might work together to fix this problem. Not everyone who has Python installed wants to learn the language. I do think that a "learning" distro that has a lot of core tools pre-installed, and ships with some tutorials, would be a decent idea. Sort of like Enthought for new users :) I don't feel IDLE is worth salvaging though. Nathan From y.turgut at gmail.com Mon Dec 26 12:08:03 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Mon, 26 Dec 2011 09:08:03 -0800 (PST) Subject: Plot seems weird References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> Message-ID: <74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com> On Dec 26, 11:28?am, Lie Ryan wrote: > On 12/26/2011 05:27 AM, Yigit Turgut wrote: > > > > > > > > > > > On Dec 25, 7:06 pm, Rick Johnson ?wrote: > >> On Dec 25, 9:33 am, Yigit Turgut ?wrote: > >>> Hi all, > > >>> I have a text file as following; > > >>> 0.200047 ? ? ? ?0.000000 > >>> 0.200053 ? ? ? ?0.160000 > >>> 0.200059 ? ? ? ?0.000000 > >>> 0.200065 ? ? ? ?0.080000 > >>> 0.200072 ? ? ? ?0.000000 > >>> 0.200078 ? ? ? ?0.160000 > > >>> And I am trying to plot it with ; > > >>> filenames = sys.argv[1:] > >>> if len(filenames) == 0: > >>> ? ? ?filenames = [sys.stdin] > >>> for filename in filenames: > >>> ? ? ?t,y1 = numpy.genfromtxt(filename, unpack=True) > >>> ? ? ?pyplot.plot(t,y1) > >>> ? ? ?pyplot.show() > > >>> But graph seems weird, not as it supposed to be. Any ideas ? > > >> Interesting. Of course "weird" leaves a LOT to be desired. On a scale > >> of 1-10, how "weird" is the result? > > > I apply a 1Khz test signal just to see if things run smoothly, but I > > see spikes at lower and higher ends (logic 0,1) where I should see a > > clean rectangle pwm signal. By the look of it I say weirdness is > > around 3/10. > > What are you expecting? Your data produces something that looks like the > plot on the right of this screenshot > (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with > that; if you are expecting a square-wave-like plot (like on the left), > then you should use a square-wave-like data, pyplot wouldn't magically > transform a spiked-plot to squared-plot. > > Here's what I use to convert the data on right plot to data on left > plot, I don't know much about numpy so it might be possible to do it > more efficiently or numpy might even have something like it already. > > from itertools import izip_longest > def to_square(t, y1): > ? ? ?sq_data = [[], []] > ? ? ?for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): > ? ? ? ? ?sq_data[0].append(x) > ? ? ? ? ?sq_data[1].append(y) > ? ? ? ? ?sq_data[0].append(xn) > ? ? ? ? ?sq_data[1].append(y) > ? ? ?return numpy.array(sq_data, dtype=float) Thanks for the tip. I know that I feed a square wave signal and record this data. Thus I believe the situation can be related to sampling frequency. Couldn't get your code working, maybe because I import the data from file. From ethan at stoneleaf.us Mon Dec 26 12:14:31 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 26 Dec 2011 09:14:31 -0800 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> <4EF7F7AC.5000500@stoneleaf.us> Message-ID: <4EF8AB77.5080605@stoneleaf.us> Devin Jeanpierre wrote: >> Um -- if you don't want a and c being passed in, why put them in the >> function signature? > > He wants both or neither to be passed in. Ah -- right. ~Ethan~ From rantingrickjohnson at gmail.com Mon Dec 26 12:27:35 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Mon, 26 Dec 2011 09:27:35 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> Message-ID: On Dec 26, 10:11?am, Nathan Rice wrote: > On Mon, Dec 26, 2011 at 9:52 AM, Rick Johnson > > > > > > > > > > wrote: > > On Dec 25, 9:27?pm, Chris Angelico wrote: > >> On Mon, Dec 26, 2011 at 4:44 AM, Rick Johnson > >> > [...] > >> Conversely, why write an IDE into IDLE when perfectly-good IDEs > >> already exist? I don't use IDLE for development per se; it's for > >> interactive Python execution, but not editing of source files. > > > I believe the answer is two fold: > > > 1. Including an IDE like IDLE into the Python distro helps noobs to > > get started quickly without needing to traverse a gauntlet of unknown > > IDEs on their own. If later they find something that they feel is more > > appropriate; so be it. > > > 2. (and most important to me... IDLE is written in Python using the > > Tkinter GUI (which ships with python also). Therefore, the source code > > for IDLE can be a GREAT teaching resource for how to write > > professional Tkinter applications. I KNOW THE CURRENT SOURCE SUCKS! > > However, we could change that. > > > So, with those points being covered, i believe IDLE is very important > > to the Python community and could be useful to more people IF we clean > > it up a bit. It's really a great little IDE with even greater > > potential. If Guido would just say something (or at least some of the > > top Pythionistas (Hettinger i am looking at you!)) this community > > might work together to fix this problem. > > Not everyone who has Python installed wants to learn the language. I'll admit you make a very valid point. But since "they" are not familiar with the language, what they don't know cannot hurt them. Although i do see a need to keep the core distro small. If we remove IDLE then we face the next big question... Should we remove Tkinter? >?I > do think that a "learning" distro that has a lot of core tools > pre-installed, and ships with some tutorials, would be a decent idea. > Sort of like Enthought for new users :) This is the kind of inventiveness i have been looking for in this group. I think this is a great idea! > ?I don't feel IDLE is worth salvaging though. Agreed. Not in it's current state anyway. From steve+comp.lang.python at pearwood.info Mon Dec 26 13:04:34 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 Dec 2011 18:04:34 GMT Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> Message-ID: <4ef8b732$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 06:52:03 -0800, Rick Johnson wrote: > If Guido would just say something (or at least some of the top > Pythionistas (Hettinger i am looking at you!)) this community might work > together to fix this problem. The sheer cluelessness displayed here about open source is painful. If Guido would "just say something", this community would yawn. He's not the boss of us. Python development is an all-volunteer effort. "The community" works on projects that we find interesting, or that we need ourselves, not just because Guido "says something" about it -- and certainly not projects that you demand we work on. Instead of demanding that GvR order us to work on IDLE, put your money where your mouth is, Rick, and stop being a blow-hard. You've been nagging us about IDLE for long enough -- what is it now, two years? Three? I haven't been counting, but it feels like geological ages. Start a project to fix IDLE. There is plenty of free hosting on the Internet for open source projects, like GitHub and Google Code Hosting. Track some bugs. Plan some concrete enhancements. Write some actual code. Demonstrate actual effort, and you may attract others who care. Otherwise, you're wasting our time. -- Steven From wolftracks at invalid.com Mon Dec 26 13:16:30 2011 From: wolftracks at invalid.com (W. eWatson) Date: Mon, 26 Dec 2011 10:16:30 -0800 Subject: Is my IDLE problem caused by .idlerc? Permissions. In-Reply-To: References: Message-ID: On 12/24/2011 11:35 AM, Dennis Lee Bieber wrote: > On Sat, 24 Dec 2011 10:55:48 -0800, "W. eWatson" > wrote: > >> >> Permissions as follows: >> SYSTEM: All. From Full control to write >> Account Unknown(S-1-5-21...): read&exec, Read >> Wayne: (normal use) All. From Full control to write >> Admin: All. From Full control to write >> WMPNetwork: Read >> >> Comments? >> >> .idlerc permissions are slightly different for Account Unknown. > > "Account Unknown" refers to a user account that has been deleted > (or, via some other system corruption, is no longer associated with a > login name -- happened to me earlier this year: my user account "name" > had been "Wulfraed" but the user account directory was under "Dennis Lee > Bieber"... When the registry crashed it disconnected name "Wulfraed" > from directory "Dennis Lee Bieber"... Lost a month of email once I got > done creating a new user account where directory and name matched and > restored from backup [I failed to back-up the old user directory before > trying a remove/create user]). > > My suggestion would be, first, remove privileges for that remnant > account where-ever found; check higher directory levels too, and try to > propagate the privilege downwards. > > Second -- could the file itself be set for read-only on the simple > properties? I just tested with Win7 -- setting the general "read-only" > did not make any changes to any of the security tab "users". Third, > confirm the owner of the file is your current user account; if not,see > if you can get "take ownership" option for the file to work. > > I wonder if removing the "Account Unknown" account might just do it. Not quite familiar with Owner, but I'll look around. I'll check the file for "read only". From y.turgut at gmail.com Mon Dec 26 13:31:36 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Mon, 26 Dec 2011 10:31:36 -0800 (PST) Subject: Multithreading Message-ID: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> I have a loop as following ; start = time.time() end = time.time() - start while(end <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> <4ef8b732$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <90a19db5-487a-4af6-92ff-8654f1091283@p41g2000yqm.googlegroups.com> On Dec 26, 12:04?pm, Steven D'Aprano wrote: > If Guido would "just say something", this community would yawn. He's not > the boss of us. Python development is an all-volunteer effort. "The > community" works on projects that we find interesting, or that we need > ourselves, not just because Guido "says something" about it Believe it or not Steven, all communities need leadership. Just because you are a rebel and don't like anyone trumping your opinions does mean that the majority of people here don't need (or want for that mater) some direction.Communities work best when leaders lead and workers work. And "leader" does not always mean a single person. NO. In the best communities leadership roles are dynamic. For example: In such a diverse field as programming language design, no ONE person can lead all the time! We need people to step forth who are competent in the various areas of expertise. Someone who is really knowledgeable writing code for Linux may not be as knowledgeable writing code for windows. Some will be better at writing efficient algorithmic code and others may be better at documentation. etc,etc... HOWEVER Even IF we adopt the intelligent paradigm of dynamic leadership, we still need a small group (or at least) one very intelligent person to bind the process together. Guido van Rossum IS that person. That is why i call for him to get more involved. Of course i don't expect him to write code. But at least he could be more involved in this community. AND PYDEV IS NOT THE ONLY MAILING LIST IN THIS FREAKING COMMUNITY! He needs to step out from behind the coattails of his dev group and be a real leader. He needs to make his opinion known about Tkinter, IDLE, and also, the drastic changes that will need to be implemented in Python 4000. How can HE expect a true FLOSS community to move forward when SO many are just blatantly ignored? HOW? > -- and > certainly not projects that you demand we work on. I am NOT demanding that people WORK on anything Steven. I am ASKING for people to get more involved! Just open your mouth and state your opinion (and not just troll all the time!). But as long as Guido and the other members of pydev refuse to acknowledge the existence of c.l.p, we will forever be a stagnate community. > Instead of demanding that GvR order us to work on IDLE, put your money > where your mouth is, Rick, and stop being a blow-hard. You've been > nagging us about IDLE for long enough -- what is it now, two years? > Three? I haven't been counting, but it feels like geological ages. > > Start a project to fix IDLE. There is plenty of free hosting on the > Internet for open source projects, like GitHub and Google Code Hosting. > Track some bugs. Plan some concrete enhancements. Write some actual code. > Demonstrate actual effort, and you may attract others who care. > Otherwise, you're wasting our time. I will do this, as soon as the "leaders" start to lead. Only a fool would invest time to make a module or package "stdlib ready" without getting the input of his fellow brethren first. Sounds like a recipe for disaster to me. Why has Guido not, at the very least, contacted me privately? He could remain anonymous. From lie.1296 at gmail.com Mon Dec 26 13:58:43 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 27 Dec 2011 05:58:43 +1100 Subject: Plot seems weird In-Reply-To: <74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com> References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> <74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com> Message-ID: On 12/27/2011 04:08 AM, Yigit Turgut wrote: > On Dec 26, 11:28 am, Lie Ryan wrote: >> On 12/26/2011 05:27 AM, Yigit Turgut wrote: >> >> >> >> >> >> >> >> >> >>> On Dec 25, 7:06 pm, Rick Johnson wrote: >>>> On Dec 25, 9:33 am, Yigit Turgut wrote: >>>>> Hi all, >> >>>>> I have a text file as following; >> >>>>> 0.200047 0.000000 >>>>> 0.200053 0.160000 >>>>> 0.200059 0.000000 >>>>> 0.200065 0.080000 >>>>> 0.200072 0.000000 >>>>> 0.200078 0.160000 >> >>>>> And I am trying to plot it with ; >> >>>>> filenames = sys.argv[1:] >>>>> if len(filenames) == 0: >>>>> filenames = [sys.stdin] >>>>> for filename in filenames: >>>>> t,y1 = numpy.genfromtxt(filename, unpack=True) >>>>> pyplot.plot(t,y1) >>>>> pyplot.show() >> >>>>> But graph seems weird, not as it supposed to be. Any ideas ? >> >>>> Interesting. Of course "weird" leaves a LOT to be desired. On a scale >>>> of 1-10, how "weird" is the result? >> >>> I apply a 1Khz test signal just to see if things run smoothly, but I >>> see spikes at lower and higher ends (logic 0,1) where I should see a >>> clean rectangle pwm signal. By the look of it I say weirdness is >>> around 3/10. >> >> What are you expecting? Your data produces something that looks like the >> plot on the right of this screenshot >> (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with >> that; if you are expecting a square-wave-like plot (like on the left), >> then you should use a square-wave-like data, pyplot wouldn't magically >> transform a spiked-plot to squared-plot. >> >> Here's what I use to convert the data on right plot to data on left >> plot, I don't know much about numpy so it might be possible to do it >> more efficiently or numpy might even have something like it already. >> >> from itertools import izip_longest >> def to_square(t, y1): >> sq_data = [[], []] >> for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): >> sq_data[0].append(x) >> sq_data[1].append(y) >> sq_data[0].append(xn) >> sq_data[1].append(y) >> return numpy.array(sq_data, dtype=float) > > Thanks for the tip. I know that I feed a square wave signal and record > this data. Thus I believe the situation can be related to sampling > frequency. It is due to sampling frequency, but also because you cannot sample a square wave perfectly because square wave has infinite steepness at the transitions. Although if you know the exact timing of the transitions, it may be possible to reconstruct the transitions perfectly. > Couldn't get your code working, maybe because I import the data from > file. not your fault, I made a mistake when copy-pasteing the code, here's the fixed code: from itertools import izip_longest def to_square(data): sq_data = [[], []] for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): sq_data[0].append(x) sq_data[1].append(y) sq_data[0].append(xn) sq_data[1].append(y) return numpy.array(sq_data, dtype=float) use it like this: t,y1 = to_square(numpy.genfromtxt(filename, unpack=True)) pyplot.plot(t,y1) pyplot.show() From y.turgut at gmail.com Mon Dec 26 14:14:41 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Mon, 26 Dec 2011 11:14:41 -0800 (PST) Subject: Plot seems weird References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> <74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com> Message-ID: <96ac870e-dffe-4556-aa3a-fab84333ceda@o9g2000vbc.googlegroups.com> On Dec 26, 8:58?pm, Lie Ryan wrote: > On 12/27/2011 04:08 AM, Yigit Turgut wrote: > > > > > > > > > > > On Dec 26, 11:28 am, Lie Ryan ?wrote: > >> On 12/26/2011 05:27 AM, Yigit Turgut wrote: > > >>> On Dec 25, 7:06 pm, Rick Johnson ? ?wrote: > >>>> On Dec 25, 9:33 am, Yigit Turgut ? ?wrote: > >>>>> Hi all, > > >>>>> I have a text file as following; > > >>>>> 0.200047 ? ? ? ?0.000000 > >>>>> 0.200053 ? ? ? ?0.160000 > >>>>> 0.200059 ? ? ? ?0.000000 > >>>>> 0.200065 ? ? ? ?0.080000 > >>>>> 0.200072 ? ? ? ?0.000000 > >>>>> 0.200078 ? ? ? ?0.160000 > > >>>>> And I am trying to plot it with ; > > >>>>> filenames = sys.argv[1:] > >>>>> if len(filenames) == 0: > >>>>> ? ? ? filenames = [sys.stdin] > >>>>> for filename in filenames: > >>>>> ? ? ? t,y1 = numpy.genfromtxt(filename, unpack=True) > >>>>> ? ? ? pyplot.plot(t,y1) > >>>>> ? ? ? pyplot.show() > > >>>>> But graph seems weird, not as it supposed to be. Any ideas ? > > >>>> Interesting. Of course "weird" leaves a LOT to be desired. On a scale > >>>> of 1-10, how "weird" is the result? > > >>> I apply a 1Khz test signal just to see if things run smoothly, but I > >>> see spikes at lower and higher ends (logic 0,1) where I should see a > >>> clean rectangle pwm signal. By the look of it I say weirdness is > >>> around 3/10. > > >> What are you expecting? Your data produces something that looks like the > >> plot on the right of this screenshot > >> (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with > >> that; if you are expecting a square-wave-like plot (like on the left), > >> then you should use a square-wave-like data, pyplot wouldn't magically > >> transform a spiked-plot to squared-plot. > > >> Here's what I use to convert the data on right plot to data on left > >> plot, I don't know much about numpy so it might be possible to do it > >> more efficiently or numpy might even have something like it already. > > >> from itertools import izip_longest > >> def to_square(t, y1): > >> ? ? ? sq_data = [[], []] > >> ? ? ? for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): > >> ? ? ? ? ? sq_data[0].append(x) > >> ? ? ? ? ? sq_data[1].append(y) > >> ? ? ? ? ? sq_data[0].append(xn) > >> ? ? ? ? ? sq_data[1].append(y) > >> ? ? ? return numpy.array(sq_data, dtype=float) > > > Thanks for the tip. I know that I feed a square wave signal and record > > this data. Thus I believe the situation can be related to sampling > > frequency. > > It is due to sampling frequency, but also because you cannot sample a > square wave perfectly because square wave has infinite steepness at the > transitions. Although if you know the exact timing of the transitions, > it may be possible to reconstruct the transitions perfectly. > > > Couldn't get your code working, maybe because I import the data from > > file. > > not your fault, I made a mistake when copy-pasteing the code, here's the > fixed code: > > from itertools import izip_longest > def to_square(data): > ? ? ? sq_data = [[], []] > ? ? ? for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): > ? ? ? ? ? sq_data[0].append(x) > ? ? ? ? ? sq_data[1].append(y) > ? ? ? ? ? sq_data[0].append(xn) > ? ? ? ? ? sq_data[1].append(y) > ? ? ? return numpy.array(sq_data, dtype=float) > > use it like this: > > t,y1 = to_square(numpy.genfromtxt(filename, unpack=True)) > pyplot.plot(t,y1) > pyplot.show() Significant improvement on the plot, pretty interesting. It runs ok but I need to know how?! (: From wolftracks at invalid.com Mon Dec 26 14:37:41 2011 From: wolftracks at invalid.com (W. eWatson) Date: Mon, 26 Dec 2011 11:37:41 -0800 Subject: Is my IDLE problem caused by .idlerc? Permissions. In-Reply-To: References: Message-ID: On 12/26/2011 10:16 AM, W. eWatson wrote: > On 12/24/2011 11:35 AM, Dennis Lee Bieber wrote: >> On Sat, 24 Dec 2011 10:55:48 -0800, "W. eWatson" >> wrote: >> >>> >>> Permissions as follows: >>> SYSTEM: All. From Full control to write >>> Account Unknown(S-1-5-21...): read&exec, Read >>> Wayne: (normal use) All. From Full control to write >>> Admin: All. From Full control to write >>> WMPNetwork: Read >>> >>> Comments? >>> >>> .idlerc permissions are slightly different for Account Unknown. >> >> "Account Unknown" refers to a user account that has been deleted >> (or, via some other system corruption, is no longer associated with a >> login name -- happened to me earlier this year: my user account "name" >> had been "Wulfraed" but the user account directory was under "Dennis Lee >> Bieber"... When the registry crashed it disconnected name "Wulfraed" >> from directory "Dennis Lee Bieber"... Lost a month of email once I got >> done creating a new user account where directory and name matched and >> restored from backup [I failed to back-up the old user directory before >> trying a remove/create user]). >> >> My suggestion would be, first, remove privileges for that remnant >> account where-ever found; check higher directory levels too, and try to >> propagate the privilege downwards. >> >> Second -- could the file itself be set for read-only on the simple >> properties? I just tested with Win7 -- setting the general "read-only" >> did not make any changes to any of the security tab "users". Third, >> confirm the owner of the file is your current user account; if not,see >> if you can get "take ownership" option for the file to work. >> >> > I wonder if removing the "Account Unknown" account might just do it. Not > quite familiar with Owner, but I'll look around. > > I'll check the file for "read only". Solved. Moving .idlerc to another folder for safe keeping, and letting IDLE create a new .idlerc folder did it. I don't have the slightest idea why that worked, but the troublesome file was dated 2/2010, probably from installing 2.5.2, and never touched again by attempts either to re-install 2.5.2 or move up to 2.7.2. From rosuav at gmail.com Mon Dec 26 14:56:14 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 06:56:14 +1100 Subject: Python education survey In-Reply-To: <90a19db5-487a-4af6-92ff-8654f1091283@p41g2000yqm.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> <4ef8b732$0$29973$c3e8da3$5496439d@news.astraweb.com> <90a19db5-487a-4af6-92ff-8654f1091283@p41g2000yqm.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 5:32 AM, Rick Johnson wrote: > Why has Guido not, at the very least, contacted me > privately? He could remain anonymous. And how would you know if he did contact you anonymously? As to your demand that one of the "top Pythionistas" [sic] say something? I declare hereby that I am, in fact, a top Pythonista. [1] Rick, go fork Python onto Bitbucket. Call it Python 4000 if you like, or just "Python with a better IDLE" or whatever. Go! Shoo! Code! There. A top Pythonista has spoken. And if the above satisfy you not, I actually said it out loud too, just to be sure. That's how important you are to The Python Community (capital letters and all). ChrisA [1] Start here and follow the thread. I've done all of them. (Actually I probably haven't, and I haven't even read the whole thread, which just came up when I googled 'pythonista', but I defy you to prove me lying.) http://mail.python.org/pipermail/python-list/2003-June/207114.html From ian.g.kelly at gmail.com Mon Dec 26 15:01:20 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 26 Dec 2011 13:01:20 -0700 Subject: Multithreading In-Reply-To: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 11:31 AM, Yigit Turgut wrote: > I have a loop as following ; > > start = time.time() > end = time.time() - start > ?while(end ? ? ? ? ?data1 = self.chan1.getWaveform() > ? ? ? ? ?end = time.time() - start > ? ? ? ? ?timer.tick(10) ?#FPS > ? ? ? ? ?screen.fill((255,255,255) if white else(0,0,0)) > ? ? ? ? ?white = not white > ? ? ? ? ?pygame.display.update() > ? ? ? ? ?for i in range(self.size): > ? ? ? ? ? ? ?end = time.time() - start > ? ? ? ? ? ? ?f.write("%3.8f\t%f\n"%(end,data1[i])) > > Roughly speaking, this loop displays something at 10 frames per second > and writes data1 to a file with timestamps. > > At first loop data1 is grabbed but to grab the second value (second > loop) it needs to wait for timer.tick to complete. When I change FPS > value [timer.tick()], capturing period (time interval between loops) > of data1 also changes. What I need is to run ; > > ? ? ? ? ?timer.tick(10) ?#FPS > ? ? ? ? ?screen.fill((255,255,255) if white else(0,0,0)) > ? ? ? ? ?white = not white > ? ? ? ? ?pygame.display.update() > > for N seconds but this shouldn't effect the interval between loops > thus I will be able to continuously grab data while displaying > something at X fps. > > What would be an effective workaround for this situation ? You essentially have two completely independent loops that need to run simultaneously with different timings. Sounds like a good case for multiple threads (or processes if you prefer, but these aren: def write_data(self, f, N): start = time.time() while self.has_more_data(): data1 = self.chan1.getWaveform() time.sleep(N) for i in range(self.size): end = time.time() - start f.write("%3.8f\t%f\n" % (end, data[i])) def write_data_with_display(self, f, N, X): thread = threading.Thread(target=self.write_data, args=(f, N)) thread.start() white = False while thread.is_alive(): timer.tick(X) screen.fill((255, 255, 255) if white else (0, 0, 0)) white = not white pygame.display.update() From ian.g.kelly at gmail.com Mon Dec 26 15:03:04 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 26 Dec 2011 13:03:04 -0700 Subject: Multithreading In-Reply-To: References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 1:01 PM, Ian Kelly wrote: > You essentially have two completely independent loops that need to run > simultaneously with different timings. ?Sounds like a good case for > multiple threads (or processes if you prefer, but these aren: I accidentally sent before I was finished. I was saying "or processes if you prefer, but these aren't CPU-bound, so why complicate things?" Cheers, Ian From y.turgut at gmail.com Mon Dec 26 15:13:01 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Mon, 26 Dec 2011 12:13:01 -0800 (PST) Subject: Multithreading References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> Message-ID: On Dec 26, 10:03?pm, Ian Kelly wrote: > On Mon, Dec 26, 2011 at 1:01 PM, Ian Kelly wrote: > > You essentially have two completely independent loops that need to run > > simultaneously with different timings. ?Sounds like a good case for > > multiple threads (or processes if you prefer, but these aren: > > I accidentally sent before I was finished. ?I was saying "or processes > if you prefer, but these aren't CPU-bound, so why complicate things?" > > Cheers, > Ian I had thought the same workaround but unfortunately loop is already under a def ; def writeWaveform(self, fo, header=''): data1 = numpy.zeros(self.size) screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) timer = pygame.time.Clock() white = True fo.write(header) start = time.time() end = time.time() - start while(end<10): data1 = self.chan1.getWaveform() end = time.time() - start timer.tick(10) #FPS screen.fill((255,255,255) if white else(0,0,0)) white = not white pygame.display.update() for i in range(self.size): end = time.time() - start f.write("%3.8f\t%f\n"%(end,data1[i])) From hoogendoorn.eelco at gmail.com Mon Dec 26 15:39:30 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 12:39:30 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> On Dec 25, 5:15?pm, Steven D'Aprano wrote: > On Sun, 25 Dec 2011 06:55:28 -0800, Eelco wrote: > > Anyway, ?braces are used at > > least an order of magnitude more than collection packing/ unpacking in > > typical code. > > That's a wild and unjustified claim. Here's a quick and dirty test, using > the standard library as an example of typical idiomatic code: > > [steve at orac ~]$ cd /usr/lib/python2.6 > [steve at orac python2.6]$ grep "[*]args" *.py | wc -l > 270 > [steve at orac python2.6]$ grep "{" *.py | wc -l > 550 > > Doesn't look like a factor of 10 difference to me. Now try it without changing the subject from round braces to everything but round braces. From ian.g.kelly at gmail.com Mon Dec 26 15:39:48 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 26 Dec 2011 13:39:48 -0700 Subject: Multithreading In-Reply-To: References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 1:13 PM, Yigit Turgut wrote: > I had thought the same workaround but unfortunately loop is already > under a def ; So nest the functions, or refactor it. Either way, that shouldn't be a significant obstacle. From hoogendoorn.eelco at gmail.com Mon Dec 26 15:58:36 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 12:58:36 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> Message-ID: <14c228af-0de5-448f-b617-832173aa6cba@p4g2000vbt.googlegroups.com> On Dec 25, 5:23?pm, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 2:38 AM, Eelco wrote: > > Until that time, im going > > to ask you to take 'type constraint' by its literal meaning; a > > coercion of the type of a symbol, rather than whatever particular > > meaning it has acquired for you (it might help if you explained that). > > Im not sure if it was you that brought that up, but let me reiterate > > that I dont mean a 'type cast', which is a runtime concept. A 'type > > constraint' is purely a linguistic construct that will be 'compiled > > out') > > The dictionary definition of constraint is "a limitation or > restriction", and you're right that it can be "compiled out". In fact, > that is probably the best definition. Assuming everything is written > correctly, you should be able to eliminate all constraints and the > code will still function correctly*; but having the constrains means > that certain illegal operations will throw errors. > > Here's two examples of tuple unpacking, one with a type constraint, > the other without: > > a, b = ('hello', [1,2,3] ) > a, b::list = ('hello', [1,2,3] ) > > The constraint on the second line means that, if the second element is > not a list, the interpreter should throw an error. It does NOT mean to > completely change the meaning of the statement to _make_ the last > argument into a list. That is not the job of a constraint. Thank you for providing clarification on what a 'type constraint' means to you. That clears things up a bit. What you are talking about goes by the name of a 'dynamic type CHECK'; some kind of syntactic sugar for something like 'assert(type(obj)==sometype)'. Like a 'type cast', this is also a runtime concept. How you manage to confuse that with what I am talking about, given that ive stated many times I am not talking about a runtime construct but a compile-time construct, is quite beyond me. (not to mention that ive quite explicitly stated what I mean by 'type constraint' many times now). By contrast, here is the first google hit for 'type constraint'. http://msdn.microsoft.com/en-us/library/d5x73970.aspx Note that this is a different application of the concept of a type constraint, but nonetheless, the concept is as I stated it: a constraint to the type of a symbol to modify its compile-time semantics. To cite from its first paragraph: "...you can apply restrictions to the kinds of types ... by using a type that is not allowed by a constraint, the result is a COMPILE-TIME ERROR" (emphasis mine) From rosuav at gmail.com Mon Dec 26 16:01:34 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 08:01:34 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 7:39 AM, Eelco wrote: > Now try it without changing the subject from round braces to > everything but round braces. Around here, the term "braces" means the curly ones - { and } - that delimit blocks of code in C, and dictionaries/sets in Python. "Brackets" may be what you're looking for, if you mean all of ()[]{}. Or if you just mean (), they're called "parentheses". If your point is that parens are used more often than packing/unpacking, that's almost certainly true, since function calls (including method invocations) are so prevalent in pretty much any code. But what does that prove? ChrisA From rosuav at gmail.com Mon Dec 26 16:05:18 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 08:05:18 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <14c228af-0de5-448f-b617-832173aa6cba@p4g2000vbt.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <14c228af-0de5-448f-b617-832173aa6cba@p4g2000vbt.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 7:58 AM, Eelco wrote: > What you are talking about goes by the name of a 'dynamic type CHECK'; > some kind of syntactic sugar for something like > 'assert(type(obj)==sometype)'. Like a 'type cast', this is also a > runtime concept... > > By contrast, here is the first google hit for 'type constraint'. > > http://msdn.microsoft.com/en-us/library/d5x73970.aspx > > "...you can apply restrictions to the kinds of types ... by using a > type that is not allowed by a constraint, the result is a COMPILE-TIME > ERROR" (emphasis mine) A constraint can be applied at compile time or at run time. It'd be valid to apply them at edit time, if you so chose - your editor could refuse to save your file until you fix the problem. Doesn't mean a thing. Python, by its nature, cannot do compile-time type checking. Under no circumstances, however, does this justify the use of the term "constraint" to mean "utterly different semantics of the same code". ChrisA From hoogendoorn.eelco at gmail.com Mon Dec 26 16:41:34 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 13:41:34 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <4ef757e3$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 25, 6:05?pm, Steven D'Aprano wrote: > On Sun, 25 Dec 2011 07:38:17 -0800, Eelco wrote: > > On Dec 25, 2:12?pm, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> wrote: > >> On Sat, 24 Dec 2011 06:39:39 -0800, Eelco wrote: > >> > On Dec 20, 4:30?am, alex23 wrote: > >> >> On Dec 19, 8:15?pm, Eelco wrote: > > >> >> > What does that have to do with collection packing/unpacking? > > >> >> It's mocking your insistance that collection unpacking is a type > >> >> constraint. > > >> > This is really going to be the last time I waste any words on this: > > >> If only that were true, but after sending this post, you immediately > >> followed up with FIVE more posts on this subject in less than half an > >> hour. > > > Did I waste any more words on collection packing and type constraints? > > No, I did not. (though I am about to, and am willing to do so for every > > question that seems genuinely aimed at engaging me on the matter) > > > Did I intend to say that I was going to let a single troll shut down my > > entire topic? No, I did not. > > Ah, well whatever you *intended* wasn't clear from your comment. At least > not clear to *me*. Always glad to help. > > Yes, indeed it would be abuse of language to call this a type > > constraint, since the fact that y is a list is indeed an outcome of > > whatever happens to pop out at the right hand side. One could redefine > > the identifier list to return any kind of object. > > So far, we agree on this. Good. > > How is 'head, *tail = sequence' or semantically entirely equivalently, > > 'head, tail::list = sequence' any different then? Of course after > > interpretation/compilation, what it boils down to is that we are > > constructing a list and binding it to the identifier tail, but that is > > not how it is formulated in python as a language > > I'm afraid it is. > > Here's the definition of assignment in Python 3:http://docs.python.org/py3k/reference/simple_stmts.html#assignment- > statements Que? 'head, *tail = sequence' Is how one currently unpacks a head and tail in idiomatic python This is semantically equivalent to 'head = sequence[0]' 'tail = list(sequence[1:])' But these forms are linguistically different, in too many different ways to mention. > > We dont have > > something of the form 'tail = list_tail(sequence)'. > > I'm afraid we do. See the definition of assignment again. Que? My claim is that the two semantically identical formulations above do not have isomorphic linguistic form. As far as I can make sense of your words, you seem to be disputing this claim, but its a claim as much worth debating as that the sun rises in the east. > > Rather, we annotate > > the identifier 'tail' with an attribute that unquestionably destinates > > it to become a list*. It is no longer that 'tail' will just take > > anything that pops out of the expression on the right hand side; > > Of course it will. Python is a dynamically typed language. It doesn't > suddenly develop static types to ensure that 'tail' becomes a list; > 'tail' is bound to a list because that's what the assignment statement > provides. How python accomplishes any of this under the hood is entirely immaterial. The form is that of a compile-time type constraint, regardless of whether the BDFL ever thought about it in these terms. > > rather, > > the semantics of what will go on at right hand side is coerced by the > > constraint placed on 'tail'. > > But it isn't a constraint placed on 'tail'. It is a consequence of the > definition of assignment in Python 3. 'tail' becomes bound to a list > because that is what the assignment statement is defined to do in that > circumstance, not because the identifier (symbol) 'tail' is constrained > to only accept lists. 'tail' may not even exist before hand, so talking > about constraints on 'tail' is an abuse of language, AS YOU AGREED ABOVE. 'tail' is (re)declared on the spot as a brand-new identifier (type constraint included); whether it exists before has no significance whatsoever, since python allows rebinding of identifiers. > > *(I call that a 'type constraint', because that is what it literally is; > > No. It is literally a name binding of a dynamically typed, unconstrained > name to an object which happens to be a list. Let me take a step back and reflect on the form of the argument we are having. I claim the object in front of us is a 'cube'. You deny this claim, by countering that it is 'just a particular configuration of atoms'. 'look at the definition!', you say; 'its just a list of coordinates, no mention of cubes whatsoever.'. 'Look at the definition of a cube', I counter. 'This particular list of coordinates happens to fit the definition, whether the BDFT intended to or not'. You are correct, in the sense that I do not disagree that it is a particular configuration of atoms. But if you had a better understanding of cubes, youd realize it meets the definition; the fact that people might not make a habit of pausing at this fact (there is generally speaking not much of a point in doing so in this case), does not diminish the validity of this perspective. But again, if this perspective does not offer you anything useful, feel free not to share in it. > > if you can make a case that this term has acquired a different meaning > > in practice, and that there is another term in common use for this kind > > of construct; please enlighten me. Until that time, im going to ask you > > to take 'type constraint' by its literal meaning; a coercion of the type > > of a symbol, > > But THERE IS NO COERCION OF THE TYPE OF THE SYMBOL. > > I am sorry for shouting, but you seem oblivious to the simple fact that > Python is not statically typed, and the symbol 'tail' is NOT coerced to a > specific type. 'tail' may not even exist before the assignment is made; > if it does exist, it could be *any type at all* -- and after the > assignment takes place, there are no restrictions on subsequent > assignments. > > 'head, *tail = sequence' is no more a type constraint than 'x = 1' is. > > Whatever the virtues of your proposal, you are doing it incalculable harm > by your insistence on this incorrect model of Python's behaviour. Whatever the virtues of your critique of my proposal, you might end up wasting less time doing so by reading a book or two on compiler theory. From hoogendoorn.eelco at gmail.com Mon Dec 26 16:51:50 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 13:51:50 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> Message-ID: <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> On Dec 26, 10:01?pm, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 7:39 AM, Eelco wrote: > > Now try it without changing the subject from round braces to > > everything but round braces. > > Around here, the term "braces" means the curly ones - { and } - that > delimit blocks of code in C, and dictionaries/sets in Python. > "Brackets" may be what you're looking for, if you mean all of ()[]{}. > Or if you just mean (), they're called "parentheses". > > If your point is that parens are used more often than > packing/unpacking, that's almost certainly true, since function calls > (including method invocations) are so prevalent in pretty much any > code. But what does that prove? That proves the original point of contention: that the below* is suboptimal language design, not because terseness always trumps verbosity, but because commonly-used constructs (such as parenthesis or round brackets or whatever you wish to call them) are more deserving of the limited space in both the ascii table and your reflexive memory, than uncommonly used ones. *original mock code by steve: class MyClass superclasslist A, B C: def method argumentlist self, x, y: t = tuple 1, 2 tuple 3, 4 endtuple endtuple return group x + y endgroup * group x - y endgroup From hoogendoorn.eelco at gmail.com Mon Dec 26 17:12:23 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 14:12:23 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <14c228af-0de5-448f-b617-832173aa6cba@p4g2000vbt.googlegroups.com> Message-ID: <82fd3bec-5afa-4e87-ae9a-8f93ecb9e366@dp8g2000vbb.googlegroups.com> On Dec 26, 10:05?pm, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 7:58 AM, Eelco wrote: > > What you are talking about goes by the name of a 'dynamic type CHECK'; > > some kind of syntactic sugar for something like > > 'assert(type(obj)==sometype)'. Like a 'type cast', this is also a > > runtime concept... > > > By contrast, here is the first google hit for 'type constraint'. > > >http://msdn.microsoft.com/en-us/library/d5x73970.aspx > > > "...you can apply restrictions to the kinds of types ... by using a > > type that is not allowed by a constraint, the result is a COMPILE-TIME > > ERROR" (emphasis mine) > > A constraint can be applied at compile time or at run time. It'd be > valid to apply them at edit time, if you so chose - your editor could > refuse to save your file until you fix the problem. Doesn't mean a > thing. A constraint in the sense that I have explained many times now, can in no way, shape or form be applied at run time. Youd have better luck applying a consternation to a squirrel. Perhaps you meant 'type check' again? But then again, that makes no sense whatsoever at compile- time... Im starting to doubt if there is any sense to be found here at all. Anyway, ill take your further silence on the matter as a 'sorry I derailed your thread with my confusion of terminology' > Python, by its nature, cannot do compile-time type checking. Python can do whatever its designers have put into it. In this case, that includes the emission of different code based on a (type) annotation at the point of declaration of an identifier (only in the particular circumstance of collection unpacking though, as far as I am aware). > Under no circumstances, however, does this justify the use of the term > "constraint" to mean "utterly different semantics of the same code". Thank you for your theory on justice. Im sure its fascinating, but until you get around to actually explaining it, im going to have to be conservative and stick with the jargon in common use though, sorry. From charleshixsn at earthlink.net Mon Dec 26 17:23:03 2011 From: charleshixsn at earthlink.net (Charles Hixson) Date: Mon, 26 Dec 2011 14:23:03 -0800 Subject: Possible bug in string handling (with kludgy work-around) Message-ID: <4EF8F3C7.3010005@earthlink.net> This doesn't cause a crash, but rather incorrect results. self.wordList = ["The", "quick", "brown", "fox", "carefully", "jumps", "over", "the", "lazy", "dog", "as", "it", "stealthily", "wends", "its", "way", "homewards", '\b.'] for i in range (len (self.wordList) ): if not isinstance(self.wordList[i], str): self.wordList = "" elif self.wordList[i] != "" and self.wordList[i][0] == "\b": print ("0: wordList[", i, "] = \"", self.wordList[i], "\"", sep = "") print ("0a: wordList[", i, "][1] = \"", self.wordList[i][1], "\"", sep = "") tmp = self.wordList[i][1] ## !! Kludge -- remove tmp to see the error self.wordList[i] = tmp + self.wordList[i][1:-1] ## !! Kludge -- remove tmp + to see the error print ("1: wordList[", i, "] = \"", self.wordList[i], "\"", sep = "") print ("len(wordList[", i, "]) = ", len(self.wordList[i]) ) -- Charles Hixson From rosuav at gmail.com Mon Dec 26 17:27:31 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 09:27:31 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 8:51 AM, Eelco wrote: > That proves the original point of contention: that [Steve's demo code] is > suboptimal language design, not because terseness always trumps > verbosity, but because commonly-used constructs (such as parenthesis > or round brackets or whatever you wish to call them) are more > deserving of the limited space in both the ascii table and your > reflexive memory, than uncommonly used ones. In Magic: The Gathering R&D, they have a term (the article reporting which I can't actually find at the moment) called "spread complexity" or "fan complexity" - the idea being that as you fan out a booster pack, you see a certain amount of complexity in front of you. The designers can afford to put more complex cards in as rares than they can as commons, because you see ten commons for every rare - so a common factors ten times as much as a rare in spread complexity. (Mark Rosewater, my apologies if I'm misremembering here!) The same applies here. When you cast your eye over a program, you're going to see certain syntactic elements a lot. Assignment, arithmetic, blocks of code (ie indent/dedent), and function calls are all extremely common; lambdas, the use of decorators, and exception handling are somewhat uncommon; and metaclasses, the writing of decorators, and reloading of modules are all quite rare. The elements that occur frequently should be: a) Readable and grokkable; b) Easily typed on a regular keyboard - no using ASCII character 126 to mean negation, tyvm! c) Make sense. Rarer elements (and I'm not talking about xenon and plutonium here) are allowed to have long names, obscure syntax, or even be shoved away in odd modules (the way module reloading is in Python 3). If 0.1% of your code is suddenly twice as large as it used to be, will you notice? But if a new syntax adds even 5% to the mindspace requirement of basic assignment, your code will majorly suffer. In summary: Terseness trumps verbosity primarily for common operations, and only when doing so does not violate rules a and c above. ChrisA From rosuav at gmail.com Mon Dec 26 17:37:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 09:37:06 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <82fd3bec-5afa-4e87-ae9a-8f93ecb9e366@dp8g2000vbb.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <14c228af-0de5-448f-b617-832173aa6cba@p4g2000vbt.googlegroups.com> <82fd3bec-5afa-4e87-ae9a-8f93ecb9e366@dp8g2000vbb.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 9:12 AM, Eelco wrote: > On Dec 26, 10:05?pm, Chris Angelico wrote: >> A constraint can be applied at compile time or at run time. It'd be >> valid to apply them at edit time, if you so chose - your editor could >> refuse to save your file until you fix the problem. Doesn't mean a >> thing. > > A constraint in the sense that I have explained many times now, can in > no way, shape or form be applied at run time. Youd have better luck > applying a consternation to a squirrel. Perhaps you meant 'type check' > again? But then again, that makes no sense whatsoever at compile- > time... Im starting to doubt if there is any sense to be found here at > all. A constraint failure causes an error at the time it's discovered. 1) Your editor pops up a message the instant you type something with such an error, and forces you to correct it before going on. 2) Your compiler refuses to produce byte-code for the module. 3) When the line of code is executed, an exception is thrown. All of these are valid ways of handling a constraint. Here is a Python example of a type constraint: def foo(arg): if not isinstance(arg,list): raise "This won't work." If you call it with something that's not a list, you get an error. Call it with a list, and execution continues normally. That's a constraint. Of course, it might not be a _type_ constraint: def foo(arg): if arg>5: raise "This won't work either." # and yes, that's oddly truer in Python 3 (Aside: In Pike, I can actually put that into a type constraint (declare the argument to be int(5..) for instance). This, however, is not germane to the conversation.) > Anyway, ill take your further silence on the matter as a 'sorry I > derailed your thread with my confusion of terminology' Like Mary Poppins, I never apologize for derailing threads :) >> Python, by its nature, cannot do compile-time type checking. > > Python can do whatever its designers have put into it. In this case, > that includes the emission of different code based on a (type) > annotation at the point of declaration of an identifier (only in the > particular circumstance of collection unpacking though, as far as I am > aware). Of course, but how can Python, without a completely different structure, do compile-time checking of object types? Everything can be monkey-patched at run-time. Anything could be affected by any function call. It's impossible to say for certain, at compile time, what data type anything will have. ChrisA From tinnews at isbd.co.uk Mon Dec 26 17:37:36 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Mon, 26 Dec 2011 22:37:36 +0000 Subject: How to check for single character change in a string? References: Message-ID: Roy Smith wrote: > In article , > Roy Smith wrote: > > > >>> len([x for x in zip(s1, s2) if x[0] != x[1]]) > > Heh, Ian Kelly's version: > > > sum(a == b for a, b in zip(str1, str2)) > > is cleaner than mine. Except that Ian's counts matches and the OP asked > for non-matches, but that's an exercise for the reader :-) :-) I'm actually walking through a directory tree and checking that file characteristics don't change in a sequence of files. What I'm looking for is 'unusual' changes in file characteristics (they're image files with camera information and such in them) in a sequential list of files. Thus if file001, file002, file003, file004 have the same camera type I'm happy, but if file003 appears to have been taken with a different camera something is probably amiss. I realise there will be *two* character changes when going from file009 to file010 but I can cope with that. I can't just extract the sequence number because in some cases they have non-numeric names, etc. -- Chris Green From rosuav at gmail.com Mon Dec 26 17:44:59 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 09:44:59 +1100 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: <4EF8F3C7.3010005@earthlink.net> References: <4EF8F3C7.3010005@earthlink.net> Message-ID: On Tue, Dec 27, 2011 at 9:23 AM, Charles Hixson wrote: > This doesn't cause a crash, but rather incorrect results. You may need to be a bit clearer. What line of code (or what expression)? What did you expect to see, and what did you see? >From examining your code, I've come up with one most-likely scenario. In Python, indexing is zero-based and, in effect, indexes the boundaries between items rather than the items themselves. Referencing string[-1] actually means asking for the boundary between the second-last and last characters; using that as your end marker actually trims off the last character. What you may want is simply: self.wordList[i][1:] which means "from character position 1 to the end of the string". Hope that helps! Chris Angelico From rantingrickjohnson at gmail.com Mon Dec 26 17:48:35 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Mon, 26 Dec 2011 14:48:35 -0800 (PST) Subject: Possible bug in string handling (with kludgy work-around) References: Message-ID: <0063a1a0-0cbc-4352-90ce-13ab2eda6884@v24g2000yqk.googlegroups.com> On Dec 26, 4:23?pm, Charles Hixson wrote: > This doesn't cause a crash, but rather incorrect results. > > self.wordList ? ?= ? ?["The", "quick", "brown", "fox", "carefully", > ? ? ? ? ? ? ? ? ?"jumps", "over", "the", "lazy", "dog", "as", "it", > ? ? ? ? ? ? ? ? ?"stealthily", "wends", "its", "way", "homewards", '\b.'] > for ? ?i ? ?in ? ?range (len (self.wordList) ): > ? ? if ? ?not isinstance(self.wordList[i], str): > ? ? ? ? self.wordList = "" > ? ?elif self.wordList[i] != "" and self.wordList[i][0] == "\b": > ? ? ? ? print ("0: wordList[", i, "] = \"", self.wordList[i], "\"", sep > = "") > ? ? ? ? print ("0a: wordList[", i, "][1] = \"", self.wordList[i][1], > "\"", sep = "") > ? ? ? ? tmp ? ?= ? ?self.wordList[i][1] ? ? ? ? ? ? ## !! Kludge -- > remove tmp to see the error > ? ? ? ? self.wordList[i] ? ?= ? ?tmp + self.wordList[i][1:-1] ?## !! > Kludge -- remove tmp + to see the error > ? ? ? ? print ("1: wordList[", i, "] = \"", self.wordList[i], "\"", sep > = "") > ? ? ? ? print ? ?("len(wordList[", i, "]) = ", len(self.wordList[i]) ) > > -- > Charles Hixson Handy rules for reporting bugs: 1. Always format code properly. 2. Always trim excess fat from code. 3. Always include relative dependencies ("self.wordlist" is only valid inside a class. In this case, change the code to a state that is NOT dependent on a class definition.) Most times after following these simple rules, you'll find egg on your face BEFORE someone else has a chance to see it and ridicule you. From y.turgut at gmail.com Mon Dec 26 18:00:30 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Mon, 26 Dec 2011 15:00:30 -0800 (PST) Subject: Multithreading References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> Message-ID: <560bea15-6abc-45ed-ac7a-cf23ff44f130@e2g2000vbb.googlegroups.com> On Dec 26, 10:01?pm, Ian Kelly wrote: > On Mon, Dec 26, 2011 at 11:31 AM, Yigit Turgut wrote: > > I have a loop as following ; > > > start = time.time() > > end = time.time() - start > > ?while(end > ? ? ? ? ?data1 = self.chan1.getWaveform() > > ? ? ? ? ?end = time.time() - start > > ? ? ? ? ?timer.tick(10) ?#FPS > > ? ? ? ? ?screen.fill((255,255,255) if white else(0,0,0)) > > ? ? ? ? ?white = not white > > ? ? ? ? ?pygame.display.update() > > ? ? ? ? ?for i in range(self.size): > > ? ? ? ? ? ? ?end = time.time() - start > > ? ? ? ? ? ? ?f.write("%3.8f\t%f\n"%(end,data1[i])) > > > Roughly speaking, this loop displays something at 10 frames per second > > and writes data1 to a file with timestamps. > > > At first loop data1 is grabbed but to grab the second value (second > > loop) it needs to wait for timer.tick to complete. When I change FPS > > value [timer.tick()], capturing period (time interval between loops) > > of data1 also changes. What I need is to run ; > > > ? ? ? ? ?timer.tick(10) ?#FPS > > ? ? ? ? ?screen.fill((255,255,255) if white else(0,0,0)) > > ? ? ? ? ?white = not white > > ? ? ? ? ?pygame.display.update() > > > for N seconds but this shouldn't effect the interval between loops > > thus I will be able to continuously grab data while displaying > > something at X fps. > > > What would be an effective workaround for this situation ? > > You essentially have two completely independent loops that need to run > simultaneously with different timings. ?Sounds like a good case for > multiple threads (or processes if you prefer, but these aren: > > def write_data(self, f, N): > ? ? start = time.time() > ? ? while self.has_more_data(): > ? ? ? ? data1 = self.chan1.getWaveform() > ? ? ? ? time.sleep(N) > ? ? ? ? for i in range(self.size): > ? ? ? ? ? ? end = time.time() - start > ? ? ? ? ? ? f.write("%3.8f\t%f\n" % (end, data[i])) Why is there N variable in write_data function ? N is related to timer.tick(N) which is related to display function ? time.sleep(N) will pause writing to file for specified amount of time which is exactly what I am trying to avoid. From rosuav at gmail.com Mon Dec 26 18:05:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 10:05:22 +1100 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: <0063a1a0-0cbc-4352-90ce-13ab2eda6884@v24g2000yqk.googlegroups.com> References: <0063a1a0-0cbc-4352-90ce-13ab2eda6884@v24g2000yqk.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 9:48 AM, Rick Johnson wrote: > Handy rules for reporting bugs: > > 1. Always format code properly. > 2. Always trim excess fat from code. > 3. Always include relative dependencies ("self.wordlist" is only valid > inside a class. In this case, change the code to a state that is NOT > dependent on a class definition.) > > Most times after following these simple rules, you'll find egg on your > face BEFORE someone else has a chance to see it and ridicule you. 4. Don't take it personally when a known troll insults you. His advice, in this case, is valid; but don't feel that you're going to be ridiculed. We don't work that way on this list. ChrisA From hoogendoorn.eelco at gmail.com Mon Dec 26 18:44:51 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 15:44:51 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> Message-ID: <5aadbf8b-c905-4115-972c-b37d093126b4@p4g2000vbt.googlegroups.com> On Dec 26, 11:27?pm, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 8:51 AM, Eelco wrote: > > That proves the original point of contention: that [Steve's demo code] is > > suboptimal language design, not because terseness always trumps > > verbosity, but because commonly-used constructs (such as parenthesis > > or round brackets or whatever you wish to call them) are more > > deserving of the limited space in both the ascii table and your > > reflexive memory, than uncommonly used ones. > > In Magic: The Gathering R&D, they have a term (the article reporting > which I can't actually find at the moment) called "spread complexity" > or "fan complexity" - the idea being that as you fan out a booster > pack, you see a certain amount of complexity in front of you. The > designers can afford to put more complex cards in as rares than they > can as commons, because you see ten commons for every rare - so a > common factors ten times as much as a rare in spread complexity. (Mark > Rosewater, my apologies if I'm misremembering here!) > > The same applies here. When you cast your eye over a program, you're > going to see certain syntactic elements a lot. Assignment, arithmetic, > blocks of code (ie indent/dedent), and function calls are all > extremely common; lambdas, the use of decorators, and exception > handling are somewhat uncommon; and metaclasses, the writing of > decorators, and reloading of modules are all quite rare. > > The elements that occur frequently should be: > a) Readable and grokkable; > b) Easily typed on a regular keyboard - no using ASCII character 126 > to mean negation, tyvm! > c) Make sense. > > Rarer elements (and I'm not talking about xenon and plutonium here) > are allowed to have long names, obscure syntax, or even be shoved away > in odd modules (the way module reloading is in Python 3). If 0.1% of > your code is suddenly twice as large as it used to be, will you > notice? But if a new syntax adds even 5% to the mindspace requirement > of basic assignment, your code will majorly suffer. > > In summary: Terseness trumps verbosity primarily for common > operations, and only when doing so does not violate rules a and c > above. > > ChrisA Good to see there is something we agree upon completely. Not that I mean to say the question as to how verbose a syntax is appropriate for collection (un)packing is settled; one could reasonably argue they find tail::tuple too verbose. But parenthesis are not a terribly good example to compare to, since they are infact so much more used they are clearly in another category. *args and **kwargs are debateable in the appropriateness of their terseness (but I personally like to err on the side of verbosity), but extended collection unpacking, as in 'head,*tail=sequence', is quite a rare construct indeed, and here I very strongly feel a more explicit syntax is preferrable. That is, as a seasoned python 2 user, I wouldnt have been willing to gamble on what this does when id come across it for the first time in python 3. Could as well be a completely new use of the asterisk. But if collection packing/unpacking would be presented as a more general construct from the start, 'head,tail::tuple=sequence' would be hard to miss. From mauriceling at gmail.com Mon Dec 26 18:45:21 2011 From: mauriceling at gmail.com (mauriceling@acm.org) Date: Mon, 26 Dec 2011 15:45:21 -0800 (PST) Subject: Regular expressions Message-ID: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Hi I am trying to change "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N: 0:" to "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1". Can anyone help me with the regular expressions needed? Thanks in advance. Maurice From ater1980 at gmail.com Mon Dec 26 18:59:23 2011 From: ater1980 at gmail.com (Alex Ter-Sarkissov) Date: Tue, 27 Dec 2011 12:59:23 +1300 Subject: installing matplotlib in MacOs 10.6.8. Message-ID: thanks Ned, that's quite weird: if I run python2.7-32 in terminal, it works like you said, but when I add it as an interpreter in Eclipse, matplotlib.pyplot doesn't get imported for some reason. Even more strange, either way platform.architecture() reports 64-bit. What's wrong here? cheers, Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon Dec 26 19:00:08 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 11:00:08 +1100 Subject: Regular expressions In-Reply-To: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 10:45 AM, mauriceling at acm.org wrote: > Hi > > I am trying to change to . > > Can anyone help me with the regular expressions needed? A regular expression defines a string based on rules. Without seeing a lot more strings, we can't know what possibilities there are for each part of the string. You probably know your data better than we ever will, even eyeballing the entire set of strings; just write down, in order, what the pieces ought to be - for instance, the first token might be a literal @ sign, followed by three upper-case letters, then a hyphen, then any number of alphanumerics followed by a colon, etc. Once you have that, it's fairly straightforward to translate that into regex syntax. ChrisA From ater1980 at gmail.com Mon Dec 26 19:02:10 2011 From: ater1980 at gmail.com (Alex Ter-Sarkissov) Date: Tue, 27 Dec 2011 13:02:10 +1300 Subject: installing matplotlib in MacOs 10.6.8. In-Reply-To: References: Message-ID: thanks Ned, that's quite weird: if I run python2.7-32 in terminal, it works like you said, but when I add it as an interpreter in Eclipse, matplotlib.pyplot doesn't get imported for some reason. Even more strange, either way platform.architecture() reports 64-bit. What's wrong here? cheers, Alex 27 ??????? 2011 ?. 3:54 ???????????? Ned Deily ???????: > In article > , > Alex Ter-Sarkissov wrote: > > hi everyone, I run python 2.7.2. in Eclipse (recently upgraded from > 2.6). I > > have a problem with installing matplotlib (I found the version for python > > 2.7. MacOs 10.3, no later versions). If I run python in terminal using > arch > > -i386 python, and then > > > > from matplotlib.pylab import * > > > > and similar stuff, everything works fine. If I run python in eclipse or > > just without arch -i386, I can import matplotlib as > > > > from matplotlib import * > > > > but actually nothing gets imported. If I do it in the same way as above, > I > > get the message > > > > no matching architecture in universal wrapper > > > > which means there's conflict of versions or something like that. I tried > > reinstalling the interpreter and adding matplotlib to forced built-ins, > but > > nothing helped. For some reason I didn't have this problem with numpy and > > tkinter. > > The message means almost certainly means that the Python you are using > is a 64-bit/32-bit universal version. When you launch Python with -arch > i386, you force the Python to run in 32-bit mode so it is compatible > with the 32-bit-only version of matplotlib you've installed in it. When > you launch Python by default, it is runnning in 64-bit mode so the > 32-bit-only C extension modules in that version of matplotlib cannot be > loaded. You might want to ask the matplotlib developers to make a > 64-bit version available. In the mean time, you could try to configure > Eclipse to invoke the python with `python2.7-32`, which is included with > distributions like the python.org one to force Python to run in 32-bit > mode on OS X. > > -- > Ned Deily, > nad at acm.org > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Mon Dec 26 19:07:49 2011 From: roy at panix.com (Roy Smith) Date: Mon, 26 Dec 2011 19:07:49 -0500 Subject: Regular expressions References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Message-ID: In article <495b6fe6-704a-42fc-b10b-484218ad8409 at b20g2000pro.googlegroups.com>, "mauriceling at acm.org" wrote: > Hi > > I am trying to change "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N: > 0:" to "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1". > > Can anyone help me with the regular expressions needed? Easy-peasy: import re input = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N: 0:" output = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1" pattern = re.compile( r'@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N: 0:') out = pattern.sub( r'@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1', input) assert out == output To be honest, I wouldn't do this with a regex. I'm not quite sure what you're trying to do, but I'm guessing it's something like "Get everything after the first space in the string; keep just the integer that's before the first ':' in that and turn the space into a slash". In that case, I'd do something like: head, tail = input.split(' ', 1) number, _ = tail.split(':') print "%s/%s" % (head, number) From mauriceling at gmail.com Mon Dec 26 19:15:30 2011 From: mauriceling at gmail.com (mauriceling@acm.org) Date: Mon, 26 Dec 2011 16:15:30 -0800 (PST) Subject: Regular expressions References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Message-ID: <4be34afe-4291-414b-9212-498074400e39@v24g2000prn.googlegroups.com> On Dec 27, 8:00?am, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 10:45 AM, mauricel... at acm.org > > wrote: > > Hi > > > I am trying to change to . > > > Can anyone help me with the regular expressions needed? > > A regular expression defines a string based on rules. Without seeing a > lot more strings, we can't know what possibilities there are for each > part of the string. You probably know your data better than we ever > will, even eyeballing the entire set of strings; just write down, in > order, what the pieces ought to be - for instance, the first token > might be a literal @ sign, followed by three upper-case letters, then > a hyphen, then any number of alphanumerics followed by a colon, etc. > Once you have that, it's fairly straightforward to translate that into > regex syntax. > > ChrisA I've tried re.sub('@\S\s[1-9]:[A-N]:[0-9]', '@\S\s', '@HWI-ST115:568:B08LLABXX: 1:1105:6465:151103 1:N:0:') but it does not seems to work. From jason at powerpull.net Mon Dec 26 19:16:51 2011 From: jason at powerpull.net (Jason Friedman) Date: Tue, 27 Dec 2011 00:16:51 +0000 Subject: Regular expressions In-Reply-To: References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Message-ID: > On Tue, Dec 27, 2011 at 10:45 AM, mauriceling at acm.org > wrote: >> Hi >> >> I am trying to change to . >> >> Can anyone help me with the regular expressions needed? > > A regular expression defines a string based on rules. Without seeing a > lot more strings, we can't know what possibilities there are for each > part of the string. You probably know your data better than we ever > will, even eyeballing the entire set of strings; just write down, in > order, what the pieces ought to be - for instance, the first token > might be a literal @ sign, followed by three upper-case letters, then > a hyphen, then any number of alphanumerics followed by a colon, etc. > Once you have that, it's fairly straightforward to translate that into > regex syntax. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list The OP told me, off list, that my guess was true: > Can we say that your string: > 1) Contains 7 colon-delimited fields, followed by > 2) whitespace, followed by > 3) 3 colon-delimited fields (A, B, C), followed by > 4) a colon? > The transformation needed is that the whitespace is replaced by a > slash, the "A" characters are taken as is, and the colons and fields > following the "A" characters are eliminated? Doubtful that my guess was 100% accurate, but nevertheless: >>> import re >>> string1 = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N:0:" >>> re.sub(r"(\S+)\s+(\S+?):.+", "\g<1>/\g<2>", string1) '@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1' From mauriceling at gmail.com Mon Dec 26 19:24:35 2011 From: mauriceling at gmail.com (mauriceling@acm.org) Date: Mon, 26 Dec 2011 16:24:35 -0800 (PST) Subject: Regular expressions References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Message-ID: <34666b77-9e7a-4093-b7c4-74adeceb6555@l18g2000pro.googlegroups.com> On Dec 27, 8:16?am, Jason Friedman wrote: > > On Tue, Dec 27, 2011 at 10:45 AM, mauricel... at acm.org > > wrote: > >> Hi > > >> I am trying to change to . > > >> Can anyone help me with the regular expressions needed? > > > A regular expression defines a string based on rules. Without seeing a > > lot more strings, we can't know what possibilities there are for each > > part of the string. You probably know your data better than we ever > > will, even eyeballing the entire set of strings; just write down, in > > order, what the pieces ought to be - for instance, the first token > > might be a literal @ sign, followed by three upper-case letters, then > > a hyphen, then any number of alphanumerics followed by a colon, etc. > > Once you have that, it's fairly straightforward to translate that into > > regex syntax. > > > ChrisA > > -- > >http://mail.python.org/mailman/listinfo/python-list > > The OP told me, off list, that my guess was true: > > > Can we say that your string: > > 1) Contains 7 colon-delimited fields, followed by > > 2) whitespace, followed by > > 3) 3 colon-delimited fields (A, B, C), followed by > > 4) a colon? > > The transformation needed is that the whitespace is replaced by a > > slash, the "A" characters are taken as is, and the colons and fields > > following the "A" characters are eliminated? > > Doubtful that my guess was 100% accurate, but nevertheless: > > >>> import re > >>> string1 = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N:0:" > >>> re.sub(r"(\S+)\s+(\S+?):.+", "\g<1>/\g<2>", string1) > > '@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1' Thanks a lot everyone. Can anyone suggest a good place to learn REs? ML From rosuav at gmail.com Mon Dec 26 19:52:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 11:52:41 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <5aadbf8b-c905-4115-972c-b37d093126b4@p4g2000vbt.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> <5aadbf8b-c905-4115-972c-b37d093126b4@p4g2000vbt.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 10:44 AM, Eelco wrote: > extended collection unpacking, as in 'head,*tail=sequence', is quite a > rare construct indeed, and here I very strongly feel a more explicit > syntax is preferrable. You may be right, but... > ... if collection packing/unpacking would be > presented as a more general construct from the start, > 'head,tail::tuple=sequence' would be hard to miss. ... it doesn't really justify a _change_. When a language is in its infancy and the only code written in it is on the designers' own computers, these sorts of debates can be tipped by relatively small differences - is it more readable, is it quick enough to type, etc. But once a language reaches a level of maturity, changes need to overwhelm the "but it's a change" hurdle - breaking existing code is majorly dangerous, and keeping two distinct ways of doing something means you get the worst of both worlds. We can argue till the cows come home as to which way would be better, _had Python started with it_. I don't think there's anything like enough difference to justify the breakage/duplication. ChrisA From steve+comp.lang.python at pearwood.info Mon Dec 26 20:10:19 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Dec 2011 01:10:19 GMT Subject: Possible bug in string handling (with kludgy work-around) References: Message-ID: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 14:23:03 -0800, Charles Hixson wrote: > This doesn't cause a crash, but rather incorrect results. Charles, your code is badly formatted and virtually unreadable. You have four spaces between some tokens, lines are too long to fit in an email or News post without word-wrapping. It is a mess of unidiomatic code filled with repeated indexing and unnecessary backslash escapes. You also don't tell us what result you expect, or what result you actually get. What is the intention of the code? What are you trying to do, and what happens instead? The code as given doesn't run -- what's self? Despite all these problems, I can see one obvious problem in your code: you test to see if self.wordList[i] is a string, and if not, you replace the *entire* wordList with the empty string. That is unlikely to do what you want, although I admit I'm guessing what you are trying to do (since you don't tell us). Some hints for you: (1) Python has two string delimiters, " and ' and you should use them both. Instead of hard-to-read backslash escapes, just swap delimiters: print "A string including a \" quote mark." # No! print 'A string including a " quote mark.' # Yes, much easier to read. The only time you should backslash-escape a quotation mark is if you need to include both sorts in a single string: print "Python has both single ' and double \" quotation marks." print 'Python has both single \' and double " quotation marks.' (2) Python is not Pascal, or whatever language you seem to be writing in the style of. You almost never should write for-loops like this: for i in range(len(something)): print something[i] Instead, you should just iterate over "something" directly: for obj in something: print obj If you also need the index, use the enumerate function: for i,obj in enumerate(something): print obj, i If you are forced to use an ancient version of Python without enumerate, do yourself a favour and write your loops like this: for i in range(len(something)): obj = something[i] print obj, i instead of repeatedly indexing the list over and over and over and over again, as you do in your own code. The use of a temporary variable makes the code much easier to read and understand. -- Steven From jason at powerpull.net Mon Dec 26 20:26:04 2011 From: jason at powerpull.net (Jason Friedman) Date: Tue, 27 Dec 2011 01:26:04 +0000 Subject: Regular expressions In-Reply-To: <34666b77-9e7a-4093-b7c4-74adeceb6555@l18g2000pro.googlegroups.com> References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> <34666b77-9e7a-4093-b7c4-74adeceb6555@l18g2000pro.googlegroups.com> Message-ID: > Thanks a lot everyone. > > Can anyone suggest a good place to learn REs? Start with the manual: http://docs.python.org/py3k/library/re.html#module-re From fredrik at dolda2000.com Mon Dec 26 20:43:56 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 02:43:56 +0100 (CET) Subject: Daemon management Message-ID: Dear list, Lately, I've had a personal itch to scratch, in that I run a couple of Python programs as daemons, and sometimes want to inspect or alter them in ad-hoc ways, or other times need to do things to them that are less ad-hoc in nature, but nevertheless lack a natural user interface. In order to solve that problem, I wrote a small library to allow the daemon to simply listen to some socket and accept arbitrary, but easily definable, commands. It also provides a "remote REPL" to allow me to run arbitrary Python code interactively in the context of the daemon. I was actually a bit surprised that I couldn't find any obvious existing solution to the problem, so I'm writing this message in order to share mine, just in case anyone else would happen to have the same problem as I had and doesn't want to reinvent the wheel yet again: Humbly, Fredrik Tolf From rich at noir.com Mon Dec 26 23:28:26 2011 From: rich at noir.com (K. Richard Pixley) Date: Mon, 26 Dec 2011 20:28:26 -0800 Subject: confused about __new__ Message-ID: I'm confused about the following. The idea here is that the set of instances of some class are small and finite, so I'd like to create them at class creation time, then hijack __new__ to simply return one of the preexisting classes instead of creating a new one each call. This seems to work in python3, but fails in python2 with: Foo(23) TypeError: unbound method __Foo__new__() must be called with Foo instance as first argument (got type instance instead) I don't understand. Can anyone explain? --rich -------------- next part -------------- A non-text attachment was scrubbed... Name: tst.py Type: text/x-python-script Size: 236 bytes Desc: not available URL: From steve+comp.lang.python at pearwood.info Mon Dec 26 23:53:31 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Dec 2011 04:53:31 GMT Subject: confused about __new__ References: Message-ID: <4ef94f4b$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 20:28:26 -0800, K. Richard Pixley wrote: > I'm confused about the following. The idea here is that the set of > instances of some class are small and finite, so I'd like to create them > at class creation time, then hijack __new__ to simply return one of the > preexisting classes instead of creating a new one each call. I'm surprised it works in Python3. Try this in Python 2 instead: Foo.__new__ = staticmethod(__Foo__new__) -- Steven From fredrik at dolda2000.com Tue Dec 27 00:01:35 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 06:01:35 +0100 (CET) Subject: Regular expressions In-Reply-To: <4be34afe-4291-414b-9212-498074400e39@v24g2000prn.googlegroups.com> References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> <4be34afe-4291-414b-9212-498074400e39@v24g2000prn.googlegroups.com> Message-ID: On Mon, 26 Dec 2011, mauriceling at acm.org wrote: > I've tried > > re.sub('@\S\s[1-9]:[A-N]:[0-9]', '@\S\s', '@HWI-ST115:568:B08LLABXX: > 1:1105:6465:151103 1:N:0:') > > but it does not seems to work. Indeed, for several reasons. First of all, your backslash sequences are interpreted by Python as string escapes. You'll need to write either "\\S" or r"\S" (the r, for raw, turns off backslash escapes). Second, when you use only "\S", that matches a single non-space character, not several; you'll need to quantify them. "\S*" will match zero or more, "\S+" will match one or more, "\S?" will match zero or one, and there are a couple of other possibilities as well (see the manual for details). In this case, you probably want to use "+" for most of those. Third, you're not marking the groups that you want to use in the replacement. Since you want to retain the entire string before the space, and the numeric element, you'll want to enclose them in parentheses to mark them as groups. Fourth, your replacement string is entirely wacky. You don't use sequences such as "\S" and "\s" to refer back to groups in the original text, but numbered references, to refer back to parenthesized groups in the order they appear in the regex. In accordance what you seemed to want, you should probably use "@\1/\2" in your case ("\1" refers back to the first parentesized group, which you be the first "\S+" part, and "\2" to the second group, which should be the "[1-9]+" part; the at-mark and slash are inserted as they are into the result string). Fifth, you'll probably want to match the last colon as well, in order not to retain it into the result string. All in all, you will probably want to use something like this to correct that regex: re.sub(r'@(\S+)\s([1-9]+):[A-N]+:[0-9]+:', r'@\1/\2', '@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N:0:') Also, you may be interested to know that you can use "\d" instead of "[0-9]". -- Fredrik Tolf From briskb5 at gmail.com Tue Dec 27 00:20:37 2011 From: briskb5 at gmail.com (briskb5 briskb5) Date: Mon, 26 Dec 2011 21:20:37 -0800 (PST) Subject: Best Online Jobs Message-ID: <97b3dc84-837b-4db5-8060-24bc981354c9@h37g2000pri.googlegroups.com> Best Online Jobs http://gsonlinejobs.yolasite.com/ From ian.g.kelly at gmail.com Tue Dec 27 00:21:11 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 26 Dec 2011 22:21:11 -0700 Subject: Multithreading In-Reply-To: <560bea15-6abc-45ed-ac7a-cf23ff44f130@e2g2000vbb.googlegroups.com> References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> <560bea15-6abc-45ed-ac7a-cf23ff44f130@e2g2000vbb.googlegroups.com> Message-ID: On Dec 26, 2011 4:13 PM, "Yigit Turgut" wrote: > Why is there N variable in write_data function ? N is related to > timer.tick(N) which is related to display function ? time.sleep(N) > will pause writing to file for specified amount of time which is > exactly what I am trying to avoid. My understanding from your first post was that the pygame loop was supposed to run at X FPS while the other loop was supposed to run every N seconds. If that is not the case and the data loop is supposed to run as fast as possible, then you don't need threads for this. Just stream the data in a tight loop, and on each iteration check the timer for how much time has elapsed to determine whether to run the pygame code without using timer.tick. If not enough time has elapsed for your target framerate, just skip that part. -------------- next part -------------- An HTML attachment was scrubbed... URL: From Rachelhere33 at yahoo.com Tue Dec 27 00:22:18 2011 From: Rachelhere33 at yahoo.com (Rachelhere33 at yahoo.com) Date: Mon, 26 Dec 2011 21:22:18 -0800 (PST) Subject: =?UTF-8?B?V2lsbCBUaGUgRWFydGggRW5kIGJlIGluIDIwMTI/4p2k4p2k4p2k4p2k4p2k4p2k4p2k?= =?UTF-8?B?4p2k?= Message-ID: <4d705d3c-2e70-42c5-be0d-288deaeda833@p13g2000yqd.googlegroups.com> Will The Earth End be in 2012????????? Many experts from across the globe are predicting that the earth is likely to end by the year 2012. The reason could be a human effect or natural disaster while interpretations of the Christian Bible reveal that the date for Armageddon, the final battle between good an evil has been set for 2012. Active Address:http://www.2012ark.net/ Discuss Address:http://www.facebook.com/pages/2012ark-the-last-life- journey/292745100770702 We provide many free seats for you on Ark.Completely free for lives. Please Click "LIKE" and use your eyes to witness what will happen with us in 2012. From rich at noir.com Tue Dec 27 00:27:23 2011 From: rich at noir.com (K Richard Pixley) Date: Mon, 26 Dec 2011 21:27:23 -0800 Subject: confused about __new__ In-Reply-To: <4ef94f4b$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef94f4b$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <%GcKq.36533$JE1.19080@newsfe21.iad> On 12/26/11 20:53 , Steven D'Aprano wrote: > On Mon, 26 Dec 2011 20:28:26 -0800, K. Richard Pixley wrote: > >> I'm confused about the following. The idea here is that the set of >> instances of some class are small and finite, so I'd like to create them >> at class creation time, then hijack __new__ to simply return one of the >> preexisting classes instead of creating a new one each call. > > I'm surprised it works in Python3. Try this in Python 2 instead: > > Foo.__new__ = staticmethod(__Foo__new__) Excellent. Thank you. --rich From fredrik at dolda2000.com Tue Dec 27 00:48:45 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 06:48:45 +0100 (CET) Subject: confused about __new__ In-Reply-To: References: Message-ID: On Mon, 26 Dec 2011, K. Richard Pixley wrote: > I don't understand. Can anyone explain? I'm also a bit confused about __new__. I'd very much appreciate it if someone could explain the following aspects of it: * The manual () says that __new__ is "a static method (special-cased so you need not declare it as such)". What does "special-cased" mean? Apparently, for instance, in OP's case, Python did not automatically detect that it should not be bound as a method. * Is there any part of the manual that explains, holistically, the greater context of object instantiation into which __new__ fits? I can only find small parts of it spread around the documentation for __new__ and __init__, but no complete explanation of it. There are several things I'm wondering about, like what it means to call a type object at all; how methods, properties and the like are bound; how pickle can instantiate a class without calling __init__; when and whether __dict__ is created and a couple of other things. Is there a complete documentation of the process anywhere that I haven't managed to find? -- Fredrik Tolf From briskbb3 at gmail.com Tue Dec 27 00:55:34 2011 From: briskbb3 at gmail.com (briskbb3 briskbb3) Date: Mon, 26 Dec 2011 21:55:34 -0800 (PST) Subject: Best Online Jobs Message-ID: Best Online Jobs http://gsonlinejobs.yolasite.com/ From carl.input at gmail.com Tue Dec 27 00:59:29 2011 From: carl.input at gmail.com (Carl Smith) Date: Mon, 26 Dec 2011 21:59:29 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> On Dec 20, 10:58?am, Andrea Crotti wrote: > On 12/20/2011 03:51 AM, Raymond Hettinger wrote: > > > > > > > > > > > Do you use IDLE when teaching Python? > > If not, what is the tool of choice? > > > Students may not be experienced with the command-line and may be > > running Windows, Linux, or Macs. ?Ideally, the tool or IDE will be > > easy to install and configure (startup directory, path, associated > > with a particular version of Python etc). > > > Though an Emacs user myself, I've been teaching with IDLE because it's > > free; it runs on multiple OSes, it has tooltips and code colorization > > and easy indent/dedent/comment/uncomment commands, it has tab > > completion; it allows easy editing at the interactive prompt; it has > > an easy run-script command (F5); it has direct access to source code > > (File OpenModule) and a class browser (Cntl+B). > > > On the downside, some python distros aren't built with the requisite > > Tcl/Tk support; some distros like the Mac OS ship with a broken Tcl/Tk > > so users have to install a fix to that as well; and IDLE sometimes > > just freezes for no reason. ?It also doesn't have an easy way to > > specify the startup directory. > > > If your goal is to quickly get new users up and running in Python, > > what IDE or editor do you recommend? > > > Raymond > > I think ipython and a good editor gives a much nicer experience > than IDLE, which I actually almost never used, and > for everything else there is python and python-mode. > > New users however can be pointed to something like PyCharm > or Eclipse+PyDev if they are more familiar to IDEs.. I agree; IPython is a excellent choice. You have a much more powerful interactive Python experience, with all the features you need from an IDE. You can use any editor (VIM) and you can also readily hack IPython to death. I think the fact that anyone with basic programming skills can substantially enhance their console is a big winner in CS education. It gives students something they personally value to work on, it's a place to store all their little bits of code and actually benefit from them in real life. I've never met a programmer that got familiar with IPython and then went on to stop using it. It should be included in the standard library and used as the default Python interactive environment. The last line of my .bashrc file: ipython3 From carl.input at gmail.com Tue Dec 27 01:14:42 2011 From: carl.input at gmail.com (Carl Smith) Date: Mon, 26 Dec 2011 22:14:42 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> Message-ID: On Dec 25, 5:44?pm, Rick Johnson wrote: > On Dec 19, 9:51?pm, Raymond Hettinger > wrote: > > > Do you use IDLE when teaching Python? > > If not, what is the tool of choice? > > I believe IDLE has the potential to be a very useful teaching tool and > even in it's current abysmal state, i find it to be quite useful. > > > Students may not be experienced with the command-line and may be > > running Windows, Linux, or Macs. ?Ideally, the tool or IDE will be > > easy to install and configure (startup directory, path, associated > > with a particular version of Python etc). > > Why install an IDE when IDLE is already there? Oh, yes, IDLE SUCKS. I > know that already. But this revelation begs the question... Why has > this community allowed IDLE to rot? Why has guido NOT started a public > discussion on the matter? > > > Though an Emacs user myself, I've been teaching with IDLE because it's > > free; it runs on multiple OSes, it has tooltips and code colorization > > and easy indent/dedent/comment/uncomment commands, it has tab > > completion; it allows easy editing at the interactive prompt; it has > > an easy run-script command (F5); it has direct access to source code > > (File OpenModule) and a class browser (Cntl+B). > > Yes, IDLE has all the basic tools anyone would need. Some people > complain about a debugger, but i never use a debugger anyway. I feel > debuggers just wreaken your debugging skills. > > > On the downside, some python distros aren't built with the requisite > > Tcl/Tk support; > > And who's fault is that? > > > some distros like the Mac OS ship with a broken Tcl/Tk > > so users have to install a fix to that as well; and IDLE sometimes > > just freezes for no reason. > > And who's fault is that? > > > ?[IDLE] also doesn't have an easy way to > > specify the startup directory. > > Are you kidding me? That could be fixed so easily! > > > If your goal is to quickly get new users up and running in Python, > > what IDE or editor do you recommend? > > IDLE, of course. But NOT in its current state. > > Why would myself (or anyone) go to the trouble of downloading third > party IDEs when IDLE is just waiting there for us to use? I for one, > like to use tools that have open source code. ?And what is a better > Python IDE than a Python IDE written in PYTHON? I ask ya? > > Also, what is the purpose of this thread Raymond? Are you (and others) > considering removing IDLE from the source distro? > > You know. Many folks in this community have known for a long time how > much i love IDLE, but at the same time how much i loath it's atrocious > code base. I also know for a fact, that many "movers and shakers" > within this community simultaneously use IDLE, and want to see IDLE > code improved. However. None of these fine folks have taken the time > to contact me privately so we can discuss such an evolution. Why is > that? It boggles the mind really. Do people seriously use IDLE? I thought it was just there for scratchers, like turtle. From tyler at tysdomain.com Tue Dec 27 01:26:39 2011 From: tyler at tysdomain.com (Littlefield, Tyler) Date: Mon, 26 Dec 2011 23:26:39 -0700 Subject: python logging module:a quick question Message-ID: <4EF9651F.7070806@tysdomain.com> Hello all: I have a basic server I am working on, and wanted some input with an error I'm getting. I am initializing the logger like so: if __name__ == "__main__": observer = log.PythonLoggingObserver() observer.start() logging.basicConfig(filename='logs/server.log', level=logging.DEBUG, format='%(asctime)s [%(levelname)s] %(module)s:%(funcname)s:%(lineno)d %(message)s') logger = logging.getLogger() logger.addHandler(logging.handlers.TimedRotatingFileHandler) ... I get the following error: File "/home/gserver/alpine/lib/python2.7/site-packages/twisted/internet/protoc ol.py", line 52, in doStart log.msg("Starting factory %r" % self) --- --- File "/home/gserver/alpine/lib/python2.7/site-packages/twisted/python/log.py", line 284, in msg self.observers[i](actualEventDict) File "/home/gserver/alpine/lib/python2.7/site-packages/twisted/python/log.py", line 532, in emit self.logger.log(level, text) File "/usr/local/lib/python2.7/logging/__init__.py", line 1195, in log self._log(level, msg, args, **kwargs) File "/usr/local/lib/python2.7/logging/__init__.py", line 1250, in _log self.handle(record) File "/usr/local/lib/python2.7/logging/__init__.py", line 1260, in handle self.callHandlers(record) File "/usr/local/lib/python2.7/logging/__init__.py", line 1299, in callHandler s if record.levelno >= hdlr.level: exceptions.AttributeError: type object 'TimedRotatingFileHandler' has no attribute 'level' I'm also curious how this rotating handler works. Will it just log to a file per day for a week, then start rotating those out with newer ones? Can I change the interval? -- Take care, Ty Web: http://tds-solutions.net The Aspen project: a light-weight barebones mud engine http://code.google.com/p/aspenmud Sent from my toaster. From nad at acm.org Tue Dec 27 01:46:57 2011 From: nad at acm.org (Ned Deily) Date: Tue, 27 Dec 2011 01:46:57 -0500 Subject: installing matplotlib in MacOs 10.6.8. References: Message-ID: In article , Alex Ter-Sarkissov wrote: > that's quite weird: if I run python2.7-32 in terminal, it works like you > said, but when I add it as an interpreter in Eclipse, matplotlib.pyplot > doesn't get imported for some reason. Even more strange, either way > platform.architecture() reports 64-bit. What's wrong here? platform.architecture() is not accurate for OS X 64-/32-bit builds. Use sys.maxsize instead. I have no experience with Eclipse so I really can't say how Eclipse might be launching the interpreter. Sorry! -- Ned Deily, nad at acm.org From ian.g.kelly at gmail.com Tue Dec 27 02:28:24 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 00:28:24 -0700 Subject: confused about __new__ In-Reply-To: References: Message-ID: On Mon, Dec 26, 2011 at 10:48 PM, Fredrik Tolf wrote: > I'm also a bit confused about __new__. I'd very much appreciate it if > someone could explain the following aspects of it: > > ?* The manual () says > ? that __new__ is "a static method (special-cased so you need not declare > ? it as such)". What does "special-cased" mean? Apparently, for > ? instance, in OP's case, ?Python did not automatically detect that it > ? should not be bound as a method. It apparently has to do with the class creation. For the special-casing to happen, the __new__ method has to be present when the class is created. If it is, then it automatically gets wrapped in a staticmethod. In the OP's case, he was adding the __new__ method after class creation, so the wrapping did not happen automatically. Compare: >>> def my_new(cls): return object.__new__(cls) ... >>> class Foo(object): ... __new__ = my_new ... >>> class Bar(object): pass ... >>> Bar.__new__ = my_new >>> Foo.__dict__['__new__'] >>> Bar.__dict__['__new__'] > ?* Is there any part of the manual that explains, holistically, the > ? greater context of object instantiation into which __new__ fits? I can > ? only find small parts of it spread around the documentation for __new__ > ? and __init__, but no complete explanation of it. There are several > ? things I'm wondering about, like what it means to call a type object at > ? all; I don't know of anything that organizes it that way specifically, but I believe the Data Model reference pretty much covers what you're looking for. From the type hierarchy, under "Callable Types": http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy """ Class Types Class types, or ?new-style classes,? are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override __new__(). The arguments of the call are passed to __new__() and, in the typical case, to __init__() to initialize the new instance. """ AFAIK, that's pretty much it. When a type is called, __new__ is called to create the new instance, and then __init__ is called to initialize it (if __new__ returned an instance of the type). > how methods, properties and the like are bound; When they're accessed, using the descriptor protocol, not as part of the instantiation process. See: http://docs.python.org/reference/datamodel.html#invoking-descriptors > how pickle can > ? instantiate a class without calling __init__; By calling the __new__ method directly instead of calling the type: http://docs.python.org/library/pickle.html#object.__getnewargs__ > when and whether __dict__ > ? is created and a couple of other things. Under the hood as part of the object creation process, unless the class uses __slots__. From lie.1296 at gmail.com Tue Dec 27 02:47:22 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 27 Dec 2011 18:47:22 +1100 Subject: confused about __new__ In-Reply-To: References: Message-ID: On 12/27/2011 04:48 PM, Fredrik Tolf wrote: > On Mon, 26 Dec 2011, K. Richard Pixley wrote: >> I don't understand. Can anyone explain? > > I'm also a bit confused about __new__. I'd very much appreciate it if > someone could explain the following aspects of it: > > * The manual () says > that __new__ is "a static method (special-cased so you need not declare > it as such)". What does "special-cased" mean? Apparently, for > instance, in OP's case, Python did not automatically detect that it > should not be bound as a method. If you declare new in the regular way: class Foo(object): def __new__(cls): ... Python would create __new__ as a static method even without applying the staticmethod decorator; that python does not detect to special case __new__ when it is added dynamically is probably an oversight in Python 2.x that was fixed in Python 3.x. From jobinchn at gmail.com Tue Dec 27 03:37:56 2011 From: jobinchn at gmail.com (earn 4 revenue) Date: Tue, 27 Dec 2011 00:37:56 -0800 (PST) Subject: Free Winning Forex course. Message-ID: Free Winning Forex course, Learn forex trading from A to Z with complete tutorials. And You Learn How Make Money in Forex. http://tiny.cc/dhuob From hoogendoorn.eelco at gmail.com Tue Dec 27 05:01:34 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 27 Dec 2011 02:01:34 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> <5aadbf8b-c905-4115-972c-b37d093126b4@p4g2000vbt.googlegroups.com> Message-ID: <1f1f2b88-a3f8-4fdb-be71-a3e5597aaf15@p42g2000vbt.googlegroups.com> On Dec 27, 1:52?am, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 10:44 AM, Eelco wrote: > > extended collection unpacking, as in 'head,*tail=sequence', is quite a > > rare construct indeed, and here I very strongly feel a more explicit > > syntax is preferrable. > > You may be right, but... > > > ... if collection packing/unpacking would be > > presented as a more general construct from the start, > > 'head,tail::tuple=sequence' would be hard to miss. > > ... it doesn't really justify a _change_. When a language is in its > infancy and the only code written in it is on the designers' own > computers, these sorts of debates can be tipped by relatively small > differences - is it more readable, is it quick enough to type, etc. > But once a language reaches a level of maturity, changes need to > overwhelm the "but it's a change" hurdle - breaking existing code is > majorly dangerous, and keeping two distinct ways of doing something > means you get the worst of both worlds. > > We can argue till the cows come home as to which way would be better, > _had Python started with it_. I don't think there's anything like > enough difference to justify the breakage/duplication. That I agree with; I think it is a questionable idea to introduce this in a new python 3 version. But I consider it a reasonable change for a 'python 4', or whatever the next major version change will be called. Writing a code-conversion tool to convert from *args to args::tuple would be quite easy indeed. From brisk67 at gmail.com Tue Dec 27 05:45:16 2011 From: brisk67 at gmail.com (brisk67 brisk67) Date: Tue, 27 Dec 2011 02:45:16 -0800 (PST) Subject: Best Online Jobs Message-ID: <0e290862-6fe8-471e-a87c-6411243c7f5c@g19g2000pri.googlegroups.com> Best Online Jobs http://gsonlinejobs.yolasite.com/ From lie.1296 at gmail.com Tue Dec 27 05:48:58 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 27 Dec 2011 21:48:58 +1100 Subject: python logging module:a quick question In-Reply-To: <4EF9651F.7070806@tysdomain.com> References: <4EF9651F.7070806@tysdomain.com> Message-ID: On 12/27/2011 05:26 PM, Littlefield, Tyler wrote: > Hello all: > I have a basic server I am working on, and wanted some input with an > error I'm getting. > I am initializing the logger like so: > if __name__ == "__main__": > observer = log.PythonLoggingObserver() > observer.start() > logging.basicConfig(filename='logs/server.log', level=logging.DEBUG, > format='%(asctime)s [%(levelname)s] %(module)s:%(funcname)s:%(lineno)d > %(message)s') > logger = logging.getLogger() > logger.addHandler(logging.handlers.TimedRotatingFileHandler) You should pass an **instance** of the handler, not the class; so it should look like this: logger.addHandler(logging.handlers.TimedRotatingFileHandler(...)) if you want to configure the handler, e.g. configure how often the log file is rotated, you can do it by passing parameters to the handler's constructure, e.g.: logger.addHandler(logging.handlers.TimedRotatingFileHandler(filename='/foo/mylog.log', when='d', interval=3, backupCount=5)) will create a handler that will log to the file /foo/mylog.log and rotate the log every 3 days and keeps the last 5 logs for backup. the logging handlers are documented over here: http://docs.python.org/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler Note that if you're using a recent enough version of python, it's also possible to configure the logging module using a dictionary-based configuration. Dictionary-based configuration is generally simpler than code-based configuration. From lie.1296 at gmail.com Tue Dec 27 05:54:57 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 27 Dec 2011 21:54:57 +1100 Subject: Plot seems weird In-Reply-To: <96ac870e-dffe-4556-aa3a-fab84333ceda@o9g2000vbc.googlegroups.com> References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> <74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com> <96ac870e-dffe-4556-aa3a-fab84333ceda@o9g2000vbc.googlegroups.com> Message-ID: On 12/27/2011 06:14 AM, Yigit Turgut wrote: > On Dec 26, 8:58 pm, Lie Ryan wrote: >> On 12/27/2011 04:08 AM, Yigit Turgut wrote: >> >> not your fault, I made a mistake when copy-pasteing the code, here's the >> fixed code: >> >> from itertools import izip_longest >> def to_square(data): >> sq_data = [[], []] >> for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): >> sq_data[0].append(x) >> sq_data[1].append(y) >> sq_data[0].append(xn) >> sq_data[1].append(y) >> return numpy.array(sq_data, dtype=float) >> >> use it like this: >> >> t,y1 = to_square(numpy.genfromtxt(filename, unpack=True)) >> pyplot.plot(t,y1) >> pyplot.show() > > Significant improvement on the plot, pretty interesting. It runs ok > but I need to know how?! (: it's pretty simple, actually; just observe the numbers before and after it's fixed by the function and it should be fairly obvious. From hoogendoorn.eelco at gmail.com Tue Dec 27 06:41:54 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 27 Dec 2011 03:41:54 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> Message-ID: On Dec 27, 6:59?am, Carl Smith wrote: > On Dec 20, 10:58?am, Andrea Crotti wrote: > > > > > > > > > > > On 12/20/2011 03:51 AM, Raymond Hettinger wrote: > > > > Do you use IDLE when teaching Python? > > > If not, what is the tool of choice? > > > > Students may not be experienced with the command-line and may be > > > running Windows, Linux, or Macs. ?Ideally, the tool or IDE will be > > > easy to install and configure (startup directory, path, associated > > > with a particular version of Python etc). > > > > Though an Emacs user myself, I've been teaching with IDLE because it's > > > free; it runs on multiple OSes, it has tooltips and code colorization > > > and easy indent/dedent/comment/uncomment commands, it has tab > > > completion; it allows easy editing at the interactive prompt; it has > > > an easy run-script command (F5); it has direct access to source code > > > (File OpenModule) and a class browser (Cntl+B). > > > > On the downside, some python distros aren't built with the requisite > > > Tcl/Tk support; some distros like the Mac OS ship with a broken Tcl/Tk > > > so users have to install a fix to that as well; and IDLE sometimes > > > just freezes for no reason. ?It also doesn't have an easy way to > > > specify the startup directory. > > > > If your goal is to quickly get new users up and running in Python, > > > what IDE or editor do you recommend? > > > > Raymond > > > I think ipython and a good editor gives a much nicer experience > > than IDLE, which I actually almost never used, and > > for everything else there is python and python-mode. > > > New users however can be pointed to something like PyCharm > > or Eclipse+PyDev if they are more familiar to IDEs.. > > I agree; IPython is a excellent choice. You have a much more powerful > interactive Python experience, with all the features you need from an > IDE. You can use any editor (VIM) and you can also readily hack > IPython to death. > > I think the fact that anyone with basic programming skills can > substantially enhance their console is a big winner in CS education. > It gives students something they personally value to work on, it's a > place to store all their little bits of code and actually benefit from > them in real life. > > I've never met a programmer that got familiar with IPython and then > went on to stop using it. It should be included in the standard > library and used as the default Python interactive environment. > > The last line of my .bashrc file: > > ipython3 Youve got one here. I like IPython a lot, but it quite rarely enters into my workflow. While I agree that a good interactive python console is a good way to get your feet wet with programming, I also strongly feel that a more comprehensive programming environment should be introduced to students. That includes opening and editing files, syntax highlighting, and code completion. And painless installation. There are no lightweight editors that provide all this functionality in conjuction with Ipython*. So I prefer to work the other way around; use something like pycharm, and open an IPython interactive session within it. *Your suggestion of VIM is especially objectionable. Though I am sure it is a great tool to you, the subject here is beginner education. Just because it is a good tool for you, does not make it a good tool for a beginner. IPython bundled with a lightweight but function-rich and non-hacker- but-WYSIWYG editor would be a great choice. But until that comes around, pycharm it is for me. From mjseger at gmail.com Tue Dec 27 08:32:38 2011 From: mjseger at gmail.com (Mark Seger) Date: Tue, 27 Dec 2011 08:32:38 -0500 Subject: Errors installing mod_python with apache Message-ID: I've tried this on both RHEL5.5 and RHEL 6.0, using the default apache that comes with the environment and itself isn't configured with mod_python. The first thing I noticed when mod_python wouldn't install was that apsx wasn't installed either. After a lot of pain, I discovered httpd-devel provided apsx and so installed that. Back to mod_python. After running ./configure --with-apxs=/usr/sbin/apxs, according to the README I need to run "make dso", but that promptly blows up because it's looking for header files in /usr/include/httpd and itlooks like the apxs files are in /usr/include/apr1, so I copied all its contents to /usr/include/httpd. Now when I try to make dso, it successfully gets past finding its header files but now fails with: [root at rhel6 mod_python-2.7.10]# make dso make[1]: Entering directory `/tmp/mod_python-2.7.10/src' gcc -I/tmp/mod_python-2.7.10/src/include -I/usr/include/httpd -I/usr/include/python2.6 -c -o mod_python.o mod_python.c In file included from /usr/include/python2.6/pyconfig.h:6, from /usr/include/python2.6/Python.h:8, from /tmp/mod_python-2.7.10/src/include/mod_python.h:77, from mod_python.c:54: /usr/include/python2.6/pyconfig-64.h:1031:1: warning: "_POSIX_C_SOURCE" redefined In file included from /usr/include/sys/types.h:26, from /usr/include/httpd/apr-x86_64.h:127, from /usr/include/httpd/apr.h:19, from /usr/include/httpd/ap_config.h:25, from /usr/include/httpd/httpd.h:43, from /tmp/mod_python-2.7.10/src/include/mod_python.h:63, from mod_python.c:54: /usr/include/features.h:213:1: warning: this is the location of the previous definition In file included from mod_python.c:54: /tmp/mod_python-2.7.10/src/include/mod_python.h:93: error: expected ?=?, ?,?, ?;?, ?asm? or ?__attribute__? before ?*? token /tmp/mod_python-2.7.10/src/include/mod_python.h:96: error: expected ?=?, ?,?, ?;?, ?asm? or ?__attribute__? before ?python_module? In file included from /tmp/mod_python-2.7.10/src/include/mod_python.h:99, from mod_python.c:54: /tmp/mod_python-2.7.10/src/include/util.h:57: error: expected ?;?, ?,? or ?)? before ?*? token In file included from /tmp/mod_python-2.7.10/src/include/mod_python.h:100, from mod_python.c:54: and a lot more. Can anyone help? -mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From charleshixsn at earthlink.net Tue Dec 27 10:54:57 2011 From: charleshixsn at earthlink.net (Charles Hixson) Date: Tue, 27 Dec 2011 07:54:57 -0800 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: References: <4EF8F3C7.3010005@earthlink.net> Message-ID: <4EF9EA51.5090301@earthlink.net> That was it! Thanks. On 12/26/2011 02:44 PM, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 9:23 AM, Charles Hixson > wrote: > >> This doesn't cause a crash, but rather incorrect results. >> > You may need to be a bit clearer. What line of code (or what > expression)? What did you expect to see, and what did you see? > > > From examining your code, I've come up with one most-likely scenario. > In Python, indexing is zero-based and, in effect, indexes the > boundaries between items rather than the items themselves. Referencing > string[-1] actually means asking for the boundary between the > second-last and last characters; using that as your end marker > actually trims off the last character. What you may want is simply: > > self.wordList[i][1:] > > which means "from character position 1 to the end of the string". > > Hope that helps! > > Chris Angelico > Sorry I didn't specify things more clearly. -- Charles Hixson From wolftracks at invalid.com Tue Dec 27 11:03:13 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 08:03:13 -0800 Subject: Which libraries for Python 2.5.2 Message-ID: I'm trying to restore Python 2.5.2 on an old XP PC for a particular application from 4-5 years ago that uses it . According to the latest manual on it, the following should be installed. python-2.5.2.msi PIL-1.1.6.win32-py2.5.exe numpy-1.1.0-win32-superpack-python2.5.exe matplotlib-0.98.1.win32-py2.5.exe When I install them, and try to run the app program, Sentinel.py, some part of matplotlib complains (error msgs) and the program quits. The program begins with: from Tkinter import * from numpy import * import Image import ImageChops import ImageTk import time import binascii import tkMessageBox import tkSimpleDialog from pylab import plot, xlabel, ylabel, title, show, xticks, bar I tried numpy-1.2.0 and matplotlib-0.98.3 and had the same difficulty. What are wiser choices? Here's the traceback. Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 1.2.2 ==== No Subprocess ==== >>> Traceback (most recent call last): File "C:\Sentinel\Sent_user-20080716.py", line 16, in (SEE THIS from the above list of imports, and the from)<*****-------< from pylab import plot, xlabel, ylabel, title, show, xticks, bar File "C:\Python25\lib\site-packages\pylab.py", line 1, in from matplotlib.pylab import * File "C:\Python25\lib\site-packages\matplotlib\pylab.py", line 206, in from matplotlib import mpl # pulls in most modules File "C:\Python25\lib\site-packages\matplotlib\mpl.py", line 1, in from matplotlib import artist File "C:\Python25\lib\site-packages\matplotlib\artist.py", line 4, in from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath File "C:\Python25\lib\site-packages\matplotlib\transforms.py", line 34, in from matplotlib._path import affine_transform ImportError: DLL load failed: The specified module could not be found. >>> From Boris.Yanovsky at va.gov Tue Dec 27 11:35:42 2011 From: Boris.Yanovsky at va.gov (Yanovsky, Boris, VHACIN) Date: Tue, 27 Dec 2011 11:35:42 -0500 Subject: PDF 508 Compliance with Python? Message-ID: Hello, I am new to Python and I hope my question makes sense. We are trying to make PDFs 508 Compliant, meaning that the objects within them have to be tagged. The problem is that we have hundreds, and potentially thousands of PDFs, since they are automated reports that we create on a regular basis. Is there a code or feature in Python that can read PDFs and tag the objects within them for this kind of compliance? Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rantingrickjohnson at gmail.com Tue Dec 27 11:37:00 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 08:37:00 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> Message-ID: <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> On Dec 27, 12:14?am, Carl Smith wrote: > Do people seriously use IDLE? I thought it was just there for > scratchers, like turtle. I know for a fact that many folks use IDLE, even some rather "well known" folks around here. The fact is, more people use IDLE than admit to using IDLE. Of course, out of respect, i will not mention their names myself. Hopefully they will chime in..? There is a stigma in this community towards IDLE. I am not sure exactly how it started, or on what logic (or lack thereof) it is based, but the stigma exists no doubt. I believe it may be just an extension of the TCL/TK stigma (since IDLE is coded using Tkinter). My logic is this: """ Including an IDE in the stdlib may have been a bad idea (although i understand and support Guido's original vision for IDLE). But since we do have it, we need to either MAINTAIN the package or REMOVE it. We cannot just stick our heads in the sand and ignore the elephant in the chicken coop. It's bad enough to bloat ANY stdlib with seldom used modules, but i dare say, it's far worse to bloat a library with seldom used modules that are poorly maintained! Every module in Python's stdlib is a testament to the skill and professionalism of this community as a whole. When a module looks or works badly, we ALL look and work badly.""" It's no big secret to anyone in this community that both Tkinter and IDLE are the red headed step children of Pythons stdlib. Very few people speak kindly of either package. I believe GvR still believes in the merits of both packages (psst: batteries included!) but he finds himself at odds with the elite at pydev. Hmm, Maybe he DOES want to remove them but fears loosing face...FYI: Guido IS is the original author of both Tkinter and IDLE... it was HIS idea after all. However, it is high time to re-ignite the original vision and form these packages into something we can be proud of -- OR -- cut our losses and remove them forever. This indecisiveness, collective bickering, and lack of true leadership is shameful. From lie.1296 at gmail.com Tue Dec 27 11:42:18 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 03:42:18 +1100 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/28/2011 03:03 AM, W. eWatson wrote: > Here's the traceback. The traceback seems to imply that matplotlib is not being installed properly. Have you tried uninstalling then reinstalling matplotlib? From ian.g.kelly at gmail.com Tue Dec 27 11:53:27 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 09:53:27 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On Tue, Dec 27, 2011 at 9:03 AM, W. eWatson wrote: > ?File "C:\Python25\lib\site-packages\matplotlib\transforms.py", line 34, in > > ? ?from matplotlib._path import affine_transform > ImportError: DLL load failed: The specified module could not be found. Do you not have the file C:\Python25\lib\site-packages\matplotlib\_path.pyd? It's in the installer archive, so you should be able to just open it up with a zip program and extract it manually. If that's missing, then there may be other things wrong with your installation, though, so I would recommend a full reinstall. From rami.chowdhury at gmail.com Tue Dec 27 12:02:06 2011 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Tue, 27 Dec 2011 17:02:06 +0000 Subject: Errors installing mod_python with apache In-Reply-To: References: Message-ID: On Tue, Dec 27, 2011 at 13:32, Mark Seger wrote: > I've tried this on both RHEL5.5 and RHEL 6.0, using the default apache that > comes with the environment and itself isn't configured with mod_python. > > The first thing I noticed when mod_python wouldn't install was that apsx > wasn't installed either. ?After a lot of pain, I discovered httpd-devel > provided apsx and so installed that. > > Back to mod_python. ?After running ./configure?--with-apxs=/usr/sbin/apxs, > according to the README I need to run "make dso", but that promptly blows up > because it's looking for header files in /usr/include/httpd and itlooks like > the apxs files are in /usr/include/apr1, so I copied all its contents to > /usr/include/httpd. > > Now when I try to make dso, it successfully gets past finding its header > files but now fails with: > > [root at rhel6 mod_python-2.7.10]# ?make dso > make[1]: Entering directory `/tmp/mod_python-2.7.10/src' > gcc ?-I/tmp/mod_python-2.7.10/src/include -I/usr/include/httpd > -I/usr/include/python2.6 ? ?-c -o mod_python.o mod_python.c > In file included from /usr/include/python2.6/pyconfig.h:6, > ? ? ? ? ? ? ? ? ?from /usr/include/python2.6/Python.h:8, > ? ? ? ? ? ? ? ? ?from /tmp/mod_python-2.7.10/src/include/mod_python.h:77, > ? ? ? ? ? ? ? ? ?from mod_python.c:54: > /usr/include/python2.6/pyconfig-64.h:1031:1: warning: "_POSIX_C_SOURCE" > redefined > In file included from /usr/include/sys/types.h:26, > ? ? ? ? ? ? ? ? ?from /usr/include/httpd/apr-x86_64.h:127, > ? ? ? ? ? ? ? ? ?from /usr/include/httpd/apr.h:19, > ? ? ? ? ? ? ? ? ?from /usr/include/httpd/ap_config.h:25, > ? ? ? ? ? ? ? ? ?from /usr/include/httpd/httpd.h:43, > ? ? ? ? ? ? ? ? ?from /tmp/mod_python-2.7.10/src/include/mod_python.h:63, > ? ? ? ? ? ? ? ? ?from mod_python.c:54: > /usr/include/features.h:213:1: warning: this is the location of the previous > definition > In file included from mod_python.c:54: > /tmp/mod_python-2.7.10/src/include/mod_python.h:93: error: expected ?=?, > ?,?, ?;?, ?asm? or ?__attribute__? before ?*? token > /tmp/mod_python-2.7.10/src/include/mod_python.h:96: error: expected ?=?, > ?,?, ?;?, ?asm? or ?__attribute__? before ?python_module? > In file included from /tmp/mod_python-2.7.10/src/include/mod_python.h:99, > ? ? ? ? ? ? ? ? ?from mod_python.c:54: > /tmp/mod_python-2.7.10/src/include/util.h:57: error: expected ?;?, ?,? or > ?)? before ?*? token > In file included from /tmp/mod_python-2.7.10/src/include/mod_python.h:100, > ? ? ? ? ? ? ? ? ?from mod_python.c:54: > > and a lot more. ?Can anyone help? Is there a reason you're not using the system package manager? Does "yum install mod_python" not find anything? How about "yum provides */mod_python.so"? -- Rami Chowdhury "A mind all logic is like a knife all blade - it makes the hand bleed that uses it." -- Rabindranath Tagore +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 From mjseger at gmail.com Tue Dec 27 12:07:58 2011 From: mjseger at gmail.com (Mark Seger) Date: Tue, 27 Dec 2011 12:07:58 -0500 Subject: Errors installing mod_python with apache In-Reply-To: References: Message-ID: I did try yum and got this: [root at rhel53 tmp]# yum install mod_python Loaded plugins: rhnplugin, security This system is not registered with RHN. RHN support will be disabled. Setting up Install Process Parsing package install arguments No package mod_python available. Nothing to do after further digging around I did find rh's mod_python rpm so I installed than and now seem to be ok. nevertheless it still bothers me the 'standard' tarball install didn't work. but I guess I'll leave that to others to worry about. thanks -mark On Tue, Dec 27, 2011 at 12:02 PM, Rami Chowdhury wrote: > On Tue, Dec 27, 2011 at 13:32, Mark Seger wrote: > > I've tried this on both RHEL5.5 and RHEL 6.0, using the default apache > that > > comes with the environment and itself isn't configured with mod_python. > > > > The first thing I noticed when mod_python wouldn't install was that apsx > > wasn't installed either. After a lot of pain, I discovered httpd-devel > > provided apsx and so installed that. > > > > Back to mod_python. After running > ./configure --with-apxs=/usr/sbin/apxs, > > according to the README I need to run "make dso", but that promptly > blows up > > because it's looking for header files in /usr/include/httpd and itlooks > like > > the apxs files are in /usr/include/apr1, so I copied all its contents to > > /usr/include/httpd. > > > > Now when I try to make dso, it successfully gets past finding its header > > files but now fails with: > > > > [root at rhel6 mod_python-2.7.10]# make dso > > make[1]: Entering directory `/tmp/mod_python-2.7.10/src' > > gcc -I/tmp/mod_python-2.7.10/src/include -I/usr/include/httpd > > -I/usr/include/python2.6 -c -o mod_python.o mod_python.c > > In file included from /usr/include/python2.6/pyconfig.h:6, > > from /usr/include/python2.6/Python.h:8, > > from /tmp/mod_python-2.7.10/src/include/mod_python.h:77, > > from mod_python.c:54: > > /usr/include/python2.6/pyconfig-64.h:1031:1: warning: "_POSIX_C_SOURCE" > > redefined > > In file included from /usr/include/sys/types.h:26, > > from /usr/include/httpd/apr-x86_64.h:127, > > from /usr/include/httpd/apr.h:19, > > from /usr/include/httpd/ap_config.h:25, > > from /usr/include/httpd/httpd.h:43, > > from /tmp/mod_python-2.7.10/src/include/mod_python.h:63, > > from mod_python.c:54: > > /usr/include/features.h:213:1: warning: this is the location of the > previous > > definition > > In file included from mod_python.c:54: > > /tmp/mod_python-2.7.10/src/include/mod_python.h:93: error: expected ?=?, > > ?,?, ?;?, ?asm? or ?__attribute__? before ?*? token > > /tmp/mod_python-2.7.10/src/include/mod_python.h:96: error: expected ?=?, > > ?,?, ?;?, ?asm? or ?__attribute__? before ?python_module? > > In file included from /tmp/mod_python-2.7.10/src/include/mod_python.h:99, > > from mod_python.c:54: > > /tmp/mod_python-2.7.10/src/include/util.h:57: error: expected ?;?, ?,? or > > ?)? before ?*? token > > In file included from > /tmp/mod_python-2.7.10/src/include/mod_python.h:100, > > from mod_python.c:54: > > > > and a lot more. Can anyone help? > > Is there a reason you're not using the system package manager? Does > "yum install mod_python" not find anything? How about "yum provides > */mod_python.so"? > > > -- > Rami Chowdhury > "A mind all logic is like a knife all blade - it makes the hand bleed > that uses it." -- Rabindranath Tagore > +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Tue Dec 27 12:23:05 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 04:23:05 +1100 Subject: Daemon management In-Reply-To: References: Message-ID: On 12/27/2011 12:43 PM, Fredrik Tolf wrote: > Dear list, > > Lately, I've had a personal itch to scratch, in that I run a couple of > Python programs as daemons, and sometimes want to inspect or alter them > in ad-hoc ways, or other times need to do things to them that are less > ad-hoc in nature, but nevertheless lack a natural user interface. > > In order to solve that problem, I wrote a small library to allow the > daemon to simply listen to some socket and accept arbitrary, but easily > definable, commands. It also provides a "remote REPL" to allow me to run > arbitrary Python code interactively in the context of the daemon. > > I was actually a bit surprised that I couldn't find any obvious existing > solution to the problem, so I'm writing this message in order to share > mine, just in case anyone else would happen to have the same problem as > I had and doesn't want to reinvent the wheel yet again: This is possible through the use of a debugger. I've never used it, but I heard good thing of winpdb which has remote debugging. (http://winpdb.org/) Another tool that I've never used is rconsole, part of rfoo library, which appears to be similar to pdm; it is also intended for the same kind of problem, managing long-running-processes/daemons. From rich at noir.com Tue Dec 27 12:41:55 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 09:41:55 -0800 Subject: confused about __new__ In-Reply-To: References: Message-ID: On 12/26/11 21:48 , Fredrik Tolf wrote: > On Mon, 26 Dec 2011, K. Richard Pixley wrote: >> I don't understand. Can anyone explain? > > I'm also a bit confused about __new__. I'd very much appreciate it if > someone could explain the following aspects of it: > > * The manual () says > that __new__ is "a static method (special-cased so you need not declare > it as such)". What does "special-cased" mean? Apparently, for > instance, in OP's case, Python did not automatically detect that it > should not be bound as a method. > > * Is there any part of the manual that explains, holistically, the > greater context of object instantiation into which __new__ fits? I can > only find small parts of it spread around the documentation for __new__ > and __init__, but no complete explanation of it. There are several > things I'm wondering about, like what it means to call a type object at > all; how methods, properties and the like are bound; how pickle can > instantiate a class without calling __init__; when and whether __dict__ > is created and a couple of other things. Is there a complete > documentation of the process anywhere that I haven't managed to find? Everything I know about this stuff I got by reading the manual. Search for __init__ and you'll find a pretty complete description of how objects are created, how to perturb that process, how attributes are looked up, and how to perturb that process. At least, you do in both python 2 and python 3 current manuals, I think. The conceptual leap for me was in recognizing that a class is just an object. The best way, (imo, so far), to create a singleton in python is to use the class itself as the singleton rather than ever instantiating it. With a little work, you can even prevent it from ever being instantiated. The concept of a "factory" gets a little weird then, as a factory might return an instantiation of some class but it might instead return a class proper - perhaps even an automatically constructed class - which is a somewhat different sort of factory. Calling a type is a little bit different. Calling a class is how you initiate an instantiation of that class. Calling an instantiation leads to __call__, (which may or may not have much semantic meaning, depending on your class). It's the difference between C() and C()(). (And the analogy holds for other builtin types, I think). Super() is also fairly well documented. But __metaclass__ could use a few more examples, imo. I'm still not entirely clear on __metaclass__. As I understand it, (which may well be completely wrong), __metaclass_ is the means of perturbing the results of "isinstance". You can create, (or delete), inheritance arbitrarily, and without even requiring real, existing classes to do it. I'm not clear on why you'd want to do this, nor why __metaclass__ is a better mechanism than, say, any of the implementations of "interface" which have been done in various tool kits or "mixins", which all seem to be somewhat overlapping in functionality. --rich From wolftracks at invalid.com Tue Dec 27 12:47:53 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 09:47:53 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 8:42 AM, Lie Ryan wrote: > On 12/28/2011 03:03 AM, W. eWatson wrote: >> Here's the traceback. > > The traceback seems to imply that matplotlib is not being installed > properly. Have you tried uninstalling then reinstalling matplotlib? > I believe I have, but I'll give it another go. From lie.1296 at gmail.com Tue Dec 27 12:50:28 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 04:50:28 +1100 Subject: Python education survey In-Reply-To: <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> Message-ID: On 12/28/2011 03:37 AM, Rick Johnson wrote: > My logic is this: > """ Including an IDE in the stdlib may have been a bad idea (although > i understand and support Guido's original vision for IDLE). But since > we do have it, we need to either MAINTAIN the package or REMOVE it. We > cannot just stick our heads in the sand and ignore the elephant in the > chicken coop. It's bad enough to bloat ANY stdlib with seldom used > modules, but i dare say, it's far worse to bloat a library with seldom > used modules that are poorly maintained! Every module in Python's > stdlib is a testament to the skill and professionalism of this > community as a whole. When a module looks or works badly, we ALL look > and work badly.""" AFAICS, Python has a pretty good reputation even outside Python community. I haven't seen anyone looks at Python badly because of IDLE. > It's no big secret to anyone in this community that both Tkinter and > IDLE are the red headed step children of Pythons stdlib. Very few > people speak kindly of either package. I believe GvR still believes in > the merits of both packages (psst: batteries included!) but he finds > himself at odds with the elite at pydev. Hmm, Maybe he DOES want to > remove them but fears loosing face...FYI: Guido IS is the original > author of both Tkinter and IDLE... it was HIS idea after all. However, > it is high time to re-ignite the original vision and form these > packages into something we can be proud of -- OR -- cut our losses and > remove them forever. I hope you're not attempting to put words into his mouth, what you think of Guido's ideas is not necessarily Guido's ideas. In any case, IDLE is open source -- I don't know exactly what license it's under but I'd assume it's the same as Python -- and anyone in this list and outside this list -- including you -- can freely fork it and work on it to improve it. In case you haven't realised it, it is pretty much impossible for a large open source project to "die"; even if Guido decided to remove IDLE from the standard library, it's not unlikely that someone will fork it and maintain it as a third party application. From lie.1296 at gmail.com Tue Dec 27 12:53:19 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 04:53:19 +1100 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> Message-ID: On 12/27/2011 10:41 PM, Eelco wrote: > > *Your suggestion of VIM is especially objectionable. Though I am sure > it is a great tool to you, the subject here is beginner education. > Just because it is a good tool for you, does not make it a good tool > for a beginner. Before using VIM, I used to use gedit (and still do, though not as often now); I don't think I've ever had any problem with not using a full blown IDE with Python. I generally don't miss not using an IDE since Python doesn't have the tradition of using overly verbose names like in Java. I'm personally of the opinion that beginners generally should start with a simple programmer text editors (gedit is a good example). Firstly, you don't want to confuse beginners with IDE vs language features; secondly, at the size of the problem beginners typically had, they don't **need** 95% of those features; and if you teach beginners powerful IDE features too early, by the time their problem gets big enough that the IDE features would actually help, they'd already forgotten about them. > IPython bundled with a lightweight but function-rich and non-hacker- > but-WYSIWYG editor would be a great choice. But until that comes > around, pycharm it is for me. From dihedral88888 at googlemail.com Tue Dec 27 12:53:55 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 27 Dec 2011 09:53:55 -0800 (PST) Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> Message-ID: <25014920.545.1325008435089.JavaMail.geo-discussion-forums@prlu4> From rich at noir.com Tue Dec 27 12:59:35 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 09:59:35 -0800 Subject: Python education survey In-Reply-To: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: On 12/19/11 19:51 , Raymond Hettinger wrote: > Do you use IDLE when teaching Python? > If not, what is the tool of choice? > If your goal is to quickly get new users up and running in Python, > what IDE or editor do you recommend? I would: a) let the students pick their own editor. b) encourage emacs and use emacs as a reference editor. The problem is that IDLE is hard to set up. (I've never managed it and I'm a well seasoned veteran). And pretty much only good for python, I'd expect. You'd do better to encourage eclipse, but setting that up isn't trivial either. You could create your own distribution of eclipse, but then you have that "only useful for python" problem again. If students are going to go anywhere else after this class, they're going to need to either be able to learn to switch editors or find an editor they can use more generally. Everyone ends up writing some html eventually, for instance. Either way requires climbing a learning curve that would be difficult to justify for a single class. OTOH, there are binary emacs distributions for all systems you've mentioned. And they work. I'm an antimicrosoft bigot, but I think my answer is probably the same regardless of whether we know the OS the students will be using or not. --rich From wolftracks at invalid.com Tue Dec 27 13:04:37 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 10:04:37 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 8:53 AM, Ian Kelly wrote: > On Tue, Dec 27, 2011 at 9:03 AM, W. eWatson wrote: >> File "C:\Python25\lib\site-packages\matplotlib\transforms.py", line 34, in >> >> from matplotlib._path import affine_transform >> ImportError: DLL load failed: The specified module could not be found. > > Do you not have the file > C:\Python25\lib\site-packages\matplotlib\_path.pyd? It's in the > installer archive, so you should be able to just open it up with a zip > program and extract it manually. If that's missing, then there may be > other things wrong with your installation, though, so I would > recommend a full reinstall. The path exits. _path.pyd exists. From rantingrickjohnson at gmail.com Tue Dec 27 13:04:40 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 10:04:40 -0800 (PST) Subject: Possible bug in string handling (with kludgy work-around) References: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: -- Note: superfluous indention removed for clarity! -- On Dec 27, 8:53?am, Dennis Lee Bieber wrote: > You can get by without the backslash in this situation too, by using > triple quoting: I would not do that because: 1. Because Python already has TWO string literal delimiters (' and ") 2. Because triple quote string literals are SPECIFICALLY created to solve the "multi-line issue" 3. Because you can confuse the hell out of someone who is reading Python code and they may miss the true purpose of triple quotes in Python But this brings up a very important topic. Why do we even need triple quote string literals to span multiple lines? Good question, and one i have never really mused on until now. It's amazing how much BS we just accept blindly! WE DON'T NEED TRIPLE QUOTE STRINGS! What we need is single quote strings that span multiple lines and triple quotes then become superfluous! For the problem of embedding quotes in string literals, we should be using markup. A SIMPLISTIC MARKUP! " This is a multi line string with a single quote --> and a double quote --> . Here is an embedded newline --> . And a backspace . Now we can dispense with all the BS! " > I find """ clearer, ''' could be a " and ' > packed tightly in some fonts, "', whereas """ can only be one construct) Another reason to ONLY use fixed width font when viewing code! Why would you use ANY font that would obscure chars SO ubiquitous as " and '? From fredrik at dolda2000.com Tue Dec 27 13:05:04 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 19:05:04 +0100 (CET) Subject: confused about __new__ In-Reply-To: References: Message-ID: On Tue, 27 Dec 2011, Ian Kelly wrote: > On Mon, Dec 26, 2011 at 10:48 PM, Fredrik Tolf wrote: >> I'm also a bit confused about __new__. I'd very much appreciate it if >> someone could explain the following aspects of it: >> >> ?* The manual () says >> ? that __new__ is "a static method (special-cased so you need not declare >> ? it as such)". What does "special-cased" mean? Apparently, for >> ? instance, in OP's case, ?Python did not automatically detect that it >> ? should not be bound as a method. > > It apparently has to do with the class creation. For the > special-casing to happen, the __new__ method has to be present when > the class is created. If it is, then it automatically gets wrapped in > a staticmethod. In the OP's case, he was adding the __new__ method > after class creation, so the wrapping did not happen automatically. Hmm, thank you. After trying a couple of things, it appears that the reason OP's method worked in Python 3 is that __get__ on ordinary functions in Python 3 simply returns the function itself when called on type lookup, rather than Python 2's unbound method objects. > http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy > """ > Class Types > Class types, or ?new-style classes,? are callable. These objects > normally act as factories for new instances of themselves, but > variations are possible for class types that override __new__(). The > arguments of the call are passed to __new__() and, in the typical > case, to __init__() to initialize the new instance. > """ > > AFAIK, that's pretty much it. When a type is called, __new__ is > called to create the new instance, and then __init__ is called to > initialize it (if __new__ returned an instance of the type). Since that description doesn't include when __dict__ is created, it isn't complete, however. I guess it's safe to assume that __dict__ is created inside object.__new__, but that also leaves some things unclear; see below. >> how methods, properties and the like are bound; > > When they're accessed, using the descriptor protocol, not as part of > the instantiation process. See: > http://docs.python.org/reference/datamodel.html#invoking-descriptors Ah, sorry. I had read that, but I had mistakenly thought that the same instance method object was always returned, which would have implied that it had to be stored for the instance somewhere, at some point. I see now that that is not actually the case, however. That clears up a whole lot for me. Thanks! >> when and whether __dict__ >> ? is created and a couple of other things. > > Under the hood as part of the object creation process, unless the > class uses __slots__. And also unless the object created is of type `object' or any other built-in type, which leaves me wondering exactly under what circumstances __dict__ actually is created. Is it some kind of special case in object.__new__ for types created with the `class' statement? There also appear to be other special cases in object.__new__: >>> object.__new__(dict) Traceback (most recent call last): File "", line 1, in TypeError: object.__new__(dict) is not safe, use dict.__new__() I guess my question reduces into the following: What does object.__new__ actually do, and what other special cases does it implement? -- Fredrik Tolf From rantingrickjohnson at gmail.com Tue Dec 27 13:11:48 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 10:11:48 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> Message-ID: <76281802-e45b-4850-82be-06a51cde1b20@g41g2000yqa.googlegroups.com> On Dec 27, 11:50?am, Lie Ryan wrote: > In case you haven't realised it, it is pretty > much impossible for a large open source project to "die"; even if Guido > decided to remove IDLE from the standard library I don't remember stating that Python would die if IDLE was removed (not sure if you misunderstood me or you're just making a general statement???). My belief is that the atrocious state of IDLE and Tkinter's code bases are making us look bad as a community. And i can go either way on the issue; remove them both, or enrich them both. Either way we improve on the current situation. My point is that we CANNOT just ignore the issue. From rantingrickjohnson at gmail.com Tue Dec 27 13:21:54 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 10:21:54 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <54c9d146-eef9-43bb-9e0b-33f51e410954@t8g2000yqg.googlegroups.com> On Dec 27, 11:59?am, K Richard Pixley wrote: > The problem is that IDLE is hard to set up. ?(I've never managed it and > I'm a well seasoned veteran). Can you qualify that statement? Do you mean "difficult to set up on certain OS's"? Because for windows there is no difficulty. > And [IDLE is] pretty much only good for python, Yes, i will agree on that! We could create snap-ins for other languages, but, there are many good multi-language editors out there already. > I'd > expect. ?You'd do better to encourage eclipse, but setting that up isn't > trivial either. ?You could create your own distribution of eclipse, but > then you have that "only useful for python" problem again. Same boat, different lake. > If students are going to go anywhere else after this class, they're > going to need to either be able to learn to switch editors or find an > editor they can use more generally. Agreed! > ?Everyone ends up writing some html > eventually, for instance. most folks write more than just HTML! Ruby, Lisp, Perl, C, Java, etc. > ?Either way requires climbing a learning curve > that would be difficult to justify for a single class. Yes, i must agree that IDLE does not scale. It's true that IDLE is only good for Python. Could we make the IDLE usable for other languages? Yes, but what good is that going to do? IDLE is already slow due to Tkinter being slow, due to Python being slow... etc. I must admit that IDLE is only useful for complete beginners, those who code only in Python, or those who don't care about using a specific Python IDE. But that damn "batteries included" and CPFE keeps creeping up! What to do, what to do? From bahamutzero8825 at gmail.com Tue Dec 27 13:26:23 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 27 Dec 2011 12:26:23 -0600 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <4EFA0DCF.90001@gmail.com> On 12/27/2011 11:59 AM, K Richard Pixley wrote: > You'd do better to encourage eclipse, but setting that up isn't > trivial either. IIRC, all I had to do to set up PyDev was copy a URL to Eclipse's "Install New Software" wizard, and have Eclipse download and install it. Extra steps are needed if a different implementation of Python (e.g. Jython) is needed, but other than that, the user only needs to specify a couple options (e.g. Python grammar version) at project creation time. This assumes that Python is already installed, but why wouldn't it be? > You could create your own distribution of eclipse, but > then you have that "only useful for python" problem again. AFAIK, Eclipse should always be good for Java unless you do some serious hacking. > If students are going to go anywhere else after this class, they're > going to need to either be able to learn to switch editors or find an > editor they can use more generally. There are a ton of editors that have syntax highlighting and other little features for many languages. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From ian.g.kelly at gmail.com Tue Dec 27 13:28:42 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 11:28:42 -0700 Subject: confused about __new__ In-Reply-To: References: Message-ID: On Tue, Dec 27, 2011 at 10:41 AM, K Richard Pixley wrote: > The conceptual leap for me was in recognizing that a class is just an > object. ?The best way, (imo, so far), to create a singleton in python is to > use the class itself as the singleton rather than ever instantiating it. > ?With a little work, you can even prevent it from ever being instantiated. I don't think that's actually possible: Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> class Foo: ... def __new__(cls): ... raise TypeError ... def __init__(self): ... raise TypeError ... >>> type(object.__new__(Foo)) I prefer the Borg pattern for singletons myself. When you need a Borg instance, you just instantiate the class normally, do whatever you need with it, and then discard it normally. It doesn't matter how many instances there are (there can even be zero) because they all share identical state. It's better encapsulation too, since client code does not even need to know that the class is a singleton variant; that becomes an implementation detail. http://www.aleax.it/Python/5ep.html http://code.activestate.com/recipes/66531/ > But __metaclass__ could use a few more examples, imo. ?I'm still not > entirely clear on __metaclass__. ?As I understand it, (which may well be > completely wrong), __metaclass_ is the means of perturbing the results of > "isinstance". ?You can create, (or delete), inheritance arbitrarily, and > without even requiring real, existing classes to do it. A metaclass is the class of another class (which is normally `type` if no metaclass is specified). It's used to customize the way that the class is created, in the same way that the class can be used to customize the way instances are created (e.g. by overriding __new__). You could use it to mess with the class's base classes, although I think that usage is rare. It's more common to use it to automatically add or modify entries in the class dict. For example, Django models use a metaclass to collect all the fields and options declared for the model class and to setup a default manager for the model class. Note that many things that can be done with a metaclass can also be done with a class decorator. The main difference as I see it is that the metaclass is inherited by subclasses, whereas a decorator would need to be applied individually to each subclass. From wolftracks at invalid.com Tue Dec 27 13:29:12 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 10:29:12 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 8:42 AM, Lie Ryan wrote: > On 12/28/2011 03:03 AM, W. eWatson wrote: >> Here's the traceback. > > The traceback seems to imply that matplotlib is not being installed > properly. Have you tried uninstalling then reinstalling matplotlib? > I just did, and the results are this: ================= Traceback (most recent call last): File "C:\Sentinel\Sent_user-20080716.py", line 16, in from pylab import plot, xlabel, ylabel, title, show, xticks, bar File "C:\Python25\lib\site-packages\pylab.py", line 1, in from matplotlib.pylab import * File "C:\Python25\lib\site-packages\matplotlib\pylab.py", line 204, in from matplotlib import mpl # pulls in most modules File "C:\Python25\lib\site-packages\matplotlib\mpl.py", line 1, in from matplotlib import artist File "C:\Python25\lib\site-packages\matplotlib\artist.py", line 4, in from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath File "C:\Python25\lib\site-packages\matplotlib\transforms.py", line 34, in from matplotlib._path import affine_transform ImportError: DLL load failed: The specified module could not be found. ===================== I'm suspicious of this line, and maybe even the app program. There may have been a change to the code that required the later two versions of numpy and matplotlib. In fact, I'm using the later version here, so I'll see if I can back up to the first Python app they produced. from pylab import plot, xlabel, ylabel, title, show, xticks, bar From lists at cheimes.de Tue Dec 27 13:36:12 2011 From: lists at cheimes.de (Christian Heimes) Date: Tue, 27 Dec 2011 19:36:12 +0100 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: Am 27.12.2011 17:03, schrieb W. eWatson: > from matplotlib._path import affine_transform > ImportError: DLL load failed: The specified module could not be found. You are missing one or more DLLs that is required to load the _path.pyd module. You can use http://www.dependencywalker.com/ to track down missing DLLs. Christian From miki.tebeka at gmail.com Tue Dec 27 14:01:02 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 27 Dec 2011 11:01:02 -0800 (PST) Subject: Daemon management In-Reply-To: References: Message-ID: <25792721.106.1325012462924.JavaMail.geo-discussion-forums@yqbl25> What was the term you used to search? Since http://pypi.python.org/pypi/ingress/0.1.1 seems to fit your description. From miki.tebeka at gmail.com Tue Dec 27 14:01:02 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 27 Dec 2011 11:01:02 -0800 (PST) Subject: Daemon management In-Reply-To: References: Message-ID: <25792721.106.1325012462924.JavaMail.geo-discussion-forums@yqbl25> What was the term you used to search? Since http://pypi.python.org/pypi/ingress/0.1.1 seems to fit your description. From hoogendoorn.eelco at gmail.com Tue Dec 27 14:45:28 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 27 Dec 2011 11:45:28 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> Message-ID: On Dec 27, 6:53?pm, Lie Ryan wrote: > On 12/27/2011 10:41 PM, Eelco wrote: > > > > > *Your suggestion of VIM is especially objectionable. Though I am sure > > it is a great tool to you, the subject here is beginner education. > > Just because it is a good tool for you, does not make it a good tool > > for a beginner. > > Before using VIM, I used to use gedit (and still do, though not as often > now); I don't think I've ever had any problem with not using a full > blown IDE with Python. I generally don't miss not using an IDE since > Python doesn't have the tradition of using overly verbose names like in > Java. As for my personal use, I very much prefer an IDE. I hate having only crappy code completion, for starters, and I like a good integrated debugger. But then again, im spoiled I suppose coming from C#. On the other hand, ive worked for many years using a very minimal notepad +command line compilation setup as well. But I can very well imagine that people are perfectly happy with more hackerish tools. That is, once they have gotten past the learning curve. > I'm personally of the opinion that beginners generally should start with > a simple programmer text editors (gedit is a good example). Firstly, you > don't want to confuse beginners with IDE vs language features; secondly, > at the size of the problem beginners typically had, they don't **need** > 95% of those features; and if you teach beginners powerful IDE features > too early, by the time their problem gets big enough that the IDE > features would actually help, they'd already forgotten about them. A good IDE should get out of your way if you want it to. I like pycharm in this regard; click an x or two, and you are facing just your text editor and console/output window. Generally, I think a non-cluttered IDE is ideal for a beginner. You dont have to explain to them how to open a file, and if you tell them to hit the 'play' button to start running their code (not a hard concept to grasp or remember either) they are good to start hacking. Also, I think code completion is a blessing to beginning programmers. IPython is good in that regard; until you switch to editing files, where your choice is between notepad, or configuring an other editor you dont want your class to spend any time on, and youll end up with something that still doesnt do much more than syntax highlighting. From rantingrickjohnson at gmail.com Tue Dec 27 15:04:26 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 12:04:26 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> Message-ID: <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> On Dec 27, 1:45?pm, Eelco wrote: > On Dec 27, 6:53?pm, Lie Ryan wrote: > > On 12/27/2011 10:41 PM, Eelco wrote: > > Before using VIM, I used to use gedit Eelco, please don't get offended, but can you (and everyone else) stop using silly verbage like "used to", "use to", "suppose to", "hard" when you "difficult", and "pretty" when you mean "very". I find this verbiage to be quite ridiculous. In this case you could have simply said... """Before using VIM, I USED gedit.""" or if you want to stress that you don't use gedit anymore you could say... """ Previously i used gedit, but have since moved on to VIM.""" Thanks > As for my personal use, I very much prefer an IDE. I hate having only > crappy code completion Syntax highlight is important ESPECIALLY if you're multi-lingual. Sometimes when i switch between the many languages i know, the highlight of the syntax is the only blindingly apparent clue. Take Python and Ruby for example. > A good IDE should get out of your way if you want it to. I like > pycharm in this regard; click an x or two, and you are facing just > your text editor and console/output window. A "good IDE" is nothing more than an extension of a base text editor with options to switch on as little or as much IDE machinery as you like. Why would you have a text editor AND an IDE? When an IDE is just an intelligent texteditor? That is one thing that i find missing in IDLE; the ability to turn off certain things. With IDLE, it's all or nothing. > Generally, I think a non-cluttered IDE is ideal for a beginner. Agreed, see last response ^^^ > You > dont have to explain to them how to open a file, and if you tell them > to hit the 'play' button to start running their code (not a hard > concept to grasp or remember either) they are good to start hacking. I always though "run" was a perfect verb for "running" code... but who knows :) From wolftracks at invalid.com Tue Dec 27 15:27:07 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 12:27:07 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: ... > > I'm suspicious of this line, and maybe even the app program. There may > have been a change to the code that required the later two versions of > numpy and matplotlib. In fact, I'm using the later version here, so I'll > see if I can back up to the first Python app they produced. > > from pylab import plot, xlabel, ylabel, title, show, xticks, bar I found what I thought might be the original, but that got changed in a later version on July 16, 2008, which became what I continued to use. The libs are as in my first post. I looked at the original Python app, and it used Python 2.4.2 but sure doesn't use the same lib versions as the 2008 version. The puzzler for me is why I can find numpy and matplotlib on my PC slightly latter than shown in the first post. It's like another release of the app came later. Unfortunately, I'm stuck on that, since our sponsors seem to be out for the entire holiday season. From rich at noir.com Tue Dec 27 15:31:10 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 12:31:10 -0800 Subject: confused about __new__ In-Reply-To: References: Message-ID: <4EFA2B0E.2000506@noir.com> On 12/27/11 10:28 , Ian Kelly wrote: > On Tue, Dec 27, 2011 at 10:41 AM, K Richard Pixley wrote: >> The conceptual leap for me was in recognizing that a class is just an >> object. The best way, (imo, so far), to create a singleton in python is to >> use the class itself as the singleton rather than ever instantiating it. >> With a little work, you can even prevent it from ever being instantiated. > I don't think that's actually possible: > > Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> class Foo: > ... def __new__(cls): > ... raise TypeError > ... def __init__(self): > ... raise TypeError > ... >>>> type(object.__new__(Foo)) > Try: class Foo(object): def __new__(cls): return cls --rich From ian.g.kelly at gmail.com Tue Dec 27 15:34:08 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 13:34:08 -0700 Subject: confused about __new__ In-Reply-To: <4EFA2B0E.2000506@noir.com> References: <4EFA2B0E.2000506@noir.com> Message-ID: On Tue, Dec 27, 2011 at 1:31 PM, K Richard Pixley wrote: > On 12/27/11 10:28 , Ian Kelly wrote: >> >> On Tue, Dec 27, 2011 at 10:41 AM, K Richard Pixley ?wrote: >>> >>> The conceptual leap for me was in recognizing that a class is just an >>> object. ?The best way, (imo, so far), to create a singleton in python is >>> to >>> use the class itself as the singleton rather than ever instantiating it. >>> ?With a little work, you can even prevent it from ever being >>> instantiated. >> >> I don't think that's actually possible: >> >> Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit >> (Intel)] on win32 >> Type "help", "copyright", "credits" or "license" for more information. >>>>> >>>>> class Foo: >> >> ... ? ? def __new__(cls): >> ... ? ? ? ? raise TypeError >> ... ? ? def __init__(self): >> ... ? ? ? ? raise TypeError >> ... >>>>> >>>>> type(object.__new__(Foo)) >> >> > > > Try: > > class Foo(object): > ? ?def __new__(cls): > ? ? ? ?return cls Okay: Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> class Foo: ... def __new__(cls): ... return cls ... >>> f1 = object.__new__(Foo) >>> f2 = object.__new__(Foo) >>> type(f1), type(f2) (, ) >>> f1 is f2 False From rich at noir.com Tue Dec 27 15:39:13 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 12:39:13 -0800 Subject: Python education survey In-Reply-To: <54c9d146-eef9-43bb-9e0b-33f51e410954@t8g2000yqg.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <54c9d146-eef9-43bb-9e0b-33f51e410954@t8g2000yqg.googlegroups.com> Message-ID: On 12/27/11 10:21 , Rick Johnson wrote: > On Dec 27, 11:59 am, K Richard Pixley wrote: > >> The problem is that IDLE is hard to set up. (I've never managed it and >> I'm a well seasoned veteran). > > Can you qualify that statement? Do you mean "difficult to set up on > certain OS's"? Because for windows there is no difficulty. The distributed binaries haven't worked when I tried on any version of linux or macosx that I used. Attempting to build from source also failed. (I'm pretty much an anti-microsoft bigot). >> Everyone ends up writing some html >> eventually, for instance. > > most folks write more than just HTML! Ruby, Lisp, Perl, C, Java, etc. Dedicated computer folks and experimental tinkerers, yes, I concur. But even casual computer users eventually want to write html, or compose for a wiki and get annoyed when their 3 hours of work in a web browser test field are lost because they changed pages. --rich From lie.1296 at gmail.com Tue Dec 27 16:13:03 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 08:13:03 +1100 Subject: Python education survey In-Reply-To: <76281802-e45b-4850-82be-06a51cde1b20@g41g2000yqa.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> <76281802-e45b-4850-82be-06a51cde1b20@g41g2000yqa.googlegroups.com> Message-ID: On 12/28/2011 05:11 AM, Rick Johnson wrote: > On Dec 27, 11:50 am, Lie Ryan wrote: >> In case you haven't realised it, it is pretty >> much impossible for a large open source project to "die"; even if Guido >> decided to remove IDLE from the standard library > > I don't remember stating that Python would die if IDLE was removed > (not sure if you misunderstood me or you're just making a general > statement???). My belief is that the atrocious state of IDLE and > Tkinter's code bases are making us look bad as a community. And i can > go either way on the issue; remove them both, or enrich them both. > Either way we improve on the current situation. My point is that we > CANNOT just ignore the issue. The point is, I didn't think it's such a pressing issue. I haven't seen anyone in or outside Python communities making their conclusion about Python based on IDLE. In any case, removing IDLE without a much better replacement is pretty much out of the question. If people installed Python in vanilla Windows install, they would only have Notepad to edit their code. From lie.1296 at gmail.com Tue Dec 27 16:23:02 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 08:23:02 +1100 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: References: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/28/2011 05:04 AM, Rick Johnson wrote: > -- > Note: superfluous indention removed for clarity! > -- > > On Dec 27, 8:53 am, Dennis Lee Bieber wrote: >> You can get by without the backslash in this situation too, by using >> triple quoting: > > I would not do that because: > 1. Because Python already has TWO string literal delimiters (' and ") > 2. Because triple quote string literals are SPECIFICALLY created to > solve the "multi-line issue" > 3. Because you can confuse the hell out of someone who is reading > Python code and they may miss the true purpose of triple quotes in > Python > > But this brings up a very important topic. Why do we even need triple > quote string literals to span multiple lines? Good question, and one i > have never really mused on until now. It's amazing how much BS we just > accept blindly! WE DON'T NEED TRIPLE QUOTE STRINGS! What we need is > single quote strings that span multiple lines and triple quotes then > become superfluous! For the problem of embedding quotes in string > literals, we should be using markup. A SIMPLISTIC MARKUP! > > " This is a multi line > string with a single quote --> > and a double quote --> . Here is an > embedded newline --> . And a backspace. > > Now we can dispense with all the BS! > " Ok, you're trolling. From tjreedy at udel.edu Tue Dec 27 16:29:03 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 27 Dec 2011 16:29:03 -0500 Subject: PDF 508 Compliance with Python? In-Reply-To: References: Message-ID: On 12/27/2011 11:35 AM, Yanovsky, Boris, VHACIN wrote: > Hello, > > I am new to Python and I hope my question makes sense. We are trying to > make PDFs 508 Compliant, meaning that the objects within them have to be > tagged. The problem is that we have hundreds, and potentially thousands > of PDFs, since they are automated reports that we create on a regular basis. > > Is there a code or feature in Python that can read PDFs and tag the > objects within them for this kind of compliance? There are 3rd party python packages that can read and write .pdfs. Google 'Python pdf library' or just go to http://pypi.python.org/pypi?%3Aaction=search&term=pdf&submit=search Make sure you check out the one from ReportLab. I suspect at least one will do what you want. -- Terry Jan Reedy From ian.g.kelly at gmail.com Tue Dec 27 16:31:00 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 14:31:00 -0700 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> <76281802-e45b-4850-82be-06a51cde1b20@g41g2000yqa.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 2:13 PM, Lie Ryan wrote: > In any case, removing IDLE without a much better replacement is pretty much > out of the question. If people installed Python in vanilla Windows install, > they would only have Notepad to edit their code. No no, Wordpad is much better for editing Python. It even supports (manual) syntax highlighting! And of course for the console lovers there is MS-DOS Edit. From wolftracks at invalid.com Tue Dec 27 16:35:15 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 13:35:15 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: I realized that I had a working copy of the app on another XP PC, so I looked at what I had installed for Python. It was not what I had posted the first time. There must have been some shift after the July 16, 2008 date. One lib that was missing was scipy. I just collected the three libs I saw there. numpy matplotlib scipy Scipy uses as its installer. scipy-0.6.0.win32-py2.5.exe I replaced numpy and matplotlib, and added scipy. I still get errors, but perhaps because the install order is now wrong. It was numpy matplotlib Does anyone know the right order? The complaint msgs were very close to what I posted before, and matplotlib was again the last of the bunch. DLL for it not found. Possibly I need the dependencywalker that Christian mentioned. From wolftracks at invalid.com Tue Dec 27 16:35:31 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 13:35:31 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 10:36 AM, Christian Heimes wrote: > Am 27.12.2011 17:03, schrieb W. eWatson: >> from matplotlib._path import affine_transform >> ImportError: DLL load failed: The specified module could not be found. > > You are missing one or more DLLs that is required to load the _path.pyd > module. You can use http://www.dependencywalker.com/ to track down > missing DLLs. > > Christian > OK, I installed it. Is there an easy way to go to track down this dll. There's a Help, but I can't print it from the PC I have it on. From tjreedy at udel.edu Tue Dec 27 16:38:58 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 27 Dec 2011 16:38:58 -0500 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: References: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/27/2011 1:04 PM, Rick Johnson wrote: > But this brings up a very important topic. Why do we even need triple > quote string literals to span multiple lines? Good question, and one i > have never really mused on until now. I have, and the reason I thought of is that people, including me, too ofter forget or accidentally fail to properly close a string literal, and type something like 'this is a fairly long single line string" and wonder why they get a syntax error lines later, or, in interactive mode, why the interpreter does not respond to a newline. Color coding editors make it easier to catch such errors, but they were less common in 1991. And there is still uncolored interactive mode. There may also be a technical reason as to how the lexer works. -- Terry Jan Reedy From hoogendoorn.eelco at gmail.com Tue Dec 27 16:44:14 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 27 Dec 2011 13:44:14 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> Message-ID: <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> On Dec 27, 9:04?pm, Rick Johnson wrote: > On Dec 27, 1:45?pm, Eelco wrote: > > > On Dec 27, 6:53?pm, Lie Ryan wrote: > > > On 12/27/2011 10:41 PM, Eelco wrote: > > > Before using VIM, I used to use gedit > > Eelco, please don't get offended, but can you (and everyone else) stop > using silly verbage like "used to", "use to", "suppose to", "hard" > when you "difficult", and "pretty" when you mean "very". I find this > verbiage to be quite ridiculous. In this case you could have simply > said... > > """Before using VIM, I USED gedit.""" > > or if you want to stress that you don't use gedit anymore you could > say... > > """ Previously i used gedit, but have since moved on to VIM.""" > > Thanks Despite the fact that you mis-attributed that quote to me, im going to be a little bit offended in the name of its actual author anyway. Thats a lot of words to waste on your linguistic preferences. Personally, I reserve the right to botch my non-native languages as much as I please. > > You > > dont have to explain to them how to open a file, and if you tell them > > to hit the 'play' button to start running their code (not a hard > > concept to grasp or remember either) they are good to start hacking. > > I always though "run" was a perfect verb for "running" code... but who > knows :) Im assuming the audience is familiar with an ipod, but not an IDE, or programming in general. To their eyes, it looks like a 'play' button; but yes, 'running' is what its called in my mind. From fredrik at dolda2000.com Tue Dec 27 16:46:27 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 22:46:27 +0100 (CET) Subject: Daemon management In-Reply-To: References: Message-ID: On Wed, 28 Dec 2011, Lie Ryan wrote: > On 12/27/2011 12:43 PM, Fredrik Tolf wrote: >> [...] > This is possible through the use of a debugger. I've never used it, but I > heard good thing of winpdb which has remote debugging. (http://winpdb.org/) Thanks, but not as long as the debugger freezes the program it attaches to. > Another tool that I've never used is rconsole, part of rfoo library, which > appears to be similar to pdm; it is also intended for the same kind of > problem, managing long-running-processes/daemons. Interesting. That does sound very similar to what I wanted, so it's kind of a shame that I didn't find it. If anything, it seems that PDM is a bit more flexible in that it can handle limited references to remote objects, event notifications from the server to the client, and has better support for handling both TCP and Unix sockets (the rconsole script, for instance, seems to lack support for connecting to Unix sockets), so I guess my time wasn't wasted. -- Fredrik Tolf From fredrik at dolda2000.com Tue Dec 27 16:52:01 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 22:52:01 +0100 (CET) Subject: Daemon management In-Reply-To: <25792721.106.1325012462924.JavaMail.geo-discussion-forums@yqbl25> References: <25792721.106.1325012462924.JavaMail.geo-discussion-forums@yqbl25> Message-ID: On Tue, 27 Dec 2011, Miki Tebeka wrote: > What was the term you used to search? Since http://pypi.python.org/pypi/ingress/0.1.1 seems to fit your description. Not quite, though. It (AFAICT, at least) only provides the REPL part, with no way to construct a more program-friendly interface. -- Fredrik Tolf From candide at free.invalid Tue Dec 27 16:56:19 2011 From: candide at free.invalid (candide) Date: Tue, 27 Dec 2011 22:56:19 +0100 Subject: Type object returned by the re.compile function Message-ID: <4efa3f05$0$6546$426a74cc@news.free.fr> The Python 2.7 official documentation here: http://docs.python.org/library/re.html#re.compile doesn't specify the type object returned by the re.compiled function. According to the documentation, re.compile returns a "regular expression object". A regular expression object seems to be an instance of the class RegexObject. The documentation mentions this class here : http://docs.python.org/library/re.html#regular-expression-objects but i don't see any existing class with this name : >>> import re >>> re.RegexObject Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'RegexObject' >>> Actually, a regular expression object doesn't seem to be a class object : >>> import re >>> reo = re.compile('') >>> reo.__class__ Traceback (most recent call last): File "", line 1, in AttributeError: __class__ >>> but has attributes : >>> dir(reo) ['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner', 'search', 'split', 'sub', 'subn'] >>> I have the same problem with the match object : the doc mentions a re.MatchObject class but this class doesn't exist : >>> re.MatchObject Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'MatchObject' >>> Any clarification is welcome. From rich at noir.com Tue Dec 27 17:04:27 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 14:04:27 -0800 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: On 12/27/11 10:26 , Andrew Berg wrote: > On 12/27/2011 11:59 AM, K Richard Pixley wrote: >> You'd do better to encourage eclipse, but setting that up isn't >> trivial either. > IIRC, all I had to do to set up PyDev was copy a URL to Eclipse's > "Install New Software" wizard, and have Eclipse download and install it. > Extra steps are needed if a different implementation of Python (e.g. > Jython) is needed, but other than that, the user only needs to specify a > couple options (e.g. Python grammar version) at project creation time. > This assumes that Python is already installed, but why wouldn't it be? You still need to match versions of PyDev to versions of Eclipse to versions of operating system to versions of other eclipse plugins. I spent a few days trying to get it together once and came to the conclusion that it was a much bigger effort than I was willing to commit to. >> You could create your own distribution of eclipse, but >> then you have that "only useful for python" problem again. > AFAIK, Eclipse should always be good for Java unless you do some serious > hacking. Depends on which versions of eclipse, java, os, other plugins, etc. >> If students are going to go anywhere else after this class, they're >> going to need to either be able to learn to switch editors or find an >> editor they can use more generally. > There are a ton of editors that have syntax highlighting and other > little features for many languages. Exactly. My preference is emacs but I'll admit that the learning curve there is pretty high by today's standards. (Whether it's worth the effort is a debatable point.) There are certainly many others. --rich From rich at noir.com Tue Dec 27 17:19:44 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 14:19:44 -0800 Subject: confused about __new__ In-Reply-To: References: <4EFA2B0E.2000506@noir.com> Message-ID: <5wrKq.39032$mJ.10777@newsfe10.iad> On 12/27/11 12:34 , Ian Kelly wrote: > On Tue, Dec 27, 2011 at 1:31 PM, K Richard Pixley wrote: >> On 12/27/11 10:28 , Ian Kelly wrote: >>> >>> On Tue, Dec 27, 2011 at 10:41 AM, K Richard Pixley wrote: >>>> >>>> The conceptual leap for me was in recognizing that a class is just an >>>> object. The best way, (imo, so far), to create a singleton in python is >>>> to >>>> use the class itself as the singleton rather than ever instantiating it. >>>> With a little work, you can even prevent it from ever being >>>> instantiated. >>> >>> I don't think that's actually possible: >>> >>> Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit >>> (Intel)] on win32 >>> Type "help", "copyright", "credits" or "license" for more information. >>>>>> >>>>>> class Foo: >>> >>> ... def __new__(cls): >>> ... raise TypeError >>> ... def __init__(self): >>> ... raise TypeError >>> ... >>>>>> >>>>>> type(object.__new__(Foo)) >>> >>> >> >> >> Try: >> >> class Foo(object): >> def __new__(cls): >> return cls > > > Okay: > > Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> class Foo: > ... def __new__(cls): > ... return cls > ... >>>> f1 = object.__new__(Foo) >>>> f2 = object.__new__(Foo) >>>> type(f1), type(f2) > (,) >>>> f1 is f2 > False I'm not seeing why you're using "object.__new__". With Foo(), it seems fine. rich at fuji-land.noir.com> python Python 2.7.2 (default, Dec 12 2011, 13:05:49) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> class Foo(object): ... def __new__(cls): ... return cls ... >>> f1 = Foo() >>> f2 = Foo() >>> id(Foo) 4298515984 >>> id(f1) 4298515984 >>> id(f2) 4298515984 rich at fuji-land.noir.com> python3 Python 3.2.2 (default, Dec 26 2011, 15:03:08) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> class Foo(object): ... def __new__(cls): ... return cls ... >>> f1 = Foo() >>> f2 = Foo() >>> id(f1) 4298841008 >>> id(f2) 4298841008 >>> f1.stuff = True >>> f2.stuff True >>> id(Foo) 4298841008 Are you trying to demonstrate that I haven't prevented you from instantiating Foo? If so, then I will cede that point. I certainly don't know enough about python internals just now to even claim to be capable of protecting a class from a hostile user. My guess is that short of a privileged/unprivileged split, or castrating the interpreter and locking you into it, such a protection would not be possible. My point was that I can indeed intercept common and convenient usage to create a lovely singleton semantic. I can't force you to use it. (Nor do I have any motivation to so do.) --rich From malaclypse2 at gmail.com Tue Dec 27 17:29:25 2011 From: malaclypse2 at gmail.com (Jerry Hill) Date: Tue, 27 Dec 2011 17:29:25 -0500 Subject: Type object returned by the re.compile function In-Reply-To: <4efa3f05$0$6546$426a74cc@news.free.fr> References: <4efa3f05$0$6546$426a74cc@news.free.fr> Message-ID: On Tue, Dec 27, 2011 at 4:56 PM, candide wrote: >>>> import re >>>> reo = re.compile('') >>>> reo.__class__ > Traceback (most recent call last): > ?File "", line 1, in > AttributeError: __class__ >>>> I'm not going to comment on what type is returned from the various functions in the re module, mostly because all I have installed locally is 2.6, and I'm not sure if anything has changed in 2.7 or 3.2. Instead, I will recommend a different tool for your toolbox. Take a look at the type() builtin function ( http://docs.python.org/library/functions.html#type ). Particularly, instead of inspecting reo.__class__ in your example above, you can print out type(reo). At a guess, those objects are missing the __class__ attribute, possibly because they are old style classes. -- Jerry From ian.g.kelly at gmail.com Tue Dec 27 17:31:30 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 15:31:30 -0700 Subject: Type object returned by the re.compile function In-Reply-To: <4efa3f05$0$6546$426a74cc@news.free.fr> References: <4efa3f05$0$6546$426a74cc@news.free.fr> Message-ID: On Tue, Dec 27, 2011 at 2:56 PM, candide wrote: > The Python 2.7 official documentation here: > > http://docs.python.org/library/re.html#re.compile > > doesn't specify the type object returned by the re.compiled function. > According to the documentation, re.compile returns a "regular expression > object". > > A regular expression object seems to be an instance of the class > RegexObject. The documentation mentions this class here : > > http://docs.python.org/library/re.html#regular-expression-objects > > but i don't see any existing class with this name : Presumably if it's not in the module then it's not meant to be directly invoked. You should always use re.compile(). If you really want to assign the type a name, though: >>> import re >>> RegexObject = type(re.compile('')) >>> RegexObject Cheers, Ian From ian.g.kelly at gmail.com Tue Dec 27 17:43:29 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 15:43:29 -0700 Subject: confused about __new__ In-Reply-To: <5wrKq.39032$mJ.10777@newsfe10.iad> References: <4EFA2B0E.2000506@noir.com> <5wrKq.39032$mJ.10777@newsfe10.iad> Message-ID: On Tue, Dec 27, 2011 at 3:19 PM, K Richard Pixley wrote: > Are you trying to demonstrate that I haven't prevented you from > instantiating Foo? ?If so, then I will cede that point. ?I certainly don't > know enough about python internals just now to even claim to be capable of > protecting a class from a hostile user. ?My guess is that short of a > privileged/unprivileged split, or castrating the interpreter and locking you > into it, such a protection would not be possible. Yes, that was the point. I certainly don't think that anybody should ever be creating class instances by directly invoking object.__new__ unless they really, really know what they are doing. I was just pointing out that I don't think it's actually possible to prevent classes from being instantiated. > My point was that I can indeed intercept common and convenient usage to > create a lovely singleton semantic. ?I can't force you to use it. (Nor do I > have any motivation to so do.) Sure, I don't disagree with that. And the object.__new__ workaround is a general point that doesn't apply only to singletons. Any class that customizes its instantiation process can be messed up by doing that. From steve+comp.lang.python at pearwood.info Tue Dec 27 17:57:26 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Dec 2011 22:57:26 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <4ef757e3$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4efa4d55$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 13:41:34 -0800, Eelco wrote: > On Dec 25, 6:05?pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Sun, 25 Dec 2011 07:38:17 -0800, Eelco wrote: [...] >> > How is 'head, *tail = sequence' or semantically entirely >> > equivalently, 'head, tail::list = sequence' any different then? Of >> > course after interpretation/compilation, what it boils down to is >> > that we are constructing a list and binding it to the identifier >> > tail, but that is not how it is formulated in python as a language >> >> I'm afraid it is. >> >> Here's the definition of assignment in Python 3: >> http://docs.python.org/py3k/reference/simple_stmts.html#assignment- >> statements > > Que? You have claimed that "constructing a list and binding it to the identifier" is not how iterator unpacking is formulated in Python the language. But that is wrong. That *is* how iterator unpacking is formulated in Python the language. The reference manual goes into detail on how assignment is defined in Python. You should read it. > 'head, *tail = sequence' > > Is how one currently unpacks a head and tail in idiomatic python > > This is semantically equivalent to > > 'head = sequence[0]' > 'tail = list(sequence[1:])' There's a reason this feature is called *iterable* unpacking: it operates on any iterable object, not just indexable sequences. 'head, *tail = sequence' is not just semantically equivalent to, but *actually is* implemented as something very close to: temp = iter(sequence) head = next(temp) tail = list(temp) del temp Extended iterable unpacking, as in 'head, *middle, tail = sequence' is a little more complex, but otherwise the same: it operates using the iterator protocol, not indexing. I recommend you read the PEP, if you haven't already done so. http://www.python.org/dev/peps/pep-3132/ > But these forms are linguistically different, in too many different ways > to mention. How about mentioning even one way? Because I have no idea what differences you are referring to, or why you think they are important. >> > We dont have >> > something of the form 'tail = list_tail(sequence)'. >> >> I'm afraid we do. See the definition of assignment again. > > Que? Again, you make a claim about Python which is contradicted by the documented behaviour of the language. You claim that we DON'T have something of the form 'tail = list_tail(sequence)', but that is *exactly* what we DO have: extracting the tail from an iterator. Obviously there is no built-in function "list_tail" but we get the same effect by just using list() on an iterator after advancing past the first item. > My claim is that the two semantically identical formulations above do > not have isomorphic linguistic form. As far as I can make sense of your > words, you seem to be disputing this claim, but its a claim as much > worth debating as that the sun rises in the east. "Isomorphic linguistic form"? Are you referring to the idea from linguistics that there are analogies between the structure of phonic and semantic units? E.g. that the structures (phoneme, syllable, word) and (sememe, onomateme, sentence) are analogous. I don't see why this is relevant, or which units you are referring to -- the human-language description of what Python does, high-level Python language features, Python byte-code, or machine code. Not that it matters, but it is unlikely that any of those are isomorphic in the linguistic sense, and I am not making any general claim that they are. If not, I have no idea what you are getting at, except possibly trying to hide behind obfuscation. >> > Rather, we annotate >> > the identifier 'tail' with an attribute that unquestionably >> > destinates it to become a list*. It is no longer that 'tail' will >> > just take anything that pops out of the expression on the right hand >> > side; >> >> Of course it will. Python is a dynamically typed language. It doesn't >> suddenly develop static types to ensure that 'tail' becomes a list; >> 'tail' is bound to a list because that's what the assignment statement >> provides. > > How python accomplishes any of this under the hood is entirely > immaterial. The form is that of a compile-time type constraint, > regardless of whether the BDFL ever thought about it in these terms. Compile-time type constraints only have meaning in statically-typed languages. Otherwise, you are applying an analogy that simply doesn't fit, like claiming that the motion of paper airplanes and birds are equivalent just because in English we use the word "fly" to describe them both. The differences between what Python actually does and compile-time type- constraints are greater than the similarities. Similarities: (1) In both cases, 'tail' ends up as a list. Differences: (1) Compiler enforces the constraint that the identifier 'tail' is always associated with a list and will prevent any attempt to associate 'tail' with some value that is not a list. Python does nothing like this: the identifier 'tail' has no type restrictions at all, and can be used for any object. (2) Errors are detectable at compile-time with an error that halts compilation; Python detects errors at runtime with an exception that can be caught and by-passed without halting execution. (3) "Constraint" has the conventional meaning of a restriction, not a result; a type-constraint refers to a variable being prevented from being set to some other type, not that it merely becomes set to that type. For example, one doesn't refer to 'x = 1' as a type-constraint merely because x gets set to an int value; why do you insist that 'head, *tail = sequence' is a type-constraint merely because tail gets set to a list? >> > rather, >> > the semantics of what will go on at right hand side is coerced by the >> > constraint placed on 'tail'. >> >> But it isn't a constraint placed on 'tail'. It is a consequence of the >> definition of assignment in Python 3. 'tail' becomes bound to a list >> because that is what the assignment statement is defined to do in that >> circumstance, not because the identifier (symbol) 'tail' is constrained >> to only accept lists. 'tail' may not even exist before hand, so talking >> about constraints on 'tail' is an abuse of language, AS YOU AGREED >> ABOVE. > > 'tail' is (re)declared on the spot as a brand-new identifier (type > constraint included); whether it exists before has no significance > whatsoever, since python allows rebinding of identifiers. Given the following: tail = "beautiful red plumage" head, *tail = [1, 2, 3, 4] tail = 42 is it your option that all three instantiations of the *identifier* 'tail' (one in each line) are *different* identifiers? Otherwise, your statement about "brand new identifier" is simply wrong. If you allow that they are the same identifier with three different values (and three different types), then this completely destroys your argument that there is a constraint on the identifier 'tail'. First 'tail' is a string, then it is a list, then it is an int. If that is a type constraint (restriction) on 'tail', then constraint has no meaning. If you state that they are three different identifiers that just happen to be identical in every way that can be detected, then you are engaged in counting angels dancing on the head of pins. Given this idea that the middle 'tail' identifier is a different identifier from the other two, then I might accept that there *could* be a type-constraint on middle 'tail', at least in some implementation of Python that hasn't yet been created. But what a pointless argument to make. >> > *(I call that a 'type constraint', because that is what it literally >> > is; >> >> No. It is literally a name binding of a dynamically typed, >> unconstrained name to an object which happens to be a list. > > Let me take a step back and reflect on the form of the argument we are > having. I claim the object in front of us is a 'cube'. You deny this > claim, by countering that it is 'just a particular configuration of > atoms'. No no no, you have utterly misunderstood my argument. I'm not calling it a configuration of atoms. I'm calling it a pyramid, and pointing out that no matter how many times you state it is a cube, it actually has FOUR sides, not six, and NONE of the sides are square, so it can't possibly be a cube. > 'look at the definition!', you say; 'its just a list of coordinates, no > mention of cubes whatsoever.'. > > 'Look at the definition of a cube', I counter. 'This particular list of > coordinates happens to fit the definition, whether the BDFT intended to > or not'. But you are wrong. It does not fit the definition of a type-constraint, except in the vacuous sense where you declare that there is some invisible distinction between the identifier 'tail' in one statement and the identical identifier 'tail' in another statement. > You are correct, in the sense that I do not disagree that it is a > particular configuration of atoms. But if you had a better understanding > of cubes, youd realize it meets the definition; I might not share your deep and expert understanding of cubes, but I understand what "type-constraint" means, and I know what Python does in iterator unpacking, and I know that there is no sensible definition of type-constraint that applies to iterator unpacking in Python. > the fact that people > might not make a habit of pausing at this fact (there is generally > speaking not much of a point in doing so in this case), does not > diminish the validity of this perspective. But again, if this > perspective does not offer you anything useful, feel free not to share > in it. Quite frankly, I should just let you witter on about type-constraints, because (again, in my opinion) it destroys your credibility and will reduce even further the already minuscule chance that your proposal will be accepted. Since I'm against the idea, I should encourage you to describe Python's behaviour in terms that will all but guarantee others will dismiss you as knowing little or nothing about Python. [...] > Whatever the virtues of your critique of my proposal, you might end up > wasting less time doing so by reading a book or two on compiler theory. Perhaps you should spend more time considering the reality of how Python works and less time trying to force the triangular peg of Python concepts into the square hole of your ideas about theoretical compilers. -- Steven From bahamutzero8825 at gmail.com Tue Dec 27 17:57:41 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 27 Dec 2011 16:57:41 -0600 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <4EFA4D65.2060400@gmail.com> On 12/27/2011 4:04 PM, K Richard Pixley wrote: > You still need to match versions of PyDev to versions of Eclipse to > versions of operating system to versions of other eclipse plugins. I > spent a few days trying to get it together once and came to the > conclusion that it was a much bigger effort than I was willing to commit to. This is more of a package management issue than an Eclipse one. I had no problems getting the very latest stable versions of everything on Windows. I'm pretty sure most schools will be using Windows for workstations anyway. In any case, if Eclipse is impractical for whatever reason, obviously it would be wise to try something else. >>> You could create your own distribution of eclipse, but >>> then you have that "only useful for python" problem again. >> AFAIK, Eclipse should always be good for Java unless you do some serious >> hacking. > > Depends on which versions of eclipse, java, os, other plugins, etc. How so? AFAIK, Eclipse is almost (if not completely) unusable without Java. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From ian.g.kelly at gmail.com Tue Dec 27 17:58:58 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 15:58:58 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On Tue, Dec 27, 2011 at 2:35 PM, W. eWatson wrote: > I replaced numpy and matplotlib, and added scipy. I still get errors, but > perhaps because the install order is now wrong. It was > numpy > matplotlib > > Does anyone know the right order? The order (numpy, scipy, matplotlib) reflects the dependencies, but since the installers are just fancied-up self-extracting zips, I don't think it should matter. > OK, I installed it. Is there an easy way to go to track down this dll. > There's a Help, but I can't print it from the PC I have it on. It should be reasonably self-explanatory, I think. Just start Dependency Walker, open up the _path.pyd file, and it will show you the full dependency tree. Below that is a list of all the modules, and any missing dependencies should helpfully show up right at the top of the list. Note that missing delay-load dependencies (those with an hourglass icon) are not necessarily problems. Since the error occurs at load-time, you're looking for a missing module that would be loaded immediately. From rozelak at volny.cz Tue Dec 27 17:59:36 2011 From: rozelak at volny.cz (rozelak at volny.cz) Date: Tue, 27 Dec 2011 23:59:36 +0100 (CET) Subject: No subject Message-ID: Hallo, I have kind of special question when extening python with C++ implemented modules. I try to implement a class, behaving also like an array. And I need to implement slice-getters. I implemented PySequenceMethods.sq_slice to get "simple" slices like: myobj[x:y] It works perfectly, without problems. But now I have a problem when need to access the array with "special" slices, like: myobj[x:y:step] myobj[::-1] # to revert the array I would like to ask which method must be implemented to get this "special" slice getters. The "simple" slice PySequenceMethods.sq_slice getter accepts only two indexes - from, to, but not a step (which would solve the issue). Thank you very much for your ansver, best regards Dan T. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rozelak at volny.cz Tue Dec 27 18:02:16 2011 From: rozelak at volny.cz (rozelak at volny.cz) Date: Wed, 28 Dec 2011 00:02:16 +0100 (CET) Subject: =?iso-8859-2?Q?Slices_when_extending_python_with_C=2B=2B_?= Message-ID: Hallo, I have kind of special question when extening python with C++ implemented modules. I try to implement a class, behaving also like an array. And I need to implement slice-getters. I implemented PySequenceMethods.sq_slice to get "simple" slices like: myobj[x:y] It works perfectly, without problems. But now I have a problem when need to access the array with "special" slices, like: myobj[x:y:step] myobj[::-1] # to revert the array I would like to ask which method must be implemented to get this "special" slice getters. The "simple" slice PySequenceMethods.sq_slice getter accepts only two indexes - from, to, but not a step (which would solve the issue). Thank you very much for your ansver, best regards Dan T. -- Tradi?n? i modern? adventn? a novoro?n? zvyky, sv?te?n? j?dlo a pit?, v?zdoba a d?rky... - ?t?te v?no?n? a silvestrovsk? speci?l port?lu VOLN?.cz na http://web.volny.cz/data/click.php?id=1301 -------------- next part -------------- An HTML attachment was scrubbed... URL: From rozelak at volny.cz Tue Dec 27 18:03:08 2011 From: rozelak at volny.cz (rozelak at volny.cz) Date: Wed, 28 Dec 2011 00:03:08 +0100 (CET) Subject: =?iso-8859-2?Q?Slices_when_extending_python_with_C=2B=2B_?= Message-ID: <65f578abac07f7ad0d107c0f6bdcf951@mail1.volny.cz> Hallo, I have kind of special question when extening python with C++ implemented modules. I try to implement a class, behaving also like an array. And I need to implement slice-getters. I implemented PySequenceMethods.sq_slice to get "simple" slices like: myobj[x:y] It works perfectly, without problems. But now I have a problem when need to access the array with "special" slices, like: myobj[x:y:step] myobj[::-1] # to revert the array I would like to ask which method must be implemented to get this "special" slice getters. The "simple" slice PySequenceMethods.sq_slice getter accepts only two indexes - from, to, but not a step (which would solve the issue). Thank you very much for your ansver, best regards Dan T. -- Tradi?n? i modern? adventn? a novoro?n? zvyky, sv?te?n? j?dlo a pit?, v?zdoba a d?rky... - ?t?te v?no?n? a silvestrovsk? speci?l port?lu VOLN?.cz na http://web.volny.cz/data/click.php?id=1301 From steve+comp.lang.python at pearwood.info Tue Dec 27 18:07:23 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Dec 2011 23:07:23 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> Message-ID: <4efa4fab$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 13:51:50 -0800, Eelco wrote: [...] >> If your point is that parens are used more often than >> packing/unpacking, that's almost certainly true, since function calls >> (including method invocations) are so prevalent in pretty much any >> code. But what does that prove? > > That proves the original point of contention: that the below* is > suboptimal language design, Well duh. I was mocking the idea that the meaning of * is context-dependent is a bad thing by pointing out that we accept context-dependent meaning for round brackets () without any difficulties. Of course it is "suboptimal language design" -- it couldn't fail to be. Context-dependency is not necessarily a bad thing. > not because terseness always trumps > verbosity, but because commonly-used constructs (such as parenthesis or > round brackets or whatever you wish to call them) Parentheses are not a construct. They are symbols (punctuation marks) which are applied to at least three different constructs: grouping, function calls, class inheritance lists. > are more deserving of > the limited space in both the ascii table and your reflexive memory, > than uncommonly used ones. Right. And since sequence packing and unpacking is a common idiom, it deserves to be given punctuation. That's my opinion. -- Steven From steve+comp.lang.python at pearwood.info Tue Dec 27 18:10:42 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Dec 2011 23:10:42 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 07:47:20 -0800, Eelco wrote: > Explicit and implicit are not well-defined terms, We can at least agree on that. > but I would say that > at the moment the signal is implicit, in the sense that one cannot see > what is going on by considering the rhs in isolation. That is a ridiculous argument. If you ignore half the statement, of course you can't tell what is going on. > Normally in > python, an assignment just binds the rhs to the identifiers on the lhs, > but in case of collection (un)packing, this rule that holds almost all > of the time is broken, and the assignment statement implies a far more > complicated construct, with a far more subtle meaning, and non-constant > time complexity. Python assignment in general is not constant time. If the left hand side is a single identifier (a name, a dotted attribute, a subscription or slice, etc.) then the assignment is arguably constant time (hand-waving away complications like attribute access, hash table collisions, descriptors, etc.); but if the left hand side is a list of targets, e.g.: a, b, c = sequence then the assignment is O(N). Python has to walk the sequence (actually, any iterator) and assign each of N items to N identifiers, hence O(N) not O(1). The trivial case of a single value on the left hand side: x = 1 is just the degenerate case for N=1. > Thats not a terrible thing, but a little extra explicitness there would > not hurt, I think it will. It makes the assignment more verbose to no benefit. > and like I argued many times before, it is a nice unification > with the situation where the unpacking can not be implicit, like inside > a function call rather than assignment. That's one opinion; but there's no need for any "extra explicitness" since the definition of assignment in Python already explicitly includes iterable unpacking. How much explicitness do you need? >> ? ? n = len(::sequence) > > Now you are just discrediting yourself in terms of having any idea what > you are talking about. ::sequence is redundant and unnecessary whether it is inside a function call to len or on the right hand side of an assignment. In both cases, it adds nothing to the code except noise. Your proposal is surreal. You started off this conversation arguing that the existing idiom for extended iterator unpacking, e.g. head, *tail = sequence is too hard to understand because it uses punctuation, and have ended up recommending that we replace it with: head, tail:: = ::sequence instead (in the generic case where you don't want to use a different type for tail). Your original use-case, where you want to change the type of tail from a list to something else, is simply solved by one extra line of code: head, *tail = sequence tail = tuple(tail) You have written thousands of words to save one line in an idiom which you claim is very uncommon. Perhaps your understanding of "pythonic" is not as good as you imagine. -- Steven From dihedral88888 at googlemail.com Tue Dec 27 18:45:06 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 27 Dec 2011 15:45:06 -0800 (PST) Subject: RSA and Cryptography in PYTHON In-Reply-To: <31261167.63.1324843108857.JavaMail.geo-discussion-forums@prdv33> References: <31261167.63.1324843108857.JavaMail.geo-discussion-forums@prdv33> Message-ID: <24097307.586.1325029506304.JavaMail.geo-discussion-forums@prmw6> 88888 Dihedral? 2011?12?26????UTC+8??3?58?28???? > Long integer is really excellent in Python. > > Encoding RSA 2048bits in a simple and elegant way in Python > is almost trivial. > > How about the nontrivial decoding part ? I am getting lousy in the news group in my writing? I mean the non-trivial decoding of the key decomposition. Of course RSA is symmetric in encoding and decoding of data. From ian.g.kelly at gmail.com Tue Dec 27 18:47:25 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 16:47:25 -0700 Subject: Slices when extending python with C++ In-Reply-To: <65f578abac07f7ad0d107c0f6bdcf951@mail1.volny.cz> References: <65f578abac07f7ad0d107c0f6bdcf951@mail1.volny.cz> Message-ID: 2011/12/27 : > Hallo, > I have kind of special question when extening python with C++ > implemented modules. > > I try to implement a class, behaving also like an array. And I need > to implement slice-getters. I implemented PySequenceMethods.sq_slice > to get "simple" slices like: > > myobj[x:y] > > It works perfectly, without problems. But now I have a problem when > need to access the array with "special" slices, like: > > myobj[x:y:step] > myobj[::-1] ?# to revert the array > > I would like to ask which method must be implemented to get this > "special" slice getters. The "simple" slice > PySequenceMethods.sq_slice getter accepts only two indexes - from, > to, but not a step (which would solve the issue). I'm not entirely certain about the C API, but in Python the __getslice__ and __setslice__ methods do not support extended slicing and have been deprecated and removed in Python 3. The practice instead is to implement __getitem__ and __setitem__ and check the type of the key object passed in. For slicing, the object passed in will be a slice object, which contains start, stop, and step attributes. For example: class MySequence(object): def __getitem__(self, key): if isinstance(key, slice): # return a subsequence using key.start, key.stop, key.step else: # return a single item indexed by key HTH, Ian From rantingrickjohnson at gmail.com Tue Dec 27 19:57:04 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 16:57:04 -0800 (PST) Subject: Possible bug in string handling (with kludgy work-around) References: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 27, 3:38?pm, Terry Reedy wrote: > On 12/27/2011 1:04 PM, Rick Johnson wrote: > > > But this brings up a very important topic. Why do we even need triple > > quote string literals to span multiple lines? Good question, and one i > > have never really mused on until now. > > I have, and the reason I thought of is that people, including me, too > ofter forget or accidentally fail to properly close a string literal, Yes, agreed. > Color coding editors make it easier to catch such errors, but they were > less common in 1991. I would say the need for triple quote strings has passed long ago. Like you say, since color lexers are ubiquitous now we don't need them. > And there is still uncolored interactive mode. I don't see interactive command line programming as a problem. I mean, who drops into a cmd line and starts writing paragraphs of string literals? Typically, one would just make a few one-liner calls here or there. Also, un-terminated string literal errors can be very aggravating. Not because they are difficult to fix, no, but because they are difficult to find! -- and sending me an error message like... "Exception: Un-terminated string literal meets EOF! line: 50,466,638" ... is about as helpful as a bullet in my head! If the interpreter finds itself at EOF BEFORE a string closes, don't you think it would be more helpful to include the currently "opened" strings START POSITION also? Heck, it would be wonderful to only have the start position since the likely-hood of a string ending at EOF is astronomical! As an intelligent lad must know, the odds that the distance from any given string's start position to it's end position is more likely to be shorter than the distance from the string's beginning to the freaking EOF! Ruby and Python are both guilty of this atrocity. From rantingrickjohnson at gmail.com Tue Dec 27 20:11:49 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 17:11:49 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <254cfbe0-19db-4d56-87b2-08f9da19aa61@q17g2000yqh.googlegroups.com> On Dec 27, 5:10?pm, Steven D'Aprano wrote: > On Sun, 25 Dec 2011 07:47:20 -0800, Eelco wrote: > Your original use-case, where you want to change the type of tail from a > list to something else, is simply solved by one extra line of code: > > head, *tail = sequence > tail = tuple(tail) i wonder if we could make this proposal a bit more "Pythonic"? Hmm... head, tuple(tail) = sequence ...YEP! From wolftracks at invalid.com Tue Dec 27 20:21:26 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 17:21:26 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 2:58 PM, Ian Kelly wrote: > On Tue, Dec 27, 2011 at 2:35 PM, W. eWatson wrote: >> I replaced numpy and matplotlib, and added scipy. I still get errors, but >> perhaps because the install order is now wrong. It was >> numpy >> matplotlib >> >> Does anyone know the right order? > > The order (numpy, scipy, matplotlib) reflects the dependencies, but > since the installers are just fancied-up self-extracting zips, I don't > think it should matter. > >> OK, I installed it. Is there an easy way to go to track down this dll. >> There's a Help, but I can't print it from the PC I have it on. > > It should be reasonably self-explanatory, I think. Just start > Dependency Walker, open up the _path.pyd file, and it will show you > the full dependency tree. Below that is a list of all the modules, > and any missing dependencies should helpfully show up right at the top > of the list. > > Note that missing delay-load dependencies (those with an hourglass > icon) are not necessarily problems. Since the error occurs at > load-time, you're looking for a missing module that would be loaded > immediately. I was too timid. When I saw File->Open, I thought I was going to have to open a file, thinking what file could it be? Well, it found several problems. These DLLs MSVCP1 EFSADU MSJAVA. There may be 100 file. Issues found: required dependency not found delay-dependency not found one nodule unresolved ... missing delay-dependency module Perhaps the next step is to download a fresh version of matplotlib? From dihedral88888 at googlemail.com Tue Dec 27 20:21:55 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 27 Dec 2011 17:21:55 -0800 (PST) Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <6529469.1070.1325035315877.JavaMail.geo-discussion-forums@prix23> There are Dr.Python, Pycrust and Notepadplus to support writing python programs. IDLE is OK, but if a program failed inside IDLE, then I might have to kill the old IDLE and restart IDLE again. From dihedral88888 at googlemail.com Tue Dec 27 20:21:55 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 27 Dec 2011 17:21:55 -0800 (PST) Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <6529469.1070.1325035315877.JavaMail.geo-discussion-forums@prix23> There are Dr.Python, Pycrust and Notepadplus to support writing python programs. IDLE is OK, but if a program failed inside IDLE, then I might have to kill the old IDLE and restart IDLE again. From rantingrickjohnson at gmail.com Tue Dec 27 20:56:33 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 17:56:33 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: On Dec 27, 3:44?pm, Eelco wrote: > Despite the fact that you mis-attributed that quote to me, im going to > be a little bit offended in the name of its actual author anyway. > Thats a lot of words to waste on your linguistic preferences. > Personally, I reserve the right to botch my non-native languages as > much as I please. I hope you're just joking a bit because i have little respect for those who refuse to better themselves. If you are learning English as a second language then you have a legitimacy excuse, but at some point that excuse just becomes a lie. In any case, i apologize for mis- quoting you. > > I always though "run" was a perfect verb for "running" code... but who > > knows :) > > Im assuming the audience is familiar with an ipod, but not an IDE, or > programming in general. To their eyes, it looks like a 'play' button; I would be very careful about introducing new words, or borrowing words, to replace tried and true technical terms that have existed for along time. As a matter of fact, the BDFL make the point better than i ever could: GvR speaking about ABC's design: """ The ABC group assumed that the users had no prior computer experience (or were willing to forget it). Thus, alternative terminology was introduced that was considered more "newbie-friendly" than standard programming terms. For example: procedures were called "how-tos" and variables "locations". """ From rantingrickjohnson at gmail.com Tue Dec 27 21:01:22 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 18:01:22 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <6529469.1070.1325035315877.JavaMail.geo-discussion-forums@prix23> Message-ID: <4a88c970-ade9-4221-85e2-2feee8f6fec3@24g2000yqi.googlegroups.com> On Dec 27, 7:21?pm, 88888 Dihedral wrote: > There are Dr.Python, Pycrust and ?Notepadplus to support writing python programs. I really like Pycrust. It's written in Python, it's code base is structured in a professional manner (IDLE you should be jealous!), and it works well. However, it is dependent on WxPython; so that's right out. > IDLE is OK, but if a program failed inside IDLE, then I ?might have > to kill the old IDLE and restart IDLE again. Yes, i've had to kill many frozen instances of IDLE. It gets aggravating. From python.list at tim.thechases.com Tue Dec 27 21:21:49 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 27 Dec 2011 20:21:49 -0600 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: <4EFA7D3D.2090908@tim.thechases.com> On 12/27/11 19:56, Rick Johnson wrote: > On Dec 27, 3:44 pm, Eelco > wrote: > >> Despite the fact that you mis-attributed that quote to me, >> im going to be a little bit offended in the name of its >> actual author anyway. Thats a lot of words to waste on your >> linguistic preferences. Personally, I reserve the right to >> botch my non-native languages as much as I please. > > I hope you're just joking a bit because i have little respect > for those who refuse to better themselves. If you are learning > English as a second language then you have a legitimacy > excuse, but at some point that excuse just becomes a lie. I'm glad you're open to learning more about English as "used to" is perfectly acceptable according to the World English Dictionary[1] """ 2. (takes an infinitive or implied infinitive) used as an auxiliary to express habitual or accustomed actions, states, etc, taking place in the past but not continuing into the present: I don't drink these days, but I used to; I used to fish here every day """ May you be found better for learning and come to give others the benefit of the doubt. -tkc [1] http://dictionary.reference.com/browse/used%20to From ian.g.kelly at gmail.com Tue Dec 27 21:27:49 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 19:27:49 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On Tue, Dec 27, 2011 at 6:21 PM, W. eWatson wrote: > Well, it found several problems. These DLLs > MSVCP1 > EFSADU > MSJAVA. I'm guessing MSVCP1 is a typo for MSVCP71? If that is missing then that is probably the culprit. That DLL is the C runtime library. It is supposed to be shipped with applications that need it, but it is so ubiquitous that it is often assumed to be present or forgotten. You can download it from http://www.dll-files.com/dllindex/dll-files.shtml?msvcp71 (or just find it on another Windows XP PC) and copy it into C:\Windows\System32. Don't forget to run regsvr32 to register it. HTH, Ian From rantingrickjohnson at gmail.com Tue Dec 27 21:42:05 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 18:42:05 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: On Dec 27, 8:21?pm, Tim Chase wrote: > I'm glad you're open to learning more about English as "used to" > is perfectly acceptable according to the World English Dictionary[1] > [...] > May you be found better for learning and come to give others the > benefit of the doubt. I don't care what ANY dictionary says. Much less a "world" dictionary. I don't validate or invalidate a word based on some phony baloney group of pseudo intellectuals who decided one to day that writing a dictionary "might be cool". I am against these words and phrases because we already have words that work just fine. Why rock the boat? Why would you use a word like "hard" (which describes the physical properties of a tangible object), to describe how "difficult" a task may be? If you insist on this lunacy, then why not use "soft" to describe how easy a task may be? Seems ridiculous now, huh? Garbage Verbiage Translator: Used to -> previously|before Supposed to -> required|expected Use to -> accustomed|acquainted Right (OOC) -> Correct Hard (OOC) -> Difficult Pretty (OOC) -> very This is group has the most dumbest smart people i have ever met! From prabhadigi2 at gmail.com Tue Dec 27 21:47:48 2011 From: prabhadigi2 at gmail.com (prabhakaran k) Date: Tue, 27 Dec 2011 18:47:48 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From wolftracks at invalid.com Tue Dec 27 22:31:08 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 19:31:08 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 6:27 PM, Ian Kelly wrote: > On Tue, Dec 27, 2011 at 6:21 PM, W. eWatson wrote: >> Well, it found several problems. These DLLs >> MSVCP1 >> EFSADU >> MSJAVA. > > I'm guessing MSVCP1 is a typo for MSVCP71? If that is missing then > that is probably the culprit. That DLL is the C runtime library. It > is supposed to be shipped with applications that need it, but it is so > ubiquitous that it is often assumed to be present or forgotten. You > can download it from > http://www.dll-files.com/dllindex/dll-files.shtml?msvcp71 (or just > find it on another Windows XP PC) and copy it into > C:\Windows\System32. Don't forget to run regsvr32 to register it. > > HTH, > Ian You are very likely right about the spelling. I wrote it down, and carried it to this PC. Sometimes I can't read my own writing. I've never used regsvr32, but I would guess it's entered into the Run (simple dialog for MS tools like regedit) selection off the Start menu. From d at davea.name Tue Dec 27 22:47:54 2011 From: d at davea.name (Dave Angel) Date: Tue, 27 Dec 2011 22:47:54 -0500 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: <4EFA916A.7000105@davea.name> On 12/27/2011 10:31 PM, W. eWatson wrote: > On 12/27/2011 6:27 PM, Ian Kelly wrote: >> >> http://www.dll-files.com/dllindex/dll-files.shtml?msvcp71 (or just >> find it on another Windows XP PC) and copy it into >> C:\Windows\System32. Don't forget to run regsvr32 to register it. >> >> HTH, >> Ian > You are very likely right about the spelling. I wrote it down, and > carried it to this PC. Sometimes I can't read my own writing. > > I've never used regsvr32, but I would guess it's entered into the Run > (simple dialog for MS tools like regedit) selection off the Start menu. Of course not. You run it from the same command window (aka DOS box, or cmd window) you've been doing all your other testing from. And like all good commands, it has built-in help so you can see what the argument list looks like. -- DaveA From rantingrickjohnson at gmail.com Tue Dec 27 22:49:12 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 19:49:12 -0800 (PST) Subject: Py-dea: Streamline string literals now! Message-ID: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Hello folks, In a recent thread i stumbled upon an epiphany of sorts concerning Python string literals, with implications that trickle down to all forms of string literals used in general programming, since, for the most part, the syntax is virtually the same! For all our lives we have been excepting a standard for string literals that is, quite literally, overkill. It seems all along the syntax has been flawed, however, we have been totally unaware... until now! *[A brief history lesson]* Python offers two main styles of delimiting string literals: * single "leading and trailing" double, or single quote chars. * triple "leading and trailing" double, or single quote chars. The single leading group is intended for one line literals whereas the triple is intended for multi-line literals. Now, in my initial days of Python-ing, i thought this was a great system. Unlike most languages that require a programmer to concatenate strings over and over again, python "seemed" to solve the multi-line issue -- but there is a greater issue! *[Current Issues]* The fact is...even with the multi-line issue solved, we still have two forms of literal delimiters that encompass two characters resulting in *four* possible legal combinations of the exact same string! I don't know about you guys, but i am not a big fan of Tim Towtdi. But the problems just keep adding up. There is also the issue of escape sequences; which i have always found to be atrociousness noisy. Everyone who has had to deal with double escapes in regexps raise your hand. The frowns are deafening. The simple fact is: The current syntax for string literals is not only deficient, it is bloated, noisy, and confusing! *[Solution]* I believe that with the ubiquitous-ness of syntax highlight, string literals only need one delimiter. In the old days (before syntax highlight was invented) i could understand how a programmer "might" miss a single (or even a triple!) closing delimiter; but those days died with the king. My proposal is to introduce a single delimiter for string literals. A new string literal that is just as good at spanning single lines as it is spanning multiple lines. A new literal that uses widely known markup tag syntax instead of cryptic and noisy escape sequences. And finally, a *literal* that is worthy of the 21st century. Thank You. From rosuav at gmail.com Tue Dec 27 22:54:29 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Dec 2011 14:54:29 +1100 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: On Wed, Dec 28, 2011 at 1:42 PM, Rick Johnson wrote: > I don't care what ANY dictionary says. Much less a "world" dictionary. > I don't validate or invalidate a word based on some phony baloney > group of pseudo intellectuals who decided one to day that writing a > dictionary "might be cool". Finally we know who Ranting Rick is the alt of. He's Humpty Dumpty! http://en.wikisource.org/wiki/Through_the_Looking-Glass,_and_What_Alice_Found_There/Chapter_VI ChrisA From rosuav at gmail.com Tue Dec 27 23:06:37 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Dec 2011 15:06:37 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Dec 28, 2011 at 10:10 AM, Steven D'Aprano wrote: > Your original use-case, where you want to change the type of tail from a > list to something else, is simply solved by one extra line of code: > > head, *tail = sequence > tail = tuple(tail) That achieves the goal of having tail as a different type, but it does have the additional cost of constructing and then discarding a temporary list. I know this is contrived, but suppose you have a huge set/frozenset using tuples as the keys, and one of your operations is to shorten all keys by removing their first elements. Current Python roughly doubles the cost of this operation, since you can't choose what type the tail is made into. But if that's what you're trying to do, it's probably best to slice instead of unpacking. Fortunately, the Zen of Python "one obvious way to do it" doesn't stop there being other ways that work too. ChrisA From rosuav at gmail.com Tue Dec 27 23:17:30 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Dec 2011 15:17:30 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: On Wed, Dec 28, 2011 at 2:49 PM, Rick Johnson wrote: > My proposal is to introduce a single delimiter for string literals. A > new string literal that is just as good at spanning single lines as it > is spanning multiple lines. A new literal that uses widely known > markup tag syntax instead of cryptic and noisy escape sequences. And > finally, a *literal* that is worthy of the 21st century. So all you're doing is introducing a different form of escaping. You can already achieve this with regular expressions in several engines - declare a different delimiter and/or escape character - in order to dodge the issue of multiple escapes. Python already has raw strings for the same reason (although the rules governing raw strings are oddly complex in edge cases). Your proposal sounds like a good idea for a specific-situation config file, but a very bad idea for Python. If you want elaboration on that, ask me for my arguments on CSV vs /etc/passwd. Or search the web for the same topic, I'm sure many others have made the same points. ChrisA From steve+comp.lang.python at pearwood.info Tue Dec 27 23:20:26 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Dec 2011 04:20:26 GMT Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: <4efa990a$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Dec 2011 19:49:12 -0800, Rick Johnson wrote: > I believe that with the ubiquitous-ness of syntax highlight, string > literals only need one delimiter. In the old days (before syntax > highlight was invented) i could understand how a programmer "might" miss > a single (or even a triple!) closing delimiter; but those days died with > the king. http://en.wikipedia.org/wiki/List_of_current_monarchs Thank you Rick for another non-solution to a non-problem. How is that fork of Python going? You promised us you would revitalise the community with a new fork of Python that throws out all the accumulated cruft of the language. I'm sure that the silent majority who agree with you are hanging out for your first release. I know I am. -- Steven From no.email at nospam.invalid Tue Dec 27 23:27:11 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 27 Dec 2011 20:27:11 -0800 Subject: RSA and Cryptography in PYTHON References: <31261167.63.1324843108857.JavaMail.geo-discussion-forums@prdv33> <24097307.586.1325029506304.JavaMail.geo-discussion-forums@prmw6> Message-ID: <7xy5tx2ue8.fsf@ruckus.brouhaha.com> 88888 Dihedral writes: >> How about the nontrivial decoding part ? > I am getting lousy in the news group in my writing? > I mean the non-trivial decoding of the key decomposition. I still don't have the slightest idea what you are asking for. From rantingrickjohnson at gmail.com Tue Dec 27 23:36:35 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 20:36:35 -0800 (PST) Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: On Dec 27, 10:17?pm, Chris Angelico wrote: > On Wed, Dec 28, 2011 at 2:49 PM, Rick Johnson > > wrote: > > My proposal is to introduce a single delimiter for string literals. A > > new string literal that is just as good at spanning single lines as it > > is spanning multiple lines. A new literal that uses widely known > > markup tag syntax instead of cryptic and noisy escape sequences. And > > finally, a *literal* that is worthy of the 21st century. > > So all you're doing is introducing a different form of escaping. Well that is but one of the proposals. The others include reducing four combinations of string literals into one, and by default, spanning multiple lines with one syntax. > You > can already achieve this with regular expressions in several engines - > declare a different delimiter and/or escape character - in order to > dodge the issue of multiple escapes. I am specifically referring to the Python language and how the interpreter "interprets" string literals. Wait, you do share the same definition of "string literal" as i do, i hope? http://en.wikipedia.org/wiki/String_literal > Python already has raw strings > for the same reason (although the rules governing raw strings are > oddly complex in edge cases). Raw strings would be history with my proposal. No more need for raw strings. This is the string literal to rule all string literals! > Your proposal sounds like a good idea for a specific-situation config > file, but a very bad idea for Python. Can you give specific reasons why? From rantingrickjohnson at gmail.com Wed Dec 28 00:34:19 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 21:34:19 -0800 (PST) Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: On Dec 27, 9:49?pm, Rick Johnson wrote: > The fact is...even with the multi-line issue solved, we still have two > forms of literal delimiters that encompass two characters resulting in > *four* possible legal combinations of the exact same string! I don't > know about you guys, but i am not a big fan of Tim Towtdi. actually i was a bit hasty with that statment and underestimated the actual number of possiblities. 1) "this is a string" 2) 'this is a string' 3) r"this is a string" 4) r'this is a string' 5) '''this is a string''' 6) """this is a string""" 7) r'''this is a string''' 8) r"""this is a string""" A) "this is difficult to \"eyeball parse\"" B) """this is "overkill"""" C) "that's just plain \"nuts\"!" Now. If anyone can look at that mess and not admit it is a disaster, well then... I am also thinking that ANY quote char is a bad choice for string literal delimiters. Why? Well because it is often necessary to embed single or double quotes into a string literal. We need to use a delimiter that is not a current delimiter elsewhere in Python, and also, is a very rare char. I believe Mr Ewing found that perfect char in his "Multi-line uber raw string literals!" (Just scroll down a bit at this link)... http://www.cosc.canterbury.ac.nz/greg.ewing/python/ideas.html ...however, requiring a programmer to start EVERY line with a marker does not seem like fun to me. And just think what a nightmare it will be to modify copy/pasted data with line markers! Although it does solve the "indention" issue with doc-strings! I think just for foreign language compatibility reasons we should stick with one of either " or ' (or maybe both), but allowing [r]""" and [r]''' is just WAY too much! We need to trim this fat. From rustompmody at gmail.com Wed Dec 28 00:50:20 2011 From: rustompmody at gmail.com (rusi) Date: Tue, 27 Dec 2011 21:50:20 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <59bae356-99a9-4250-9b56-b3f8723d2a2b@r13g2000prr.googlegroups.com> On Dec 27, 11:26?pm, Andrew Berg wrote: > On 12/27/2011 11:59 AM, K Richard Pixley wrote:> You'd do better to encourage eclipse, but setting that up isn't > > trivial either. > > IIRC, all I had to do to set up PyDev was copy a URL to Eclipse's > "Install New Software" wizard, and have Eclipse download and install it. > Extra steps are needed if a different implementation of Python (e.g. > Jython) is needed, but other than that, the user only needs to specify a > couple options (e.g. Python grammar version) at project creation time. > This assumes that Python is already installed, but why wouldn't it be? There was a recent announcement about eclipse for python (pydev) http://www.mail-archive.com/python-list at python.org/msg320434.html It did not work for me -- got a backtrace. I responded with that. There was no further response. Just mentioning that at least in some cases it does not 'just work.' From angle6602 at gmail.com Wed Dec 28 01:01:30 2011 From: angle6602 at gmail.com (angle 9) Date: Tue, 27 Dec 2011 22:01:30 -0800 (PST) Subject: google form filling Message-ID: <98d92b5b-9f83-48f0-ade1-247f22c8201c@k22g2000prb.googlegroups.com> google form filling http://gsonlinejobs.yolasite.com/ From angle6602 at gmail.com Wed Dec 28 01:01:55 2011 From: angle6602 at gmail.com (angle 9) Date: Tue, 27 Dec 2011 22:01:55 -0800 (PST) Subject: google form filling Message-ID: google form filling http://gsonlinejobs.yolasite.com/ From steve+comp.lang.python at pearwood.info Wed Dec 28 01:25:35 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Dec 2011 06:25:35 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Dec 2011 15:06:37 +1100, Chris Angelico wrote: > On Wed, Dec 28, 2011 at 10:10 AM, Steven D'Aprano > wrote: >> Your original use-case, where you want to change the type of tail from >> a list to something else, is simply solved by one extra line of code: >> >> head, *tail = sequence >> tail = tuple(tail) > > That achieves the goal of having tail as a different type, but it does > have the additional cost of constructing and then discarding a temporary > list. I know this is contrived, but suppose you have a huge > set/frozenset using tuples as the keys, and one of your operations is to > shorten all keys by removing their first elements. Current Python > roughly doubles the cost of this operation, since you can't choose what > type the tail is made into. The First Rule of Program Optimization: - Don't do it. The Second Rule of Program Optimization (for experts only): - Don't do it yet. Building syntax to optimize imagined problems is rarely a good idea. The difference between 2 seconds processing your huge set and 4 seconds processing it is unlikely to be significant unless you have dozens of such huge sets and less than a minute to process them all. And your idea of "huge" is probably not that big... it makes me laugh when people ask how to optimize code "because my actual data has HUNDREDS of items!". Whoop-de-doo. Come back when you have a hundred million items, then I'll take your question seriously. (All references to "you" and "your" are generic, and not aimed at Chris personally. Stupid English language.) > But if that's what you're trying to do, it's probably best to slice > instead of unpacking. Assuming the iterable is a sequence. Fortunately, most iterable constructors accept iterators directly, so for the cost of an extra line (three instead of two), you can handle data structures as big as will fit into memory: # I want to keep both the old and the new set it = iter(huge_set_of_tuples) head = next(it) # actually an arbitrary item tail = set(x[1:] for x in it) # and everything else If you don't need both the old and the new: head = huge_set_of_tuples.pop() tail = set() while huge_set_of_tuples: tail.add(huge_set_of_tuples.pop()[1:]) assert huge_set_of_tuples == set([]) If you rely on language features, who knows how efficient the compiler will be? head, tail::tuple = ::sequence may create a temporary list before building the tuple anyway. And why not? That's what this *must* do: head, second, middle::tuple, second_from_last, last = ::iterator because tuples are immutable and can't be grown or shrunk, so why assume the language designers special cased the first form above? > Fortunately, the Zen of Python "one obvious way to > do it" doesn't stop there being other ways that work too. Exactly. It is astonishing how many people think that if there isn't a built-in language feature, with special syntax, to do something, there's a problem that needs to be solved. -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 28 01:58:37 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Dec 2011 06:58:37 GMT Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Dec 2011 21:34:19 -0800, Rick Johnson wrote: > Now. If anyone can look at that mess and not admit it is a disaster, > well then... It isn't a disaster. A disaster is when people die, lose their houses, get tossed out into the street to starve, radioactive contamination everywhere, floods, fire, the first born of every family being struck down suddenly, that sort of thing. Not having eight ways to write string literals. That's merely a convenience. > I am also thinking that ANY quote char is a bad choice for string > literal delimiters. Why? Well because it is often necessary to embed > single or double quotes into a string literal. We need to use a > delimiter that is not a current delimiter elsewhere in Python, and also, > is a very rare char. I believe Mr Ewing found that perfect char in his > "Multi-line uber raw string literals!" (Just scroll down a bit at this > link)... > > http://www.cosc.canterbury.ac.nz/greg.ewing/python/ideas.html Not surprisingly, you haven't thought this through. I'm sure Greg Ewing has, which is why he hasn't proposed *replacing* string delimiters with his multi-line format. That's why he proposed it as a *statement* and not string-builder syntax. Without an end-delimiter, how do you embed string literals in expressions? -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 28 01:59:15 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Dec 2011 06:59:15 GMT Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Dec 2011 18:42:05 -0800, Rick Johnson wrote: > On Dec 27, 8:21?pm, Tim Chase wrote: > >> I'm glad you're open to learning more about English as "used to" is >> perfectly acceptable according to the World English Dictionary[1] [...] >> May you be found better for learning and come to give others the >> benefit of the doubt. > > I don't care what ANY dictionary says. Much less a "world" dictionary. I > don't validate or invalidate a word based on some phony baloney group of > pseudo intellectuals who decided one to day that writing a dictionary > "might be cool". I am against these words and phrases because we already > have words that work just fine. Why rock the boat? Why do you say "rock" when the word "shake" is just as good? Why do you say "boat" when we already have "ship"? Why do you say "pseudo intellectuals" when you could say "fake intellectuals"? Why do I waste my time reading your pretentious self-important nonsense? [...] > This is group has the most dumbest smart people i have ever met! Considering I keep expecting you to stop trolling, I admit this applies to me. -- Steven From rustompmody at gmail.com Wed Dec 28 02:05:36 2011 From: rustompmody at gmail.com (rusi) Date: Tue, 27 Dec 2011 23:05:36 -0800 (PST) Subject: Regular expressions References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> <4be34afe-4291-414b-9212-498074400e39@v24g2000prn.googlegroups.com> Message-ID: <675670e4-a954-46f3-a90d-cd4c5934ffec@h7g2000prn.googlegroups.com> On Dec 27, 10:01?am, Fredrik Tolf wrote: > On Mon, 26 Dec 2011, mauricel... at acm.org wrote: > > I've tried > > > re.sub('@\S\s[1-9]:[A-N]:[0-9]', '@\S\s', '@HWI-ST115:568:B08LLABXX: > > 1:1105:6465:151103 1:N:0:') > > > but it does not seems to work. > > Indeed, for several reasons. First of all, your backslash sequences are > interpreted by Python as string escapes. You'll need to write either "\\S" > or r"\S" (the r, for raw, turns off backslash escapes). > > Second, when you use only "\S", that matches a single non-space character, > not several; you'll need to quantify them. "\S*" will match zero or more, > "\S+" will match one or more, "\S?" will match zero or one, and there are > a couple of other possibilities as well (see the manual for details). In > this case, you probably want to use "+" for most of those. > > Third, you're not marking the groups that you want to use in the > replacement. Since you want to retain the entire string before the space, > and the numeric element, you'll want to enclose them in parentheses to > mark them as groups. > > Fourth, your replacement string is entirely wacky. You don't use sequences > such as "\S" and "\s" to refer back to groups in the original text, but > numbered references, to refer back to parenthesized groups in the order > they appear in the regex. In accordance what you seemed to want, you > should probably use "@\1/\2" in your case ("\1" refers back to the first > parentesized group, which you be the first "\S+" part, and "\2" to the > second group, which should be the "[1-9]+" part; the at-mark and slash > are inserted as they are into the result string). > > Fifth, you'll probably want to match the last colon as well, in order not > to retain it into the result string. > > All in all, you will probably want to use something like this to correct > that regex: > > re.sub(r'@(\S+)\s([1-9]+):[A-N]+:[0-9]+:', r'@\1/\2', > ? ? ? ? '@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N:0:') > > Also, you may be interested to know that you can use "\d" instead of > "[0-9]". > > -- > > Fredrik Tolf For practical 'get-the-hands-dirty' experience look at python-specific: http://kodos.sourceforge.net/ Online: http://gskinner.com/RegExr/ emacs-specific: re-builder and regex-tool http://bc.tech.coop/blog/071103.html From rosuav at gmail.com Wed Dec 28 02:08:37 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Dec 2011 18:08:37 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Dec 28, 2011 at 5:25 PM, Steven D'Aprano wrote: > On Wed, 28 Dec 2011 15:06:37 +1100, Chris Angelico wrote: > >> ... suppose you have a huge >> set/frozenset using tuples as the keys, and one of your operations is to >> shorten all keys by removing their first elements. Current Python >> roughly doubles the cost of this operation, since you can't choose what >> type the tail is made into. > > The First Rule of Program Optimization: > - Don't do it. > > The Second Rule of Program Optimization (for experts only): > - Don't do it yet. > > > Building syntax to optimize imagined problems is rarely a good idea. The > difference between 2 seconds processing your huge set and 4 seconds > processing it is unlikely to be significant unless you have dozens of > such huge sets and less than a minute to process them all. > > And your idea of "huge" is probably not that big... it makes me laugh > when people ask how to optimize code "because my actual data has HUNDREDS > of items!". Whoop-de-doo. Come back when you have a hundred million > items, then I'll take your question seriously. > > (All references to "you" and "your" are generic, and not aimed at Chris > personally. Stupid English language.) And what you're seeing there is the _best possible_ situation I could think of, the strongest possible justification for new syntax. Granted, that may say more about me and my imagination than about the problem, but the challenge is open: Come up with something that actually needs this. ChrisA From panupatc at gmail.com Wed Dec 28 02:45:55 2011 From: panupatc at gmail.com (Panupat Chongstitwattana) Date: Wed, 28 Dec 2011 14:45:55 +0700 Subject: user login session for stand alone PyQt Message-ID: For my current project I'm making PyQt GUI to be used inside Maya. One of my requirement is that users need to first login with username and password. After authenticating the account, I'm not sure how I can store the session. I tried Cookie.Simplecookie but I guess it doesn't wok because the GUI is not running through HTTP. Any suggestion appreciate. From rosuav at gmail.com Wed Dec 28 02:47:44 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Dec 2011 18:47:44 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: On Wed, Dec 28, 2011 at 4:34 PM, Rick Johnson wrote: > I am also thinking that ANY quote char is a bad choice for string > literal delimiters. Why? Well because it is often necessary to embed > single or double quotes into a string literal. Postgres allows dollar-delimited strings, which get around this issue somewhat. http://www.postgresql.org/docs/9.1/static/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING But for most strings, it simply makes sense to use a quote character. Most strings don't need both ' and " in them. You cannot pick one character to be your ultimate delimiter, because there will always be occasion to embed it. (If nothing else, what happens when you emit code?) You want the delimiter to be easily typed and recognized, and that guarantees that it'll be something that's going to want to be emitted. It's necessary to have multiple options, or escaping. ChrisA From rustompmody at gmail.com Wed Dec 28 02:49:18 2011 From: rustompmody at gmail.com (rusi) Date: Tue, 27 Dec 2011 23:49:18 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 28, 11:59?am, Steven D'Aprano wrote: > Why do I waste my time reading your pretentious self-important nonsense? > > > This is group has the most dumbest smart people i have ever met! > > Considering I keep expecting you to stop trolling, I admit this applies > to me. http://www.goodreads.com/quotes/show/55234 From bouleetbil at gmail.com Wed Dec 28 02:58:59 2011 From: bouleetbil at gmail.com (bouleetbil (gaetan)) Date: Wed, 28 Dec 2011 08:58:59 +0100 Subject: ctypes argument by reference Message-ID: <20111228085859.1e368ef6@gmail.com> Hi I try to write a python backend for pamcan-g2, for that I should write a callback for *trans_cb_conv In python I've write : trans_cb_event = CFUNCTYPE(ctypes.c_char_p,ctypes.c_void_p,POINTER(ctypes.c_int) ... def fpm_trans_conv(event,pkg,response): foo... response=1 and for call the callback if trans_init(pm_trans,flags, trans_cb_event(fpm_progress_event), trans_cb_conv(fpm_trans_conv), None) == -1 : foo... Into libpacman response is a C int pointer : ... QUESTION(trans, PM_TRANS_CONV_LOCAL_UPTODATE, local, NULL, NULL, &resp); ... but resp=0 howto pass response by reference instead by value ? If you would more informations/code : the .h is here : http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=blob_plain;f=lib/libpacman/pacman.h;hb=HEAD the python code here : http://git.frugalware.org/gitweb/gitweb.cgi?p=frugal-tweak.git;a=blob_plain;f=py-pacman/py-pacman.py;hb=4abed4ee445f009387a3a51957e231c010c123bb trans_cb_event line 315 fpm_trans_conv line 676 pacman_trans_init callback 708 From ladasky at my-deja.com Wed Dec 28 03:51:48 2011 From: ladasky at my-deja.com (John Ladasky) Date: Wed, 28 Dec 2011 00:51:48 -0800 (PST) Subject: Multiprocessing bug, is information ever omitted from a traceback? References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> Message-ID: <96329d43-30b4-4c90-a925-d6500e21ad7c@c42g2000prb.googlegroups.com> I hope that the interested parties who offered me help will remember this thread title. It has been a few weeks. After fussing around with an alternate installation of Python 2.7.2 on top of Ubuntu Linux 10.04, I decided it would be easier to upgrade to Ubuntu 11.10, for which Python 2.7.2 is the default Python version. I have reproduced my Python 2.6.6 bug in Python 2.7.2, as you will see. On Dec 9, 3:14?pm, I wrote: > All lines of code referenced in the traceback are in the standard > library code: > > Exception in thread Thread-1: > Traceback (most recent call last): > ? File "/usr/lib/python2.6/threading.py", line 532, in > __bootstrap_inner > ? ? self.run() > ? File "/usr/lib/python2.6/threading.py", line 484, in run > ? ? self.__target(*self.__args, **self.__kwargs) > ? File "/usr/lib/python2.6/multiprocessing/pool.py", line 284, in > _handle_tasks > ? ? put(task) > TypeError: expected string or Unicode object, NoneType found > > Fortunately, I have a working version of my code. ?I was trying to add > new features, and only my new code is causing trouble. ?This has > allowed me to examine the contexts of task when everything works. > > Task is not a string when the program works. ?Task is not None when > the program doesn't work. ?In fact, task is a deeply-nested tuple. ?NO > PART of this tuple ever contains any strings, as far as I can tell. > More details in my original thread. Here's the traceback that I obtained tonight. It's essentially the same, except for the line numbers in the library code and the thread number which yielded the exception: | Exception in thread Thread-2: | Traceback (most recent call last): | File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner | self.run() | File "/usr/lib/python2.7/threading.py", line 505, in run | self.__target(*self.__args, **self.__kwargs) | File "/usr/lib/python2.7/multiprocessing/pool.py", line 315, in _handle_tasks | put(task) | TypeError: expected string or Unicode object, NoneType found I still have more work to do, I know. I will try to pare down my fairly-complex code to a minimal example. Check back again in a few days? Weeks? Thanks! From robert.kern at gmail.com Wed Dec 28 04:24:42 2011 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 28 Dec 2011 09:24:42 +0000 Subject: Slices when extending python with C++ In-Reply-To: References: Message-ID: On 12/27/11 11:02 PM, rozelak at volny.cz wrote: > Hallo, > I have kind of special question when extening python with C++ > implemented modules. > > I try to implement a class, behaving also like an array. And I need > to implement slice-getters. I implemented PySequenceMethods.sq_slice > to get "simple" slices like: > > myobj[x:y] > > It works perfectly, without problems. But now I have a problem when > need to access the array with "special" slices, like: > > myobj[x:y:step] > myobj[::-1] # to revert the array > > I would like to ask which method must be implemented to get this > "special" slice getters. The "simple" slice > PySequenceMethods.sq_slice getter accepts only two indexes - from, > to, but not a step (which would solve the issue). The sq_slice slot is deprecated, just like the __getslice__() method on the Python side. Instead, implement sq_item to accept a slice object in addition to integer indices. http://docs.python.org/c-api/slice.html -- 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 From emekamicro at gmail.com Wed Dec 28 06:12:50 2011 From: emekamicro at gmail.com (Emeka) Date: Wed, 28 Dec 2011 12:12:50 +0100 Subject: globals function Message-ID: Hello All, What is the downside of using globals function Regards, Janus -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Wed Dec 28 06:57:52 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 28 Dec 2011 03:57:52 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <4ef757e3$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa4d55$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <222e6aa9-1fb9-4186-a23d-e9fd2757329e@dp8g2000vbb.googlegroups.com> On Dec 27, 11:57?pm, Steven D'Aprano wrote: > On Mon, 26 Dec 2011 13:41:34 -0800, Eelco wrote: > > On Dec 25, 6:05?pm, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> wrote: > >> On Sun, 25 Dec 2011 07:38:17 -0800, Eelco wrote: > > [...] > > >> > How is 'head, *tail = sequence' or semantically entirely > >> > equivalently, 'head, tail::list = sequence' any different then? Of > >> > course after interpretation/compilation, what it boils down to is > >> > that we are constructing a list and binding it to the identifier > >> > tail, but that is not how it is formulated in python as a language > > >> I'm afraid it is. > > >> Here's the definition of assignment in Python 3: > >>http://docs.python.org/py3k/reference/simple_stmts.html#assignment- > >> statements > > > Que? > > You have claimed that "constructing a list and binding it to the > identifier" is not how iterator unpacking is formulated in Python the > language. But that is wrong. That *is* how iterator unpacking is > formulated in Python the language. The reference manual goes into detail > on how assignment is defined in Python. You should read it. > > > 'head, *tail = sequence' > > > Is how one currently unpacks a head and tail in idiomatic python > > > This is semantically equivalent to > > > 'head = sequence[0]' > > 'tail = list(sequence[1:])' > > There's a reason this feature is called *iterable* unpacking: it operates > on any iterable object, not just indexable sequences. > > 'head, *tail = sequence' is not just semantically equivalent to, but > *actually is* implemented as something very close to: > > temp = iter(sequence) > head = next(temp) > tail = list(temp) > del temp > > Extended iterable unpacking, as in 'head, *middle, tail = sequence' is a > little more complex, but otherwise the same: it operates using the > iterator protocol, not indexing. I recommend you read the PEP, if you > haven't already done so. > > http://www.python.org/dev/peps/pep-3132/ > > > But these forms are linguistically different, in too many different ways > > to mention. > > How about mentioning even one way? Because I have no idea what > differences you are referring to, or why you think they are important. The one spans two lines; the other one. Need I go on? > >> > We dont have > >> > something of the form 'tail = list_tail(sequence)'. > > >> I'm afraid we do. See the definition of assignment again. > > > Que? > > Again, you make a claim about Python which is contradicted by the > documented behaviour of the language. You claim that we DON'T have > something of the form 'tail = list_tail(sequence)', but that is *exactly* > what we DO have: extracting the tail from an iterator. > > Obviously there is no built-in function "list_tail" but we get the same > effect by just using list() on an iterator after advancing past the first > item. > > > My claim is that the two semantically identical formulations above do > > not have isomorphic linguistic form. As far as I can make sense of your > > words, you seem to be disputing this claim, but its a claim as much > > worth debating as that the sun rises in the east. > > "Isomorphic linguistic form"? Are you referring to the idea from > linguistics that there are analogies between the structure of phonic and > semantic units? E.g. that the structures (phoneme, syllable, word) and > (sememe, onomateme, sentence) are analogous. > > I don't see why this is relevant, or which units you are referring to -- > the human-language description of what Python does, high-level Python > language features, Python byte-code, or machine code. Not that it > matters, but it is unlikely that any of those are isomorphic in the > linguistic sense, and I am not making any general claim that they are. > > If not, I have no idea what you are getting at, except possibly trying to > hide behind obfuscation. I wasnt too worried about obfuscation, since I think there is little left to lose on that front. Let me make a last-ditch effort to explain though: When python reads your code, it parses it into a symbolic graph representation. The two snippets under consideration do not parse into the same graph, in the same way that insignificant whitespace or arbitrary identifier names do, for instance. Hence, not linguistically isomorphic. The very function of a programming language is to transform this representation of the code into semantically identical but different code; (machine code, eventually). You fail to grok the difference between semantic equivalence and linguistic equivalence. Yes, there can be a semantic equivalence between iterable unpacking and a syntax of the form tail_list(sequence). And python does indeed probably apply a transformation of this kind under the hood (though we couldnt care less what it does exactly). AND IT PERFORMS THAT TRANSFORMATION USING THE TYPE CONSTRAINT GIVEN. Another example: the C code 'float x = 3', how would you interpret that? Is it 'not a type constraint on x', because eventually your C compiler turns it into machine code that doesnt leave a trace of that type constraint anyway? No, its a type constraint, exactly because C uses it to emit code specific to the type specified. > >> > Rather, we annotate > >> > the identifier 'tail' with an attribute that unquestionably > >> > destinates it to become a list*. It is no longer that 'tail' will > >> > just take anything that pops out of the expression on the right hand > >> > side; > > >> Of course it will. Python is a dynamically typed language. It doesn't > >> suddenly develop static types to ensure that 'tail' becomes a list; > >> 'tail' is bound to a list because that's what the assignment statement > >> provides. > > > How python accomplishes any of this under the hood is entirely > > immaterial. The form is that of a compile-time type constraint, > > regardless of whether the BDFL ever thought about it in these terms. > > Compile-time type constraints only have meaning in statically-typed > languages. Otherwise, you are applying an analogy that simply doesn't > fit, like claiming that the motion of paper airplanes and birds are > equivalent just because in English we use the word "fly" to describe them > both. > > The differences between what Python actually does and compile-time type- > constraints are greater than the similarities. > > Similarities: > > (1) In both cases, 'tail' ends up as a list. > > Differences: > > (1) Compiler enforces the constraint that the identifier 'tail' is always > associated with a list and will prevent any attempt to associate 'tail' > with some value that is not a list. Python does nothing like this: the > identifier 'tail' has no type restrictions at all, and can be used for > any object. After it is rebound, yes. Within the context of the iterable unpacking statement however, IT IS ALWAYS A LIST. Not only is it always a list, PYTHON *NEEDS* THIS KNOWLEDGE ABOUT TAIL TO EMIT THE CORRECT CODE (explicitly or implicitly; thats why I say 'it can be viewed as a type constraint') Just because the type constraint only applies within the statement does not make it any less of a type constraint; just one with different semantics, from say, C. > (3) "Constraint" has the conventional meaning of a restriction, not a > result; a type-constraint refers to a variable being prevented from being > set to some other type, not that it merely becomes set to that type. For > example, one doesn't refer to 'x = 1' as a type-constraint merely because > x gets set to an int value; why do you insist that 'head, *tail = > sequence' is a type-constraint merely because tail gets set to a list? Yes, the constraint is unique in this case; that does not make it any less of a constraint. A formula of the form 'x = 1' is called a constraint in mathematics. Collections with one element are still called collections. Do you have a problem with that too? > >> > rather, > >> > the semantics of what will go on at right hand side is coerced by the > >> > constraint placed on 'tail'. > > >> But it isn't a constraint placed on 'tail'. It is a consequence of the > >> definition of assignment in Python 3. 'tail' becomes bound to a list > >> because that is what the assignment statement is defined to do in that > >> circumstance, not because the identifier (symbol) 'tail' is constrained > >> to only accept lists. 'tail' may not even exist before hand, so talking > >> about constraints on 'tail' is an abuse of language, AS YOU AGREED > >> ABOVE. > > > 'tail' is (re)declared on the spot as a brand-new identifier (type > > constraint included); whether it exists before has no significance > > whatsoever, since python allows rebinding of identifiers. > > Given the following: > > tail = "beautiful red plumage" > head, *tail = [1, 2, 3, 4] > tail = 42 > > is it your option that all three instantiations of the *identifier* > 'tail' (one in each line) are *different* identifiers? Otherwise, your > statement about "brand new identifier" is simply wrong. > > If you allow that they are the same identifier with three different > values (and three different types), then this completely destroys your > argument that there is a constraint on the identifier 'tail'. First > 'tail' is a string, then it is a list, then it is an int. If that is a > type constraint (restriction) on 'tail', then constraint has no meaning. > > If you state that they are three different identifiers that just happen > to be identical in every way that can be detected, then you are engaged > in counting angels dancing on the head of pins. Given this idea that the > middle 'tail' identifier is a different identifier from the other two, > then I might accept that there *could* be a type-constraint on middle > 'tail', at least in some implementation of Python that hasn't yet been > created. But what a pointless argument to make. > > >> > *(I call that a 'type constraint', because that is what it literally > >> > is; > > >> No. It is literally a name binding of a dynamically typed, > >> unconstrained name to an object which happens to be a list. > > > Let me take a step back and reflect on the form of the argument we are > > having. I claim the object in front of us is a 'cube'. You deny this > > claim, by countering that it is 'just a particular configuration of > > atoms'. > > No no no, you have utterly misunderstood my argument. I'm not calling it > a configuration of atoms. I'm calling it a pyramid, and pointing out that > no matter how many times you state it is a cube, it actually has FOUR > sides, not six, and NONE of the sides are square, so it can't possibly be > a cube. For that to be true, you would have had to explain to me somewhere what it is you mean by a type constraint, and how this does not fit the bill. Granted, you did that for the first time in this post; and I added further detail why I think your particular usage of the term is too narrow; C does not have a unique claim to the concept. Or whereever you picked up your use of the term. Like I said, I mean it in its literal sense (and in the sense that happens to match the first hit on google; probably no coincidence). > > 'look at the definition!', you say; 'its just a list of coordinates, no > > mention of cubes whatsoever.'. > > > 'Look at the definition of a cube', I counter. 'This particular list of > > coordinates happens to fit the definition, whether the BDFT intended to > > or not'. > > But you are wrong. It does not fit the definition of a type-constraint, > except in the vacuous sense where you declare that there is some > invisible distinction between the identifier 'tail' in one statement and > the identical identifier 'tail' in another statement. Its not vacuous; its essential to the mechanics behind the transformation of the iterable unpacking statement to its compiled form. But its meaning does not carry beyond that, no. > > You are correct, in the sense that I do not disagree that it is a > > particular configuration of atoms. But if you had a better understanding > > of cubes, youd realize it meets the definition; > > I might not share your deep and expert understanding of cubes, but I > understand what "type-constraint" means, and I know what Python does in > iterator unpacking, and I know that there is no sensible definition of > type-constraint that applies to iterator unpacking in Python. You seem to know what a type constraint is in a language like C, and you seem to model your complete understanding from that particular instance of its use, rather than look at the actual definition of the term, and seeing where it might apply. From adeep.adeep93 at gmail.com Wed Dec 28 07:02:45 2011 From: adeep.adeep93 at gmail.com (adeep 2) Date: Wed, 28 Dec 2011 04:02:45 -0800 (PST) Subject: Google form filling jobs Message-ID: <37c0df0e-50bf-413d-876e-6a86b5fc5197@u10g2000prl.googlegroups.com> Google form filling jobs http://gsonlinejobs.yolasite.com/ From hoogendoorn.eelco at gmail.com Wed Dec 28 07:04:00 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 28 Dec 2011 04:04:00 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> <4efa4fab$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 28, 12:07?am, Steven D'Aprano wrote: > On Mon, 26 Dec 2011 13:51:50 -0800, Eelco wrote: > > [...] > > >> If your point is that parens are used more often than > >> packing/unpacking, that's almost certainly true, since function calls > >> (including method invocations) are so prevalent in pretty much any > >> code. But what does that prove? > > > That proves the original point of contention: that the below* is > > suboptimal language design, > > Well duh. This is where the referee should interrupt you for snipping someones citation right before a 'but' > I was mocking the idea that the meaning of * is context-dependent is a > bad thing by pointing out that we accept context-dependent meaning for > round brackets () without any difficulties. Of course it is "suboptimal > language design" -- it couldn't fail to be. Context-dependency is not > necessarily a bad thing. You know, so you dont end up simply restating my point while trying to make it seem like you disagree. > > not because terseness always trumps > > verbosity, but because commonly-used constructs (such as parenthesis or > > round brackets or whatever you wish to call them) > > Parentheses are not a construct. They are symbols (punctuation marks) > which are applied to at least three different constructs: grouping, > function calls, class inheritance lists. Parenthesis encompass a class of constructs. Happy now? > > are more deserving of > > the limited space in both the ascii table and your reflexive memory, > > than uncommonly used ones. > > Right. And since sequence packing and unpacking is a common idiom, it > deserves to be given punctuation. That's my opinion. Its a valid opinion. But if we are going to be quantitative about terms such as 'common', you know that there will be at least an order of magnitude difference between these constructs in commonality, if not two. Thats what makes your example a poor one. If you could verbosify a construct of the same commonality and arrive at equally absurd code, you would have a point. From hoogendoorn.eelco at gmail.com Wed Dec 28 07:05:00 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 28 Dec 2011 04:05:00 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <254cfbe0-19db-4d56-87b2-08f9da19aa61@q17g2000yqh.googlegroups.com> Message-ID: On Dec 28, 2:11?am, Rick Johnson wrote: > On Dec 27, 5:10?pm, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> wrote: > > On Sun, 25 Dec 2011 07:47:20 -0800, Eelco wrote: > > Your original use-case, where you want to change the type of tail from a > > list to something else, is simply solved by one extra line of code: > > > head, *tail = sequence > > tail = tuple(tail) > > i wonder if we could make this proposal a bit more "Pythonic"? Hmm... > > head, tuple(tail) = sequence > > ...YEP! That has been considered; it was my first thought too, but this requires one to break the symmetry between collection packing and unpacking. From emekamicro at gmail.com Wed Dec 28 07:05:56 2011 From: emekamicro at gmail.com (Emeka) Date: Wed, 28 Dec 2011 13:05:56 +0100 Subject: Get Class properties Message-ID: Hello All, Say I have a class like below class Town: state = StateClass() cities = CityClass() Is there way to introspect such that one can list the properties keys and their values in such a way that it would look like playing around dictionary ? Regards, Janus -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Wed Dec 28 07:08:09 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 28 Dec 2011 04:08:09 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> On Dec 28, 8:08?am, Chris Angelico wrote: > On Wed, Dec 28, 2011 at 5:25 PM, Steven D'Aprano > > > > > > > > > > wrote: > > On Wed, 28 Dec 2011 15:06:37 +1100, Chris Angelico wrote: > > >> ... suppose you have a huge > >> set/frozenset using tuples as the keys, and one of your operations is to > >> shorten all keys by removing their first elements. Current Python > >> roughly doubles the cost of this operation, since you can't choose what > >> type the tail is made into. > > > The First Rule of Program Optimization: > > - Don't do it. > > > The Second Rule of Program Optimization (for experts only): > > - Don't do it yet. > > > Building syntax to optimize imagined problems is rarely a good idea. The > > difference between 2 seconds processing your huge set and 4 seconds > > processing it is unlikely to be significant unless you have dozens of > > such huge sets and less than a minute to process them all. > > > And your idea of "huge" is probably not that big... it makes me laugh > > when people ask how to optimize code "because my actual data has HUNDREDS > > of items!". Whoop-de-doo. Come back when you have a hundred million > > items, then I'll take your question seriously. > > > (All references to "you" and "your" are generic, and not aimed at Chris > > personally. Stupid English language.) > > And what you're seeing there is the _best possible_ situation I could > think of, the strongest possible justification for new syntax. > Granted, that may say more about me and my imagination than about the > problem, but the challenge is open: Come up with something that > actually needs this. > > ChrisA I personally feel any performance benefits are but a plus; they are not the motivating factor for this idea. I simply like the added verbosity and explicitness, thats the bottom line. From emekamicro at gmail.com Wed Dec 28 07:13:41 2011 From: emekamicro at gmail.com (Emeka) Date: Wed, 28 Dec 2011 13:13:41 +0100 Subject: Get Class properties In-Reply-To: References: Message-ID: Hello All, I have seen what I am looking for.. __dict__. Thanks! Regards, Janus On Wed, Dec 28, 2011 at 1:05 PM, Emeka wrote: > > Hello All, > > Say I have a class like below > > class Town: > state = StateClass() > cities = CityClass() > > > Is there way to introspect such that one can list the properties keys and > their values in such a way that it would look like playing around > dictionary ? > > Regards, > Janus > -- > *Satajanus Nig. Ltd > > > * > -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Wed Dec 28 07:25:29 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 28 Dec 2011 04:25:29 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: <5dd7607f-b5d6-4785-8b59-fd73d8d8c1d9@p4g2000vbt.googlegroups.com> On Dec 28, 2:56?am, Rick Johnson wrote: > On Dec 27, 3:44?pm, Eelco wrote: > > > Despite the fact that you mis-attributed that quote to me, im going to > > be a little bit offended in the name of its actual author anyway. > > Thats a lot of words to waste on your linguistic preferences. > > Personally, I reserve the right to botch my non-native languages as > > much as I please. > > I hope you're just joking a bit because i have little respect for > those who refuse to better themselves. If you are learning English as > a second language then you have a legitimacy excuse, but at some point > that excuse just becomes a lie. In any case, i apologize for mis- > quoting you. Yes, I was joking a bit; I learned my english primarily on programming boards, and im proud to say it rivals that of a majority of native speakers (low bar to beat, true). Furthermore, you are free to direct criticism at my writing or that of anyone else, but I must say I dont much care to hear it. A language is learned by using it, in reading, writing or speech; not by grammar nazis, or style nazis for that matter. Im here to discuss issues related to python, and anyone who manages to make himself understood is welcome to do so, as far as I am concerned. Im much more worried whether they have something interesting to contribute to the actual discussion. Not getting stuck picking nits, fighting personal feuds or getting dragged into the swamp by trolls; those are the real challenges, in my opinion. If you are insistent on bettering yourself; there is half a dozen other languages we could continue the conversation in. Your marginal gain per sentence read and written might be much larger there than in english. From rozelak at volny.cz Wed Dec 28 08:01:49 2011 From: rozelak at volny.cz (rozelak at volny.cz) Date: Wed, 28 Dec 2011 14:01:49 +0100 (CET) Subject: =?iso-8859-2?Q?Re:_Slices_when_extending_python_with_C=2B=2B?= Message-ID: <18d00d51ecfd54dcfeb18e1708f6c77d@mail2.volny.cz> Dear Robert, thank you very much for your answer. I understand what you mean and I have looked at slice object and C-api methods it provides. It should be easy to implement it. The only question is how exactly yo implement the general getter, since sq_item you mention (assume you mean PySequenceMethods.sq_item) has the following signature: PyObject * (* ssizeargfunc)(PyObject *, Py_ssize_t) accepting Py_ssize_t as the index, not a PyObject * which would hold the slice. So, how exactly to implement the getter? As a general method named __getitem__ registered in PyMethodDef? Or in another way? Thank you very much again. Regards, Dan ----- P?VODN? ZPR?VA ----- Od: "Robert Kern" Komu: python-list at python.org P?edm?t: Re: Slices when extending python with C++ Datum: 28.12.2011 - 10:24:42 > On 12/27/11 11:02 PM, rozelak at volny.cz wrote: > > Hallo, > > I have kind of special question when extening > > python with C++ > > > implemented modules. > > > > I try to implement a class, behaving also like > > an array. And I need > > > to implement slice-getters. I implemented > > PySequenceMethods.sq_slice > > > to get "simple" slices like: > > > > myobj[x:y] > > > > It works perfectly, without problems. But now I > > have a problem when > > > need to access the array with "special" slices, > > like: > > > > > myobj[x:y:step] > > myobj[::-1] # to revert the array > > > > I would like to ask which method must be > > implemented to get this > > > "special" slice getters. The "simple" slice > > PySequenceMethods.sq_slice getter accepts only > > two indexes - from, > > > to, but not a step (which would solve the > > issue). > > > The sq_slice slot is deprecated, just like the > __getslice__() method on the > Python side. Instead, implement sq_item to accept > a slice object in addition to > integer indices. > > http://docs.python.org/c-api/slice.html > > -- > 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 > -- Tradi?n? i modern? adventn? a novoro?n? zvyky, sv?te?n? j?dlo a pit?, v?zdoba a d?rky... - ?t?te v?no?n? a silvestrovsk? speci?l port?lu VOLN?.cz na http://web.volny.cz/data/click.php?id=1301 From robert.kern at gmail.com Wed Dec 28 08:18:36 2011 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 28 Dec 2011 13:18:36 +0000 Subject: Slices when extending python with C++ In-Reply-To: <18d00d51ecfd54dcfeb18e1708f6c77d@mail2.volny.cz> References: <18d00d51ecfd54dcfeb18e1708f6c77d@mail2.volny.cz> Message-ID: On 12/28/11 1:01 PM, rozelak at volny.cz wrote: > Dear Robert, > > thank you very much for your answer. I understand what you mean and > I have looked at slice object and C-api methods it provides. It > should be easy to implement it. > > The only question is how exactly yo implement the general getter, > since sq_item you mention (assume you mean > PySequenceMethods.sq_item) has the following signature: > > PyObject * (* ssizeargfunc)(PyObject *, Py_ssize_t) > > accepting Py_ssize_t as the index, not a PyObject * which would hold > the slice. > > So, how exactly to implement the getter? As a general method named > __getitem__ registered in PyMethodDef? Or in another way? Sorry, PyMappingMethods.mp_subscript is the general function that you need to implement. -- 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 From prabhap2w at gmail.com Wed Dec 28 09:46:19 2011 From: prabhap2w at gmail.com (PRABHAKARAN K) Date: Wed, 28 Dec 2011 06:46:19 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From prabhap2w at gmail.com Wed Dec 28 09:47:07 2011 From: prabhap2w at gmail.com (PRABHAKARAN K) Date: Wed, 28 Dec 2011 06:47:07 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From k.sahithi2862 at gmail.com Wed Dec 28 10:11:30 2011 From: k.sahithi2862 at gmail.com (SAHITHI) Date: Wed, 28 Dec 2011 07:11:30 -0800 (PST) Subject: NEW HOT PHOTOS & VIDEOS Message-ID: <02fd5c97-0ebb-4c5c-8411-554638b8eb0c@37g2000prc.googlegroups.com> FOR GOOD JOBS SITES TO YOU http://goodjobssites.blogspot.com/ SRIKANTH DEVARA MOVIE GALLERY http://actressgallery-kalyani.blogspot.com/2011/12/devaraya-movie-stills.html O MANASA MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/o-manasa-movie-stills.html KULLUMANALI MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/kullu-manali-movie-stills.html BUSINESSMAN MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/businessman-movie-stills.html HOT ACTRESS BIKINI STILLS IN 2012 CALENDAR http://actressgallery-kalyani.blogspot.com/2011/12/2012-actress-calendar-wallpapers.html SAMANTHA SOUTHSCOPE HOT PHOTOS http://actressgallery-kalyani.blogspot.com/2011/12/samantha-at-south-scope-magazine.html DEEPIKA PADUKONE SPICY WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/deepika-padukone.html KATRINA KAIF LATEST HOT WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/katrina-kaif-wallpapers.html LOVE FAILURE MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/love-failure-movie-stills.html 4FRIENDS MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/4-friends-movie-stills.html NANDEESWARUDU MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/nandeeswarudu-movie-stills.html HARI PRIYA HOT PHOTO STILLS http://actressgallery-kalyani.blogspot.com/2011/12/haripariya-actress.html SHRUTI HASSAN HOT IN 3 MOVIE http://actressgallery-kalyani.blogspot.com/2011/11/shruti-hassan-in-3-movie.html PANJA MOVIE LATEST STILLS http://actressgallery-kalyani.blogspot.com/2011/11/panja-movie-stills.html NIPPU MOVIE WORKING STILLS http://actressgallery-kalyani.blogspot.com/2011/11/nippu-movie-stills.html FOR FAST UPDATES IN FILM INDUSTRY KATRINA KAIF RARE PHOTOS http://allyouwants.blogspot.com/2011/12/katrina-kaif.html KAJAL AGARWAL LATEST STILLS http://allyouwants.blogspot.com/2011/03/kajal-latest-stills.html PRIYANKA CHOPRA LATEST HOT PHOTOSHOOT http://allyouwants.blogspot.com/2011/08/priyanka-chopra.html TAMIL ACTRESS HOT PHOTOS&VIDEOS http://allyouwants.blogspot.com/2011/08/tamil-actress.html FOR ONLY HOT GUYS SEE THIS LATEST HOT KATRINA KAIF PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/katrina-kaif-hot.html ANUSHKA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/anushka-hot.html PRIYANKA TIWARI HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/12/priyanka-tiwari-hot.html TAMANNA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/tamanna-hot.html PARUL HOT PHOTO STILLS http://hotactress-kalyani.blogspot.com/2011/12/parul-hot.html ADITI AGARWAL NEW ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/12/aditi-agarwal-hot.html PAYAL GHOSH HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/payal-ghosh-hot.html RAGINI DWIVEDI HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/12/ragini-dwivedi.html PARVATHI MELTON LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/parvathi-melton-hot.html SARAH JANE DIAS HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/sarah-jane-dias-hot.html KAJAL AGARWAL HOT SAREE STILLS http://hotactress-kalyani.blogspot.com/2011/11/kajal-agarwal-hot-in-saree.html POONAM KAUR HOT ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/11/poonam-kaur-hot.html From bahamutzero8825 at gmail.com Wed Dec 28 10:59:00 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 28 Dec 2011 09:59:00 -0600 Subject: RSA and Cryptography in PYTHON In-Reply-To: <24097307.586.1325029506304.JavaMail.geo-discussion-forums@prmw6> References: <31261167.63.1324843108857.JavaMail.geo-discussion-forums@prdv33> <24097307.586.1325029506304.JavaMail.geo-discussion-forums@prmw6> Message-ID: <4EFB3CC4.6080409@gmail.com> On 12/27/2011 5:45 PM, 88888 Dihedral wrote: > I am getting lousy in the news group in my writing? > > I mean the non-trivial decoding of the key decomposition. > Of course RSA is symmetric in encoding and decoding of data. You really are a markov chain bot. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From lorenzo.digregorio at gmail.com Wed Dec 28 11:18:10 2011 From: lorenzo.digregorio at gmail.com (Lorenzo Di Gregorio) Date: Wed, 28 Dec 2011 08:18:10 -0800 (PST) Subject: Global variables in a C extension for Python Message-ID: Hello, I've written a C extension for Python which works so far, but now I've stumbled onto a simple problem for which I just can't find any example on the web, so here I am crying for help ;-) I'll trying to reduce the problem to a minimal example. Let's say I need to call from Python functions of a C program like: static int counter = 0; void do_something(...) { ... counter++; ... } void do_something_else(...) { ... counter++; ... } So they access a common global variable. I've written the wrappers for the functions, but I'd like to place "counter" in the module's space and have wrappers accessing it like self->counter. I do not need to make "counter" visible to Python, I just need the global static variable available for C. I've got somehow a clue of how this should work, but not much more than a clue, and I'd appreciate to see a simple example. Best Regards, Lorenzo From wolftracks at invalid.com Wed Dec 28 11:33:15 2011 From: wolftracks at invalid.com (W. eWatson) Date: Wed, 28 Dec 2011 08:33:15 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 7:31 PM, W. eWatson wrote: > On 12/27/2011 6:27 PM, Ian Kelly wrote: >> On Tue, Dec 27, 2011 at 6:21 PM, W. eWatson >> wrote: >>> Well, it found several problems. These DLLs >>> MSVCP1 >>> EFSADU >>> MSJAVA. >> >> I'm guessing MSVCP1 is a typo for MSVCP71? If that is missing then >> that is probably the culprit. That DLL is the C runtime library. It >> is supposed to be shipped with applications that need it, but it is so >> ubiquitous that it is often assumed to be present or forgotten. You >> can download it from >> http://www.dll-files.com/dllindex/dll-files.shtml?msvcp71 (or just >> find it on another Windows XP PC) and copy it into >> C:\Windows\System32. Don't forget to run regsvr32 to register it. >> >> HTH, >> Ian > You are very likely right about the spelling. I wrote it down, and > carried it to this PC. Sometimes I can't read my own writing. > ... Well, thing went slightly awry. The link gave me two choices. Download msv...dll fixer, and download fixer. I took the latter. However, just checking on the other one, found that I got the same exe file. I installed it on the XP PC, and pressed what looked like a reasonable place to start. It would download the dll, and register it. Well, it seemed more interested in the registry. It scanned four areas of the registry, and found 123 problems in total. To fix them would require buying something. I noticed a large button near the top that said download dll. It found one the internet, and guess what? More purchase for the download. It seems like dll-fixer has a corner on the market. I found this , but he almost has too much to say. He issues a warning about getting a dll off the web. He does offer this. Run the sfc /scannow System File Checker command to replace a missing or corrupt copy of the msvcp71.dll file. If this DLL file is provided my Microsoft, the System File Checker tool should restore it. I guess I'm missing something here. OK, I'm borrowing one from my XP laptop. Back later. From wolftracks at invalid.com Wed Dec 28 11:46:48 2011 From: wolftracks at invalid.com (W. eWatson) Date: Wed, 28 Dec 2011 08:46:48 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: A new dilemma. The PC XP in question with Python has the the msvcp71.dll file in System32. The one I took off my other laptop has a slightly newer one. Feb 2003 vs Aug 2003. Perhaps the (python PC) has a corrupt one? From affdfsdfdsfsd at b.com Wed Dec 28 12:16:48 2011 From: affdfsdfdsfsd at b.com (Tracubik) Date: 28 Dec 2011 17:16:48 GMT Subject: pls explain this flags use Message-ID: <4efb4f00$0$1393$4fafbaef@reader1.news.tin.it> Hi all, i've found here (http://python-gtk-3-tutorial.readthedocs.org/en/latest/ layout.html#table) this code: [quote] If omitted, xoptions and yoptions defaults to Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL. [end quote] xoptions have 3 flags: EXPAND, FILL, SHRINK so, how it is supposed to work? it means that by default xoptions is set to EXPAND true, FILL true, SHRINK false? how it work? "|" is used with bits afaik, so i suppouse the xoptions is 3 bits? and the default is EXPAND FILL SHRINK 1 1 0 (or something similar) so i have AttachOptions.EXPAND = 100 AttachOptions.FILL = 010 EXPAND|FILL = 100|010 = 110 is that right? so if i want EXPAND *and* FILL i have to make EXPAND *binary or* FILL? i'm new at this sintassi so i'm trying to guess on my own, pls feel free to tell me if i'm wrong and where thanks and best wishes Medeo From ian.g.kelly at gmail.com Wed Dec 28 12:37:14 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 28 Dec 2011 10:37:14 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On Wed, Dec 28, 2011 at 9:33 AM, W. eWatson wrote: > Well, thing went slightly awry. The link gave me two choices. Download > msv...dll fixer, and download fixer. I took the latter. However, just > checking on the other one, found that I got the same exe file. I installed > it on the XP PC, and pressed what looked like a reasonable place to start. > It would download the dll, and register it. > > Well, it seemed more interested in the registry. It scanned four areas of > the registry, and found 123 problems in total. To fix them would require > buying something. > > I noticed a large button near the top that said download dll. It found one > the internet, and guess what? More purchase for the download. > > It seems like dll-fixer has a corner on the market. Definitely don't download that spamware fixer program -- who knows what that does? You want the grey "Download zip-file" link, not the not the flashy "Download fixer" ad links. Anyway, it sounds like you've found another copy of the DLL. If I were you, I would uninstall that fixer ASAP and then run an anti-malware and anti-virus tool or two, just in case. > Run the sfc /scannow System File Checker command to replace a missing or > corrupt copy of the msvcp71.dll file. If this DLL file is provided my > Microsoft, the System File Checker tool should restore it. I don't believe it is provided by Microsoft, but it wouldn't hurt to try. > A new dilemma. The PC XP in question with Python has the the msvcp71.dll > file in System32. The one I took off my other laptop has a slightly newer > one. Feb 2003 vs Aug 2003. Weird. Try registering the existing dll, try replacing it with the other one (be sure to back up the original first), etc. From tinnews at isbd.co.uk Wed Dec 28 12:38:33 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Wed, 28 Dec 2011 17:38:33 +0000 Subject: Python app installs itself in two places - why? Message-ID: I have installed an application called pycocuma on my xubuntu 11.10 system. It works OK and I'm aiming to develop it a little as its 'owner' has long since stopped work on it. However I'm a little puzzled by the way it has installed itself (it's a standard package from the Ubuntu repositories), all the python source is installed in two places:- /usr/lib/python2.7/dist-packages/pycocumalib /usr/share/pyshared/pycocumalib All the files in /usr/lib/python2.7/dist-packages/pycocumalib are actually symbolic links to the ones in /usr/share/pyshared/pycocumalib but I just wondered why they're in both places. Is it just 'historic' or is there a good sound reason for it? -- Chris Green From lie.1296 at gmail.com Wed Dec 28 12:54:05 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 29 Dec 2011 04:54:05 +1100 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: References: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/28/2011 11:57 AM, Rick Johnson wrote: > On Dec 27, 3:38 pm, Terry Reedy wrote: >> On 12/27/2011 1:04 PM, Rick Johnson wrote: >> >>> But this brings up a very important topic. Why do we even need triple >>> quote string literals to span multiple lines? Good question, and one i >>> have never really mused on until now. >> >> I have, and the reason I thought of is that people, including me, too >> ofter forget or accidentally fail to properly close a string literal, > > Yes, agreed. > >> Color coding editors make it easier to catch such errors, but they were >> less common in 1991. > > I would say the need for triple quote strings has passed long ago. > Like you say, since color lexers are ubiquitous now we don't need > them. > >> And there is still uncolored interactive mode. > > I don't see interactive command line programming as a problem. I mean, > who drops into a cmd line and starts writing paragraphs of string > literals? Typically, one would just make a few one-liner calls here or > there. Also, un-terminated string literal errors can be very > aggravating. Not because they are difficult to fix, no, but because > they are difficult to find! -- and sending me an error message > like... > > "Exception: Un-terminated string literal meets EOF! line: 50,466,638" > > ... is about as helpful as a bullet in my head! > > If the interpreter finds itself at EOF BEFORE a string closes, don't > you think it would be more helpful to include the currently "opened" > strings START POSITION also? No it wouldn't. Once you get an unterminated string literal, the string would terminate at the next string opening. Then it would fuck the parser since it will try to parse what was supposed to be a string literal as a code. For example: hello = 'bar' s = "boo, I missed a quote here print 'hello = ', hello, "; s = ", s the parser would misleadingly show that you have an unclosed string literal here: vvv print 'hello = ', hello, "; s = ", s ^^^ instead of on line 2. While an experienced programmer should be able to figure out what's wrong, I can see a beginner programmer trying to "fix" the problem like this: print 'hello = ', hello, "; s = ", s" and then complaining that print doesn't print. Limiting string literals to one line limits the possibility of damage to a single line. You will still have the same problem if you missed to close triple-quoted string, but since triple-quoted string are much rarer and they're pretty eye-catching, this sort of error harder are much harder. From ian.g.kelly at gmail.com Wed Dec 28 12:56:58 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 28 Dec 2011 10:56:58 -0700 Subject: pls explain this flags use In-Reply-To: <4efb4f00$0$1393$4fafbaef@reader1.news.tin.it> References: <4efb4f00$0$1393$4fafbaef@reader1.news.tin.it> Message-ID: On Wed, Dec 28, 2011 at 10:16 AM, Tracubik wrote: > Hi all, > i've found here (http://python-gtk-3-tutorial.readthedocs.org/en/latest/ > layout.html#table) this code: > > [quote] > If omitted, xoptions and yoptions defaults to > Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL. > [end quote] > > xoptions have 3 flags: EXPAND, FILL, SHRINK > > so, how it is supposed to work? > it means that by default xoptions is set to EXPAND true, FILL true, > SHRINK false? Yes. > how it work? "|" is used with bits afaik, so i suppouse the xoptions is 3 > bits? Yes. > and the default is > EXPAND FILL SHRINK > ?1 ? ? ?1 ? ? 0 > > (or something similar) > > so i have > > AttachOptions.EXPAND = 100 > AttachOptions.FILL ? = 010 > > EXPAND|FILL = 100|010 = 110 > > is that right? > so if i want EXPAND *and* FILL i have to make EXPAND *binary or* FILL? Yes. Binary or is used to set flags, binary and is used to test whether specific flags are set, e.g.: if options & AttachOptions.EXPAND: # do something expandy else: # do something not expandy From nirmaltech28 at gmail.com Wed Dec 28 13:02:13 2011 From: nirmaltech28 at gmail.com (Nirmal Kumar) Date: Wed, 28 Dec 2011 10:02:13 -0800 (PST) Subject: reverse() is not working Message-ID: <20618381.643.1325095333260.JavaMail.geo-discussion-forums@prez5> I am trying to pass the id to thanks view through reverse. But it's not working. I'm getting this error Reverse for 'reg.views.thanks' with arguments '(20,)' and keyword arguments '{}' not found. I posted the question with the code in stackoverflow: http://stackoverflow.com/questions/8648196/reverse-is-not-working How can I transfer arguments from one function to another in views?. Is it a normal thing or I have to use any built in functions? Thanks. From lie.1296 at gmail.com Wed Dec 28 13:37:08 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 29 Dec 2011 05:37:08 +1100 Subject: reverse() is not working In-Reply-To: <20618381.643.1325095333260.JavaMail.geo-discussion-forums@prez5> References: <20618381.643.1325095333260.JavaMail.geo-discussion-forums@prez5> Message-ID: On 12/29/2011 05:02 AM, Nirmal Kumar wrote: > I am trying to pass the id to thanks view through reverse. But it's not working. I'm getting this error > > Reverse for 'reg.views.thanks' with arguments '(20,)' and keyword arguments '{}' not found. > > I posted the question with the code in stackoverflow: > > http://stackoverflow.com/questions/8648196/reverse-is-not-working > > How can I transfer arguments from one function to another in views?. Is it a normal thing or I have to use any built in functions? > > Thanks. Welcome to Python Mailing List; your question is about django, django have their own mailing list, your question is probably better asked there. From tinnews at isbd.co.uk Wed Dec 28 14:04:59 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Wed, 28 Dec 2011 19:04:59 +0000 Subject: Where does this readOne() method come from? Message-ID: In the (rather sparse) documentation for the vobject package it has, in the section about parsing iCalendar objects, the following:- Parsing iCalendar objects ========================= To parse one top level component from an existing iCalendar stream or string, use the readOne function: >>> parsedCal = vobject.readOne(icalstream) >>> parsedCal.vevent.dtstart.value datetime.datetime(2006, 2, 16, 0, 0, tzinfo=tzutc()) Similarly, readComponents is a generator yielding one top level component at a time from a stream or string. >>> vobject.readComponents(icalstream).next().vevent.dtstart.value datetime.datetime(2006, 2, 16, 0, 0, tzinfo=tzutc()) More examples can be found in source code doctests. However *nowhere* can I find anything that tells me what or where the readOne() is. It's not to be found in the full epydoc API documentation for vobject (or, at least, I can't find it). All I want to do is read a .ics file and parse it. I used to use the icalendar package but that seems less well supported than vobject so I'm trying to use vobject instead but I'm not getting far at present. It sort of feels like "everyone knows what readOne() is", but I don't! :-) -- Chris Green From rantingrickjohnson at gmail.com Wed Dec 28 14:36:17 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Wed, 28 Dec 2011 11:36:17 -0800 (PST) Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 28, 12:58?am, Steven D'Aprano wrote: > On Tue, 27 Dec 2011 21:34:19 -0800, Rick Johnson wrote: > > I am also thinking that ANY quote char is a bad choice for string > > literal delimiters. Why? Well because it is often necessary to embed > > single or double quotes into a string literal. We need to use a > > delimiter that is not a current delimiter elsewhere in Python, and also, > > is a very rare char. I believe Mr Ewing found that perfect char in his > > "Multi-line uber raw string literals!" (Just scroll down a bit at this > > link)... > > > ? ?http://www.cosc.canterbury.ac.nz/greg.ewing/python/ideas.html > > Not surprisingly, you haven't thought this through. I'm sure Greg Ewing > has, which is why he hasn't proposed *replacing* string delimiters with > his multi-line format. That's why he proposed it as a *statement* and not > string-builder syntax. > > Without an end-delimiter, how do you embed string literals in expressions? Did you even read what i wrote? And if you did, you missed the point! My point was... while Greg's idea is nice, it is not the answer. HOWEVER, he did find the perfect char, and that char is the pipe! --> | mlstr = ||| this is a multi line sting that is delimited by "triple pipes". Or we could just 'single pipes' if we like, however, i think the "triple pipe' is easier to see. Since the pipe char is so rare in Python source, it becomes the obvious choice. And, best of all, no more worries about "embedded quotes". YAY! ||| slstr = |this is a single line string| The point is people, we should be using string delimiters that are ANYTHING besides " and '. Stop being a sheep and use your brain! From dbinks at codeaurora.org Wed Dec 28 15:10:12 2011 From: dbinks at codeaurora.org (Dominic Binks) Date: Wed, 28 Dec 2011 12:10:12 -0800 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EFB77A4.4010605@codeaurora.org> On 12/28/2011 11:36 AM, Rick Johnson wrote: > On Dec 28, 12:58 am, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Tue, 27 Dec 2011 21:34:19 -0800, Rick Johnson wrote: > >>> I am also thinking that ANY quote char is a bad choice for string >>> literal delimiters. Why? Well because it is often necessary to embed >>> single or double quotes into a string literal. We need to use a >>> delimiter that is not a current delimiter elsewhere in Python, and also, >>> is a very rare char. I believe Mr Ewing found that perfect char in his >>> "Multi-line uber raw string literals!" (Just scroll down a bit at this >>> link)... >> >>> http://www.cosc.canterbury.ac.nz/greg.ewing/python/ideas.html >> >> Not surprisingly, you haven't thought this through. I'm sure Greg Ewing >> has, which is why he hasn't proposed *replacing* string delimiters with >> his multi-line format. That's why he proposed it as a *statement* and not >> string-builder syntax. >> >> Without an end-delimiter, how do you embed string literals in expressions? > > Did you even read what i wrote? And if you did, you missed the point! > > My point was... while Greg's idea is nice, it is not the answer. > HOWEVER, he did find the perfect char, and that char is the pipe! --> > | > > mlstr = ||| > this is a > multi line sting that is > delimited by "triple pipes". Or we > could just 'single pipes' if we like, however, i think > the "triple pipe' is easier to see. Since the pipe char > is so rare in Python source, it becomes the obvious > choice. And, best of all, no more worries about > "embedded quotes". YAY! > ||| > > slstr = |this is a single line string| > > The point is people, we should be using string delimiters that are > ANYTHING besides " and '. Stop being a sheep and use your brain! I disagree that quotes are a bad thing. Most programming languages use +, -, / and * for arithmentic operations (* is closest character on a keyboard to x that is not the letter ex). Why do they choose to do this? It's actually a lot more work for the language implementer(s) to do this rather than simply providing functions add(x,y), subtract(x,y), divide(x,y) and multiply(x,y). The answer is very simple - convention. We use these symbols in mathematics and so it's much more convenient to follow through the mathematical convention. The learning curve is much lower (and it's less typing). After all experienced programmers know this is what is going on under the hood in pretty much all programming languages. Compilers are just making it easy for us. While it may indeed make sense to use a different character for delimiting strings, in English we use " to delimit spoken words in a narrative. Since strings most closely resemble spoken words from the point of view a programming language, using double quotes is a sensible choice since it eases learning. Convention is a very strong thing to argue against (not to mention huge code breakage as a result of such a change). Personally, I try to ensure I use consistency in the way I use the different quoting mechanisms, but that's just a personal choice. Sometimes that leads me to less pleasant looking strings, but I believe the consistency of style makes it easier to read. I think this proposal falls in the, let's make python case-insensitive kind of idea - it's never going to happen cause it offers very little benefit at huge cost. And I'm not going to contribute to this thread any further cause it's a pointless waste of my time to write it and others time to read it. -- Dominic Binks: dbinks at codeaurora.org Employee of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum From lie.1296 at gmail.com Wed Dec 28 15:13:17 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 29 Dec 2011 07:13:17 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/29/2011 06:36 AM, Rick Johnson wrote: > > mlstr = ||| > this is a > multi line sting that is > delimited by "triple pipes". Or we > could just 'single pipes' if we like, however, i think > the "triple pipe' is easier to see. Since the pipe char > is so rare in Python source, it becomes the obvious > choice. And, best of all, no more worries about > "embedded quotes". YAY! > ||| > > slstr = |this is a single line string| > > The point is people, we should be using string delimiters that are > ANYTHING besides " and '. Stop being a sheep and use your brain! This will incur the wrath of all linux/unix sysadmins all over the world. You are just replacing one problem with another; in your obliviousness, you had just missed the very obvious fact that once you replace quotes with pipes, quotes is extremely rare in Python (in fact you won't be seeing any quotes except inside string literals). From tjreedy at udel.edu Wed Dec 28 15:41:38 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 28 Dec 2011 15:41:38 -0500 Subject: Where does this readOne() method come from? In-Reply-To: References: Message-ID: On 12/28/2011 2:04 PM, tinnews at isbd.co.uk wrote: > In the (rather sparse) documentation for the vobject package it has, > in the section about parsing iCalendar objects, the following:- > > Parsing iCalendar objects > ========================= > > To parse one top level component from an existing iCalendar stream or > string, use the readOne function: > > >>> parsedCal = vobject.readOne(icalstream) It is obviously supposed to come from the 'vobject' package, whatever that is. I have never heard of vobject before, though. Try reading the source if the doc is limited. Or ask the author or mailing list if there is one. > It sort of feels like "everyone knows what readOne() is", Nope PS You already know the answer to the question you asked in the subject line. Better would have been "What is the vobject.readOne function?" to catch the eye of someone who *does* know something about vobject. -- Terry Jan Reedy From rosuav at gmail.com Wed Dec 28 15:45:31 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 07:45:31 +1100 Subject: Where does this readOne() method come from? In-Reply-To: References: Message-ID: On Thu, Dec 29, 2011 at 6:04 AM, wrote: > In the (rather sparse) documentation for the vobject package it has, > in the section about parsing iCalendar objects, the following:- > > ? ?>>> parsedCal = vobject.readOne(icalstream) Presumably you have this vobject package. Assuming it's installed correctly, all you need to do is: import vobject and then vobject.readOne should be available. Chris Angelico From ian.g.kelly at gmail.com Wed Dec 28 15:52:31 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 28 Dec 2011 13:52:31 -0700 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Dec 28, 2011 at 12:36 PM, Rick Johnson wrote: > My point was... while Greg's idea is nice, it is not the answer. > HOWEVER, he did find the perfect char, and that char is the pipe! --> > | > > mlstr = ||| > this is a > multi line sting that is > delimited by "triple pipes". Or we > could just 'single pipes' if we like, however, i think > the "triple pipe' is easier to see. Since the pipe char > is so rare in Python source, it becomes the obvious > choice. And, best of all, no more worries about > "embedded quotes". YAY! > ||| > > slstr = |this is a single line string| > > The point is people, we should be using string delimiters that are > ANYTHING besides " and '. Stop being a sheep and use your brain! So those who do shell scripting from Python might replace this: subprocess.call("cat {0} | awk '/foo|bar/ {print $3;}' | sort | uniq >{1}".format(infile, outfile), shell=True) with this: subprocess.call(|cat {0} \| awk '/foo\|bar/ {print $3;}' \| sort \| uniq >{1}|.format(infile, outfile), shell=True) or if we combine Rick's string escaping proposal: subprocess.call(|cat {0} awk '/foobar/ {print $3;}' sort uniq {1}|.format(infile, outfile), shell=True) Yeah, that's so much better. I especially like how nice and readable the regex is now. From lie.1296 at gmail.com Wed Dec 28 15:53:39 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 29 Dec 2011 07:53:39 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: On 12/28/2011 04:34 PM, Rick Johnson wrote: > On Dec 27, 9:49 pm, Rick Johnson wrote: > >> The fact is...even with the multi-line issue solved, we still have two >> forms of literal delimiters that encompass two characters resulting in >> *four* possible legal combinations of the exact same string! I don't >> know about you guys, but i am not a big fan of Tim Towtdi. > > actually i was a bit hasty with that statment and underestimated the > actual number of possiblities. > > 1) "this is a string" > 2) 'this is a string' > 3) r"this is a string" > 4) r'this is a string' > 5) '''this is a string''' > 6) """this is a string""" > 7) r'''this is a string''' > 8) r"""this is a string""" you missed u"nicode" string and b"yte" string, each of them available in both single and double quote flavor and single and triple quote flavor. Also, it's possible to mix them together ur"unicode raw string" or br"byte raw string", they are also in single and double quote flavor and single and triple quote flavor. And of course, I can't believe you forget Guido's favourite version, g"", available in musical and sirloin cloth flavor. From wolftracks at invalid.com Wed Dec 28 15:55:34 2011 From: wolftracks at invalid.com (W. eWatson) Date: Wed, 28 Dec 2011 12:55:34 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/28/2011 9:37 AM, Ian Kelly wrote: > On Wed, Dec 28, 2011 at 9:33 AM, W. eWatson wrote: >> Well, thing went slightly awry. The link gave me two choices. Download >> msv...dll fixer, and download fixer. I took the latter. However, just >> checking on the other one, found that I got the same exe file. I installed >> it on the XP PC, and pressed what looked like a reasonable place to start. >> It would download the dll, and register it. >> >> Well, it seemed more interested in the registry. It scanned four areas of >> the registry, and found 123 problems in total. To fix them would require >> buying something. >> >> I noticed a large button near the top that said download dll. It found one >> the internet, and guess what? More purchase for the download. >> >> It seems like dll-fixer has a corner on the market. > > Definitely don't download that spamware fixer program -- who knows > what that does? You want the grey "Download zip-file" link, not the > not the flashy "Download fixer" ad links. Anyway, it sounds like > you've found another copy of the DLL. > > If I were you, I would uninstall that fixer ASAP and then run an > anti-malware and anti-virus tool or two, just in case. > >> Run the sfc /scannow System File Checker command to replace a missing or >> corrupt copy of the msvcp71.dll file. If this DLL file is provided my >> Microsoft, the System File Checker tool should restore it. > > I don't believe it is provided by Microsoft, but it wouldn't hurt to try. > >> A new dilemma. The PC XP in question with Python has the the msvcp71.dll >> file in System32. The one I took off my other laptop has a slightly newer >> one. Feb 2003 vs Aug 2003. > > Weird. Try registering the existing dll, try replacing it with the > other one (be sure to back up the original first), etc. I haven't installed the newer version yet, Aug 2003. I thought I'd see what happened if I entered sfc /scannow. Whatever, happened the black window flashed by in a split second. Somehow this doesn't seem helpful . Although, 20 lines down or so it says: If sfc discovers that a protected file has been overwritten, it retrieves the correct version of the file from the %systemroot%\system32\dllcache folder, and then replaces the incorrect file. I'm going to run this by a XP NG. From python at bdurham.com Wed Dec 28 16:03:34 2011 From: python at bdurham.com (python at bdurham.com) Date: Wed, 28 Dec 2011 16:03:34 -0500 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: <1325106214.5274.140661016836917@webmail.messagingengine.com> Lie, > And of course, I can't believe you forget Guido's favourite version, g"", available in musical and sirloin cloth flavor. LMAO! That was brilliant! :) Cheers! Malcolm From nathan.alexander.rice at gmail.com Wed Dec 28 16:24:50 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 28 Dec 2011 16:24:50 -0500 Subject: Py-dea: Streamline string literals now! In-Reply-To: <1325106214.5274.140661016836917@webmail.messagingengine.com> References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <1325106214.5274.140661016836917@webmail.messagingengine.com> Message-ID: Quotes are obnoxious in the nesting sense because everyone uses quotes for string delimiters. By the same token, quotes are wonderful because not only are they intuitive to programmers, but they are intuitive in general. Parenthesis are pretty much in the same boat... I *HATE* them nested, but they are so intuitive that replacing them is a non starter; Just write code that doesn't nest parenthesis. Nathan From rosuav at gmail.com Wed Dec 28 16:42:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 08:42:46 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <1325106214.5274.140661016836917@webmail.messagingengine.com> Message-ID: On Thu, Dec 29, 2011 at 8:24 AM, Nathan Rice wrote: > Quotes are obnoxious in the nesting sense because everyone uses quotes > for string delimiters. ?By the same token, quotes are wonderful > because not only are they intuitive to programmers, but they are > intuitive in general. ?Parenthesis are pretty much in the same boat... > I *HATE* them nested, but they are so intuitive that replacing them is > a non starter; ?Just write code that doesn't nest parenthesis. Parentheses have different starting and ending delimiters and must be 'properly nested' (ie there must be exactly-matching inner parens inside any given set of outer parens (note that English has similar rules - you can't mis-nest parentheses (at any depth) in either language)). You can't guarantee the same about quoted strings - suppose the starting delimiter were ' and the ending " (or vice versa), it still wouldn't deal with the issue of coming across an apostrophe inside a quoted string. In actual fact, the real problem is that quoted strings need to be able to contain _anything_. The only true solution to that is length-provided strings: s = "4spam q = "14Hello, world!\n This works beautifully in interchange formats, but rather poorly in source code (or, for that matter, anything editable). ChrisA From nathan.alexander.rice at gmail.com Wed Dec 28 17:24:47 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 28 Dec 2011 17:24:47 -0500 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <1325106214.5274.140661016836917@webmail.messagingengine.com> Message-ID: On Wed, Dec 28, 2011 at 4:42 PM, Chris Angelico wrote: > On Thu, Dec 29, 2011 at 8:24 AM, Nathan Rice > wrote: >> Quotes are obnoxious in the nesting sense because everyone uses quotes >> for string delimiters. ?By the same token, quotes are wonderful >> because not only are they intuitive to programmers, but they are >> intuitive in general. ?Parenthesis are pretty much in the same boat... >> I *HATE* them nested, but they are so intuitive that replacing them is >> a non starter; ?Just write code that doesn't nest parenthesis. > > Parentheses have different starting and ending delimiters and must be > 'properly nested' (ie there must be exactly-matching inner parens > inside any given set of outer parens (note that English has similar > rules - you can't mis-nest parentheses (at any depth) in either > language)). You can't guarantee the same about quoted strings - > suppose the starting delimiter were ' and the ending " (or vice > versa), it still wouldn't deal with the issue of coming across an > apostrophe inside a quoted string. I think you read more into my statement than was intended. Parens are bad like nested quotes are bad in the sense that they made statements difficult to read and confusing. While it is entirely possible to parse nested strings automatically in a probabilistic manner with nearly flawless accuracy by examining everything between the start and end of the line, generally I feel that people are uncomfortable with probabilistic techniques in the realm of programming :) Best just to make the user be explicit. Nathan From lie.1296 at gmail.com Wed Dec 28 17:29:24 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 29 Dec 2011 09:29:24 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> Message-ID: On 12/28/2011 11:08 PM, Eelco wrote: > I personally feel any performance benefits are but a plus; they are > not the motivating factor for this idea. I simply like the added > verbosity and explicitness, thats the bottom line. Any performance benefits are a plus, I agree, as long as it doesn't make my language looks like Perl. Now get off my lawn! From dilara.ally at gmail.com Wed Dec 28 17:51:25 2011 From: dilara.ally at gmail.com (Dilara Ally) Date: Wed, 28 Dec 2011 14:51:25 -0800 Subject: importing MySQLdb Message-ID: <4EFB9D6D.3080506@gmail.com> Hi All I'm trying to import the MySQLdb for python. I downloaded the proper setuptools egg (ver2.7) for a Mac with OSX10.6 I then downloaded the MySQL-python-1.2.3 and ran the following commands python setup.py build sudo python setup.py install Then to test that the module was properly loaded I used the interactive python prompt ad ran the following command: import MySQLdb The error message read: Traceback (most recent call last): File "", line 1, in File "MySQLdb/__init__.py", line 19, in import _mysql File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 7, in File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 6, in __bootstrap__ ImportError: dlopen(/Users/dally/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Users/dally/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so Reason: image not found Does anyone have any ideas on how to fix this problem? Any help will be greatly appreciated! Dilara -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Dec 28 17:54:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Dec 2011 22:54:16 GMT Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Dec 2011 11:36:17 -0800, Rick Johnson wrote: > The point is people, we should be using string delimiters that are > ANYTHING besides " and '. Stop being a sheep and use your brain! "ANYTHING", hey? I propose we use ? and ? as the opening and closing string delimiters. Problem solved! Thank you Rick for yet another brilliant, well-thought-out idea. I look forward to seeing your fork of Python with this change. How is it going? I hope you aren't going to disappoint the legions of your fans who are relying on you to save the Python community from neglect. -- Steven From rosuav at gmail.com Wed Dec 28 18:08:07 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 10:08:07 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 29, 2011 at 9:54 AM, Steven D'Aprano wrote: > Thank you Rick for yet another brilliant, well-thought-out idea. I look > forward to seeing your fork of Python with this change. How is it going? > I hope you aren't going to disappoint the legions of your fans who are > relying on you to save the Python community from neglect. But Steven, the shocking state of IDLE has utterly destroyed any reputation Python may have had. There's not going to be any community left to neglect, soon! That said, though, the new string delimiters will solve many problems. We should adopt a strict policy: all syntactic elements MUST be comprised of non-ASCII characters, thus allowing all ASCII characters to represent themselves literally. I'm not sure what "representing themselves literally" would mean, since strings already have perfect delimiters, but it seems like a good policy. By the way, doubling the delimiter works for nesting strings. ?? and ?? look just fine. ChrisA From rozelak at volny.cz Wed Dec 28 18:32:27 2011 From: rozelak at volny.cz (rozelak at volny.cz) Date: Thu, 29 Dec 2011 00:32:27 +0100 (CET) Subject: =?iso-8859-2?Q?Re:_Slices_when_extending_python_with_C=2B=2B?= In-Reply-To: References: <18d00d51ecfd54dcfeb18e1708f6c77d@mail2.volny.cz> Message-ID: <0c601b98d7f6993c45d7cf533f1f8b43@mail2.volny.cz> Great, it's working! Thank you very much, Robert! Dan T. ----- P?VODN? ZPR?VA ----- Od: "Robert Kern" Komu: python-list at python.org P?edm?t: Re: Slices when extending python with C++ Datum: 28.12.2011 - 14:18:36 > On 12/28/11 1:01 PM, rozelak at volny.cz wrote: > > Dear Robert, > > > > thank you very much for your answer. I > > understand what you mean and > > > I have looked at slice object and C-api methods > > it provides. It > > > should be easy to implement it. > > > > The only question is how exactly yo implement > > the general getter, > > > since sq_item you mention (assume you mean > > PySequenceMethods.sq_item) has the following > > signature: > > > > > PyObject * (* ssizeargfunc)(PyObject *, > > Py_ssize_t) > > > > > accepting Py_ssize_t as the index, not a > > PyObject * which would hold > > > the slice. > > > > So, how exactly to implement the getter? As a > > general method named > > > __getitem__ registered in PyMethodDef? Or in > > another way? > > > Sorry, PyMappingMethods.mp_subscript is the > general function that you need to > implement. > > -- > 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 > -- Tradi?n? i modern? adventn? a novoro?n? zvyky, sv?te?n? j?dlo a pit?, v?zdoba a d?rky... - ?t?te v?no?n? a silvestrovsk? speci?l port?lu VOLN?.cz na http://web.volny.cz/data/click.php?id=1301 From rosuav at gmail.com Wed Dec 28 19:27:21 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 11:27:21 +1100 Subject: Get Class properties In-Reply-To: References: Message-ID: On Wed, Dec 28, 2011 at 11:13 PM, Emeka wrote: > Hello All, > > I have seen what I am looking for.. __dict__. > Yep! You may also want to look at the dir() function. Chris Angelico From dan at tombstonezero.net Wed Dec 28 20:44:18 2011 From: dan at tombstonezero.net (Dan Sommers) Date: Thu, 29 Dec 2011 01:44:18 +0000 (UTC) Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, 28 Dec 2011 22:54:16 +0000, Steven D'Aprano wrote: > On Wed, 28 Dec 2011 11:36:17 -0800, Rick Johnson wrote: > >> The point is people, we should be using string delimiters that are >> ANYTHING besides " and '. Stop being a sheep and use your brain! > > "ANYTHING", hey? > > I propose we use ? and ? as the opening and closing string delimiters. > Problem solved! Why stop at pre-determined, literal delimiters? That's way too easy on the parser, whether that parser is a computer or a person. __string_delimiter__ = ? # magic syntax; no quotes needed x = ?hello? __string_delimiter__ = # # that first # isn't a comment marker x = # this isn't a comment; it's part of the string this is a multi-line string # __string_delimiter__ = '' # the delimiter is delimited by whitespace # now I can have comments, again, too x = ''"hello"'' # now x contains two double-quote characters I'm sure that the implementation is trivial, and it's so much easier to write strings that contain quotes (not to mention how easy it is to read those strings back later). -- Dan From mobilebackup77 at gmail.com Wed Dec 28 21:35:39 2011 From: mobilebackup77 at gmail.com (Ram) Date: Wed, 28 Dec 2011 18:35:39 -0800 (PST) Subject: logging.getLogger( __name__ ) Message-ID: <2ccfda7d-ac94-4c51-bf64-513bc69f04d6@f1g2000yqi.googlegroups.com> How does this line work? How do I get my logger to point to a file to be named as /tmp/modulename.log : I can do this using inspect, but there probably is a better way? Thanks, --Ram From wolftracks at invalid.com Wed Dec 28 22:04:13 2011 From: wolftracks at invalid.com (W. eWatson) Date: Wed, 28 Dec 2011 19:04:13 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/28/2011 12:55 PM, W. eWatson wrote: > On 12/28/2011 9:37 AM, Ian Kelly wrote: >> On Wed, Dec 28, 2011 at 9:33 AM, W. eWatson >> wrote: >>> Well, thing went slightly awry. The link gave me two choices. Download >>> msv...dll fixer, and download fixer. I took the latter. However, just >>> checking on the other one, found that I got the same exe file. I >>> installed >>> it on the XP PC, and pressed what looked like a reasonable place to >>> start. >>> It would download the dll, and register it. >>> >>> Well, it seemed more interested in the registry. It scanned four >>> areas of >>> the registry, and found 123 problems in total. To fix them would require >>> buying something. >>> >>> I noticed a large button near the top that said download dll. It >>> found one >>> the internet, and guess what? More purchase for the download. >>> >>> It seems like dll-fixer has a corner on the market. >> >> Definitely don't download that spamware fixer program -- who knows >> what that does? You want the grey "Download zip-file" link, not the >> not the flashy "Download fixer" ad links. Anyway, it sounds like >> you've found another copy of the DLL. >> >> If I were you, I would uninstall that fixer ASAP and then run an >> anti-malware and anti-virus tool or two, just in case. >> >>> Run the sfc /scannow System File Checker command to replace a missing or >>> corrupt copy of the msvcp71.dll file. If this DLL file is provided my >>> Microsoft, the System File Checker tool should restore it. >> >> I don't believe it is provided by Microsoft, but it wouldn't hurt to try. >> >>> A new dilemma. The PC XP in question with Python has the the msvcp71.dll >>> file in System32. The one I took off my other laptop has a slightly >>> newer >>> one. Feb 2003 vs Aug 2003. >> >> Weird. Try registering the existing dll, try replacing it with the >> other one (be sure to back up the original first), etc. > > I haven't installed the newer version yet, Aug 2003. I thought I'd see > what happened if I entered sfc /scannow. Whatever, happened the black > window flashed by in a split second. > > Somehow this doesn't seem helpful > . > Although, 20 lines down or so it says: > If sfc discovers that a protected file has been overwritten, it > retrieves the correct version of the file from the > %systemroot%\system32\dllcache folder, and then replaces the incorrect > file. > > I'm going to run this by a XP NG. > > That certainly didn't help. I'm going to save the msvcp71.dll from the Python laptop somewhere, then insert the one from my other XP laptop. Finally, I'll register it. regsvr32. Just to review, I presume not from cmd, but Run? From benjamin.kaplan at case.edu Wed Dec 28 23:02:21 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 28 Dec 2011 23:02:21 -0500 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On Wed, Dec 28, 2011 at 10:04 PM, W. eWatson wrote: > On 12/28/2011 12:55 PM, W. eWatson wrote: >> >> On 12/28/2011 9:37 AM, Ian Kelly wrote: >>> >>> On Wed, Dec 28, 2011 at 9:33 AM, W. eWatson >>> wrote: >>>> >>>> Well, thing went slightly awry. The link gave me two choices. Download >>>> msv...dll fixer, and download fixer. I took the latter. However, just >>>> checking on the other one, found that I got the same exe file. I >>>> installed >>>> it on the XP PC, and pressed what looked like a reasonable place to >>>> start. >>>> It would download the dll, and register it. >>>> >>>> Well, it seemed more interested in the registry. It scanned four >>>> areas of >>>> the registry, and found 123 problems in total. To fix them would require >>>> buying something. >>>> >>>> I noticed a large button near the top that said download dll. It >>>> found one >>>> the internet, and guess what? More purchase for the download. >>>> >>>> It seems like dll-fixer has a corner on the market. >>> >>> >>> Definitely don't download that spamware fixer program -- who knows >>> what that does? You want the grey "Download zip-file" link, not the >>> not the flashy "Download fixer" ad links. Anyway, it sounds like >>> you've found another copy of the DLL. >>> >>> If I were you, I would uninstall that fixer ASAP and then run an >>> anti-malware and anti-virus tool or two, just in case. >>> >>>> Run the sfc /scannow System File Checker command to replace a missing or >>>> corrupt copy of the msvcp71.dll file. If this DLL file is provided my >>>> Microsoft, the System File Checker tool should restore it. >>> >>> >>> I don't believe it is provided by Microsoft, but it wouldn't hurt to try. >>> >>>> A new dilemma. The PC XP in question with Python has the the msvcp71.dll >>>> file in System32. The one I took off my other laptop has a slightly >>>> newer >>>> one. Feb 2003 vs Aug 2003. >>> >>> >>> Weird. Try registering the existing dll, try replacing it with the >>> other one (be sure to back up the original first), etc. >> >> >> I haven't installed the newer version yet, Aug 2003. I thought I'd see >> what happened if I entered sfc /scannow. Whatever, happened the black >> window flashed by in a split second. >> >> Somehow this doesn't seem helpful >> >> . >> Although, 20 lines down or so it says: >> If sfc discovers that a protected file has been overwritten, it >> retrieves the correct version of the file from the >> %systemroot%\system32\dllcache folder, and then replaces the incorrect >> file. >> >> I'm going to run this by a XP NG. >> >> > That certainly didn't help. > > I'm going to save the msvcp71.dll from the Python laptop somewhere, then > insert the one from my other XP laptop. Finally, I'll register it. regsvr32. > Just to review, I presume not from cmd, but Run? It should work from either but I prefer to use cmd in case there's an error message. From torriem at gmail.com Thu Dec 29 00:09:59 2011 From: torriem at gmail.com (Michael Torrie) Date: Wed, 28 Dec 2011 22:09:59 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: <4EFBF627.6090909@gmail.com> On 12/28/2011 08:04 PM, W. eWatson wrote: > I'm going to save the msvcp71.dll from the Python laptop somewhere, then > insert the one from my other XP laptop. Finally, I'll register it. > regsvr32. Just to review, I presume not from cmd, but Run? Seems like the bulk of your problems are coming from not using cmd (flashing black error windows). Anything non-GUI that could return an error message should be done from cmd. The Run dialog is mainly to be used to launch a cmd window! From wolftracks at invalid.com Thu Dec 29 00:56:59 2011 From: wolftracks at invalid.com (W. eWatson) Date: Wed, 28 Dec 2011 21:56:59 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/28/2011 9:09 PM, Michael Torrie wrote: > On 12/28/2011 08:04 PM, W. eWatson wrote: >> I'm going to save the msvcp71.dll from the Python laptop somewhere, then >> insert the one from my other XP laptop. Finally, I'll register it. >> regsvr32. Just to review, I presume not from cmd, but Run? > > Seems like the bulk of your problems are coming from not using cmd > (flashing black error windows). Anything non-GUI that could return an > error message should be done from cmd. The Run dialog is mainly to be > used to launch a cmd window! > it appears more than just regsvr32 is need. Alone it fails with a msg. It needs an argument. I tried regsvr32 msvcp71.dll It didn't like that either. Got "msvcp71.dll was located but the dll server entry was not found. File could not be registered." Arguments are /u,/s/i/n. From prabhadigi2 at gmail.com Thu Dec 29 01:47:09 2011 From: prabhadigi2 at gmail.com (prabhakaran k) Date: Wed, 28 Dec 2011 22:47:09 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: <62cb12ae-98a1-41cb-9b1b-01d19ef305b6@g19g2000pri.googlegroups.com> Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From prabhadigi2 at gmail.com Thu Dec 29 01:47:14 2011 From: prabhadigi2 at gmail.com (prabhakaran k) Date: Wed, 28 Dec 2011 22:47:14 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From rosuav at gmail.com Thu Dec 29 02:37:25 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 18:37:25 +1100 Subject: Which libraries for Python 2.5.2 In-Reply-To: <4EFBF627.6090909@gmail.com> References: <4EFBF627.6090909@gmail.com> Message-ID: On Thu, Dec 29, 2011 at 4:09 PM, Michael Torrie wrote: > The Run dialog is mainly to be > used to launch a cmd window! On any Windows computer that I use, that's strictly true. The only program I ever Start|Run is cmd. ChrisA From lars at rational-it.com Thu Dec 29 04:55:02 2011 From: lars at rational-it.com (lars van gemerden) Date: Thu, 29 Dec 2011 01:55:02 -0800 (PST) Subject: pickling instances of metaclass generated classes Message-ID: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> Hello, Can someone help me with the following: I am using metaclasses to make classes and these classes to make instances. Now I want to use multiprocessing, which needs to pickle these instances. Pickle cannot find the class definitions of the instances. I am trying to add a line to the __new__ of the metaclass to add the new class under the right name in the right module/place, so pickle can find it. Is this the right approach? Can anyone explain to me where/how to add these classes for pickle to find and maybe why? Thanks in advance, Lars From robert.kern at gmail.com Thu Dec 29 06:08:29 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 29 Dec 2011 11:08:29 +0000 Subject: pickling instances of metaclass generated classes In-Reply-To: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> Message-ID: On 12/29/11 9:55 AM, lars van gemerden wrote: > Hello, > > Can someone help me with the following: > > I am using metaclasses to make classes and these classes to make > instances. Now I want to use multiprocessing, which needs to pickle > these instances. > > Pickle cannot find the class definitions of the instances. I am trying > to add a line to the __new__ of the metaclass to add the new class > under the right name in the right module/place, so pickle can find > it. > > Is this the right approach? Can anyone explain to me where/how to add > these classes for pickle to find and maybe why? Can you post some code (preferably pared down to a minimal example that fails)? I'm not really clear on what you are doing. I would expect that a class defined by a class statement would usually work fine unless if the metaclass is doing something particularly weird to it. In any case, you can probably just explicitly register a reduction function for each type using copy_reg.pickle(): http://docs.python.org/library/copy_reg -- 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 From kenzo.zombie at gmail.com Thu Dec 29 06:48:28 2011 From: kenzo.zombie at gmail.com (Sayantan Datta) Date: Thu, 29 Dec 2011 17:18:28 +0530 Subject: Help in rotate 13 program Message-ID: please help me, this code doesn't work, the output file comes out to be empty. How do i fix it? and where is it going wrong? ==============rot13.py============== #!/usr/bin/env python import sys import string CHAR_MAP=dict(zip(string.ascii_lowercase, string.ascii_lowercase[13:26] + string.ascii_lowercase[0:13])) def rotate13_letter(letter) : """ Return the 13-char rotation of a letter """ do_upper = False if letter.isupper() : do_upper = True letter = letter.lower() if letter not in CHAR_MAP : return letter else : letter = CHAR_MAP[letter] if do_upper : letter=letter.upper() return letter if __name__ == '__main__' : for line in sys.stdin : for char in line : sys.stdout.write(rotate13_letter(char)) ===========sample.html=========== Hello, World!

Hi there, all of you earthlings

Take us to your leader.

===============TERMINAL============ this is what i run in the terminal, cat sample.html | python rot13.py rot13.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Thu Dec 29 06:55:14 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Thu, 29 Dec 2011 03:55:14 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> Message-ID: On Dec 28, 11:29?pm, Lie Ryan wrote: > On 12/28/2011 11:08 PM, Eelco wrote: > > > I personally feel any performance benefits are but a plus; they are > > not the motivating factor for this idea. I simply like the added > > verbosity and explicitness, thats the bottom line. > > Any performance benefits are a plus, I agree, as long as it doesn't make > my language looks like Perl. Now get off my lawn! Im no perl expert, but it says on the wikipedia page a common criticism is its overuse of otherwise meaningless special characters; and I would agree; I puked a little in my mouth looking at the code samples. I would argue that the use of single special characters to signal a relatively complex and uncommon construct is exactly what I am trying to avoid with this proposal. From robert.kern at gmail.com Thu Dec 29 07:04:21 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 29 Dec 2011 12:04:21 +0000 Subject: Help in rotate 13 program In-Reply-To: References: Message-ID: On 12/29/11 11:48 AM, Sayantan Datta wrote: > cat sample.html | python rot13.py rot13.html cat sample.html | python rot13.py > rot13.html -- 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 From rosuav at gmail.com Thu Dec 29 07:04:31 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 23:04:31 +1100 Subject: Help in rotate 13 program In-Reply-To: References: Message-ID: On Thu, Dec 29, 2011 at 10:48 PM, Sayantan Datta wrote: > ? ? for line in sys.stdin : > ? ? ? for char in line : > sys.stdout.write(rotate13_letter(char)) > > cat sample.html | python rot13.py rot13.html You're reading from stdin, which is correct, but you're writing to stdout and not redirecting it. You need to put an arrow before rot13.html to indicate redirection: cat sample.html | python rot13.py >rot13.html Note though that 'cat' is superfluous here; all you need to do is redirect input: python rot13.py rot13.html Hope that helps! Chris Angelico From ben+python at benfinney.id.au Thu Dec 29 07:53:21 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 29 Dec 2011 23:53:21 +1100 Subject: logging.getLogger( __name__ ) References: <2ccfda7d-ac94-4c51-bf64-513bc69f04d6@f1g2000yqi.googlegroups.com> Message-ID: <87zkeb35fi.fsf@benfinney.id.au> Ram writes: > How does this line work? What line? Please present some code in an example that we can run. Make the example as small as possible so it's clear what you are asking about. -- \ ?See, in my line of work you gotta keep repeating things over | `\ and over and over again, for the truth to sink in; to kinda | _o__) catapult the propaganda.? ?George W. Bush, 2005-05 | Ben Finney From __peter__ at web.de Thu Dec 29 08:10:16 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 29 Dec 2011 14:10:16 +0100 Subject: Help in rotate 13 program References: Message-ID: Sayantan Datta wrote: > please help me, this code doesn't work, > the output file comes out to be empty. How do i fix it? and where is it > going wrong? I don't know if that's a copy-n-paste error: the indentation of > if __name__ == '__main__' : > for line in sys.stdin : > for char in line : > sys.stdout.write(rotate13_letter(char)) should be if __name__ == '__main__': for line in sys.stdin: for char in line: sys.stdout.write(rotate13_letter(char)) From steve+comp.lang.python at pearwood.info Thu Dec 29 08:23:24 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 29 Dec 2011 13:23:24 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> Message-ID: <4efc69cc$0$29966$c3e8da3$5496439d@news.astraweb.com> On Thu, 29 Dec 2011 03:55:14 -0800, Eelco wrote: > I would argue that the use of single special characters to signal a > relatively complex and uncommon construct is exactly what I am trying to > avoid with this proposal. This would be the proposal to change the existing head, *tail = sequence to your proposed: head, tail:: = ::sequence (when happy with the default list for tail), or head, tail::tuple = ::sequence to avoid an explicit call to "tail = tuple(tail)" after the unpacking. Either way, with or without an explicit type declaration on the left hand side, you are increasing the number of punctuation characters from one to four. If your aim is to minimize the number of punctuation characters, you're doing it wrong. -- Steven From jerome at jolimont.fr Thu Dec 29 08:55:33 2011 From: jerome at jolimont.fr (=?UTF-8?B?SsOpcsO0bWU=?=) Date: Thu, 29 Dec 2011 14:55:33 +0100 Subject: Which library for audio playback ? Message-ID: <20111229145533.23a472bc@bouzin.lan> I'm writing a small application that plays sound through the speakers. The sounds are juste sine waves of arbitrary frequency I create in the code, not sample .wav files. I didn't expect the choice for an audio library to be that complicated. There are several libraries, and none of them seems to be *the* reference. Searching the web, I found these resources : http://wiki.python.org/moin/Audio http://wiki.python.org/moin/PythonInMusic * I privileged ALSA to OSS as I read elsewhere that OSS is deprecated, or on its way to be. (And JACK is not widely installed, apart from specific applications (audio work).) * Then, I picked the alsaaudio library (http://pyalsaaudio.sourceforge.net/). I don't remember exactly why. I think the project had the most recent updates. I don't think any project claims to be (let alone aims at being) complete. I'm wondering if I made a sensible choice. There are other libraries, including the following two that are platform independent : * PyAudiere (http://pyaudiere.org/), OSS , not packaged for debian * PyAudio (http://people.csail.mit.edu/hubert/pyaudio/) What solution would you recommend ? Are there other criterions I should take into account ? Thanks. -- J?r?me From alec.taylor6 at gmail.com Thu Dec 29 09:03:53 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 30 Dec 2011 01:03:53 +1100 Subject: Good notation for showing MVC interactions (i.e. Django) Message-ID: Good morning, I'm developing various websites and functionality to cater to various different use-cases up in Django. Is there a good notation for showing what behaviour is at each stage, i.e. using swimlanes? I use BPMN 2 notation for everything, but I feel I am overusing this, and that there would be a more suitable one for this. Thanks for all suggestions, Alec Taylor From hoogendoorn.eelco at gmail.com Thu Dec 29 09:20:11 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Thu, 29 Dec 2011 06:20:11 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> <4efc69cc$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1673c1c1-b9ed-4860-9c7d-108554a03ec9@d10g2000vbk.googlegroups.com> On Dec 29, 2:23?pm, Steven D'Aprano wrote: > On Thu, 29 Dec 2011 03:55:14 -0800, Eelco wrote: > > I would argue that the use of single special characters to signal a > > relatively complex and uncommon construct is exactly what I am trying to > > avoid with this proposal. > > This would be the proposal to change the existing > > ? ? head, *tail = sequence > > to your proposed: > > ? ? head, tail:: = ::sequence > > (when happy with the default list for tail), or > > ? ? head, tail::tuple = ::sequence > > to avoid an explicit call to "tail = tuple(tail)" after the unpacking. > > Either way, with or without an explicit type declaration on the left hand > side, you are increasing the number of punctuation characters from one to > four. If your aim is to minimize the number of punctuation characters, > you're doing it wrong. > > -- > Steven The goal is not to minimize the number of (special) characters to type. To goal is to minimize the number of special characters which are hard to interpret at a glance. I would prefer : over ::, but both are a single special character construct. Adding those characters once more on the rhs is similarly, not an increase in the number of concepts employed; merely a more explicit form of the same construct. And besides, I dont much like 'head, tail:: = ::sequence'. I threw that out there to appease the terseness advocates, but to me it largely defeats the purpose, because indeed it is hardly any different from the original. I like the explicit mentioning of the collection type to be constructed; that is what really brings it more towards 'for line in file' explicit obviousness to my mind. From tinnews at isbd.co.uk Thu Dec 29 09:36:05 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Thu, 29 Dec 2011 14:36:05 +0000 Subject: Where does this readOne() method come from? References: Message-ID: Chris Angelico wrote: > On Thu, Dec 29, 2011 at 6:04 AM, wrote: > > In the (rather sparse) documentation for the vobject package it has, > > in the section about parsing iCalendar objects, the following:- > > > > ? ?>>> parsedCal = vobject.readOne(icalstream) > > Presumably you have this vobject package. Assuming it's installed > correctly, all you need to do is: > > import vobject > > and then vobject.readOne should be available. > Digging harder into the epydoc documentation I finally found vobject.vobject.readOne(). However having twice been befuddled by vobject, once when using it for vCard and this time using it for vCal I've decided to move back to the python-icalendar I was using before. The only reason I tried to use vobject is that it's in the Ubuntu repositories (I doing this on an xubuntu system), but it doesn't appear to have a current maintainer and python-icalendar does so even though it's a 'manual' install I'm sticking with python-icalendar. -- Chris Green From roy at panix.com Thu Dec 29 09:44:04 2011 From: roy at panix.com (Roy Smith) Date: Thu, 29 Dec 2011 09:44:04 -0500 Subject: Good notation for showing MVC interactions (i.e. Django) References: Message-ID: In article , Alec Taylor wrote: > Is there a good notation for showing what behaviour is at each stage, > i.e. using swimlanes? > > I use BPMN 2 notation for everything, but I feel I am overusing this, > and that there would be a more suitable one for this. Swimlanes? You mean like http://tinyurl.com/caqf3h5 ? Never used them. Never used BPMN either. I've found most of these heavyweight modeling tools to be more of a hinderance than a help. People get all wrapped up in making pretty diagrams when they should be spending more time writing code. I find that when thinking something through at the whiteboard with another developer, the most common diagram I'll use is a UML sequence diagram. I drew one on the wall just yesterday. Here's one (http://www.panix.com/~roy/SongzaSessionHandoff.pdf) I did up a bit fancier a while ago while working through a gnarly design problem. As with most things in UML, I find the basic concept useful and don't sweat the details of what different shaped arrowheads mean or what flavor of rectangle I'm supposed to be drawing. From ukim86 at gmail.com Thu Dec 29 11:07:11 2011 From: ukim86 at gmail.com (Prim) Date: Thu, 29 Dec 2011 08:07:11 -0800 (PST) Subject: About instance.name look up order Message-ID: First, sorry about my poor english. Put these in IPython under ubuntu. ------------------------------------------------------------------------------------- class C: def __init__(self): self.x = 1 def print(self): print self.x c = C() c.x --> 1, so c.x mean a attr of c named 'x' c.print() --> pirnt 1, so c.print mean a method of c named 'print' ------------------------------------------------------------------------------------- class C: def __init__(self): self.x = 1 def x(self): print 'x method' def y(self): print 'y method' c = C() c.x --> 1 c.x() --> TypeError: 'int' object is not callable c.y --> bound method C.y #Q1: instance.name will get the attr first, than method? ------------------------------------------------------------------------------------- class C: def x(self): print 'x method' def __getattr__(self, attr): print 'in __getattr__ method' return attr c = C() c.x --> print in __getattr__ method, then throw TypeError: 'str' object is not callable c.x() --> print in __getattr__ method, x method 2 lines #Q2: why c.x would get a exception? t = c.x t --> print in __getattr__ method, then throw TypeError: 'str' object is not callable t() --> print x method t = c.x() --> print x method, t == None #Q3 why t=c.x() and c.x() output different? #Q4, if when I define the class use property too, then instance.name look up order would be? Thanks for your reply. From alec.taylor6 at gmail.com Thu Dec 29 11:07:17 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 30 Dec 2011 03:07:17 +1100 Subject: Good notation for showing MVC interactions (i.e. Django) In-Reply-To: References: Message-ID: An example of a BPMN2 diagram with swimlanes, which I created for a project: http://i40.tinypic.com/262r6nr.jpg What I am looking for is something suited towards showing architecture bounds and the interactions between each section of MVC. > I find that when thinking something through at the whiteboard with > another developer, the most common diagram I'll use is a UML sequence > diagram. I drew one on the wall just yesterday. Here's one > (http://www.panix.com/~roy/SongzaSessionHandoff.pdf) I did up a bit > fancier a while ago while working through a gnarly design problem. > That's a good notation, and one which I will use for RESTful API modelling. I sometimes use use-case diagrams and quite rarely also use class diagrams and ER Diagrams. I use BPMN2 to show logic to both technical and non-technical team-members. From emekamicro at gmail.com Thu Dec 29 11:09:45 2011 From: emekamicro at gmail.com (Emeka) Date: Thu, 29 Dec 2011 17:09:45 +0100 Subject: Get Class properties In-Reply-To: References: Message-ID: Chris, Thanks a million! Regards, Emeka On Thu, Dec 29, 2011 at 1:27 AM, Chris Angelico wrote: > On Wed, Dec 28, 2011 at 11:13 PM, Emeka wrote: > > Hello All, > > > > I have seen what I am looking for.. __dict__. > > > > Yep! You may also want to look at the dir() function. > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list > -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From kenzo.zombie at gmail.com Thu Dec 29 11:25:50 2011 From: kenzo.zombie at gmail.com (Sayantan Datta) Date: Thu, 29 Dec 2011 21:55:50 +0530 Subject: Help in rotate 13 program In-Reply-To: References: Message-ID: On Thu, Dec 29, 2011 at 5:34 PM, Chris Angelico wrote: > On Thu, Dec 29, 2011 at 10:48 PM, Sayantan Datta > wrote: > > for line in sys.stdin : > > for char in line : > > sys.stdout.write(rotate13_letter(char)) > > > > cat sample.html | python rot13.py rot13.html > > You're reading from stdin, which is correct, but you're writing to > stdout and not redirecting it. You need to put an arrow before > rot13.html to indicate redirection: > > cat sample.html | python rot13.py >rot13.html > > Note though that 'cat' is superfluous here; all you need to do is > redirect input: > python rot13.py rot13.html > > Hope that helps! > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list > did that, but the output file is still empty? Does the fault lie somewhere else? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu Dec 29 11:32:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Dec 2011 03:32:55 +1100 Subject: Help in rotate 13 program In-Reply-To: References: Message-ID: On Fri, Dec 30, 2011 at 3:25 AM, Sayantan Datta wrote: > did that, but the output file is still empty? Does the fault lie somewhere > else? I'm not sure if it's a copy-paste problem, but check your indentation: your "if __name__" line seems to be indented, it should be flush left. Indentation makes it part of the function, so your script will define a function and never call it. ChrisA From d at davea.name Thu Dec 29 11:39:29 2011 From: d at davea.name (Dave Angel) Date: Thu, 29 Dec 2011 11:39:29 -0500 Subject: Help in rotate 13 program In-Reply-To: References: Message-ID: <4EFC97C1.2010908@davea.name> On 12/29/2011 11:25 AM, Sayantan Datta wrote: > On Thu, Dec 29, 2011 at 5:34 PM, Chris Angelico wrote: > >> On Thu, Dec 29, 2011 at 10:48 PM, Sayantan Datta >> wrote: >>> for line in sys.stdin : >>> for char in line : >>> sys.stdout.write(rotate13_letter(char)) >>> >>> cat sample.html | python rot13.py rot13.html >> You're reading from stdin, which is correct, but you're writing to >> stdout and not redirecting it. You need to put an arrow before >> rot13.html to indicate redirection: >> >> cat sample.html | python rot13.py>rot13.html >> >> Note though that 'cat' is superfluous here; all you need to do is >> redirect input: >> python rot13.pyrot13.html >> >> Hope that helps! >> >> Chris Angelico >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > did that, but the output file is still empty? Does the fault lie somewhere > else? > Both Peter and Chris pointed out that you have the if __name__ == "__main__" line indented. If that's true in your actual file, then the program does nothing useful. Why not run it without output redirection, and see what it displays? And stick an unindented print line in there, just to see it do something. -- DaveA From wolftracks at invalid.com Thu Dec 29 12:23:49 2011 From: wolftracks at invalid.com (W. eWatson) Date: Thu, 29 Dec 2011 09:23:49 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/29/2011 6:54 AM, Dennis Lee Bieber wrote: > On Wed, 28 Dec 2011 21:56:59 -0800, "W. eWatson" > wrote: > > >> It didn't like that either. Got "msvcp71.dll was located but the dll >> server entry was not found. File could not be registered." >> >> Arguments are /u,/s/i/n. > > According to the pop-up I get (Win7) when supplying an invalid > argument, > > /n - do not call DllRegisterServer; this option must be used with /i > and that reads > /i - Call DllInstall passing it an optional [cmdline]; when used > with /u calls dll uninstall > > So.. If the register server entry point was not found, maybe you > should try with /i/n to make it call the /other/ registration entry > point. Well, it's worth a try. However, what "other" entry point. > > > > {And, in line with the other commentators; I don't even use start/run > for the command shell -- I use the shell often enough to have put a > dedicated shortcut on the start menu itself (and have since added > PowerShell shortcuts). Only thing I use start/run with is the registry > editor (regedt32 or regedit) and to start the despised Internet Explorer > (iexplore, which has otherwise been removed from direct mouse access) -- > in short; only programs with GUI interfaces get started from start/run} To keep it simple, I now just enter cmd, and work there. I'm not usually working with these low level operations. Occasionally, I use some of the network commands. From wolftracks at invalid.com Thu Dec 29 12:26:59 2011 From: wolftracks at invalid.com (W. eWatson) Date: Thu, 29 Dec 2011 09:26:59 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/29/2011 9:04 AM, Dennis Lee Bieber wrote: > On Thu, 29 Dec 2011 09:54:48 -0500, Dennis Lee Bieber > wrote: > > Talking to myself.. It's the best way to get people to listen to you. :-) > >> >> According to the pop-up I get (Win7) when supplying an invalid >> argument, >> > Maybe regsrv32 was rewritten for Win7 64bit -- and assumed folks > would run it from the start dialog (or file search in Win7) since the > error messages do pop-up in windows, even when running in a command > shell. I'm installing Python on an XP PC that's 5 years old. This PC I'm writing from is Win7. ... From ian.g.kelly at gmail.com Thu Dec 29 12:44:54 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 29 Dec 2011 10:44:54 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: <8u6pf7hj0g0vt6k8n0clj3h59e42qjhbqq@4ax.com> References: <8u6pf7hj0g0vt6k8n0clj3h59e42qjhbqq@4ax.com> Message-ID: On Thu, Dec 29, 2011 at 10:04 AM, Dennis Lee Bieber wrote: > On Thu, 29 Dec 2011 09:54:48 -0500, Dennis Lee Bieber > wrote: > > ? ? ? ?Talking to myself.. > >> >> ? ? ? According to the pop-up I get (Win7) when supplying an invalid >>argument, >> > ? ? ? ?Maybe regsrv32 was rewritten for Win7 64bit -- and assumed folks > would run it from the start dialog (or file search in Win7) since the > error messages do pop-up in windows, even when running in a command > shell. As far as I know, it's always been like that. From kevin.p.dwyer at gmail.com Thu Dec 29 13:03:13 2011 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Thu, 29 Dec 2011 18:03:13 +0000 Subject: About instance.name look up order References: Message-ID: Prim wrote: > First, sorry about my poor english. > Put these in IPython under ubuntu. > ------------------------------------------------------------------------------------- > class C: > def __init__(self): > self.x = 1 > def print(self): > print self.x > c = C() > c.x --> 1, so c.x mean a attr of c named 'x' > c.print() --> pirnt 1, so c.print mean a method of c named 'print' > ------------------------------------------------------------------------------------- > class C: > def __init__(self): > self.x = 1 > def x(self): > print 'x method' > def y(self): > print 'y method' > c = C() > c.x --> 1 > c.x() --> TypeError: 'int' object is not callable > c.y --> bound method C.y > #Q1: instance.name will get the attr first, than method? > ------------------------------------------------------------------------------------- > class C: > def x(self): > print 'x method' > def __getattr__(self, attr): > print 'in __getattr__ method' > return attr > c = C() > c.x --> print in __getattr__ method, then throw TypeError: 'str' > object is not callable > c.x() --> print in __getattr__ method, x method 2 lines > #Q2: why c.x would get a exception? > > t = c.x > t --> print in __getattr__ method, then throw TypeError: 'str' object > is not callable > t() --> print x method > t = c.x() --> print x method, t == None > #Q3 why t=c.x() and c.x() output different? > > #Q4, if when I define the class use property too, then instance.name > look up order would be? > > Thanks for your reply. Hello, Python always looks for attributes in the instance first, then in the class, and then in the class's superclasses. In your first example, by defining "x" in C.__init__ you are creating an instance attribute named "x". When the attribute c.x is requested, Python finds an attribute "x" in the instance and returns it; the method "x" is found in the class, but the attribute lookup does not proceed this far. Try looking at C.__dict__ and c.__dict__ in the interpreter to see how the attributes are stored. See also http://docs.python.org/reference/datamodel.html#customizing-attribute-access Cheers Kev From ian.g.kelly at gmail.com Thu Dec 29 13:35:49 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 29 Dec 2011 11:35:49 -0700 Subject: About instance.name look up order In-Reply-To: References: Message-ID: On Thu, Dec 29, 2011 at 9:07 AM, Prim wrote: > class C: > ? ?def x(self): > ? ? ? ?print 'x method' > ? ?def __getattr__(self, attr): > ? ? ? ?print 'in __getattr__ method' > ? ? ? ?return attr > c = C() > c.x --> print in __getattr__ method, then throw TypeError: 'str' > object is not callable > c.x() --> print in __getattr__ method, x method 2 lines > #Q2: why c.x would get a exception? I'm not sure exactly what's going on there, but as a general rule you should use new-style classes, not classic classes, which are deprecated and have been removed in Python 3. In Python 2 if you define a class without any base classes, you get a classic class. To define C as a new-style class in Python 2, derive it from object (or another new-style class): >>> class C(object): ... def x(self): ... print 'x method' ... def __getattr__(self, attr): ... print 'in __getattr__ method' ... return attr ... >>> c = C() >>> c.x > >>> c.x() x method Note that in this case __getattr__ never gets called because the name was found using the normal lookup mechanism. You would need to use __getattribute__ if you want it to be called unconditionally. > #Q4, if when I define the class use property too, then instance.name > look up order would be? You can't use properties in classic classes. In a new-style class, if you access a property, then any instance attribute with the same name is ignored and can't be set in the first place. From wolftracks at invalid.com Thu Dec 29 14:31:17 2011 From: wolftracks at invalid.com (W. eWatson) Date: Thu, 29 Dec 2011 11:31:17 -0800 Subject: Which libraries for Python 2.5.2 [SOLVED] In-Reply-To: References: <8u6pf7hj0g0vt6k8n0clj3h59e42qjhbqq@4ax.com> Message-ID: On 12/29/2011 9:44 AM, Ian Kelly wrote: > On Thu, Dec 29, 2011 at 10:04 AM, Dennis Lee Bieber > wrote: >> On Thu, 29 Dec 2011 09:54:48 -0500, Dennis Lee Bieber >> wrote: >> >> Talking to myself.. >> >>> >>> According to the pop-up I get (Win7) when supplying an invalid >>> argument, >>> >> Maybe regsrv32 was rewritten for Win7 64bit -- and assumed folks >> would run it from the start dialog (or file search in Win7) since the >> error messages do pop-up in windows, even when running in a command >> shell. > > As far as I know, it's always been like that. Well, I thought I'd take the PC to a computer shop to see why when I move windows they shake a bit. The technician took about 2 minutes to show me uninstalled devices under System Devices. He thought they should be fixed. The system was showing lots of device errors. They wanted $75 to repair it, but I told them a friend who will use the PC will have to decide that. This PC belonged to my wife at one time for 5 years. She had put Linux on it, but she no longer needed it. I restored it to XP Home. When I told her where the problems were she found a driver DVD, so I'm using it now to fill in the gaps. Maybe it'll get by the dll problem. It certainly is adding a VGA driver, so I think the window wobbles may get fixed. Well, it now works w/o any difficulty. It was the incomplete install that did it. It now has acrobat, wallpaper, etc, like most XP systems. No more wobbles! Yea! Thanks to all that helped. From ian.g.kelly at gmail.com Thu Dec 29 14:55:04 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 29 Dec 2011 12:55:04 -0700 Subject: pickling instances of metaclass generated classes In-Reply-To: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> Message-ID: On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden wrote: > Hello, > > Can someone help me with the following: > > I am using metaclasses to make classes and these classes to make > instances. Now I want to use multiprocessing, which needs to pickle > these instances. > > Pickle cannot find the class definitions of the instances. I am trying > to add a line to the __new__ of the metaclass to add the new class > under the right name in the right module/place, so pickle can find > it. > > Is this the right approach? Can anyone explain to me where/how to add > these classes for pickle to find and maybe why? It sounds like you're trying to do something like this? >>> class MetaClass(type): ... pass ... >>> instance = MetaClass('', (object,), {})() >>> instance <__main__. object at 0x00CC00F0> >>> import pickle >>> pickle.dumps(instance) Traceback (most recent call last): File "", line 1, in File "c:\python27\lib\pickle.py", line 1374, in dumps Pickler(file, protocol).dump(obj) File "c:\python27\lib\pickle.py", line 224, in dump self.save(obj) File "c:\python27\lib\pickle.py", line 331, in save self.save_reduce(obj=obj, *rv) File "c:\python27\lib\pickle.py", line 401, in save_reduce save(args) File "c:\python27\lib\pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "c:\python27\lib\pickle.py", line 562, in save_tuple save(element) File "c:\python27\lib\pickle.py", line 295, in save self.save_global(obj) File "c:\python27\lib\pickle.py", line 748, in save_global (obj, module, name)) pickle.PicklingError: Can't pickle '>: it's not found as __main__. Yeah, pickle's not going to work with anonymous classes. As you suggest, you could dynamically add the classes to the module namespace so that pickle.dumps will find them, but bear in mind that they will also have to exist when calling pickle.loads, so you will need to be able to reconstruct the same anonymous classes before unpickling later on. Cheers, Ian From merwin.irc at gmail.com Thu Dec 29 15:22:55 2011 From: merwin.irc at gmail.com (Merwin) Date: Thu, 29 Dec 2011 21:22:55 +0100 Subject: PySide / PyQt autocompletion in IDEs Message-ID: <4EFCCC1F.7000605@gmail.com> Hi, I would like to work with PyQt / PySide, but there is a small problem : methods arguments are not completed by IDE's autocompletion. When, typing "PySide.", I correctly get the module's attributes, but when I want to see what arguments are expected to a constructor / method, all I get is *args, **kwargs. I perfectly understand why this happen, but do you know a way to get around this ? I spend more time looking for arguments in Qt's documentation than effectively programming, which is a problem :-/ Thanks for reading, From vim at aryehleib.com Thu Dec 29 15:40:37 2011 From: vim at aryehleib.com (Aryeh Leib Taurog) Date: Thu, 29 Dec 2011 12:40:37 -0800 (PST) Subject: Augmented online documentation Message-ID: <82741962-c597-42fb-9300-e37103bcf63a@d8g2000yqk.googlegroups.com> I have created for myself a small google chrome extension which generates a treeview toc pane of the online python docs as well as the matplotlib docs. This is intended to facilitate doc navigation, much the same way the navigation pane in a chm viewer does. Read more and install it at https://bitbucket.org/altaurog/sphdoctoc/overview enjoy, Aryeh Leib Taurog From lie.1296 at gmail.com Thu Dec 29 18:19:10 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 30 Dec 2011 10:19:10 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/29/2011 12:44 PM, Dan Sommers wrote: > On Wed, 28 Dec 2011 22:54:16 +0000, Steven D'Aprano wrote: > >> On Wed, 28 Dec 2011 11:36:17 -0800, Rick Johnson wrote: >> >>> The point is people, we should be using string delimiters that are >>> ANYTHING besides " and '. Stop being a sheep and use your brain! >> >> "ANYTHING", hey? >> >> I propose we use ? and ? as the opening and closing string delimiters. >> Problem solved! > > Why stop at pre-determined, literal delimiters? That's way too easy on > the parser, whether that parser is a computer or a person. > > __string_delimiter__ = ? # magic syntax; no quotes needed > > x = ?hello? > > __string_delimiter__ = # # that first # isn't a comment marker > > x = # this isn't a comment; it's part of the string > this is a multi-line string > # > > __string_delimiter__ = '' # the delimiter is delimited by whitespace > # now I can have comments, again, too > > x = ''"hello"'' # now x contains two double-quote characters > > I'm sure that the implementation is trivial, and it's so much easier to > write strings that contain quotes (not to mention how easy it is to read > those strings back later). right and you can generalize this idea even further __comment_marker__ = $ __string_delimiter__ = [#, %] $ also, even further __comment_marker_keyword = #__my_own_comment_marker__% __my_own_comment_marker__ = [&, *] __assignment_symbol__ = <- __attribute_delimiter__ <- -> & for attributes * __call_delimiter__ = [!,@] l = [] l->append!5@ you're a genius!! From rosuav at gmail.com Thu Dec 29 18:24:14 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Dec 2011 10:24:14 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 30, 2011 at 10:19 AM, Lie Ryan wrote: > right and you can generalize this idea even further Don't stop generalizing. Everything should be custom. http://rosuav.com/1/?id=683 ChrisA From lie.1296 at gmail.com Thu Dec 29 18:24:57 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 30 Dec 2011 10:24:57 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4efc69cc$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> <4efc69cc$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/30/2011 12:23 AM, Steven D'Aprano wrote: > On Thu, 29 Dec 2011 03:55:14 -0800, Eelco wrote: > >> I would argue that the use of single special characters to signal a >> relatively complex and uncommon construct is exactly what I am trying to >> avoid with this proposal. > > This would be the proposal to change the existing > > head, *tail = sequence > > to your proposed: > > head, tail:: = ::sequence > > (when happy with the default list for tail), or > > head, tail::tuple = ::sequence > > to avoid an explicit call to "tail = tuple(tail)" after the unpacking. > > Either way, with or without an explicit type declaration on the left hand > side, you are increasing the number of punctuation characters from one to > four. If your aim is to minimize the number of punctuation characters, > you're doing it wrong. Another drawback of it is that it looks misleadingly similar to C++ namespace notation. From rosuav at gmail.com Thu Dec 29 18:30:54 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Dec 2011 10:30:54 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> <4efc69cc$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 30, 2011 at 10:24 AM, Lie Ryan wrote: > Another drawback of it is that it looks misleadingly similar to C++ > namespace notation. Granted, but I don't see that as a drawback. The current notation is just as similar to C's pointer-dereference notation, but that hasn't led people to think that tail holds a pointer to the location where something should be stored. This would be a serious concern with notations that are common across many languages (eg "x*y" to mean multiplication, which isn't strictly what mathematics uses), but with language-specific notations, especially such brief ones, it's understood that there'll be duplication. ChrisA From umchaselee at gmail.com Thu Dec 29 20:04:12 2011 From: umchaselee at gmail.com (umchaselee at gmail.com) Date: Thu, 29 Dec 2011 17:04:12 -0800 (PST) Subject: db agnostic admin for Flask? In-Reply-To: <21fa456b-67ef-4c00-af59-e3d238302b2e@t36g2000prj.googlegroups.com> References: <21fa456b-67ef-4c00-af59-e3d238302b2e@t36g2000prj.googlegroups.com> Message-ID: <11135484.2432.1325207052620.JavaMail.geo-discussion-forums@yqmw40> I was considering building an admin that would consist of two components, a totally separate HTML/CSS/JS frontend that presumed API endpoints, and a backend written in Tornado (similar to Flask in a lot of ways) that caught all of the presumed endpoints and performed the db operations. The idea of separating them is to make the frontend portable across backends so that a backend could be written in Tornado/Flask/Django/RoR(*shudder*)/whatever you want as long as it followed the conventions leading to the API endpoints. I've seen a couple backends out there but haven't been too happy with any of them, and I haven't found any doing something like this. From kenzo.zombie at gmail.com Thu Dec 29 23:31:05 2011 From: kenzo.zombie at gmail.com (Sayantan Datta) Date: Fri, 30 Dec 2011 10:01:05 +0530 Subject: Help in rotate 13 program In-Reply-To: <4EFC97C1.2010908@davea.name> References: <4EFC97C1.2010908@davea.name> Message-ID: On Thu, Dec 29, 2011 at 10:09 PM, Dave Angel wrote: > On 12/29/2011 11:25 AM, Sayantan Datta wrote: > >> On Thu, Dec 29, 2011 at 5:34 PM, Chris Angelico wrote: >> >> On Thu, Dec 29, 2011 at 10:48 PM, Sayantan Datta >>> wrote: >>> >>>> for line in sys.stdin : >>>> for char in line : >>>> sys.stdout.write(rotate13_**letter(char)) >>>> >>>> cat sample.html | python rot13.py rot13.html >>>> >>> You're reading from stdin, which is correct, but you're writing to >>> stdout and not redirecting it. You need to put an arrow before >>> rot13.html to indicate redirection: >>> >>> cat sample.html | python rot13.py>rot13.html >>> >>> Note though that 'cat' is superfluous here; all you need to do is >>> redirect input: >>> python rot13.pyrot13.**html >>> >>> Hope that helps! >>> >>> Chris Angelico >>> -- >>> http://mail.python.org/**mailman/listinfo/python-list >>> >>> did that, but the output file is still empty? Does the fault lie >> somewhere >> else? >> >> Both Peter and Chris pointed out that you have the if __name__ == > "__main__" line indented. If that's true in your actual file, then the > program does nothing useful. > > Why not run it without output redirection, and see what it displays? And > stick an unindented print line in there, just to see it do something. > > > > -- > > DaveA > > hmm, yes, it was, noticed it right now. Huh, i made such a silly mistake.. Anyway, thanks a lot. :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From p_s_d_a_s_i_l_v_a at netcabo.pt Fri Dec 30 02:17:13 2011 From: p_s_d_a_s_i_l_v_a at netcabo.pt (Paulo da Silva) Date: Fri, 30 Dec 2011 07:17:13 +0000 Subject: Generating sin/square waves sound Message-ID: Hi, Sorry if this is a FAQ, but I have googled and didn't find any satisfatory answer. Is there a simple way, preferably multiplataform (or linux), of generating sinusoidal/square waves sound in python? Thanks for any answers/suggestions. From lars at rational-it.com Fri Dec 30 04:50:26 2011 From: lars at rational-it.com (lars van gemerden) Date: Fri, 30 Dec 2011 01:50:26 -0800 (PST) Subject: pickling instances of metaclass generated classes References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> Message-ID: On Dec 29, 8:55?pm, Ian Kelly wrote: > On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden wrote: > > > Hello, > > > Can someone help me with the following: > > > I am using metaclasses to make classes and these classes to make > > instances. Now I want to use multiprocessing, which needs to pickle > > these instances. > > > Pickle cannot find the class definitions of the instances. I am trying > > to add a line to the __new__ of the metaclass to add the new class > > under the right name in the right module/place, so pickle can find > > it. > > > Is this the right approach? Can anyone explain to me where/how to add > > these classes for pickle to find and maybe why? > > It sounds like you're trying to do something like this? > > >>> class MetaClass(type): > > ... ? ? pass > ...>>> instance = MetaClass('', (object,), {})() > >>> instance > > <__main__. object at 0x00CC00F0>>>> import pickle > >>> pickle.dumps(instance) > > Traceback (most recent call last): > ? File "", line 1, in > ? File "c:\python27\lib\pickle.py", line 1374, in dumps > ? ? Pickler(file, protocol).dump(obj) > ? File "c:\python27\lib\pickle.py", line 224, in dump > ? ? self.save(obj) > ? File "c:\python27\lib\pickle.py", line 331, in save > ? ? self.save_reduce(obj=obj, *rv) > ? File "c:\python27\lib\pickle.py", line 401, in save_reduce > ? ? save(args) > ? File "c:\python27\lib\pickle.py", line 286, in save > ? ? f(self, obj) # Call unbound method with explicit self > ? File "c:\python27\lib\pickle.py", line 562, in save_tuple > ? ? save(element) > ? File "c:\python27\lib\pickle.py", line 295, in save > ? ? self.save_global(obj) > ? File "c:\python27\lib\pickle.py", line 748, in save_global > ? ? (obj, module, name)) > pickle.PicklingError: Can't pickle '>: > it's not found as __main__. > > Yeah, pickle's not going to work with anonymous classes. ?As you > suggest, you could dynamically add the classes to the module namespace > so that pickle.dumps will find them, but bear in mind that they will > also have to exist when calling pickle.loads, so you will need to be > able to reconstruct the same anonymous classes before unpickling later > on. > > Cheers, > Ian Thank you Ian for the minimal example. This is almost the case i was trying to discribe, however the classes will be named at runtime and the class definitions will be persisted in a database. Can you help me with how to add the classes to the correct namespace? The application spans multiple modules (or compared to the example, the metaclass definition will be in another module then one where the class and instance will be generated). Cheers, Lars From dihedral88888 at googlemail.com Fri Dec 30 04:51:06 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 30 Dec 2011 01:51:06 -0800 (PST) Subject: Generating sin/square waves sound In-Reply-To: References: Message-ID: <15235180.160.1325238666252.JavaMail.geo-discussion-forums@prlk31> Please check PYGAME and Simple Directmedia library. Python is used as the director like role and functions in SDL do most of the jobs in Pygame. From d at davea.name Fri Dec 30 05:05:01 2011 From: d at davea.name (Dave Angel) Date: Fri, 30 Dec 2011 05:05:01 -0500 Subject: Generating sin/square waves sound In-Reply-To: References: Message-ID: <4EFD8CCD.9080908@davea.name> On 12/30/2011 02:17 AM, Paulo da Silva wrote: > Hi, > Sorry if this is a FAQ, but I have googled and didn't find any > satisfatory answer. > > Is there a simple way, preferably multiplataform (or linux), of > generating sinusoidal/square waves sound in python? > > Thanks for any answers/suggestions. If you're willing to be Linux-only, then I believe you can do it without any extra libraries. You build up a string (8 bit char, on Python 2.x) of samples, and write it to "/dev/audio". When i experimented, I was only interested in a few seconds, so a single write was all I needed. Note that the samples are 8 bits, and they are offset by 128. So a zero signal would be a string of 128 values. A very quiet square wave might be a bunch of 126, followed by a bunch of 130. and so on. And the loudest might be a bunch of 2's followed by a bunch of 253's. You'll have to experiment with data rate; The data is sent out at a constant rate from your string, but I don't know what that rate is. -- DaveA From nad at acm.org Fri Dec 30 05:45:02 2011 From: nad at acm.org (Ned Deily) Date: Fri, 30 Dec 2011 02:45:02 -0800 Subject: importing MySQLdb References: <4EFB9D6D.3080506@gmail.com> Message-ID: In article <4EFB9D6D.3080506 at gmail.com>, Dilara Ally wrote: > I'm trying to import the MySQLdb for python. I downloaded the proper > setuptools egg (ver2.7) for a Mac with OSX10.6 > > I then downloaded the MySQL-python-1.2.3 and ran the following commands > > python setup.py build > sudo python setup.py install > > Then to test that the module was properly loaded I used the interactive > python prompt ad ran the following command: > > import MySQLdb > > The error message read: > > Traceback (most recent call last): > File "", line 1, in > File "MySQLdb/__init__.py", line 19, in > import _mysql > File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 7, in > File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 6, in > __bootstrap__ > ImportError: > dlopen(/Users/dally/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.eg > g-tmp/_mysql.so, > 2): Library not loaded: libmysqlclient.18.dylib > Referenced from: > /Users/dally/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_ > mysql.so > Reason: image not found > > Does anyone have any ideas on how to fix this problem? Any help will be > greatly appreciated! If you installed the MySQL client libraries from a MySQL binary installer, chances are that the library name in the client library does not contain the full path. The MySQL project has a pretty dismal record for releasing faulty OS X binaries. There are a few workarounds, the best being either to use install_name_tool to fix the path or to set the DYLD_LIBRARY_PATH environment variable. See, for example, this blog post: http://www.blog.bridgeutopiaweb.com/post/how-to-fix-mysql-load-issues-on- mac-os-x/ -- Ned Deily, nad at acm.org From lars at rational-it.com Fri Dec 30 06:16:10 2011 From: lars at rational-it.com (lars van gemerden) Date: Fri, 30 Dec 2011 03:16:10 -0800 (PST) Subject: pickling instances of metaclass generated classes References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> Message-ID: <208b4f8b-c347-44c6-ac9d-73662cc36dbd@o9g2000yqa.googlegroups.com> On Dec 29, 8:55?pm, Ian Kelly wrote: > On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden wrote: > > > Hello, > > > Can someone help me with the following: > > > I am using metaclasses to make classes and these classes to make > > instances. Now I want to use multiprocessing, which needs to pickle > > these instances. > > > Pickle cannot find the class definitions of the instances. I am trying > > to add a line to the __new__ of the metaclass to add the new class > > under the right name in the right module/place, so pickle can find > > it. > > > Is this the right approach? Can anyone explain to me where/how to add > > these classes for pickle to find and maybe why? > > It sounds like you're trying to do something like this? > > >>> class MetaClass(type): > > ... ? ? pass > ...>>> instance = MetaClass('', (object,), {})() > >>> instance > > <__main__. object at 0x00CC00F0>>>> import pickle > >>> pickle.dumps(instance) > > Traceback (most recent call last): > ? File "", line 1, in > ? File "c:\python27\lib\pickle.py", line 1374, in dumps > ? ? Pickler(file, protocol).dump(obj) > ? File "c:\python27\lib\pickle.py", line 224, in dump > ? ? self.save(obj) > ? File "c:\python27\lib\pickle.py", line 331, in save > ? ? self.save_reduce(obj=obj, *rv) > ? File "c:\python27\lib\pickle.py", line 401, in save_reduce > ? ? save(args) > ? File "c:\python27\lib\pickle.py", line 286, in save > ? ? f(self, obj) # Call unbound method with explicit self > ? File "c:\python27\lib\pickle.py", line 562, in save_tuple > ? ? save(element) > ? File "c:\python27\lib\pickle.py", line 295, in save > ? ? self.save_global(obj) > ? File "c:\python27\lib\pickle.py", line 748, in save_global > ? ? (obj, module, name)) > pickle.PicklingError: Can't pickle '>: > it's not found as __main__. > > Yeah, pickle's not going to work with anonymous classes. ?As you > suggest, you could dynamically add the classes to the module namespace > so that pickle.dumps will find them, but bear in mind that they will > also have to exist when calling pickle.loads, so you will need to be > able to reconstruct the same anonymous classes before unpickling later > on. > > Cheers, > Ian Ian also wrote: ''' Actually, I was wrong, you probably don't need to do that. I suggest going with Robert Kern's suggestion to either register the class with the copy_reg module, or (perhaps better since it won't leak registrations) implement a __reduce__ method on the class. For example, this seems to work: >>> def reconstructor(*metaclass_args): ... cls = MetaClass.build_class(*metaclass_args) ... self = cls.__new__(cls) ... return self ... >>> class MetaClass(type): ... @classmethod ... def build_class(mcs, arg1, arg2, arg3): ... # Do something useful with the args... ... class _AnonymousClass(object): ... __metaclass__ = mcs ... def __reduce__(self): ... return (reconstructor, ('foo', 'bar', 'baz'), self.__dict__) ... return _AnonymousClass ... >>> instance = MetaClass.build_class('foo', 'bar', 'baz')() >>> instance <__main__._AnonymousClass object at 0x011DB410> >>> instance.banana = 42 >>> import pickle >>> s = pickle.dumps(instance) >>> s "c__main__\nreconstructor \np0\n(S'foo'\np1\nS'bar'\np2\nS'baz'\np3\ntp4\nRp5\n(dp6\nS'banana'\np7\nI42\nsb." >>> inst2 = pickle.loads(s) >>> inst2 <__main__._AnonymousClass object at 0x011DBE90> >>> inst2.banana 42 >>> inst2.__class__ is instance.__class__ False Cheers, Ian ''' From foobar at invalid.invalid Fri Dec 30 06:23:50 2011 From: foobar at invalid.invalid (mblume) Date: Fri, 30 Dec 2011 11:23:50 +0000 (UTC) Subject: Generating sin/square waves sound References: Message-ID: Am Fri, 30 Dec 2011 07:17:13 +0000 schrieb Paulo da Silva: > Hi, > Sorry if this is a FAQ, but I have googled and didn't find any > satisfatory answer. > > Is there a simple way, preferably multiplataform (or linux), of > generating sinusoidal/square waves sound in python? > > Thanks for any answers/suggestions. Have a look at the wave module, available under Windows and Linux, which operates on .WAV files. The following snippet might get you going: #!/usr/bin/python import math, wave, struct def signal(t, freq): return math.sin(2.0*math.pi*freq*t) wout = wave.open("sample.wav", "wb") nchan = 1 sampwidth = 2 framerate = 8000 nframes = 7 * framerate comptype = "NONE" compname = "no compression" wout.setparams((nchan, sampwidth, framerate, nframes, comptype, compname)) ts = 1.0 / framerate t = 0.0 n = 0 data = [] vals = [] while n < nframes: vals.append(signal(t, 517.0)) n = n + 1 t = t + ts mx = max((abs(x) for x in vals)) vals = [ x/mx for x in vals ] data = "" for v in vals: data = data + struct.pack(" Message-ID: lars van gemerden wrote: > On Dec 29, 8:55 pm, Ian Kelly wrote: >> On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden >> wrote: >> >> > Hello, >> >> > Can someone help me with the following: >> >> > I am using metaclasses to make classes and these classes to make >> > instances. Now I want to use multiprocessing, which needs to pickle >> > these instances. >> >> > Pickle cannot find the class definitions of the instances. I am trying >> > to add a line to the __new__ of the metaclass to add the new class >> > under the right name in the right module/place, so pickle can find >> > it. >> >> > Is this the right approach? Can anyone explain to me where/how to add >> > these classes for pickle to find and maybe why? >> >> It sounds like you're trying to do something like this? >> >> >>> class MetaClass(type): >> >> ... pass >> ...>>> instance = MetaClass('', (object,), {})() >> >>> instance >> >> <__main__. object at 0x00CC00F0>>>> import pickle >> >>> pickle.dumps(instance) >> >> Traceback (most recent call last): >> File "", line 1, in >> File "c:\python27\lib\pickle.py", line 1374, in dumps >> Pickler(file, protocol).dump(obj) >> File "c:\python27\lib\pickle.py", line 224, in dump >> self.save(obj) >> File "c:\python27\lib\pickle.py", line 331, in save >> self.save_reduce(obj=obj, *rv) >> File "c:\python27\lib\pickle.py", line 401, in save_reduce >> save(args) >> File "c:\python27\lib\pickle.py", line 286, in save >> f(self, obj) # Call unbound method with explicit self >> File "c:\python27\lib\pickle.py", line 562, in save_tuple >> save(element) >> File "c:\python27\lib\pickle.py", line 295, in save >> self.save_global(obj) >> File "c:\python27\lib\pickle.py", line 748, in save_global >> (obj, module, name)) >> pickle.PicklingError: Can't pickle '>: >> it's not found as __main__. >> >> Yeah, pickle's not going to work with anonymous classes. As you >> suggest, you could dynamically add the classes to the module namespace >> so that pickle.dumps will find them, but bear in mind that they will >> also have to exist when calling pickle.loads, so you will need to be >> able to reconstruct the same anonymous classes before unpickling later >> on. >> >> Cheers, >> Ian > > Thank you Ian for the minimal example. This is almost the case i was > trying to discribe, however the classes will be named at runtime and > the class definitions will be persisted in a database. > > Can you help me with how to add the classes to the correct namespace? > The application spans multiple modules (or compared to the example, > the metaclass definition will be in another module then one where the > class and instance will be generated). If the metaclass is global in whatever module you don't need to bother about that. The problem is that you cannot access the classes (metaclass instances) under a dotted name. One workaround is a pseudo-module that does whatever is needed to recreate the class: import pickle import sys class MetaClass(type): pass class M(object): def __init__(self, module): self.__module = module def __getattr__(self, name): print "creating class", name class_ = MetaClass(name, (), {"__module__": self.__module}) setattr(self, name, class_) return class_ sys.modules["m"] = M("m") import m c = m.x s = pickle.dumps(c) print repr(s) d = pickle.loads(s) assert c is d sys.modules["m"] = M("m") e = pickle.loads(s) assert c is not e The official way is probably what Robert mentioned, via the copy_reg module, but I didn't get it to work. From mo.menezes at gmail.com Fri Dec 30 08:21:57 2011 From: mo.menezes at gmail.com (Mario Menezes) Date: Fri, 30 Dec 2011 05:21:57 -0800 (PST) Subject: Is python.org down? Message-ID: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Hi, I?m trying to access pypi.python.org as well as www.python.org and none of them is up? Anybody else having problems with python.org today? Is there a scheduled downtime? Thanks Mario Menezes Brazil From tist.verdonck at gmail.com Fri Dec 30 08:39:37 2011 From: tist.verdonck at gmail.com (tistje) Date: Fri, 30 Dec 2011 05:39:37 -0800 (PST) Subject: Is python.org down? References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: <2f72f9e7-8045-455c-ade7-506c378fe733@b32g2000yqn.googlegroups.com> On 30 dec, 14:21, Mario Menezes wrote: > Hi, > > ?I?m trying to access pypi.python.org as well as www.python.org and none of them is up? > > ? Anybody else having problems with python.org today? Is there a scheduled downtime? > > ? Thanks > > Mario Menezes > Brazil Seems like it is down ... I downloaded the 2.7.2 Windows installer a couple of hours ago, but now even the download page isn't accessible. Tist Verdonck Belgium From rosuav at gmail.com Fri Dec 30 08:48:47 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 31 Dec 2011 00:48:47 +1100 Subject: Is python.org down? In-Reply-To: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: On Sat, Dec 31, 2011 at 12:21 AM, Mario Menezes wrote: > Hi, > > ?I?m trying to access pypi.python.org as well as www.python.org and none of them is up? > > ?Anybody else having problems with python.org today? Is there a scheduled downtime? > > ?Thanks Appears to be up now, for me at least. No reported troubles with DNS. If you're still unable to get to it, try doing a DNS lookup, or try pinging 82.94.164.162 (which is www.python.org according to my lookup). Which one's out? ChrisA From jerome at jolimont.fr Fri Dec 30 08:50:56 2011 From: jerome at jolimont.fr (=?UTF-8?B?SsOpcsO0bWU=?=) Date: Fri, 30 Dec 2011 14:50:56 +0100 Subject: Is python.org down? In-Reply-To: <2f72f9e7-8045-455c-ade7-506c378fe733@b32g2000yqn.googlegroups.com> References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> <2f72f9e7-8045-455c-ade7-506c378fe733@b32g2000yqn.googlegroups.com> Message-ID: <20111230145056.40205fd6@bouzin.lan> Fri, 30 Dec 2011 05:39:37 -0800 (PST) tistje a ?crit: > On 30 dec, 14:21, Mario Menezes wrote: > > Hi, > > > > ?I?m trying to access pypi.python.org as well as www.python.org and none > > of them is up? > > > > ? Anybody else having problems with python.org today? Is there a > > scheduled downtime? > Seems like it is down ... I downloaded the 2.7.2 Windows installer a > couple of hours ago, but now even the download page isn't accessible. http://www.python.org seems to be working, now. -- J?r?me France From tist.verdonck at gmail.com Fri Dec 30 08:51:35 2011 From: tist.verdonck at gmail.com (tistje) Date: Fri, 30 Dec 2011 05:51:35 -0800 (PST) Subject: Is python.org down? References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: <7be1cf8a-677d-492e-8252-f4b68dcb9189@u6g2000vbg.googlegroups.com> On 30 dec, 14:48, Chris Angelico wrote: > On Sat, Dec 31, 2011 at 12:21 AM, Mario Menezes wrote: > > Hi, > > > ?I?m trying to access pypi.python.org as well aswww.python.organd none of them is up? > > > ?Anybody else having problems with python.org today? Is there a scheduled downtime? > > > ?Thanks > > Appears to be up now, for me at least. No reported troubles with DNS. > If you're still unable to get to it, try doing a DNS lookup, or try > pinging 82.94.164.162 (which iswww.python.orgaccording to my > lookup). Which one's out? > > ChrisA only pypi is still unaccessible (502 Bad Gateway). Tist From mo.menezes at gmail.com Fri Dec 30 08:52:32 2011 From: mo.menezes at gmail.com (Mario Menezes) Date: Fri, 30 Dec 2011 05:52:32 -0800 (PST) Subject: Is python.org down? In-Reply-To: References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: <22152854.1188.1325253153021.JavaMail.geo-discussion-forums@yqbu38> Yeah! www.python.org is up now. pypi.python.org still with problems (502 Bad Gateway). will wait couple time. somebody seems to be working on this. thanks. From mo.menezes at gmail.com Fri Dec 30 08:52:32 2011 From: mo.menezes at gmail.com (Mario Menezes) Date: Fri, 30 Dec 2011 05:52:32 -0800 (PST) Subject: Is python.org down? In-Reply-To: References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: <22152854.1188.1325253153021.JavaMail.geo-discussion-forums@yqbu38> Yeah! www.python.org is up now. pypi.python.org still with problems (502 Bad Gateway). will wait couple time. somebody seems to be working on this. thanks. From python at bdurham.com Fri Dec 30 09:11:51 2011 From: python at bdurham.com (python at bdurham.com) Date: Fri, 30 Dec 2011 09:11:51 -0500 Subject: Is python.org down? In-Reply-To: References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: <1325254311.9097.140661017425721@webmail.messagingengine.com> A good tool for determining whether a site is down or just unavailable to you: http://downorisitjustme.com Malcolm From vikashagrawal1990 at gmail.com Fri Dec 30 09:51:50 2011 From: vikashagrawal1990 at gmail.com (vikash agrawal) Date: Fri, 30 Dec 2011 20:21:50 +0530 Subject: Is python.org down? In-Reply-To: <7be1cf8a-677d-492e-8252-f4b68dcb9189@u6g2000vbg.googlegroups.com> References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> <7be1cf8a-677d-492e-8252-f4b68dcb9189@u6g2000vbg.googlegroups.com> Message-ID: hi Everyone, it seems http://in.pycon.org/2011/ is also down :( Regards Vikash Agrawal -- sent via HTC Sensation -------------- next part -------------- An HTML attachment was scrubbed... URL: From lars at rational-it.com Fri Dec 30 10:56:11 2011 From: lars at rational-it.com (lars van gemerden) Date: Fri, 30 Dec 2011 07:56:11 -0800 (PST) Subject: pickling instances of metaclass generated classes References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> <208b4f8b-c347-44c6-ac9d-73662cc36dbd@o9g2000yqa.googlegroups.com> Message-ID: On Dec 30, 12:16?pm, lars van gemerden wrote: > On Dec 29, 8:55?pm, Ian Kelly wrote: > > > > > > > > > > > On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden wrote: > > > > Hello, > > > > Can someone help me with the following: > > > > I am using metaclasses to make classes and these classes to make > > > instances. Now I want to use multiprocessing, which needs to pickle > > > these instances. > > > > Pickle cannot find the class definitions of the instances. I am trying > > > to add a line to the __new__ of the metaclass to add the new class > > > under the right name in the right module/place, so pickle can find > > > it. > > > > Is this the right approach? Can anyone explain to me where/how to add > > > these classes for pickle to find and maybe why? > > > It sounds like you're trying to do something like this? > > > >>> class MetaClass(type): > > > ... ? ? pass > > ...>>> instance = MetaClass('', (object,), {})() > > >>> instance > > > <__main__. object at 0x00CC00F0>>>> import pickle > > >>> pickle.dumps(instance) > > > Traceback (most recent call last): > > ? File "", line 1, in > > ? File "c:\python27\lib\pickle.py", line 1374, in dumps > > ? ? Pickler(file, protocol).dump(obj) > > ? File "c:\python27\lib\pickle.py", line 224, in dump > > ? ? self.save(obj) > > ? File "c:\python27\lib\pickle.py", line 331, in save > > ? ? self.save_reduce(obj=obj, *rv) > > ? File "c:\python27\lib\pickle.py", line 401, in save_reduce > > ? ? save(args) > > ? File "c:\python27\lib\pickle.py", line 286, in save > > ? ? f(self, obj) # Call unbound method with explicit self > > ? File "c:\python27\lib\pickle.py", line 562, in save_tuple > > ? ? save(element) > > ? File "c:\python27\lib\pickle.py", line 295, in save > > ? ? self.save_global(obj) > > ? File "c:\python27\lib\pickle.py", line 748, in save_global > > ? ? (obj, module, name)) > > pickle.PicklingError: Can't pickle '>: > > it's not found as __main__. > > > Yeah, pickle's not going to work with anonymous classes. ?As you > > suggest, you could dynamically add the classes to the module namespace > > so that pickle.dumps will find them, but bear in mind that they will > > also have to exist when calling pickle.loads, so you will need to be > > able to reconstruct the same anonymous classes before unpickling later > > on. > > > Cheers, > > Ian > Ian also wrote: > > ''' > Actually, I was wrong, you probably don't need to do that. ?I suggest > going with Robert Kern's suggestion to either register the class with > the copy_reg module, or (perhaps better since it won't leak > registrations) implement a __reduce__ method on the class. ?For > example, this seems to work: > > >>> def reconstructor(*metaclass_args): > > ... ? ? cls = MetaClass.build_class(*metaclass_args) > ... ? ? self = cls.__new__(cls) > ... ? ? return self > ...>>> class MetaClass(type): > > ... ? ? @classmethod > ... ? ? def build_class(mcs, arg1, arg2, arg3): > ... ? ? ? ? # Do something useful with the args... > ... ? ? ? ? class _AnonymousClass(object): > ... ? ? ? ? ? ? __metaclass__ = mcs > ... ? ? ? ? ? ? def __reduce__(self): > ... ? ? ? ? ? ? ? ? return (reconstructor, ('foo', 'bar', 'baz'), > self.__dict__) > ... ? ? ? ? return _AnonymousClass > ...>>> instance = MetaClass.build_class('foo', 'bar', 'baz')() > >>> instance > > <__main__._AnonymousClass object at 0x011DB410>>>> instance.banana = 42 > >>> import pickle > >>> s = pickle.dumps(instance) > >>> s > > "c__main__\nreconstructor > \np0\n(S'foo'\np1\nS'bar'\np2\nS'baz'\np3\ntp4\nRp5\n(dp6\nS'banana'\np7\nI 42\nsb.">>> inst2 = pickle.loads(s) > >>> inst2 > > <__main__._AnonymousClass object at 0x011DBE90>>>> inst2.banana > 42 > >>> inst2.__class__ is instance.__class__ > > False > > Cheers, > Ian > > ''' Interesting, though I cannot say I completely understand this solution (looked up __reduce__, but still). I am trying to adapt this example to a situation where the metaclass generated classes are named at runtime (not anonymous), but cannot figure it out. Cheers, Lars From lars at rational-it.com Fri Dec 30 11:51:38 2011 From: lars at rational-it.com (lars van gemerden) Date: Fri, 30 Dec 2011 08:51:38 -0800 (PST) Subject: pickling instances of metaclass generated classes References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> <208b4f8b-c347-44c6-ac9d-73662cc36dbd@o9g2000yqa.googlegroups.com> Message-ID: On Dec 30, 4:56?pm, lars van gemerden wrote: > On Dec 30, 12:16?pm, lars van gemerden wrote: > > > > > > > > > > > On Dec 29, 8:55?pm, Ian Kelly wrote: > > > > On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden wrote: > > > > > Hello, > > > > > Can someone help me with the following: > > > > > I am using metaclasses to make classes and these classes to make > > > > instances. Now I want to use multiprocessing, which needs to pickle > > > > these instances. > > > > > Pickle cannot find the class definitions of the instances. I am trying > > > > to add a line to the __new__ of the metaclass to add the new class > > > > under the right name in the right module/place, so pickle can find > > > > it. > > > > > Is this the right approach? Can anyone explain to me where/how to add > > > > these classes for pickle to find and maybe why? > > > > It sounds like you're trying to do something like this? > > > > >>> class MetaClass(type): > > > > ... ? ? pass > > > ...>>> instance = MetaClass('', (object,), {})() > > > >>> instance > > > > <__main__. object at 0x00CC00F0>>>> import pickle > > > >>> pickle.dumps(instance) > > > > Traceback (most recent call last): > > > ? File "", line 1, in > > > ? File "c:\python27\lib\pickle.py", line 1374, in dumps > > > ? ? Pickler(file, protocol).dump(obj) > > > ? File "c:\python27\lib\pickle.py", line 224, in dump > > > ? ? self.save(obj) > > > ? File "c:\python27\lib\pickle.py", line 331, in save > > > ? ? self.save_reduce(obj=obj, *rv) > > > ? File "c:\python27\lib\pickle.py", line 401, in save_reduce > > > ? ? save(args) > > > ? File "c:\python27\lib\pickle.py", line 286, in save > > > ? ? f(self, obj) # Call unbound method with explicit self > > > ? File "c:\python27\lib\pickle.py", line 562, in save_tuple > > > ? ? save(element) > > > ? File "c:\python27\lib\pickle.py", line 295, in save > > > ? ? self.save_global(obj) > > > ? File "c:\python27\lib\pickle.py", line 748, in save_global > > > ? ? (obj, module, name)) > > > pickle.PicklingError: Can't pickle '>: > > > it's not found as __main__. > > > > Yeah, pickle's not going to work with anonymous classes. ?As you > > > suggest, you could dynamically add the classes to the module namespace > > > so that pickle.dumps will find them, but bear in mind that they will > > > also have to exist when calling pickle.loads, so you will need to be > > > able to reconstruct the same anonymous classes before unpickling later > > > on. > > > > Cheers, > > > Ian > > Ian also wrote: > > > ''' > > Actually, I was wrong, you probably don't need to do that. ?I suggest > > going with Robert Kern's suggestion to either register the class with > > the copy_reg module, or (perhaps better since it won't leak > > registrations) implement a __reduce__ method on the class. ?For > > example, this seems to work: > > > >>> def reconstructor(*metaclass_args): > > > ... ? ? cls = MetaClass.build_class(*metaclass_args) > > ... ? ? self = cls.__new__(cls) > > ... ? ? return self > > ...>>> class MetaClass(type): > > > ... ? ? @classmethod > > ... ? ? def build_class(mcs, arg1, arg2, arg3): > > ... ? ? ? ? # Do something useful with the args... > > ... ? ? ? ? class _AnonymousClass(object): > > ... ? ? ? ? ? ? __metaclass__ = mcs > > ... ? ? ? ? ? ? def __reduce__(self): > > ... ? ? ? ? ? ? ? ? return (reconstructor, ('foo', 'bar', 'baz'), > > self.__dict__) > > ... ? ? ? ? return _AnonymousClass > > ...>>> instance = MetaClass.build_class('foo', 'bar', 'baz')() > > >>> instance > > > <__main__._AnonymousClass object at 0x011DB410>>>> instance.banana = 42 > > >>> import pickle > > >>> s = pickle.dumps(instance) > > >>> s > > > "c__main__\nreconstructor > > \np0\n(S'foo'\np1\nS'bar'\np2\nS'baz'\np3\ntp4\nRp5\n(dp6\nS'banana'\np7\nI 42\nsb.">>> inst2 = pickle.loads(s) > > >>> inst2 > > > <__main__._AnonymousClass object at 0x011DBE90>>>> inst2.banana > > 42 > > >>> inst2.__class__ is instance.__class__ > > > False > > > Cheers, > > Ian > > > ''' > > Interesting, though I cannot say I completely understand this solution > (looked up __reduce__, but still). I am trying to adapt this example > to a situation where the metaclass generated classes are named at > runtime (not anonymous), but cannot figure it out. > > Cheers, Lars Found a way to name the classes: def reconstructor(*metaclass_args): cls = MetaClass2.build_class(*metaclass_args) self = cls.__new__(cls) return self class MetaClass(type): @classmethod def build_class(mcs, name, arg1, arg2, arg3): return mcs(name, (object,), {"__reduce__": lambda e: (reconstructor2, (name, arg1, arg2, arg3), e.__dict__)}) I still wonder whether it might be easier to add the class to the namespace. Can anyone help me with that? Regards, Lars From roy at panix.com Fri Dec 30 12:01:58 2011 From: roy at panix.com (Roy Smith) Date: Fri, 30 Dec 2011 09:01:58 -0800 (PST) Subject: Doing a HTTP DELETE operation with urllib2? Message-ID: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> Is there some way to make urllib2.urlopen() perform a DELETE instead of a GET or POST? I'm hoping I don't have to dip way down into httplib. I've got an application test framework built on top of urllib2. It makes heavy use of HTTPCookieProcessor. If I need to use the httplib calls directly, I'll have to re-implement a lot of that machinery. From ian.g.kelly at gmail.com Fri Dec 30 12:09:35 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 30 Dec 2011 10:09:35 -0700 Subject: pickling instances of metaclass generated classes In-Reply-To: References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> <208b4f8b-c347-44c6-ac9d-73662cc36dbd@o9g2000yqa.googlegroups.com> Message-ID: On Fri, Dec 30, 2011 at 9:51 AM, lars van gemerden wrote: > I still wonder whether it might be easier to add the class to the > namespace. Can anyone help me with that? from mypackage import mymodule setattr(mymodule, myclass.__name__, myclass) From akhi3030 at gmail.com Fri Dec 30 12:27:44 2011 From: akhi3030 at gmail.com (akhilesh singhania) Date: Fri, 30 Dec 2011 17:27:44 +0000 Subject: Creating a binary only python distribution of a C extension module and including some additional python and c files in it Message-ID: Hi, I have a extension module in C which I want to distribute in binary format, ideally an rpm. Additionally, I want to include some python files (examples on how to use the extension module) and source for a library the module dynamically links to (c,h, and make files). How do I specify the example python file in setup.py so that it will be included in the rpm? If I specify it as scripts, I get the following error: $ python setup.py bdist --format=rpm running build_scripts creating build/scripts-2.6 error: file 'foo.py' does not exist error: Bad exit status from /var/tmp/rpm-tmp.yjws9x (%build) If I specify it as data_files, I get the following error: $ python setup.py bdist --format=rpm error: Installed (but unpackaged) file(s) found: /usr/foo.pyc /usr/foo.pyo If I specify it as py_modules, I do not get errors but it is not included in the resulting rpm. Specifying it as a script works if I use $ python setup.py bdist --format=gztar Additionally, can I control the hierarchy of how the files are laid out in the rpm? Currently, I am specifying the c,h,make files as data_files and they get placed in /usr, which is not desirable. Regards, akhi From dmitrey15 at gmail.com Fri Dec 30 12:51:52 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Fri, 30 Dec 2011 09:51:52 -0800 (PST) Subject: How to get function string name from i-th stack position? Message-ID: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> hi all, how to get string name of a function that is n levels above the current Python interpreter position? Thank you in advance, D. From python.list at tim.thechases.com Fri Dec 30 13:35:53 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 30 Dec 2011 12:35:53 -0600 Subject: How to get function string name from i-th stack position? In-Reply-To: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: <4EFE0489.7060607@tim.thechases.com> On 12/30/11 11:51, dmitrey wrote: > how to get string name of a function that is n levels above > the current Python interpreter position? Use the results of traceback.extract_stack() from traceback import extract_stack def one(x): print "one", x stk = extract_stack() for mod, lineno, fun_name, call_code_text in stk: print "[%s:%i] in %s" % (mod, lineno, fun_name) def two(x): print "two", x one(x) def three(x): print "three", x two(x) three("Hi") -tkc From dmitrey15 at gmail.com Fri Dec 30 13:43:50 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Fri, 30 Dec 2011 10:43:50 -0800 (PST) Subject: How to get function string name from i-th stack position? References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: On Dec 30, 8:35?pm, Tim Chase wrote: > On 12/30/11 11:51, dmitrey wrote: > > > how to get string name of a function that is n levels above > > the current Python interpreter position? > > Use the results of traceback.extract_stack() > > ? ?from traceback import extract_stack > ? ?def one(x): > ? ? ?print "one", x > ? ? ?stk = extract_stack() > ? ? ?for mod, lineno, fun_name, call_code_text in stk: > ? ? ? ?print "[%s:%i] in %s" % (mod, lineno, fun_name) > ? ?def two(x): > ? ? ?print "two", x > ? ? ?one(x) > ? ?def three(x): > ? ? ?print "three", x > ? ? ?two(x) > ? ?three("Hi") > > -tkc Thank you. And what should I do to get function by itself instead of its string name, e.g. I want to know does this function is my_func or any other? For example, I would like to check is this function Python sum(), or maybe numpy.sum(), or anything else? Regards, D. From roy at panix.com Fri Dec 30 13:57:06 2011 From: roy at panix.com (Roy Smith) Date: Fri, 30 Dec 2011 10:57:06 -0800 (PST) Subject: Doing a HTTP DELETE operation with urllib2? In-Reply-To: References: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> Message-ID: <20419811.1501.1325271426676.JavaMail.geo-discussion-forums@vbmq3> Ah, cool. I didn't know you could do that. Thanks. From roy at panix.com Fri Dec 30 13:57:06 2011 From: roy at panix.com (Roy Smith) Date: Fri, 30 Dec 2011 10:57:06 -0800 (PST) Subject: Doing a HTTP DELETE operation with urllib2? In-Reply-To: References: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> Message-ID: <20419811.1501.1325271426676.JavaMail.geo-discussion-forums@vbmq3> Ah, cool. I didn't know you could do that. Thanks. From dougsland at redhat.com Fri Dec 30 15:14:56 2011 From: dougsland at redhat.com (Douglas Landgraf) Date: Fri, 30 Dec 2011 15:14:56 -0500 Subject: Doing a HTTP DELETE operation with urllib2? In-Reply-To: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> References: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> Message-ID: <4EFE1BC0.4060506@redhat.com> Hi, On 12/30/2011 12:01 PM, Roy Smith wrote: > Is there some way to make urllib2.urlopen() perform a DELETE instead of a GET or POST? > > I'm hoping I don't have to dip way down into httplib. I've got an application test framework built on top of urllib2. It makes heavy use of HTTPCookieProcessor. If I need to use the httplib calls directly, I'll have to re-implement a lot of that machinery. You might want to look: https://github.com/dougsland/rhev3-restapi-scripts/blob/master/sample-delete.py -- Cheers Douglas From steve+comp.lang.python at pearwood.info Fri Dec 30 15:37:41 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Dec 2011 20:37:41 GMT Subject: Doing a HTTP DELETE operation with urllib2? References: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> <20419811.1501.1325271426676.JavaMail.geo-discussion-forums@vbmq3> Message-ID: <4efe2115$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Dec 2011 10:57:06 -0800, Roy Smith wrote: > Ah, cool. I didn't know you could do that. Thanks. Who are you talking to, and what is "that"? Replies with no context are somewhat less than useful. It might have made sense in your head when you wrote the reply, but to those reading, it is rather cryptic. -- Steven From a24061 at ducksburg.com Fri Dec 30 15:40:16 2011 From: a24061 at ducksburg.com (Adam Funk) Date: Fri, 30 Dec 2011 20:40:16 +0000 Subject: mutually exclusive arguments to a constructor Message-ID: (Warning: this question obviously reflects the fact that I am more accustomed to using Java than Python.) Suppose I'm creating a class that represents a bearing or azimuth, created either from a string of traditional bearing notation ("N24d30mE") or from a number indicating the angle in degrees as usually measured in trigonometry (65.5, measured counter-clockwise from the x-axis). The class will have methods to return the same bearing in various formats. In Java, I would write two constructors, one taking a single String argument and one taking a single Double argument. But in Python, a class can have only one __init__ method, although it can have a lot of optional arguments with default values. What's the correct way to deal with a situation like the one I've outlined above? -- Unix is a user-friendly operating system. It's just very choosy about its friends. From d at davea.name Fri Dec 30 15:46:55 2011 From: d at davea.name (Dave Angel) Date: Fri, 30 Dec 2011 15:46:55 -0500 Subject: Doing a HTTP DELETE operation with urllib2? In-Reply-To: <4efe2115$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> <20419811.1501.1325271426676.JavaMail.geo-discussion-forums@vbmq3> <4efe2115$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EFE233F.4030607@davea.name> On 12/30/2011 03:37 PM, Steven D'Aprano wrote: > On Fri, 30 Dec 2011 10:57:06 -0800, Roy Smith wrote: > >> Ah, cool. I didn't know you could do that. Thanks. > Who are you talking to, and what is "that"? > > Replies with no context are somewhat less than useful. It might have made > sense in your head when you wrote the reply, but to those reading, it is > rather cryptic. > > Actually I think he was replying to Roy Smith's message, which has a timestamp of 77 minutes later. Interesting the effects you get when some clocks are allowed to drift. -- DaveA From gd.usenet at spamfence.net Fri Dec 30 16:00:58 2011 From: gd.usenet at spamfence.net (=?ISO-8859-1?Q?G=FCnther?= Dietrich) Date: Fri, 30 Dec 2011 22:00:58 +0100 Subject: mutually exclusive arguments to a constructor References: Message-ID: Adam Funk wrote: >Suppose I'm creating a class that represents a bearing or azimuth, >created either from a string of traditional bearing notation >("N24d30mE") or from a number indicating the angle in degrees as >usually measured in trigonometry (65.5, measured counter-clockwise >from the x-axis). The class will have methods to return the same >bearing in various formats. > >In Java, I would write two constructors, one taking a single String >argument and one taking a single Double argument. But in Python, a >class can have only one __init__ method, although it can have a lot of >optional arguments with default values. What's the correct way to >deal with a situation like the one I've outlined above? You can determine the type of the input data by using isinstance() and take the appropriate actions depending on this decision: >>> class MyClass(object): ... def __init__(self, input_data): ... if isinstance(input_data, basestring): ... print "Do actions for string type input" ... elif isinstance(input_data, float): ... print "Do actions for float type input" ... def get_output_data(self): ... return "output data" ... >>> a = MyClass("String") Do actions for string type input >>> b = MyClass(15.9) Do actions for float type input Best regards, G?nther From mwilson at the-wire.com Fri Dec 30 16:08:51 2011 From: mwilson at the-wire.com (Mel Wilson) Date: Fri, 30 Dec 2011 16:08:51 -0500 Subject: mutually exclusive arguments to a constructor References: Message-ID: Adam Funk wrote: > (Warning: this question obviously reflects the fact that I am more > accustomed to using Java than Python.) > > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as > usually measured in trigonometry (65.5, measured counter-clockwise > from the x-axis). The class will have methods to return the same > bearing in various formats. > > In Java, I would write two constructors, one taking a single String > argument and one taking a single Double argument. But in Python, a > class can have only one __init__ method, although it can have a lot of > optional arguments with default values. What's the correct way to > deal with a situation like the one I've outlined above? Cleanest from the point of view of the class source code would be factory functions at the module level, or special classmethods to deal with the less common cases. You see this a lot in wxPython when they have to deal with overloaded C++ constructors. Most like the Java would be to check within __init__ for a string argument that could be parsed as a bearing, and failing that fall back to treating the argument as a numeric angle. Neither fish nor fowl would be to accept named arguments for the different kinds of values. Mel. From arnodel at gmail.com Fri Dec 30 16:14:05 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Fri, 30 Dec 2011 21:14:05 +0000 Subject: mutually exclusive arguments to a constructor In-Reply-To: References: Message-ID: On 30 December 2011 20:40, Adam Funk wrote: > (Warning: this question obviously reflects the fact that I am more > accustomed to using Java than Python.) > > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as > usually measured in trigonometry (65.5, measured counter-clockwise > from the x-axis). ?The class will have methods to return the same > bearing in various formats. > > In Java, I would write two constructors, one taking a single String > argument and one taking a single Double argument. ?But in Python, a > class can have only one __init__ method, although it can have a lot of > optional arguments with default values. ?What's the correct way to > deal with a situation like the one I've outlined above? (Using Python 3 below) Method 1 ---------- Your __init__ method could take the angle as an argument (which seems the most natural to me). Then you could have a class method that takes the string i.e. class Bearing: def __init__(self, angle): self.angle = angle # or whatever your internal reprsentation is @classmethod def fromstring(cls, string): # Here, work out the angle from the string return cls(angle) So you can do: b = Bearing(65.5) or b = Bearing.fromstring("N24d30mE") Method 2 ---------- You can test the type of the argument of the __init__ method class Bearing: def __init__(self, arg): if isinstance(arg, str): # here calculate the value of angle else: angle = float(angle) self.angle = angle Now you can do: b = Bearing(65.5) or b = Bearing("N24d30mE") Both methods are used for builtin types: >>> int('12') 12 >>> int(12.5) 12 >>> dict([(1, 2), (3, 4)]) {1: 2, 3: 4} >>> dict.fromkeys([1, 2]) {1: None, 2: None} HTH -- Arnaud From jason at powerpull.net Fri Dec 30 16:18:29 2011 From: jason at powerpull.net (Jason Friedman) Date: Fri, 30 Dec 2011 21:18:29 +0000 Subject: mutually exclusive arguments to a constructor In-Reply-To: References: Message-ID: > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as > usually measured in trigonometry (65.5, measured counter-clockwise > from the x-axis). ?The class will have methods to return the same > bearing in various formats. > > In Java, I would write two constructors, one taking a single String > argument and one taking a single Double argument. ?But in Python, a > class can have only one __init__ method, although it can have a lot of > optional arguments with default values. ?What's the correct way to > deal with a situation like the one I've outlined above? Similar to other answers already posted: #!/usr/bin/env python class azimuth: def __init__(self, bearing, heading): self.bearing = bearing self.heading = heading if not bearing: self.bearing = 30.5 # or, realistically, a calculation based on the heading if not heading: self.heading = "N..." # or, realistically, a calculation based on the bearing @staticmethod def getBearingInstance(bearing): return azimuth(bearing, None) @staticmethod def getHeadingInstance(heading): return azimuth(None, heading) azimuth1 = azimuth.getBearingInstance("N24d30mE") print azimuth1.heading azimuth2 = azimuth.getHeadingInstance(30) print azimuth2.bearing From ian.g.kelly at gmail.com Fri Dec 30 16:48:49 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 30 Dec 2011 14:48:49 -0700 Subject: How to get function string name from i-th stack position? In-Reply-To: References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: On Fri, Dec 30, 2011 at 11:43 AM, dmitrey wrote: > Thank you. And what should I do to get function by itself instead of > its string name, e.g. I want to know does this function is my_func or > any other? For example, I would like to check is this function Python > sum(), or maybe numpy.sum(), or anything else? The Python stack only includes Python code objects. Built-ins like sum won't appear in it because they're basically C functions and don't have associated code objects. If you really want to see them, you could probably do something with ctypes to inspect the C stack, but I don't recommend it. You can get the Python code objects from the stack by calling inspect.stack(), which includes each frame object currently on the stack as the first member of each tuple. E.g.: frames = map(operator.itemgetter(0), inspect.stack()) Each frame has an f_code attribute that stores the code object associated with that frame. Getting the actual function from the code object is tricky, for two reasons. One, not all code objects represent functions. There are also code objects for modules, class definitions, and probably other thing as well. Two, code objects don't have associated functions. The relationship is the reverse: functions have associated code objects. You would have to iterate over each function that you're interested in, looking for one with a func_code attribute that "is" the frame's f_code attribute. In any case, testing function identity is a rather large rabbit hole that is best avoided. These are mathematically the same function: def plus1(value): return value + 1 plus_one = lambda x: x + 1 But they are two distinct function objects, and there is no way programmatically to determine that they are the same function except by comparing the bytecode (which won't work generally because of the halting problem). What is it that you're trying to do? Perhaps the helpful folks on the list will be able to suggest a better solution if you can provide more details. Cheers, Ian From steve+comp.lang.python at pearwood.info Fri Dec 30 17:13:15 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Dec 2011 22:13:15 GMT Subject: mutually exclusive arguments to a constructor References: Message-ID: <4efe377b$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Dec 2011 20:40:16 +0000, Adam Funk wrote: > (Warning: this question obviously reflects the fact that I am more > accustomed to using Java than Python.) > > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as usually > measured in trigonometry (65.5, measured counter-clockwise from the > x-axis). The class will have methods to return the same bearing in > various formats. > > In Java, I would write two constructors, one taking a single String > argument and one taking a single Double argument. But in Python, a > class can have only one __init__ method, although it can have a lot of > optional arguments with default values. What's the correct way to deal > with a situation like the one I've outlined above? The most idiomatic way to do this would be with named constructor functions, or by testing the argument type in __init__. For example: # Method 1 class Azimuth(object): def __init__(self, angle): # Initialise an azimuth object from an angle (float) self._angle = float(angle) @classmethod def from_bearing(cls, bearing): # Create an azimuth object from a bearing (string). angle = cls.bearing_to_angle(bearing) return cls(angle) @staticmethod def bearing_to_angle(bearing): # Convert a bearing (string) into a float. return 0.0 # whatever... Note some features of this version: * Normal methods receive the instance as first argument, "self". * We use the classmethod and staticmethod decorators to create class and static methods. Be warned that the meaning of these are NOT the same as in Java! * Class methods receive the class object as first argument, "cls". Hence the name. Note that in Python, classes are objects too. * We make from_bearing a class method, so we can call it from either the class itself: ang = Azimuth.from_bearing("25N14E") or from an existing instance: ang2 = ang.from_bearing("17N31W") * Static methods don't receive either the class or the instance. They are equivalent to a top level function, except encapsulated inside a class. # Method 2 class Azimuth(object): def __init__(self, arg): # Initialise an azimuth object from arg, either an angle (float) # or a bearing (string). if isinstance(arg, str): angle = bearing_to_angle(arg) else: angle = float(arg) self._angle = float(angle) def bearing_to_angle(bearing): # Convert a bearing (string) into a float. return 0.0 # whatever... Note that in this example, I've turned bearing_to_angle into a regular function outside of the class instead of a static method. Just because I can. This is probably slightly more idiomatic than the use of static methods. Either method is acceptable, although the first is slightly more "pure" because it doesn't use isinstance. The second may fail if the user passes a string-like object which behaves identically to strings, but doesn't inherit from str. If you care about that, you should prefer the first way with an explicit from_bearing method. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 30 17:21:09 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Dec 2011 22:21:09 GMT Subject: mutually exclusive arguments to a constructor References: Message-ID: <4efe3955$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Dec 2011 21:18:29 +0000, Jason Friedman wrote: > class azimuth: > def __init__(self, bearing, heading): It is conventional, and recommended, to use an initial capital letter for classes. (Yes, Python built-ins violate that rule, and indeed so do some non-built-ins.) See PEP 8 for the recommended style guide. [...] > @staticmethod > def getBearingInstance(bearing): > return azimuth(bearing, None) > @staticmethod > def getHeadingInstance(heading): > return azimuth(None, heading) In this case, you should use classmethod rather than staticmethod and avoid hard-coding the class: @classmethod def getBearingInstance(cls, bearing): return cls(bearing, None) That way subclassing will work correctly: class MyAzimuth(azimuth): pass angle = MyAzimuth.getBearingInstance("N24d30mE") will return a MyAzimuth instance instead of an azimuth instance. -- Steven From roy at panix.com Fri Dec 30 18:24:23 2011 From: roy at panix.com (Roy Smith) Date: Fri, 30 Dec 2011 18:24:23 -0500 Subject: mutually exclusive arguments to a constructor References: Message-ID: In article , Adam Funk wrote: > (Warning: this question obviously reflects the fact that I am more > accustomed to using Java than Python.) > > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as > usually measured in trigonometry (65.5, measured counter-clockwise > from the x-axis). There's two ways to do this. One would be to have the __init__ method switch on the type of its argument: def __init__(self, bearing_or_azimuth): if isinstance(bearing_or_azimuth, basestring): # do the bearing thing else: # do the azimuth thing I suspect many people would consider that unpythonic. The other way would be what, in the C++/Java world, would be called the "named constructor idiom". Just write two factory functions: class DirectionIndicatingThingie: @staticmethod def from_bearing(cls, bearing): dit = DirectionIndicatingThingie() dit.direction = whatever return dit and likewise for from_azimuth() "But!", some C++/Java type bondage addicts might cry, "there's nothing to prevent somebody from creating a DirectionIndicatingThingie directly, bypassing the factory functions. There's no way to make the constructor private!". To which the free-willed pythonistas would respond, "If it hurts when you do that, don't do that". From rosuav at gmail.com Fri Dec 30 18:36:18 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 31 Dec 2011 10:36:18 +1100 Subject: mutually exclusive arguments to a constructor In-Reply-To: References: Message-ID: On Sat, Dec 31, 2011 at 10:24 AM, Roy Smith wrote: > "But!", some C++/Java type bondage addicts might cry, "there's nothing > to prevent somebody from creating a DirectionIndicatingThingie directly, > bypassing the factory functions. ?There's no way to make the constructor > private!". ?To which the free-willed pythonistas would respond, "If it > hurts when you do that, don't do that". You know a Python programmer's been at your C++ code when it opens: #define class struct ChrisA From roy at panix.com Fri Dec 30 18:39:16 2011 From: roy at panix.com (Roy Smith) Date: Fri, 30 Dec 2011 18:39:16 -0500 Subject: mutually exclusive arguments to a constructor References: Message-ID: In article , Chris Angelico wrote: > On Sat, Dec 31, 2011 at 10:24 AM, Roy Smith wrote: > > "But!", some C++/Java type bondage addicts might cry, "there's nothing > > to prevent somebody from creating a DirectionIndicatingThingie directly, > > bypassing the factory functions. ?There's no way to make the constructor > > private!". ?To which the free-willed pythonistas would respond, "If it > > hurts when you do that, don't do that". > > You know a Python programmer's been at your C++ code when it opens: > #define class struct Why stop there? #define private public From rosuav at gmail.com Fri Dec 30 18:47:20 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 31 Dec 2011 10:47:20 +1100 Subject: mutually exclusive arguments to a constructor In-Reply-To: References: Message-ID: On Sat, Dec 31, 2011 at 10:39 AM, Roy Smith wrote: > In article , > ?Chris Angelico wrote: > >> You know a Python programmer's been at your C++ code when it opens: >> #define class struct > > Why stop there? > > #define private public Probably yeah, do both. Anyway, life's so much easier when you don't have to write trivial getter/setter methods (and then maintain them). I've never had a situation where I've changed a private member while keeping the getters and setters unchanged; the ONLY benefit accessor methods have ever given to me personally has been logging (and granted, that is hard to do without them - since you can't override __getitem__ in C++ - but how often do you really need that facility?). I used to believe in the separation of interface from implementation. Then I realised that most of the separation was transparent anyway, and gave up on it. And then realised why the separation is a good idea after all. :) ChrisA From rich at noir.com Fri Dec 30 20:17:51 2011 From: rich at noir.com (K Richard Pixley) Date: Fri, 30 Dec 2011 17:17:51 -0800 Subject: Which library for audio playback ? In-Reply-To: References: Message-ID: <3ptLq.44453$8O1.22863@newsfe07.iad> On 12/29/11 05:55 , J?r?me wrote: > I'm writing a small application that plays sound through the speakers. The > sounds are juste sine waves of arbitrary frequency I create in the code, not > sample .wav files. > > I didn't expect the choice for an audio library to be that complicated. There > are several libraries, and none of them seems to be *the* reference. > > Searching the web, I found these resources : > > http://wiki.python.org/moin/Audio > http://wiki.python.org/moin/PythonInMusic > > * I privileged ALSA to OSS as I read elsewhere that OSS is deprecated, or on > its way to be. (And JACK is not widely installed, apart from specific > applications (audio work).) > > * Then, I picked the alsaaudio library (http://pyalsaaudio.sourceforge.net/). > I don't remember exactly why. I think the project had the most recent > updates. I don't think any project claims to be (let alone aims at being) > complete. > > I'm wondering if I made a sensible choice. > > There are other libraries, including the following two that are platform > independent : > > * PyAudiere (http://pyaudiere.org/), OSS , not packaged for debian > * PyAudio (http://people.csail.mit.edu/hubert/pyaudio/) > > What solution would you recommend ? > > Are there other criterions I should take into account ? > > Thanks. > I made a similar survey of available libraries recently. I'm interested in MIDI also, though, primarily on mac. There doesn't seem to be any definitive audio library for linux, although several, (jack, oss, alsa), exist. (My impression is similar to yours, OSS is obsolete. Jack may be the better library technically, but is not as widely ported or available as ALSA.) There is a definitive library for both audio and MIDI on mac - core audio. There really aren't any competitors. There is also a definitive library for windows, although I don't use windows. It's the open source one with low latency that everyone in the professional world uses to replace windows because, (surprise!), windows isn't capable of coping. I have found python libraries for each of these, although some are very low level libraries, basically just wrapping something even lower. If anyone has done an integrated "full solution" for linux or mac, I didn't find it. The closest I found was PortMIDI and PortAudio which appear to have ports for all three platforms as well as one or two sets of python bindings and seem to be high enough level to be both useful and productive. The hard part there is that PortMIDI and PortAudio come in source, which requires a bit of hunting to track down prerequisites, etc. Please keep us posted as I'm chasing a similar problem. --rich From rich at noir.com Fri Dec 30 20:19:07 2011 From: rich at noir.com (K Richard Pixley) Date: Fri, 30 Dec 2011 17:19:07 -0800 Subject: Generating sin/square waves sound In-Reply-To: References: Message-ID: On 12/29/11 23:17 , Paulo da Silva wrote: > Hi, > Sorry if this is a FAQ, but I have googled and didn't find any > satisfatory answer. > > Is there a simple way, preferably multiplataform (or linux), of > generating sinusoidal/square waves sound in python? > > Thanks for any answers/suggestions. I just posted on this elsewhere. Look for a thread titled: "Which library for audio playback ?" --rich From pavanbond0001 at gmail.com Fri Dec 30 22:31:58 2011 From: pavanbond0001 at gmail.com (pavanm k) Date: Fri, 30 Dec 2011 19:31:58 -0800 (PST) Subject: ONLINE DATA ENTRY JOBS Message-ID: <7a86d02c-b365-4827-a2f0-b4f3d21a11d3@c42g2000prb.googlegroups.com> ONLINE DATA ENTRY JOBS http://pavankumaronlinejobs.blogsspot.com From niklasro at gmail.com Fri Dec 30 23:58:10 2011 From: niklasro at gmail.com (Niklas Rosencrantz) Date: Fri, 30 Dec 2011 20:58:10 -0800 (PST) Subject: InvalidResponseError: headers must be str Message-ID: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> I'm upgrading from python 2.5 to python 2.7 and then I'm starting to get this error: InvalidResponseError: headers must be str I think it is because somewhere my HTTP headers are cast to unicode instead of string but I can't find where in the code? The example code I'm trying to upgrade to python 2.7 is https://github.com/supernifty/gae-paypal-online-market-example class Pay( object ): def __init__( self, amount, return_url, cancel_url, remote_address, secondary_receiver=None, ipn_url=None, shipping=False ): headers = { 'X-PAYPAL-SECURITY-USERID': str(settings.PAYPAL_USERID), 'X-PAYPAL-SECURITY-PASSWORD': str(settings.PAYPAL_PASSWORD), 'X-PAYPAL-SECURITY-SIGNATURE': str(settings.PAYPAL_SIGNATURE), 'X-PAYPAL-REQUEST-DATA-FORMAT': str('JSON'), 'X-PAYPAL-RESPONSE-DATA-FORMAT': str('JSON'), 'X-PAYPAL-APPLICATION-ID': str(settings.PAYPAL_APPLICATION_ID), 'X-PAYPAL-DEVICE-IPADDRESS': str(remote_address), } data = { 'currencyCode': 'USD', 'returnUrl': return_url, 'cancelUrl': cancel_url, 'requestEnvelope': { 'errorLanguage': 'en_US' }, } if shipping: data['actionType'] = 'CREATE' else: data['actionType'] = 'PAY' if secondary_receiver == None: # simple payment data['receiverList'] = { 'receiver': [ { 'email': settings.PAYPAL_EMAIL, 'amount': '%f' % amount } ] } else: # chained commission = amount * settings.PAYPAL_COMMISSION data['receiverList'] = { 'receiver': [ { 'email': settings.PAYPAL_EMAIL, 'amount': '%0.2f' % amount, 'primary': 'true' }, { 'email': secondary_receiver, 'amount': '%0.2f' % ( amount - commission ), 'primary': 'false' }, ] } if ipn_url != None: data['ipnNotificationUrl'] = str(ipn_url) self.raw_request = json.dumps(data) #request = urllib2.Request( "%s%s" % ( settings.PAYPAL_ENDPOINT, "Pay" ), data=self.raw_request, headers=headers ) #self.raw_response = urllib2.urlopen( request ).read() self.raw_response = url_request( "%s%s" % ( str(settings.PAYPAL_ENDPOINT), str("Pay") ), data=self.raw_request, headers=headers ).content() logging.info( "response was: %s" % self.raw_response ) self.response = json.loads( str(self.raw_response) ) From emekamicro at gmail.com Sat Dec 31 00:48:29 2011 From: emekamicro at gmail.com (Emeka) Date: Sat, 31 Dec 2011 07:48:29 +0200 Subject: dict_to_xml Message-ID: Hello All, I have a dictionary object I would like to convert to xml. Could some assist with the link to libs to use? Or good examples. Regards, Janus -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sat Dec 31 01:40:42 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Dec 2011 06:40:42 GMT Subject: InvalidResponseError: headers must be str References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> Message-ID: <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Dec 2011 20:58:10 -0800, Niklas Rosencrantz wrote: > I'm upgrading from python 2.5 to python 2.7 and then I'm starting to get > this error: > > InvalidResponseError: headers must be str Please show the ENTIRE traceback, not just the error message. The traceback is very informative: it shows the actual line of code that causes the problem, plus the entire sequence of calls that lead to it. The error message on its own is almost useless. Please COPY AND PASTE the traceback, do not re-type it from memory, summarize it, simplify it, or otherwise change it in any way. > I think it is because somewhere my HTTP headers are cast to unicode Why do you think that? > instead of string but I can't find where in the code? Have you tried searching for any of these? - direct calls to unicode() - unicode literals u"..." - calls to the decode method some_string.decode( ... ) > The example code > I'm trying to upgrade to python 2.7 is > https://github.com/supernifty/gae-paypal-online-market-example > > class Pay( object ): > def __init__( self, amount, return_url, cancel_url, remote_address, > secondary_receiver=None, ipn_url=None, shipping=False ): > headers = { > 'X-PAYPAL-SECURITY-USERID': str(settings.PAYPAL_USERID), > 'X-PAYPAL-SECURITY-PASSWORD': str(settings.PAYPAL_PASSWORD), > 'X-PAYPAL-SECURITY-SIGNATURE': str(settings.PAYPAL_SIGNATURE), > 'X-PAYPAL-REQUEST-DATA-FORMAT': str('JSON'), > 'X-PAYPAL-RESPONSE-DATA-FORMAT': str('JSON'), > 'X-PAYPAL-APPLICATION-ID': str(settings.PAYPAL_APPLICATION_ID), > 'X-PAYPAL-DEVICE-IPADDRESS': str(remote_address), > } 'JSON' is already a string. Calling str() on it is a waste of time. What values do the various settings.* have? If they are already strings, calling str() again is a waste of time. I see you do this all through your class, needlessly calling str() on strings. -- Steven From vojtech.rylko at gmail.com Sat Dec 31 03:08:24 2011 From: vojtech.rylko at gmail.com (=?UTF-8?B?Vm9qdMSbY2ggUnlsa28=?=) Date: Sat, 31 Dec 2011 09:08:24 +0100 Subject: dict_to_xml In-Reply-To: References: Message-ID: <4EFEC2F8.9080006@gmail.com> Hello, there is one simple example which I made for my needs - https://github.com/vojtarylko/dict2xml Regards, Vojt?ch Dne 31.12.2011 6:48, Emeka napsal(a): > Hello All, > > > I have a dictionary object I would like to convert to xml. > > Could some assist with the link to libs to use? Or good examples. > > Regards, > Janus > > -- > /Satajanus Nig. Ltd > > > / > > From niklasro at gmail.com Sat Dec 31 03:32:54 2011 From: niklasro at gmail.com (Niklas Rosencrantz) Date: Sat, 31 Dec 2011 00:32:54 -0800 (PST) Subject: InvalidResponseError: headers must be str In-Reply-To: <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <3262240.2559.1325320374936.JavaMail.geo-discussion-forums@yqdj19> Thank you for the reply. I had the same error message before and it was resolved when I removed a casting of a header value from unicode to str. Now in this case I can't see where that happens or what causes the error. The full trace I'm experiencing now is File "/media/Lexar/montao/google/appengine/runtime/wsgi.py", line 129, in _StartResponse raise InvalidResponseError('headers must be str') InvalidResponseError: headers must be str INFO 2011-12-31 04:55:36,484 recording.py:372] Saved; key: __appstats__:034800, part: 137 bytes, full: 27325 bytes, overhead: 0.001 + 0.008; link: http://localhost:8080/_ah/stats/details?time=1325307334890 ERROR 2011-12-31 04:55:36,484 wsgi.py:186] Traceback (most recent call last): File "/media/Lexar/montao/google/appengine/runtime/wsgi.py", line 175, in Handle for chunk in result: File "/media/Lexar/montao/google/appengine/ext/appstats/recording.py", line 926, in appstats_wsgi_wrapper result = app(environ, appstats_start_response) File "/media/Lexar/montao/lib/webapp2/webapp2.py", line 1524, in __call__ return self._internal_error(e)(environ, start_response) File "/media/Lexar/montao/lib/webapp2/webapp2.py", line 1522, in __call__ return response(environ, start_response) File "/media/Lexar/montao/lib/webob/webob/__init__.py", line 2000, in __call__ start_response(self.status, self.headerlist) File "/media/Lexar/montao/google/appengine/ext/appstats/recording.py", line 923, in appstats_start_response return start_response(status, headers, exc_info) File "/media/Lexar/montao/google/appengine/runtime/wsgi.py", line 129, in _StartResponse raise InvalidResponseError('headers must be str') InvalidResponseError: headers must be str From dmitrey15 at gmail.com Sat Dec 31 03:44:50 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Sat, 31 Dec 2011 00:44:50 -0800 (PST) Subject: How to get function string name from i-th stack position? References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: <4b0f3a04-3b29-4cb7-af52-2001b25d560a@t30g2000vbx.googlegroups.com> On Dec 30, 11:48?pm, Ian Kelly wrote: > On Fri, Dec 30, 2011 at 11:43 AM, dmitrey wrote: > > Thank you. And what should I do to get function by itself instead of > > its string name, e.g. I want to know does this function is my_func or > > any other? For example, I would like to check is this function Python > > sum(), or maybe numpy.sum(), or anything else? > > The Python stack only includes Python code objects. ?Built-ins like > sum won't appear in it because they're basically C functions and don't > have associated code objects. ?If you really want to see them, you > could probably do something with ctypes to inspect the C stack, but I > don't recommend it. > > You can get the Python code objects from the stack by calling > inspect.stack(), which includes each frame object currently on the > stack as the first member of each tuple. ?E.g.: > > frames = map(operator.itemgetter(0), inspect.stack()) > > Each frame has an f_code attribute that stores the code object > associated with that frame. ?Getting the actual function from the code > object is tricky, for two reasons. ?One, not all code objects > represent functions. ?There are also code objects for modules, class > definitions, and probably other thing as well. ?Two, code objects > don't have associated functions. The relationship is the reverse: > functions have associated code objects. ?You would have to iterate > over each function that you're interested in, looking for one with a > func_code attribute that "is" the frame's f_code attribute. > > In any case, testing function identity is a rather large rabbit hole > that is best avoided. ?These are mathematically the same function: > > def plus1(value): > ? ? return value + 1 > > plus_one = lambda x: x + 1 > > But they are two distinct function objects, and there is no way > programmatically to determine that they are the same function except > by comparing the bytecode (which won't work generally because of the > halting problem). > > What is it that you're trying to do? ?Perhaps the helpful folks on the > list will be able to suggest a better solution if you can provide more > details. > > Cheers, > Ian Maybe it is somehow possible to compare function id with my candidates id, e.g. PythonSumID = id(sum) import numpy NumpySumID = id(numpy.sum) func = getting_function_from_Nth_stack_level_above if id(func) == PythonSumID: .... elif id(func) == NumpySumID: .... else: .... I need it due to the following reason: FuncDesigner users often use Python or numpy sum on FuncDesigner objects, while FuncDesigner.sum() is optimized for this case, works faster and doesn't lead to "Max recursion dept exceeded", that sometimes trigger for numpy or Python sum() when number of summarized elements is more than several hundreds. I would like to print warning "you'd better use FuncDesigner sum" if this case has been identified. Regards, D. From niklasro at gmail.com Sat Dec 31 03:44:50 2011 From: niklasro at gmail.com (Niklas Rosencrantz) Date: Sat, 31 Dec 2011 00:44:50 -0800 (PST) Subject: InvalidResponseError: headers must be str In-Reply-To: <3262240.2559.1325320374936.JavaMail.geo-discussion-forums@yqdj19> References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> <3262240.2559.1325320374936.JavaMail.geo-discussion-forums@yqdj19> Message-ID: <29532651.581.1325321090077.JavaMail.geo-discussion-forums@yqlp13> I can log the headers and it seems that they are strings: INFO 2011-12-31 08:43:03,286 paypal.py:143] headers: {'X-PAYPAL-REQUEST-DATA-FORMAT': 'JSON', 'X-PAYPAL-SECURITY-PASSWORD': '1324348659', 'X-PAYPAL-RESPONSE-DATA-FORMAT': 'JSON', 'X-PAYPAL-SECURITY-SIGNATURE': 'Al6H7Bq3kp4bKFht2fR-p2FlAbp3AJFKl5pFMzHpo.QKYe4Uob5YPIm.', 'X-PAYPAL-SECURITY-USERID': 'niklas_1354859649_biz_api1.eddaconsult.se', 'X-PAYPAL-DEVICE-IPADDRESS': '127.0.0.1', 'X-PAYPAL-APPLICATION-ID': 'APP-80W284485P519543T'} From storchaka at gmail.com Sat Dec 31 05:04:13 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Sat, 31 Dec 2011 12:04:13 +0200 Subject: InvalidResponseError: headers must be str In-Reply-To: <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: 31.12.11 08:40, Steven D'Aprano ???????(??): > 'JSON' is already a string. Calling str() on it is a waste of time. from __future__ import unicode_literals From jerome at jolimont.fr Sat Dec 31 06:02:31 2011 From: jerome at jolimont.fr (=?UTF-8?B?SsOpcsO0bWU=?=) Date: Sat, 31 Dec 2011 12:02:31 +0100 Subject: Which library for audio playback ? In-Reply-To: <3ptLq.44453$8O1.22863@newsfe07.iad> References: <3ptLq.44453$8O1.22863@newsfe07.iad> Message-ID: <20111231120231.36165856@bouzin.lan> Fri, 30 Dec 2011 17:17:51 -0800 K Richard Pixley a ?crit: > I made a similar survey of available libraries recently. I'm interested > in MIDI also, though, primarily on mac. > > There doesn't seem to be any definitive audio library for linux, > although several, (jack, oss, alsa), exist. (My impression is similar > to yours, OSS is obsolete. Jack may be the better library technically, > but is not as widely ported or available as ALSA.) > > There is a definitive library for both audio and MIDI on mac - core > audio. There really aren't any competitors. > > There is also a definitive library for windows, although I don't use > windows. It's the open source one with low latency that everyone in the > professional world uses to replace windows because, (surprise!), windows > isn't capable of coping. > > I have found python libraries for each of these, although some are very > low level libraries, basically just wrapping something even lower. If > anyone has done an integrated "full solution" for linux or mac, I didn't > find it. The closest I found was PortMIDI and PortAudio which appear to > have ports for all three platforms as well as one or two sets of python > bindings and seem to be high enough level to be both useful and > productive. The hard part there is that PortMIDI and PortAudio come in > source, which requires a bit of hunting to track down prerequisites, etc. > > Please keep us posted as I'm chasing a similar problem. Hi. Looking at it again, I've narrowed down to the following few : ossaudiodev http://docs.python.org/library/ossaudiodev.html Linux only but included in python pygame http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound Most occurring answer on forums and mailing-lists on the net for this question. Might be a little overkill... PortAudio http://people.csail.mit.edu/hubert/pyaudio/ http://www.portaudio.com/ Cross-platform, packaged for debian. Could fit my needs as well. Generally, the examples I find are about opening and playing a wavefile. I guess I'm gonna have to work a little to feed them a homegrown sinewave of arbitrary frequency. Regarding PortAudio, python /usr/share/doc/python-pyaudio/examples/system_info.py (and other examples) yield complaints about jack not being running. I assume they could be tweaked to use ALSA instead. I haven't had time to dig any further. If I come up with something, I'll keep you posted. Thanks for your answer. -- J?r?me From lie.1296 at gmail.com Sat Dec 31 06:41:46 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 31 Dec 2011 22:41:46 +1100 Subject: How to get function string name from i-th stack position? In-Reply-To: References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: On 12/31/2011 08:48 AM, Ian Kelly wrote: > > But they are two distinct function objects, and there is no way > programmatically to determine that they are the same function except > by comparing the bytecode (which won't work generally because of the > halting problem). Actually, it is often possible to determine that two functions are the same function, you simply need a to compare whether the function object lives in the same memory address. It is also possible to determine if two functions are different, if the function object are in different memory address than the function is different function. What is difficult to do due to the Halting problem is comparing whether two different functions are "equivalent" (and therefore interchangeable). I think the OP wants to find the former, not the latter. The former is trivial, the latter impossible. From steve+comp.lang.python at pearwood.info Sat Dec 31 07:52:13 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Dec 2011 12:52:13 GMT Subject: InvalidResponseError: headers must be str References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com> On Sat, 31 Dec 2011 12:04:13 +0200, Serhiy Storchaka wrote: > 31.12.11 08:40, Steven D'Aprano ???????(??): >> 'JSON' is already a string. Calling str() on it is a waste of time. > > from __future__ import unicode_literals Fair point. Your correction is noted. -- Steven From niklasro at gmail.com Sat Dec 31 08:31:14 2011 From: niklasro at gmail.com (Niklas Rosencrantz) Date: Sat, 31 Dec 2011 05:31:14 -0800 (PST) Subject: InvalidResponseError: headers must be str In-Reply-To: <4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> <4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9589290.553.1325338274423.JavaMail.geo-discussion-forums@yqcb25> I'm still no further to reaching a solution and my efforts of logging the headers didn't produce anything. I'm certain that it's upgrading from ptyhon 2.5 to python 2.7 that causes this since the GAE SDK uses WSGI instead of CGI now. Any idea about my problem? Thank you From steve+comp.lang.python at pearwood.info Sat Dec 31 09:31:56 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Dec 2011 14:31:56 GMT Subject: InvalidResponseError: headers must be str References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> <4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com> <9589290.553.1325338274423.JavaMail.geo-discussion-forums@yqcb25> Message-ID: <4eff1cdb$0$29966$c3e8da3$5496439d@news.astraweb.com> On Sat, 31 Dec 2011 05:31:14 -0800, Niklas Rosencrantz wrote: > I'm still no further to reaching a solution and my efforts of logging > the headers didn't produce anything. I'm certain that it's upgrading > from ptyhon 2.5 to python 2.7 that causes this since the GAE SDK uses > WSGI instead of CGI now. Any idea about my problem? > > Thank you Have patience. It has been less than a day since you first asked, and it is New Years Day or New Years Eve (depending on where you are). Most people will be away from their computers or busy celebrating. -- Steven From nvidhya01 at gmail.com Sat Dec 31 10:22:51 2011 From: nvidhya01 at gmail.com (n v) Date: Sat, 31 Dec 2011 07:22:51 -0800 (PST) Subject: funny minny year@@@@@@@@@@@@@@@@@@@@ Message-ID: <4cc4cf06-0a55-4a07-9127-c8f38c4bf1fa@n13g2000prf.googlegroups.com> http://123maza.com/48/silver424/ From invalid at invalid.invalid Sat Dec 31 11:27:11 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Sat, 31 Dec 2011 16:27:11 +0000 (UTC) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-12-28, Steven D'Aprano wrote: > On Tue, 27 Dec 2011 18:42:05 -0800, Rick Johnson wrote: > >> I don't care what ANY dictionary says. Much less a "world" >> dictionary. I don't validate or invalidate a word based on some phony >> baloney group of pseudo intellectuals who decided one to day that >> writing a dictionary "might be cool". I am against these words and >> phrases because we already have words that work just fine. Why rock >> the boat? > > Why do you say "rock" when the word "shake" is just as good? > > Why do you say "boat" when we already have "ship"? > > Why do you say "pseudo intellectuals" when you could say "fake > intellectuals"? > > Why do I waste my time reading your pretentious self-important nonsense? http://xkcd.com/386/ ;) -- Grant Edwards grant.b.edwards Yow! Not SENSUOUS ... only at "FROLICSOME" ... and in gmail.com need of DENTAL WORK ... in PAIN!!! From ian.g.kelly at gmail.com Sat Dec 31 12:05:38 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 31 Dec 2011 10:05:38 -0700 Subject: How to get function string name from i-th stack position? In-Reply-To: References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: On Sat, Dec 31, 2011 at 4:41 AM, Lie Ryan wrote: > On 12/31/2011 08:48 AM, Ian Kelly wrote: >> >> >> But they are two distinct function objects, and there is no way >> programmatically to determine that they are the same function except >> by comparing the bytecode (which won't work generally because of the >> halting problem). > > > Actually, it is often possible to determine that two functions are the same > function, you simply need a to compare whether the function object lives in > the same memory address. It is also possible to determine if two functions > are different, if the function object are in different memory address than > the function is different function. > > What is difficult to do due to the Halting problem is comparing whether two > different functions are "equivalent" (and therefore interchangeable). Yes, that is what I said. You can determine whether two function objects are the same, but not whether they are *mathematically* the same function. > I think the OP wants to find the former, not the latter. The former is > trivial, the latter impossible. Based on his subsequent clarification, I agree, although from his original post it sounded like he was just trying to identify summing functions in general. However, finding the built-in sum function on the stack is not trivial at all, perhaps not even possible. From ian.g.kelly at gmail.com Sat Dec 31 12:13:08 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 31 Dec 2011 10:13:08 -0700 Subject: How to get function string name from i-th stack position? In-Reply-To: <4b0f3a04-3b29-4cb7-af52-2001b25d560a@t30g2000vbx.googlegroups.com> References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> <4b0f3a04-3b29-4cb7-af52-2001b25d560a@t30g2000vbx.googlegroups.com> Message-ID: On Sat, Dec 31, 2011 at 1:44 AM, dmitrey wrote: > On Dec 30, 11:48?pm, Ian Kelly wrote: >> On Fri, Dec 30, 2011 at 11:43 AM, dmitrey wrote: >> > Thank you. And what should I do to get function by itself instead of >> > its string name, e.g. I want to know does this function is my_func or >> > any other? For example, I would like to check is this function Python >> > sum(), or maybe numpy.sum(), or anything else? >> >> The Python stack only includes Python code objects. ?Built-ins like >> sum won't appear in it because they're basically C functions and don't >> have associated code objects. ?If you really want to see them, you >> could probably do something with ctypes to inspect the C stack, but I >> don't recommend it. >> >> You can get the Python code objects from the stack by calling >> inspect.stack(), which includes each frame object currently on the >> stack as the first member of each tuple. ?E.g.: >> >> frames = map(operator.itemgetter(0), inspect.stack()) >> >> Each frame has an f_code attribute that stores the code object >> associated with that frame. ?Getting the actual function from the code >> object is tricky, for two reasons. ?One, not all code objects >> represent functions. ?There are also code objects for modules, class >> definitions, and probably other thing as well. ?Two, code objects >> don't have associated functions. The relationship is the reverse: >> functions have associated code objects. ?You would have to iterate >> over each function that you're interested in, looking for one with a >> func_code attribute that "is" the frame's f_code attribute. >> >> In any case, testing function identity is a rather large rabbit hole >> that is best avoided. ?These are mathematically the same function: >> >> def plus1(value): >> ? ? return value + 1 >> >> plus_one = lambda x: x + 1 >> >> But they are two distinct function objects, and there is no way >> programmatically to determine that they are the same function except >> by comparing the bytecode (which won't work generally because of the >> halting problem). >> >> What is it that you're trying to do? ?Perhaps the helpful folks on the >> list will be able to suggest a better solution if you can provide more >> details. >> >> Cheers, >> Ian > > Maybe it is somehow possible to compare function id with my candidates > id, e.g. > PythonSumID = id(sum) > import numpy > NumpySumID = id(numpy.sum) > func = getting_function_from_Nth_stack_level_above > if id(func) == PythonSumID: > ?.... > elif id(func) == NumpySumID: > ?.... > else: > ?.... > I need it due to the following reason: FuncDesigner users often use > Python or numpy sum on FuncDesigner objects, while FuncDesigner.sum() > is optimized for this case, works faster and doesn't lead to "Max > recursion dept exceeded", that sometimes trigger for numpy or Python > sum() when number of summarized elements is more than several > hundreds. I would like to print warning "you'd better use FuncDesigner > sum" if this case has been identified. The only think I can think of would be to replace the sum built-in with a wrapper that checks whether it's being called on FuncDesigner objects and issues a warning. Users might not like you messing with their built-ins in that way, though. From dgeorge29ca at gmail.com Sat Dec 31 13:19:34 2011 From: dgeorge29ca at gmail.com (davidfx) Date: Sat, 31 Dec 2011 10:19:34 -0800 (PST) Subject: .format vs. % Message-ID: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> Hello everyone, I just have a quick question about .format and %r %s %d. Should we always be using .format() for formatting strings or %? Example a = 'apples' print "I love {0}.".format(a) If I wanted to put .format into a variable, how would I do that. Thanks for your information in advance. David From roy at panix.com Sat Dec 31 13:23:35 2011 From: roy at panix.com (Roy Smith) Date: Sat, 31 Dec 2011 13:23:35 -0500 Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Grant Edwards wrote: > On 2011-12-28, Steven D'Aprano wrote: > > On Tue, 27 Dec 2011 18:42:05 -0800, Rick Johnson wrote: > > > >> I don't care what ANY dictionary says. Much less a "world" > >> dictionary. I don't validate or invalidate a word based on some phony > >> baloney group of pseudo intellectuals who decided one to day that > >> writing a dictionary "might be cool". I am against these words and > >> phrases because we already have words that work just fine. Why rock > >> the boat? > > > > Why do you say "rock" when the word "shake" is just as good? > > > > Why do you say "boat" when we already have "ship"? > > > > Why do you say "pseudo intellectuals" when you could say "fake > > intellectuals"? > > > > Why do I waste my time reading your pretentious self-important nonsense? > > http://xkcd.com/386/ > > ;) Why ROFLMAO when double-plus funny works just as well? From yasar11732 at gmail.com Sat Dec 31 13:34:01 2011 From: yasar11732 at gmail.com (=?utf-8?B?WWHFn2FyIEFyYWJhY8Sx?=) Date: Sat, 31 Dec 2011 20:34:01 +0200 Subject: .format vs. % In-Reply-To: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> Message-ID: What exactly do you mean by putting .format into a variable? You mean like this: "{name} is very {adj} {gender}".format(name="sandy",adj="diligent",gender="female") Sat, 31 Dec 2011 20:19:34 +0200 tarihinde davidfx ??yle yazm??: > Hello everyone, > I just have a quick question about .format and %r %s %d. > > Should we always be using .format() for formatting strings or %? > > Example a = 'apples' > print "I love {0}.".format(a) > > If I wanted to put .format into a variable, how would I do that. > > Thanks for your information in advance. > > David -- Opera'n?n e-posta istemcisi ile g?nderildi: http://www.opera.com/mail From bahamutzero8825 at gmail.com Sat Dec 31 13:34:06 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 31 Dec 2011 12:34:06 -0600 Subject: .format vs. % In-Reply-To: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> Message-ID: <4EFF559E.1050408@gmail.com> On 12/31/2011 12:19 PM, davidfx wrote: > Should we always be using .format() for formatting strings or %? In new code, yes. %-style formatting will eventually go away, but probably not for a long time. > If I wanted to put .format into a variable, how would I do that. What do you mean? -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From dgeorge29ca at gmail.com Sat Dec 31 13:44:08 2011 From: dgeorge29ca at gmail.com (davidfx) Date: Sat, 31 Dec 2011 10:44:08 -0800 (PST) Subject: .format vs. % In-Reply-To: References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> Message-ID: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) What is the .format version of this concept? From dgeorge29ca at gmail.com Sat Dec 31 13:44:08 2011 From: dgeorge29ca at gmail.com (davidfx) Date: Sat, 31 Dec 2011 10:44:08 -0800 (PST) Subject: .format vs. % In-Reply-To: References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> Message-ID: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) What is the .format version of this concept? From edriscoll at wisc.edu Sat Dec 31 13:47:02 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 31 Dec 2011 13:47:02 -0500 Subject: .format vs. % In-Reply-To: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: <4EFF58A6.4070605@wisc.edu> How 'bout just: >>> s = "{0} {1} {2} {3}" >>> s.format(1, 2, 3, 4) '1 2 3 4' Evan On 12/31/2011 13:44, davidfx wrote: > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From yasar11732 at gmail.com Sat Dec 31 13:51:53 2011 From: yasar11732 at gmail.com (=?utf-8?B?WWHFn2FyIEFyYWJhY8Sx?=) Date: Sat, 31 Dec 2011 20:51:53 +0200 Subject: .format vs. % In-Reply-To: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: You mean like this? =========================== >>> a = "I like {name}" >>> a.format(name="myself") 'I like myself' ============================ Sat, 31 Dec 2011 20:44:08 +0200 tarihinde davidfx ??yle yazm??: > Thanks for your response. I know the following code is not going to be > correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > -- Opera'n?n e-posta istemcisi ile g?nderildi: http://www.opera.com/mail From benjamin.kaplan at case.edu Sat Dec 31 13:57:13 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 31 Dec 2011 13:57:13 -0500 Subject: .format vs. % In-Reply-To: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: On Dec 31, 2011 1:46 PM, "davidfx" wrote: > > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > format is a method of the string class. You store the string the same way you would any other. formatter = "Hello, {}" print(formatter.format("world")) -------------- next part -------------- An HTML attachment was scrubbed... URL: From alex.kapps at web.de Sat Dec 31 13:59:19 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sat, 31 Dec 2011 19:59:19 +0100 Subject: .format vs. % In-Reply-To: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: <4EFF5B87.9060805@web.de> On 31.12.2011 19:44, davidfx wrote: > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > formatter = "{0} {1} {2} {3}" formatter.format(1,2,3,4) I have to say, I simply hate .format(), and I will be very, very upset if classic formatting will really be removed. I understand that .format() has a lot of advantages for more complex formatting, but for the simple stuff I usually do, it's just terribly inconvenient (try {} on a German keyboard). From jason at feedmagnet.com Sat Dec 31 14:05:17 2011 From: jason at feedmagnet.com (Jason Ford) Date: Sat, 31 Dec 2011 11:05:17 -0800 (PST) Subject: Python powering giant screen in Times Square Message-ID: Verizon has a giant HD screen in Times Square today showing a live feed of social media activity ? and it's built entirely in Python. Our app, FeedMagnet, aggregates and curates social content and is powering the screen. It has a Django front-end and pure Python backend to talk to Facebook, Twitter, Instagram, Google+, Foursquare, and other sources. So if you happen to be in Times Square for the crazy party today, tell the people around you that big social media screen is powered by Python! - Jason (founder of FeedMagnet) From alex.kapps at web.de Sat Dec 31 14:06:05 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sat, 31 Dec 2011 20:06:05 +0100 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EFF5D1D.4000905@web.de> On 31.12.2011 19:23, Roy Smith wrote: >>> Why do I waste my time reading your pretentious self-important nonsense? >> >> http://xkcd.com/386/ >> >> ;) > > Why ROFLMAO when double-plus funny works just as well? xkcd/386 has been the excuse for replying to RR for ages and I still don't understand why he gets that much advertence. Charity? Sympathy for the lone and broken? FWIW, it undermines all my attempts to block him. Sigh. From python.list at tim.thechases.com Sat Dec 31 14:24:10 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 31 Dec 2011 13:24:10 -0600 Subject: .format vs. % In-Reply-To: References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: <4EFF615A.1050008@tim.thechases.com> On 12/31/11 12:57, Benjamin Kaplan wrote: > format is a method of the string class. You store the string the same way > you would any other. > > formatter = "Hello, {}" > print(formatter.format("world")) Just to note that this syntax doesn't quite work in some earlier versions (tested below in 2.6, which came stock in Debian Stable) >>> formatter = "Hello, {}" >>> print(formatter.format("world")) Traceback (most recent call last): File "", line 1, in ValueError: zero length field name in format It needs to be spelled "{0}" -tkc From magawake at gmail.com Sat Dec 31 14:24:37 2011 From: magawake at gmail.com (Mag Gam) Date: Sat, 31 Dec 2011 14:24:37 -0500 Subject: python curses wrapper Message-ID: Hello, I have been struggling reseting the terminal when I try to do KeyboardInterrupt exception therefore I read the documentation for curses.wrapper and it seems to take care of it for me, http://docs.python.org/library/curses.html#curses.wrapper. Can someone please provide a Hello World example with python curses wrapper? tia From lie.1296 at gmail.com Sat Dec 31 14:34:45 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 01 Jan 2012 06:34:45 +1100 Subject: .format vs. % In-Reply-To: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: On 01/01/2012 05:44 AM, davidfx wrote: > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > I don't think the (%r)epr-formatting exist anymore, so if you want to do that you'll need to call repr manually. From alex.kapps at web.de Sat Dec 31 14:34:49 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sat, 31 Dec 2011 20:34:49 +0100 Subject: python curses wrapper In-Reply-To: References: Message-ID: <4EFF63D9.5010100@web.de> On 31.12.2011 20:24, Mag Gam wrote: > Hello, > > I have been struggling reseting the terminal when I try to do > KeyboardInterrupt exception therefore I read the documentation for > curses.wrapper and it seems to take care of it for me, > http://docs.python.org/library/curses.html#curses.wrapper. > > Can someone please provide a Hello World example with python curses wrapper? > > tia Use atexit.register() to register a cleanup function which is called when the program exits: import atexit import curses def cleanup(): curses.nocbreak() stdscr.keypad(0) curses.echo() curses.endwin() atexit.register(cleanup) stdscr = curses.initscr() curses.noecho() curses.cbreak() stdscr.keypad(1) curses.start_color() curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLUE) curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK) stdscr.bkgd(curses.color_pair(1)) stdscr.refresh() win = curses.newwin(5, 20, 5, 5) win.bkgd(curses.color_pair(2)) win.box() win.addstr(2, 2, "Hallo, Welt!") win.refresh() c = stdscr.getch() From robert.kern at gmail.com Sat Dec 31 14:43:50 2011 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 31 Dec 2011 19:43:50 +0000 Subject: .format vs. % In-Reply-To: References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: On 12/31/11 7:34 PM, Lie Ryan wrote: > On 01/01/2012 05:44 AM, davidfx wrote: >> Thanks for your response. I know the following code is not going to be correct >> but I want to show you what I was thinking. >> >> formatter = "%r %r %r %r" >> >> print formatter % (1, 2, 3, 4) >> >> What is the .format version of this concept? >> > > I don't think the (%r)epr-formatting exist anymore, so if you want to do that > you'll need to call repr manually. Yes, it does. formatter = '{!r} {!r} {!r} {!r}' print formatter.format(1,2,3,4) -- 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 From alex.kapps at web.de Sat Dec 31 14:49:55 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sat, 31 Dec 2011 20:49:55 +0100 Subject: python curses wrapper In-Reply-To: <4EFF63D9.5010100@web.de> References: <4EFF63D9.5010100@web.de> Message-ID: <4EFF6763.7080209@web.de> On 31.12.2011 20:34, Alexander Kapps wrote: > On 31.12.2011 20:24, Mag Gam wrote: >> Hello, >> >> I have been struggling reseting the terminal when I try to do >> KeyboardInterrupt exception therefore I read the documentation for >> curses.wrapper and it seems to take care of it for me, >> http://docs.python.org/library/curses.html#curses.wrapper. >> >> Can someone please provide a Hello World example with python >> curses wrapper? >> >> tia > > Use atexit.register() to register a cleanup function which is called > when the program exits: Oh, sorry, I missed the curses.wrapper() part. Here is an example: import curses def main(screen): curses.start_color() curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLUE) curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK) screen.bkgd(curses.color_pair(1)) screen.refresh() win = curses.newwin(5, 20, 5, 5) win.bkgd(curses.color_pair(2)) win.box() win.addstr(2, 2, "Hallo, Welt!") win.refresh() c = screen.getch() try: curses.wrapper(main) except KeyboardInterrupt: print "Got KeyboardInterrupt exception. Exiting..." exit() From magawake at gmail.com Sat Dec 31 14:52:10 2011 From: magawake at gmail.com (Mag Gam) Date: Sat, 31 Dec 2011 14:52:10 -0500 Subject: python curses wrapper In-Reply-To: <4EFF63D9.5010100@web.de> References: <4EFF63D9.5010100@web.de> Message-ID: thanks for your prompt reply. Why would I have to use atexeit? According to the documentation, curses.wrapper should handle what cleanup() should be doing. Neverthless, good to know it exists :p On Sat, Dec 31, 2011 at 2:34 PM, Alexander Kapps wrote: > On 31.12.2011 20:24, Mag Gam wrote: >> >> Hello, >> >> I have been struggling reseting the terminal when I try to do >> KeyboardInterrupt exception therefore I read the documentation for >> curses.wrapper and it seems to take care of it for me, >> http://docs.python.org/library/curses.html#curses.wrapper. >> >> Can someone please provide a Hello World example with python curses >> wrapper? >> >> tia > > > Use atexit.register() to register a cleanup function which is called when > the program exits: > > import atexit > import curses > > def cleanup(): > ? ?curses.nocbreak() > ? ?stdscr.keypad(0) > ? ?curses.echo() > ? ?curses.endwin() > > atexit.register(cleanup) > > stdscr = curses.initscr() > curses.noecho() > curses.cbreak() > stdscr.keypad(1) > > curses.start_color() > curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLUE) > curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK) > > stdscr.bkgd(curses.color_pair(1)) > stdscr.refresh() > > win = curses.newwin(5, 20, 5, 5) > win.bkgd(curses.color_pair(2)) > win.box() > win.addstr(2, 2, "Hallo, Welt!") > win.refresh() > > c = stdscr.getch() > > -- > http://mail.python.org/mailman/listinfo/python-list From tundra at tundraware.com Sat Dec 31 15:26:27 2011 From: tundra at tundraware.com (Tim Daneliuk) Date: Sat, 31 Dec 2011 14:26:27 -0600 Subject: Best Way To Bound Function Execution Time Message-ID: I am writing some paramiko-based ssh routines. One of them logs into a remote server and then does a sudo command. The problem is that if the user provides the incorrect sudo password, the call hangs the other end is waiting for the correct password to be entered. Is there some standard Pythonic way to bound how long a function call can run, after which time it is forcefully terminated? TIA, -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From ericsnowcurrently at gmail.com Sat Dec 31 15:47:57 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Sat, 31 Dec 2011 13:47:57 -0700 Subject: Python powering giant screen in Times Square In-Reply-To: References: Message-ID: On Sat, Dec 31, 2011 at 12:05 PM, Jason Ford wrote: > Verizon has a giant HD screen in Times Square today showing a live > feed of social media activity ? and it's built entirely in Python. > > Our app, FeedMagnet, aggregates and curates social content and is > powering the screen. It has a Django front-end and pure Python backend > to talk to Facebook, Twitter, Instagram, Google+, Foursquare, and > other sources. > > So if you happen to be in Times Square for the crazy party today, tell > the people around you that big social media screen is powered by > Python! Wow! That's awesome. Thanks for sharing this! -eric > > - Jason (founder of FeedMagnet) > -- > http://mail.python.org/mailman/listinfo/python-list From a24061 at ducksburg.com Sat Dec 31 15:55:09 2011 From: a24061 at ducksburg.com (Adam Funk) Date: Sat, 31 Dec 2011 20:55:09 +0000 Subject: mutually exclusive arguments to a constructor References: Message-ID: On 2011-12-30, G?nther Dietrich wrote: > Adam Funk wrote: > >>Suppose I'm creating a class that represents a bearing or azimuth, >>created either from a string of traditional bearing notation >>("N24d30mE") or from a number indicating the angle in degrees as >>usually measured in trigonometry (65.5, measured counter-clockwise >>from the x-axis). The class will have methods to return the same >>bearing in various formats. ... > You can determine the type of the input data by using isinstance() and > take the appropriate actions depending on this decision: > >>>> class MyClass(object): > ... def __init__(self, input_data): > ... if isinstance(input_data, basestring): > ... print "Do actions for string type input" > ... elif isinstance(input_data, float): > ... print "Do actions for float type input" > ... def get_output_data(self): > ... return "output data" Aha, I think I like this approach best, partly because I realized after writing my post that it might also be good to accept strings representing "pure" angles (e.g., "65d30m"). So I think I'll use isinstance *and then* check the input string against some regexes to determine whether it's in traditional surveying notation or trig notation in DMS. -- The generation of random numbers is too important to be left to chance. [Robert R. Coveyou] From a24061 at ducksburg.com Sat Dec 31 15:59:17 2011 From: a24061 at ducksburg.com (Adam Funk) Date: Sat, 31 Dec 2011 20:59:17 +0000 Subject: mutually exclusive arguments to a constructor References: Message-ID: <5m94t8x025.ln2@news.ducksburg.com> On 2011-12-30, Roy Smith wrote: > "But!", some C++/Java type bondage addicts might cry, "there's nothing > to prevent somebody from creating a DirectionIndicatingThingie directly, > bypassing the factory functions. There's no way to make the constructor > private!". To which the free-willed pythonistas would respond, "If it > hurts when you do that, don't do that". Actually one problem that can occur in large Java projects is that the package structure requires some things to have public constructors (even when you'd rather not do that) so the Factory class in the main package has access to them. -- English has perfect phonetic spelling. It just doesn't have phonetic pronunciation. [Peter Moylan] From tjreedy at udel.edu Sat Dec 31 16:09:09 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 31 Dec 2011 16:09:09 -0500 Subject: .format vs. % In-Reply-To: <4EFF615A.1050008@tim.thechases.com> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> <4EFF615A.1050008@tim.thechases.com> Message-ID: On 12/31/2011 2:24 PM, Tim Chase wrote: > On 12/31/11 12:57, Benjamin Kaplan wrote: >> format is a method of the string class. You store the string the same way >> you would any other. >> >> formatter = "Hello, {}" >> print(formatter.format("world")) > > Just to note that this syntax doesn't quite work in some earlier > versions (tested below in 2.6, which came stock in Debian Stable) Autonumbering of {} was introduced, at my suggestion, in 3.1 and included with 2.7 (I presume). > > >>> formatter = "Hello, {}" > >>> print(formatter.format("world")) > Traceback (most recent call last): > File "", line 1, in > ValueError: zero length field name in format > > > It needs to be spelled "{0}" > > -tkc > > -- Terry Jan Reedy From tjreedy at udel.edu Sat Dec 31 16:27:50 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 31 Dec 2011 16:27:50 -0500 Subject: Python powering giant screen in Times Square In-Reply-To: References: Message-ID: On 12/31/2011 2:05 PM, Jason Ford wrote: > Verizon has a giant HD screen in Times Square today showing a live > feed of social media activity ? and it's built entirely in Python. > > Our app, FeedMagnet, aggregates and curates social content and is > powering the screen. It has a Django front-end and pure Python backend > to talk to Facebook, Twitter, Instagram, Google+, Foursquare, and > other sources. > > So if you happen to be in Times Square for the crazy party today, tell > the people around you that big social media screen is powered by > Python! Great! I will look for it on the TV broadcasts. -- Terry Jan Reedy From d.l.goldsmith at gmail.com Sat Dec 31 17:13:01 2011 From: d.l.goldsmith at gmail.com (OlyDLG) Date: Sat, 31 Dec 2011 14:13:01 -0800 (PST) Subject: [Windows 7, Python 2.6] Can't write to a directory made w/ os.makedirs Message-ID: <50693cc2-93c9-481d-9fb1-42312e69b2d9@c42g2000prb.googlegroups.com> Hi! I'm working on a script utilizing os.makedirs to make directories to which I'm then trying to write files created by exe's spawned w/ subprocess.call; I'm developing in Stani's Python Editor, debugging using Winpdb. I've gotten to the point where subprocess.Popen._execute_child is raising a WindowsError(5,'Access is denied') exception. I've tried: setting the mode in the makedirs call; using os.chmod(, stat.S_IWRITE); and even setting a breakpoint before the subprocess.call and unsetting the read-only attribute of the pertinent directory tree using the Windows directory Properties dialog--which doesn't "take," if that's a clue--all to no avail. I suspect that maybe it has to do w/ not running Winpdb as administrator, but I'm not sure how to do that when starting Winpdb from w/in SPE, and in any event, it doesn't seem reasonable that there's no way to get this to work w/in the script itself. Please help! From fabiofz at gmail.com Sat Dec 31 18:20:50 2011 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Sat, 31 Dec 2011 21:20:50 -0200 Subject: PySide / PyQt autocompletion in IDEs In-Reply-To: <4EFCCC1F.7000605@gmail.com> References: <4EFCCC1F.7000605@gmail.com> Message-ID: On Thu, Dec 29, 2011 at 6:22 PM, Merwin wrote: > Hi, > > I would like to work with PyQt / PySide, but there is a small problem : > methods arguments are not completed by IDE's autocompletion. > > When, typing "PySide.", I correctly get the module's attributes, but when I > want to see what arguments are expected to a constructor / method, all I get > is *args, **kwargs. > > I perfectly understand why this happen, but do you know a way to get around > this ? > > I spend more time looking for arguments in Qt's documentation than > effectively programming, which is a problem :-/ In PyDev you can use a .api file (the same from QScintilla) to be used for the code-completion. Take a look at: http://pydev.org/manual_101_interpreter.html for details (I know it's available for PyQt, but I'm not sure about PySide, but if it's not, maybe it can be requested for the PySide guys). Cheers, Fabio From no.email at nospam.invalid Sat Dec 31 18:47:36 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Sat, 31 Dec 2011 15:47:36 -0800 Subject: Best Way To Bound Function Execution Time References: Message-ID: <7xpqf4e21z.fsf@ruckus.brouhaha.com> Tim Daneliuk writes: > Is there some standard Pythonic way to bound how long a function > call can run, after which time it is forcefully terminated? Basically, run it in a separate process and use os.kill to kill it. From niklasro at gmail.com Sat Dec 31 20:40:15 2011 From: niklasro at gmail.com (Niklas Rosencrantz) Date: Sat, 31 Dec 2011 17:40:15 -0800 (PST) Subject: InvalidResponseError: headers must be str In-Reply-To: <4eff1cdb$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> <4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com> <9589290.553.1325338274423.JavaMail.geo-discussion-forums@yqcb25> <4eff1cdb$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <19725208.758.1325382015274.JavaMail.geo-discussion-forums@yqja5> Thanks for the replies here. I will have patience but this bug is blocking my integration efforts. I tried logging the TCP packets with tcpdump and nothing special appeared. Niklas From invalid at invalid.invalid Sat Dec 31 21:36:47 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Sun, 1 Jan 2012 02:36:47 +0000 (UTC) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-12-31, Alexander Kapps wrote: > On 31.12.2011 19:23, Roy Smith wrote: > >>>> Why do I waste my time reading your pretentious self-important nonsense? >>> >>> http://xkcd.com/386/ >>> >>> ;) >> >> Why ROFLMAO when double-plus funny works just as well? > > xkcd/386 has been the excuse for replying to RR for ages and I still > don't understand why he gets that much advertence. Charity? Sympathy > for the lone and broken? Sadly, RR's post are often (in the supposed words of Wolfgang Pauli) "not even wrong". -- Grant From alex.kapps at web.de Sat Dec 31 23:53:06 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sun, 01 Jan 2012 05:53:06 +0100 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EFFE6B2.7040000@web.de> On 01.01.2012 03:36, Grant Edwards wrote: > On 2011-12-31, Alexander Kapps wrote: >> On 31.12.2011 19:23, Roy Smith wrote: >> >>>>> Why do I waste my time reading your pretentious self-important nonsense? >>>> >>>> http://xkcd.com/386/ >>>> >>>> ;) >>> >>> Why ROFLMAO when double-plus funny works just as well? >> >> xkcd/386 has been the excuse for replying to RR for ages and I still >> don't understand why he gets that much advertence. Charity? Sympathy >> for the lone and broken? > > Sadly, RR's post are often (in the supposed words of Wolfgang Pauli) > "not even wrong". > I'm sure, RR is now jumping up high in rapture for being compared to high-profile scientist geniuses. Move fuel for his self-affirmation. I'll give my entire kingdom (or important body-parts, in case my kingdom isn't enough) if people would just understand that perfection(ists) is/are *the most* dangerous thing possible. From roy at panix.com Thu Dec 1 00:00:52 2011 From: roy at panix.com (Roy Smith) Date: Thu, 01 Dec 2011 00:00:52 -0500 Subject: Disable readline References: <4ed6ffed$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4ed6ffed$0$29986$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Is there a way to disable readline support in the interactive interpreter > at runtime? Either from within an existing session, or when the session > starts up will do. I'm assuming Python uses the standard GNU readline(). If that's correct, then you can configure its behavior by editing your ~/.inputrc file. Try "man readline". I don't see any global "disable readline" flag, but you may be able to get that effect by deleting all the key mappings, or some such silliness. Another possibility is setting your TERM environment variable to something that readline can't support: ~$ TERM=asr33 ~$ python Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. Cannot read termcap database; using dumb terminal settings. Cannot read termcap database; using dumb terminal settings. Cannot read termcap database; using dumb terminal settings. >>> > If all else fails, are there any traps or pitfalls in installing a second > Python installation with readline support disabled? None at all, as long as you keep them from stomping on each other. The easiest method is probably to use virtualenv. BTW, readline is the coolest, awesomist, most frabjulously gnarly thing to be invented since the pointed stick. The idea that somebody would want to turn it off (even for testing) disturbs me deeply. From roy at panix.com Thu Dec 1 00:02:35 2011 From: roy at panix.com (Roy Smith) Date: Thu, 01 Dec 2011 00:02:35 -0500 Subject: Need some IPC pointers References: <4ED69A1A.3080609@gmail.com> Message-ID: In article , Alec Taylor wrote: > Sure, I'll give you some pointers: > > 0x3A28213A > 0x6339392C > 0x7363682E What, no 0xDEADBEEF ??? From alec.taylor6 at gmail.com Thu Dec 1 00:21:53 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Thu, 1 Dec 2011 16:21:53 +1100 Subject: Need some IPC pointers In-Reply-To: References: <4ED69A1A.3080609@gmail.com> Message-ID: :P On Thu, Dec 1, 2011 at 4:02 PM, Roy Smith wrote: > In article , > ?Alec Taylor wrote: > >> Sure, I'll give you some pointers: >> >> 0x3A28213A >> 0x6339392C >> 0x7363682E > > What, no 0xDEADBEEF ??? > -- > http://mail.python.org/mailman/listinfo/python-list From rustompmody at gmail.com Thu Dec 1 00:32:30 2011 From: rustompmody at gmail.com (rusi) Date: Wed, 30 Nov 2011 21:32:30 -0800 (PST) Subject: Python crashes on segmentation error References: <8bffed0f-1050-418a-834d-f7060a899ab7@h5g2000yqk.googlegroups.com> Message-ID: <53778994-faa7-4b48-a568-85dd3daed693@n6g2000vbg.googlegroups.com> On Dec 1, 3:29?am, Terry Reedy wrote: > On 11/30/2011 7:58 AM, Christian Heimes wrote: > > > > > > > > > > > Am 30.11.2011 11:42, schrieb Ben Richardson: > >> Python crashes every time i run the following command? > > >>>>> import cv > >> Segmentation fault: 11 > > >> Python quit unexpectedly - any help would be greatly appreciated - > >> thankyou in advance :) > > >> Here is crash report? > > [...] > > >> Thread 0 Crashed:: Dispatch queue: com.apple.main-thread > >> 0 ? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?000000000000000000 0 + 0 > >> 1 ? org.python.python ? ? ? ? ? ? ? ? ?0x00000001006c7885 PyImport_Import > >> + 121 > >> 2 ? org.python.python ? ? ? ? ? ? ? ? ?0x00000001006c7a1e > >> PyImport_ImportModule + 32 > >> 3 ? cv.so ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?0x00000001004f5082 initcv + 18 > >> 4 ? org.python.python ? ? ? ? ? ? ? ? ?0x00000001000dc071 > >> _PyImport_LoadDynamicModule + 177 > > > It looks like you have a faulty 3rd party library with an extension > > module called cv.so that is causing the segfault. The segfault occurs > > either inside or during the import of the cv module. initcv() is the > > init function of the cv module that is called automatically during the > > import. > > In other words, contact the authors of the cv module. Or perhaps first > check to make sure that the cv.so build you are running is intended to > be compatible with the Python build you are running. > > -- > Terry Jan Reedy IOW python is a very safe language when python means python It can be very unsafe when it includes and tries to link carelessly written C. A safer intermediate option may be to try via ctypes [Of course assuming that clarifying from the authors of cv what is their intended use scenario is somehow a non-option] From bahamutzero8825 at gmail.com Thu Dec 1 00:48:54 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 30 Nov 2011 23:48:54 -0600 Subject: Need some IPC pointers In-Reply-To: References: <4ED69A1A.3080609@gmail.com> Message-ID: <4ED71546.3090909@gmail.com> On 11/30/2011 10:35 PM, Alec Taylor wrote: > Sure, I'll give you some pointers: I almost rephrased the subject line because I knew someone would make that joke. :P -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From steve+comp.lang.python at pearwood.info Thu Dec 1 01:42:19 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 01 Dec 2011 06:42:19 GMT Subject: Disable readline References: <4ed6ffed$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ed721cb$0$29986$c3e8da3$5496439d@news.astraweb.com> On Thu, 01 Dec 2011 00:00:52 -0500, Roy Smith wrote: > Another possibility is setting your TERM environment variable to > something that readline can't support: > > ~$ TERM=asr33 > ~$ python > Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. > build 5646)] on darwin Type "help", "copyright", "credits" or "license" > for more information. Cannot read termcap database; > using dumb terminal settings. > Cannot read termcap database; > using dumb terminal settings. > Cannot read termcap database; > using dumb terminal settings. Damn, my python is smarter than your python. steve at runes:~$ TERM=asr33 steve at runes:~$ python Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> And readline continues to work :/ I think I'll install from source a build with readline disabled. > BTW, readline is the coolest, awesomist, most frabjulously gnarly thing > to be invented since the pointed stick. The idea that somebody would > want to turn it off (even for testing) disturbs me deeply. I know! I don't use more than about 1% of what readline offers, but I can't imagine not using it. -- Steven From nmb.ten at gmail.com Thu Dec 1 02:03:26 2011 From: nmb.ten at gmail.com (=?UTF-8?B?0JrQsNC30LHQtdC6?=) Date: Wed, 30 Nov 2011 23:03:26 -0800 (PST) Subject: Python, django datetime to string conversation helper Message-ID: <098fc72f-90c0-4726-8575-7317a0c07ab2@v29g2000yqv.googlegroups.com> Online tool allows to select datetime to string format directives quickly and to check result. From nmb.ten at gmail.com Thu Dec 1 02:07:55 2011 From: nmb.ten at gmail.com (=?UTF-8?B?0JrQsNC30LHQtdC6?=) Date: Wed, 30 Nov 2011 23:07:55 -0800 (PST) Subject: Python, django datetime to string conversation helper References: <098fc72f-90c0-4726-8575-7317a0c07ab2@v29g2000yqv.googlegroups.com> Message-ID: <953a5e7d-772e-486b-851d-922480e4e838@w15g2000yqc.googlegroups.com> On 1 ???, 11:03, ?????? wrote: > Online tool allows to select datetime to string format directives > quickly and to check result. Sorry, here is the link. http://datetostr.org From rosuav at gmail.com Thu Dec 1 03:21:10 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 1 Dec 2011 19:21:10 +1100 Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: On Thu, Dec 1, 2011 at 2:15 PM, Roy Smith wrote: > for suffix in [''] + [str(i) for i in xrange(-1, -20, -1)]: > > It generates the right sequence of strings. ?But, if you came upon that > code, would it make sense to you, or would you spend half the afternoon > trying to figure out what it did and the other half of the afternoon > ranting about the deranged lunatic who wrote it? That's a self-contained piece of code.If I came upon it, I'd probably copy and paste it to IDLE, see what it comes up with, and proceed from there. Deranged you may be, but so long as code can be dropped into an interactive interpreter, it's fine. ChrisA From arnodel at gmail.com Thu Dec 1 03:35:15 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Thu, 1 Dec 2011 08:35:15 +0000 Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: On 1 December 2011 03:15, Roy Smith wrote: > I need to try a bunch of names in sequence until I find one that works > (definition of "works" is unimportant). ?The algorithm is: > > 1) Given a base name, "foo", first see if just plain "foo" works. > > 2) If not, try "foo-1", "foo-2", and so on > > 3) If you reach "foo-20", give up. > > What would you say if you saw this: > > for suffix in [''] + [str(i) for i in xrange(-1, -20, -1)]: > It's a little obfuscated ;) I would go for the simple: for i in xrange(21): suffix = "-%s" % i if i else "" .... -- Arnaud From janedenone at googlemail.com Thu Dec 1 05:01:33 2011 From: janedenone at googlemail.com (janedenone) Date: Thu, 1 Dec 2011 02:01:33 -0800 (PST) Subject: Reading twice from STDIN Message-ID: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> Hi, I would like to read from a pipe, parse the input and ask the user what to do next: message = sys.stdin.read() # message is parsed and URLs are printed as a list to choose from... selected_index = raw_input('Which URL to open?') Calling raw_input() always raises in an EOFError. I tried reopening and resetting sys.stdin, but nothing worked (at least on OX X 10.7, see http://stackoverflow.com/questions/8034595/python-raw-input-following-sys-stdin-read-throws-eoferror). I am surprised to find that a seemingly trivial task cannot be accomplished with Python 2.7 on a current Mac. Or am I missing something simple? - Jan From hniksic at xemacs.org Thu Dec 1 05:21:13 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 01 Dec 2011 11:21:13 +0100 Subject: unpack('>f', b'\x00\x01\x00\x00') References: <6b7251ef-3479-412f-8acb-882be1e25633@n35g2000yqf.googlegroups.com> Message-ID: <87obvsbnja.fsf@xemacs.org> Chris Rebert writes: > C does not have a built-in fixed-point datatype, so the `struct` > module doesn't handle fixed-point numbers directly. The built-in decimal module supports fixed-point arithmetic, but the struct module doesn't know about it. A bug report (or patch) by someone who works with binary representations of fixed-point would be a good start to improve it. From hv at tbz-pariv.de Thu Dec 1 05:25:02 2011 From: hv at tbz-pariv.de (Thomas Guettler) Date: Thu, 01 Dec 2011 11:25:02 +0100 Subject: get date from email Message-ID: <9jp2vuFrg4U1@mid.individual.net> Hi, up to now I use this code to parse date values from emails: msg=email.message_from_file(open(file_name)) date=None date_str=msg.get('date') if date_str: date_tuple=email.utils.parsedate_tz(date_str) if date_tuple: date=datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple)) if date: ... # valid date found Somehow this looks too complicated. Any chance to integrate the datetime module into the email module? related: http://stackoverflow.com/questions/1790795/python-parsing-date-with-timezone-from-an-email -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de From gelonida at gmail.com Thu Dec 1 05:37:39 2011 From: gelonida at gmail.com (Gelonida N) Date: Thu, 01 Dec 2011 11:37:39 +0100 Subject: order independent hash? In-Reply-To: References: Message-ID: On 11/30/2011 01:32 PM, Neal Becker wrote: > I like to hash a list of words (actually, the command line args of my program) > in such a way that different words will create different hash, but not sensitive > to the order of the words. Any ideas? > Do youmean hash like digest like md5sum / sha1 ? You should sort the words alphabetically, concatenate them with a space or any character, that will NEVER be part of a word and calulate the hash. If words can exist multiple times, then youhad tu uniqufy them (u using a python dict / set) first. From gelonida at gmail.com Thu Dec 1 05:41:53 2011 From: gelonida at gmail.com (Gelonida N) Date: Thu, 01 Dec 2011 11:41:53 +0100 Subject: Reading twice from STDIN In-Reply-To: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> Message-ID: On 12/01/2011 11:01 AM, janedenone wrote: Hi, > > I would like to read from a pipe, parse the input and ask the user > what to do next: > > message = sys.stdin.read() With above line you said, that you want to read ALL data from stdin, so it's obvious that any following command will be unable to reda anything from standartd in Thus the EOF error. If you want to get input you had to read directly from the console (tty) and NOT from stdin. Stdin has already been consumed. It is possible in python to get the tty related to your console window and read from it. Unfortunately I don't kno whte commands by heart and I don't have time now to look it up. Perhaps somebody else can point you in the right direction. If not I'll it up till tomorrow. From markpackt at gmail.com Thu Dec 1 05:53:51 2011 From: markpackt at gmail.com (Mark) Date: Thu, 1 Dec 2011 02:53:51 -0800 (PST) Subject: Complete beginner, any help appreciated :) - For Loops Message-ID: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Hi there, I'm a complete beginner to Python and, aside from HTML and CSS, to coding in general. I've spent a few hours on it and think I understand most of the syntax. However, I'm wondering a bit about For Loops. I know that the basic syntax for them is to define a list, and then to use something like: for x in y However, what does "for" and "in" mean in this context? Can anyone help me to understand this? I know it's a really basic question, but hopefully it will see me on my way to coding properly :) Thanks a lot. From moky.math at gmail.com Thu Dec 1 06:14:01 2011 From: moky.math at gmail.com (Laurent Claessens) Date: Thu, 01 Dec 2011 12:14:01 +0100 Subject: Complete beginner, any help appreciated :) - For Loops In-Reply-To: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Message-ID: > for x in y > > However, what does "for" and "in" mean in this context? It means basically the same as in Englsish Does the following links answer the question ? http://www.ibiblio.org/g2swap/byteofpython/read/for-loop.html http://dsnra.jpl.nasa.gov/software/Python/diveintopython.pdf (page 58) Have a nice day Laurent From pedro.h.souto at gmail.com Thu Dec 1 06:32:11 2011 From: pedro.h.souto at gmail.com (Pedro Henrique G. Souto) Date: Thu, 01 Dec 2011 09:32:11 -0200 Subject: Complete beginner, any help appreciated :) - For Loops In-Reply-To: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Message-ID: <4ED765BB.7050305@gmail.com> On 01/12/2011 08:53, Mark wrote: > Hi there, > > I'm a complete beginner to Python and, aside from HTML and CSS, to coding in general. I've spent a few hours on it and think I understand most of the syntax. > > However, I'm wondering a bit about For Loops. I know that the basic syntax for them is to define a list, and then to use something like: > > for x in y > > However, what does "for" and "in" mean in this context? Can anyone help me to understand this? I know it's a really basic question, but hopefully it will see me on my way to coding properly :) > > Thanks a lot. That means (in a free translation) "For each one of 'x' in 'y', do this" 'y' is a list, for example, then it means: "For each one of the elements of the list 'y' (the element on the current iteration is named 'x'), do this" Good Luck! Att; Pedro Henrique G. Souto ??????????????? ? ???d?_?b??? ? ??????????????? From fred.sells at adventistcare.org Thu Dec 1 06:51:10 2011 From: fred.sells at adventistcare.org (Sells, Fred) Date: Thu, 1 Dec 2011 06:51:10 -0500 Subject: Py and SQL In-Reply-To: References: Message-ID: I find it easier to code like this Sql = ???select yadda, yadda, yadda FROM a,b,c Where this=that ORDER BY deudderting??? With the appropriate %s(varname) and % against a dictionary rather than positional args, but that?s just me. From: python-list-bounces+frsells=adventistcare.org at python.org [mailto:python-list-bounces+frsells=adventistcare.org at python.org] On Behalf Of Jerry Hill Sent: Wednesday, November 30, 2011 5:15 PM To: Verde Denim Cc: Python list Subject: Re: Py and SQL On Wed, Nov 30, 2011 at 3:30 PM, Verde Denim wrote: dbCursor1.execute('select lpad(' ', 2*level) || c "Privilege, Roles and Users" from ( select null p, name c from system_privilege_map where name like upper(\'%&enter_privliege%\') union select granted_role p, grantee c from dba_role_privs union select privilege p, grantee c from dba_sys_privs) start with p is null connect by p = prior c') I think this is your problem. Your string is delimited with single quotes on the outside ('), but you also have a mix of single and double quotes inside your string. If you were to assign this to a variable and print it out, you would probably see the problem right away. You have two options. First, you could flip the outer quotes to double quotes, then switch all of the quotes inside the string to single quotes (I think that will work fine in SQL). Second, you could use a triple-quoted string by switching the outer quotes to ''' or """. Doing that would let you mix whatever kinds of quotes you like inside your string, like this (untested): sql = '''select lpad(' ', 2*level) || c "Privilege, Roles and Users" from ( select null p, name c from system_privilege_map where name like upper(\'%&enter_privliege%\') union select granted_role p, grantee c from dba_role_privs union select privilege p, grantee c from dba_sys_privs) start with p is null connect by p = prior c''' dbCursor1.execute(sql) Once you do that, I think you will find that the "&enter_priviliege" bit in your SQL isn't going to do what you want. I assume you're expecting that to automatically pop up some sort of dialog box asking the user to enter a value for that variable? That isn't going to happen in python. That's a function of the database IDE you use. You'll need to use python to ask the user for the privilege level, then substitute it into the sql yourself. -- Jerry -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Dec 1 06:56:44 2011 From: d at davea.name (Dave Angel) Date: Thu, 01 Dec 2011 06:56:44 -0500 Subject: Complete beginner, any help appreciated :) - For Loops In-Reply-To: <4ED765BB.7050305@gmail.com> References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> <4ED765BB.7050305@gmail.com> Message-ID: <4ED76B7C.9020005@davea.name> On 12/01/2011 06:32 AM, Pedro Henrique G. Souto wrote: > > On 01/12/2011 08:53, Mark wrote: >> Hi there, >> >> I'm a complete beginner to Python and, aside from HTML and CSS, to >> coding in general. I've spent a few hours on it and think I >> understand most of the syntax. >> >> However, I'm wondering a bit about For Loops. I know that the basic >> syntax for them is to define a list, and then to use something like: >> >> for x in y >> >> However, what does "for" and "in" mean in this context? Can anyone >> help me to understand this? I know it's a really basic question, but >> hopefully it will see me on my way to coding properly :) >> >> Thanks a lot. > > That means (in a free translation) > > "For each one of 'x' in 'y', do this" > > 'y' is a list, for example, then it means: "For each one of the > elements of the list 'y' (the element on the current iteration is > named 'x'), do this" > > And if y is a string, it means for each character in y, do this x = the character (then do the indented part) And if y is an arbitrary iterable, it means for each thing the iterable produces x = the thing (then do the indented part) Each time you go through the loop, x will be equal to the next item in the sequence. So you can systematically process the items in the list (or characters in the string, or values from an iterable), one at a time. The only real caveat is to not do something that would change the list (etc.) while you're looping through it. What are some other examples of iterables? infile = open("myfile.txt", "r") #infile is an iterable for line in infile: print "**", line, "**" for val in (1, 4, 9, 2007) for x in xrange(50000000): #this does the same as range(), but doesn't actually build the list. #this way, we don't run out of memory You can also code your own generator function, which is an iterable, and may be used like the above. Some things in this message assume Python 2.x. in Python 3, range works differently, as does print. -- DaveA From markpackt at gmail.com Thu Dec 1 07:15:50 2011 From: markpackt at gmail.com (Mark) Date: Thu, 1 Dec 2011 04:15:50 -0800 (PST) Subject: Complete beginner, any help appreciated :) - For Loops In-Reply-To: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Message-ID: <28925560.347.1322741750955.JavaMail.geo-discussion-forums@prij11> Thanks a lot for the answers everyone, I really appreciate you getting back to me so quickly. I think that I understand where I am with this now :) From ben+python at benfinney.id.au Thu Dec 1 08:36:37 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 02 Dec 2011 00:36:37 +1100 Subject: Complete beginner, any help appreciated :) - For Loops References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Message-ID: <87aa7c4dne.fsf@benfinney.id.au> Mark writes: > I'm a complete beginner to Python and, aside from HTML and CSS, to > coding in general. I've spent a few hours on it and think I understand > most of the syntax. Welcome! You should work your way through the Python tutorial, from beginning to end . Actually do each exercise, experiment if you have questions, and then continue. Repeat until done. -- \ ?Ours is a world where people don't know what they want and are | `\ willing to go through hell to get it.? ?Donald Robert Perry | _o__) Marquis | Ben Finney From ndbecker2 at gmail.com Thu Dec 1 08:55:38 2011 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 01 Dec 2011 08:55:38 -0500 Subject: order independent hash? References: Message-ID: Gelonida N wrote: > On 11/30/2011 01:32 PM, Neal Becker wrote: >> I like to hash a list of words (actually, the command line args of my >> program) in such a way that different words will create different hash, but >> not sensitive >> to the order of the words. Any ideas? >> > Do youmean hash like digest like md5sum / sha1 ? > > > You should sort the words alphabetically, concatenate them with a space > or any character, that will NEVER be part of a word and calulate the hash. > > If words can exist multiple times, then youhad tu uniqufy them (u using > a python dict / set) first. Yes that sounds just like what I wanted - thanks! From nicholas.dokos at hp.com Thu Dec 1 09:30:11 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Thu, 01 Dec 2011 09:30:11 -0500 Subject: Disable readline In-Reply-To: Message from Steven D'Aprano of "01 Dec 2011 06:42:19 GMT." <4ed721cb$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <4ed6ffed$0$29986$c3e8da3$5496439d@news.astraweb.com> <4ed721cb$0$29986$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8137.1322749811@alphaville.dokosmarshall.org> Steven D'Aprano wrote: > On Thu, 01 Dec 2011 00:00:52 -0500, Roy Smith wrote: > > > Another possibility is setting your TERM environment variable to > > something that readline can't support: > > > > ~$ TERM=asr33 > > ~$ python > > Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) [GCC 4.2.1 (Apple Inc. > > build 5646)] on darwin Type "help", "copyright", "credits" or "license" > > for more information. Cannot read termcap database; > > using dumb terminal settings. > > Cannot read termcap database; > > using dumb terminal settings. > > Cannot read termcap database; > > using dumb terminal settings. > > > Damn, my python is smarter than your python. > > steve at runes:~$ TERM=asr33 > steve at runes:~$ python > Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) > [GCC 4.4.5] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> > > And readline continues to work :/ > Two things: o Is TERM exported? Maybe Roy's is and yours isn't. o Is asr33 in your termcap database? Try export TERM=dumb perhaps? Nick > > I think I'll install from source a build with readline disabled. > > > > BTW, readline is the coolest, awesomist, most frabjulously gnarly thing > > to be invented since the pointed stick. The idea that somebody would > > want to turn it off (even for testing) disturbs me deeply. > > I know! I don't use more than about 1% of what readline offers, but I > can't imagine not using it. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > From dihedral88888 at googlemail.com Thu Dec 1 10:35:32 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 07:35:32 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> On Wednesday, November 30, 2011 8:47:13 PM UTC+8, Peter Otten wrote: > Neal Becker wrote: > > > I like to hash a list of words (actually, the command line args of my > > program) in such a way that different words will create different hash, > > but not sensitive to the order of the words. Any ideas? > > You mean a typical python hash value which would be /likely/ to differ for > different lists and /guaranteed/ to be equal for equal lists is not good > enough? Because that would be easy: > > args = sys.argv[1:] > hash(tuple(sorted(args))) # consider duplicate args I knew a hash can replace a bi-directional linked list. The value can be a multi-field string to be parsed for further actions. Is this what you are asking? From dihedral88888 at googlemail.com Thu Dec 1 10:35:32 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 07:35:32 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> On Wednesday, November 30, 2011 8:47:13 PM UTC+8, Peter Otten wrote: > Neal Becker wrote: > > > I like to hash a list of words (actually, the command line args of my > > program) in such a way that different words will create different hash, > > but not sensitive to the order of the words. Any ideas? > > You mean a typical python hash value which would be /likely/ to differ for > different lists and /guaranteed/ to be equal for equal lists is not good > enough? Because that would be easy: > > args = sys.argv[1:] > hash(tuple(sorted(args))) # consider duplicate args I knew a hash can replace a bi-directional linked list. The value can be a multi-field string to be parsed for further actions. Is this what you are asking? From d at davea.name Thu Dec 1 10:52:46 2011 From: d at davea.name (Dave Angel) Date: Thu, 01 Dec 2011 10:52:46 -0500 Subject: order independent hash? In-Reply-To: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> Message-ID: <4ED7A2CE.6070306@davea.name> On 12/01/2011 10:35 AM, 88888 Dihedral wrote: > On Wednesday, November 30, 2011 8:47:13 PM UTC+8, Peter Otten wrote: >> Neal Becker wrote: >> >>> I like to hash a list of words (actually, the command line args of my >>> program) in such a way that different words will create different hash, >>> but not sensitive to the order of the words. Any ideas? >> You mean a typical python hash value which would be /likely/ to differ for >> different lists and /guaranteed/ to be equal for equal lists is not good >> enough? Because that would be easy: >> >> args = sys.argv[1:] >> hash(tuple(sorted(args))) # consider duplicate args > I knew a hash can replace a bi-directional linked list. > The value can be a multi-field string to be parsed for further actions. > Is this what you are asking? A hash is a number, so I don't see how it can replace any kind of linked list. Perhaps you're thinking of some other language. -- DaveA From dihedral88888 at googlemail.com Thu Dec 1 11:17:08 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 08:17:08 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> Message-ID: <31562283.427.1322756228499.JavaMail.geo-discussion-forums@pret21> On Thursday, December 1, 2011 11:52:46 PM UTC+8, Dave Angel wrote: > On 12/01/2011 10:35 AM, 88888 Dihedral wrote: > > On Wednesday, November 30, 2011 8:47:13 PM UTC+8, Peter Otten wrote: > >> Neal Becker wrote: > >> > >>> I like to hash a list of words (actually, the command line args of my > >>> program) in such a way that different words will create different hash, > >>> but not sensitive to the order of the words. Any ideas? > >> You mean a typical python hash value which would be /likely/ to differ for > >> different lists and /guaranteed/ to be equal for equal lists is not good > >> enough? Because that would be easy: > >> > >> args = sys.argv[1:] > >> hash(tuple(sorted(args))) # consider duplicate args > > I knew a hash can replace a bi-directional linked list. > > The value can be a multi-field string to be parsed for further actions. > > Is this what you are asking? > A hash is a number, so I don't see how it can replace any kind of linked > list. Perhaps you're thinking of some other language. > > -- > > DaveA A long number of a varied length allowed but can interpreted by the programmer. From dihedral88888 at googlemail.com Thu Dec 1 11:17:08 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 08:17:08 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> Message-ID: <31562283.427.1322756228499.JavaMail.geo-discussion-forums@pret21> On Thursday, December 1, 2011 11:52:46 PM UTC+8, Dave Angel wrote: > On 12/01/2011 10:35 AM, 88888 Dihedral wrote: > > On Wednesday, November 30, 2011 8:47:13 PM UTC+8, Peter Otten wrote: > >> Neal Becker wrote: > >> > >>> I like to hash a list of words (actually, the command line args of my > >>> program) in such a way that different words will create different hash, > >>> but not sensitive to the order of the words. Any ideas? > >> You mean a typical python hash value which would be /likely/ to differ for > >> different lists and /guaranteed/ to be equal for equal lists is not good > >> enough? Because that would be easy: > >> > >> args = sys.argv[1:] > >> hash(tuple(sorted(args))) # consider duplicate args > > I knew a hash can replace a bi-directional linked list. > > The value can be a multi-field string to be parsed for further actions. > > Is this what you are asking? > A hash is a number, so I don't see how it can replace any kind of linked > list. Perhaps you're thinking of some other language. > > -- > > DaveA A long number of a varied length allowed but can interpreted by the programmer. From rosuav at gmail.com Thu Dec 1 11:18:29 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 03:18:29 +1100 Subject: order independent hash? In-Reply-To: <4ED7A2CE.6070306@davea.name> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> Message-ID: On Fri, Dec 2, 2011 at 2:52 AM, Dave Angel wrote: > On 12/01/2011 10:35 AM, 88888 Dihedral wrote: >> I knew a hash can replace a bi-directional linked list. >> The value ?can be a multi-field string ?to be parsed for further actions. >> Is this what you are asking? > > A hash is a number, so I don't see how it can replace any kind of linked > list. ?Perhaps you're thinking of some other language. A hashtable is a form of data structure that involves hashing values (ie calculating hashes for them) and storing them for easier retrieval than a simple linked list offers. That may be what you're thinking of. ChrisA From adrianp at bringtechnology.com Thu Dec 1 12:41:22 2011 From: adrianp at bringtechnology.com (Adrian Powell) Date: Thu, 01 Dec 2011 12:41:22 -0500 Subject: platform issues? Message-ID: <20111201124122.8818@web003.roc2.bluetie.com> I'm new to python and I'm trying to get a twitter client running on a new machine but it keeps on failing. I tracked the problem down to an issue opening URLs and wrote this little test case: import urllib2 url = 'http://www.google.com/' opener = urllib2.build_opener() url_data = opener.open(url).read url_data When I run that on a dev machine it works fine, but when it's on one of our servers it crashes: >>> import urllib2 >>> url = 'http://www.google.com/' >>> opener = urllib2.build_opener() >>> url_data = opener.open(url).read Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/urllib2.py", line 391, in open response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py", line 409, in _open '_open', req) File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 1173, in http_open return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/python2.7/urllib2.py", line 1148, in do_open raise URLError(err) urllib2.URLError: Our servers have the same version of python and we're running the same OS (Fedora 14). Can anyone think of what might be causing this problem, or why it works on most (but not all) machines? Thanks From redcat at catfolks.net Thu Dec 1 13:00:41 2011 From: redcat at catfolks.net (Redcat) Date: 1 Dec 2011 18:00:41 GMT Subject: platform issues? References: Message-ID: <9jptm8Fug2U9@mid.individual.net> > Our servers have the same version of python and we're running the same > OS (Fedora 14). Can anyone think of what might be causing this problem, > or why it works on most (but not all) machines? > > Thanks Is the server able to resolve www.google.com properly? If so, are you POSITIVE that the "url = " line on the server has no typos? From devplayer at gmail.com Thu Dec 1 13:03:53 2011 From: devplayer at gmail.com (DevPlayer) Date: Thu, 1 Dec 2011 10:03:53 -0800 (PST) Subject: Using the Python Interpreter as a Reference References: <79379487-0081-4067-92ed-c6717652e1ff@y7g2000vbe.googlegroups.com> <4eb0af60-26b3-45d5-8aff-566505003d6a@m10g2000vbc.googlegroups.com> <4ed2cddc$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed491fe$0$14018$c3e8da3$76491128@news.astraweb.com> Message-ID: On Nov 29, 3:04?am, Steven D'Aprano wrote: > On Tue, 29 Nov 2011 12:49:49 +1100, Chris Angelico wrote: > > On Tue, Nov 29, 2011 at 11:54 AM, DevPlayer wrote: > >> To me, I would think the interpreter finding the coder's intended > >> indent wouldn't be that hard. And just make the need for consistant > >> spaces or tabs irrevelent simply by reformatting the indent as > >> expected. Pretty much all my text editors can. > > > The trouble with having a language declaration that "a tab is equivalent > > to X spaces" is that there's no consensus as to what X should be. > > Historically X has always been 8, and quite a few programs still assume > > this. I personally like 4. Some keep things narrow with 2. You can even > > go 1 - a strict substitution of \t with \x20. Once you declare it in > > your language, you immediately break everyone who uses anything > > different. > > Why should we enforce how many spaces a tab is set to? That is literally > only visible to the editor, not the compiler. (Unless the parser is > particularly stupid and merely substitutes X spaces for a tab every time > it sees one.) > > Mixed spaces and tabs in an indent are ambiguous, and so raises > SyntaxError (or at least it *should* raise SyntaxError). But separate > code blocks can use different absolute indent levels without ambiguity, > so long as the relative indentation is consistent: > > def ham(): # tab stops at 4 and 8 > ? ? do(a) > ? ? do(b) > ? ? if c: > ? ? ? ? do(c) > ? ? else: > ? ? ? ? do(d) > > def spam(): # tab stops at 8 and 12 > ? ? ? ? do(a) > ? ? ? ? do(b) > ? ? ? ? if c: > ? ? ? ? ? ? do(c) > ? ? ? ? else: > ? ? ? ? ? ? do(d) > > There is no meaningful difference in indentation between ham and spam > above. In either case, I could replace spaces with tabs to get the same > relative indents regardless of the absolute indentation. > > I can appreciate the aesthetic argument that *within a single file*, > indentation should be consistent. But it's not logically necessary for > the parser: it need only be consistent within a single code unit > (function or class usually). In other words: syntax should only care > about relative indentation, absolute indentation belongs as a coding > standard. > > -- > Steven Great point. Your point is why I started writting my own little Python parser/scanner (as an pet project/lesson project) and is in part, why I wrote my little reindent() in the first place. I am/was trying to figure out the algorithim for how Python's indent/dedents made logical "code blocks" or "closures" or whatever they are called. That and see if there was a way to interpret docstrings in various formats. I often get indentation syntax errors when I cut and paste stackflow.com, devprogrammer.com, daniweb.com, etc. code snippets into my various IDEs/ editors and I wanted to make a little tabnanny of my own. Some of the IDEs and editors (Editra) allow plugins. So I wanted to get a baseline for the various plugins. So I call the reindent() after I call the blocker(), which determines a block by -releative- indent/dedent as +1 or, 0 or -1 whether that be +4 spaces, +2 spaces, +1 tab or -3 spaces, whatever...; as you, Steve, mentioned indent is not fixed but relative. (btw I'v posted this in this topic because I thought it could contribute to how Unit's choice of how indents are handled my benefit from this discussion). My thoughts on tab verse space chars;, for most old and even current commandline interfaces, text editors, and historically line printers, tabs simply acted like a macro to move x number of fixed spaces. It wasn't until varible width fonts, kerning, and other advanced printing techniques became mainstream<- did space chars differ in meaning from tab chars, exception perhaps being file storage space (way back in the day). I only say this because I can't stand hitting the right and left arrow keys or using multiple fingers to move left and right 3 more times then I have to while editing my text. I -feel- I move faster around my text files with tabs then I do with 4 spaces. They only annoyance I've had with Python's flexiblity with indent using tab and or spaces is the docstrings. \t = tab def myfunc(): \t"""here is my docstring that bla bla bla's \t\tand indented do da days... \t""" verse (s = space) def myfunc(): ssss"""here is my docstring that bla bla bla's ssssssssand indented do da days... ssss""" verse space = tab or space def myfunc(): "here is my docstring that bla bla bla's"\ " and indented do da days..."\ "\n" verse the legal and sometimes seen form: def myfunc(): \tmyval = somefunc( arg0, arg1, arg2, lotsofargs, \t\tsssspaces_to_align_arg_to_arg0) If you strip the text and display it using basic reformatting in fixed font, like in the python interpreter, no biggy. But when you want to display the docstrings in an advanced font, like that of an ide with truetype fonts, formatting gets lost without having the app account for all the various ways Python coders do their docstrings. So if you wanted a consistant flexible (really) long term way to autogenerate and publish your and others code/packages/libraries, it gets tricky. Ask big-python-library maintainers know, many people go to chat and forums simply because autogenerated documention wasn't clear to the non-expert, thus repeating the same questions over and over. AND perhaps in the beginning it was well documented but since new constructs and refactoring of code, often auto-generated documentation can get "reduced" to something unuseful to those not-in- the-know. If whitespace indentation is used to define some construct of a programming language, then how people like to document their code (by using various forms of whitespace) could impact how well -some- people learn the new language. Auto-generated documention could very well be influenced by that decision. What form of whitespace indentation could effect readability/ understandable code. For example reducing indentation to 2 spaces instead of 4 might encourage some coders to use even deeper indentation because the code could fix more stuff (it's not being used up by whitespace indentation). Many feel that's a bad thing (I do too). (not knowing Unit code; I'll use Python) class Wow(object): def myfunc(self): a = func() if a: small_indent = something() if small_indent: getting_silly = godeeper() while getting_silly > a: bet_you_get_the_point += getting_silly - code_makes_no_sense() for i in range(100: if bet_you_get_the_point in SOMEGLOBAL['part1'][0]: display(a, small_indent, getting_silly, bet_you_get_the_point) Well, that may be a little hyperbolic. But with 2 spaces you can encourage coders to get very deep, indentially, and still fit 80 chars. (newly invented word: indentially. But I don't care, you get the point.) To me, each viewer of that code should be able to put the indent to something that fits their taste. And lets face it. most source code today is viewed in some gui text editor that can reinterpret that indent to whatever, without generating interperter indentation errors later. The only exception to consistancy would again be docstrings or more precisely string blocks, or block strings, or tripe quoted, whatever you want to call them. My words are not meant to garner flames or anything, just to cause thought; From jeanmichel at sequans.com Thu Dec 1 13:41:59 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 01 Dec 2011 19:41:59 +0100 Subject: platform issues? In-Reply-To: <20111201124122.8818@web003.roc2.bluetie.com> References: <20111201124122.8818@web003.roc2.bluetie.com> Message-ID: <4ED7CA77.6020405@sequans.com> Adrian Powell wrote: > I'm new to python and I'm trying to get a twitter client running on a new machine but it keeps on failing. I tracked the problem down to an issue opening URLs and wrote this little test case: > > import urllib2 > url = 'http://www.google.com/' > opener = urllib2.build_opener() > url_data = opener.open(url).read > url_data > > > When I run that on a dev machine it works fine, but when it's on one of our servers it crashes: > > >>>> import urllib2 >>>> url = 'http://www.google.com/' >>>> opener = urllib2.build_opener() >>>> url_data = opener.open(url).read >>>> > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.7/urllib2.py", line 391, in open > response = self._open(req, data) > File "/usr/lib/python2.7/urllib2.py", line 409, in _open > '_open', req) > File "/usr/lib/python2.7/urllib2.py", line 369, in _call_chain > result = func(*args) > File "/usr/lib/python2.7/urllib2.py", line 1173, in http_open > return self.do_open(httplib.HTTPConnection, req) > File "/usr/lib/python2.7/urllib2.py", line 1148, in do_open > raise URLError(err) > urllib2.URLError: > > > Our servers have the same version of python and we're running the same OS (Fedora 14). Can anyone think of what might be causing this problem, or why it works on most (but not all) machines? > > Thanks > Actually, Python did not crashed, some module raised an exception and nothing has been written to handle that exception, hence python stopping. But as you can see, python gave you a lot of meaningful informations about why it was not able to continue execution. urllib2.URLError: Your server may not be able to resolve that URL while your local dev machine can. JM From adrianp at bringtechnology.com Thu Dec 1 13:43:38 2011 From: adrianp at bringtechnology.com (Adrian Powell) Date: Thu, 01 Dec 2011 13:43:38 -0500 Subject: platform issues? Message-ID: <20111201134338.8582@web001.roc2.bluetie.com> to run the tests, I sit at my dev machine and SSH to the server where I can copy & paste the test case directly into the python interpreter. If there's a typo, it's going to both of them equally. I am concerned about the name lookup since our servers have had some network glitches in the past and I'm not confident in our DNS. We can browse the net on the server without a problem but to be sure, I tried this: On the server: >>> socket.gethostbyaddr(socket.gethostbyname('www.google.com')) ('yyz06s07-in-f20.1e100.net', [], ['74.125.226.84']) >>> socket.gethostbyaddr(socket.gethostbyname('www.twitter.com')) ('www2.twitter.com', [], ['199.59.149.198']) On the dev environment: >>> socket.gethostbyaddr(socket.gethostbyname('www.google.com')) ('sea09s02-in-f16.1e100.net', [], ['173.194.33.48']) >>> socket.gethostbyaddr(socket.gethostbyname('www.twitter.com')) ('www4.twitter.com', [], ['199.59.149.230']) Since I'm actually trying to write a twitter client, I was more focused on the results from twitter. Since google and twitter are so huge and so distributed, I'd bet neither are good tests for this, but they look approximately right. Are there better ways to test if our network setup is good? At least that would be a relatively easy thing to fix. Adrian. -----Original Message----- From: "Redcat" [redcat at catfolks.net] Date: 12/01/2011 01:05 PM To: python-list at python.org Subject: Re: platform issues? > Our servers have the same version of python and we're running the same > OS (Fedora 14). Can anyone think of what might be causing this problem, > or why it works on most (but not all) machines? > > Thanks Is the server able to resolve www.google.com properly? If so, are you POSITIVE that the "url = " line on the server has no typos? -- http://mail.python.org/mailman/listinfo/python-list From redcat at catfolks.net Thu Dec 1 14:03:45 2011 From: redcat at catfolks.net (Redcat) Date: 1 Dec 2011 19:03:45 GMT Subject: platform issues? References: Message-ID: <9jq1chFug2U10@mid.individual.net> On Thu, 01 Dec 2011 13:43:38 -0500, Adrian Powell wrote: > Since I'm actually trying to write a twitter client, I was more focused > on the results from twitter. Since google and twitter are so huge and so > distributed, I'd bet neither are good tests for this, but they look > approximately right. > > Are there better ways to test if our network setup is good? At least > that would be a relatively easy thing to fix. I think I'd first try "dig www.google.com" to be sure that it is resolving, then I believe I'd try to fetch the URL from your code sample using cUrl. If both of those work then I'm not sure what I'd look at next. From ian at excess.org Thu Dec 1 14:05:14 2011 From: ian at excess.org (Ian Ward) Date: Thu, 01 Dec 2011 14:05:14 -0500 Subject: ANN: Urwid 1.0.1, Urwid 0.9.9.3 - Console UI Library Message-ID: <4ED7CFEA.7070006@excess.org> Announcing Urwid 1.0.1 and Urwid 0.9.9.3 ---------------------------------------- Urwid home page: http://excess.org/urwid/ Manual: http://excess.org/urwid/wiki/UrwidManual Tarballs: http://excess.org/urwid/urwid-1.0.1.tar.gz http://excess.org/urwid/urwid-0.9.9.3.tar.gz About these releases: ===================== These are bug-fix releases for Urwid 1.0.0 and Urwid 0.9.9.2. Release 0.9.9.3 may be the last in the 0.9.9 series. Users are strongly encouraged to upgrade to the 1.0 series. New in 1.0.1: ============= * Fix for Terminal widget in BSD/OSX * Fix for a Filler mouse_event() position bug * Fix support for mouse positions up to x=255, y=255 * Fixes for a number of string encoding issues under Python 3 * Fix for a LineBox border __init__() parameters * Fix input input of UTF-8 in tour.py example by converting captions to unicode * Fix tutorial examples' use of TextCanvas and switch to using unicode literals * Prevent raw_display from calling tcseattr() or tcgetattr() on non-ttys * Disable curses_display external event loop support: screen resizing and gpm events are not properly supported * Mark PollingListWalker as deprecated New in 0.9.9.3: =============== * ListBox now includes a get_cursor_coords() method, allowing nested ListBox widgets * Fix for a Filler mouse_event() position bug * Fix support for mouse positions up to x=255, y=255 * Fix for leaks of None object in str_util extension * Fix for WidgetWrap and AttrMap not working with fixed widgets About Urwid =========== Urwid is a console UI library for Python. It features fluid interface resizing, Unicode support, multiple text layouts, simple attribute markup, powerful scrolling list boxes and flexible interface design. Urwid is released under the GNU LGPL. From zak.mc.kraken at libero.it Thu Dec 1 15:35:02 2011 From: zak.mc.kraken at libero.it (Vito 'ZeD' De Tullio) Date: Thu, 01 Dec 2011 21:35:02 +0100 Subject: Clever hack or code abomination? References: Message-ID: Arnaud Delobelle wrote: > On 1 December 2011 03:15, Roy Smith wrote: >> I need to try a bunch of names in sequence until I find one that works >> (definition of "works" is unimportant). The algorithm is: >> >> 1) Given a base name, "foo", first see if just plain "foo" works. >> >> 2) If not, try "foo-1", "foo-2", and so on >> >> 3) If you reach "foo-20", give up. >> >> What would you say if you saw this: >> >> for suffix in [''] + [str(i) for i in xrange(-1, -20, -1)]: >> > > It's a little obfuscated ;) I would go for the simple: > > for i in xrange(21): > suffix = "-%s" % i if i else "" > .... > obfuscated for obfuscated, you can merge the two ideas: for suffix in ('-%s' % i if i else '' for i in xrange(21)): ... -- By ZeD From adrianp at bringtechnology.com Thu Dec 1 15:35:17 2011 From: adrianp at bringtechnology.com (Adrian Powell) Date: Thu, 01 Dec 2011 15:35:17 -0500 Subject: platform issues? Message-ID: <20111201153517.4824@web002.roc2.bluetie.com> The curl & dig ideas were great, thanks. Turns out the machine's network was badly configured and that's probably what was causing this problem. Curiously firefox could browse these sites without any problem despite not having any proxy defined and it took the command line to reveal the issue. Thank you for your help, this had been bugging us for days! Adrian. -----Original Message----- From: "Redcat" [redcat at catfolks.net] Date: 12/01/2011 02:05 PM To: python-list at python.org Subject: Re: platform issues? On Thu, 01 Dec 2011 13:43:38 -0500, Adrian Powell wrote: > Since I'm actually trying to write a twitter client, I was more focused > on the results from twitter. Since google and twitter are so huge and so > distributed, I'd bet neither are good tests for this, but they look > approximately right. > > Are there better ways to test if our network setup is good? At least > that would be a relatively easy thing to fix. I think I'd first try "dig www.google.com" to be sure that it is resolving, then I believe I'd try to fetch the URL from your code sample using cUrl. If both of those work then I'm not sure what I'd look at next. -- http://mail.python.org/mailman/listinfo/python-list From martin.hellwig at gmail.com Thu Dec 1 16:13:51 2011 From: martin.hellwig at gmail.com (Martin P. Hellwig) Date: Thu, 01 Dec 2011 21:13:51 +0000 Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: On 01/12/2011 03:15, Roy Smith wrote: Well, I have seen much worse, so the WTFs/minute(*) count won't be too bad. However, as general rule for readability; If you think you have to ask, don't bother asking, spend that time rethinking and write a more readable solution. *) http://www.osnews.com/story/19266/WTFs_m -- mph From steve+comp.lang.python at pearwood.info Thu Dec 1 19:15:55 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2011 00:15:55 GMT Subject: Clever hack or code abomination? References: Message-ID: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> On Thu, 01 Dec 2011 21:13:51 +0000, Martin P. Hellwig wrote: > On 01/12/2011 03:15, Roy Smith wrote: > Well, I have seen much worse, so the WTFs/minute(*) count won't be too > bad. > > However, as general rule for readability; If you think you have to ask, > don't bother asking, spend that time rethinking and write a more > readable solution. That's generally good advice, but in this case, I don't know why Roy Smith thought he had to ask. His initial snippet was pretty much standard vanilla Python: a for loop over a list. The list was made by adding a list containing a single element '', and a second list made from a list comprehension that converted a bunch of numbers into strings. It uses no advanced features like generators. A newbie to Python could work it out. In my opinion, if anyone thinks that's "obfuscated" (as opposed to merely "not quite idiomatic"), either their standards for readability are impossibly high, or they just can't read Python code. Try this on for size. def obfuscated_prefixes(q=20): c = str() while isinstance(type(c), type(type)): yield None or c while isinstance(c, object): try: f = type(q)(c[c.index(chr(45))+1:])+type(q)(1) except ValueError: c = str.join('\n', list(map(chr, (45, 48))) + [c])[::2] else: break if f <= q: pass else: break try: c = (lambda a,b: a+b)(c[:c.index(chr(45))+1], type(c)(f)) except IndexError: c = c[1::] -- Steven From onexpadREMOVE at EVOMERyahoodotyouknow.com Thu Dec 1 19:23:22 2011 From: onexpadREMOVE at EVOMERyahoodotyouknow.com (Kyle T. Jones) Date: Thu, 01 Dec 2011 18:23:22 -0600 Subject: Complete beginner, any help appreciated :) - For Loops In-Reply-To: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> References: <5646135.332.1322736831034.JavaMail.geo-discussion-forums@prjr26> Message-ID: On 12/1/11 4:53 AM, Mark wrote: > Hi there, > > I'm a complete beginner to Python and, aside from HTML and CSS, to coding in general. I've spent a few hours on it and think I understand most of the syntax. > > However, I'm wondering a bit about For Loops. I know that the basic syntax for them is to define a list, and then to use something like: > > for x in y > > However, what does "for" and "in" mean in this context? Can anyone help me to understand this? I know it's a really basic question, but hopefully it will see me on my way to coding properly :) > > Thanks a lot. y is some list ["help","beginner","question"] for x in y: if x=="question": print "FOUND" Clear? Cheers. From steve+comp.lang.python at pearwood.info Thu Dec 1 19:43:02 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2011 00:43:02 GMT Subject: Using the Python Interpreter as a Reference References: <79379487-0081-4067-92ed-c6717652e1ff@y7g2000vbe.googlegroups.com> <4eb0af60-26b3-45d5-8aff-566505003d6a@m10g2000vbc.googlegroups.com> <4ed2cddc$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed491fe$0$14018$c3e8da3$76491128@news.astraweb.com> Message-ID: <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> On Thu, 01 Dec 2011 10:03:53 -0800, DevPlayer wrote: [...] > Well, that may be a little hyperbolic. But with 2 spaces you can > encourage coders to get very deep, indentially, and still fit 80 chars. Why would you want to encourage coders to write deeply indented code? In my opinion, if your code is indented four or more levels, you should start to think about refactorising your code; if you reach six levels, your code is probably a mess. class K: def spam(): if x: for a in b: # This is about as deep as comfortable while y: # Code is starting to smell try: # Code smell is now beginning to reek with z as c: # And now more of a stench try: # A burning, painful stench if d: # Help! I can't breathe!!! for e in f: # WTF are you thinking? try: # DIE YOU M***********ER!!! while g: # gibbers quietly ... The beauty of languages like Python where indentation is significant is that you can't hide from the ugliness of this code. class K: { # Code looks okay to a casual glance. def spam():{ if x: { for a in b:{ while y:{ try:{ with z as c:{ try:{ if d:{ for e in f:{ try:{ while g:{ ... }}}} }}}} }}}} Deeply indented code *is* painful, it should *look* painful. -- Steven From rosuav at gmail.com Thu Dec 1 21:02:31 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 13:02:31 +1100 Subject: Using the Python Interpreter as a Reference In-Reply-To: <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <79379487-0081-4067-92ed-c6717652e1ff@y7g2000vbe.googlegroups.com> <4eb0af60-26b3-45d5-8aff-566505003d6a@m10g2000vbc.googlegroups.com> <4ed2cddc$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed491fe$0$14018$c3e8da3$76491128@news.astraweb.com> <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 2, 2011 at 11:43 AM, Steven D'Aprano wrote: > Why would you want to encourage coders to write deeply indented code? > > In my opinion, if your code is indented four or more levels, you should > start to think about refactorising your code; if you reach six levels, > your code is probably a mess. So... would it be a bad thing to wrap up all my code into a single massive expression that returns a single integer for success/failure? Oh wait, the only time I ever saw code do that was in the IOCCC. Still, it WAS pretty awesome code! IOCCC is a code perfume factory. Cloying smell. ChrisA From rosuav at gmail.com Thu Dec 1 21:07:57 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 13:07:57 +1100 Subject: Clever hack or code abomination? In-Reply-To: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 2, 2011 at 11:15 AM, Steven D'Aprano wrote: > Try this on for size. > > > ? ? ? ? ? ? ? ?f = type(q)(c[c.index(chr(45))+1:])+type(q)(1) > ? ? ? ? ? ? ? ?c = str.join('\n', list(map(chr, (45, 48))) + [c])[::2] > ? ? ? ? ? ?c = (lambda a,b: a+b)(c[:c.index(chr(45))+1], type(c)(f)) I would consider integer representations of ASCII to be code smell. It's not instantly obvious that 45 means '-', even if you happen to know the ASCII table by heart (which most people won't). This is one thing that I like about C's quote handling; double quotes for a string, or single quotes for an integer with that character's value. It's clearer than the Python (and other) requirement to have an actual function call: for (int i=0;i<10;++i) { digit[i]='0'+i; letter[i]='A'+i; } versus for i in range(10): digit[i]=chr(ord('0')+i) letter[i]=chr(ord('A')+i) Ignoring the fact that you'd probably use a list comp in Python, this is imho a win for C. ChrisA From drsalists at gmail.com Thu Dec 1 21:46:10 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Thu, 1 Dec 2011 18:46:10 -0800 Subject: Reading twice from STDIN In-Reply-To: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> Message-ID: On 12/1/11, janedenone wrote: > Hi, > > I would like to read from a pipe, parse the input and ask the user > what to do next: > > message = sys.stdin.read() > # message is parsed and URLs are printed as a list to choose from... > selected_index = raw_input('Which URL to open?') > > Calling raw_input() always raises in an EOFError. I tried reopening > and resetting sys.stdin, but nothing worked (at least on OX X 10.7, > see > You can read piped data from sys.stdin normally. Then if you want something from the user, at least on most *ix's, you would open /dev/tty and get user input from there. 'Not sure about OS/X. http://stackoverflow.com/questions/8034595/python-raw-input-following-sys-stdin-read-throws-eoferror). > > I am surprised to find that a seemingly trivial task cannot be > accomplished with Python 2.7 on a current Mac. Or am I missing > something simple? > > - Jan > -- > http://mail.python.org/mailman/listinfo/python-list > From tjreedy at udel.edu Thu Dec 1 21:49:07 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 01 Dec 2011 21:49:07 -0500 Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: On 11/30/2011 10:49 PM, Matt Joiner wrote: > def possible_names(): > yield "foo" > for i in range(20): > yield "foo-" + str(i) This is my favorite -- crystal clear with only the variable part variable. And it works in both 2.x and 3.x. -- Terry Jan Reedy From python.list at tim.thechases.com Thu Dec 1 23:02:18 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 01 Dec 2011 22:02:18 -0600 Subject: Passing along cmd.Cmd completion to contained class Message-ID: <4ED84DCA.2040802@tim.thechases.com> I have sub-classes of cmd.Cmd in an arrangement somewhat like class Config(cmd.Cmd): def do_foo(self, line): print "Fooing %r" % line def complete_foo(self, text, line, begidx, endidx): ... class MyTUI(cmd.Cmd): def __init__(self, configger=Config, *args, **kwargs): cmd.Cmd.__init__(self, *args, **kwargs) self.config = configger() def complete_config(self, text, line, begidx, endidx): magic_here(self.config, text, line, begidx, endidx) I've been sparring with getting MyTUI.complete_config() to reach into self.config for completions so I can type things like (cmd) config (cmd) config foo and have it suggest from Config as if I had done something like def do_config(self, line) self.config.cmdloop() and then asked for completions. That would mean that the first one would act like Config.completenames() and the second one would act like Config.complete_foo(...) Is there a best way to get this pass-along behavior? Thanks, -tkc From dihedral88888 at googlemail.com Thu Dec 1 23:29:40 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 20:29:40 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> Message-ID: <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> On Friday, December 2, 2011 12:18:29 AM UTC+8, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 2:52 AM, Dave Angel wrote: > > On 12/01/2011 10:35 AM, 88888 Dihedral wrote: > >> I knew a hash can replace a bi-directional linked list. > >> The value ?can be a multi-field string ?to be parsed for further actions. > >> Is this what you are asking? > > > > A hash is a number, so I don't see how it can replace any kind of linked > > list. ?Perhaps you're thinking of some other language. > > A hashtable is a form of data structure that involves hashing values > (ie calculating hashes for them) and storing them for easier retrieval > than a simple linked list offers. That may be what you're thinking of. > > ChrisA I clear my point a hash is a collection of (key, value) pairs that have well defined methods and behavior to be used in programming. The basic operations of a hash normally includes the following: 1. insertion of a (key, value) pair into the hash 2. deletion of a (key, value) from the hash 3. inquiring a hash by a key to retrieve the value if the (key, value) pair available in the hash. If no key matched, the hash will return a not found result. The hash can grow with (k,v) pairs accumulated in the run time. An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. Some implementations of a hash might pose some restrictions of k and v for some reasons. But in object programming k and v can be objects to be manipulated by the programmer. From dihedral88888 at googlemail.com Thu Dec 1 23:29:40 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 20:29:40 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> Message-ID: <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> On Friday, December 2, 2011 12:18:29 AM UTC+8, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 2:52 AM, Dave Angel wrote: > > On 12/01/2011 10:35 AM, 88888 Dihedral wrote: > >> I knew a hash can replace a bi-directional linked list. > >> The value ?can be a multi-field string ?to be parsed for further actions. > >> Is this what you are asking? > > > > A hash is a number, so I don't see how it can replace any kind of linked > > list. ?Perhaps you're thinking of some other language. > > A hashtable is a form of data structure that involves hashing values > (ie calculating hashes for them) and storing them for easier retrieval > than a simple linked list offers. That may be what you're thinking of. > > ChrisA I clear my point a hash is a collection of (key, value) pairs that have well defined methods and behavior to be used in programming. The basic operations of a hash normally includes the following: 1. insertion of a (key, value) pair into the hash 2. deletion of a (key, value) from the hash 3. inquiring a hash by a key to retrieve the value if the (key, value) pair available in the hash. If no key matched, the hash will return a not found result. The hash can grow with (k,v) pairs accumulated in the run time. An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. Some implementations of a hash might pose some restrictions of k and v for some reasons. But in object programming k and v can be objects to be manipulated by the programmer. From steve+comp.lang.python at pearwood.info Thu Dec 1 23:33:57 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2011 04:33:57 GMT Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> Message-ID: <4ed85535$0$29988$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Dec 2011 03:18:29 +1100, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 2:52 AM, Dave Angel wrote: >> On 12/01/2011 10:35 AM, 88888 Dihedral wrote: >>> I knew a hash can replace a bi-directional linked list. The value ?can >>> be a multi-field string ?to be parsed for further actions. Is this >>> what you are asking? >> >> A hash is a number, so I don't see how it can replace any kind of >> linked list. ?Perhaps you're thinking of some other language. > > A hashtable is a form of data structure that involves hashing values (ie > calculating hashes for them) and storing them for easier retrieval than > a simple linked list offers. That may be what you're thinking of. Python dicts are hash tables. -- Steven From rosuav at gmail.com Fri Dec 2 00:00:10 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 16:00:10 +1100 Subject: order independent hash? In-Reply-To: <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: On Fri, Dec 2, 2011 at 3:29 PM, 88888 Dihedral wrote: > I clear my point a hash is a collection of (key, value) pairs that have > well defined methods and behavior to be used in programming. > > The basic operations of a hash normally includes the following: > > 1. insertion of a (key, value) pair ?into the hash > 2. deletion of a (key, value) from the hash > 3. inquiring ?a hash by a key to retrieve the value if the (key, value) > pair available in the hash. If no key matched, the hash will return > a not found result. > > The hash can grow with (k,v) pairs accumulated in the run time. > An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. That's a hash table - think of a Python dictionary: On Fri, Dec 2, 2011 at 3:33 PM, Steven D'Aprano wrote: > Python dicts are hash tables. Although strictly speaking, isn't that "Python dicts are implemented as hash tables in CPython"? Or is the hashtable implementation mandated? Anyway, near enough. Cryptography and data verification use hashing too (look at the various historic hashing algorithms - CRC, MD5, SHA, etc). The concept of a hash is a number (usually of a fixed size) that is calculated from a string or other large data type, such that hashing the same input will always give the same output, but hashing different input will usually give different output. It's then possible to identify a large object solely by its hash, as is done in git, for instance; or to transmit both the data and the hash, as is done in message protection schemes (many archiving programs/formats include a hash of the uncompressed data). These have nothing to do with (key,value) pairs, but are important uses of hashes. ChrisA From anacrolix at gmail.com Fri Dec 2 00:11:06 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Fri, 2 Dec 2011 16:11:06 +1100 Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: Thank you. ?_? On Fri, Dec 2, 2011 at 1:49 PM, Terry Reedy wrote: > On 11/30/2011 10:49 PM, Matt Joiner wrote: >> >> def possible_names(): >> ? ? yield "foo" >> ? ? for i in range(20): >> ? ? ? ? yield "foo-" + str(i) > > > This is my favorite -- crystal clear with only the variable part variable. > And it works in both 2.x and 3.x. > > -- > Terry Jan Reedy > > -- > http://mail.python.org/mailman/listinfo/python-list From steve+comp.lang.python at pearwood.info Fri Dec 2 00:34:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2011 05:34:16 GMT Subject: Clever hack or code abomination? References: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ed86358$0$29988$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Dec 2011 13:07:57 +1100, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 11:15 AM, Steven D'Aprano > wrote: >> Try this on for size. >> >> >> ? ? ? ? ? ? ? ?f = type(q)(c[c.index(chr(45))+1:])+type(q)(1) c >> ? ? ? ? ? ? ? ?= str.join('\n', list(map(chr, (45, 48))) + >> ? ? ? ? ? ? ? ?[c])[::2] >> ? ? ? ? ? ?c = (lambda a,b: a+b)(c[:c.index(chr(45))+1], >> ? ? ? ? ? ?type(c)(f)) > > I would consider integer representations of ASCII to be code smell. It's > not instantly obvious that 45 means '-', even if you happen to know the > ASCII table by heart (which most people won't). This is one thing that I > like about C's quote handling; double quotes for a string, or single > quotes for an integer with that character's value. It's clearer than the > Python (and other) requirement to have an actual function call: > > for (int i=0;i<10;++i) { > digit[i]='0'+i; > letter[i]='A'+i; > } I would disagree that this is clear at all. You're adding what looks like a character, but is actually an integer, with an integer. And then just to add insult to injury, you're storing integers into arrays that are named as if they were characters. In what mad universe would you describe 65 as a letter? To say nothing of the fact that C's trick only works (for some definition of works) for ASCII. Take for example one of the many EBCDIC encodings, cp500. If you expect 'I' + 1 to equal 'J', you will be sorely disappointed: py> u'I'.encode('cp500') '\xc9' py> u'J'.encode('cp500') '\xd1' Characters are not integers, and C conflates them, to the disservice of all. If fewer people learned C, fewer people would have such trouble understanding Unicode. Anyone unfamiliar with C's model would have trouble guessing what 'A' + 1 should mean. Should it be? - an error - 'B' - 'A1' - the numeric value of variable A plus 1 - 66 (assuming ascii encoding) - 194 (assuming cp500 encoding) - some other number - something else? How about 1000 + 'A'? > versus > > for i in range(10): > digit[i]=chr(ord('0')+i) > letter[i]=chr(ord('A')+i) It's a tad more verbose, but it's explicit about what is being done. Take the character '0', find out what ordinal value it encodes to, add 1 to that value, re-encode back to a character. That's exactly what C does, only it does it explicitly. Note that this still doesn't work the way we might like in EBCDIC, but the very fact that you are forced to think about explicit conversion steps means you are less likely to make unwarranted assumptions about what characters convert to. Better than both, I would say, would be for string objects to have successor and predecessor methods, that skip ahead (or back) the specified number of code points (defaulting to 1): 'A'.succ() => 'B' 'A'.succ(5) => 'F' with appropriate exceptions if you try to go below 0 or above the largest code point. -- Steven From dihedral88888 at googlemail.com Fri Dec 2 00:48:27 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 21:48:27 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: <1576053.251.1322804908048.JavaMail.geo-discussion-forums@pruu5> On Friday, December 2, 2011 1:00:10 PM UTC+8, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 3:29 PM, 88888 Dihedral > wrote: > > I clear my point a hash is a collection of (key, value) pairs that have > > well defined methods and behavior to be used in programming. > > > > The basic operations of a hash normally includes the following: > > > > 1. insertion of a (key, value) pair ?into the hash > > 2. deletion of a (key, value) from the hash > > 3. inquiring ?a hash by a key to retrieve the value if the (key, value) > > pair available in the hash. If no key matched, the hash will return > > a not found result. > > > > The hash can grow with (k,v) pairs accumulated in the run time. > > An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > That's a hash table - think of a Python dictionary: > > On Fri, Dec 2, 2011 at 3:33 PM, Steven D'Aprano > wrote: > > Python dicts are hash tables. > > Although strictly speaking, isn't that "Python dicts are implemented > as hash tables in CPython"? Or is the hashtable implementation > mandated? Anyway, near enough. > > Cryptography and data verification use hashing too (look at the > various historic hashing algorithms - CRC, MD5, SHA, etc). The concept > of a hash is a number (usually of a fixed size) that is calculated > from a string or other large data type, such that hashing the same > input will always give the same output, but hashing different input > will usually give different output. It's then possible to identify a > large object solely by its hash, as is done in git, for instance; or > to transmit both the data and the hash, as is done in message > protection schemes (many archiving programs/formats include a hash of > the uncompressed data). These have nothing to do with (key,value) > pairs, but are important uses of hashes. > > ChrisA If one tries to insert a (k,v1) and then a (k,v2) pair into a hash with v1 not equals V2, what could happen in your understanding of a hash? A hash function is different from a hash or so called a hash table in my post. If the hash collision rate is not specified, then it is trivial to write a hash function with the conditions you specified. A hash function applied to a set of data items only is of very limited use at all. A hash stores (k,v) pairs specified in the run time with auto memory management build in is not a simple hash function to produce data signatures only clearly in my post. What I said a hash which is lifted as a basic type in python is called a dictionary in python. It is called a map in c++'s generics library. From dihedral88888 at googlemail.com Fri Dec 2 00:48:27 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 1 Dec 2011 21:48:27 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: <1576053.251.1322804908048.JavaMail.geo-discussion-forums@pruu5> On Friday, December 2, 2011 1:00:10 PM UTC+8, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 3:29 PM, 88888 Dihedral > wrote: > > I clear my point a hash is a collection of (key, value) pairs that have > > well defined methods and behavior to be used in programming. > > > > The basic operations of a hash normally includes the following: > > > > 1. insertion of a (key, value) pair ?into the hash > > 2. deletion of a (key, value) from the hash > > 3. inquiring ?a hash by a key to retrieve the value if the (key, value) > > pair available in the hash. If no key matched, the hash will return > > a not found result. > > > > The hash can grow with (k,v) pairs accumulated in the run time. > > An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > That's a hash table - think of a Python dictionary: > > On Fri, Dec 2, 2011 at 3:33 PM, Steven D'Aprano > wrote: > > Python dicts are hash tables. > > Although strictly speaking, isn't that "Python dicts are implemented > as hash tables in CPython"? Or is the hashtable implementation > mandated? Anyway, near enough. > > Cryptography and data verification use hashing too (look at the > various historic hashing algorithms - CRC, MD5, SHA, etc). The concept > of a hash is a number (usually of a fixed size) that is calculated > from a string or other large data type, such that hashing the same > input will always give the same output, but hashing different input > will usually give different output. It's then possible to identify a > large object solely by its hash, as is done in git, for instance; or > to transmit both the data and the hash, as is done in message > protection schemes (many archiving programs/formats include a hash of > the uncompressed data). These have nothing to do with (key,value) > pairs, but are important uses of hashes. > > ChrisA If one tries to insert a (k,v1) and then a (k,v2) pair into a hash with v1 not equals V2, what could happen in your understanding of a hash? A hash function is different from a hash or so called a hash table in my post. If the hash collision rate is not specified, then it is trivial to write a hash function with the conditions you specified. A hash function applied to a set of data items only is of very limited use at all. A hash stores (k,v) pairs specified in the run time with auto memory management build in is not a simple hash function to produce data signatures only clearly in my post. What I said a hash which is lifted as a basic type in python is called a dictionary in python. It is called a map in c++'s generics library. From rosuav at gmail.com Fri Dec 2 01:02:01 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 17:02:01 +1100 Subject: Clever hack or code abomination? In-Reply-To: <4ed86358$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed86358$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 2, 2011 at 4:34 PM, Steven D'Aprano wrote: > On Fri, 02 Dec 2011 13:07:57 +1100, Chris Angelico wrote: >> I would consider integer representations of ASCII to be code smell. It's >> not instantly obvious that 45 means '-', even if you happen to know the >> ASCII table by heart (which most people won't). Note, I'm not saying that C's way is perfect; merely that using the integer 45 to represent a hyphen is worse. > In what mad universe would you describe > 65 as a letter? I dunno, this universe looks pretty mad from where I am... wait, where am I? Oh, right. Rutledge's Private Clinic... nice soft walls they have here... > To say nothing of the fact that C's trick only works (for some definition > of works) for ASCII. Take for example one of the many EBCDIC encodings, > cp500. If you expect 'I' + 1 to equal 'J', you will be sorely > disappointed: > > py> u'I'.encode('cp500') > '\xc9' > py> u'J'.encode('cp500') > '\xd1' Nothing to do with C, this is a feature of ASCII. Anything involving arithmetic on ordinals depends on your encoding, regardless of conflation of int and char. > Anyone unfamiliar with C's model would have trouble guessing what 'A' + 1 > should mean. Should it be? > > - ?an error > - ?'B' > - ?'A1' > - ?the numeric value of variable A plus 1 > - ?66 ?(assuming ascii encoding) > - ?194 ?(assuming cp500 encoding) > - ?some other number > - ?something else? Agreed. But implicit casting is both a minefield and a source of immense amounts of clarity. Imagine if you couldn't implicitly cast int to float - it'd stop you from losing precision on large integers, but it would get in the way any time you want to work with integers and floating point together. > Note that this still doesn't work the way we might like in EBCDIC, but > the very fact that you are forced to think about explicit conversion > steps means you are less likely to make unwarranted assumptions about > what characters convert to. I don't know about that. Anyone brought up on ASCII and moving to EBCDIC will likely have trouble with this, no matter how many function calls it takes. > Better than both, I would say, would be for string objects to have > successor and predecessor methods, that skip ahead (or back) the > specified number of code points (defaulting to 1): > > 'A'.succ() ?=> 'B' > 'A'.succ(5) ?=> 'F' > > with appropriate exceptions if you try to go below 0 or above the largest > code point. ... and this still has that same issue. Arithmetic on codepoints depends on that. I'd be fine with a simple syntax that gives a Unicode codepoint, rather than an ASCII one; at least that's standardized. Being able to work with characters as though they're integers is a huge advantage in low-level code, but not so vital in Python. But if you're going to do it, you may as well do it without all the syntactic salt of explicit conversion functions. ChrisA From hansmu at xs4all.nl Fri Dec 2 02:53:08 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 02 Dec 2011 08:53:08 +0100 Subject: Reading twice from STDIN In-Reply-To: References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> Message-ID: <4ed883e4$0$6920$e4fe514c@news2.news.xs4all.nl> On 2/12/11 03:46:10, Dan Stromberg wrote: > You can read piped data from sys.stdin normally. Then if you want > something from the user, at least on most *ix's, you would open > /dev/tty and get user input from there. 'Not sure about OS/X. Reading from /dev/tty works fine on OS/X. -- HansM From durumdara at gmail.com Fri Dec 2 03:13:43 2011 From: durumdara at gmail.com (durumdara) Date: Fri, 2 Dec 2011 00:13:43 -0800 (PST) Subject: Python 3 - xml - crlf handling problem References: <3aae0b18-a194-444f-a2fc-da156204bd95@20g2000yqa.googlegroups.com> Message-ID: <6cc21b82-aa31-47d2-8510-5d629b6c12f2@t16g2000vba.googlegroups.com> Dear Stefan! So: may I don't understand the things well, but I thought that parser drop the "nondata" CRLF-s + other characters (not preserve them). Then don't matters that I read the XML from a file, or I create it from code, because all of them generating SAME RESULT. But Python don't do that. If I make xml from code, the code is without plus characters. But Python preserves parsed CRLF characters somewhere, and they are also flushing into the result. Example: original=''' AnyText ''' If I parse this, and write with toxml, the CRLF-s remaining in the code, but if I create this document line by line, there is no CRLF, the toxml write "only lined" xml. This also meaning that if I use prettyxml call, to prettying the xml, the file size is growing. If there is a multiple processing queue - if two pythons communicating in xml files, the size can growing every time. Py1 - read the Py2's file, process it, and write to a result file Py2 - read the Py1's result file, process it, and pass back to Py1 this can grow the file with each call, because "pretty" CRLF-s not normalized out from the code. original=''' AnyText ''' def main(): f = open('test.0.xml','w') f.write(original.strip()) f.close() for i in range(1, 10 + 1): xo = parse('test.%d.xml' % (i - 1)) de = xo.documentElement de.setAttribute('c', str(i)) t = de.getElementsByTagName('element')[0] tn = t.childNodes[0] print (dir(t)) print (tn) print (tn.nodeValue) tn.nodeValue = str(i) + '\t' + '\n' #s = xo.toxml() s = xo.toprettyxml() f = open('test.%d.xml' % i,'w') f.write(s) f.close() sys.exit() And: because Python is not converting CRLF to &013; I cannot make different from "prettied source's CRLF" (loaded from template file), "my own pretty's CRLF" (my own topretty), and really contained CRLF (for example a memo field's value). My case is that the processor application (for whom I pass the XML from Python) is sensitive to "plus CRLF"-s in text nodes, I must do something these "plus" items to avoid external's program errors. I got these templates and input files from prettied format (with CRLFS), but I must "eat" them to make an XML that one lined if possible. I hope you understand my problem with it. Thanks: dd From steve+comp.lang.python at pearwood.info Fri Dec 2 03:54:24 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2011 08:54:24 GMT Subject: Clever hack or code abomination? References: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed86358$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ed8923f$0$29988$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Dec 2011 17:02:01 +1100, Chris Angelico wrote: > On Fri, Dec 2, 2011 at 4:34 PM, Steven D'Aprano > wrote: >> On Fri, 02 Dec 2011 13:07:57 +1100, Chris Angelico wrote: >>> I would consider integer representations of ASCII to be code smell. >>> It's not instantly obvious that 45 means '-', even if you happen to >>> know the ASCII table by heart (which most people won't). > > Note, I'm not saying that C's way is perfect; merely that using the > integer 45 to represent a hyphen is worse. Dude, it was deliberately obfuscated code. I even named the function "obfuscated_prefixes". I thought that would have been a hint It's kinda scary that of all the sins against readability committed in my function, including isinstance(type(c), type(type)) which I was particularly proud of, the only criticism you came up with was that chr(45) is hard to read. I'm impressed [...] >> Note that this still doesn't work the way we might like in EBCDIC, but >> the very fact that you are forced to think about explicit conversion >> steps means you are less likely to make unwarranted assumptions about >> what characters convert to. > > I don't know about that. Anyone brought up on ASCII and moving to EBCDIC > will likely have trouble with this, no matter how many function calls it > takes. Of course you will, because EBCDIC is a pile of festering garbage :) But IMAO you're less likely to have trouble with with Unicode if you haven't been trained to treat characters as synonymous with integers. And besides, given how rare such byte-manipulations on ASCII characters are in Python, it would be a shame to lose the ability to use '' and "" for strings just to avoid calling ord and chr functions. >> Better than both, I would say, would be for string objects to have >> successor and predecessor methods, that skip ahead (or back) the >> specified number of code points (defaulting to 1): >> >> 'A'.succ() ?=> 'B' >> 'A'.succ(5) ?=> 'F' >> >> with appropriate exceptions if you try to go below 0 or above the >> largest code point. > > ... and this still has that same issue. Arithmetic on codepoints depends > on that. We shouldn't be doing arithmetic on code points. Or at least we shouldn't unless we are writing a Unicode library that *needs* to care about the implementation. We should only care about the interface, that the character after 'A' is 'B'. Implementation-wise, we shouldn't care whether A and B are represented in memory by 0x0041 and 0x0042, or by 0x14AF and 0x9B30. All we really need to know is that B comes immediately after A. Everything else is implementation. But I fear that the idea of working with chr and ord is far to ingrained now to get rid of it. -- Steven From janedenone at googlemail.com Fri Dec 2 04:09:17 2011 From: janedenone at googlemail.com (janedenone) Date: Fri, 2 Dec 2011 01:09:17 -0800 (PST) Subject: Reading twice from STDIN References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> <4ed883e4$0$6920$e4fe514c@news2.news.xs4all.nl> Message-ID: <321b6b8b-16b2-433e-9bfc-b5ec22e77ff6@i6g2000vbe.googlegroups.com> On Dec 2, 8:53?am, Hans Mulder wrote: > On 2/12/11 03:46:10, Dan Stromberg wrote: > > > You can read piped data from sys.stdin normally. ?Then if you want > > something from the user, at least on most *ix's, you would open > > /dev/tty and get user input from there. ?'Not sure about OS/X. > > Reading from /dev/tty works fine on OS/X. > > -- HansM Many thanks for the pointers ? I had tried sys.stdin = open('/dev/tty', 'r') but the actual solution is slightly more complicated. With your help (and another round of Google searches), I found exactly what I was looking for: http://turambar.org/jarkko/stdin_input.php Because raw_input() (input() in Python 3) reads from file descriptor 0, the solution is to copy this file descriptor elsewhere, and use a file descriptor pointing to /dev/tty for the user input. Thanks again - Jan From rosuav at gmail.com Fri Dec 2 04:16:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 2 Dec 2011 20:16:55 +1100 Subject: Clever hack or code abomination? In-Reply-To: <4ed8923f$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4ed818bb$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed86358$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed8923f$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 2, 2011 at 7:54 PM, Steven D'Aprano wrote: > Dude, it was deliberately obfuscated code. I even named the function > "obfuscated_prefixes". I thought that would have been a hint > > It's kinda scary that of all the sins against readability committed in my > function, including isinstance(type(c), type(type)) which I was > particularly proud of, the only criticism you came up with was that > chr(45) is hard to read. I'm impressed Heh! I know it was obfuscated, and that's why I didn't bother mentioning the obvious. > And besides, given how rare such byte-manipulations on ASCII characters > are in Python, it would be a shame to lose the ability to use '' and "" > for strings just to avoid calling ord and chr functions. Agreed. That flexibility is a _huge_ advantage. Definitely not worth changing anything. ChrisA From hniksic at xemacs.org Fri Dec 2 04:53:47 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Fri, 02 Dec 2011 10:53:47 +0100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: <87k46fb8pg.fsf@xemacs.org> Chris Angelico writes: >> The hash can grow with (k,v) pairs accumulated in the run time. >> An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > That's a hash table In many contexts "hash table" is shortened to "hash" when there is no ambiguity. This is especially popular among Perl programmers where the equivalent of dict is called a hash. > Although strictly speaking, isn't that "Python dicts are implemented > as hash tables in CPython"? Or is the hashtable implementation > mandated? It's pretty much mandated because of the __hash__ protocol. From a24061 at ducksburg.com Fri Dec 2 05:30:13 2011 From: a24061 at ducksburg.com (Adam Funk) Date: Fri, 02 Dec 2011 10:30:13 +0000 Subject: suppressing bad characters in output PCDATA (converting JSON to XML) References: <91j4q8xgv9.ln2@news.ducksburg.com> Message-ID: On 2011-11-29, Stefan Behnel wrote: > Adam Funk, 29.11.2011 13:57: >> On 2011-11-28, Stefan Behnel wrote: >>> If the name "big_json" is supposed to hint at a large set of data, you may >>> want to use something other than minidom. Take a look at the >>> xml.etree.cElementTree module instead, which is substantially more memory >>> efficient. >> >> Well, the input file in this case contains one big JSON list of >> reasonably sized elements, each of which I'm turning into a separate >> XML file. The output files range from 600 to 6000 bytes. > > It's also substantially easier to use, but if your XML writing code works > already, why change it. That module looks useful --- thanks for the tip. (TBH, I'm using minidom mainly because I've used it before and the API is similar to the DOM APIs I've used in other languages.) > You should read up on Unicode a bit. It wouldn't do me any harm. :-) >>>> I thought this would force all the output to be valid, but xmlstarlet >>>> gives some errors like these on a few documents: >>>> >>>> PCDATA invalid Char value 7 >>>> PCDATA invalid Char value 31 >>> >>> This strongly hints at a broken encoding, which can easily be triggered by >>> your erroneous encode-and-encode cycles above. >> >> No, I've checked the JSON input and those exact control characters are >> there too. > > Ah, right, I didn't look closely enough. Those are forbidden in XML: > > http://www.w3.org/TR/REC-xml/#charsets > > It's sad that minidom (apparently) lets them pass through without even a > warning. Yes, it is! I've now found this, which seems to fix the problem: http://bitkickers.blogspot.com/2011/05/stripping-control-characters-in-python.html -- The internet is quite simply a glorious place. Where else can you find bootlegged music and films, questionable women, deep seated xenophobia and amusing cats all together in the same place? [Tom Belshaw] From stefan_ml at behnel.de Fri Dec 2 06:23:54 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 02 Dec 2011 12:23:54 +0100 Subject: Python 3 - xml - crlf handling problem In-Reply-To: <6cc21b82-aa31-47d2-8510-5d629b6c12f2@t16g2000vba.googlegroups.com> References: <3aae0b18-a194-444f-a2fc-da156204bd95@20g2000yqa.googlegroups.com> <6cc21b82-aa31-47d2-8510-5d629b6c12f2@t16g2000vba.googlegroups.com> Message-ID: durumdara, 02.12.2011 09:13: > So: may I don't understand the things well, but I thought that parser > drop the "nondata" CRLF-s + other characters (not preserve them). Well, it does that, at least on my side (which is not under Windows): =================== original=''' AnyText ''' from xml.dom.minidom import parse def main(): f = open('test.0.xml', 'wb') f.write(original.strip().replace('\n', '\r\n').encode('utf8')) f.close() xo = parse('test.0.xml') de = xo.documentElement print(repr(de.childNodes[0].nodeValue)) print(repr(de.childNodes[1].childNodes[0].nodeValue)) if __name__ == '__main__': main() =================== This prints '\n ' and '\n AnyText\n ' on my side, so the whitespace normalisation in the parser properly did its work. > Then don't matters that I read the XML from a file, or I create it > from code, because all of them generating SAME RESULT. > But Python don't do that. > If I make xml from code, the code is without plus characters. What do you mean by "plus characters"? It's not the "+" character that you are referring to, right? Do you mean additional characters? Such as the additional '\r'? > But Python preserves parsed CRLF characters somewhere, and they are > also flushing into the result. > > Example: > > original=''' > > > > AnyText > > > ''' > > If I parse this, and write with toxml, the CRLF-s remaining in the > code, but if I create this document line by line, there is no CRLF, > the toxml write "only lined" xml. > > This also meaning that if I use prettyxml call, to prettying the xml, > the file size is growing. > > If there is a multiple processing queue - if two pythons communicating > in xml files, the size can growing every time. > > Py1 - read the Py2's file, process it, and write to a result file > Py2 - read the Py1's result file, process it, and pass back to Py1 > this can grow the file with each call, because "pretty" CRLF-s not > normalized out from the code. > > original=''' > > > > AnyText > > > ''' > > def main(): > f = open('test.0.xml','w') > f.write(original.strip()) > f.close() > > for i in range(1, 10 + 1): > xo = parse('test.%d.xml' % (i - 1)) > de = xo.documentElement > de.setAttribute('c', str(i)) > t = de.getElementsByTagName('element')[0] > tn = t.childNodes[0] > print (dir(t)) > print (tn) > print (tn.nodeValue) > tn.nodeValue = str(i) + '\t' + '\n' > #s = xo.toxml() > s = xo.toprettyxml() > f = open('test.%d.xml' % i,'w') > f.write(s) > f.close() > > sys.exit() > > And: because Python is not converting CRLF to&013; I cannot make > different from "prettied source's CRLF" (loaded from template file), > "my own pretty's CRLF" (my own topretty), and really contained CRLF > (for example a memo field's value). > > My case is that the processor application (for whom I pass the XML > from Python) is sensitive to "plus CRLF"-s in text nodes, I must do > something these "plus" items to avoid external's program errors. > > I got these templates and input files from prettied format (with > CRLFS), but I must "eat" them to make an XML that one lined if > possible. > > I hope you understand my problem with it. Still not quite, but never mind. May or may not be a problem in minidom or your code. For example, you shouldn't open the output file in text mode but in binary mode (i.e. "wb") because you are writing bytes into it. Here's what I tried with ElementTree, and it seems to do what your code above wants. The indent() function is taken from Fredrik's element lib page: http://effbot.org/zone/element-lib.htm ======================== original=''' AnyText ''' def indent(elem, level=0): i = "\n" + level*" " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: indent(elem, level+1) if not elem.tail or not elem.tail.strip(): elem.tail = i else: if level and (not elem.tail or not elem.tail.strip()): elem.tail = i def main(): f = open('test.0.xml','w', encoding='utf8') f.write(original.strip()) f.close() from xml.etree.cElementTree import parse for i in range(10): tree = parse('test.%d.xml' % i) root = tree.getroot() root.set('c', str(i+1)) t = root.find('.//element') t.text = '%d\t\n' % (i+1) indent(root) tree.write('test.%d.xml' % (i+1), encoding='utf8') if __name__ == '__main__': main() ======================== Hope that helps, Stefan From dihedral88888 at googlemail.com Fri Dec 2 06:48:17 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 03:48:17 -0800 (PST) Subject: order independent hash? In-Reply-To: <87k46fb8pg.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> Message-ID: <24346295.348.1322826497244.JavaMail.geo-discussion-forums@prfi36> On Friday, December 2, 2011 5:53:47 PM UTC+8, Hrvoje Niksic wrote: > Chris Angelico writes: > > >> The hash can grow with (k,v) pairs accumulated in the run time. > >> An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > > > That's a hash table > > In many contexts "hash table" is shortened to "hash" when there is no > ambiguity. This is especially popular among Perl programmers where the > equivalent of dict is called a hash. > > > Although strictly speaking, isn't that "Python dicts are implemented > > as hash tables in CPython"? Or is the hashtable implementation > > mandated? > > It's pretty much mandated because of the __hash__ protocol. A hash table definitely can replace a bi-directional list. But this is for applications or languages like C. In python hash is a build in type. Perl's lazy way of programming is famous. From hansmu at xs4all.nl Fri Dec 2 08:04:42 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 02 Dec 2011 14:04:42 +0100 Subject: Reading twice from STDIN In-Reply-To: <321b6b8b-16b2-433e-9bfc-b5ec22e77ff6@i6g2000vbe.googlegroups.com> References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> <4ed883e4$0$6920$e4fe514c@news2.news.xs4all.nl> <321b6b8b-16b2-433e-9bfc-b5ec22e77ff6@i6g2000vbe.googlegroups.com> Message-ID: <4ed8cceb$0$6931$e4fe514c@news2.news.xs4all.nl> On 2/12/11 10:09:17, janedenone wrote: > I had tried > > sys.stdin = open('/dev/tty', 'r') That seems to work for me. This code: import sys if sys.version_info.major == 2: input = raw_input for tp in enumerate(sys.stdin): print("%d: %s" % tp) sys.stdin = open('/dev/tty', 'r') answer = input('What is the carrying capacity of a swallow? ') print("You answered: %s" % answer) print("File descriptor is %d" % sys.stdin.fileno()) ... does what I expect it to do in both Python 2.7 and Python 3.2. > but the actual solution is slightly more complicated. With your help > (and another round of Google searches), I found exactly what I was > looking for: > > http://turambar.org/jarkko/stdin_input.php > > Because raw_input() (input() in Python 3) reads from file descriptor > 0, the solution is to copy this file descriptor elsewhere, and use a > file descriptor pointing to /dev/tty for the user input. That's odd. For some reason, I can get away with a simple sys.stdin = open('/dev/tty') and raw_input will merrily read from file descriptor 3. I'm using Pyhton 2.7.1 and 3.2 on MacOS/X 10.5.0. What version are you using? -- HansM From miki.tebeka at gmail.com Fri Dec 2 10:01:40 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Fri, 2 Dec 2011 07:01:40 -0800 (PST) Subject: Passing along cmd.Cmd completion to contained class In-Reply-To: References: Message-ID: <1593725.176.1322838100582.JavaMail.geo-discussion-forums@yqmd7> This is not tested, but maybe it'll work :) def complete(self, text, state): if text.startswith('config): return self.config.complete(text, state) return Cmd.complete(self, text, state) From miki.tebeka at gmail.com Fri Dec 2 10:01:40 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Fri, 2 Dec 2011 07:01:40 -0800 (PST) Subject: Passing along cmd.Cmd completion to contained class In-Reply-To: References: Message-ID: <1593725.176.1322838100582.JavaMail.geo-discussion-forums@yqmd7> This is not tested, but maybe it'll work :) def complete(self, text, state): if text.startswith('config): return self.config.complete(text, state) return Cmd.complete(self, text, state) From mbadoiu at gmail.com Fri Dec 2 10:05:49 2011 From: mbadoiu at gmail.com (Mihai Badoiu) Date: Fri, 2 Dec 2011 10:05:49 -0500 Subject: Multiprocessing: killing children when parent dies Message-ID: In the multiprocessing module, on a Process p, by just doing p.daemon=1 before p.start(), we can make the child die when the parent exits. However, the child does not die if the parent gets killed. How can I make sure the child die when the parent gets killed? thanks, --mihai -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Fri Dec 2 10:13:34 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Dec 2011 02:13:34 +1100 Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: Message-ID: On Sat, Dec 3, 2011 at 2:05 AM, Mihai Badoiu wrote: > In the multiprocessing module, on a Process p, by just doing p.daemon=1 > before p.start(), we can make the child die when the parent exits. ?However, > the child does not die if the parent gets killed. > How can I make sure the child die when the parent gets killed? Are you in control of the killing of the parent? One easy way would be to catch the signal (say SIGINT) and initiate an orderly shutdown, signalling the children first. ChrisA From devplayer at gmail.com Fri Dec 2 10:18:35 2011 From: devplayer at gmail.com (DevPlayer) Date: Fri, 2 Dec 2011 07:18:35 -0800 (PST) Subject: python 2.5 and ast References: <4ED37475.3050709@gmail.com> <4ED457C5.2020407@davea.name> <4ed4c2ce$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ED4E35E.6090405@gmail.com> Message-ID: <4bbaa89c-55d2-40d1-91fb-cb00c3f2239a@s4g2000yqk.googlegroups.com> On Nov 30, 1:03?pm, Andrea Crotti wrote: > Another thing about the AST, I am having fun trying to for example list > out all > the unused imports. > > I have already a visitor which works quite nicely I think, but now I > would like > to get a way to find all the unused imports, so I need more visitors that > find out all the used names. > > I didn't find too much documentation about these kind of things, so any > suggestions on how to approach? > > Pylint and snakefood have similar code, but they use the old "compiler" > module, > so it's not really helpful. There was another topic in these forums recently about "un-importing" modules (and how you can not do that reliably without restarting python). There was various ways mentioned of keeping track of what was imported. And there was mentioned reasonable ways of finding all possible imports on a computer. By "unused imports" I assume you mean "all unused imports in the application's source code" as opposed to meaning "all unused modules/ packages/libraries on that computer." Personally I'd love to see more tutorials on the AST module; An AST for Dummies. Pretty much the tutorials talk about parsing an expression like "1+2=3". But I'd like to see how blocks are compiled/ managed by the indent/dedent tokens and how the complete algorithm for finding qouted strings is implimented (using really simple explanations). Some google buzzwords to help with your search for your question: sys import cache, import hook, pydoc walkpackages(). And I just found this little tool; never knew about it: C:\PythonXX \Tools\Scripts\pydocui.pyw From kmichael.aye at gmail.com Fri Dec 2 10:27:04 2011 From: kmichael.aye at gmail.com (K.-Michael Aye) Date: Fri, 2 Dec 2011 16:27:04 +0100 Subject: Why are Sphinx docs incompatible with Safari Reader? Message-ID: Anybody knows? Have a nice weekend! Michael From janedenone at googlemail.com Fri Dec 2 10:27:43 2011 From: janedenone at googlemail.com (janedenone) Date: Fri, 2 Dec 2011 07:27:43 -0800 (PST) Subject: Reading twice from STDIN References: <6de9515a-17b2-40a3-a113-5733cad9f794@cu3g2000vbb.googlegroups.com> <4ed883e4$0$6920$e4fe514c@news2.news.xs4all.nl> <321b6b8b-16b2-433e-9bfc-b5ec22e77ff6@i6g2000vbe.googlegroups.com> <4ed8cceb$0$6931$e4fe514c@news2.news.xs4all.nl> Message-ID: On Dec 2, 2:04?pm, Hans Mulder wrote: > That's odd. ?For some reason, I can get away with a simple > > ? ? ?sys.stdin = open('/dev/tty') > > and raw_input will merrily read from file descriptor 3. > > I'm using Pyhton 2.7.1 and 3.2 on MacOS/X 10.5.0. > > What version are you using? > > -- HansM Python 2.7.1 and 3.2.2 on Mac OS X 10.7.2. But what's really strange ??you're right. It works now. I have no idea what I did before, but I can confirm that the following code executes flawlessly: message = sys.stdin.read() urls = re.findall(r'(?:http|www)\S+', message) sys.stdin = open('/dev/tty') # Asks user to select a URL only if more than one URL is present if len(urls) > 1: for index, url in enumerate(urls): print '{0}: {1}\n'.format(index, url) selected_index = raw_input('Which URL to open? ') selected_index = int(selected_index) else: selected_index = 0 selected_url = urls[selected_index] Quite embarrassing. In any case, I am happy to have found the simplest solution. Thanks again, Jan From alec.taylor6 at gmail.com Fri Dec 2 10:57:08 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Sat, 3 Dec 2011 02:57:08 +1100 Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: Message-ID: I've been in philosophical discussions all day. This topic title makes me cringe :P From dihedral88888 at googlemail.com Fri Dec 2 10:57:56 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 07:57:56 -0800 (PST) Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: Message-ID: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> On Friday, December 2, 2011 11:13:34 PM UTC+8, Chris Angelico wrote: > On Sat, Dec 3, 2011 at 2:05 AM, Mihai Badoiu wrote: > > In the multiprocessing module, on a Process p, by just doing p.daemon=1 > > before p.start(), we can make the child die when the parent exits. ?However, > > the child does not die if the parent gets killed. > > How can I make sure the child die when the parent gets killed? > > Are you in control of the killing of the parent? One easy way would be > to catch the signal (say SIGINT) and initiate an orderly shutdown, > signalling the children first. > > ChrisA Multiple thread supporting programming languages in true OOP as Erlang and Python do not talk about POSIX signals. From dihedral88888 at googlemail.com Fri Dec 2 10:57:56 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 07:57:56 -0800 (PST) Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: Message-ID: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> On Friday, December 2, 2011 11:13:34 PM UTC+8, Chris Angelico wrote: > On Sat, Dec 3, 2011 at 2:05 AM, Mihai Badoiu wrote: > > In the multiprocessing module, on a Process p, by just doing p.daemon=1 > > before p.start(), we can make the child die when the parent exits. ?However, > > the child does not die if the parent gets killed. > > How can I make sure the child die when the parent gets killed? > > Are you in control of the killing of the parent? One easy way would be > to catch the signal (say SIGINT) and initiate an orderly shutdown, > signalling the children first. > > ChrisA Multiple thread supporting programming languages in true OOP as Erlang and Python do not talk about POSIX signals. From rosuav at gmail.com Fri Dec 2 11:14:15 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Dec 2011 03:14:15 +1100 Subject: Multiprocessing: killing children when parent dies In-Reply-To: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> Message-ID: On Sat, Dec 3, 2011 at 2:57 AM, 88888 Dihedral wrote: > Multiple thread supporting programming languages ?in true OOP as Erlang and Python do not talk about POSIX signals. The OP talked about multiprocessing. Each thread of execution is a separate context, and can receive signals. ChrisA From mdickinson at enthought.com Fri Dec 2 11:25:25 2011 From: mdickinson at enthought.com (Mark Dickinson) Date: Fri, 2 Dec 2011 08:25:25 -0800 (PST) Subject: unpack('>f', b'\x00\x01\x00\x00') References: <6b7251ef-3479-412f-8acb-882be1e25633@n35g2000yqf.googlegroups.com> <87obvsbnja.fsf@xemacs.org> Message-ID: <1bf12228-a5bc-4f1f-8928-3bc2274fdb34@gl2g2000vbb.googlegroups.com> On Dec 1, 10:21?am, Hrvoje Niksic wrote: > Chris Rebert writes: > > C does not have a built-in fixed-point datatype, so the `struct` > > module doesn't handle fixed-point numbers directly. > > The built-in decimal module supports fixed-point arithmetic, but the > struct module doesn't know about it. ?A bug report (or patch) by someone > who works with binary representations of fixed-point would be a good > start to improve it. Not really: the decimal module is for floating-point, not fixed- point, though its semantics for significant trailing zeros can help give the appearance of fixed-point arithmetic for simple operations (addition, subtraction) that stay within suitable bounds. And decimal fixed-point isn't so much use for a binary fixed-point format, anyway. -- Mark From dihedral88888 at googlemail.com Fri Dec 2 11:27:17 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 08:27:17 -0800 (PST) Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> Message-ID: <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Please check Erlang that spawn so easily. And there are Python packages can do the same task. From dihedral88888 at googlemail.com Fri Dec 2 11:27:17 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 08:27:17 -0800 (PST) Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> Message-ID: <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Please check Erlang that spawn so easily. And there are Python packages can do the same task. From andrea.crotti.0 at gmail.com Fri Dec 2 11:54:34 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Fri, 02 Dec 2011 16:54:34 +0000 Subject: python 2.5 and ast In-Reply-To: <4bbaa89c-55d2-40d1-91fb-cb00c3f2239a@s4g2000yqk.googlegroups.com> References: <4ED37475.3050709@gmail.com> <4ED457C5.2020407@davea.name> <4ed4c2ce$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ED4E35E.6090405@gmail.com> <4bbaa89c-55d2-40d1-91fb-cb00c3f2239a@s4g2000yqk.googlegroups.com> Message-ID: <4ED902CA.7090404@gmail.com> On 12/02/2011 03:18 PM, DevPlayer wrote: > There was another topic in these forums recently about "un-importing" > modules (and how you can not do that reliably without restarting > python). There was various ways mentioned of keeping track of what was > imported. And there was mentioned reasonable ways of finding all > possible imports on a computer. > > By "unused imports" I assume you mean "all unused imports in the > application's source code" as opposed to meaning "all unused modules/ > packages/libraries on that computer." > > Personally I'd love to see more tutorials on the AST module; An AST > for Dummies. Pretty much the tutorials talk about parsing an > expression like "1+2=3". But I'd like to see how blocks are compiled/ > managed by the indent/dedent tokens and how the complete algorithm for > finding qouted strings is implimented (using really simple > explanations). > > Some google buzzwords to help with your search for your question: sys > import cache, import hook, pydoc walkpackages(). > > And I just found this little tool; never knew about it: C:\PythonXX > \Tools\Scripts\pydocui.pyw > Yes I meant unused imports in each of the modules actually... The problem for me is to actually understand what are all the possible AST node that can involve the use of an actual import, and I didn't find anything helpful aboupt that. The ASDL definition says everything in theory but without some examples is a bit hard. I got something nice however: http://stackoverflow.com/questions/8340567/python-ast-to-dot-graph/8342383#8342383 But on windows and Python 2.5 nothing is working :/, even if I blandly copied the ast.py from the python 2.7 code file.. File "h:\long\path\devsonly\ast.py", line 166, in iter_fields for field in node._fields: TypeError: 'NoneType' object is not iterable From dihedral88888 at googlemail.com Fri Dec 2 12:00:40 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 09:00:40 -0800 (PST) Subject: order independent hash? In-Reply-To: <87k46fb8pg.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> Message-ID: <23692480.457.1322845240706.JavaMail.geo-discussion-forums@prfi36> On Friday, December 2, 2011 5:53:47 PM UTC+8, Hrvoje Niksic wrote: > Chris Angelico writes: > > >> The hash can grow with (k,v) pairs accumulated in the run time. > >> An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > > > That's a hash table > > In many contexts "hash table" is shortened to "hash" when there is no > ambiguity. This is especially popular among Perl programmers where the > equivalent of dict is called a hash. > > > Although strictly speaking, isn't that "Python dicts are implemented > > as hash tables in CPython"? Or is the hashtable implementation > > mandated? > > It's pretty much mandated because of the __hash__ protocol. I agree with you. When a hash is inserted with (k,v1) and then (k,v2) such that v1!=v2, there are several possibilities to handle this situation in programming: 1. v2 just replaces v1 with or without a message 2. the programmer can delete the (k,v1) entry and then insert a (k, (v1,v2)) entry. 3. the programmer initializes a tuple of (v1,v2), and then delete the (k,v1) entry to be replaced by a (k,(v1,v2)) entry. Hashes of hashes are really powerful in Python to be used to build a relational DB of thousands of thousands of objects or records in a piece of cake. A similar hash library in C is to be used everywhere is not difficult at all. From andrea.crotti.0 at gmail.com Fri Dec 2 12:01:47 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Fri, 02 Dec 2011 17:01:47 +0000 Subject: python 2.5 and ast In-Reply-To: <4ED902CA.7090404@gmail.com> References: <4ED37475.3050709@gmail.com> <4ED457C5.2020407@davea.name> <4ed4c2ce$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ED4E35E.6090405@gmail.com> <4bbaa89c-55d2-40d1-91fb-cb00c3f2239a@s4g2000yqk.googlegroups.com> <4ED902CA.7090404@gmail.com> Message-ID: <4ED9047B.2010603@gmail.com> And on a related topic, how can I actually detect other types of imports, for example __import__ Doing a dump I get this: In [113]: ast.dump(ast.parse('__import__("module")')) Out[113]: "Module(body=[Expr(value=Call(func=Name(id='__import__', ctx=Load()), args=[Str(s='module')], keywords=[], starargs=None, kwargs=None))])" So the visitor should be quite smart, and the string passed can't be always be done. I think the easiest way is just to use regexp and look for them warning the user that there might be other stupid imports, the harder way is to try to detect it and if the string passed is actually known at compile-time use it, otherwise warn the user. From chris.hulan at gmail.com Fri Dec 2 12:20:38 2011 From: chris.hulan at gmail.com (Chris Hulan) Date: Fri, 2 Dec 2011 09:20:38 -0800 (PST) Subject: Clever hack or code abomination? In-Reply-To: References: Message-ID: <31721129.458.1322846439035.JavaMail.geo-discussion-forums@prjr26> I'd just look a the unit tests for clarification/examples From gnarlodious at gmail.com Fri Dec 2 12:49:25 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 2 Dec 2011 09:49:25 -0800 (PST) Subject: Referencing module.instance Message-ID: What I am doing is importing modules that have an identical instance name. So I say: import Grid Grid has its instance: Grid.Grid() and this is the same for all modules of my webapp. allowedPages is a list of modules to import, so they are quoted strings: for page in self.allowedPages: setattr(self, page, __import__(page)) The problem is that the attribute name needs to reference the Grid.Grid instance and not the Grid module. How would I do this? I can do it literally: setattr(self, 'Grid', Grid.Grid) however doing it as a reference eludes me. Or is there some nifty Pythonic way of bulk importing modules? -- Gnarlie From dihedral88888 at googlemail.com Fri Dec 2 13:18:12 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 10:18:12 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> On Wednesday, November 30, 2011 8:32:39 PM UTC+8, Neal Becker wrote: > I like to hash a list of words (actually, the command line args of my program) > in such a way that different words will create different hash, but not sensitive > to the order of the words. Any ideas? For each word of your list as the key, an object of a hash can be used further is stored as the value in the hash of the list of words. Assume that word 1 is the key and the hash stored is a sentence that uses the key word 1. This is very useful to learn vocabularies and writing sentences. From dihedral88888 at googlemail.com Fri Dec 2 13:18:12 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 10:18:12 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> On Wednesday, November 30, 2011 8:32:39 PM UTC+8, Neal Becker wrote: > I like to hash a list of words (actually, the command line args of my program) > in such a way that different words will create different hash, but not sensitive > to the order of the words. Any ideas? For each word of your list as the key, an object of a hash can be used further is stored as the value in the hash of the list of words. Assume that word 1 is the key and the hash stored is a sentence that uses the key word 1. This is very useful to learn vocabularies and writing sentences. From dihedral88888 at googlemail.com Fri Dec 2 13:28:10 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 10:28:10 -0800 (PST) Subject: python 2.5 and ast In-Reply-To: References: Message-ID: <6866657.516.1322850490609.JavaMail.geo-discussion-forums@prjr26> On Monday, November 28, 2011 7:45:57 PM UTC+8, Andrea Crotti wrote: > I'm happily using the ast module to analyze some code, > but my scripts need also to run unfortunately on python 2.5 > > The _ast was there already, but the ast helpers not yet. > Is it ok if I just copy over the source from the ast helpers in my code base > or is there a smarter way? > (I don't even need all of them, just "parse" and NodeVisitor at the moment) Sounds like a hash for nodes and a hash for a tree. A hash can replace a tree of finite number of nodes. An array or list is enough to replace a tree of finite number of nodes. The heap sort ordering is simple but illustrative . After all the tasks of the tree are done, the hash of the tree will be deleted by Python's garbage collection mechanism. A hash can replace an indexed array with (k,v) pairs for k=0,1,2,3...n in a trivial way. From dihedral88888 at googlemail.com Fri Dec 2 13:28:10 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 2 Dec 2011 10:28:10 -0800 (PST) Subject: python 2.5 and ast In-Reply-To: References: Message-ID: <6866657.516.1322850490609.JavaMail.geo-discussion-forums@prjr26> On Monday, November 28, 2011 7:45:57 PM UTC+8, Andrea Crotti wrote: > I'm happily using the ast module to analyze some code, > but my scripts need also to run unfortunately on python 2.5 > > The _ast was there already, but the ast helpers not yet. > Is it ok if I just copy over the source from the ast helpers in my code base > or is there a smarter way? > (I don't even need all of them, just "parse" and NodeVisitor at the moment) Sounds like a hash for nodes and a hash for a tree. A hash can replace a tree of finite number of nodes. An array or list is enough to replace a tree of finite number of nodes. The heap sort ordering is simple but illustrative . After all the tasks of the tree are done, the hash of the tree will be deleted by Python's garbage collection mechanism. A hash can replace an indexed array with (k,v) pairs for k=0,1,2,3...n in a trivial way. From snorble at hotmail.com Fri Dec 2 13:34:24 2011 From: snorble at hotmail.com (snorble) Date: Fri, 2 Dec 2011 10:34:24 -0800 (PST) Subject: Install Python on Windows without Start Menu icons? Message-ID: Is it possible to automate the Python installation on Windows using the MSI file so it does not add a Start Menu folder? I would like to push out Python to all of my office workstations, but I'd like for it to be relatively silent from the user's point of view. From ian.g.kelly at gmail.com Fri Dec 2 13:37:26 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 2 Dec 2011 11:37:26 -0700 Subject: order independent hash? In-Reply-To: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> References: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> Message-ID: On Fri, Dec 2, 2011 at 11:18 AM, 88888 Dihedral wrote: > On Wednesday, November 30, 2011 8:32:39 PM UTC+8, Neal Becker wrote: >> I like to hash a list of words (actually, the command line args of my program) >> in such a way that different words will create different hash, but not sensitive >> to the order of the words. ?Any ideas? > > For each word of your list as the key, an object of a hash can be used further > is stored as the value in the hash of the list of words. > > Assume that word 1 is the key and the hash stored is a sentence that uses > the key word 1. > > This is very useful to learn vocabularies and writing sentences. The OP is talking about hash functions, not hash tables, and furthermore he is talking about hashing the entire unordered collection of words, not hashing individual words or ordered sentences. From d at davea.name Fri Dec 2 13:39:44 2011 From: d at davea.name (Dave Angel) Date: Fri, 02 Dec 2011 13:39:44 -0500 Subject: order independent hash? In-Reply-To: References: Message-ID: <4ED91B70.1080804@davea.name> On 12/01/2011 08:55 AM, Neal Becker wrote: > Gelonida N wrote: > >> On 11/30/2011 01:32 PM, Neal Becker wrote: >>> I like to hash a list of words (actually, the command line args of my >>> program) in such a way that different words will create different hash, but >>> not sensitive >>> to the order of the words. Any ideas? >>> >> Do youmean hash like digest like md5sum / sha1 ? >> >> >> You should sort the words alphabetically, concatenate them with a space >> or any character, that will NEVER be part of a word and calulate the hash. >> >> If words can exist multiple times, then youhad tu uniqufy them (u using >> a python dict / set) first. > Yes that sounds just like what I wanted - thanks! > "different words will create different hash, but not sensitive to the order of the words" Just note that you can only say that "probably create" a different hash. If the hash number is constrained in size (eg. 32 bits), then a collision is possible. (Unless of course you also constrain the alphabet and/or length of the words involved) -- DaveA From ian.g.kelly at gmail.com Fri Dec 2 13:43:56 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 2 Dec 2011 11:43:56 -0700 Subject: python 2.5 and ast In-Reply-To: <6866657.516.1322850490609.JavaMail.geo-discussion-forums@prjr26> References: <6866657.516.1322850490609.JavaMail.geo-discussion-forums@prjr26> Message-ID: On Fri, Dec 2, 2011 at 11:28 AM, 88888 Dihedral wrote: > On Monday, November 28, 2011 7:45:57 PM UTC+8, Andrea Crotti wrote: >> I'm happily using the ast module to analyze some code, >> but my scripts need also to run unfortunately on python 2.5 >> >> The _ast was there already, but the ast helpers not yet. >> Is it ok if I just copy over the source from the ast helpers in my code base >> or is there a smarter way? >> (I don't even need all of them, just "parse" and NodeVisitor at the moment) > > Sounds like a hash for nodes and a hash for a tree. > A hash can replace a tree of finite number of nodes. > An array or list is enough to replace a tree of finite number of nodes. > > The heap sort ordering is simple but illustrative . > > After all the tasks ?of the tree are done, the hash of the tree > will be deleted by Python's garbage collection mechanism. > > A hash can replace an indexed array with (k,v) pairs for k=0,1,2,3...n in > a trivial way. What in the world does any of this have to do with using the ast module in Python 2.5? I am starting to suspect that "88888 Dihedral" may be a bot. From icanbob at gmail.com Fri Dec 2 14:06:27 2011 From: icanbob at gmail.com (bobicanprogram) Date: Fri, 2 Dec 2011 11:06:27 -0800 (PST) Subject: Need some IPC pointers References: Message-ID: <904c723e-ef2b-49ae-87a1-cc287a69192b@u6g2000vbg.googlegroups.com> On Nov 30, 4:03 pm, Andrew Berg wrote: > I've done some research, but I'm not sure what's most appropriate for my > situation. What I want to do is have a long running process that spawns > processes (that aren't necessarily written in Python) and communicates > with them. The children can be spawned at any time and communicate at > any time. Being able to communicate with non-local processes would be > nice, but is not necessary. The implementation needs to be > cross-platform, but child processes will use the same OS as the parent > during runtime. > I don't think I'll ever need to transfer anything complicated or large - > just strings or possibly tuples/lists. I'd rather not go outside the > standard library (but I'd consider it). I don't need to worry about > compatibility with older Python versions; if it only works with Python > 3.2, that's not a problem. > I'm thinking sockets, but perhaps there's something simpler/easier. > > -- > CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 You might want to take a look at the SIMPL toolkit. http://www.icanprogram.com/06py/lesson1/lesson1.html SIMPL modules can be written (and mixed) in any number of supported languages including Python, C, C++, JAVA, Tcl/Tk or PHP. Non local communication is handled by generic surrogates. Because of this surrogate architecture SIMPL modules can often be tested locally and then deployed into the cloud without any changes. bob From fred.sells at adventistcare.org Fri Dec 2 15:29:22 2011 From: fred.sells at adventistcare.org (Sells, Fred) Date: Fri, 2 Dec 2011 15:29:22 -0500 Subject: Using the Python Interpreter as a Reference In-Reply-To: <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <79379487-0081-4067-92ed-c6717652e1ff@y7g2000vbe.googlegroups.com><4eb0af60-26b3-45d5-8aff-566505003d6a@m10g2000vbc.googlegroups.com><4ed2cddc$0$29988$c3e8da3$5496439d@news.astraweb.com><4ed491fe$0$14018$c3e8da3$76491128@news.astraweb.com> <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven, that's probably the most elegant explanation of the "pythonic" way I've ever seen. I'm saving it for the next time upper management want to use Java again. -----Original Message----- From: python-list-bounces+frsells=adventistcare.org at python.org [mailto:python-list-bounces+frsells=adventistcare.org at python.org] On Behalf Of Steven D'Aprano Sent: Thursday, December 01, 2011 7:43 PM To: python-list at python.org Subject: Re: Using the Python Interpreter as a Reference On Thu, 01 Dec 2011 10:03:53 -0800, DevPlayer wrote: [...] > Well, that may be a little hyperbolic. But with 2 spaces you can > encourage coders to get very deep, indentially, and still fit 80 chars. Why would you want to encourage coders to write deeply indented code? In my opinion, if your code is indented four or more levels, you should start to think about refactorising your code; if you reach six levels, your code is probably a mess. class K: def spam(): if x: for a in b: # This is about as deep as comfortable while y: # Code is starting to smell try: # Code smell is now beginning to reek with z as c: # And now more of a stench try: # A burning, painful stench if d: # Help! I can't breathe!!! for e in f: # WTF are you thinking? try: # DIE YOU M***********ER!!! while g: # gibbers quietly ... The beauty of languages like Python where indentation is significant is that you can't hide from the ugliness of this code. class K: { # Code looks okay to a casual glance. def spam():{ if x: { for a in b:{ while y:{ try:{ with z as c:{ try:{ if d:{ for e in f:{ try:{ while g:{ ... }}}} }}}} }}}} Deeply indented code *is* painful, it should *look* painful. -- Steven -- http://mail.python.org/mailman/listinfo/python-list From jeanpierreda at gmail.com Fri Dec 2 15:58:23 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 2 Dec 2011 15:58:23 -0500 Subject: Using the Python Interpreter as a Reference In-Reply-To: <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <79379487-0081-4067-92ed-c6717652e1ff@y7g2000vbe.googlegroups.com> <4eb0af60-26b3-45d5-8aff-566505003d6a@m10g2000vbc.googlegroups.com> <4ed2cddc$0$29988$c3e8da3$5496439d@news.astraweb.com> <4ed491fe$0$14018$c3e8da3$76491128@news.astraweb.com> <4ed81f16$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: > In my opinion, if your code is indented four or more levels, you should > start to think about refactorising your code; if you reach six levels, > your code is probably a mess. Here's some code I encountered while grading assignments from first-year CS students: if 'not' in temp_holder: for item in (range(len(ial))): for key in (range(len(ial[item]))): if type(ial[item][key]) == str: if temp[term].find(ial[item][key]) > 0: for value in range(len(ial[item][1])): if ial[item][1][value] in images: while ial[item][1][value] in images: images.remove(ial[item][1][value]) I think after some point of nesting, not only can we conclude that the code is difficult to read, we can probably conclude the author wasn't thinking very clearly about what he or she was doing. Devin On Thu, Dec 1, 2011 at 7:43 PM, Steven D'Aprano wrote: > On Thu, 01 Dec 2011 10:03:53 -0800, DevPlayer wrote: > > [...] >> Well, that may be a little hyperbolic. But with 2 spaces you can >> encourage coders to get very deep, indentially, and still fit 80 chars. > > Why would you want to encourage coders to write deeply indented code? > > In my opinion, if your code is indented four or more levels, you should > start to think about refactorising your code; if you reach six levels, > your code is probably a mess. > > class K: > ? ?def spam(): > ? ? ? ?if x: > ? ? ? ? ? ?for a in b: > ? ? ? ? ? ? ? ?# This is about as deep as comfortable > ? ? ? ? ? ? ? ?while y: > ? ? ? ? ? ? ? ? ? ?# Code is starting to smell > ? ? ? ? ? ? ? ? ? ?try: > ? ? ? ? ? ? ? ? ? ? ? ?# Code smell is now beginning to reek > ? ? ? ? ? ? ? ? ? ? ? ?with z as c: > ? ? ? ? ? ? ? ? ? ? ? ? ? ?# And now more of a stench > ? ? ? ? ? ? ? ? ? ? ? ? ? ?try: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# A burning, painful stench > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if d: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# Help! I can't breathe!!! > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?for e in f: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# WTF are you thinking? > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?try: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# DIE YOU M***********ER!!! > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?while g: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# gibbers quietly > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?... > > > The beauty of languages like Python where indentation is significant is > that you can't hide from the ugliness of this code. > > class K: { > ?# Code looks okay to a casual glance. > ?def spam():{ > ? if x: { for a in b:{ > ? ? while y:{ try:{ with z as c:{ > ? ? ? try:{ if d:{ for e in f:{ try:{ > ? ? ? ? while g:{ ... }}}} > ? ? ? }}}} > ? ? }}}} > > Deeply indented code *is* painful, it should *look* painful. > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From ben+python at benfinney.id.au Fri Dec 2 16:31:18 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 03 Dec 2011 08:31:18 +1100 Subject: Referencing module.instance References: Message-ID: <87sjl23bkp.fsf@benfinney.id.au> Gnarlodious writes: > What I am doing is importing modules that have an identical instance > name. Best to fix that, then. > import Grid That's a poorly-named module. PEP 8 recommends module names be all lowercase. > Grid has its instance: > > Grid.Grid() And this is the reason: PEP 8 recommends class names be named with TitleCase, so they're distinguishable from module names. So the above should be: import grid grid.Grid() > for page in self.allowedPages: > setattr(self, page, __import__(page)) For completeness, I'll note that the function name would be PEP 8 compatible as ?allowed_pages?. > The problem is that the attribute name needs to reference the > Grid.Grid instance and not the Grid module. How would I do this? > I can do it literally: > setattr(self, 'Grid', Grid.Grid) Why do you shy away from this? You're already using ?__import__?, and I don't know why that is since you already showed that you're importing the module explicitly anyway. -- \ ?Don't be afraid of missing opportunities. Behind every failure | `\ is an opportunity somebody wishes they had missed.? ?Jane | _o__) Wagner, via Lily Tomlin | Ben Finney From tjreedy at udel.edu Fri Dec 2 16:40:24 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 02 Dec 2011 16:40:24 -0500 Subject: order independent hash? In-Reply-To: <87k46fb8pg.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> Message-ID: On 12/2/2011 4:53 AM, Hrvoje Niksic wrote: > Chris Angelico writes: > >>> The hash can grow with (k,v) pairs accumulated in the run time. >>> An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. >> >> That's a hash table > > In many contexts "hash table" is shortened to "hash" when there is no > ambiguity. This is especially popular among Perl programmers where the > equivalent of dict is called a hash. > >> Although strictly speaking, isn't that "Python dicts are implemented >> as hash tables in CPython"? Or is the hashtable implementation >> mandated? > > It's pretty much mandated because of the __hash__ protocol. Lib Ref 4.8. Mapping Types ? dict "A mapping object maps hashable values to arbitrary objects." This does not say that the mapping has to *use* the hash value ;-). Even if it does, it could use a tree structure instead of a hash table. But hash tables seem to work well for general purposes, "Mappings are mutable objects." (This would change if a frozendict were added.) especially when additions and deletions are needed. -- Terry Jan Reedy From ursusmaximus at gmail.com Fri Dec 2 16:47:11 2011 From: ursusmaximus at gmail.com (Ron) Date: Fri, 2 Dec 2011 13:47:11 -0800 (PST) Subject: Django ported to Python3! Message-ID: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> It looks like Vinay Sajip has succeeded in porting Django to Python3 (in a shared code base for Python 3.2 and Python 2.7). This is an astoundingly good job, done very fast and is big news. See https://groups.google.com/forum/#!topic/django-developers/XjrX3FIPT-U and the actual code is at Bitbucket https://bitbucket.org/vinay.sajip/django With NumPy and SciPy already ported, and with Matplotlib almost there, maybe PIL and others will follow shortly. This could be a turning point, or a milestone, or whatever you want to call it. Vinay is a hero who should be thanked and congratulated! In an infinitely less important note, Python411 podcasts are finally back online after a three month occultation due to a prolonged and ugly billing dispute with Libsyn. Maybe I can interview Vinay and have him tell us about the porting effort! From yobu18.m at gmail.com Fri Dec 2 19:45:25 2011 From: yobu18.m at gmail.com (kutty) Date: Fri, 2 Dec 2011 16:45:25 -0800 (PST) Subject: hiiiiiiiiiiiiiiiii man makes a money, money makes whommmmmmmmmmmmm Message-ID: <4e5e8990-da41-4810-b6f6-15b97530c062@da3g2000vbb.googlegroups.com> just logon to thzzzzzzzz websites From steve+comp.lang.python at pearwood.info Fri Dec 2 20:04:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Dec 2011 01:04:49 GMT Subject: order independent hash? References: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> Message-ID: <4ed975b1$0$29988$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Dec 2011 10:18:12 -0800, 88888 Dihedral wrote: [...] Dihedral, EVERY SINGLE ONE of your messages is double posted. You are sending to the newsgroup and the mailing list, but they are aliases for each other. Please stop double posting. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 2 20:24:26 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Dec 2011 01:24:26 GMT Subject: Referencing module.instance References: Message-ID: <4ed97a4a$0$29988$c3e8da3$5496439d@news.astraweb.com> On Fri, 02 Dec 2011 09:49:25 -0800, Gnarlodious wrote: > What I am doing is importing modules that have an identical instance > name. So I say: > > import Grid > > Grid has its instance: > > Grid.Grid() > > and this is the same for all modules of my webapp. allowedPages is a > list of modules to import, so they are quoted strings: > > for page in self.allowedPages: > setattr(self, page, __import__(page)) So if allowedPages looks like this: ['Grid', 'Spam', 'Ham', 'Cheese'] you expect your instance to look like this: self.Grid # has the value Grid.Grid self.Spam # has the value Spam.Grid etc. self.Ham self.Cheese Correct? I'm not sure I like that design, it feels wrong, but perhaps I don't understand the reason for it. Moving along... > The problem is that the attribute name needs to reference the Grid.Grid > instance and not the Grid module. How would I do this? I can do it > literally: > setattr(self, 'Grid', Grid.Grid) > > however doing it as a reference eludes me. __import__(page) returns a module, so just grab that attribute from the module. for page in self.allowedPages: setattr(self, page, __import__(page).Grid) But note that this is an binding operation, like an assignment. If you rebind module.Grid, your instance self will not see the change: Spam.Grid = 42 # some value instance.read_pages() # whatever you call the method assert instance.Spam == 42 Spam.Grid = 23 # rebind the original assert instance.Spam == 23 # THIS WILL FAIL!!! If you need these attributes to be aliases to the module attribute, rather than independent bindings, you need to use computed properties rather than plain attributes. I leave that as an exercise, mainly because I suspect it will be very difficult. Wait... no, I think it's easy! (Famous last words...) Try this untested code: class MyClass(object): allowedPages = ('Grid', 'Spam', 'Ham', 'Cheese') def __init__(self): self.modules = {} for name in allowedPages: self.modules[name] = __import__(name) def __getattribute__(self, name): if name in self.allowedPages: return self.modules[name].Grid return super(MyClass, self).__getattribute__(name) -- Steven From gnarlodious at gmail.com Fri Dec 2 22:49:22 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 2 Dec 2011 19:49:22 -0800 (PST) Subject: Referencing module.instance In-Reply-To: References: Message-ID: <4121414.852.1322884162706.JavaMail.geo-discussion-forums@prnu18> To explain my reasoning, this scheme will allow me to run the script three ways, as shell, as one-shot CGI or as a persistent mod_wsgi module. So to be more exhaustive: In __init__ I can say: import Grid self.Grid = Grid.Grid self.Grid is now the instance of Grid inside the module Grid. then later on I __call__ with the input data: html=self.Grid(queryString, environ) and this works great for one module, a default output which is now running on my server. But now I want to use the query string to load the instance, which means it comes in as text. Say I load a list of modules: allowedPages=['Gnomon', 'Grid', 'Lexicon', 'Harmonics'] I need to import them all at __init__, and set the instance for each like this: self.Gnomon=Gnomon.Gnomon self.Grid=Grid.Grid self.Lexicon=Lexicon.Lexicon self.Harmonics=Harmonics.Harmonics Then dynamically, in __call_, I just use the CGI string to invoke my target object. What I have now is: self.modules = {} page = 'Gnomon' self.modules[page] = __import__(page) which gives me A solution is to set self.modules[page] to the Gnomon instance of the Gnomon module. How to? -- Gnarlie http://Sectrum.com From antti.ylikoski at tkk.fi Fri Dec 2 22:54:19 2011 From: antti.ylikoski at tkk.fi (Antti J Ylikoski) Date: Sat, 03 Dec 2011 05:54:19 +0200 Subject: Python 2 or 3 Message-ID: I'm in the process of learning Python. I already can code objet-oriented programs with the language. I have in my hands the O'Reilly book by Mark Lutz, Programming Python, in two versions: the 2nd Edition, which covers Python 2, and the 4th edition, which covers Python 3. In the "official Python site" so to speak, http://www.python.org, it is mentioned that the authors recommend the visitor, who is a novice, to learn Python 2 rather than Python 3, because most of existing software has been writen with Python 2. The O'Reilly book has some 1200 pages. I would not want to invest such an amount of work and time to an obsolete language (i. e. Python 2). What is the opinion of the wizards here, shall I learm Python 2 or Python 3? I'm posting this here because I feel that this point is interesting to other students of Python. Cheers, Antti "Andy" Ylikoski Helsinki, Finland, the EU From gnarlodious at gmail.com Fri Dec 2 23:10:24 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 2 Dec 2011 20:10:24 -0800 (PST) Subject: Python 2 or 3 In-Reply-To: References: Message-ID: <33402104.714.1322885424968.JavaMail.geo-discussion-forums@prgi20> If you are writing your own scripts, I would recommend Py3 for learning. But if you are studying existing scripts to learn, Py2 might be better. I have been doing Python for about 2 years and started learning Py3 with no regrets. Py2 is not going to be "obsolete" for quite a while. It is almost all the same as Py3, just a few updates mostly of concern to those who maintain existing code. And actually, Python has been incrementally improved upon all along, just that Py3 was the cutoff point for stuff that HAD to change. -- Gnarlie From bahamutzero8825 at gmail.com Fri Dec 2 23:20:19 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 02 Dec 2011 22:20:19 -0600 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: <4ED9A383.1090103@gmail.com> On 12/2/2011 9:54 PM, Antti J Ylikoski wrote: > What is the opinion of the wizards here, shall I learm Python 2 or > Python 3? I'm posting this here because I feel that this point is > interesting to other students of Python. Unless you are tied to Python 2 in some way, go for Python 3 and don't look back. There are a few major software projects that still do not support Python 3 (more and more are getting ported or replaced as time goes on), and many production systems are still using Python 2, but don't let /potential/ roadblocks keep you away from Python 3. Another thing to note is that, at least AFAICT, Jython, IronPython and PyPy are not going to support Python 3 any time soon, so if you need to use one of them, Python 2 is the way to go. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From roy at panix.com Fri Dec 2 23:27:19 2011 From: roy at panix.com (Roy Smith) Date: Fri, 02 Dec 2011 23:27:19 -0500 Subject: Python 2 or 3 References: Message-ID: In article , Antti J Ylikoski wrote: > I have in my hands the O'Reilly book by Mark Lutz, Programming > Python, in two versions: the 2nd Edition, which covers Python 2, and > the 4th edition, which covers Python 3. The engineer in me really has to wonder what the 3rd edition might have covered :-) > I would not want to invest such an amount of work and time to an > obsolete language (i. e. Python 2). I think the best that can be said for Python 2 is, "It's not dead yet!". The vast majority of production Python code written today is for 2.x, for x in {5, 6, 7}. The biggest thing that's holding back adoption of 3 is that most of the major packages don't support 3 yet (but I saw an announcement just this morning that django has been ported to 3). I predict that 2012 will be the year of Python-3. I expect we're at the point now that all major packages will either get ported to 3 in the next year or two, or become abandonware. Also, people building new packages will come out with versions for both 2 and 3 if they want their stuff to get widely adopted. > What is the opinion of the wizards here, shall I learm Python 2 or > Python 3? I'm posting this here because I feel that this point is > interesting to other students of Python. The difficult thing here is that you are living on the cusp. If you came back and asked that question in a couple of years, I strongly suspect the answer would be, "Don't bother with 2; 3 is what everybody uses today". But, we're not there quite yet. Learn 2. From drsalists at gmail.com Fri Dec 2 23:42:06 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Fri, 2 Dec 2011 20:42:06 -0800 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On 12/2/11, Antti J Ylikoski wrote: > > I'm in the process of learning Python. I already can code > objet-oriented programs with the language. I have in my hands the > O'Reilly book by Mark Lutz, Programming Python, in two versions: the > 2nd Edition, which covers Python 2, and the 4th edition, which covers > Python 3. > > In the "official Python site" so to speak, http://www.python.org, it > is mentioned that the authors recommend the visitor, who is a novice, > to learn Python 2 rather than Python 3, because most of existing > software has been writen with Python 2. > > The O'Reilly book has some 1200 pages. I would not want to invest > such an amount of work and time to an obsolete language (i. e. Python > 2). It mostly depends on what sort of thing you're learning Python for. If you have a project in mind that has large dependencies that still require Python 2.x, then you're probably best off with 2.x for now. Otherwise, go with 3.x. Or do what I've been enjoying: Install 2.x and 3.x, and test with both, each step of the way. In this manner, you can pretty easily write new code that runs on either equally well. But again, there's the matter of dependencies. > What is the opinion of the wizards here, shall I learm Python 2 or > Python 3? I'm posting this here because I feel that this point is > interesting to other students of Python. > > > Cheers, Antti "Andy" Ylikoski > Helsinki, Finland, the EU > -- > http://mail.python.org/mailman/listinfo/python-list > From tjreedy at udel.edu Sat Dec 3 01:23:33 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 03 Dec 2011 01:23:33 -0500 Subject: Python 2 or 3 In-Reply-To: <4ED9A383.1090103@gmail.com> References: <4ED9A383.1090103@gmail.com> Message-ID: On 12/2/2011 11:20 PM, Andrew Berg wrote: > thing to note is that, at least AFAICT, Jython, IronPython and PyPy are > not going to support Python 3 any time soon, PyPy has a roadmap for 3.2 http://pypy.org/py3donate.html They definitely plan to do it one way or another. -- Terry Jan Reedy From bahamutzero8825 at gmail.com Sat Dec 3 01:37:39 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 03 Dec 2011 00:37:39 -0600 Subject: Python 2 or 3 In-Reply-To: References: <4ED9A383.1090103@gmail.com> Message-ID: <4ED9C3B3.8050609@gmail.com> On 12/3/2011 12:23 AM, Terry Reedy wrote: > PyPy has a roadmap for 3.2 > http://pypy.org/py3donate.html > They definitely plan to do it one way or another. I never said there were no plans, but at $2567 out of $60k, I don't see it happening soon. Unless someone decides to donate a huge sum of money or a large amount of code in the next couple months, I don't see a release happening until at least the middle of next year. It would definitely not be practical to learn Python 3 at this point if your primary implementation is PyPy. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From bahamutzero8825 at gmail.com Sat Dec 3 01:41:43 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 03 Dec 2011 00:41:43 -0600 Subject: Python 2 or 3 In-Reply-To: References: <4ED9A383.1090103@gmail.com> Message-ID: <4ED9C4A7.40206@gmail.com> I accidentally quoted the wrong figure. I meant $4369 of $105000. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From stefan_ml at behnel.de Sat Dec 3 01:48:25 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 03 Dec 2011 07:48:25 +0100 Subject: Django ported to Python3! In-Reply-To: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> References: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> Message-ID: Ron, 02.12.2011 22:47: > It looks like Vinay Sajip has succeeded in porting Django to Python3 > (in a shared code base for Python 3.2 and Python 2.7). This is an > astoundingly good job, done very fast and is big news. > See https://groups.google.com/forum/#!topic/django-developers/XjrX3FIPT-U > and the actual code is at Bitbucket https://bitbucket.org/vinay.sajip/django > > With NumPy and SciPy already ported, and with Matplotlib almost there, > maybe PIL and others will follow shortly. This could be a turning > point, or a milestone, or whatever you want to call it. Vinay is a > hero who should be thanked and congratulated! Note that most of the work was done by Martin von L?wis, quite a while back in the early days of Python 3.x. http://wiki.python.org/moin/PortingDjangoTo3k He also did a huge amount of lobbying to get the changes accepted before the time that the project originally envisioned. The original plans of the Django project were to only *start* the porting after dropping support for Python 2.5 somewhere in the future. Martin made it rather clear with his patch (and keeps reiterating it wherever he can) that you can support both in one code base, even in a project as large as Django. Stefan From anacrolix at gmail.com Sat Dec 3 01:53:14 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Sat, 3 Dec 2011 17:53:14 +1100 Subject: Django ported to Python3! In-Reply-To: References: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> Message-ID: As long as we can dump python 2, a big congrats to anyone who makes this possible. Thanks martin On Dec 3, 2011 5:51 PM, "Stefan Behnel" wrote: > Ron, 02.12.2011 22:47: > >> It looks like Vinay Sajip has succeeded in porting Django to Python3 >> (in a shared code base for Python 3.2 and Python 2.7). This is an >> astoundingly good job, done very fast and is big news. >> See https://groups.google.com/**forum/#!topic/django-** >> developers/XjrX3FIPT-U >> and the actual code is at Bitbucket https://bitbucket.org/vinay.** >> sajip/django >> >> With NumPy and SciPy already ported, and with Matplotlib almost there, >> maybe PIL and others will follow shortly. This could be a turning >> point, or a milestone, or whatever you want to call it. Vinay is a >> hero who should be thanked and congratulated! >> > > Note that most of the work was done by Martin von L?wis, quite a while > back in the early days of Python 3.x. > > http://wiki.python.org/moin/**PortingDjangoTo3k > > He also did a huge amount of lobbying to get the changes accepted before > the time that the project originally envisioned. The original plans of the > Django project were to only *start* the porting after dropping support for > Python 2.5 somewhere in the future. Martin made it rather clear with his > patch (and keeps reiterating it wherever he can) that you can support both > in one code base, even in a project as large as Django. > > Stefan > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From anacrolix at gmail.com Sat Dec 3 01:54:28 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Sat, 3 Dec 2011 17:54:28 +1100 Subject: Python 2 or 3 In-Reply-To: <4ED9C3B3.8050609@gmail.com> References: <4ED9A383.1090103@gmail.com> <4ED9C3B3.8050609@gmail.com> Message-ID: 2 without a doubt. On Dec 3, 2011 5:40 PM, "Andrew Berg" wrote: > On 12/3/2011 12:23 AM, Terry Reedy wrote: > > PyPy has a roadmap for 3.2 > > http://pypy.org/py3donate.html > > They definitely plan to do it one way or another. > I never said there were no plans, but at $2567 out of $60k, I don't see > it happening soon. Unless someone decides to donate a huge sum of money > or a large amount of code in the next couple months, I don't see a > release happening until at least the middle of next year. It would > definitely not be practical to learn Python 3 at this point if your > primary implementation is PyPy. > > -- > CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wxjmfauth at gmail.com Sat Dec 3 03:59:12 2011 From: wxjmfauth at gmail.com (jmfauth) Date: Sat, 3 Dec 2011 00:59:12 -0800 (PST) Subject: Python 2 or 3 References: Message-ID: <834e5eac-4b53-44c1-bfad-e54bba60a06d@i8g2000vbh.googlegroups.com> On 3 d?c, 04:54, Antti J Ylikoski wrote: > Helsinki, Finland, the EU <<<<<<<<<<< >>> sys.version '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]' >>> '?l?phant' '\xe9l\xe9phant' >>> >>> sys.version '3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)]' >>> '?l?phant' '?l?phant' >>> jmf From whatsjacksemail at gmail.com Sat Dec 3 04:01:41 2011 From: whatsjacksemail at gmail.com (Jack Keegan) Date: Sat, 3 Dec 2011 09:01:41 +0000 Subject: Multiprocessing: killing children when parent dies In-Reply-To: <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Message-ID: I think the OP meant when the parent gets killed (by ctrl+c or similar), not deleted. At least that's what I think when I think of a program being killed. Is it even possible to send a signal in such a case? Cheers, Jack On Fri, Dec 2, 2011 at 4:27 PM, 88888 Dihedral wrote: > Please check Erlang that spawn so easily. And there are Python packages > can do the same task. > -- > http://mail.python.org/mailman/listinfo/python-list > -- The earth is a very small stage in a vast cosmic arena. Think of the rivers of blood spilled by all those generals and emperors so that in glory and in triumph they could become the momentary masters of a fraction of a dot. - Carl Sagan [Pale Blue Dot] -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sat Dec 3 05:04:07 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Dec 2011 10:04:07 GMT Subject: Python 2 or 3 References: Message-ID: <4ed9f416$0$29988$c3e8da3$5496439d@news.astraweb.com> On Sat, 03 Dec 2011 05:54:19 +0200, Antti J Ylikoski wrote: > The O'Reilly book has some 1200 pages. I would not want to invest such > an amount of work and time to an obsolete language (i. e. Python 2). Python 2 is not an obsolete language. The differences between Python 2 and Python 3 are minor, more like different dialects of one language than two languages. I learned Python 1.5 and watched the extended transition between 1.5 -> 2.2, and in my opinion, the 2.5 -> 3.2 transition is not that much more difficult. An experienced programmer won't have any difficulty learning both, and swapping between them. Newbies who have never programmed before often have trouble with the differences between 2.x and 3.x, but if you are an experienced programmer, you shouldn't have any trouble at all. The biggest difference to the language is that strings are now Unicode instead of byte strings; this has forced a lot of English speakers to learn about Unicode and bytes instead of assuming that the universe is ASCII. Apart from that, the differences are, in my opinion, trivial. However, there are a lot of them: http://docs.python.org/release/3.0.1/whatsnew/3.0.html But the most important ones in my opinion are: * unicode text, as mentioned above, and the consequences of this; * some of the modules in the standard library have been renamed; * various new modules have been added; * some built-in functions and methods which used to return lists now return lazy iterators or views; * print is now a function instead of a statement; * a small number of syntax changes, some of which are also supported by Python 2.6 and 2.7; * "classic classes" no longer exist, so there is now only one class mechanism, not two. If you can deal with the difference between these two lines without getting confused: print md5.md5("spam").hexdigest() # Python 2.x print(hashlib.md5("spam").hexdigest()) # Python 3.x you're half way there. -- Steven From gnarlodious at gmail.com Sat Dec 3 05:37:14 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Sat, 3 Dec 2011 02:37:14 -0800 (PST) Subject: Referencing module.instance In-Reply-To: References: Message-ID: <9331481.63.1322908634300.JavaMail.geo-discussion-forums@prij11> HA! After much experimenting I hit upon getattr(__import__(page), page): for page in self.allowedPages: scriptPath = '{}/{}.py'.format(os.path.dirname(__file__), page) if os.path.exists(scriptPath): self.modules[page] = getattr(__import__(page), page) Then in __call_ I just say: target = incoming CGI variable self.modules[target](qList, environ) and it works! -- Gnarlie From rosuav at gmail.com Sat Dec 3 05:44:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 3 Dec 2011 21:44:22 +1100 Subject: Python 2 or 3 In-Reply-To: <4ed9f416$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4ed9f416$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Dec 3, 2011 at 9:04 PM, Steven D'Aprano wrote: > If you can deal with the difference between these two lines without > getting confused: > > print md5.md5("spam").hexdigest() ?# Python 2.x > print(hashlib.md5("spam").hexdigest()) ?# Python 3.x The second line needs to be: print(hashlib.md5("spam".encode()).hexdigest()) Relatively insignificant differences, since Python 2 and Python 3 both support both bytes and unicode strings. The only difference is that Py3 makes Unicode the default, forcing you to be explicit when you want bytes. ChrisA From arnodel at gmail.com Sat Dec 3 07:03:14 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sat, 3 Dec 2011 12:03:14 +0000 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On 3 December 2011 03:54, Antti J Ylikoski wrote: > > I'm in the process of learning Python. ?I already can code > objet-oriented programs with the language. ?I have in my hands the > O'Reilly book by Mark Lutz, Programming Python, in two versions: the > 2nd Edition, which covers Python 2, and the 4th edition, which covers > Python 3. > > In the "official Python site" so to speak, http://www.python.org, it > is mentioned that the authors recommend the visitor, who is a novice, > to learn Python 2 rather than Python 3, because most of existing > software has been writen with Python 2. > > The O'Reilly book has some 1200 pages. ?I would not want to invest > such an amount of work and time to an obsolete language (i. e. Python > 2). Python 2 and Python 3 are mostly the same language. Learning either will be equally valuable IMHO. There are some significant differences but if you have a good understanding of one, you will have no problem adapting very quickly to the other. And Python 2 is definitely not obsolete :) -- Arnaud From pedro.h.souto at gmail.com Sat Dec 3 09:01:45 2011 From: pedro.h.souto at gmail.com (Pedro Henrique G. Souto) Date: Sat, 03 Dec 2011 12:01:45 -0200 Subject: Install Python on Windows without Start Menu icons? In-Reply-To: References: Message-ID: <4EDA2BC9.2030607@gmail.com> On 02/12/2011 16:34, snorble wrote: > Is it possible to automate the Python installation on Windows using > the MSI file so it does not add a Start Menu folder? I would like to > push out Python to all of my office workstations, but I'd like for it > to be relatively silent from the user's point of view. If you just want to run python scripts in those machines (not developing in it), you can use something like py2exe [http://www.py2exe.org/]. It converts a python script to a standalone executable. Good luck! -- Att; Pedro Henrique G. Souto ??????????????? ? ???d?_?b??? ? ??????????????? From ursusmaximus at gmail.com Sat Dec 3 11:03:18 2011 From: ursusmaximus at gmail.com (Ron) Date: Sat, 3 Dec 2011 08:03:18 -0800 (PST) Subject: Django ported to Python3! References: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> Message-ID: Thanks Stefan for clarifying that. I guess Martin deserves most of the credit. But I still admire how Sajip jumped in, and I especially admire how the core team accepted his work without taking a "Not Invented Here" attitude. I sure hope the port is accepted into the main trunk soon. There is just such a huge difference bewtween "90% done" and actually released. Often code is 90% done but is never finished. And Django has such enormous psychological significance for Python 3. Many important projects will never begin serious porting until after Django officially supports Python 3. And many Python folks will finally start to take Python 3 seriously only when Django does announce official support. Ron From dihedral88888 at googlemail.com Sat Dec 3 11:36:09 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 3 Dec 2011 08:36:09 -0800 (PST) Subject: order independent hash? In-Reply-To: <4ed975b1$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <14681078.446.1322849892244.JavaMail.geo-discussion-forums@prmw23> <4ed975b1$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <33348771.70.1322930169970.JavaMail.geo-discussion-forums@prgi20> On Saturday, December 3, 2011 9:04:49 AM UTC+8, Steven D'Aprano wrote: > On Fri, 02 Dec 2011 10:18:12 -0800, 88888 Dihedral wrote: > [...] > > > Dihedral, EVERY SINGLE ONE of your messages is double posted. You are > sending to the newsgroup and the mailing list, but they are aliases for > each other. Please stop double posting. > > > -- > Steven A list can replace a hash. I knew that in lisp. From gelonida at gmail.com Sat Dec 3 15:26:25 2011 From: gelonida at gmail.com (Gelonida N) Date: Sat, 03 Dec 2011 21:26:25 +0100 Subject: order independent hash? In-Reply-To: <4ED91B70.1080804@davea.name> References: <4ED91B70.1080804@davea.name> Message-ID: On 12/02/2011 07:39 PM, Dave Angel wrote: > On 12/01/2011 08:55 AM, Neal Becker wrote: >> Gelonida N wrote: >> >>> On 11/30/2011 01:32 PM, Neal Becker wrote: >>>> I like to hash a list of words (actually, the command line args of my >>>> program) in such a way that different words will create different >>>> hash, but >>>> not sensitive >>>> to the order of the words. Any ideas? >>>> >>> Do youmean hash like digest like md5sum / sha1 ? >>> >>> >>> You should sort the words alphabetically, concatenate them with a space >>> or any character, that will NEVER be part of a word and calulate the >>> hash. >>> >>> If words can exist multiple times, then youhad tu uniqufy them (u using >>> a python dict / set) first. >> Yes that sounds just like what I wanted - thanks! >> You're welcome. is it working as expected? From gelonida at gmail.com Sat Dec 3 15:59:40 2011 From: gelonida at gmail.com (Gelonida N) Date: Sat, 03 Dec 2011 21:59:40 +0100 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On 12/03/2011 04:54 AM, Antti J Ylikoski wrote: > > I'm in the process of learning Python. I already can code > objet-oriented programs with the language. I have in my hands the > O'Reilly book by Mark Lutz, Programming Python, in two versions: the > 2nd Edition, which covers Python 2, and the 4th edition, which covers > Python 3. > > In the "official Python site" so to speak, http://www.python.org, it > is mentioned that the authors recommend the visitor, who is a novice, > to learn Python 2 rather than Python 3, because most of existing > software has been writen with Python 2. > > The O'Reilly book has some 1200 pages. I would not want to invest > such an amount of work and time to an obsolete language (i. e. Python > 2). > > What is the opinion of the wizards here, shall I learm Python 2 or > Python 3? I'm posting this here because I feel that this point is > interesting to other students of Python. > > > Cheers, Antti "Andy" Ylikoski > Helsinki, Finland, the EU I would still stick with python 2. In my opinion there is no reason to rush to the most recent version. Especially when working in a corporate environment or when being obliged to use certain web servers / servers you will notice, that you will still encounter quite some hosts with python 2.5 and even some with python2.4. Most machines, that I have to use, run python 2.6 (and some python 2.5) I personally try to write most of my code such that it could still run with python 2.5 I decided that python2.4 is my cut-off point, though I still have one machines and some embedded devices, which just run 2.4 Other considerations - there are still more libraries / packages available for python 2 than for python 3, though this is changing. - if you write code nicely enough in python 2, then you can translate it to python 3. autmatically. However up to my knowledge you cannot automatically translate python 3 code to python 2 code. With that strategy your code will be able to run on most web servers and your own machines. If you want to write your own code which does not have to run on other machines and / or if you want to heavily use unicode, then it might be better to start with Python 3. From arnodel at gmail.com Sat Dec 3 16:34:08 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sat, 3 Dec 2011 21:34:08 +0000 Subject: How to generate java .properties files in python Message-ID: Hi all, I need to generate some java .properties files in Python (2.6 / 2.7). It's a simple format to store key/value pairs e.g. blue=bleu green=vert red=rouge The key/value are unicode strings. The annoying thing is that the file is encoded in ISO 8859-1, with all non Latin1 characters escaped in the form \uHHHH (same as how unicode characters are escaped in Python). I thought I could use the "unicode_escape" codec. But it doesn't work because it escapes Latin1 characters with escape sequences of the form \xHH, which is not valid in a java .properties file. Is there a simple way to achieve this? I could do something like this: def encode(u): """encode a unicode string in .properties format""" return u"".join(u"\\u%04x" % ord(c) if ord(c) > 0xFF else c for c in u).encode("latin_1") but it would be quite inefficient as I have many to generate. -- Arnaud From roy at panix.com Sat Dec 3 17:29:07 2011 From: roy at panix.com (Roy Smith) Date: Sat, 03 Dec 2011 17:29:07 -0500 Subject: Django ported to Python3! References: <420b2302-3532-4107-8670-8c67230be8dc@q16g2000yqn.googlegroups.com> Message-ID: In article , Ron wrote: > Django has such > enormous psychological significance for Python 3. Many important > projects will never begin serious porting until after Django > officially supports Python 3. And many Python folks will finally start > to take Python 3 seriously only when Django does announce official > support. In a somewhat related topic, it looks like Mongodb will also be supporting Python 3 soon. It's on the roadmap for their 2.2 release (https://jira.mongodb.org/browse/PYTHON-84). There's no date announced yet, but extrapolating from past release schedules, I'd guess mid 2012. From __peter__ at web.de Sat Dec 3 18:51:16 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 04 Dec 2011 00:51:16 +0100 Subject: How to generate java .properties files in python References: Message-ID: Arnaud Delobelle wrote: > I need to generate some java .properties files in Python (2.6 / 2.7). > It's a simple format to store key/value pairs e.g. > > blue=bleu > green=vert > red=rouge > > The key/value are unicode strings. The annoying thing is that the > file is encoded in ISO 8859-1, with all non Latin1 characters escaped > in the form \uHHHH (same as how unicode characters are escaped in > Python). > > I thought I could use the "unicode_escape" codec. But it doesn't work > because it escapes Latin1 characters with escape sequences of the form > \xHH, which is not valid in a java .properties file. > > Is there a simple way to achieve this? I could do something like this: > > def encode(u): > """encode a unicode string in .properties format""" > return u"".join(u"\\u%04x" % ord(c) if ord(c) > 0xFF else c for c > in u).encode("latin_1") > > but it would be quite inefficient as I have many to generate. >>> class D(dict): ... def __missing__(self, key): ... result = self[key] = u"\\u%04x" % key ... return result ... >>> d = D(enumerate(map(unichr, range(256)))) >>> u"?hnlich ?blich n?tig ???" u'\xe4hnlich \xfcblich n\xf6tig \u03a6\u03a7\u03a8' >>> u"?hnlich ?blich n?tig ???".translate(d) u'\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' >>> u"?hnlich ?blich n?tig ???".translate(d).encode("latin1") '\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' From rosuav at gmail.com Sat Dec 3 21:52:23 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Dec 2011 13:52:23 +1100 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On Sun, Dec 4, 2011 at 7:59 AM, Gelonida N wrote: > if you write code nicely enough in python 2, then you can translate it > ?to python 3. autmatically. It's entirely possible to write code that can run on both Python 2 and Python 3 - at least, if you can target 2.6/2.7 and get the appropriate future directives. Add in a few exception-guarded imports for the renamed modules, and then all you're left with is a few things where you can take a little care while coding, run it through both versions to test, and have it all work. ChrisA From tjreedy at udel.edu Sat Dec 3 21:52:32 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 03 Dec 2011 21:52:32 -0500 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On 12/3/2011 3:59 PM, Gelonida N wrote: > I would still stick with python 2. > > In my opinion there is no reason to rush to the most recent version. Python 3 is 3 years old. Starting with it now is hardly rushing. There are several reasons someone 'in the process of learning Python' might want to start with it. The removal of old stuff makes it more steamlined and easier to learn. There is only one class system instead of two, one meaning of '/' instead of two, and one version of the input, range, map, and filter functions instead of two. For anyone working with unicode instead of ascii, Python 3 is much better, and 3.3 will be better yet. There are numerous improvements to the standard library. Most but not all bugfixes have been backported; changes and new features have not. What's New in 3.0/1/2 is a long list of possible reasons. -- Terry Jan Reedy From rosuav at gmail.com Sat Dec 3 22:02:15 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 4 Dec 2011 14:02:15 +1100 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: On Sun, Dec 4, 2011 at 1:52 PM, Terry Reedy wrote: > For anyone working with unicode instead of ascii... Which, frankly, should be everyone. You can't get away with assuming that a character is a byte any more; even if you stick to the US, you're going to run into some non-ASCII symbols sooner or later. Of course, you can work with UTF-8, which means that anything that fits into 7-bit ASCII will be represented as itself; but you still need to be aware of the difference between 'bytes' and 'str' (or between 'str' and 'unicode'). ChrisA From arnodel at gmail.com Sun Dec 4 04:32:23 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sun, 4 Dec 2011 09:32:23 +0000 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: On 3 December 2011 23:51, Peter Otten <__peter__ at web.de> wrote: > Arnaud Delobelle wrote: > >> I need to generate some java .properties files in Python (2.6 / 2.7). >> It's a simple format to store key/value pairs e.g. >> >> blue=bleu >> green=vert >> red=rouge >> >> The key/value are unicode strings. ?The annoying thing is that the >> file is encoded in ISO 8859-1, with all non Latin1 characters escaped >> in the form \uHHHH (same as how unicode characters are escaped in >> Python). >> >> I thought I could use the "unicode_escape" codec. ?But it doesn't work >> because it escapes Latin1 characters with escape sequences of the form >> \xHH, which is not valid in a java .properties file. >> >> Is there a simple way to achieve this? I could do something like this: >> >> def encode(u): >> ? ? """encode a unicode string in .properties format""" >> ? ? return u"".join(u"\\u%04x" % ord(c) if ord(c) > 0xFF else c for c >> in u).encode("latin_1") >> >> but it would be quite inefficient as I have many to generate. > >>>> class D(dict): > ... ? ? def __missing__(self, key): > ... ? ? ? ? ? ? result = self[key] = u"\\u%04x" % key > ... ? ? ? ? ? ? return result > ... >>>> d = D(enumerate(map(unichr, range(256)))) >>>> u"?hnlich ?blich n?tig ???" > u'\xe4hnlich \xfcblich n\xf6tig \u03a6\u03a7\u03a8' >>>> u"?hnlich ?blich n?tig ???".translate(d) > u'\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' >>>> u"?hnlich ?blich n?tig ???".translate(d).encode("latin1") > '\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' A very nice solution - thanks, Peter. -- Arnaud From __peter__ at web.de Sun Dec 4 05:22:10 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 04 Dec 2011 11:22:10 +0100 Subject: How to generate java .properties files in python References: Message-ID: Arnaud Delobelle wrote: > On 3 December 2011 23:51, Peter Otten <__peter__ at web.de> wrote: >> Arnaud Delobelle wrote: >> >>> I need to generate some java .properties files in Python (2.6 / 2.7). >>> It's a simple format to store key/value pairs e.g. >>> >>> blue=bleu >>> green=vert >>> red=rouge >>> >>> The key/value are unicode strings. The annoying thing is that the >>> file is encoded in ISO 8859-1, with all non Latin1 characters escaped >>> in the form \uHHHH (same as how unicode characters are escaped in >>> Python). >>> >>> I thought I could use the "unicode_escape" codec. But it doesn't work >>> because it escapes Latin1 characters with escape sequences of the form >>> \xHH, which is not valid in a java .properties file. >>> >>> Is there a simple way to achieve this? I could do something like this: >>> >>> def encode(u): >>> """encode a unicode string in .properties format""" >>> return u"".join(u"\\u%04x" % ord(c) if ord(c) > 0xFF else c for c >>> in u).encode("latin_1") >>> >>> but it would be quite inefficient as I have many to generate. >> >>>>> class D(dict): >> ... def __missing__(self, key): >> ... result = self[key] = u"\\u%04x" % key >> ... return result >> ... >>>>> d = D(enumerate(map(unichr, range(256)))) >>>>> u"?hnlich ?blich n?tig ???" >> u'\xe4hnlich \xfcblich n\xf6tig \u03a6\u03a7\u03a8' >>>>> u"?hnlich ?blich n?tig ???".translate(d) >> u'\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' >>>>> u"?hnlich ?blich n?tig ???".translate(d).encode("latin1") >> '\xe4hnlich \xfcblich n\xf6tig \\u03a6\\u03a7\\u03a8' > > A very nice solution - thanks, Peter. I found another one: >>> u"??? ???".encode("latin1", "backslashreplace") '\xe4\xf6\xfc \\u03a6\\u03a7\\u03a8' From lie.1296 at gmail.com Sun Dec 4 08:29:15 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 00:29:15 +1100 Subject: order independent hash? In-Reply-To: <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: On 12/02/2011 03:29 PM, 88888 Dihedral wrote: > > I clear my point a hash is a collection of (key, value) pairs that have > well defined methods and behavior to be used in programming. > > The basic operations of a hash normally includes the following: > > 1. insertion of a (key, value) pair into the hash > 2. deletion of a (key, value) from the hash > 3. inquiring a hash by a key to retrieve the value if the (key, value) > pair available in the hash. If no key matched, the hash will return > a not found result. > > The hash can grow with (k,v) pairs accumulated in the run time. > An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. > > Some implementations of a hash might pose some restrictions of k and v > for some reasons. But in object programming k and v can be objects > to be manipulated by the programmer. Strictly speaking, what you're describing is just a dictionary/mapping abstract data type (ADT), not a hashtable. Hashtable is a particular way to implement the dictionary/mapping ADT. Python's dictionary is implemented as hashtable, but there are other ways to implement a dictionary/mapping, such as using a sorted tree. For a data structure to be considered a Hashtable, in addition to having the properties of a dictionary that you described, the data structure must also uses a "hashing function" to encode the dictionary's keys into integer that will be used to calculate the index for the corresponding value in its internal array. A hashtable also must provide mechanism to deal with hash collisions to maintains its invariants as a dictionary. From lie.1296 at gmail.com Sun Dec 4 08:40:20 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 00:40:20 +1100 Subject: order independent hash? In-Reply-To: <1576053.251.1322804908048.JavaMail.geo-discussion-forums@pruu5> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <1576053.251.1322804908048.JavaMail.geo-discussion-forums@pruu5> Message-ID: On 12/02/2011 04:48 PM, 88888 Dihedral wrote: > On Friday, December 2, 2011 1:00:10 PM UTC+8, Chris Angelico wrote: >> On Fri, Dec 2, 2011 at 3:29 PM, 88888 Dihedral >> wrote: >>> I clear my point a hash is a collection of (key, value) pairs that have >>> well defined methods and behavior to be used in programming. >>> >>> The basic operations of a hash normally includes the following: >>> >>> 1. insertion of a (key, value) pair into the hash >>> 2. deletion of a (key, value) from the hash >>> 3. inquiring a hash by a key to retrieve the value if the (key, value) >>> pair available in the hash. If no key matched, the hash will return >>> a not found result. >>> >>> The hash can grow with (k,v) pairs accumulated in the run time. >>> An auto memory management mechanism is required for a hash of a non-fixed size of (k,v) pairs. >> >> That's a hash table - think of a Python dictionary: >> >> On Fri, Dec 2, 2011 at 3:33 PM, Steven D'Aprano >> wrote: >>> Python dicts are hash tables. >> >> Although strictly speaking, isn't that "Python dicts are implemented >> as hash tables in CPython"? Or is the hashtable implementation >> mandated? Anyway, near enough. >> > > >> Cryptography and data verification use hashing too (look at the >> various historic hashing algorithms - CRC, MD5, SHA, etc). The concept >> of a hash is a number (usually of a fixed size) that is calculated >> from a string or other large data type, such that hashing the same >> input will always give the same output, but hashing different input >> will usually give different output. It's then possible to identify a >> large object solely by its hash, as is done in git, for instance; or >> to transmit both the data and the hash, as is done in message >> protection schemes (many archiving programs/formats include a hash of >> the uncompressed data). These have nothing to do with (key,value) >> pairs, but are important uses of hashes. >> >> ChrisA > > If one tries to insert a (k,v1) and then a (k,v2) pair into a > hash with v1 not equals V2, what could happen in your understanding of > a hash? Don't try to argue, in English, `hash != hash` is true; it's just a typical occurence of homonyms. Just because they have the same name doesn't mean hash (function) has to have somewhat similar properties to hash (table). > A hash function is different from a hash or so called a hash table in > my post. Indeed. > If the hash collision rate is not specified, then it is trivial to write a hash function with the conditions you specified. A hash function applied to a set of data items only is of very limited use at all. It's trivial indeed, but a hashtable couldn't exist without hash function. And without a good hash function, a hash table's performance may degrade into O(n) access/insertion/deletion. > A hash stores (k,v) pairs specified in the run time with auto memory > management build in is not a simple hash function to produce data signatures only clearly in my post. > > What I said a hash which is lifted as a basic type in python is called a dictionary in python. > > It is called a map in c++'s generics library. Putting aside all these, it's pretty obvious from the beginning that OP was referring to hash functions, not hash tables. From rschroev_nospam_ml at fastmail.fm Sun Dec 4 08:41:19 2011 From: rschroev_nospam_ml at fastmail.fm (Roel Schroeven) Date: Sun, 04 Dec 2011 14:41:19 +0100 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> Message-ID: <3MKCq.194015$th7.47228@newsfe07.ams2> Op 2011-12-02 6:48, 88888 Dihedral schreef: > A hash stores (k,v) pairs specified in the run time with auto memory > management build in is not a simple hash function to produce data > signatures only clearly in my post. > > What I said a hash which is lifted as a basic type in python is > called a dictionary in python. > > It is called a map in c++'s generics library. Not exactly: a C++ std::map uses a tree structure (which is why it keeps the keys sorted). C++ STL also has std::hash_map which, as the name implies, does use a hash table implementation. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven From hniksic at xemacs.org Sun Dec 4 08:55:32 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Sun, 04 Dec 2011 14:55:32 +0100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> Message-ID: <87aa78bfvv.fsf@xemacs.org> Terry Reedy writes: >> [Hashing is] pretty much mandated because of the __hash__ protocol. > > Lib Ref 4.8. Mapping Types ? dict > "A mapping object maps hashable values to arbitrary objects." > > This does not say that the mapping has to *use* the hash value ;-). > Even if it does, it could use a tree structure instead of a hash > table. An arbitrary mapping doesn't, but reference to the hash protocol was in the context of implementation constraints for dicts themselves (my response quotes the relevant part of Chris's message). If a Python implementation tried to implement dict as a tree, instances of classes that define only __eq__ and __hash__ would not be correctly inserted in such a dict. This would be a major source of incompatibility with Python code, both in the standard library and at large. From rosuav at gmail.com Sun Dec 4 09:08:53 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 01:08:53 +1100 Subject: order independent hash? In-Reply-To: <87aa78bfvv.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> Message-ID: 2011/12/5 Hrvoje Niksic : > If a Python > implementation tried to implement dict as a tree, instances of classes > that define only __eq__ and __hash__ would not be correctly inserted in > such a dict. Couldn't you just make a tree of hash values? Okay, that's probably not the most useful way to do things, but technically it'd comply with the spec. ChrisA From glen.rice.noaa at gmail.com Sun Dec 4 09:25:16 2011 From: glen.rice.noaa at gmail.com (Glen Rice) Date: Sun, 4 Dec 2011 06:25:16 -0800 (PST) Subject: struct calcsize discrepency? Message-ID: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> In IPython: >import struct >struct.calcsize('4s') 4 >struct.calcsize('Q') 8 >struct.calcsize('4sQ') 16 This doesn't make sense to me. Can anyone explain? From arnodel at gmail.com Sun Dec 4 09:33:34 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sun, 4 Dec 2011 14:33:34 +0000 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: On 4 December 2011 10:22, Peter Otten <__peter__ at web.de> wrote: > I found another one: > >>>> u"??? ???".encode("latin1", "backslashreplace") > '\xe4\xf6\xfc \\u03a6\\u03a7\\u03a8' That's it! I was hoping for a built-in solution and this is it. FTR, the 'backslashreplace' argument tells the encoder to replace any character that it can't encode with a backslash escape sequence. Which is exactly what I needed, only I hadn't realised it. Thanks! -- Arnaud From rosuav at gmail.com Sun Dec 4 09:35:28 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 01:35:28 +1100 Subject: struct calcsize discrepency? In-Reply-To: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> Message-ID: On Mon, Dec 5, 2011 at 1:25 AM, Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me. ?Can anyone explain? Same thing happens in CPython, and it looks to be the result of alignment. >>> struct.calcsize("4sQ") 16 >>> struct.calcsize("Q4s") 12 The eight-byte integer is aligned on an eight-byte boundary, so when it follows a four-byte string, you get four padding bytes inserted. Put them in the other order, and the padding disappears. (Caveat: I don't use the struct module much, this is based on conjecture.) ChrisA From duncan.booth at invalid.invalid Sun Dec 4 09:38:06 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 4 Dec 2011 14:38:06 GMT Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> Message-ID: Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me. Can anyone explain? > When you mix different types in a struct there can be padding inserted between the items. In this case the 8 byte unsigned long long must always start on an 8 byte boundary so 4 padding bytes are inserted. See http://docs.python.org/library/struct.html?highlight=struct#byte-order- size-and-alignment in particular the first sentence: "By default, C types are represented in the machine?s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler)." -- Duncan Booth http://kupuguy.blogspot.com From glen.rice.noaa at gmail.com Sun Dec 4 09:40:53 2011 From: glen.rice.noaa at gmail.com (Glen Rice) Date: Sun, 4 Dec 2011 06:40:53 -0800 (PST) Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> Message-ID: <59cea0e3-2f09-4f47-a0fc-c970037dc8e2@p14g2000yqp.googlegroups.com> On Dec 4, 9:38?am, Duncan Booth wrote: > Glen Rice wrote: > > In IPython: > >>import struct > >>struct.calcsize('4s') > > 4 > >>struct.calcsize('Q') > > 8 > >>struct.calcsize('4sQ') > > 16 > > > This doesn't make sense to me. ?Can anyone explain? > > When you mix different types in a struct there can be padding inserted > between the items. In this case the 8 byte unsigned long long must always > start on an 8 byte boundary so 4 padding bytes are inserted. > > Seehttp://docs.python.org/library/struct.html?highlight=struct#byte-order- > size-and-alignment in particular the first sentence: > > "By default, C types are represented in the machine s native format and > byte order, and properly aligned by skipping pad bytes if necessary > (according to the rules used by the C compiler)." > > -- > Duncan Boothhttp://kupuguy.blogspot.com Chris / Duncan, Thanks. I missed that in the docs. From __peter__ at web.de Sun Dec 4 09:49:36 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 04 Dec 2011 15:49:36 +0100 Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> Message-ID: Glen Rice wrote: > In IPython: >>import struct >>struct.calcsize('4s') > 4 >>struct.calcsize('Q') > 8 >>struct.calcsize('4sQ') > 16 > > This doesn't make sense to me. Can anyone explain? A C compiler can insert padding bytes into a struct: """By default, the result of packing a given C struct includes pad bytes in order to maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. This behavior is chosen so that the bytes of a packed struct correspond exactly to the layout in memory of the corresponding C struct. To handle platform-independent data formats or omit implicit pad bytes, use standard size and alignment instead of native size and alignment: see Byte Order, Size, and Alignment for details. """ http://docs.python.org/library/struct.html#struct-alignment You can avoid this by specifying a non-native byte order (little endian, big endian, or "network"): >>> struct.calcsize("4sQ") 16 >>> struct.calcsize("!4sQ") 12 From d at davea.name Sun Dec 4 09:51:41 2011 From: d at davea.name (Dave Angel) Date: Sun, 04 Dec 2011 09:51:41 -0500 Subject: struct calcsize discrepency? In-Reply-To: References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> Message-ID: <4EDB88FD.9000108@davea.name> On 12/04/2011 09:35 AM, Chris Angelico wrote: > On Mon, Dec 5, 2011 at 1:25 AM, Glen Rice wrote: >> In IPython: >>> import struct >>> struct.calcsize('4s') >> 4 >>> struct.calcsize('Q') >> 8 >>> struct.calcsize('4sQ') >> 16 >> >> This doesn't make sense to me. Can anyone explain? > Same thing happens in CPython, and it looks to be the result of alignment. > >>>> struct.calcsize("4sQ") > 16 >>>> struct.calcsize("Q4s") > 12 > > The eight-byte integer is aligned on an eight-byte boundary, so when > it follows a four-byte string, you get four padding bytes inserted. > Put them in the other order, and the padding disappears. > NOT disappears. In C, the padding to the largest alignment occurs at the end of a structure as well as between items. Otherwise, an array of the struct would not be safely aligned. if you have an 8byte item followed by a 4 byte item, the total size is 16. > (Caveat: I don't use the struct module much, this is based on conjecture.) > > ChrisA -- DaveA From rosuav at gmail.com Sun Dec 4 10:17:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 02:17:41 +1100 Subject: struct calcsize discrepency? In-Reply-To: <4EDB88FD.9000108@davea.name> References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> <4EDB88FD.9000108@davea.name> Message-ID: On Mon, Dec 5, 2011 at 1:51 AM, Dave Angel wrote: > On 12/04/2011 09:35 AM, Chris Angelico wrote: >> >>>>> struct.calcsize("4sQ") >> >> 16 >>>>> >>>>> struct.calcsize("Q4s") >> >> 12 >> >> The eight-byte integer is aligned on an eight-byte boundary, so when >> it follows a four-byte string, you get four padding bytes inserted. >> Put them in the other order, and the padding disappears. >> > NOT disappears. ?In C, the padding to the largest alignment occurs at the > end of a structure as well as between items. ?Otherwise, an array of the > struct would not be safely aligned. ?if you have an 8byte item followed by a > 4 byte item, the total size is 16. That's padding of the array, not of the structure. But you're right in that removing padding from inside the structure will in this case result in padding outside the structure. However, in more realistic scenarios, it's often possible to truly eliminate padding by ordering members appropriately. ChrisA From dihedral88888 at googlemail.com Sun Dec 4 10:39:03 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 07:39:03 -0800 (PST) Subject: order independent hash? In-Reply-To: <3MKCq.194015$th7.47228@newsfe07.ams2> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> Message-ID: <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> On Sunday, December 4, 2011 9:41:19 PM UTC+8, Roel Schroeven wrote: > Op 2011-12-02 6:48, 88888 Dihedral schreef: > > A hash stores (k,v) pairs specified in the run time with auto memory > > management build in is not a simple hash function to produce data > > signatures only clearly in my post. > > > > What I said a hash which is lifted as a basic type in python is > > called a dictionary in python. > > > > It is called a map in c++'s generics library. > > Not exactly: a C++ std::map uses a tree structure (which is why it keeps > the keys sorted). C++ STL also has std::hash_map which, as the name > implies, does use a hash table implementation. > Thanks for your comments. Are we gonna talk about the way to implement a hash table or the use of a hash table in programming? From phd at phdru.name Sun Dec 4 12:24:59 2011 From: phd at phdru.name (Oleg Broytman) Date: Sun, 4 Dec 2011 21:24:59 +0400 Subject: SQLObject 1.2.1 Message-ID: <20111204172459.GC1538@iskra.aviel.ru> Hello! I'm pleased to announce version 1.2.1, the first stable release of branch 1.2 of SQLObject. What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as classes, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with. SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite, Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB). Where is SQLObject ================== Site: http://sqlobject.org Development: http://sqlobject.org/devel/ Mailing list: https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss Archives: http://news.gmane.org/gmane.comp.python.sqlobject Download: http://pypi.python.org/pypi/SQLObject/1.2.1 News and changes: http://sqlobject.org/News.html What's New ========== * A bug was fixed in handling ``modulo`` operator - SQLite implements only ``%``, MySQL - only ``MOD()``, PostgreSQL implements both. For a more complete list, please see the news: http://sqlobject.org/News.html Oleg. -- Oleg Broytman http://phdru.name/ phd at phdru.name Programmers don't die, they just GOSUB without RETURN. From ian.g.kelly at gmail.com Sun Dec 4 12:41:14 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 4 Dec 2011 10:41:14 -0700 Subject: order independent hash? In-Reply-To: <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> Message-ID: On Sun, Dec 4, 2011 at 8:39 AM, 88888 Dihedral wrote: > Thanks for your comments. Are we gonna talk about the way to implement a hash > table or the use of a hash table in programming? Implementing a hash table is not very relevant on a list about Python, which already has them built into the language; and any pure Python implementation would be uselessly slow. If you want to talk about ways to use dicts, please start a different thread for it. As has been pointed out several times now, it is off-topic for this thread, which is about hash *functions*. From dihedral88888 at googlemail.com Sun Dec 4 13:06:23 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 10:06:23 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> Message-ID: <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> A hash that can hash objects is not a trivial hash function On Monday, December 5, 2011 1:41:14 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 8:39 AM, 88888 Dihedral > wrote: > > Thanks for your comments. Are we gonna talk about the way to implement a hash > > table or the use of a hash table in programming? > > Implementing a hash table is not very relevant on a list about Python, > which already has them built into the language; and any pure Python > implementation would be uselessly slow. > > If you want to talk about ways to use dicts, please start a different > thread for it. As has been pointed out several times now, it is > off-topic for this thread, which is about hash *functions*. A hash that can hash objects is not a hash function at all. Are you miss-leading the power of true OOP ? From dihedral88888 at googlemail.com Sun Dec 4 13:06:23 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 10:06:23 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> Message-ID: <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> A hash that can hash objects is not a trivial hash function On Monday, December 5, 2011 1:41:14 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 8:39 AM, 88888 Dihedral > wrote: > > Thanks for your comments. Are we gonna talk about the way to implement a hash > > table or the use of a hash table in programming? > > Implementing a hash table is not very relevant on a list about Python, > which already has them built into the language; and any pure Python > implementation would be uselessly slow. > > If you want to talk about ways to use dicts, please start a different > thread for it. As has been pointed out several times now, it is > off-topic for this thread, which is about hash *functions*. A hash that can hash objects is not a hash function at all. Are you miss-leading the power of true OOP ? From wescpy at gmail.com Sun Dec 4 13:10:20 2011 From: wescpy at gmail.com (wesley chun) Date: Sun, 4 Dec 2011 10:10:20 -0800 Subject: LinuxJournal Readers' Choice Awards 2011 Best {Programming, Scripting} Language Message-ID: in recent news... Python wins LinuxJournal's Readers' Choice Awards 2011 as Best Programming Language: http://www.linuxjournal.com/slideshow/readers-choice-2011?page=27 yee-haw!! it's even more amazing that Python has won this title 3 straight years. let's celebrate and get back to building great things. wait, in other news... Python wins LinuxJournal's Readers' Choice Awards 2011 as Best Scripting Language: http://www.linuxjournal.com/slideshow/readers-choice-2011?page=28 interestingly enough, this happened last year as well: http://www.linuxjournal.com/content/readers-choice-awards-2010 in fact, Python has nearly won this one 6 straight years, from 2006-2011, except bash won in 2009. is it the same people who are voting (practically) every year? :-) cheers, --wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 ? ? http://corepython.com wesley.chun : wescpy-gmail.com : @wescpy python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From python.list at tim.thechases.com Sun Dec 4 13:57:50 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 04 Dec 2011 12:57:50 -0600 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> Message-ID: <4EDBC2AE.2070303@tim.thechases.com> On 12/04/11 08:08, Chris Angelico wrote: > 2011/12/5 Hrvoje Niksic: >> If a Python >> implementation tried to implement dict as a tree, instances of classes >> that define only __eq__ and __hash__ would not be correctly inserted in >> such a dict. > > Couldn't you just make a tree of hash values? Okay, that's probably > not the most useful way to do things, but technically it'd comply with > the spec. From an interface perspective, I suppose it would work. However one of the main computer-science reasons for addressing by a hash is to get O(1) access to items (modulo pessimal hash structures/algorithms which can approach O(N) if everything hashes to the same value/bucket), rather than the O(logN) time you'd get from a tree. So folks reaching for a hash/map might be surprised if performance degraded with the size of the contents. -tkc From ian.g.kelly at gmail.com Sun Dec 4 15:13:01 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 4 Dec 2011 13:13:01 -0700 Subject: order independent hash? In-Reply-To: <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> Message-ID: On Sun, Dec 4, 2011 at 11:06 AM, 88888 Dihedral wrote: >> If you want to talk about ways to use dicts, please start a different >> thread for it. ?As has been pointed out several times now, it is >> off-topic for this thread, which is about hash *functions*. > > A hash that can hash objects is not a hash function at all. Please explain what you think a hash function is, then. Per Wikipedia, "A hash function is any algorithm or subroutine that maps large data sets to smaller data sets, called keys." > Are you miss-leading the power of true OOP ? I have no idea what you are suggesting. I was not talking about OOP at all. From zaphod at beeblebrox.net Sun Dec 4 15:18:40 2011 From: zaphod at beeblebrox.net (Zaphod) Date: Sun, 04 Dec 2011 20:18:40 GMT Subject: Python 2 or 3 References: Message-ID: On Sat, 03 Dec 2011 05:54:19 +0200, Antti J Ylikoski wrote: > I'm in the process of learning Python. I already can code > objet-oriented programs with the language. I have in my hands the > O'Reilly book by Mark Lutz, Programming Python, in two versions: the 2nd > Edition, which covers Python 2, and the 4th edition, which covers Python > 3. It doesn't matter that much which one you learn. The differences aren't that big between the two. You could easily use the 2nd edition but use python 3 and the biggest difference you will find is between print "stuff" vs print("stuff"). In fact, doing it this way will make you a better programmer. > Cheers, Antti "Andy" Ylikoski > Helsinki, Finland, the EU ps: I quite like your Finnish "Dudesons" program. They remind me of some of the rednecks I grew up with here on Vancouver Island (British Columbia, Canada) From blair.bethwaite at gmail.com Sun Dec 4 18:10:53 2011 From: blair.bethwaite at gmail.com (Blairo) Date: Sun, 4 Dec 2011 15:10:53 -0800 (PST) Subject: SSL connection issue with Windows CPython Message-ID: Hi all, I've written a Python API for the Windows Azure Service Management web- service. Requests against the web-service are HTTPS with a client certificate used for authentication. This works fine with CPython (tested with 2.6 and 2.7) on Linux, but something is amiss with the SSL connection with vanilla python.org CPython (again 2.6 and 2.7) on Windows. I've managed to boil it down to a simple test case, which should return a list of Azure data-centre locations (requires an Azure account): -------------------- Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> sub_id = '506...-...-...-...-...' >>> cert = 'c:\\users\\blair\\...\\azure.pem' >>> import socket,ssl >>> s = ssl.wrap_socket(socket.socket(), certfile=cert) >>> s.connect(('management.core.windows.net',443)) >>> s.send("GET /%s/locations HTTP/1.1\r\nAccept-Encoding: identity\r\nX-Ms-Version: 2011-10-01\r\nHost: management.core.windows.net\r\nConnection: close\r\nUser-Agent: Python-urllib/2.7\r\n\r\n" % sub_id) 202 >>> s.read(2048) Traceback (most recent call last): File "", line 1, in s.read(2048) File "C:\Python27\lib\ssl.py", line 138, in read return self._sslobj.read(len) error: [Errno 10054] An existing connection was forcibly closed by the remote host -------------------- What's interesting is that the exact same code works with ActivePython (2.6 and 2.7), output omitted here for brevity. There is more detail on a (currently unanswered) stackoverflow post: http://stackoverflow.com/questions/8342714/python-https-against-azure-service-management-api-fails-on-windows . I'm not sure where/what the difference is. My best guess so far is that ActivePython bundles a newer version of OpenSSL then the python.org version and that the problem must be there. Any further insight would be appreciated. TIA, ~Blair From dihedral88888 at googlemail.com Sun Dec 4 18:17:21 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 15:17:21 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> Message-ID: <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> On Monday, December 5, 2011 4:13:01 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 11:06 AM, 88888 Dihedral > wrote: > >> If you want to talk about ways to use dicts, please start a different > >> thread for it. ?As has been pointed out several times now, it is > >> off-topic for this thread, which is about hash *functions*. > > > > A hash that can hash objects is not a hash function at all. > > Please explain what you think a hash function is, then. Per > Wikipedia, "A hash function is any algorithm or subroutine that maps > large data sets to smaller data sets, called keys." > > > Are you miss-leading the power of true OOP ? > > I have no idea what you are suggesting. I was not talking about OOP at all. In python the (k,v) pair in a dictionary k and v can be both an objects. v can be a tuple or a list. There are some restrictions on k to be an hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both mapped to the same position, then something has to be done to resolve this. From dihedral88888 at googlemail.com Sun Dec 4 18:17:21 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 15:17:21 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> Message-ID: <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> On Monday, December 5, 2011 4:13:01 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 11:06 AM, 88888 Dihedral > wrote: > >> If you want to talk about ways to use dicts, please start a different > >> thread for it. ?As has been pointed out several times now, it is > >> off-topic for this thread, which is about hash *functions*. > > > > A hash that can hash objects is not a hash function at all. > > Please explain what you think a hash function is, then. Per > Wikipedia, "A hash function is any algorithm or subroutine that maps > large data sets to smaller data sets, called keys." > > > Are you miss-leading the power of true OOP ? > > I have no idea what you are suggesting. I was not talking about OOP at all. In python the (k,v) pair in a dictionary k and v can be both an objects. v can be a tuple or a list. There are some restrictions on k to be an hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both mapped to the same position, then something has to be done to resolve this. From anacrolix at gmail.com Sun Dec 4 18:21:12 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Mon, 5 Dec 2011 10:21:12 +1100 Subject: order independent hash? In-Reply-To: <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: Duh. What's the point you're trying to make? On Mon, Dec 5, 2011 at 10:17 AM, 88888 Dihedral wrote: > On Monday, December 5, 2011 4:13:01 AM UTC+8, Ian wrote: >> On Sun, Dec 4, 2011 at 11:06 AM, 88888 Dihedral >> wrote: >> >> If you want to talk about ways to use dicts, please start a different >> >> thread for it. ?As has been pointed out several times now, it is >> >> off-topic for this thread, which is about hash *functions*. >> > >> > A hash that can hash objects is not a hash function at all. >> >> Please explain what you think a hash function is, then. ?Per >> Wikipedia, "A hash function is any algorithm or subroutine that maps >> large data sets to smaller data sets, called keys." >> >> > Are you miss-leading the power of true OOP ? >> >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > v can be a tuple or a list. ?There are some restrictions on k to be an > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > position, then something has to be done to resolve this. > > > > > > > -- > http://mail.python.org/mailman/listinfo/python-list -- ?_? From ian.g.kelly at gmail.com Sun Dec 4 18:24:49 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 4 Dec 2011 16:24:49 -0700 Subject: order independent hash? In-Reply-To: <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral wrote: >> Please explain what you think a hash function is, then. ?Per >> Wikipedia, "A hash function is any algorithm or subroutine that maps >> large data sets to smaller data sets, called keys." >> >> > Are you miss-leading the power of true OOP ? >> >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > v can be a tuple or a list. ?There are some restrictions on k to be an > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > position, then something has to be done to resolve this. I understand how dicts / hash tables work. I don't need you to explain that to me. What you haven't explained is why you stated that a hash function that operates on objects is not a hash function, or what you meant by "misleading the power of true OOP". From tjreedy at udel.edu Sun Dec 4 19:28:12 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 04 Dec 2011 19:28:12 -0500 Subject: order independent hash? In-Reply-To: <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: On 12/4/2011 6:17 PM, 88888 Dihedral wrote: > In python the (k,v) pair in a dictionary k and v can be both an objects. > v can be a tuple or a list. In Python, everything is an object. *tuple* and *list* are subclasses of *object*. The value v for a dict can be any object, and must be an object. -- Terry Jan Reedy From dihedral88888 at googlemail.com Sun Dec 4 19:52:14 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 16:52:14 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral > wrote: > >> Please explain what you think a hash function is, then. ?Per > >> Wikipedia, "A hash function is any algorithm or subroutine that maps > >> large data sets to smaller data sets, called keys." > >> > >> > Are you miss-leading the power of true OOP ? > >> > >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > > v can be a tuple or a list. ?There are some restrictions on k to be an > > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > > position, then something has to be done to resolve this. > > I understand how dicts / hash tables work. I don't need you to > explain that to me. What you haven't explained is why you stated that > a hash function that operates on objects is not a hash function, or > what you meant by "misleading the power of true OOP". If v is a tuple or a list then a dictionary in python can replace a bi-directional list or a tree under the assumption that the hash which accesses values stored in a much faster way when well implemented. From dihedral88888 at googlemail.com Sun Dec 4 19:52:14 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 4 Dec 2011 16:52:14 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral > wrote: > >> Please explain what you think a hash function is, then. ?Per > >> Wikipedia, "A hash function is any algorithm or subroutine that maps > >> large data sets to smaller data sets, called keys." > >> > >> > Are you miss-leading the power of true OOP ? > >> > >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > > v can be a tuple or a list. ?There are some restrictions on k to be an > > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > > position, then something has to be done to resolve this. > > I understand how dicts / hash tables work. I don't need you to > explain that to me. What you haven't explained is why you stated that > a hash function that operates on objects is not a hash function, or > what you meant by "misleading the power of true OOP". If v is a tuple or a list then a dictionary in python can replace a bi-directional list or a tree under the assumption that the hash which accesses values stored in a much faster way when well implemented. From lie.1296 at gmail.com Sun Dec 4 20:59:46 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 12:59:46 +1100 Subject: order independent hash? In-Reply-To: <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> Message-ID: On 12/05/2011 11:52 AM, 88888 Dihedral wrote: > On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: >> On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral >> wrote: >>>> Please explain what you think a hash function is, then. Per >>>> Wikipedia, "A hash function is any algorithm or subroutine that maps >>>> large data sets to smaller data sets, called keys." >>>> >>>>> Are you miss-leading the power of true OOP ? >>>> >>>> I have no idea what you are suggesting. I was not talking about OOP at all. >>> >>> In python the (k,v) pair in a dictionary k and v can be both an objects. >>> v can be a tuple or a list. There are some restrictions on k to be an >>> hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both mapped to the same >>> position, then something has to be done to resolve this. >> >> I understand how dicts / hash tables work. I don't need you to >> explain that to me. What you haven't explained is why you stated that >> a hash function that operates on objects is not a hash function, or >> what you meant by "misleading the power of true OOP". > > If v is a tuple or a list then a dictionary in python can replace a bi-directional list or a tree under the assumption that the hash which accesses values stored in a much faster way when well implemented. trying not to be rude, but the more you talk, the more I"m convince that you're trolling. Welcome to my killfile. From steve+comp.lang.python at pearwood.info Sun Dec 4 21:00:53 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 02:00:53 GMT Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: <4edc25d5$0$11114$c3e8da3@news.astraweb.com> Dihedral, you're back to double posting. Please stop. Send to the mailing list, or to the newsgroup, it doesn't matter. But don't send to both. Further comments below. On Sun, 04 Dec 2011 16:52:14 -0800, 88888 Dihedral wrote: > If v is a tuple or a list then a dictionary in python can replace a > bi-directional list or a tree under the assumption that the hash which > accesses values stored in a much faster way when well implemented. No it can't. The keys in a hash tables are unordered. You get an order when you iterate over them, but that order is arbitrary. Keys in a list or tree are ordered. E.g. collections.OrderedDict remembers the order that keys are added. If Python were to replace OrderedDicts with dicts, it would break code. -- Steven From ethan at stoneleaf.us Sun Dec 4 21:08:01 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 04 Dec 2011 18:08:01 -0800 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> Message-ID: <4EDC2781.4020801@stoneleaf.us> Lie Ryan wrote: > On 12/05/2011 11:52 AM, 88888 Dihedral wrote: >> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: >>> On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral >>> wrote: >>>>> Please explain what you think a hash function is, then. Per >>>>> Wikipedia, "A hash function is any algorithm or subroutine that maps >>>>> large data sets to smaller data sets, called keys." >>>>> >>>>>> Are you miss-leading the power of true OOP ? >>>>> >>>>> I have no idea what you are suggesting. I was not talking about >>>>> OOP at all. >>>> >>>> In python the (k,v) pair in a dictionary k and v can be both an >>>> objects. >>>> v can be a tuple or a list. There are some restrictions on k to be an >>>> hashable type in python's implementation. The key is used to >>>> compute the position of the pair to be stored in a hash table. The >>>> hash function maps key k to the position in the hash table. If >>>> k1!=k2 are both mapped to the same >>>> position, then something has to be done to resolve this. >>> >>> I understand how dicts / hash tables work. I don't need you to >>> explain that to me. What you haven't explained is why you stated that >>> a hash function that operates on objects is not a hash function, or >>> what you meant by "misleading the power of true OOP". >> >> If v is a tuple or a list then a dictionary in python can replace a >> bi-directional list or a tree under the assumption that the hash >> which accesses values stored in a much faster way when well >> implemented. > > trying not to be rude, but the more you talk, the more I"m convince that > you're trolling. Welcome to my killfile. I think he's a bot, and he's been in my killfile for a while now. ~Ethan~ From anacrolix at gmail.com Sun Dec 4 22:02:46 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Mon, 5 Dec 2011 14:02:46 +1100 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> Message-ID: Yes. I sent a mail earlier asking such and it was bounced. I'm one email from also blocking this fellow. On Mon, Dec 5, 2011 at 12:59 PM, Lie Ryan wrote: > On 12/05/2011 11:52 AM, 88888 Dihedral wrote: >> >> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: >>> >>> On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral >>> ?wrote: >>>>> >>>>> Please explain what you think a hash function is, then. ?Per >>>>> Wikipedia, "A hash function is any algorithm or subroutine that maps >>>>> large data sets to smaller data sets, called keys." >>>>> >>>>>> Are you miss-leading the power of true OOP ? >>>>> >>>>> >>>>> I have no idea what you are suggesting. ?I was not talking about OOP at >>>>> all. >>>> >>>> >>>> In python the (k,v) pair in a dictionary k and v can be ?both an >>>> objects. >>>> v can be a tuple or a list. ?There are some restrictions on k to be an >>>> ?hashable type in python's implementation. The key is used to compute >>>> the position of the pair to be stored in a ?hash table. The hash function >>>> maps key k to the position in the hash table. If k1!=k2 are both ?mapped to >>>> the same >>>> position, then something has to be done to resolve this. >>> >>> >>> I understand how dicts / hash tables work. ?I don't need you to >>> explain that to me. ?What you haven't explained is why you stated that >>> a hash function that operates on objects is not a hash function, or >>> what you meant by "misleading the power of true OOP". >> >> >> If v is a tuple or a list then a dictionary in python can replace a >> bi-directional list or a tree under the assumption that the hash which >> ?accesses values stored in a ?much faster way ?when well implemented. > > > trying not to be rude, but the more you talk, the more I"m convince that > you're trolling. Welcome to my killfile. > > -- > http://mail.python.org/mailman/listinfo/python-list -- ?_? From ben+python at benfinney.id.au Sun Dec 4 23:52:16 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 05 Dec 2011 15:52:16 +1100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> <22840051.509.1323046334160.JavaMail.geo-discussion-forums@prnu18> Message-ID: <87obvn39j3.fsf@benfinney.id.au> Ethan Furman writes: > Lie Ryan wrote: > > trying not to be rude, but the more you talk, the more I"m convince > > that you're trolling. Welcome to my killfile. > > I think he's a bot, and he's been in my killfile for a while now. Having a ludicrous name doesn't help, and is part of what dropped them into my kill file. Maybe when they give a human-friendly name, and drop the insistence on being right rather than learning something, they can emerge from some of our kill files. -- \ ?We jealously reserve the right to be mistaken in our view of | `\ what exists, given that theories often change under pressure | _o__) from further investigation.? ?Thomas W. Clark, 2009 | Ben Finney From drsalists at gmail.com Mon Dec 5 00:50:08 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Sun, 4 Dec 2011 21:50:08 -0800 Subject: order independent hash? In-Reply-To: References: Message-ID: Two methods: 1) If you need your hash only once in an infrequent while, then save the elements in a list, appending as needed, and sort prior to hashing, as needed 2) If you need your hash more often, you could keep your elements in a treap or red-black tree; these will maintain sortedness throughout the life of the datastructure. 3) If A bunch of log(n) or n or nlog(n) operations doesn't sound appealing, then you might try this one: Create some sort of mapping from your elements to the integers. Then just use a sum. This won't scatter things nearly as well as a cryptographic hash, but it's very fast, because you don't need to reevaluate some of your members as you go. HTH On 11/30/11, Neal Becker wrote: > I like to hash a list of words (actually, the command line args of my > program) > in such a way that different words will create different hash, but not > sensitive > to the order of the words. Any ideas? > > -- > http://mail.python.org/mailman/listinfo/python-list > From ss27051980 at gmail.com Mon Dec 5 02:11:40 2011 From: ss27051980 at gmail.com (Suresh Sharma) Date: Mon, 5 Dec 2011 12:41:40 +0530 Subject: class print method... Message-ID: Hello All, I am new to python and i have stuck up on a particular issue with classes, i understand this might be a very dumb question but please help me out. I have created two classes and whenever i try to print the objects i get this message but not the data, __main__.cards instance at ****(memory location) i even tried using __str__ but calling it also produces the same result. Can anyone please help me how to get my objects printed. I googled a lot but could not find anything relevant. thanks in advance regards suresh -------------- next part -------------- An HTML attachment was scrubbed... URL: From dickinsm at gmail.com Mon Dec 5 02:42:22 2011 From: dickinsm at gmail.com (Mark Dickinson) Date: Sun, 4 Dec 2011 23:42:22 -0800 (PST) Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> <4EDB88FD.9000108@davea.name> Message-ID: On Dec 4, 3:17?pm, Chris Angelico wrote: > On Mon, Dec 5, 2011 at 1:51 AM, Dave Angel wrote: > > In C, the padding to the largest alignment occurs at the > > end of a structure as well as between items. ?Otherwise, an array of the > > struct would not be safely aligned. ?if you have an 8byte item followed by a > > 4 byte item, the total size is 16. > > That's padding of the array, not of the structure. That's a strange way to think of it, especially since the padding also happens for a single struct object when there's no array present. I find it cleaner to think of C as having no padding in arrays, but padding at the end of a struct. See C99 6.7.2.1p15: 'There may be unnamed padding at the end of a structure or union.' There's no mention in the standard of padding for arrays. -- Mark From d at davea.name Mon Dec 5 02:48:45 2011 From: d at davea.name (Dave Angel) Date: Mon, 05 Dec 2011 02:48:45 -0500 Subject: class print method... In-Reply-To: References: Message-ID: <4EDC775D.4000906@davea.name> On 12/05/2011 02:11 AM, Suresh Sharma wrote: > Hello All, > I am new to python and i have stuck up on a particular issue with classes, > i understand this might be a very dumb question but please help me out. > > I have created two classes and whenever i try to print the objects i get > this message but not the data, __main__.cards instance at ****(memory > location) i even tried using __str__ but calling it also produces the same > result. Can anyone please help me how to get my objects printed. I googled > a lot but could not find anything relevant. > > thanks in advance > > regards > suresh > You were close, but you have it backward. You don't call __str__() to print an object, you implement __str__() in your object. If you write a class without also writing __str__(), then print won't know what to do with it. -- DaveA From news3 at mystrobl.de Mon Dec 5 03:01:41 2011 From: news3 at mystrobl.de (Wolfgang Strobl) Date: Mon, 05 Dec 2011 09:01:41 +0100 Subject: Install Python on Windows without Start Menu icons? References: Message-ID: "Pedro Henrique G. Souto" : >On 02/12/2011 16:34, snorble wrote: >> Is it possible to automate the Python installation on Windows using >> the MSI file so it does not add a Start Menu folder? I would like to >> push out Python to all of my office workstations, but I'd like for it >> to be relatively silent from the user's point of view. > >If you just want to run python scripts in those machines (not developing >in it), you can use something like py2exe [http://www.py2exe.org/]. That doesn't answer the question. Snorble might use "ORCA", a MSI editor from Microsoft. http://forums.frontmotion.com/viewtopic.php?f=10&t=837 discusses a similar question for Firefox. I haven't tried it myself, but would give it a try. -- Thank you for observing all safety precautions From dpalao.python at gmail.com Mon Dec 5 03:02:08 2011 From: dpalao.python at gmail.com (DPalao) Date: Mon, 5 Dec 2011 09:02:08 +0100 Subject: 70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object In-Reply-To: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> References: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> Message-ID: <3650_1323072147_pB582GT7023203_201112050902.09089.dpalao.python@gmail.com> El Martes Noviembre 29 2011, DPalao escribi?: > Hello, > I'm trying to use multiprocessing to parallelize a code. There is a number > of tasks (usually 12) that can be run independently. Each task produces a > numpy array, and at the end, those arrays must be combined. > I implemented this using Queues (multiprocessing.Queue): one for input and > another for output. > But the code blocks. And it must be related to the size of the item I put > on the Queue: if I put a small array, the code works well; if the array is > realistically large (in my case if can vary from 160kB to 1MB), the code > blocks apparently forever. > I have tried this: > http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-lar > ge- objects/ > but it didn't work (especifically I put a None sentinel at the end for each > worker). > > Before I change the implementation, > is there a way to bypass this problem with multiprocessing.Queue? > Should I post the code (or a sketchy version of it)? > > TIA, > > David Just for reference. The other day I found the explanation by "ryles" on his/her mail of 27th aug 2009, with title "Re: Q: multiprocessing.Queue size limitations or bug...". It is very clarifying. After having read that I arranged the program such that the main process did not need to know when the others have finished, so I changed the process join call with a queue get call, until a None (one per process) is returned. Best, David From rosuav at gmail.com Mon Dec 5 03:09:13 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 19:09:13 +1100 Subject: struct calcsize discrepency? In-Reply-To: References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> <4EDB88FD.9000108@davea.name> Message-ID: On Mon, Dec 5, 2011 at 6:42 PM, Mark Dickinson wrote: > That's a strange way to think of it, especially since the padding also > happens for a single struct object when there's no array present. ?I > find it cleaner to think of C as having no padding in arrays, but > padding at the end of a struct. ?See C99 6.7.2.1p15: 'There may be > unnamed padding at the end of a structure or union.' ?There's no > mention in the standard of padding for arrays. May be, yes, but since calcsize() is returning 12 when the elements are put in the other order, it would seem to be not counting such padding. The way I look at it, padding is always used to place the beginning of something; in an array, it places the beginning of the second element on a convenient boundary, rather than filling out the first element to that boundary. I tried a similar thing with a couple of C compilers, and both of them gave the same sizeof() value for both orderings, which would imply that they _do_ include such padding in the structure's end. My statement that the padding was removed was based solely on calcsize()'s different result. ChrisA From mdickinson at enthought.com Mon Dec 5 03:20:32 2011 From: mdickinson at enthought.com (Mark Dickinson) Date: Mon, 5 Dec 2011 00:20:32 -0800 (PST) Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> <4EDB88FD.9000108@davea.name> Message-ID: On Dec 5, 8:09?am, Chris Angelico wrote: > May be, yes, but since calcsize() is returning 12 when the elements > are put in the other order, it would seem to be not counting such > padding. Indeed. That's arguably a bug in the struct module, and one that people have had to find workarounds for in the past. See note 3 at: http://docs.python.org/library/struct.html#byte-order-size-and-alignment If it weren't for backwards compatibility issues, I'd say that this should be fixed. -- Mark From alec.taylor6 at gmail.com Mon Dec 5 03:20:43 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Mon, 5 Dec 2011 19:20:43 +1100 Subject: Can't install pycrypto Message-ID: Good afternoon, Unfortunately my pycrpto install fails (tried with pip, easy_install and pip -e git+) Error: http://pastebin.com/wjjfTZvd How do I get this working? Thanks for all suggestions, Alec Taylor From boB Mon Dec 5 03:57:01 2011 From: boB (boB) Date: 5 Dec 2011 02:57:01 -0600 Subject: 70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object References: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> Message-ID: On Mon, 5 Dec 2011 09:02:08 +0100, DPalao wrote: >El Martes Noviembre 29 2011, DPalao escribi?: >> Hello, >> I'm trying to use multiprocessing to parallelize a code. There is a number >> of tasks (usually 12) that can be run independently. Each task produces a >> numpy array, and at the end, those arrays must be combined. >> I implemented this using Queues (multiprocessing.Queue): one for input and >> another for output. >> But the code blocks. And it must be related to the size of the item I put >> on the Queue: if I put a small array, the code works well; if the array is >> realistically large (in my case if can vary from 160kB to 1MB), the code >> blocks apparently forever. >> I have tried this: >> http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-lar >> ge- objects/ >> but it didn't work (especifically I put a None sentinel at the end for each >> worker). >> >> Before I change the implementation, >> is there a way to bypass this problem with multiprocessing.Queue? >> Should I post the code (or a sketchy version of it)? >> >> TIA, >> >> David > >Just for reference. The other day I found the explanation by "ryles" on >his/her mail of 27th aug 2009, with title "Re: Q: multiprocessing.Queue size >limitations or bug...". It is very clarifying. >After having read that I arranged the program such that the main process did >not need to know when the others have finished, so I changed the process join >call with a queue get call, until a None (one per process) is returned. > >Best, > >David Why do people add character like [* SPAM *] to their subject lines ?? Is it supposed to do something ?? I figured since programmers hang out here, maybe one of you know this. Thanks, boB From rosuav at gmail.com Mon Dec 5 04:11:28 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 20:11:28 +1100 Subject: Spam in subject lines (Re: 70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object) Message-ID: On Mon, Dec 5, 2011 at 7:57 PM, wrote: > Why do people add character ?like ? ?[* SPAM *] ?to their ?subject > lines ?? ? Is it supposed to do something ??? ? I figured since > programmers hang out here, maybe one of you know this. People don't. It's something added by a spam filter that thought that the email was likely to be junk (in this case, a 70% probability thereof). If you're curious as to exactly _why_ the filter thought that, check the email headers - it's all there, in exhaustive detail. The purpose of the tag is to let recipients make some simple filter rules (eg "if it has SPAM in the subject, put it in a separate folder") and/or to be able to eyeball spam easily. Chris Angelico From steve+comp.lang.python at pearwood.info Mon Dec 5 04:41:06 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 09:41:06 GMT Subject: Spam in subject lines (Re: multiprocessing.Queue blocks when sending large object) References: Message-ID: <4edc91b2$0$11114$c3e8da3@news.astraweb.com> On Mon, 05 Dec 2011 20:11:28 +1100, Chris Angelico wrote: > On Mon, Dec 5, 2011 at 7:57 PM, wrote: >> Why do people add character ?like ? ?[* SPAM *] ?to their ?subject >> lines ?? ? Is it supposed to do something ??? ? I figured since >> programmers hang out here, maybe one of you know this. > > People don't. It's something added by a spam filter that thought that > the email was likely to be junk (in this case, a 70% probability > thereof). If you're curious as to exactly _why_ the filter thought that, > check the email headers - it's all there, in exhaustive detail. Hilariously, I have occasionally received spam where the spammer included SPAM in their subject line under the mistaken impression that this sign of honesty would make me more likely to read the body of the email. Actually, in hindsight, not so mistaken really... -- Steven From ss27051980 at gmail.com Mon Dec 5 06:18:12 2011 From: ss27051980 at gmail.com (Suresh Sharma) Date: Mon, 5 Dec 2011 16:48:12 +0530 Subject: Fwd: class print method... In-Reply-To: References: <4EDC775D.4000906@davea.name> Message-ID: Pls help its really frustrating ---------- Forwarded message ---------- From: Suresh Sharma Date: Monday, December 5, 2011 Subject: class print method... To: "d at davea.name" Dave, Thanx for the quick response, i am sorry that i did not explain correctly look at the code below inspite of this i am just getting class object at memory location.I am sort i typed all this code on my android in a hurry so.indentation could.not.be.managed but this.similar code when i run all my objects created by class deck are not shown but stored in varioia meory locations. How can i display them. Please help Suit=[aces,clubs,diamonds,hearts] Rank=[2,3,4,5,6,7,8,9,j,Q,K,A] class Card: Def __init__(self,rannk,suiit): Self.suiit=suiit Self.rannk=rannk Def __str__(self): Return suit[suiit],rank[rannk] Class deck: Def __init__(cards): Self.cards=[ ] For suit in range(4): For rank in range(13): Self.cards.append(Card(suit,rank)) Def __str__(self): s = "" For card in self.cards: S = str(self.cards) Return s. On Monday, December 5, 2011, Dave Angel wrote: > On 12/05/2011 02:11 AM, Suresh Sharma wrote: >> >> Hello All, >> I am new to python and i have stuck up on a particular issue with classes, >> i understand this might be a very dumb question but please help me out. >> >> I have created two classes and whenever i try to print the objects i get >> this message but not the data, __main__.cards instance at ****(memory >> location) i even tried using __str__ but calling it also produces the same >> result. Can anyone please help me how to get my objects printed. I googled >> a lot but could not find anything relevant. >> >> thanks in advance >> >> regards >> suresh >> > You were close, but you have it backward. You don't call __str__() to print an object, you implement __str__() in your object. > > If you write a class without also writing __str__(), then print won't know what to do with it. > > -- > > DaveA > > -- Suresh Sharma Regional Project Manager, O2F,Mumbai Maharashtra-400101. -- Suresh Sharma Regional Project Manager, O2F,Mumbai Maharashtra-400101. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon Dec 5 06:35:51 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 5 Dec 2011 22:35:51 +1100 Subject: class print method... In-Reply-To: References: <4EDC775D.4000906@davea.name> Message-ID: On Mon, Dec 5, 2011 at 10:18 PM, Suresh Sharma wrote: > > Suit=[aces,clubs,diamonds,hearts] > Rank=[2,3,4,5,6,7,8,9,j,Q,K,A] > class Card: > Def __init__(self,rannk,suiit): > Self.suiit=suiit > Self.rannk=rannk > > Def __str__(self): > Return suit[suiit],rank[rannk] > > Class deck: > Def __init__(cards): > Self.cards=[ ] > For suit in range(4): > For rank in range(13): > Self.cards.append(Card(suit,rank)) > > Def __str__(self): > s = "" > For card in self.cards: > S = str(self.cards) > Return s. Python is case sensitive, and indentation is significant. Paste your code exactly as you're trying to run it, and we'll be better able to help you. Chris Angelico From dihedral88888 at googlemail.com Mon Dec 5 07:09:34 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 5 Dec 2011 04:09:34 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <952995.674.1323086974877.JavaMail.geo-discussion-forums@preu18> On Monday, December 5, 2011 1:50:08 PM UTC+8, Dan Stromberg wrote: > Two methods: > 1) If you need your hash only once in an infrequent while, then save > the elements in a list, appending as needed, and sort prior to > hashing, as needed > > 2) If you need your hash more often, you could keep your elements in a > treap or red-black tree; these will maintain sortedness throughout the > life of the datastructure. > > 3) If A bunch of log(n) or n or nlog(n) operations doesn't sound > appealing, then you might try this one: Create some sort of mapping > from your elements to the integers. Then just use a sum. This won't > scatter things nearly as well as a cryptographic hash, but it's very > fast, because you don't need to reevaluate some of your members as you > go. > > HTH > A sorted list can behave like a hash table. This is of O(log(n)) in accesses of n items in theory. I agree with you a hash key computation method too slow than a list of n items in accesses for a range of n items should be reloadable. But this is not supported in Python yet. For tedious trivial jobs of non-heavy computing , I'll opt for easy use. From dihedral88888 at googlemail.com Mon Dec 5 07:09:34 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 5 Dec 2011 04:09:34 -0800 (PST) Subject: order independent hash? In-Reply-To: References: Message-ID: <952995.674.1323086974877.JavaMail.geo-discussion-forums@preu18> On Monday, December 5, 2011 1:50:08 PM UTC+8, Dan Stromberg wrote: > Two methods: > 1) If you need your hash only once in an infrequent while, then save > the elements in a list, appending as needed, and sort prior to > hashing, as needed > > 2) If you need your hash more often, you could keep your elements in a > treap or red-black tree; these will maintain sortedness throughout the > life of the datastructure. > > 3) If A bunch of log(n) or n or nlog(n) operations doesn't sound > appealing, then you might try this one: Create some sort of mapping > from your elements to the integers. Then just use a sum. This won't > scatter things nearly as well as a cryptographic hash, but it's very > fast, because you don't need to reevaluate some of your members as you > go. > > HTH > A sorted list can behave like a hash table. This is of O(log(n)) in accesses of n items in theory. I agree with you a hash key computation method too slow than a list of n items in accesses for a range of n items should be reloadable. But this is not supported in Python yet. For tedious trivial jobs of non-heavy computing , I'll opt for easy use. From lie.1296 at gmail.com Mon Dec 5 07:27:59 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 23:27:59 +1100 Subject: 70% [* SPAM *] multiprocessing.Queue blocks when sending large object In-Reply-To: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> References: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> Message-ID: On 11/30/2011 06:09 AM, DPalao wrote: > Hello, > I'm trying to use multiprocessing to parallelize a code. There is a number of > tasks (usually 12) that can be run independently. Each task produces a numpy > array, and at the end, those arrays must be combined. > I implemented this using Queues (multiprocessing.Queue): one for input and > another for output. > But the code blocks. And it must be related to the size of the item I put on > the Queue: if I put a small array, the code works well; if the array is > realistically large (in my case if can vary from 160kB to 1MB), the code > blocks apparently forever. > I have tried this: > http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-large- > objects/ > but it didn't work (especifically I put a None sentinel at the end for each > worker). > > Before I change the implementation, > is there a way to bypass this problem with multiprocessing.Queue? > Should I post the code (or a sketchy version of it)? Transferring data over multiprocessing.Queue involves copying the whole object across an inter-process pipe, so you need to have a reasonably large workload in the processes to justify the cost of the copying to benefit from running the workload in parallel. You may try to avoid the cost of copying by using shared memory (http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes); you can use Queue for communicating when a new data comes in or when a task is done, but put the large data in shared memory. Be careful not to access the data from multiple processes concurrently. In any case, have you tried a multithreaded solution? numpy is a C extension, and I believe it releases the GIL when working, so it wouldn't be in your way to achieve parallelism. From lie.1296 at gmail.com Mon Dec 5 07:41:25 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 23:41:25 +1100 Subject: Fwd: class print method... In-Reply-To: References: <4EDC775D.4000906@davea.name> Message-ID: On 12/05/2011 10:18 PM, Suresh Sharma wrote: > > Pls help its really frustrating > ---------- Forwarded message ---------- > From: Suresh Sharma > Date: Monday, December 5, 2011 > Subject: class print method... > To: "d at davea.name " > > > > Dave, > Thanx for the quick response, i am sorry that i did not explain > correctly look at the code below inspite of this i am just getting class > object at memory location.I am sort i typed all this code on my android > in a hurry so.indentation could.not.be.managed but this.similar code > when i run all my objects created by class deck are not shown but stored > in varioia meory locations. How can i display them. > I think you're in the right track, however I suspect you're running the code in the shell instead of as a script. The shell uses __repr__() to print objects instead of __str__(), so you either need to use 'print' or you need to call str(), note the following: Python 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> suits = ['spades', 'clubs', 'diamonds', 'hearts'] >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] >>> class Card: ... def __init__(self, rank, suit): ... self.suit = suit ... self.rank = rank ... def __str__(self): ... return suits[self.suit] + ' ' + ranks[self.rank] ... >>> Card(2, 3) #1 <__main__.Card instance at 0x7f719c3a20e0> >>> str(Card(2, 3)) #2 of your 'hearts 3' >>> print Card(2, 3) #3 hearts 3 In #1, the output is the __repr__() of your Card class; you can modify this output by overriding the __repr__() on your Card class. In #2, the output is the __repr__() of a string, the string is the return value from __str__() of your Card class. The repr of a string is the string enclosed in quotes, which is why there is an extra pair of quotes. In #3, you're 'print'-ing a string, the string is the return value from __str__() of your Card class. There's no extra quotes, since 'print' prints the string itself, not the repr of the string. From lie.1296 at gmail.com Mon Dec 5 07:52:37 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 05 Dec 2011 23:52:37 +1100 Subject: Install Python on Windows without Start Menu icons? In-Reply-To: References: Message-ID: On 12/05/2011 07:01 PM, Wolfgang Strobl wrote: > "Pedro Henrique G. Souto": > >> On 02/12/2011 16:34, snorble wrote: >>> Is it possible to automate the Python installation on Windows using >>> the MSI file so it does not add a Start Menu folder? I would like to >>> push out Python to all of my office workstations, but I'd like for it >>> to be relatively silent from the user's point of view. >> >> If you just want to run python scripts in those machines (not developing >> in it), you can use something like py2exe [http://www.py2exe.org/]. > > That doesn't answer the question. But it may solve his problem, which is the whole point. Often in discussions, an OP may ask the wrong question that does not solve their problem or is a roundabout way to solve their problem (and it may not necessarily be their fault that they asked the wrong question). Which is why it is often best to describe the actual problem in addition to asking a specific question. From d at davea.name Mon Dec 5 08:09:49 2011 From: d at davea.name (Dave Angel) Date: Mon, 05 Dec 2011 08:09:49 -0500 Subject: Fwd: class print method... In-Reply-To: References: <4EDC775D.4000906@davea.name> Message-ID: <4EDCC29D.7010903@davea.name> On 12/05/2011 07:41 AM, Lie Ryan wrote: > On 12/05/2011 10:18 PM, Suresh Sharma wrote: >> >> Pls help its really frustrating >> ---------- Forwarded message ---------- >> From: Suresh Sharma >> Date: Monday, December 5, 2011 >> Subject: class print method... >> To: "d at davea.name " > > >> >> >> Dave, >> Thanx for the quick response, i am sorry that i did not explain >> correctly look at the code below inspite of this i am just getting class >> object at memory location.I am sort i typed all this code on my android >> in a hurry so.indentation could.not.be.managed but this.similar code >> when i run all my objects created by class deck are not shown but stored >> in varioia meory locations. How can i display them. >> > > I think you're in the right track, however I suspect you're running > the code in the shell instead of as a script. The shell uses > __repr__() to print objects instead of __str__(), so you either need > to use 'print' or you need to call str(), note the following: > > Python 2.7.2+ (default, Oct 4 2011, 20:06:09) > [GCC 4.6.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> suits = ['spades', 'clubs', 'diamonds', 'hearts'] > >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', > 'Q', 'K'] > >>> class Card: > ... def __init__(self, rank, suit): > ... self.suit = suit > ... self.rank = rank > ... def __str__(self): > ... return suits[self.suit] + ' ' + ranks[self.rank] > ... > >>> Card(2, 3) #1 > <__main__.Card instance at 0x7f719c3a20e0> > >>> str(Card(2, 3)) #2 of your > 'hearts 3' > >>> print Card(2, 3) #3 > hearts 3 > > In #1, the output is the __repr__() of your Card class; you can modify > this output by overriding the __repr__() on your Card class. > > In #2, the output is the __repr__() of a string, the string is the > return value from __str__() of your Card class. The repr of a string > is the string enclosed in quotes, which is why there is an extra pair > of quotes. > > In #3, you're 'print'-ing a string, the string is the return value > from __str__() of your Card class. There's no extra quotes, since > 'print' prints the string itself, not the repr of the string. > Lie is most likely correct. When I saw your message, i saw many bugs, but didn't know which were caused by your retyping, and which were actually there. I also saw no mainline. While i was deciding how to respond, Lie came up with his answer. Notice that he fixed lots of your bugs, and explains the distinction of print, as you'd use in your program, and the output of the debugger (or some IDEs. There are other places where defining a __str__() can be useful, such as if you print a list. The __str__() method of list calls the __repr__() for each object inside it. So it can be useful to define both, even if you decide to have them do the same thing. Lie didn't include your Deck class, which has more problems. If you really want print to work on the whole Deck, you're going to have to build a single string for the whole thing. Currently you just return the name of the first card. Instead of return inside the loop, you'll have to construct a string (perhaps by concatenating a new value each time through), and put the return after the loop. Notice also that you top-posted, which is the wrong place for your comments. Please put your comments after whatever part you're quoting from other messages, as Lie and I both demonstrate. -- DaveA From bex.lewis at gmail.com Mon Dec 5 08:38:45 2011 From: bex.lewis at gmail.com (becky_lewis) Date: Mon, 5 Dec 2011 05:38:45 -0800 (PST) Subject: Can't install pycrypto References: Message-ID: <832e164a-d1ab-4be2-ad82-df951a9328fe@n10g2000vbg.googlegroups.com> My best guess from the error and a quick look at the setup.py file is that during setup "chmod 0755 configure" is trying to run but failing. I'm guessing that you're on windows and don't actually have chmod, hence the error. The project on github looks active so you could go and ask for a windows friendly setup.py file and install using: python setup.py install or you could attempt to edit it yourself. I'd suggest asking for a windows friendly file on github as they will either give you a reason for it not being possible or will make the fix and everybody can benefit :) Hope that helps, Becky Lewis On Dec 5, 8:20?am, Alec Taylor wrote: > Good afternoon, > > Unfortunately my pycrpto install fails (tried with pip, easy_install > and pip -e git+) > > Error:http://pastebin.com/wjjfTZvd > > How do I get this working? > > Thanks for all suggestions, > > Alec Taylor From patrickwayodi at gmail.com Mon Dec 5 09:53:28 2011 From: patrickwayodi at gmail.com (patrickwayodi) Date: Mon, 5 Dec 2011 06:53:28 -0800 (PST) Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) Message-ID: Hi, How can I install Python on Debian GNU/Linux? I downloaded the file "Python-2.7.2.tar.bz2" but I don't know how to install it. Patrick. From rosuav at gmail.com Mon Dec 5 10:08:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 02:08:55 +1100 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) In-Reply-To: References: Message-ID: On Tue, Dec 6, 2011 at 1:53 AM, patrickwayodi wrote: > Hi, > How can I install Python on Debian GNU/Linux? I downloaded the file > "Python-2.7.2.tar.bz2" but I don't know how to install it. You should actually already have Python installed. Try typing 'python' at a terminal and see if it invokes the interactive interpreter. ChrisA From brian at python.org Mon Dec 5 10:11:32 2011 From: brian at python.org (Brian Curtin) Date: Mon, 5 Dec 2011 09:11:32 -0600 Subject: Install Python on Windows without Start Menu icons? In-Reply-To: References: Message-ID: On Fri, Dec 2, 2011 at 12:34, snorble wrote: > Is it possible to automate the Python installation on Windows using > the MSI file so it does not add a Start Menu folder? I would like to > push out Python to all of my office workstations, but I'd like for it > to be relatively silent from the user's point of view. > -- > http://mail.python.org/mailman/listinfo/python-list > http://www.python.org/getit/releases/2.4/msi/ outlines some of the MSI command line features, but I don't see the option to not add the Start Menu folder. -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrickwayodi at gmail.com Mon Dec 5 10:40:34 2011 From: patrickwayodi at gmail.com (patrickwayodi) Date: Mon, 5 Dec 2011 07:40:34 -0800 (PST) Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) References: Message-ID: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> > You should actually already have Python installed. Try typing 'python' > at a terminal and see if it invokes the interactive interpreter. > > ChrisA Yes, I have Python installed, but it's an old version. So I want to upgrade to "Python-2.7.2.tar.bz2". From rosuav at gmail.com Mon Dec 5 10:51:21 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 02:51:21 +1100 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) In-Reply-To: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> Message-ID: On Tue, Dec 6, 2011 at 2:40 AM, patrickwayodi wrote: >> You should actually already have Python installed. Try typing 'python' >> at a terminal and see if it invokes the interactive interpreter. >> >> ChrisA > > > Yes, I have Python installed, but it's an old version. So I want to > upgrade to "Python-2.7.2.tar.bz2". Ah gotcha. I believe you can 'sudo apt-get install python2.7' - at least, you can on the Ubuntu system next to me. Not sure though; I build my Python from source straight from Mercurial. What you have there, I think, is a source code snapshot. You'd need to extract it and then do the usual incantation: $ ./configure $ make $ sudo make install If you don't have the compiler/build environment set up, you'll have to do that first. If you aren't too concerned about the exact version you get, the above apt-get line should get you a stable Python in the 2.7 branch. ChrisA From steve+comp.lang.python at pearwood.info Mon Dec 5 11:09:43 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 16:09:43 GMT Subject: Backspace does not erase in stdout Message-ID: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> I have a function which reads characters from stdin and writes stars to stdout, but backspacing does not erase the stars as I expected. Tested in Python 2.6 on Linux. This will almost certainly not work on Windows. import sys, tty, termios def getpass(): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) chars = [] try: tty.setraw(sys.stdin.fileno()) while 1: c = sys.stdin.read(1) if c in '\n\r': # Enter or Return key. break elif c == '\x7f': # Backspace key. if chars: # Rubout previous character. sys.stdout.write('\b') sys.stdout.flush() del chars[-1] else: # Obfuscate the letter typed. sys.stdout.write('*') chars.append(c) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) sys.stdout.write('\n') return ''.join(chars) When I call this function and then type, I get a serious of asterisks as expected. Each time I press the backspace key, the cursor moves one character to the left, but the asterisks remain visible. Is there a way to erase the character other than backspacing, writing a space, then backspacing again? That feels inelegant. -- Steven From rosuav at gmail.com Mon Dec 5 11:13:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 03:13:41 +1100 Subject: Backspace does not erase in stdout In-Reply-To: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano wrote: > > Is there a way to erase the character other than backspacing, writing a > space, then backspacing again? That feels inelegant. Emitting "\b \b" is one very common way to do a destructive backspace. Inelegant? Perhaps, but a common inelegance. ChrisA From steve+comp.lang.python at pearwood.info Mon Dec 5 11:23:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 16:23:21 GMT Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> Message-ID: <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Dec 2011 02:51:21 +1100, Chris Angelico wrote: > On Tue, Dec 6, 2011 at 2:40 AM, patrickwayodi > wrote: >>> You should actually already have Python installed. Try typing 'python' >>> at a terminal and see if it invokes the interactive interpreter. >>> >>> ChrisA >> >> >> Yes, I have Python installed, but it's an old version. So I want to >> upgrade to "Python-2.7.2.tar.bz2". > > Ah gotcha. I believe you can 'sudo apt-get install python2.7' - at > least, you can on the Ubuntu system next to me. Not sure though; I build > my Python from source straight from Mercurial. > > What you have there, I think, is a source code snapshot. You'd need to > extract it and then do the usual incantation: $ ./configure > $ make > $ sudo make install And you have now potentially broken your system python :( Generally speaking, unless you are an expert, you should not use make install when installing Python from source, because it will replace the system Python with the newly installed one. Instead use `sudo make altinstall`. This is exactly the same as install, except it won't replace the python symlink that points to the actual Python executable. That way system tools that call Python get the version they are expecting, together with any libraries installed for their use, while you can call the version you prefer manually. Or set up an alias in your bashrc file. > If you don't have the compiler/build environment set up, you'll have to > do that first. The trickiest part for me is ensuring that tkinter works correctly. After installing Python from source about a dozen times now, I still don't know why sometimes it works and sometimes it doesn't. -- Steven From invalid at invalid.invalid Mon Dec 5 11:23:55 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 5 Dec 2011 16:23:55 +0000 (UTC) Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-12-05, Chris Angelico wrote: > On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano > wrote: >> >> Is there a way to erase the character other than backspacing, writing a >> space, then backspacing again? That feels inelegant. > > Emitting "\b \b" is one very common way to do a destructive backspace. > Inelegant? Perhaps, but a common inelegance. That's pretty much the only way I've seen it done for the past 25 years. What would be more elegant? -- Grant Edwards grant.b.edwards Yow! This PIZZA symbolizes at my COMPLETE EMOTIONAL gmail.com RECOVERY!! From rosuav at gmail.com Mon Dec 5 11:28:27 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 03:28:27 +1100 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) In-Reply-To: <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Dec 6, 2011 at 3:23 AM, Steven D'Aprano wrote: > Generally speaking, unless you are an expert, you should not use make > install when installing Python from source, because it will replace the > system Python with the newly installed one. > > Instead use `sudo make altinstall` Mea culpa, forgot that. Yes, use altinstall. Although it's probably not a problem to replace 2.6.6 with 2.7.2 - I doubt that'll break many things. ChrisA From lists at cheimes.de Mon Dec 5 11:37:08 2011 From: lists at cheimes.de (Christian Heimes) Date: Mon, 05 Dec 2011 17:37:08 +0100 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) In-Reply-To: <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 05.12.2011 17:23, schrieb Steven D'Aprano: > The trickiest part for me is ensuring that tkinter works correctly. After > installing Python from source about a dozen times now, I still don't know > why sometimes it works and sometimes it doesn't. sudo apt-get build-dep python2.7 Done ;) However there are additional pitfalls if you have multiarch support or Kernel 3.x. Both are well documented in my blog http://lipyrary.blogspot.com/ Christian From lists at cheimes.de Mon Dec 5 11:39:40 2011 From: lists at cheimes.de (Christian Heimes) Date: Mon, 05 Dec 2011 17:39:40 +0100 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) In-Reply-To: References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 05.12.2011 17:28, schrieb Chris Angelico: > On Tue, Dec 6, 2011 at 3:23 AM, Steven D'Aprano > wrote: >> Generally speaking, unless you are an expert, you should not use make >> install when installing Python from source, because it will replace the >> system Python with the newly installed one. >> >> Instead use `sudo make altinstall` > > Mea culpa, forgot that. Yes, use altinstall. Although it's probably > not a problem to replace 2.6.6 with 2.7.2 - I doubt that'll break many > things. Except that all 3rd party extensions and packages are missing if you install Python manually. Unless you *really* know what you are doing you shouldn't install Python manually. Debian's backports should provide a well integrated Python 2.7 package. Christian From roy at panix.com Mon Dec 5 12:04:52 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 09:04:52 -0800 (PST) Subject: Scope of variable inside list comprehensions? Message-ID: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Consider the following django snippet. Song(id) raises DoesNotExist if the id is unknown. try: songs = [Song(id) for id in song_ids] except Song.DoesNotExist: print "unknown song id (%d)" % id Is id guaranteed to be in scope in the print statement? I found one thread (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) which says yes, but hints that it might not always be in the future. Now that we're in the future, is that still true? And for Python 3 also? The current docs, http://docs.python.org/tutorial/datastructures.html#list-comprehensions, are mute on this point. From steve+comp.lang.python at pearwood.info Mon Dec 5 12:24:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 17:24:59 GMT Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4edcfe6b$0$29988$c3e8da3$5496439d@news.astraweb.com> On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > On 2011-12-05, Chris Angelico wrote: >> On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano >> wrote: >>> >>> Is there a way to erase the character other than backspacing, writing >>> a space, then backspacing again? That feels inelegant. >> >> Emitting "\b \b" is one very common way to do a destructive backspace. >> Inelegant? Perhaps, but a common inelegance. > > That's pretty much the only way I've seen it done for the past 25 years. > > What would be more elegant? For backspace to actually backspace, and not just move the cursor. Thanks for those who answered, I guess I'll just do the backspace, space, backspace dance. -- Steven From invalid at invalid.invalid Mon Dec 5 12:40:38 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Mon, 5 Dec 2011 17:40:38 +0000 (UTC) Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> <4edcfe6b$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-12-05, Steven D'Aprano wrote: > On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > >> On 2011-12-05, Chris Angelico wrote: >>> On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano >>> wrote: >>>> >>>> Is there a way to erase the character other than backspacing, writing >>>> a space, then backspacing again? That feels inelegant. >>> >>> Emitting "\b \b" is one very common way to do a destructive backspace. >>> Inelegant? Perhaps, but a common inelegance. >> >> That's pretty much the only way I've seen it done for the past 25 years. >> >> What would be more elegant? > > For backspace to actually backspace, and not just move the cursor. Ah, but "just move the cursor" is what backspace has always meant. It's been that way for 100 years -- since the days of typewriters and teletypes. ;) > Thanks for those who answered, I guess I'll just do the backspace, > space, backspace dance. After thinking a while, I do remember one program I ran across recently that when you hit backspace would erase the entire line, then rewrite the entire line stopping one character short of where it was before. Even at network speeds it was noticable when the link was encrypted. -- Grant Edwards grant.b.edwards Yow! Inside, I'm already at SOBBING! gmail.com From dpalao.python at gmail.com Mon Dec 5 12:49:44 2011 From: dpalao.python at gmail.com (DPalao) Date: Mon, 5 Dec 2011 18:49:44 +0100 Subject: 70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when sending large object In-Reply-To: References: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> Message-ID: <31727_1323107396_pB5Hno58011636_201112051849.44264.dpalao.python@gmail.com> El Lunes Diciembre 5 2011, boB at mail.python.org escribi?: > On Mon, 5 Dec 2011 09:02:08 +0100, DPalao > > wrote: > >El Martes Noviembre 29 2011, DPalao escribi?: > >> Hello, > >> I'm trying to use multiprocessing to parallelize a code. There is a > >> number of tasks (usually 12) that can be run independently. Each task > >> produces a numpy array, and at the end, those arrays must be combined. > >> I implemented this using Queues (multiprocessing.Queue): one for input > >> and another for output. > >> But the code blocks. And it must be related to the size of the item I > >> put on the Queue: if I put a small array, the code works well; if the > >> array is realistically large (in my case if can vary from 160kB to > >> 1MB), the code blocks apparently forever. > >> I have tried this: > >> http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and- > >> lar ge- objects/ > >> but it didn't work (especifically I put a None sentinel at the end for > >> each worker). > >> > >> Before I change the implementation, > >> is there a way to bypass this problem with multiprocessing.Queue? > >> Should I post the code (or a sketchy version of it)? > >> > >> TIA, > >> > >> David > > > >Just for reference. The other day I found the explanation by "ryles" on > >his/her mail of 27th aug 2009, with title "Re: Q: multiprocessing.Queue > >size limitations or bug...". It is very clarifying. > >After having read that I arranged the program such that the main process > >did not need to know when the others have finished, so I changed the > >process join call with a queue get call, until a None (one per process) > >is returned. > > > >Best, > > > >David > > Why do people add character like [* SPAM *] to their subject > lines ?? Is it supposed to do something ?? I figured since > programmers hang out here, maybe one of you know this. > > Thanks, > boB Obviously it was not me who added the disgusting "70% [* SPAM *]" string to the subject. And I'd like to know the answer too. David From jpiitula at ling.helsinki.fi Mon Dec 5 13:04:04 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 05 Dec 2011 20:04:04 +0200 Subject: Scope of variable inside list comprehensions? References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: Roy Smith writes: > Consider the following django snippet. Song(id) raises DoesNotExist > if the id is unknown. > > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id > > Is id guaranteed to be in scope in the print statement? I found one > thread > (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) > which says yes, but hints that it might not always be in the future. > Now that we're in the future, is that still true? And for Python 3 > also? Another id is in scope in this Python3 example (3.1.2, Ubuntu): >>> try: ... songs = [1/0 for id in [1]] ... except Exception: ... print('Caught', id) ... Caught From ckaynor at zindagigames.com Mon Dec 5 13:04:36 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Mon, 5 Dec 2011 10:04:36 -0800 Subject: Scope of variable inside list comprehensions? In-Reply-To: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: On Mon, Dec 5, 2011 at 9:04 AM, Roy Smith wrote: > Consider the following django snippet. Song(id) raises DoesNotExist if > the id is unknown. > > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id > > Is id guaranteed to be in scope in the print statement? I found one > thread ( > http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) > which says yes, but hints that it might not always be in the future. Now > that we're in the future, is that still true? And for Python 3 also? > In Python2, id will always be in scope, unless the first iteration fails (aka, an empty iterable, or a custom iterable which raises an exception before yielding a result). I believe in Python3, the scoping of list comprehensions was changed to match that of generator expressions, which is that the loop variable(s) fall out of scope outside of the expression. > > The current docs, > http://docs.python.org/tutorial/datastructures.html#list-comprehensions, > are mute on this point. > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Mon Dec 5 13:10:08 2011 From: __peter__ at web.de (Peter Otten) Date: Mon, 05 Dec 2011 19:10:08 +0100 Subject: Scope of variable inside list comprehensions? References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: Roy Smith wrote: > Consider the following django snippet. Song(id) raises DoesNotExist if > the id is unknown. > > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id > > Is id guaranteed to be in scope in the print statement? I found one > thread > (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) > which says yes, but hints that it might not always be in the future. Now > that we're in the future, is that still true? And for Python 3 also? > > The current docs, > http://docs.python.org/tutorial/datastructures.html#list-comprehensions, > are mute on this point. If you are using a generator expression id will already be out of scope in Python 2. In Python 3 list comprehensions have been changed to work the same way: $ cat song.py class DoesNotExist(Exception): pass class Song: def __init__(self, id): if id == 2: raise DoesNotExist ids = [1, 2] try: songs = [Song(i) for i in ids] except DoesNotExist as e: print("song #%d does not exist" % i) $ python song.py song #2 does not exist $ python3 song.py Traceback (most recent call last): File "song.py", line 11, in songs = [Song(i) for i in ids] File "song.py", line 11, in songs = [Song(i) for i in ids] File "song.py", line 7, in __init__ raise DoesNotExist __main__.DoesNotExist During handling of the above exception, another exception occurred: Traceback (most recent call last): File "song.py", line 13, in print("song #%d does not exist" % i) NameError: name 'i' is not defined $ $ cat song_gen.py class DoesNotExist(Exception): pass class Song: def __init__(self, id): if id == 2: raise DoesNotExist ids = [1, 2] try: songs = list(Song(i) for i in ids) except DoesNotExist as e: print("song #%d does not exist" % i) $ python song_gen.py Traceback (most recent call last): File "song_gen.py", line 13, in print("song #%d does not exist" % i) NameError: name 'i' is not defined So you'd rather store the id in the exception. From patrickwayodi at gmail.com Mon Dec 5 13:16:30 2011 From: patrickwayodi at gmail.com (Patrick Wayodi) Date: Mon, 5 Dec 2011 21:16:30 +0300 Subject: Pythonbrew is not installing Python 2.7 successfully Message-ID: Hi, I'm trying to install Python 2.7 using Pythonbrew on a Debian GNU/Linux system, but I've failed. This is the output I got on my terminal: root at debian:~# pythonbrew install /home/guest/python27 Copying /home/guest/python27 into /root/.pythonbrew/build/Python-python27 This could take a while. You can run the following command on another shell to track the status: tail -f /root/.pythonbrew/log/build.log Installing Python-python27 into /root/.pythonbrew/pythons/Python-python27 ERROR: Failed to install Python-python27. See /root/.pythonbrew/log/build.log to see why. pythonbrew install --force python27 root at debian:~# The log file build.log was empty. Please help me. Patrick. From dpalao.python at gmail.com Mon Dec 5 13:28:22 2011 From: dpalao.python at gmail.com (DPalao) Date: Mon, 5 Dec 2011 19:28:22 +0100 Subject: 70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object In-Reply-To: References: <23330_1322593803_pATJ9jRv011464_201111292009.44527.dpalao.python@gmail.com> Message-ID: <31719_1323109709_pB5ISMgK014037_201112051928.22417.dpalao.python@gmail.com> Hi Lie, Thank you for the reply. El Lunes Diciembre 5 2011, Lie Ryan escribi?: > On 11/30/2011 06:09 AM, DPalao wrote: > > Hello, > > I'm trying to use multiprocessing to parallelize a code. There is a > > number of tasks (usually 12) that can be run independently. Each task > > produces a numpy array, and at the end, those arrays must be combined. > > I implemented this using Queues (multiprocessing.Queue): one for input > > and another for output. > > But the code blocks. And it must be related to the size of the item I put > > on the Queue: if I put a small array, the code works well; if the array > > is realistically large (in my case if can vary from 160kB to 1MB), the > > code blocks apparently forever. > > I have tried this: > > http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-l > > arge- objects/ > > but it didn't work (especifically I put a None sentinel at the end for > > each worker). > > > > Before I change the implementation, > > is there a way to bypass this problem with multiprocessing.Queue? > > Should I post the code (or a sketchy version of it)? > > Transferring data over multiprocessing.Queue involves copying the whole > object across an inter-process pipe, so you need to have a reasonably > large workload in the processes to justify the cost of the copying to > benefit from running the workload in parallel. > > You may try to avoid the cost of copying by using shared memory > (http://docs.python.org/library/multiprocessing.html#sharing-state-between- > processes); you can use Queue for communicating when a new data comes in or > when a task is done, but put the large data in shared memory. Be careful > not to access the data from multiple processes concurrently. > Yep, that was my first thought, but the arrays's elements are complex64 (or complex in general), and I don't know how to easily convert from multiprocessing.Array to/from numpy.array when the type is complex. Doing that would require some extra conversions forth and back which make the solution not very attractive to me. I tried with a Manager too, but the array cannot be modified from within the worker processes. In principle, the array I need to share is expected to be, at most, ~2MB in size, and typically should be only <200kB. So, in principle, there is no huge extra workload. But that could change, and I'd like to be prepared for it, so any idea about using an Array or a Manager or another shared memory thing would be great. > In any case, have you tried a multithreaded solution? numpy is a C > extension, and I believe it releases the GIL when working, so it > wouldn't be in your way to achieve parallelism. That possibility I didn't know. What does exactly break the GIL? The sharing of a numpy array? What if I need to also share some other "standard" python data (eg, a dictionary)? Best regards, David From ursusmaximus at gmail.com Mon Dec 5 13:33:56 2011 From: ursusmaximus at gmail.com (Ron) Date: Mon, 5 Dec 2011 10:33:56 -0800 (PST) Subject: LinuxJournal Readers' Choice Awards 2011 Best {Programming, Scripting} Language References: Message-ID: <3ec3d1e0-e734-4fab-ad0a-d3b3f851cd12@y18g2000yqy.googlegroups.com> Hello Wesley, Thanks for the interesting news from Linux Journal. Now, enquring minds want to know, when will there be a Core Python 3? Ron :-) From jeanmichel at sequans.com Mon Dec 5 13:57:15 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 05 Dec 2011 19:57:15 +0100 Subject: Scope of variable inside list comprehensions? In-Reply-To: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: <4EDD140B.4060407@sequans.com> Roy Smith wrote: > Consider the following django snippet. Song(id) raises DoesNotExist if the id is unknown. > > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id > > Is id guaranteed to be in scope in the print statement? I found one thread (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) which says yes, but hints that it might not always be in the future. Now that we're in the future, is that still true? And for Python 3 also? > > The current docs, http://docs.python.org/tutorial/datastructures.html#list-comprehensions, are mute on this point. > For python 2, id will always be defined *as you meant it*. But you're doing something wrong : overiding the 'id' builtin function. With python 3 you will probably print the 'id' builtin function representation, which is correct but not want you want to achieve. The proper way to propagate information with exceptions is using the exception itself: try: songs = [Song(_id) for _id in song_ids] except Song.DoesNotExist, exc: print exc class DoesNotExist(Exception): def __init__(self, songId): self.songId = songId def __str__(self): return "Unkown Song Id %s" % self.songId class Song: def __init__(self, songId): if whatever: raise DoesNotExist(songId) self.id=songId JM From roy at panix.com Mon Dec 5 14:15:04 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 11:15:04 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> Hmmm, the use of id was just a simplification for the sake of posting. The real code is a bit more complicated and used a different variable name, but that's a good point. As far as storing the value in the exception, unfortunately, DoesNotExist is not my exception; it comes from deep within django. I'm just passing it along. From roy at panix.com Mon Dec 5 14:15:04 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 11:15:04 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> Hmmm, the use of id was just a simplification for the sake of posting. The real code is a bit more complicated and used a different variable name, but that's a good point. As far as storing the value in the exception, unfortunately, DoesNotExist is not my exception; it comes from deep within django. I'm just passing it along. From tjreedy at udel.edu Mon Dec 5 14:55:01 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 05 Dec 2011 14:55:01 -0500 Subject: Fwd: class print method... In-Reply-To: <4EDCC29D.7010903@davea.name> References: <4EDC775D.4000906@davea.name> <4EDCC29D.7010903@davea.name> Message-ID: On 12/5/2011 8:09 AM, Dave Angel wrote: > useful, such as if you print a list. The __str__() method of list calls > the __repr__() for each object inside it. So it can be useful to define > both, even if you decide to have them do the same thing. If you want __str__ and __repr__ to do the same thing, you only need *one* def statement and an assignment. def __str__(self): __repr__ = __str__ -- Terry Jan Reedy From storchaka at gmail.com Mon Dec 5 15:05:43 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Mon, 05 Dec 2011 22:05:43 +0200 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: 03.12.11 23:34, Arnaud Delobelle ???????(??): > Is there a simple way to achieve this? I could do something like this: > > def encode(u): > """encode a unicode string in .properties format""" > return u"".join(u"\\u%04x" % ord(c) if ord(c)> 0xFF else c for c > in u).encode("latin_1") You must also encode backslash ('\\'), whitespaces and control characters (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'. And don't forget characters with code >0xFFFF. From tjreedy at udel.edu Mon Dec 5 15:22:05 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 05 Dec 2011 15:22:05 -0500 Subject: Scope of variable inside list comprehensions? In-Reply-To: <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> Message-ID: On 12/5/2011 2:15 PM, Roy Smith wrote: > Hmmm, the use of id was just a simplification for the sake of > posting. The real code is a bit more complicated and used a > different variable name, but that's a good point. > > As far as storing the value in the exception, unfortunately, > DoesNotExist is not my exception; it comes from deep within django. > I'm just passing it along. It is hard to sensibly answer a question when the questioner *significantly* changes the problem in the process of 'simplifying' it. Both of those are significant changes ;-) Changing a name to a built-in name is a complexification, not a simplification, because it introduces new issues that were not in the original. Changing the exception from one you do not control to one you apparently do also changes the appropriate answer. If you do not control the exception and you want guaranteed access to the loop variable with Python 3 (and the upgrade of django to work with Python 3 is more or less done), then use an explicit loop. If you want the loop to continue after an error, instead of stopping, you can put the try/except within the loop, instead of without. This possibility is one advantage of using an explicit loop. songs = [] for song_id in song_ids: try: songs.append(Song(song_id)) except django.error: print("unknown song id {}".format(song_id)) -- Terry Jan Reedy From arnodel at gmail.com Mon Dec 5 15:25:39 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 5 Dec 2011 20:25:39 +0000 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: On 5 December 2011 20:05, Serhiy Storchaka wrote: > 03.12.11 23:34, Arnaud Delobelle ???????(??): > >> Is there a simple way to achieve this? I could do something like this: >> >> def encode(u): >> ? ? """encode a unicode string in .properties format""" >> ? ? return u"".join(u"\\u%04x" % ord(c) if ord(c)> ?0xFF else c for c >> in u).encode("latin_1") > > > You must also encode backslash ('\\'), whitespaces and control characters > (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'. Fortunately there aren't any of these in the keys. > And don't forget characters with code >0xFFFF. I haven't thought of these. I don't think that I have any, but I'll check. Do you know how they are encoded? -- Arnaud From snorble at hotmail.com Mon Dec 5 16:26:41 2011 From: snorble at hotmail.com (snorble) Date: Mon, 5 Dec 2011 13:26:41 -0800 (PST) Subject: Install Python on Windows without Start Menu icons? References: Message-ID: <63f25ebf-aa27-4a0b-b20f-84ccf9da7182@y12g2000vba.googlegroups.com> On Dec 5, 2:01?am, Wolfgang Strobl wrote: > "Pedro Henrique G. Souto" : > > >On 02/12/2011 16:34, snorble wrote: > >> Is it possible to automate the Python installation on Windows using > >> the MSI file so it does not add a Start Menu folder? I would like to > >> push out Python to all of my office workstations, but I'd like for it > >> to be relatively silent from the user's point of view. > > >If you just want to run python scripts in those machines (not developing > >in it), you can use something like py2exe [http://www.py2exe.org/]. > > That doesn't answer the question. > > Snorble might use ?"ORCA", a MSI editor from Microsoft. > > http://forums.frontmotion.com/viewtopic.php?f=10&t=837 > > discusses a similar question for Firefox. > > I haven't tried it myself, but would give it a try. > > -- > Thank you for observing all safety precautions Thank you! I got it working using Orca. Initially I had removed all of the rows from the Shortcut table and saved the .msi file, but the resulting .msi file was only about 600 KB (the original .msi file is about 15 MB). I had to create a .mst transform file, and that allows Python to install without any Start Menu entries. For reference, here's what I did. I opened the python-2.7.2.msi file in Orca, then click the Transform menu, then New Transform. Then go to the Shortcut table and highlight all of the entries and press the Delete key, and acknowledge that it will delete all of the rows. Then click the Transform menu and click Generate Transform and save it (python-2.7.2-no-icons.mst in my case). Once you have the .mst file, you can install it from the command line: msiexec /i python-2.7.2.msi TRANSFORMS=python-2.7.2-no-icons.mst Or to silently install: msiexec /qn /i python-2.7.2.msi TRANSFORMS=python-2.7.2-no-icons.mst From ralf at systemexit.de Mon Dec 5 16:44:57 2011 From: ralf at systemexit.de (Ralf Schmitt) Date: Mon, 05 Dec 2011 22:44:57 +0100 Subject: [ANNOUNCE] pypiserver 0.5.0 - minimal pypi server Message-ID: <87r50ivgkm.fsf@myhost.localnet> Hi, I've just uploaded pypiserver 0.5.0 to the python package index. pypiserver is a minimal PyPI compatible server. It can be used to serve a set of packages and eggs to easy_install or pip. pypiserver is easy to install (i.e. just easy_install pypiserver). It doesn't have any external dependencies. http://pypi.python.org/pypi/pypiserver/ should contain enough information to easily get you started running your own PyPI server in a few minutes. The code is available on github: https://github.com/schmir/pypiserver Changes in version 0.5.0 ------------------------- - make setup.py install without calling 2to3 by changing source code to be compatible with both python 2 and python 3. We now ship a slightly patched version of bottle. The upcoming bottle 0.11 also contains these changes. - make the single-file pypi-server-standalone.py work with python 3 -- Cheers, Ralf From storchaka at gmail.com Mon Dec 5 16:46:42 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Mon, 05 Dec 2011 23:46:42 +0200 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: 05.12.11 22:25, Arnaud Delobelle ???????(??): > On 5 December 2011 20:05, Serhiy Storchaka wrote: >> You must also encode backslash ('\\'), whitespaces and control characters >> (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'. > Fortunately there aren't any of these in the keys. "a=b" is same as "a= b". >> And don't forget characters with code>0xFFFF. > I haven't thought of these. I don't think that I have any, but I'll > check. Do you know how they are encoded? Java uses UTF-16. if i>=0x110000: raise some exception if i>=0x10000: return '\\u%04X\\u%04X' % (0xD800+((i-0x10000)>>10), 0xDC00+(i&0x3FF)) From rosuav at gmail.com Mon Dec 5 17:00:47 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 09:00:47 +1100 Subject: Backspace does not erase in stdout In-Reply-To: References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> <4edcfe6b$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Dec 6, 2011 at 4:40 AM, Grant Edwards wrote: > After thinking a while, I do remember one program I ran across > recently that when you hit backspace would erase the entire line, then > rewrite the entire line stopping one character short of where it was > before. ?Even at network speeds it was noticable when the link was > encrypted. I've seen a few programs that do that. Benefit is that it works even when (a) you're doing more than just "backspace one letter" (eg deleting an entire word), and (b) when your logical line wraps over multiple physical lines. It gets ugly fast, though. ChrisA From questions.anon at gmail.com Mon Dec 5 17:32:35 2011 From: questions.anon at gmail.com (questions anon) Date: Tue, 6 Dec 2011 09:32:35 +1100 Subject: loop through arrays and find maximum Message-ID: I would like to calculate the max and min across many netcdf files. I know how to create one big array and then concatenate and find the numpy.max but when I run this on 1000's of arrays I have a memory error. What I would prefer is to loop through the arrays and produce the maximum without having the make a big array. Does anyone have any ideas as to how I could achieve this? My idea goes something like: netCDF_list=[] maxarray=[] for dir in glob.glob(MainFolder + '*/01/')+ glob.glob(MainFolder + '*/02/')+ glob.glob(MainFolder + '*/12/'): for ncfile in glob.glob(dir + '*.nc'): netCDF_list.append(ncfile) for filename in netCDF_list: ncfile=netCDF4.Dataset( filename) TSFC=ncfile.variables['T_SFC'][:] fillvalue=ncfile.variables['T_SFC']._FillValue TSFC=MA.masked_values(TSFC, fillvalue) for i in TSFC: if i == N.max(TSFC, axis=0): maxarray.append(i) else: pass print maxarray -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Mon Dec 5 17:35:06 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2011 22:35:06 GMT Subject: Scope of variable inside list comprehensions? References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> On Mon, 05 Dec 2011 19:57:15 +0100, Jean-Michel Pichavant wrote: > The proper way to propagate information with exceptions is using the > exception itself: > > try: > songs = [Song(_id) for _id in song_ids] > except Song.DoesNotExist, exc: > print exc I'm not entirely sure that this is the proper way to propagate the exception. I see far to many people catching exceptions to print them, or worse, to print a generic, useless message like "an error occurred". The problem here is that having caught the exception, songs now does not exist, and will surely cause another, unexpected, exception in a moment or two when the code attempts to use it. Since the error is (apparently) unrecoverable, the right way as far as I can see is: songs = [Song(_id) for _id in song_ids] allowing any exception to be fatal and the traceback to be printed as normal. If the error is recoverable, you will likely need to do more to recover from it than merely print the exception and continue. -- Steven From roy at panix.com Mon Dec 5 17:36:48 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 14:36:48 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> Message-ID: <27498659.319.1323124608749.JavaMail.geo-discussion-forums@yqcv9> Well, in my defense, I did ask a pretty narrow question, "Is id guaranteed to be in scope in the print statement?". While I will admit that not knowing whether I could alter the exception, or whether id masked a builtin or not does complexify answering some questions, those are questions I didn't ask :-) From roy at panix.com Mon Dec 5 17:36:48 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 14:36:48 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> Message-ID: <27498659.319.1323124608749.JavaMail.geo-discussion-forums@yqcv9> Well, in my defense, I did ask a pretty narrow question, "Is id guaranteed to be in scope in the print statement?". While I will admit that not knowing whether I could alter the exception, or whether id masked a builtin or not does complexify answering some questions, those are questions I didn't ask :-) From arnodel at gmail.com Mon Dec 5 17:45:56 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 5 Dec 2011 22:45:56 +0000 Subject: How to generate java .properties files in python In-Reply-To: References: Message-ID: On 5 December 2011 21:46, Serhiy Storchaka wrote: > 05.12.11 22:25, Arnaud Delobelle ???????(??): >> On 5 December 2011 20:05, Serhiy Storchaka ?wrote: >>> You must also encode backslash ('\\'), whitespaces and control characters >>> (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'. >> Fortunately there aren't any of these in the keys. > > "a=b" is same as "a= b". You're right - all my values are trimmed for whitespace on both sides, so it's OK for me. Although if I wanted to make a general purpose tool I would have to take this into account. I might just escape all whitespace after all, for safety's sake. >>> And don't forget characters with code>0xFFFF. >> I haven't thought of these. ?I don't think that I have any, but I'll >> check. ?Do you know how they are encoded? > > Java uses UTF-16. > > if i>=0x110000: > ? ?raise some exception > if i>=0x10000: > ? ?return '\\u%04X\\u%04X' % (0xD800+((i-0x10000)>>10), 0xDC00+(i&0x3FF)) Thanks. I'm using .encode("latin1", "backslashreplace") now as suggested by Peter Otten. I can't see from the docs if this uses UTF-16 escape sequences. -- Arnaud From roy at panix.com Mon Dec 5 17:57:43 2011 From: roy at panix.com (Roy Smith) Date: Mon, 5 Dec 2011 14:57:43 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <14157454.1816.1323125863703.JavaMail.geo-discussion-forums@yqbz41> Sigh. I attempted to reduce this to a minimal example to focus the discussion on the question of list comprehension variable scope. Instead I seem to have gotten people off on other tangents. I suppose I should post more of the real code... song_ids = request.POST.getlist('song_id') try: songs = [Song.get(int(id)) for id in song_ids] except Song.DoesNotExist: return HttpResponseBadRequest("unknown song id (%d)" % id) I may be in the minority here, but it doesn't bother me much that my use of 'id' shadows a built-in. Especially in small scopes like this, I use whatever variable names make the the code easiest to read and don't worry about shadowing builtins. I don't have an exhaustive list of builtins in my head, so even if I worried about common ones like file or id, I'm sure I'd miss some others. So I don't sweat it. If shadowing builtins was really evil, they'd be reserved keywords and then you wouldn't be able to do it. From drsalists at gmail.com Mon Dec 5 18:05:26 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Mon, 5 Dec 2011 15:05:26 -0800 Subject: order independent hash? In-Reply-To: <952995.674.1323086974877.JavaMail.geo-discussion-forums@preu18> References: <952995.674.1323086974877.JavaMail.geo-discussion-forums@preu18> Message-ID: On 12/5/11, 88888 Dihedral wrote: > On Monday, December 5, 2011 1:50:08 PM UTC+8, Dan Stromberg wrote: >> Two methods: >> 1) If you need your hash only once in an infrequent while, then save >> the elements in a list, appending as needed, and sort prior to >> hashing, as needed >> >> 2) If you need your hash more often, you could keep your elements in a >> treap or red-black tree; these will maintain sortedness throughout the >> life of the datastructure. >> >> 3) If A bunch of log(n) or n or nlog(n) operations doesn't sound >> appealing, then you might try this one: Create some sort of mapping >> from your elements to the integers. Then just use a sum. This won't >> scatter things nearly as well as a cryptographic hash, but it's very >> fast, because you don't need to reevaluate some of your members as you >> go. >> >> HTH >> > A sorted list can behave like a hash table. This is of O(log(n)) in > accesses > of n items in theory. > > I agree with you a hash key computation method too slow than a list of n > items in accesses for a range of n items should be reloadable. > > But this is not supported in Python yet. > > For tedious trivial jobs of non-heavy computing , I'll opt for easy use. A sorted list is O(log(n)) for lookups, but O(n) for insertions. If you have a process doing both, the table operations are O(n). A hash table that isn't overfilled is O(1) for lookups, O(1) for insertions. But this is not ordered. Here's a straightforward treap implementation for python, with pure python and cython versions: http://pypi.python.org/pypi/treap/0.995 There's also at least one red-black tree implementation available. From tjreedy at udel.edu Mon Dec 5 21:23:17 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 05 Dec 2011 21:23:17 -0500 Subject: Scope of variable inside list comprehensions? In-Reply-To: <27498659.319.1323124608749.JavaMail.geo-discussion-forums@yqcv9> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <3206622.1235.1323112504669.JavaMail.geo-discussion-forums@yqiw17> <27498659.319.1323124608749.JavaMail.geo-discussion-forums@yqcv9> Message-ID: On 12/5/2011 5:36 PM, Roy Smith wrote: > Well, in my defense, I did ask a pretty narrow question, "Is id > guaranteed to be in scope in the print statement?". Yes for 2.x, guaranteed no for 3.x. If you had simply asked "Is the loop variable of a list comprehension guaranteed to be in scope after the list comprehension?", without a distracting example, that is the answer you would have received. I intend(ed) to inform, not attack, hence no 'defense' needed. > While I will > admit that not knowing whether I could alter the exception, or > whether id masked a builtin or not does complexify answering some > questions, those are questions I didn't ask :-) Except that it bears on the question you did ask because it means that the code will run in 3.x but with different results, whereas a random name will fail in 3.x with a NameError. -- Terry Jan Reedy From rosuav at gmail.com Mon Dec 5 21:35:45 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 13:35:45 +1100 Subject: Scope of variable inside list comprehensions? In-Reply-To: <14157454.1816.1323125863703.JavaMail.geo-discussion-forums@yqbz41> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> <14157454.1816.1323125863703.JavaMail.geo-discussion-forums@yqbz41> Message-ID: On Tue, Dec 6, 2011 at 9:57 AM, Roy Smith wrote: > I may be in the minority here, but it doesn't bother me much that my use of 'id' shadows a built-in. ?Especially in small scopes like this, I use whatever variable names make the the code easiest to read and don't worry about shadowing builtins. ?I don't have an exhaustive list of builtins in my head, so even if I worried about common ones like file or id, I'm sure I'd miss some others. ?So I don't sweat it. > > If shadowing builtins was really evil, they'd be reserved keywords and then you wouldn't be able to do it. Agreed. The name 'id' is one that's shadowed benignly in a lot of code. In the context of the original question, it's not an issue - there'll be no confusion. Python's scoping rules are "do what the programmer probably wants". This works most of the time, but occasionally you get edge cases where things are a bit weird, and C-style explicit scoping (with infinitely nested block scope) begins to look better. But for probably 99% of situations, Python's system "just works". If you need more flexibility in the exception you throw, it may be worth putting together a filter: def HttpSongId(id): try: return Song.get(int(id)) except Song.DoesNotExist: return HttpResponseBadRequest("unknown song id (%d)" % id) song_ids = request.POST.getlist('song_id') songs = [HttpSongId(id) for id in song_ids] This encapsulates things in a somewhat weird way, but if there's any other work to be done at the same time, you could probably come up with a better name for the exception filter function. ChrisA From xahlee at gmail.com Mon Dec 5 23:36:45 2011 From: xahlee at gmail.com (Xah Lee) Date: Mon, 5 Dec 2011 20:36:45 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> Message-ID: <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> On Dec 5, 4:31?am, Tim Bradshaw wrote: > On 2011-12-05 11:51:11 +0000, Xah Lee said: > > > python has more readible syntax, more modern computer language > > concepts, and more robust libraries. These qualities in turn made it > > popular. > > Yet you still post here: why? i don't like python, and i prefer emacs lisp. The primary reason is that python is not functional, especially with python 3. The python community is full of fanatics with their drivels. In that respect, it's not unlike Common Lisp community and Scheme lisp community. see also: ?Python Documentation Problems? http://xahlee.org/perl-python/python_doc_index.html ?Computer Language Design: What's List Comprehension and Why is It Harmful?? http://xahlee.org/comp/list_comprehension.html ?Lambda in Python 3000? http://xahlee.org/perl-python/python_3000.html ?What Languages to Hate? http://xahlee.org/UnixResource_dir/writ/language_to_hate.html ?Xah on Programing Languages? http://xahlee.org/Periodic_dosage_dir/comp_lang.html Xah From rosuav at gmail.com Tue Dec 6 00:10:27 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 16:10:27 +1100 Subject: Questions about LISP and Python. In-Reply-To: <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: On Tue, Dec 6, 2011 at 3:36 PM, Xah Lee wrote: > i don't like python, and i prefer emacs lisp. The primary reason is > that python is not functional, especially with python 3. The python > community is full of fanatics with their drivels. In that respect, > it's not unlike Common Lisp community and Scheme lisp community. So you hate Python. Fine. Why post here? Why not just abandon Python as a dead loss and go code in Lithp? Clearly something is keeping you here. Is it that there's something about Python that you really like, or are you just trolling? ChrisA From s.pasoev at gmail.com Tue Dec 6 00:49:55 2011 From: s.pasoev at gmail.com (Sergi Pasoev) Date: Tue, 6 Dec 2011 10:19:55 +0430 Subject: Single key press Message-ID: Hi. I wonder if it is realistic to get a single key press in Python without ncurses or any similar library. In single key press I mean something like j and k in Gnu less program, you press the key and and it is captured by the script without need to press enter afterwards From ss27051980 at gmail.com Tue Dec 6 00:53:16 2011 From: ss27051980 at gmail.com (Suresh Sharma) Date: Tue, 6 Dec 2011 11:23:16 +0530 Subject: Fwd: class print method... In-Reply-To: References: <4EDC775D.4000906@davea.name> Message-ID: Dave / Ryan Thanks i have got it and it worked after using repr statement. Thanks everyone for their valuable feedback. On Mon, Dec 5, 2011 at 6:11 PM, Lie Ryan wrote: > On 12/05/2011 10:18 PM, Suresh Sharma wrote: > >> >> Pls help its really frustrating >> ---------- Forwarded message ---------- >> From: Suresh Sharma >> Date: Monday, December 5, 2011 >> Subject: class print method... >> To: "d at davea.name " > > >> >> >> Dave, >> Thanx for the quick response, i am sorry that i did not explain >> correctly look at the code below inspite of this i am just getting class >> object at memory location.I am sort i typed all this code on my android >> in a hurry so.indentation could.not.be.managed but this.similar code >> when i run all my objects created by class deck are not shown but stored >> in varioia meory locations. How can i display them. >> >> > I think you're in the right track, however I suspect you're running the > code in the shell instead of as a script. The shell uses __repr__() to > print objects instead of __str__(), so you either need to use 'print' or > you need to call str(), note the following: > > Python 2.7.2+ (default, Oct 4 2011, 20:06:09) > [GCC 4.6.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> suits = ['spades', 'clubs', 'diamonds', 'hearts'] > >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', > 'K'] > >>> class Card: > ... def __init__(self, rank, suit): > ... self.suit = suit > ... self.rank = rank > ... def __str__(self): > ... return suits[self.suit] + ' ' + ranks[self.rank] > ... > >>> Card(2, 3) #1 > <__main__.Card instance at 0x7f719c3a20e0> > >>> str(Card(2, 3)) #2 of your > 'hearts 3' > >>> print Card(2, 3) #3 > hearts 3 > > In #1, the output is the __repr__() of your Card class; you can modify > this output by overriding the __repr__() on your Card class. > > In #2, the output is the __repr__() of a string, the string is the return > value from __str__() of your Card class. The repr of a string is the string > enclosed in quotes, which is why there is an extra pair of quotes. > > In #3, you're 'print'-ing a string, the string is the return value from > __str__() of your Card class. There's no extra quotes, since 'print' prints > the string itself, not the repr of the string. > > -- > http://mail.python.org/**mailman/listinfo/python-list > -- Suresh Sharma Regional Project Manager, O2F,Mumbai Maharashtra-400101. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dihedral88888 at googlemail.com Tue Dec 6 01:27:48 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 5 Dec 2011 22:27:48 -0800 (PST) Subject: Single key press In-Reply-To: References: Message-ID: <29114360.50.1323152868752.JavaMail.geo-discussion-forums@prcu10> On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: > Hi. > > I wonder if it is realistic to get a single key press in Python > without ncurses or > any similar library. In single key press I mean something like j and k > in Gnu less > program, you press the key and and it is captured by the script without need to > press enter afterwards Sounds like the fast key searching for a list in the DOS application in the old days. This is easy in GUI of just tens of items, but for thousands of items such as in a directory listing, some API is really slow. From dihedral88888 at googlemail.com Tue Dec 6 01:27:48 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 5 Dec 2011 22:27:48 -0800 (PST) Subject: Single key press In-Reply-To: References: Message-ID: <29114360.50.1323152868752.JavaMail.geo-discussion-forums@prcu10> On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: > Hi. > > I wonder if it is realistic to get a single key press in Python > without ncurses or > any similar library. In single key press I mean something like j and k > in Gnu less > program, you press the key and and it is captured by the script without need to > press enter afterwards Sounds like the fast key searching for a list in the DOS application in the old days. This is easy in GUI of just tens of items, but for thousands of items such as in a directory listing, some API is really slow. From r.grimm at science-computing.de Tue Dec 6 01:42:35 2011 From: r.grimm at science-computing.de (Rainer Grimm) Date: Mon, 5 Dec 2011 22:42:35 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> Message-ID: <11334186.28.1323153755482.JavaMail.geo-discussion-forums@vbxw7> Hello, > try: > songs = [Song(id) for id in song_ids] > except Song.DoesNotExist: > print "unknown song id (%d)" % id that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world. It's only syntactic sugar for the functions map and filter. So functions have to be pure functions. To say it in other words, they have to be side-effect free. But the python construct from above pollutes the namespace with name id. Greetings from Rottenburg, Rainer From wuwei23 at gmail.com Tue Dec 6 02:02:42 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 5 Dec 2011 23:02:42 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: On Dec 6, 2:36?pm, Xah Lee wrote: > The python community is full of fanatics with their drivels. You do know that you could just fuck right off and leave us to it, yes? In general, it's the person who is shrilly imposing their minority opinion on a disinterested audience that deserves the title 'fanatic'. From anacrolix at gmail.com Tue Dec 6 02:54:09 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Tue, 6 Dec 2011 18:54:09 +1100 Subject: Questions about LISP and Python. In-Reply-To: References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: This guy is an even better troll than that 88888 guy. His spelling is equally bad. His essays make some good points, but I don't see why he doesn't shut his trap and move on. ?_? On Tue, Dec 6, 2011 at 6:02 PM, alex23 wrote: > On Dec 6, 2:36?pm, Xah Lee wrote: >> The python community is full of fanatics with their drivels. > > You do know that you could just fuck right off and leave us to it, > yes? > > In general, it's the person who is shrilly imposing their minority > opinion on a disinterested audience that deserves the title 'fanatic'. > -- > http://mail.python.org/mailman/listinfo/python-list From s.pasoev at gmail.com Tue Dec 6 03:29:54 2011 From: s.pasoev at gmail.com (Sergi Pasoev) Date: Tue, 6 Dec 2011 12:59:54 +0430 Subject: Python-list Digest, Vol 99, Issue 27 In-Reply-To: References: Message-ID: I do not want GUI, I just want a console application which will read a single character input. When you read from stdin for example, you have it to be terminated. 2011/12/6 : > Send Python-list mailing list submissions to > ? ? ? ?python-list at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > ? ? ? ?http://mail.python.org/mailman/listinfo/python-list > or, via email, send a message with subject or body 'help' to > ? ? ? ?python-list-request at python.org > > You can reach the person managing the list at > ? ? ? ?python-list-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Python-list digest..." > > Today's Topics: > > ? 1. Re: Questions about LISP and Python. (Xah Lee) > ? 2. Re: Questions about LISP and Python. (Chris Angelico) > ? 3. Re: 70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when > ? ? ?sending ? large object (Dennis Lee Bieber) > ? 4. Single key press (Sergi Pasoev) > ? 5. Re: Fwd: class print method... (Suresh Sharma) > ? 6. Re: Single key press (88888 Dihedral) > ? 7. Re: Single key press (88888 Dihedral) > ? 8. Re: Scope of variable inside list comprehensions? (Rainer Grimm) > ? 9. Re: Questions about LISP and Python. (alex23) > ?10. Re: Questions about LISP and Python. (Matt Joiner) > > > ---------- Message transf?r? ---------- > From:?Xah Lee > To:?python-list at python.org > Date:?Mon, 5 Dec 2011 20:36:45 -0800 (PST) > Subject:?Re: Questions about LISP and Python. > On Dec 5, 4:31?am, Tim Bradshaw wrote: >> On 2011-12-05 11:51:11 +0000, Xah Lee said: >> >> > python has more readible syntax, more modern computer language >> > concepts, and more robust libraries. These qualities in turn made it >> > popular. >> >> Yet you still post here: why? > > i don't like python, and i prefer emacs lisp. The primary reason is > that python is not functional, especially with python 3. The python > community is full of fanatics with their drivels. In that respect, > it's not unlike Common Lisp community and Scheme lisp community. > > see also: > > ?Python Documentation Problems? > http://xahlee.org/perl-python/python_doc_index.html > > ?Computer Language Design: What's List Comprehension and Why is It > Harmful?? > http://xahlee.org/comp/list_comprehension.html > > ?Lambda in Python 3000? > http://xahlee.org/perl-python/python_3000.html > > ?What Languages to Hate? > http://xahlee.org/UnixResource_dir/writ/language_to_hate.html > > ?Xah on Programing Languages? > http://xahlee.org/Periodic_dosage_dir/comp_lang.html > > ?Xah > > > > ---------- Message transf?r? ---------- > From:?Chris Angelico > To:?python-list at python.org > Date:?Tue, 6 Dec 2011 16:10:27 +1100 > Subject:?Re: Questions about LISP and Python. > On Tue, Dec 6, 2011 at 3:36 PM, Xah Lee wrote: >> i don't like python, and i prefer emacs lisp. The primary reason is >> that python is not functional, especially with python 3. The python >> community is full of fanatics with their drivels. In that respect, >> it's not unlike Common Lisp community and Scheme lisp community. > > So you hate Python. Fine. Why post here? Why not just abandon Python > as a dead loss and go code in Lithp? > > Clearly something is keeping you here. Is it that there's something > about Python that you really like, or are you just trolling? > > ChrisA > > > > ---------- Message transf?r? ---------- > From:?Dennis Lee Bieber > To:?python-list at python.org > Date:?Mon, 05 Dec 2011 21:15:09 -0800 > Subject:?Re: 70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when sending large object > On Mon, 5 Dec 2011 18:49:44 +0100, DPalao > wrote: > >>Obviously it was not me who added the disgusting "70% [* SPAM *]" string to >>the subject. And I'd like to know the answer too. >> > ? ? ? ?Based upon your headers, somewhere your message went through a spam > check program... > > X-Greylist: Sender succeeded SMTP AUTH, not delayed by > milter-greylist-4.3.7 > ? ? ? ?(monster.roma2.infn.it [141.108.255.100]); > ? ? ? ?Mon, 05 Dec 2011 09:02:22 +0100 (CET) > X-PMX-Version: 5.6.1.2065439, Antispam-Engine: 2.7.2.376379, > ? ? ? ?Antispam-Data: 2011.12.5.74814 > X-PMX-Spam: 70% > X-PMX-Spam-report: The following antispam rules were triggered by this > message: > ? ? ? ?Rule ? ? ? ? ? ? ? ? ? ? ?Score Description > ? ? ? ?RDNS_SUSP_FORGED_FROM ? ? 3.500 From domain appears to be forged, > ? ? ? ?and arrived via a host with a known suspicious rDNS. > ? ? ? ?SXL_IP_DYNAMIC 3.000 Received via a known dynamic IP (SXL lookup): > ? ? ? ?34.140.47.93.fur > ? ? ? ?FORGED_FROM_GMAIL ? ? ? ? 0.100 Appears to forge gmail in the from > ? ? ? ?FROM_NAME_ONE_WORD ? ? ? ?0.050 Name in From header is a single word > ? ? ? ?BODYTEXTP_SIZE_3000_LESS 0.000 Body size of the text/plain part is > less > ? ? ? ?than 3k > ? ? ? ?BODY_SIZE_1500_1599 0.000 Message body size is 1500 to 1599 bytes > ? ? ? ?BODY_SIZE_2000_LESS 0.000 Message body size is less than 2000 bytes. > ? ? ? ?BODY_SIZE_5000_LESS 0.000 Message body size is less than 5000 bytes. > ? ? ? ?BODY_SIZE_7000_LESS 0.000 Message body size is less than 5000 bytes. > ? ? ? ?RDNS_GENERIC_POOLED 0.000 Sender's PTR record matches generic pooled > -- > ? ? ? ?Wulfraed ? ? ? ? ? ? ? ? Dennis Lee Bieber ? ? ? ? AF6VN > ? ? ? ?wlfraed at ix.netcom.com ? ?HTTP://wlfraed.home.netcom.com/ > > > > > ---------- Message transf?r? ---------- > From:?Sergi Pasoev > To:?python-list at python.org > Date:?Tue, 6 Dec 2011 10:19:55 +0430 > Subject:?Single key press > Hi. > > I wonder if it is realistic to get a single key press in Python > without ncurses or > any similar library. In single key press I mean something like j and k > in Gnu less > program, you press the key and and it is captured by the script without need to > press enter afterwards > > > > ---------- Message transf?r? ---------- > From:?Suresh Sharma > To:?Lie Ryan > Date:?Tue, 6 Dec 2011 11:23:16 +0530 > Subject:?Re: Fwd: class print method... > Dave / Ryan > Thanks i have got it and it worked after using repr statement. Thanks everyone for their valuable feedback. > > > > On Mon, Dec 5, 2011 at 6:11 PM, Lie Ryan wrote: >> >> On 12/05/2011 10:18 PM, Suresh Sharma wrote: >>> >>> >>> Pls help its really frustrating >>> ---------- Forwarded message ---------- >>> From: Suresh Sharma >>> Date: Monday, December 5, 2011 >>> Subject: class print method... >>> To: "d at davea.name " >> > >>> >>> >>> Dave, >>> Thanx for the quick response, i am sorry that i did not explain >>> correctly look at the code below inspite of this i am just getting class >>> object at memory location.I am sort i typed all this code on my android >>> in a hurry so.indentation could.not.be.managed but this.similar code >>> when i run all my objects created by class deck are not shown but stored >>> in varioia meory locations. How can i display them. >>> >> >> I think you're in the right track, however I suspect you're running the code in the shell instead of as a script. The shell uses __repr__() to print objects instead of __str__(), so you either need to use 'print' or you need to call str(), note the following: >> >> Python 2.7.2+ (default, Oct ?4 2011, 20:06:09) >> [GCC 4.6.1] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. >> >>> suits = ['spades', 'clubs', 'diamonds', 'hearts'] >> >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] >> >>> class Card: >> ... ? ? def __init__(self, rank, suit): >> ... ? ? ? ? self.suit = suit >> ... ? ? ? ? self.rank = rank >> ... ? ? def __str__(self): >> ... ? ? ? ? return suits[self.suit] + ' ' + ranks[self.rank] >> ... >> >>> Card(2, 3) #1 >> <__main__.Card instance at 0x7f719c3a20e0> >> >>> str(Card(2, 3)) #2 of your >> 'hearts 3' >> >>> print Card(2, 3) #3 >> hearts 3 >> >> In #1, the output is the __repr__() of your Card class; you can modify this output by overriding the __repr__() on your Card class. >> >> In #2, the output is the __repr__() of a string, the string is the return value from __str__() of your Card class. The repr of a string is the string enclosed in quotes, which is why there is an extra pair of quotes. >> >> In #3, you're 'print'-ing a string, the string is the return value from __str__() of your Card class. There's no extra quotes, since 'print' prints the string itself, not the repr of the string. >> >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > > > -- > Suresh Sharma > Regional Project Manager, > O2F,Mumbai > Maharashtra-400101. > > > > > > ---------- Message transf?r? ---------- > From:?88888 Dihedral > To:?comp.lang.python at googlegroups.com > Date:?Mon, 5 Dec 2011 22:27:48 -0800 (PST) > Subject:?Re: Single key press > On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: >> Hi. >> >> I wonder if it is realistic to get a single key press in Python >> without ncurses or >> any similar library. In single key press I mean something like j and k >> in Gnu less >> program, you press the key and and it is captured by the script without need to >> press enter afterwards > > Sounds like the ?fast key searching for a list in the DOS application in the > old days. > > This is easy in GUI of just tens of items, but for thousands of items such > as in a directory listing, some API is really slow. > > > > > > ---------- Message transf?r? ---------- > From:?88888 Dihedral > To:?python-list at python.org > Date:?Mon, 5 Dec 2011 22:27:48 -0800 (PST) > Subject:?Re: Single key press > On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: >> Hi. >> >> I wonder if it is realistic to get a single key press in Python >> without ncurses or >> any similar library. In single key press I mean something like j and k >> in Gnu less >> program, you press the key and and it is captured by the script without need to >> press enter afterwards > > Sounds like the ?fast key searching for a list in the DOS application in the > old days. > > This is easy in GUI of just tens of items, but for thousands of items such > as in a directory listing, some API is really slow. > > > > > > ---------- Message transf?r? ---------- > From:?Rainer Grimm > To:?python-list at python.org > Date:?Mon, 5 Dec 2011 22:42:35 -0800 (PST) > Subject:?Re: Scope of variable inside list comprehensions? > Hello, > >> ? ? try: >> ? ? ? ? songs = [Song(id) for id in song_ids] >> ? ? except Song.DoesNotExist: >> ? ? ? ? print "unknown song id (%d)" % id > that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world. It's only syntactic sugar for the functions map and filter. So functions have to be pure functions. To say it in other words, they have to be side-effect free. But the python construct from above pollutes the namespace with name id. > > Greetings from Rottenburg, > Rainer > > > > ---------- Message transf?r? ---------- > From:?alex23 > To:?python-list at python.org > Date:?Mon, 5 Dec 2011 23:02:42 -0800 (PST) > Subject:?Re: Questions about LISP and Python. > On Dec 6, 2:36?pm, Xah Lee wrote: >> The python community is full of fanatics with their drivels. > > You do know that you could just fuck right off and leave us to it, > yes? > > In general, it's the person who is shrilly imposing their minority > opinion on a disinterested audience that deserves the title 'fanatic'. > > > > ---------- Message transf?r? ---------- > From:?Matt Joiner > To:?alex23 > Date:?Tue, 6 Dec 2011 18:54:09 +1100 > Subject:?Re: Questions about LISP and Python. > This guy is an even better troll than that 88888 guy. His spelling is > equally bad. His essays make some good points, but I don't see why he > doesn't shut his trap and move on. > > ?_? > > > > On Tue, Dec 6, 2011 at 6:02 PM, alex23 wrote: >> On Dec 6, 2:36?pm, Xah Lee wrote: >>> The python community is full of fanatics with their drivels. >> >> You do know that you could just fuck right off and leave us to it, >> yes? >> >> In general, it's the person who is shrilly imposing their minority >> opinion on a disinterested audience that deserves the title 'fanatic'. >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > -- > http://mail.python.org/mailman/listinfo/python-list From wuwei23 at gmail.com Tue Dec 6 03:39:53 2011 From: wuwei23 at gmail.com (alex23) Date: Tue, 6 Dec 2011 00:39:53 -0800 (PST) Subject: Single key press References: Message-ID: <90d8ede3-f42e-4342-91cf-cb671cd1b63b@c13g2000vbh.googlegroups.com> On Dec 6, 3:49?pm, Sergi Pasoev wrote: > I wonder if it is realistic to get a single key press in Python > without ncurses or any similar library. It's possible using Tkinter in the standard library: http://www.daniweb.com/software-development/python/code/216830 From steve+comp.lang.python at pearwood.info Tue Dec 6 03:48:39 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Dec 2011 08:48:39 GMT Subject: Single key press References: Message-ID: <4eddd6e7$0$11114$c3e8da3@news.astraweb.com> On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: > Hi. > > I wonder if it is realistic to get a single key press in Python without > ncurses or > any similar library. In single key press I mean something like j and k > in Gnu less > program, you press the key and and it is captured by the script without > need to press enter afterwards Coincidentally, I was working on that exact function last night, so you inspired me to put it up on the Python cookbook. This version should work on any POSIX system such as Linux, as well as Windows. I don't know if it will work on Macintosh. http://code.activestate.com/recipes/577977-get-single-keypress/ -- Steven From nobody at nowhere.com Tue Dec 6 03:55:16 2011 From: nobody at nowhere.com (Nobody) Date: Tue, 06 Dec 2011 08:55:16 +0000 Subject: struct calcsize discrepency? References: <102eb8db-da87-4c89-bf9e-df02e65a641e@4g2000yqu.googlegroups.com> <4EDB88FD.9000108@davea.name> Message-ID: On Mon, 05 Dec 2011 00:20:32 -0800, Mark Dickinson wrote: >> May be, yes, but since calcsize() is returning 12 when the elements >> are put in the other order, it would seem to be not counting such >> padding. > > Indeed. That's arguably a bug in the struct module, There's no "arguably" about it. The documentation says: Native size and alignment are determined using the C compiler?s sizeof expression. But given: struct { unsigned long long a; char b[4]; } foo; struct { char b[4]; unsigned long long a; } bar; sizeof(foo) will always equal sizeof(bar). If long long is 8 bytes and has 8-byte alignment, both will be 16. If you want consistency with the in-memory representation used by C/C++ programs (and the on-disk representation used by C/C++ programs which write the in-memory representation directly to file), use ctypes; e.g.: >>> from ctypes import * >>> class foo(Structure): _fields_ = [ ("a", c_ulonglong), ("b", c_char * 4)] >>> sizeof(foo) 16 From alex.mizrahi at gmail.com Tue Dec 6 04:12:31 2011 From: alex.mizrahi at gmail.com (Alex Mizrahi) Date: Tue, 06 Dec 2011 11:12:31 +0200 Subject: Questions about LISP and Python. In-Reply-To: <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: <4edddc81$0$283$14726298@news.sunsite.dk> > i don't like python, and i prefer emacs lisp. The primary reason is > that python is not functional, especially with python 3. The python > community is full of fanatics with their drivels. In that respect, > it's not unlike Common Lisp community and Scheme lisp community. What functional programming features do you use, Xah? Last time I've checked, you didn't care about closures. In this respect, Python is better at functional programming than Emacs Lisp. Do you just like writing arbitrarily long expression? Or you think that eval is functional? From nobody at nowhere.com Tue Dec 6 04:27:29 2011 From: nobody at nowhere.com (Nobody) Date: Tue, 06 Dec 2011 09:27:29 +0000 Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: >> Emitting "\b \b" is one very common way to do a destructive backspace. >> Inelegant? Perhaps, but a common inelegance. > > That's pretty much the only way I've seen it done for the past 25 > years. ... before which, it was BS-DEL-BS. DEL being 127 means that it punches through all of the holes on the paper tape, causing whatever was there previously to be replaced with a DEL character. BS-SPC-BS would only punch through one of the holes. From hansmu at xs4all.nl Tue Dec 6 04:28:32 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 06 Dec 2011 10:28:32 +0100 Subject: Single key press In-Reply-To: <4eddd6e7$0$11114$c3e8da3@news.astraweb.com> References: <4eddd6e7$0$11114$c3e8da3@news.astraweb.com> Message-ID: <4edde040$0$6973$e4fe514c@news2.news.xs4all.nl> On 6/12/11 09:48:39, Steven D'Aprano wrote: > On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: > >> Hi. >> >> I wonder if it is realistic to get a single key press in Python without >> ncurses or >> any similar library. In single key press I mean something like j and k >> in Gnu less >> program, you press the key and and it is captured by the script without >> need to press enter afterwards > > > Coincidentally, I was working on that exact function last night, so you > inspired me to put it up on the Python cookbook. This version should work > on any POSIX system such as Linux, as well as Windows. I don't know if it > will work on Macintosh. > > http://code.activestate.com/recipes/577977-get-single-keypress/ It works fine on MacOS/X, but probably not on MacOS Classic. -- HansM From nobody at nowhere.com Tue Dec 6 04:48:55 2011 From: nobody at nowhere.com (Nobody) Date: Tue, 06 Dec 2011 09:48:55 +0000 Subject: Single key press References: Message-ID: On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: > I wonder if it is realistic to get a single key press in Python > without ncurses or any similar library. On Unix, you need to use termios.tcsetattr() to disable "canonical mode". Otherwise, the tty driver will only pass data up to the application when the user enters a newline or EOF. If you do this, you also need to handle SIGTSTP and SIGCONT, restoring the original terminal settings on SIGTSTP (typically generated by Ctrl-Z) and restoring the application's setting on SIGCONT. From dihedral88888 at googlemail.com Tue Dec 6 05:16:42 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 6 Dec 2011 02:16:42 -0800 (PST) Subject: Scope of variable inside list comprehensions? In-Reply-To: <11334186.28.1323153755482.JavaMail.geo-discussion-forums@vbxw7> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <11334186.28.1323153755482.JavaMail.geo-discussion-forums@vbxw7> Message-ID: <72527.110.1323166602941.JavaMail.geo-discussion-forums@prgk20> On Tuesday, December 6, 2011 2:42:35 PM UTC+8, Rainer Grimm wrote: > Hello, > > > try: > > songs = [Song(id) for id in song_ids] > > except Song.DoesNotExist: > > print "unknown song id (%d)" % id > that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world. It's only syntactic sugar for the functions map and filter. So functions have to be pure functions. To say it in other words, they have to be side-effect free. But the python construct from above pollutes the namespace with name id. > > Greetings from Rottenburg, > Rainer The list might have to grow in a careless way that might lead to a crash in the for inside a list that can't be trapped for errors directly. From jeanmichel at sequans.com Tue Dec 6 05:38:07 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 06 Dec 2011 11:38:07 +0100 Subject: Scope of variable inside list comprehensions? In-Reply-To: <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <25531460.861.1323104692320.JavaMail.geo-discussion-forums@yqiv14> <4edd471a$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EDDF08F.4090508@sequans.com> Steven D'Aprano wrote: > On Mon, 05 Dec 2011 19:57:15 +0100, Jean-Michel Pichavant wrote: > > >> The proper way to propagate information with exceptions is using the >> exception itself: >> >> try: >> songs = [Song(_id) for _id in song_ids] >> except Song.DoesNotExist, exc: >> print exc >> > > > I'm not entirely sure that this is the proper way to propagate the > exception. I see far to many people catching exceptions to print them, or > worse, to print a generic, useless message like "an error occurred". > [snip] You misread me, I was referering to passing *information* with exception (in other words, use the exception attributes). In the example I gave, the exception has the songId value responsible for raising the error. I totaly second your opinion on how poor the above handler is (hmm not sure about this grammar construct, it sounds like a Yoda sentence). JM From lanyjie at yahoo.com Tue Dec 6 06:06:04 2011 From: lanyjie at yahoo.com (Yingjie Lan) Date: Tue, 6 Dec 2011 03:06:04 -0800 (PST) Subject: the deceptive continuous assignments Message-ID: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> Hi, I just figured out this with Python3.2 IDLE: >>> class k: pass >>> x=k() >>> x.thing = 1 >>> x.thing 1 >>> x = x.thing = 1 Traceback (most recent call last): ? File "", line 1, in ? ? x = x.thing = 1 AttributeError: 'int' object has no attribute 'thing' >>> x 1 >>> ================ when I do x=x.thing=1, I thought it would be like in C, 1 is first assigned to x.thing, then it is further assigned to x. But what seems to be going on here is that 1 is first assigned to x, then to x.thing (which causes an error). Any reason why would Python deviate from C in this regard? Thanks! Yingjie From andrea.crotti.0 at gmail.com Tue Dec 6 06:28:08 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 06 Dec 2011 11:28:08 +0000 Subject: whitespace cleanup Message-ID: <4EDDFC48.2000903@gmail.com> Now on Emacs I have a hook before every save that cleans up all the "wrong" white spaces, with the 'whitespace-cleanup' function. I would like that also for my non emacsers colleagues, and possibly with a Python script. I looked up around but I can't find anything useful, any advice? Thanks, Andrea From pedro.h.souto at gmail.com Tue Dec 6 06:49:58 2011 From: pedro.h.souto at gmail.com (Pedro Henrique G. Souto) Date: Tue, 06 Dec 2011 09:49:58 -0200 Subject: whitespace cleanup In-Reply-To: <4EDDFC48.2000903@gmail.com> References: <4EDDFC48.2000903@gmail.com> Message-ID: <4EDE0166.1030006@gmail.com> On 06/12/2011 09:28, Andrea Crotti wrote: > Now on Emacs I have a hook before every save that cleans up all the > "wrong" white spaces, > with the 'whitespace-cleanup' function. > > I would like that also for my non emacsers colleagues, and possibly with > a Python script. > I looked up around but I can't find anything useful, any advice? You can use the strip() method: [http://docs.python.org/release/2.3/lib/module-string.html] While reading the file as strings, the strip() method rips out all of the extra whitespace. > Thanks, > Andrea > Good luck! -- Att; Pedro Henrique G. Souto From andrea.crotti.0 at gmail.com Tue Dec 6 06:59:57 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 06 Dec 2011 11:59:57 +0000 Subject: whitespace cleanup In-Reply-To: <4EDE0166.1030006@gmail.com> References: <4EDDFC48.2000903@gmail.com> <4EDE0166.1030006@gmail.com> Message-ID: <4EDE03BD.2030106@gmail.com> On 12/06/2011 11:49 AM, Pedro Henrique G. Souto wrote: > On 06/12/2011 09:28, Andrea Crotti wrote: > > Now on Emacs I have a hook before every save that cleans up all the > > "wrong" white spaces, > > with the 'whitespace-cleanup' function. > > > > I would like that also for my non emacsers colleagues, and possibly > with > > a Python script. > > I looked up around but I can't find anything useful, any advice? > > You can use the strip() method: > [http://docs.python.org/release/2.3/lib/module-string.html] > > While reading the file as strings, the strip() method rips out all of > the extra whitespace. > > > Thanks, > > Andrea > > > > Good luck! > Well it's not so simple, I clearly don't want to strip out whitespace in the beginning of the line, or my nice code will break miserably ;) So I think some magic using tokens/regexp/python grammar would be useful to do something reliable, or is it easier than that? From rosuav at gmail.com Tue Dec 6 07:06:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 23:06:55 +1100 Subject: whitespace cleanup In-Reply-To: <4EDE03BD.2030106@gmail.com> References: <4EDDFC48.2000903@gmail.com> <4EDE0166.1030006@gmail.com> <4EDE03BD.2030106@gmail.com> Message-ID: On Tue, Dec 6, 2011 at 10:59 PM, Andrea Crotti wrote: > Well it's not so simple, I clearly don't want to strip out whitespace in the > beginning of the line, > or my nice code will break miserably ;) The question is: What is "wrong" whitespace? Whatever you declare to be wrong, you can probably come up with a regexp or other spec that will catch it (or conversely, declare some pattern to be "right" and look for anything else). ChrisA From pedro.h.souto at gmail.com Tue Dec 6 07:17:12 2011 From: pedro.h.souto at gmail.com (Pedro Henrique G. Souto) Date: Tue, 06 Dec 2011 10:17:12 -0200 Subject: whitespace cleanup In-Reply-To: References: <4EDDFC48.2000903@gmail.com> <4EDE0166.1030006@gmail.com> <4EDE03BD.2030106@gmail.com> Message-ID: <4EDE07C8.3050004@gmail.com> On 06/12/2011 10:06, Chris Angelico wrote: > On Tue, Dec 6, 2011 at 10:59 PM, Andrea Crotti > wrote: >> Well it's not so simple, I clearly don't want to strip out whitespace in the >> beginning of the line, >> or my nice code will break miserably ;) > > The question is: What is "wrong" whitespace? Whatever you declare to > be wrong, you can probably come up with a regexp or other spec that > will catch it (or conversely, declare some pattern to be "right" and > look for anything else). > > ChrisA Something like PythonTidy does what you want? http://pypi.python.org/pypi/PythonTidy If you like to write your own script, or if what you want is similar, but not the same, the source code is a good place to start: http://lacusveris.com/PythonTidy/PythonTidy-1.20.python -- Att; Pedro Henrique G. Souto From tjreedy at udel.edu Tue Dec 6 07:20:45 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Dec 2011 07:20:45 -0500 Subject: the deceptive continuous assignments In-Reply-To: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> References: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> Message-ID: On 12/6/2011 6:06 AM, Yingjie Lan wrote: > Hi, I just figured out this with Python3.2 IDLE: > >>>> class k: pass >>>> x=k() >>>> x.thing = 1 >>>> x.thing > 1 >>>> x = x.thing = 1 > Traceback (most recent call last): > File "", line 1, in > x = x.thing = 1 > AttributeError: 'int' object has no attribute 'thing' >>>> x > 1 >>>> > > ================ > when I do x=x.thing=1, I thought it would > be like in C, 1 is first assigned to x.thing, > then it is further assigned to x. In C, assignments are expressions, not statements. a = b = c is parsed as a = (b = c). a get the result of (b = c), which happens to be c. In Python, the syntax really should be a =, b = c in that the assignments are more in parallel. > But what seems to be going on here is > that 1 is first assigned to x, then > to x.thing (which causes an error). > > Any reason why would Python deviate > from C in this regard? Python is consistently left to right in expression evaluation. For assignment, it first does left to right on the source side, and then on the target side. "An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right." You have two target lists. x,x.thing = 1,2 would assign 1 to x and then get an error trying to assign a different object to x.thing. In your case, you get an error trying to assign the same object rather than a different object, but the principle is the same. "WARNING: Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are ?safe? (for example a, b = b, a swaps two variables), overlaps within the collection of assigned-to variables are not safe!" You found an unsafe overlap. x.thing = x = 1 would work, though it seems strange (and unlikely in practice) to rebind x to an int after it is bound to a class k instance. -- Terry Jan Reedy From rosuav at gmail.com Tue Dec 6 07:33:26 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 6 Dec 2011 23:33:26 +1100 Subject: the deceptive continuous assignments In-Reply-To: References: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> Message-ID: On Tue, Dec 6, 2011 at 11:20 PM, Terry Reedy wrote: > You found an unsafe overlap. > x.thing = x = 1 > would work, though it seems strange (and unlikely in practice) to rebind x > to an int after it is bound to a class k instance. This code is starting to look like it wants to work with a linked list. >>> class node: def __init__(self,x): self.payload=x self.next=None def walk(self): print("My payload is: "+self.payload) if self.next: self.next.walk() >>> head=tail=node("This") >>> tail.next=tail=node("is") >>> tail.next=tail=node("a") >>> tail.next=tail=node("test.") >>> head.walk() My payload is: This My payload is: is My payload is: a My payload is: test. >>> ChrisA From andrea.crotti.0 at gmail.com Tue Dec 6 08:00:04 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 06 Dec 2011 13:00:04 +0000 Subject: whitespace cleanup In-Reply-To: <4EDE07C8.3050004@gmail.com> References: <4EDDFC48.2000903@gmail.com> <4EDE0166.1030006@gmail.com> <4EDE03BD.2030106@gmail.com> <4EDE07C8.3050004@gmail.com> Message-ID: <4EDE11D4.7060508@gmail.com> On 12/06/2011 12:17 PM, Pedro Henrique G. Souto wrote: > > Something like PythonTidy does what you want? > > http://pypi.python.org/pypi/PythonTidy > > If you like to write your own script, or if what you want is similar, > but not the same, the source code is a good place to start: > > http://lacusveris.com/PythonTidy/PythonTidy-1.20.python > For wrong space I meant what is not pep8 complaint.. Actually even pep8.py is quite good in finding out these things, but it just warns about and doesn't propose to fix the problems. PythonTidy is more what I need, using easy_install doesn't work though, but the direct link on the pypi page does.. From rantingrickjohnson at gmail.com Tue Dec 6 08:13:09 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 6 Dec 2011 05:13:09 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> On Dec 5, 11:10?pm, Chris Angelico wrote: > On Tue, Dec 6, 2011 at 3:36 PM, Xah Lee wrote: > > i don't like python, and i prefer emacs lisp. The primary reason is > > that python is not functional, especially with python 3. The python > > community is full of fanatics with their drivels. In that respect, > > it's not unlike Common Lisp community and Scheme lisp community. > > So you hate Python. Fine. Why post here? Why not just abandon Python > as a dead loss and go code in Lithp? > > Clearly something is keeping you here. Is it that there's something > about Python that you really like, or are you just trolling? I would say Mr. Lee does in fact find some usefulness of Python (as do i) HOWEVER he also laments the asinities that plague the language, the documentation, and especially, this community. Anyone who would take the time to write *articulate* suggestions for grave deficiencies in the Python documentation must care. Mr. Lee, has documented many of the atrocities of the doc in his fine website. I just recently finished reading a few more of his fine suggestions and I especially enjoyed the "os.path.split" rant. PRICELESS! Listen, python is a great language, but not a perfect one. You people seem to get all upset at even the slightest suggestion that Python is not perfect. The haughty arrogance i see day after day around me just makes me sick. Grow up! You are not perfect and neither is Python... or Guido for that matter. This community is chalk full (from top to bottom!) of arrogant, tech geeking, unix hacks who look down on the "outsiders" as excrement. I believe it's high time for these folks to eat a big slice of humble pie -- i just fear there is not enough to go around! O_O From rantingrickjohnson at gmail.com Tue Dec 6 08:21:53 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 6 Dec 2011 05:21:53 -0800 (PST) Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 6, 3:27?am, Nobody wrote: > On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > >> Emitting "\b \b" is one very common way to do a destructive backspace. > >> Inelegant? Perhaps, but a common inelegance. > > > That's pretty much the only way I've seen it done for the past 25 > > years. > > ... before which, it was BS-DEL-BS. > > DEL being 127 means that it punches through all of the holes on the paper > tape, causing whatever was there previously to be replaced with a DEL > character. BS-SPC-BS would only punch through one of the holes. *Wise Observer Speculates:* Why on earth are we "21st century slaves" to an archaic mid 20th century technology that punches holes in paper tape? Anyone? >>> isinstance(Progress, None) True From andrea.crotti.0 at gmail.com Tue Dec 6 08:22:05 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 06 Dec 2011 13:22:05 +0000 Subject: whitespace cleanup In-Reply-To: <4EDE07C8.3050004@gmail.com> References: <4EDDFC48.2000903@gmail.com> <4EDE0166.1030006@gmail.com> <4EDE03BD.2030106@gmail.com> <4EDE07C8.3050004@gmail.com> Message-ID: <4EDE16FD.4030107@gmail.com> So apparently PythonTidy uses the AST (the compiler module) to find out the problems, and generates new nodes with a put method to write out the code with the fix. An awful lot of code is needed to make it work apparently, not very neat. Pep8 instead uses tokenize and regular expressions only, and the code is much neater, not sure how is possible to make it fix things though. From rosuav at gmail.com Tue Dec 6 08:27:48 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Dec 2011 00:27:48 +1100 Subject: Questions about LISP and Python. In-Reply-To: <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: On Wed, Dec 7, 2011 at 12:13 AM, Rick Johnson wrote: > This community is chalk full (from top to bottom!) of arrogant, tech > geeking, unix hacks who look down on the "outsiders" as excrement. I > believe it's high time for these folks to eat a big slice of humble > pie -- i just fear there is not enough to go around! O_O It's also chock full (note that) of trolls. Do we need a python-troll-list at python.org? You two could discuss the upcoming Pyson 4000 (that's Python with a Lithp) and how it's going to revolutionize the world. ChrisA From floris.bruynooghe at gmail.com Tue Dec 6 08:37:47 2011 From: floris.bruynooghe at gmail.com (Floris Bruynooghe) Date: Tue, 6 Dec 2011 05:37:47 -0800 (PST) Subject: Need some IPC pointers In-Reply-To: References: Message-ID: <31237669.168.1323178667071.JavaMail.geo-discussion-forums@vbko11> I'm surprised no one has mentioned zeromq as transport yet. It provides scaling from in proc (between threads) to inter-process and remote machines in a fairly transparent way. It's obviously not the python stdlib and as any system there are downsides too. Regards, Floris From floris.bruynooghe at gmail.com Tue Dec 6 08:37:47 2011 From: floris.bruynooghe at gmail.com (Floris Bruynooghe) Date: Tue, 6 Dec 2011 05:37:47 -0800 (PST) Subject: Need some IPC pointers In-Reply-To: References: Message-ID: <31237669.168.1323178667071.JavaMail.geo-discussion-forums@vbko11> I'm surprised no one has mentioned zeromq as transport yet. It provides scaling from in proc (between threads) to inter-process and remote machines in a fairly transparent way. It's obviously not the python stdlib and as any system there are downsides too. Regards, Floris From rosuav at gmail.com Tue Dec 6 09:03:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Dec 2011 01:03:55 +1100 Subject: Backspace does not erase in stdout In-Reply-To: References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Dec 7, 2011 at 12:21 AM, Rick Johnson wrote: > *Wise Observer Speculates:* Why on earth are we "21st century slaves" > to an archaic mid 20th century technology that punches holes in paper > tape? Anyone? > >>>> isinstance(Progress, None) > True I'm not sure. Let's see... * Manned, powered, heavier-than-air flight: A hundred years. * TCP/IP: Thirty years. * Sliced bread: Eighty years. (What was the greatest thing BEFORE sliced bread?) * The English language: A few hundred, depending on how you count Why are we still eating sliced bread on board aeroplanes while posting in English on internet forums? Clearly we've made no progress since then. What language and communication protocol do you propose for the new python-troll-list? Getting back to the point though... The use of 127 for DEL is a consequence of the exact technology of paper tape. However, the question of whether BS is destructive or not is separate. Should it parallel other characters (draw character at cursor then advance cursor, vs retard cursor then erase character at cursor), or should it parallel cursor movement (eg carriage return, line feed, vertical tab)? If your BS is defined nondestructively, you can implement the destructive version with three characters; if it's defined destructively, you can't implement a nondestructive without some fiddliness (some systems use characters 28-31 for cursor movement). ChrisA From bahamutzero8825 at gmail.com Tue Dec 6 09:05:28 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 06 Dec 2011 08:05:28 -0600 Subject: Need some IPC pointers In-Reply-To: <3519788.203.1322688146008.JavaMail.geo-discussion-forums@yqfv40> References: <3519788.203.1322688146008.JavaMail.geo-discussion-forums@yqfv40> Message-ID: <4EDE2128.6040509@gmail.com> What about named pipes? I don't mind a bit of "if Windows do this, else, do that" as long I'm not coding two or more completely different approaches. I'm not too familiar with named pipes, though; perhaps someone with some experience could chime in. Apparently this didn't go through to Google Groups the first time; it may show up twice for some. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From skip at montanaro.dyndns.org Tue Dec 6 09:26:16 2011 From: skip at montanaro.dyndns.org (Skip Montanaro) Date: Tue, 6 Dec 2011 08:26:16 -0600 (CST) Subject: How to build 64-bit Python on Solaris with GCC? Message-ID: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> I'd like to build a 64-bit version of Python on Solaris using gcc. I did a bit of Googling, but everything I came up with seemed old, inconclusive or assumes the use of the Sun Studio compiler, with which i have no experience. Does anyone have a recipe for the subject build? Thanks, -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ From roy at panix.com Tue Dec 6 09:45:08 2011 From: roy at panix.com (Roy Smith) Date: Tue, 06 Dec 2011 09:45:08 -0500 Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Nobody wrote: > On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > > >> Emitting "\b \b" is one very common way to do a destructive backspace. > >> Inelegant? Perhaps, but a common inelegance. > > > > That's pretty much the only way I've seen it done for the past 25 > > years. > > ... before which, it was BS-DEL-BS. > > DEL being 127 means that it punches through all of the holes on the paper > tape, causing whatever was there previously to be replaced with a DEL > character. BS-SPC-BS would only punch through one of the holes. I don't remember having a DEL key on the ASR-33. It did have a RUBOUT key (http://regmedia.co.uk/2010/06/21/teletype_hereis.jpg) which performed the function you describe. I don't remember if that actually sent a 177 code over the wire or if it was a purely local function. Also, the BS code only caused the printhead to back up. If you wanted to back up the tape, you had to press the "B. SP." button the the punch (http://pichotjm.free.fr/Multi8/TTY/ASR33-3.html). Of course, sending a BS-SPS-BS sequence on a printing terminal didn't do anything about erasing what had previously been printed. To do that, you needed one of those fancy vee-de-oh things. On the ASR-33, to prevent passwords from being readable, you either disabled echo, or sent something like BS-*-BS-X-BS-$-BS-%-BS-#-BS- at -BS-&-BS-H-BS-Z-BS-M in the hopes that nothing would be readable under all that garbage. From roy at panix.com Tue Dec 6 09:49:01 2011 From: roy at panix.com (Roy Smith) Date: Tue, 06 Dec 2011 09:49:01 -0500 Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Rick Johnson wrote: > *Wise Observer Speculates:* Why on earth are we "21st century slaves" > to an archaic mid 20th century technology that punches holes in paper > tape? Anyone? Or to an archaic mid 20th century technology that limited lines to 80 characters? From invalid at invalid.invalid Tue Dec 6 10:22:18 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 6 Dec 2011 15:22:18 +0000 (UTC) Subject: Single key press References: Message-ID: On 2011-12-06, Sergi Pasoev wrote: > I wonder if it is realistic to get a single key press in Python > without ncurses or > any similar library. Yes. Just put the tty associated with stdin in raw mode and make single byte read() calls on it. Remember to restore the tty settings when your program exits. -- Grant Edwards grant.b.edwards Yow! World War III? at No thanks! gmail.com From invalid at invalid.invalid Tue Dec 6 10:24:43 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 6 Dec 2011 15:24:43 +0000 (UTC) Subject: Backspace does not erase in stdout References: <4edcecc6$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-12-06, Nobody wrote: > On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > >>> Emitting "\b \b" is one very common way to do a destructive backspace. >>> Inelegant? Perhaps, but a common inelegance. >> >> That's pretty much the only way I've seen it done for the past 25 >> years. > > ... before which, it was BS-DEL-BS. > > DEL being 127 means that it punches through all of the holes on the > paper tape, causing whatever was there previously to be replaced with > a DEL character. > BS-SPC-BS would only punch through one of the holes. Ah yes. And when you read a tape, you silently ignored DEL characters. I had completely fogotten about that. I only used paper tape very briefly, and only for storage, never as a "live" medium for editing. -- Grant Edwards grant.b.edwards Yow! I own seven-eighths of at all the artists in downtown gmail.com Burbank! From dihedral88888 at googlemail.com Tue Dec 6 10:38:15 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 6 Dec 2011 07:38:15 -0800 (PST) Subject: Single key press In-Reply-To: References: Message-ID: <29192698.206.1323185896052.JavaMail.geo-discussion-forums@prfs25> On Tuesday, December 6, 2011 11:22:18 PM UTC+8, Grant Edwards wrote: > On 2011-12-06, Sergi Pasoev wrote: > > > I wonder if it is realistic to get a single key press in Python > > without ncurses or > > any similar library. > > Yes. Just put the tty associated with stdin in raw mode and make > single byte read() calls on it. Remember to restore the tty settings > when your program exits. > > -- > Grant Edwards grant.b.edwards Yow! World War III? > at No thanks! > gmail.com Cheers to virus scan software that has to scan fast and easily for all files under a drive. Easy clean up in APPS. From p.f.moore at gmail.com Tue Dec 6 10:57:25 2011 From: p.f.moore at gmail.com (Paul Moore) Date: Tue, 6 Dec 2011 07:57:25 -0800 (PST) Subject: Referring to the class name from a class variable where inheritance is involved Message-ID: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> I want to set up an inheritance hierarchy. The base class will define a string value which should include the class name, but I don't want people who inherit from my class to have to remember to override the value. If I do this using an instance variable, it's reasonably easy: >>> class Base: ... def __init__(self): ... self.key = 'Key_for_' + self.__class__.__name__ ... def display(self): ... print self.key ... >>> class Inherited(Base): ... pass ... >>> b = Base() >>> i = Inherited() >>> b.display() Key_for_Base >>> i.display() Key_for_Inherited Rather than having the key for every instance, I'd like to use a class variable, but I can't see how I'd make that work (a class variable which is inherited but has a different value in derived classes). I could use a classmethod,but that feels like even more overkill than an instance attribute. Is there a way of doing this via class variables or something, or more relevantly, I guess, what would be the idiomatic way of doing something like this? Thanks, Paul From ian.g.kelly at gmail.com Tue Dec 6 11:58:02 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Dec 2011 09:58:02 -0700 Subject: Questions about LISP and Python. In-Reply-To: <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: On Tue, Dec 6, 2011 at 6:13 AM, Rick Johnson wrote: > I would say Mr. Lee does in fact find some usefulness of Python (as do > i) HOWEVER he also laments the asinities that plague the language, the > documentation, and especially, this community. > > Anyone who would take the time to write *articulate* suggestions for > grave deficiencies in the Python documentation must care. Mr. Lee, has > documented many of the atrocities of the doc in his fine website. I > just recently finished reading a few more of his fine suggestions and > I especially enjoyed the "os.path.split" rant. PRICELESS! I would be much more impressed if instead of simply whining about the documentation, Xah Lee would come off his high horse and actually submit a patch to fix it. That would be the mark of somebody who actually cares about the quality of the documentation. He seems to be suggesting that the problems he points out are just examples of systemic problems that make the whole incomprehensible, yet most of what he specifically identifies seems to be just copy editing for conciseness. What I find hilarious, though, is that Xah Lee has written his own Python documentation, and it's equally bad, if not worse. Just for example, I'll critique an arbitrary section. Let's talk about for loops. http://xahlee.org/perl-python/for_statement.html """This is a example of ?for? statement.. a = range(1,51) # creates a list for x in a: if x % 2 == 0: print x, 'even' """ What is a "for" statement? What does it do? If I'm new to programming, I have no idea, and the above hasn't even attempted to introduce the concept, so that example is meaningless to me. Fortunately, he starts to explain it. """In the above, the percent ?%? symbol calculates the remainder of division. The range(m,n) function gives a list from m to n-1""" Wait, I thought this section was about "for" statements. Why is the author suddenly (and poorly) explaining modulo division and the range function? These things belong in their own section. """Note that in this example, ?for? goes over a list.""" Oh, okay, the author finally is telling us what "for" does. It is used to "go over" lists, apparently so that we can get to the other side. Note that this documentation still has not said anything at all about "looping" or "repeating" or "iterating" that might help me understand what that means. Instead we have "goes over". If I did not already understand what a "for" statement does, this would not make it any clearer. It reminds me of when I was 6 or so and I asked my mother what division was, and she told me that it's how many times a number "goes into" another number. Even though I was mathematically inclined, I didn't understand that all, because I had no idea what it meant for one number to enter another. """Each time making ?x? the value of the element.""" Okay, apparently there's an element involved, and "x" is going to be its value. I really don't think I need to say anything more about this "sentence". """Python also supports ?break? and ?continue? to exit the loop. ?break? will exit the loop. ?continue? will skip code and start the next iteration.""" Finally the author breaks down and uses the words "loop" and "iteration", so if the reader actually makes it this far, then they might actually start to piece together everything that came before this. I also want to highlight his introduction of list comprehensions: http://xahlee.org/perl-python/list_comprehension.html """This construct uses a irregular syntax (called ?list comprehension?) to generate a expression normally built by nested loops. This construct has acquired a incomprehensible name ?list comprehension? in computing industry and academia. ... Remember, this jargonized ?list comprehension? is nothing more than a irregular syntax for building a list from loops. Its purpose is purely of syntactical convenience. Advanced languages such as functional languages often have this power without the syntax irregularity. (For example, Mathematica's Table function.) """ What was that again about "author masturbation" polluting the documentation, Xah? On Tue, Dec 6, 2011 at 6:13 AM, Rick Johnson wrote: > Listen, python is a great language, but not a perfect one. You people > seem to get all upset at even the slightest suggestion that Python is > not perfect. The haughty arrogance i see day after day around me just > makes me sick. Grow up! You are not perfect and neither is Python... > or Guido for that matter. I don't think Python, Guido, or myself are perfect. I think that most people here don't think that either. Python has its warts. For example, I don't like generator syntax. To be a generator, a function only has to have a "yield" statement somewhere in its body. This is not good self-documenting code. A generator should be clearly marked as such somewhere at the top of the definition. I also think that tacking coroutines onto generators was an ugly hack. Generators and coroutines are conceptually very different; let's keep them that way. And I strongly dislike super(); you may have seen me posting previously on the subject. You need to understand that people don't reject your suggestions because they're resistant to change. They reject your suggestions because you make sweeping pronouncements about the way things should be and then expect that everybody will automatically agree with you because you're so obviously right. In this, you seem to think that people should regard you in the way that you imagine they already regard Guido. In the end, you come across as a clown and an egomaniac. From matt at scotweb.co.uk Tue Dec 6 12:29:45 2011 From: matt at scotweb.co.uk (Matt Saxton) Date: Tue, 06 Dec 2011 17:29:45 +0000 Subject: Referring to the class name from a class variable where inheritance is involved In-Reply-To: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> References: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> Message-ID: <4EDE5109.5000209@scotweb.co.uk> On 06/12/11 15:57, Paul Moore wrote: > I want to set up an inheritance hierarchy. The base class will define > a string value which should include the class name, but I don't want > people who inherit from my class to have to remember to override the > value. > > If I do this using an instance variable, it's reasonably easy: > >>>> class Base: > ... def __init__(self): > ... self.key = 'Key_for_' + self.__class__.__name__ > ... def display(self): > ... print self.key > ... >>>> class Inherited(Base): > ... pass > ... >>>> b = Base() >>>> i = Inherited() >>>> b.display() > Key_for_Base >>>> i.display() > Key_for_Inherited > > Rather than having the key for every instance, I'd like to use a class > variable, but I can't see how I'd make that work (a class variable > which is inherited but has a different value in derived classes). I > could use a classmethod,but that feels like even more overkill than an > instance attribute. > > Is there a way of doing this via class variables or something, or more > relevantly, I guess, what would be the idiomatic way of doing > something like this? > > Thanks, > Paul You can use a metaclass for this: >>> class BaseMeta(type): ... def __new__(mcs, name, bases, dict): ... dict['key'] = 'Key_for_%s' % name ... return type.__new__(mcs, name, bases, dict) ... >>> class Base: ... __metaclass__ = BaseMeta ... >>> class Inherited(Base): ... pass ... >>> Base.key 'Key_for_Base' >>> Inherited.key 'Key_for_Inheritor' You can find more info on metaclasses here: http://http://docs.python.org/reference/datamodel.html#customizing-class-creation Regards Matt From tobiah at teranews.com Tue Dec 6 12:33:30 2011 From: tobiah at teranews.com (Tobiah) Date: Tue, 06 Dec 2011 09:33:30 -0800 Subject: Python 2 or 3 In-Reply-To: References: Message-ID: > What is the opinion of the wizards here, shall I learm Python 2 or > Python 3? I'm posting this here because I feel that this point is > interesting to other students of Python. Use the newer version and don't look back. From s.pasoev at gmail.com Tue Dec 6 12:34:36 2011 From: s.pasoev at gmail.com (Sergi Pasoev) Date: Tue, 6 Dec 2011 22:04:36 +0430 Subject: Python-list Digest, Vol 99, Issue 28 In-Reply-To: References: Message-ID: Thanks Steven. Maybe you mean Gnu/Linux when you say Linux. 2011/12/6 : > Send Python-list mailing list submissions to > ? ? ? ?python-list at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > ? ? ? ?http://mail.python.org/mailman/listinfo/python-list > or, via email, send a message with subject or body 'help' to > ? ? ? ?python-list-request at python.org > > You can reach the person managing the list at > ? ? ? ?python-list-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Python-list digest..." > > Today's Topics: > > ? 1. Re: Python-list Digest, Vol 99, Issue 27 (Sergi Pasoev) > ? 2. Re: Single key press (alex23) > ? 3. Re: Single key press (Steven D'Aprano) > ? 4. Re: struct calcsize discrepency? (Nobody) > ? 5. Re: Questions about LISP and Python. (Alex Mizrahi) > ? 6. Re: Backspace does not erase in stdout (Nobody) > ? 7. Re: Single key press (Hans Mulder) > ? 8. Re: Single key press (Nobody) > ? 9. Re: Scope of variable inside list comprehensions? (88888 Dihedral) > ?10. Re: Scope of variable inside list comprehensions? > ? ? ?(Jean-Michel Pichavant) > > > ---------- Message transf?r? ---------- > From:?Sergi Pasoev > To:?python-list at python.org > Date:?Tue, 6 Dec 2011 12:59:54 +0430 > Subject:?Re: Python-list Digest, Vol 99, Issue 27 > I do not want GUI, I just want a console application which will read a > single character > input. When you read from stdin for example, you have it to be terminated. > > 2011/12/6 ?: >> Send Python-list mailing list submissions to >> ? ? ? ?python-list at python.org >> >> To subscribe or unsubscribe via the World Wide Web, visit >> ? ? ? ?http://mail.python.org/mailman/listinfo/python-list >> or, via email, send a message with subject or body 'help' to >> ? ? ? ?python-list-request at python.org >> >> You can reach the person managing the list at >> ? ? ? ?python-list-owner at python.org >> >> When replying, please edit your Subject line so it is more specific >> than "Re: Contents of Python-list digest..." >> >> Today's Topics: >> >> ? 1. Re: Questions about LISP and Python. (Xah Lee) >> ? 2. Re: Questions about LISP and Python. (Chris Angelico) >> ? 3. Re: 70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when >> ? ? ?sending ? large object (Dennis Lee Bieber) >> ? 4. Single key press (Sergi Pasoev) >> ? 5. Re: Fwd: class print method... (Suresh Sharma) >> ? 6. Re: Single key press (88888 Dihedral) >> ? 7. Re: Single key press (88888 Dihedral) >> ? 8. Re: Scope of variable inside list comprehensions? (Rainer Grimm) >> ? 9. Re: Questions about LISP and Python. (alex23) >> ?10. Re: Questions about LISP and Python. (Matt Joiner) >> >> >> ---------- Message transf?r? ---------- >> From:?Xah Lee >> To:?python-list at python.org >> Date:?Mon, 5 Dec 2011 20:36:45 -0800 (PST) >> Subject:?Re: Questions about LISP and Python. >> On Dec 5, 4:31?am, Tim Bradshaw wrote: >>> On 2011-12-05 11:51:11 +0000, Xah Lee said: >>> >>> > python has more readible syntax, more modern computer language >>> > concepts, and more robust libraries. These qualities in turn made it >>> > popular. >>> >>> Yet you still post here: why? >> >> i don't like python, and i prefer emacs lisp. The primary reason is >> that python is not functional, especially with python 3. The python >> community is full of fanatics with their drivels. In that respect, >> it's not unlike Common Lisp community and Scheme lisp community. >> >> see also: >> >> ?Python Documentation Problems? >> http://xahlee.org/perl-python/python_doc_index.html >> >> ?Computer Language Design: What's List Comprehension and Why is It >> Harmful?? >> http://xahlee.org/comp/list_comprehension.html >> >> ?Lambda in Python 3000? >> http://xahlee.org/perl-python/python_3000.html >> >> ?What Languages to Hate? >> http://xahlee.org/UnixResource_dir/writ/language_to_hate.html >> >> ?Xah on Programing Languages? >> http://xahlee.org/Periodic_dosage_dir/comp_lang.html >> >> ?Xah >> >> >> >> ---------- Message transf?r? ---------- >> From:?Chris Angelico >> To:?python-list at python.org >> Date:?Tue, 6 Dec 2011 16:10:27 +1100 >> Subject:?Re: Questions about LISP and Python. >> On Tue, Dec 6, 2011 at 3:36 PM, Xah Lee wrote: >>> i don't like python, and i prefer emacs lisp. The primary reason is >>> that python is not functional, especially with python 3. The python >>> community is full of fanatics with their drivels. In that respect, >>> it's not unlike Common Lisp community and Scheme lisp community. >> >> So you hate Python. Fine. Why post here? Why not just abandon Python >> as a dead loss and go code in Lithp? >> >> Clearly something is keeping you here. Is it that there's something >> about Python that you really like, or are you just trolling? >> >> ChrisA >> >> >> >> ---------- Message transf?r? ---------- >> From:?Dennis Lee Bieber >> To:?python-list at python.org >> Date:?Mon, 05 Dec 2011 21:15:09 -0800 >> Subject:?Re: 70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when sending large object >> On Mon, 5 Dec 2011 18:49:44 +0100, DPalao >> wrote: >> >>>Obviously it was not me who added the disgusting "70% [* SPAM *]" string to >>>the subject. And I'd like to know the answer too. >>> >> ? ? ? ?Based upon your headers, somewhere your message went through a spam >> check program... >> >> X-Greylist: Sender succeeded SMTP AUTH, not delayed by >> milter-greylist-4.3.7 >> ? ? ? ?(monster.roma2.infn.it [141.108.255.100]); >> ? ? ? ?Mon, 05 Dec 2011 09:02:22 +0100 (CET) >> X-PMX-Version: 5.6.1.2065439, Antispam-Engine: 2.7.2.376379, >> ? ? ? ?Antispam-Data: 2011.12.5.74814 >> X-PMX-Spam: 70% >> X-PMX-Spam-report: The following antispam rules were triggered by this >> message: >> ? ? ? ?Rule ? ? ? ? ? ? ? ? ? ? ?Score Description >> ? ? ? ?RDNS_SUSP_FORGED_FROM ? ? 3.500 From domain appears to be forged, >> ? ? ? ?and arrived via a host with a known suspicious rDNS. >> ? ? ? ?SXL_IP_DYNAMIC 3.000 Received via a known dynamic IP (SXL lookup): >> ? ? ? ?34.140.47.93.fur >> ? ? ? ?FORGED_FROM_GMAIL ? ? ? ? 0.100 Appears to forge gmail in the from >> ? ? ? ?FROM_NAME_ONE_WORD ? ? ? ?0.050 Name in From header is a single word >> ? ? ? ?BODYTEXTP_SIZE_3000_LESS 0.000 Body size of the text/plain part is >> less >> ? ? ? ?than 3k >> ? ? ? ?BODY_SIZE_1500_1599 0.000 Message body size is 1500 to 1599 bytes >> ? ? ? ?BODY_SIZE_2000_LESS 0.000 Message body size is less than 2000 bytes. >> ? ? ? ?BODY_SIZE_5000_LESS 0.000 Message body size is less than 5000 bytes. >> ? ? ? ?BODY_SIZE_7000_LESS 0.000 Message body size is less than 5000 bytes. >> ? ? ? ?RDNS_GENERIC_POOLED 0.000 Sender's PTR record matches generic pooled >> -- >> ? ? ? ?Wulfraed ? ? ? ? ? ? ? ? Dennis Lee Bieber ? ? ? ? AF6VN >> ? ? ? ?wlfraed at ix.netcom.com ? ?HTTP://wlfraed.home.netcom.com/ >> >> >> >> >> ---------- Message transf?r? ---------- >> From:?Sergi Pasoev >> To:?python-list at python.org >> Date:?Tue, 6 Dec 2011 10:19:55 +0430 >> Subject:?Single key press >> Hi. >> >> I wonder if it is realistic to get a single key press in Python >> without ncurses or >> any similar library. In single key press I mean something like j and k >> in Gnu less >> program, you press the key and and it is captured by the script without need to >> press enter afterwards >> >> >> >> ---------- Message transf?r? ---------- >> From:?Suresh Sharma >> To:?Lie Ryan >> Date:?Tue, 6 Dec 2011 11:23:16 +0530 >> Subject:?Re: Fwd: class print method... >> Dave / Ryan >> Thanks i have got it and it worked after using repr statement. Thanks everyone for their valuable feedback. >> >> >> >> On Mon, Dec 5, 2011 at 6:11 PM, Lie Ryan wrote: >>> >>> On 12/05/2011 10:18 PM, Suresh Sharma wrote: >>>> >>>> >>>> Pls help its really frustrating >>>> ---------- Forwarded message ---------- >>>> From: Suresh Sharma >>>> Date: Monday, December 5, 2011 >>>> Subject: class print method... >>>> To: "d at davea.name " >>> > >>>> >>>> >>>> Dave, >>>> Thanx for the quick response, i am sorry that i did not explain >>>> correctly look at the code below inspite of this i am just getting class >>>> object at memory location.I am sort i typed all this code on my android >>>> in a hurry so.indentation could.not.be.managed but this.similar code >>>> when i run all my objects created by class deck are not shown but stored >>>> in varioia meory locations. How can i display them. >>>> >>> >>> I think you're in the right track, however I suspect you're running the code in the shell instead of as a script. The shell uses __repr__() to print objects instead of __str__(), so you either need to use 'print' or you need to call str(), note the following: >>> >>> Python 2.7.2+ (default, Oct ?4 2011, 20:06:09) >>> [GCC 4.6.1] on linux2 >>> Type "help", "copyright", "credits" or "license" for more information. >>> >>> suits = ['spades', 'clubs', 'diamonds', 'hearts'] >>> >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] >>> >>> class Card: >>> ... ? ? def __init__(self, rank, suit): >>> ... ? ? ? ? self.suit = suit >>> ... ? ? ? ? self.rank = rank >>> ... ? ? def __str__(self): >>> ... ? ? ? ? return suits[self.suit] + ' ' + ranks[self.rank] >>> ... >>> >>> Card(2, 3) #1 >>> <__main__.Card instance at 0x7f719c3a20e0> >>> >>> str(Card(2, 3)) #2 of your >>> 'hearts 3' >>> >>> print Card(2, 3) #3 >>> hearts 3 >>> >>> In #1, the output is the __repr__() of your Card class; you can modify this output by overriding the __repr__() on your Card class. >>> >>> In #2, the output is the __repr__() of a string, the string is the return value from __str__() of your Card class. The repr of a string is the string enclosed in quotes, which is why there is an extra pair of quotes. >>> >>> In #3, you're 'print'-ing a string, the string is the return value from __str__() of your Card class. There's no extra quotes, since 'print' prints the string itself, not the repr of the string. >>> >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> >> >> >> >> -- >> Suresh Sharma >> Regional Project Manager, >> O2F,Mumbai >> Maharashtra-400101. >> >> >> >> >> >> ---------- Message transf?r? ---------- >> From:?88888 Dihedral >> To:?comp.lang.python at googlegroups.com >> Date:?Mon, 5 Dec 2011 22:27:48 -0800 (PST) >> Subject:?Re: Single key press >> On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: >>> Hi. >>> >>> I wonder if it is realistic to get a single key press in Python >>> without ncurses or >>> any similar library. In single key press I mean something like j and k >>> in Gnu less >>> program, you press the key and and it is captured by the script without need to >>> press enter afterwards >> >> Sounds like the ?fast key searching for a list in the DOS application in the >> old days. >> >> This is easy in GUI of just tens of items, but for thousands of items such >> as in a directory listing, some API is really slow. >> >> >> >> >> >> ---------- Message transf?r? ---------- >> From:?88888 Dihedral >> To:?python-list at python.org >> Date:?Mon, 5 Dec 2011 22:27:48 -0800 (PST) >> Subject:?Re: Single key press >> On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote: >>> Hi. >>> >>> I wonder if it is realistic to get a single key press in Python >>> without ncurses or >>> any similar library. In single key press I mean something like j and k >>> in Gnu less >>> program, you press the key and and it is captured by the script without need to >>> press enter afterwards >> >> Sounds like the ?fast key searching for a list in the DOS application in the >> old days. >> >> This is easy in GUI of just tens of items, but for thousands of items such >> as in a directory listing, some API is really slow. >> >> >> >> >> >> ---------- Message transf?r? ---------- >> From:?Rainer Grimm >> To:?python-list at python.org >> Date:?Mon, 5 Dec 2011 22:42:35 -0800 (PST) >> Subject:?Re: Scope of variable inside list comprehensions? >> Hello, >> >>> ? ? try: >>> ? ? ? ? songs = [Song(id) for id in song_ids] >>> ? ? except Song.DoesNotExist: >>> ? ? ? ? print "unknown song id (%d)" % id >> that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world. It's only syntactic sugar for the functions map and filter. So functions have to be pure functions. To say it in other words, they have to be side-effect free. But the python construct from above pollutes the namespace with name id. >> >> Greetings from Rottenburg, >> Rainer >> >> >> >> ---------- Message transf?r? ---------- >> From:?alex23 >> To:?python-list at python.org >> Date:?Mon, 5 Dec 2011 23:02:42 -0800 (PST) >> Subject:?Re: Questions about LISP and Python. >> On Dec 6, 2:36?pm, Xah Lee wrote: >>> The python community is full of fanatics with their drivels. >> >> You do know that you could just fuck right off and leave us to it, >> yes? >> >> In general, it's the person who is shrilly imposing their minority >> opinion on a disinterested audience that deserves the title 'fanatic'. >> >> >> >> ---------- Message transf?r? ---------- >> From:?Matt Joiner >> To:?alex23 >> Date:?Tue, 6 Dec 2011 18:54:09 +1100 >> Subject:?Re: Questions about LISP and Python. >> This guy is an even better troll than that 88888 guy. His spelling is >> equally bad. His essays make some good points, but I don't see why he >> doesn't shut his trap and move on. >> >> ?_? >> >> >> >> On Tue, Dec 6, 2011 at 6:02 PM, alex23 wrote: >>> On Dec 6, 2:36?pm, Xah Lee wrote: >>>> The python community is full of fanatics with their drivels. >>> >>> You do know that you could just fuck right off and leave us to it, >>> yes? >>> >>> In general, it's the person who is shrilly imposing their minority >>> opinion on a disinterested audience that deserves the title 'fanatic'. >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> >> >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > > ---------- Message transf?r? ---------- > From:?alex23 > To:?python-list at python.org > Date:?Tue, 6 Dec 2011 00:39:53 -0800 (PST) > Subject:?Re: Single key press > On Dec 6, 3:49?pm, Sergi Pasoev wrote: >> I wonder if it is realistic to get a single key press in Python >> without ncurses or any similar library. > > It's possible using Tkinter in the standard library: > > http://www.daniweb.com/software-development/python/code/216830 > > > > > ---------- Message transf?r? ---------- > From:?Steven D'Aprano > To:?python-list at python.org > Date:?06 Dec 2011 08:48:39 GMT > Subject:?Re: Single key press > On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: > >> Hi. >> >> I wonder if it is realistic to get a single key press in Python without >> ncurses or >> any similar library. In single key press I mean something like j and k >> in Gnu less >> program, you press the key and and it is captured by the script without >> need to press enter afterwards > > > Coincidentally, I was working on that exact function last night, so you > inspired me to put it up on the Python cookbook. This version should work > on any POSIX system such as Linux, as well as Windows. I don't know if it > will work on Macintosh. > > http://code.activestate.com/recipes/577977-get-single-keypress/ > > > > -- > Steven > > > > ---------- Message transf?r? ---------- > From:?Nobody > To:?python-list at python.org > Date:?Tue, 06 Dec 2011 08:55:16 +0000 > Subject:?Re: struct calcsize discrepency? > On Mon, 05 Dec 2011 00:20:32 -0800, Mark Dickinson wrote: > >>> May be, yes, but since calcsize() is returning 12 when the elements >>> are put in the other order, it would seem to be not counting such >>> padding. >> >> Indeed. ?That's arguably a bug in the struct module, > > There's no "arguably" about it. The documentation says: > > ? ? ? ?Native size and alignment are determined using the C compiler?s sizeof > ? ? ? ?expression. > > But given: > > ? ? ? ?struct { unsigned long long a; char b[4]; } foo; > ? ? ? ?struct { char b[4]; unsigned long long a; } bar; > > sizeof(foo) will always equal sizeof(bar). If long long is 8 bytes and has > 8-byte alignment, both will be 16. > > If you want consistency with the in-memory representation used by > C/C++ programs (and the on-disk representation used by C/C++ programs > which write the in-memory representation directly to file), use ctypes; > e.g.: > > ? ? ? ?>>> from ctypes import * > ? ? ? ?>>> class foo(Structure): > ? ? ? ? ? ? ? ?_fields_ = [ > ? ? ? ? ? ? ? ? ? ? ? ?("a", c_ulonglong), > ? ? ? ? ? ? ? ? ? ? ? ?("b", c_char * 4)] > > ? ? ? ?>>> sizeof(foo) > ? ? ? ?16 > > > > > ---------- Message transf?r? ---------- > From:?Alex Mizrahi > To:?python-list at python.org > Date:?Tue, 06 Dec 2011 11:12:31 +0200 > Subject:?Re: Questions about LISP and Python. >> >> i don't like python, and i prefer emacs lisp. The primary reason is >> that python is not functional, especially with python 3. The python >> community is full of fanatics with their drivels. In that respect, >> it's not unlike Common Lisp community and Scheme lisp community. > > > What functional programming features do you use, Xah? Last time I've checked, you didn't care about closures. In this respect, Python is better at functional programming than Emacs Lisp. > > Do you just like writing arbitrarily long expression? Or you think that eval is functional? > > > > ---------- Message transf?r? ---------- > From:?Nobody > To:?python-list at python.org > Date:?Tue, 06 Dec 2011 09:27:29 +0000 > Subject:?Re: Backspace does not erase in stdout > On Mon, 05 Dec 2011 16:23:55 +0000, Grant Edwards wrote: > >>> Emitting "\b \b" is one very common way to do a destructive backspace. >>> Inelegant? Perhaps, but a common inelegance. >> >> That's pretty much the only way I've seen it done for the past 25 >> years. > > ... before which, it was BS-DEL-BS. > > DEL being 127 means that it punches through all of the holes on the paper > tape, causing whatever was there previously to be replaced with a DEL > character. BS-SPC-BS would only punch through one of the holes. > > > > > ---------- Message transf?r? ---------- > From:?Hans Mulder > To:?python-list at python.org > Date:?Tue, 06 Dec 2011 10:28:32 +0100 > Subject:?Re: Single key press > On 6/12/11 09:48:39, Steven D'Aprano wrote: >> >> On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: >> >>> Hi. >>> >>> I wonder if it is realistic to get a single key press in Python without >>> ncurses or >>> any similar library. In single key press I mean something like j and k >>> in Gnu less >>> program, you press the key and and it is captured by the script without >>> need to press enter afterwards >> >> >> >> Coincidentally, I was working on that exact function last night, so you >> inspired me to put it up on the Python cookbook. This version should work >> on any POSIX system such as Linux, as well as Windows. I don't know if it >> will work on Macintosh. >> >> http://code.activestate.com/recipes/577977-get-single-keypress/ > > > It works fine on MacOS/X, but probably not on MacOS Classic. > > -- HansM > > > > > > > ---------- Message transf?r? ---------- > From:?Nobody > To:?python-list at python.org > Date:?Tue, 06 Dec 2011 09:48:55 +0000 > Subject:?Re: Single key press > On Tue, 06 Dec 2011 10:19:55 +0430, Sergi Pasoev wrote: > >> I wonder if it is realistic to get a single key press in Python >> without ncurses or any similar library. > > On Unix, you need to use termios.tcsetattr() to disable "canonical mode". > Otherwise, the tty driver will only pass data up to the application when > the user enters a newline or EOF. > > If you do this, you also need to handle SIGTSTP and SIGCONT, restoring the > original terminal settings on SIGTSTP (typically generated by Ctrl-Z) and > restoring the application's setting on SIGCONT. > > > > > ---------- Message transf?r? ---------- > From:?88888 Dihedral > To:?python-list at python.org > Date:?Tue, 6 Dec 2011 02:16:42 -0800 (PST) > Subject:?Re: Scope of variable inside list comprehensions? > On Tuesday, December 6, 2011 2:42:35 PM UTC+8, Rainer Grimm wrote: >> Hello, >> >> > ? ? try: >> > ? ? ? ? songs = [Song(id) for id in song_ids] >> > ? ? except Song.DoesNotExist: >> > ? ? ? ? print "unknown song id (%d)" % id >> that's is a bad programming style. So it will be forbidden with python 3. The reason is that list comprehension is a construct from the functional world. It's only syntactic sugar for the functions map and filter. So functions have to be pure functions. To say it in other words, they have to be side-effect free. But the python construct from above pollutes the namespace with name id. >> >> Greetings from Rottenburg, >> Rainer > > The list might have to grow in a careless way that might lead to a crash > in the for inside a list that can't be trapped for errors directly. > > > > > ---------- Message transf?r? ---------- > From:?Jean-Michel Pichavant > To:?Steven D'Aprano > Date:?Tue, 06 Dec 2011 11:38:07 +0100 > Subject:?Re: Scope of variable inside list comprehensions? > Steven D'Aprano wrote: >> >> On Mon, 05 Dec 2011 19:57:15 +0100, Jean-Michel Pichavant wrote: >> >> >>> >>> The proper way to propagate information with exceptions is using the >>> exception itself: >>> >>> try: >>> ? ?songs = [Song(_id) for _id in song_ids] >>> except Song.DoesNotExist, exc: >>> ? ?print exc >>> >> >> >> >> I'm not entirely sure that this is the proper way to propagate the exception. I see far to many people catching exceptions to print them, or worse, to print a generic, useless message like "an error occurred". >> > > [snip] > > You misread me, I was referering to passing *information* with exception (in other words, use the exception attributes). In the example I gave, the exception has the songId value responsible for raising the error. > I totaly second your opinion on how poor the above handler is (hmm not sure about this grammar construct, it sounds like a Yoda sentence). > > JM > > > -- > http://mail.python.org/mailman/listinfo/python-list From s.pasoev at gmail.com Tue Dec 6 12:55:29 2011 From: s.pasoev at gmail.com (Sergi Pasoev) Date: Tue, 6 Dec 2011 22:25:29 +0430 Subject: 2 or 3 ? Message-ID: Yes indeed, use the newer version. I thought this problem was already solved, Python 3 has already been there for long enough time. From ian.g.kelly at gmail.com Tue Dec 6 13:12:14 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Dec 2011 11:12:14 -0700 Subject: Referring to the class name from a class variable where inheritance is involved In-Reply-To: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> References: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> Message-ID: On Tue, Dec 6, 2011 at 8:57 AM, Paul Moore wrote: > I want to set up an inheritance hierarchy. The base class will define > a string value which should include the class name, but I don't want > people who inherit from my class to have to remember to override the > value. > > If I do this using an instance variable, it's reasonably easy: > >>>> class Base: > ... ? ? def __init__(self): > ... ? ? ? ? self.key = 'Key_for_' + self.__class__.__name__ > ... ? ? def display(self): > ... ? ? ? ? print self.key > ... >>>> class Inherited(Base): > ... ? ? pass > ... >>>> b = Base() >>>> i = Inherited() >>>> b.display() > Key_for_Base >>>> i.display() > Key_for_Inherited > > Rather than having the key for every instance, I'd like to use a class > variable, but I can't see how I'd make that work (a class variable > which is inherited but has a different value in derived classes). I > could use a classmethod,but that feels like even more overkill than an > instance attribute. > > Is there a way of doing this via class variables or something, or more > relevantly, I guess, what would be the idiomatic way of doing > something like this? How about a class property? class classproperty(object): def __init__(self, fget): self.__fget = fget def __get__(self, instance, owner): return self.__fget(owner) class BaseClass(object): @classproperty def key(cls): return "Key_for_" + cls.__name__ class DerivedClass(BaseClass): pass assert BaseClass.key == BaseClass().key == "Key_for_BaseClass" assert DerivedClass.key == DerivedClass().key == "Key_for_DerivedClass" If you like, you can also expand classproperty to allow setters and deleters like property does. Cheers, Ian From ian.g.kelly at gmail.com Tue Dec 6 13:21:39 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 6 Dec 2011 11:21:39 -0700 Subject: Referring to the class name from a class variable where inheritance is involved In-Reply-To: References: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> Message-ID: On Tue, Dec 6, 2011 at 11:12 AM, Ian Kelly wrote: > If you like, you can also expand classproperty to allow setters and > deleters like property does. My mistake, you can't do this. __set__ and __delete__ are only invoked on instances, not on the class. Get-only class properties are fine, though. From python.list at tim.thechases.com Tue Dec 6 13:29:30 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 06 Dec 2011 12:29:30 -0600 Subject: Python-list Digest, Vol 99, Issue 28 In-Reply-To: References: Message-ID: <4EDE5F0A.90200@tim.thechases.com> 12/06/11 11:34, Sergi Pasoev wrote: (after trimming all the digest rubbish the way you should have done in the first place) >> Coincidentally, I was working on that exact function last >> night, so you inspired me to put it up on the Python >> cookbook. This version should work on any POSIX system such >> as Linux, as well as Windows. I don't know if it will work >> on Macintosh. >> >> http://code.activestate.com/recipes/577977-get-single-keypress/ > > Thanks Steven. Maybe you mean Gnu/Linux when you say Linux. Sheesh. The guy hands you free code to do what you ask, and 99% of your reply is uselessly-quoted digest, 0.8% is complaining about his terminology, and 0.1% is saying thanks. 1) it's the POSIX API in question here, nothing particularly GNU about the Linux implementation of it. 2) if you want to be pedantic about it, you should really be calling such systems something like Xorg/[Gnome|KDE|Fluxbox|LXDE|OpenBox|BlackBox|FVWM|XFCE]/Linux/GNU/Mozilla/TeX/MySQL/Postgresql/sqlite/git/CVS/Subversion/Bazaar/Mercurial/Python/Perl/PHP/Apache. And why don't you call it that? Because it's a pain, and if you say "Linux" everybody knows that you're talking about an operating system that uses Linux and has other contributions in it. So I'm pretty sure that Steven did *not* mean to type "GNU/Linux" or he would have typed it that way. :) -tkc From p.f.moore at gmail.com Tue Dec 6 13:53:03 2011 From: p.f.moore at gmail.com (Paul Moore) Date: Tue, 6 Dec 2011 18:53:03 +0000 Subject: Referring to the class name from a class variable where inheritance is involved In-Reply-To: References: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> Message-ID: On 6 December 2011 18:12, Ian Kelly wrote: > How about a class property? > > class classproperty(object): > ? ?def __init__(self, fget): > ? ? ? ?self.__fget = fget > ? ?def __get__(self, instance, owner): > ? ? ? ?return self.__fget(owner) Nice :-) About as heavyweight as a classmethod, though, so it doesn't buy me much over that (beyond avoiding a set of parens, which isn't a big deal). Either classmethod or classproperty gives me the key benefit I'm after, which is avoiding the per-instance overhead, though. TBH, this is a huge case of premature optimisation, though, so it's probably little more than an academic or style question. :-) Paul From p.f.moore at gmail.com Tue Dec 6 13:59:48 2011 From: p.f.moore at gmail.com (Paul Moore) Date: Tue, 6 Dec 2011 10:59:48 -0800 (PST) Subject: Referring to the class name from a class variable where inheritance is involved References: <6519de1f-9648-4983-8914-bcff85e96005@e2g2000vbb.googlegroups.com> Message-ID: <5b66b2e5-1ec6-4804-8429-15be3b9c0197@i8g2000vbh.googlegroups.com> On Dec 6, 5:29?pm, Matt Saxton wrote: > You can use a metaclass for this: > > ?>>> class BaseMeta(type): > ... ? ? def __new__(mcs, name, bases, dict): > ... ? ? ? ? dict['key'] = 'Key_for_%s' % name > ... ? ? ? ? return type.__new__(mcs, name, bases, dict) > ... > ?>>> class Base: > ... ? ? __metaclass__ = BaseMeta > ... > ?>>> class Inherited(Base): > ... ? ? pass > ... > ?>>> Base.key > 'Key_for_Base' > ?>>> Inherited.key > 'Key_for_Inheritor' I like that! I tend to think of metaclasses as for "complicated" stuff. It's nice to see a simple use. Paul From ladasky at my-deja.com Tue Dec 6 14:13:19 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 6 Dec 2011 11:13:19 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? Message-ID: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> Hi, folks, Back in 2002, I got back into programming after a nine-year hiatus. I needed a new programming language, was guided to Python 2.2, and was off to the races. I chose the SciTE program editor, and I have been using it ever since. I'm now using Python 2.6 on Ubuntu Linux 10.10. My programming needs have grown more sophisticated, but I'm still using SciTE. Pretty much all of my recent posts to comp.lang.python have concerned multiprocessing. I put together a decent system for my current project, and had it all working. Then I realized that I needed to refactor and expand some code, which I did -- and somehow, I generated a bug that I simply cannot understand. I've been puzzling over it for three days. The error is occurring inside one of my subprocesses. As far as I know, SciTE is limited in what it can do in this situation. The program does not return when a subprocess generates an exception. I see the error message, but then the program simply hangs. I have tried invoking the subprocess directly without scheduling it through multiprocessing.Pool. It works fine. So the problem is occurring inside Pool. I tried opening my code in IDLE, and figured I could step through it, or at least call functions one line at a time. It appears that multiprocessing code is not compatible with IDLE. IDLE simply crashes when I try to invoke any of the important functions. I know, you want me to post a minimal example. Most of the time, that's possible, and I do it. Trust me, this time it isn't. I have about 500 lines of code, split across three files. These implement a neural network, some test data, and multiprocessing methods for network evaluation. I made several concerted changes to the code, and turned a working system into this: ============================================= Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner self.run() File "/usr/lib/python2.6/threading.py", line 484, in run self.__target(*self.__args, **self.__kwargs) File "/usr/lib/python2.6/multiprocessing/pool.py", line 225, in _handle_tasks put(task) TypeError: expected string or Unicode object, NoneType found ============================================= Here's what I think would help me debug this error: I would like to catch the TypeError, and examine the contents of task. I need to accomplish this WITHOUT adding a try...except block to the Python library file multiprocessing/pool.py. I don't know whether this is possible, because the traceback isn't clear about where my OWN code calls the code which is generating the error. After that, if the cause of the error still is not obvious, I might need to go back to the working program. Somehow I want to examine the contents of task when the program works, and no TypeError is being generated. By comparing the two, I hope to see a difference. From that, I should be able to figure out how I have broken what is being fed to Pool.__init__ and/or MapResult.__init__. Any suggestions how I might best accomplish this task? Does this error message look familiar to anyone? More generally, should I consider graduating from SciTE? I have had a look at a few of the more comprehensive IDE's over the years, and I'll have to say that I found them to be intimidating. I found it to be a huge chore just to open a single Python script and run it inside an IDE. It seems like you had to know how to set up a complete, multi- script project before you could even accomplish simple tasks. That steep learning curve is the reason that I didn't choose Java as my programming language. So, if any of you have pertinent recommendations in the IDE department, please feel free to guide me that way. Thanks! From ladasky at my-deja.com Tue Dec 6 14:19:55 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 6 Dec 2011 11:19:55 -0800 (PST) Subject: Clever hack or code abomination? References: Message-ID: On Dec 1, 12:21?am, Chris Angelico wrote: > On Thu, Dec 1, 2011 at 2:15 PM, Roy Smith wrote: > That's a self-contained piece of code.If I came upon it, I'd probably > copy and paste it to IDLE, see what it comes up with, and proceed from > there. +1. That was going to be my comment exactly. From ladasky at my-deja.com Tue Dec 6 14:27:29 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 6 Dec 2011 11:27:29 -0800 (PST) Subject: why is bytearray treated so inefficiently by pickle? References: <4ed24a33$0$6869$e4fe514c@news2.news.xs4all.nl> Message-ID: <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> On a related note, pickling of arrays of float64 objects, as generated by the numpy package for example, are wildly inefficient with memory. A half-million float64's requires about 4 megabytes, but the pickle file I generated from a numpy.ndarray of this size was 42 megabytes. I know that numpy has its own pickle protocol, and that it's supposed to help with this problem. Still, if this is a general problem with Python and pickling numbers, it might be worth solving it in the language itself. From irmen at -NOSPAM-xs4all.nl Tue Dec 6 14:44:02 2011 From: irmen at -NOSPAM-xs4all.nl (Irmen de Jong) Date: Tue, 06 Dec 2011 20:44:02 +0100 Subject: why is bytearray treated so inefficiently by pickle? In-Reply-To: <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> References: <4ed24a33$0$6869$e4fe514c@news2.news.xs4all.nl> <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> Message-ID: <4ede7082$0$6925$e4fe514c@news2.news.xs4all.nl> On 06-12-11 20:27, John Ladasky wrote: > On a related note, pickling of arrays of float64 objects, as generated > by the numpy package for example, are wildly inefficient with memory. > A half-million float64's requires about 4 megabytes, but the pickle > file I generated from a numpy.ndarray of this size was 42 megabytes. > > I know that numpy has its own pickle protocol, and that it's supposed > to help with this problem. Still, if this is a general problem with > Python and pickling numbers, it might be worth solving it in the > language itself. Python provides ample ways for custom types to influence the way they're pickled (getstate/setstate, reduce). Are numpy's arrays are pickled similar to Python's own array types? In that case, when using Python 2.x, they're pickled very inefficiently indeed (every element is encoded with its own token). In Python 3.x, array pickling is very efficient because it stores the machine type representation in the pickle. Irmen From robert.kern at gmail.com Tue Dec 6 15:25:02 2011 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 06 Dec 2011 20:25:02 +0000 Subject: why is bytearray treated so inefficiently by pickle? In-Reply-To: <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> References: <4ed24a33$0$6869$e4fe514c@news2.news.xs4all.nl> <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> Message-ID: On 12/6/11 7:27 PM, John Ladasky wrote: > On a related note, pickling of arrays of float64 objects, as generated > by the numpy package for example, are wildly inefficient with memory. > A half-million float64's requires about 4 megabytes, but the pickle > file I generated from a numpy.ndarray of this size was 42 megabytes. > > I know that numpy has its own pickle protocol, and that it's supposed > to help with this problem. Still, if this is a general problem with > Python and pickling numbers, it might be worth solving it in the > language itself. It is. Use protocol=HIGHEST_PROTOCOL when dumping the array to a pickle. [~] |1> big = np.linspace(0.0, 1.0, 500000) [~] |2> import cPickle [~] |3> len(cPickle.dumps(big)) 11102362 [~] |4> len(cPickle.dumps(big, protocol=cPickle.HIGHEST_PROTOCOL)) 4000135 The original conception for pickle was that it would have an ASCII representation for optimal cross-platform compatibility. These were the days when people still used FTP regularly, and you could easily (and silently!) screw up binary data if you sent it in ASCII mode by accident. This necessarily creates large files for numpy arrays. Further iterations on the pickling protocol let numpy use raw binary data in the pickle. However, for backwards compatibility, the default protocol is the one Python started out with. If you explicitly use the most recent protocol, then you will get the efficiency benefits. -- 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 From nawijn at gmail.com Tue Dec 6 15:49:46 2011 From: nawijn at gmail.com (Marco Nawijn) Date: Tue, 6 Dec 2011 12:49:46 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> Message-ID: <1b100b62-9e1c-46b0-a453-9d73c1104c31@y12g2000vba.googlegroups.com> On Dec 6, 8:13?pm, John Ladasky wrote: > Hi, folks, > > Back in 2002, I got back into programming after a nine-year hiatus. ?I > needed a new programming language, was guided to Python 2.2, and was > off to the races. ?I chose the SciTE program editor, and I have been > using it ever since. ?I'm now using Python 2.6 on Ubuntu Linux 10.10. > > My programming needs have grown more sophisticated, but I'm still > using SciTE. ?Pretty much all of my recent posts to comp.lang.python > have concerned multiprocessing. ?I put together a decent system for my > current project, and had it all working. ?Then I realized that I > needed to refactor and expand some code, which I did -- and somehow, I > generated a bug that I simply cannot understand. ?I've been puzzling > over it for three days. > > The error is occurring inside one of my subprocesses. ?As far as I > know, SciTE is limited in what it can do in this situation. ?The > program does not return when a subprocess generates an exception. ?I > see the error message, but then the program simply hangs. > > I have tried invoking the subprocess directly without scheduling it > through multiprocessing.Pool. ?It works fine. ?So the problem is > occurring inside Pool. > > I tried opening my code in IDLE, and figured I could step through it, > or at least call functions one line at a time. ?It appears that > multiprocessing code is not compatible with IDLE. ?IDLE simply crashes > when I try to invoke any of the important functions. > > I know, you want me to post a minimal example. ?Most of the time, > that's possible, and I do it. ?Trust me, this time it isn't. ?I have > about 500 lines of code, split across three files. ?These implement a > neural network, some test data, and multiprocessing methods for > network evaluation. ?I made several concerted changes to the code, and > turned a working system into this: > > ============================================= > > Exception in thread Thread-1: > Traceback (most recent call last): > ? File "/usr/lib/python2.6/threading.py", line 532, in > __bootstrap_inner > ? ? self.run() > ? File "/usr/lib/python2.6/threading.py", line 484, in run > ? ? self.__target(*self.__args, **self.__kwargs) > ? File "/usr/lib/python2.6/multiprocessing/pool.py", line 225, in > _handle_tasks > ? ? put(task) > TypeError: expected string or Unicode object, NoneType found > > ============================================= > > Here's what I think would help me debug this error: I would like to > catch the TypeError, and examine the contents of task. ?I need to > accomplish this WITHOUT adding a try...except block to the Python > library file multiprocessing/pool.py. ?I don't know whether this is > possible, because the traceback isn't clear about where my OWN code > calls the code which is generating the error. > > After that, if the cause of the error still is not obvious, I might > need to go back to the working program. ?Somehow I want to examine the > contents of task when the program works, and no TypeError is being > generated. ?By comparing the two, I hope to see a difference. ?From > that, I should be able to figure out how I have broken what is being > fed to Pool.__init__ and/or MapResult.__init__. > > Any suggestions how I might best accomplish this task? ?Does this > error message look familiar to anyone? > > More generally, should I consider graduating from SciTE? ?I have had a > look at a few of the more comprehensive IDE's over the years, and I'll > have to say that I found them to be intimidating. ?I found it to be a > huge chore just to open a single Python script and run it inside an > IDE. ?It seems like you had to know how to set up a complete, multi- > script project before you could even accomplish simple tasks. ?That > steep learning curve is the reason that I didn't choose Java as my > programming language. > > So, if any of you have pertinent recommendations in the IDE > department, please feel free to guide me that way. > > Thanks! Hello John, One way of trying to debug the issue could be to use ipython and ipdb. You cadn than run your code from within the ipython shell. The debugger will hold at the type error, but keep the context. At this point you should be able to evaluate task. As a side comment to your IDE remarks. I keep switching between VIM and Aptana/Pydev. The more I learn about VIM the more I feel comfortable and productive. In combination with ipython it is quite a solid development environment. On the other hand Pydev is very user friendly, powerfull and easy to learn. Debugging in Pydev is excellent. Regards, Marco From tjreedy at udel.edu Tue Dec 6 16:42:26 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Dec 2011 16:42:26 -0500 Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? In-Reply-To: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> Message-ID: On 12/6/2011 2:13 PM, John Ladasky wrote: > Exception in thread Thread-1: > Traceback (most recent call last): > File "/usr/lib/python2.6/threading.py", line 532, in > __bootstrap_inner > self.run() > File "/usr/lib/python2.6/threading.py", line 484, in run > self.__target(*self.__args, **self.__kwargs) > File "/usr/lib/python2.6/multiprocessing/pool.py", line 225, in > _handle_tasks > put(task) > TypeError: expected string or Unicode object, NoneType found > > ============================================= > > Here's what I think would help me debug this error: I would like to > catch the TypeError, and examine the contents of task. The traceback says that it is None, which has no contents ;=). > I need to > accomplish this WITHOUT adding a try...except block to the Python > library file multiprocessing/pool.py. I do not understand this statement. You should feel free to make a backup copy of pool.py and then modify it for debugging. > I don't know whether this is > possible, because the traceback isn't clear about where my OWN code > calls the code which is generating the error. It appears to be threading trying to call your code, and failing, that is the problem. But I do not know how threading, multiprocessing.pool, and your code are supposed to interact. > After that, if the cause of the error still is not obvious, I might > need to go back to the working program. Somehow I want to examine the > contents of task when the program works, and no TypeError is being > generated. The traceback says that it is a string. I would start with the line that fails 'put(task)', and work backwards to see where 'task' comes from and how it could become None. It is even possible that multiprocessing.pool has a bug that you ran into. -- Terry Jan Reedy From tjreedy at udel.edu Tue Dec 6 17:11:45 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Dec 2011 17:11:45 -0500 Subject: the deceptive continuous assignments In-Reply-To: References: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> Message-ID: On 12/6/2011 7:33 AM, Chris Angelico wrote: > On Tue, Dec 6, 2011 at 11:20 PM, Terry Reedy wrote: >> You found an unsafe overlap. >> x.thing = x = 1 >> would work, though it seems strange (and unlikely in practice) to rebind x >> to an int after it is bound to a class k instance. > > This code is starting to look like it wants to work with a linked list. > >>>> class node: > def __init__(self,x): > self.payload=x > self.next=None > def walk(self): > print("My payload is: "+self.payload) > if self.next: self.next.walk() > >>>> head=tail=node("This") >>>> tail.next=tail=node("is") >>>> tail.next=tail=node("a") >>>> tail.next=tail=node("test.") >>>> head.walk() > My payload is: This > My payload is: is > My payload is: a > My payload is: test. Cute. I am used to linked lists being built from the bottem up in functional languages with immutable nodes. I might even use something like this. Of course, for a list of any length, walk needs to be iterative. def walk(self): while self: print("My payload is: "+self.payload) self = self.next -- Terry Jan Reedy From rosuav at gmail.com Tue Dec 6 17:20:50 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Dec 2011 09:20:50 +1100 Subject: the deceptive continuous assignments In-Reply-To: References: <1323169564.63710.YahooMailNeo@web121505.mail.ne1.yahoo.com> Message-ID: On Wed, Dec 7, 2011 at 9:11 AM, Terry Reedy wrote: > Cute. I am used to linked lists being built from the bottem up in functional > languages with immutable nodes. I might even use something like this. Of > course, for a list of any length, walk needs to be iterative. > > ? ?def walk(self): > ? ? ? ?while self: > > ? ? ? ? ? ?print("My payload is: "+self.payload) > ? ? ? ? ? ?self = self.next Of course, but I was coding top-down and didn't feel like going back to add the "while" :) ChrisA From tjreedy at udel.edu Tue Dec 6 17:24:01 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Dec 2011 17:24:01 -0500 Subject: why is bytearray treated so inefficiently by pickle? In-Reply-To: <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> References: <4ed24a33$0$6869$e4fe514c@news2.news.xs4all.nl> <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> Message-ID: On Nov 30, Irmen de Jong opened a tracker issue with a patch improve bytearray pickling. http://bugs.python.org/issue13503 Yesterday, Dec 5, Antoine Pitrou applied a revised fix. http://hg.python.org/cpython/rev/e2959a6a1440/ The commit message: "Issue #13503: Use a more efficient reduction format for bytearrays with pickle protocol >= 3. The old reduction format is kept with older protocols in order to allow unpickling under Python 2." -- Terry Jan Reedy From ladasky at my-deja.com Tue Dec 6 19:17:23 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 6 Dec 2011 16:17:23 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1b100b62-9e1c-46b0-a453-9d73c1104c31@y12g2000vba.googlegroups.com> Message-ID: Thanks, Marco. I've noticed that the matplotlib reference manual recommends ipython. I haven't been clear what its advantages are, but if interacting with multiprocessing correctly is one of them, I'll try it. If ipython does everything that IDLE does and more, why is IDLE still shipped with Python anyway? I'll follow up on your IDE recommendations too after trying ipython. From anacrolix at gmail.com Tue Dec 6 19:26:54 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Wed, 7 Dec 2011 11:26:54 +1100 Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? In-Reply-To: References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1b100b62-9e1c-46b0-a453-9d73c1104c31@y12g2000vba.googlegroups.com> Message-ID: John I'm in a similar position. I've been using Geany for 2+ years and haven't found anything to replace it. Either the replacement tool makes it too difficult to work with Python correctly, or I spend more time trying to understand it, rather than getting the job done. I also use vim on occasion when GUI isn't an option. I seem to do okay, so I'm not sure you're at any disadvantage. A stand alone graphical debugger would be handy tho... On Wed, Dec 7, 2011 at 11:17 AM, John Ladasky wrote: > Thanks, Marco. > > I've noticed that the matplotlib reference manual recommends ipython. > I haven't been clear what its advantages are, but if interacting with > multiprocessing correctly is one of them, I'll try it. > > If ipython does everything that IDLE does and more, why is IDLE still > shipped with Python anyway? > > I'll follow up on your IDE recommendations too after trying ipython. > -- > http://mail.python.org/mailman/listinfo/python-list -- ?_? From ladasky at my-deja.com Tue Dec 6 19:30:16 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 6 Dec 2011 16:30:16 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> Message-ID: <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> On Dec 6, 1:42?pm, Terry Reedy wrote: > On 12/6/2011 2:13 PM, John Ladasky wrote: > > Exception in thread Thread-1: > > Traceback (most recent call last): > > ? ?File "/usr/lib/python2.6/threading.py", line 532, in > > __bootstrap_inner > > ? ? ?self.run() > > ? ?File "/usr/lib/python2.6/threading.py", line 484, in run > > ? ? ?self.__target(*self.__args, **self.__kwargs) > > ? ?File "/usr/lib/python2.6/multiprocessing/pool.py", line 225, in > > _handle_tasks > > ? ? ?put(task) > > TypeError: expected string or Unicode object, NoneType found > > > ============================================= > > > Here's what I think would help me debug this error: I would like to > > catch the TypeError, and examine the contents of task. > > The traceback says that it is None, which has no contents ;=). I'm not sure about that. I don't submit the task variable, it's something that Pool builds from what I submit. Is task == None when it should be a string? Or, is task an iterable which contains one element which should be a string? And what's supposed to be in that string anyway? My first reading of the source code of Pool didn't make this clear to me. Also, I've noticed that tracebacks from subprocesses are less informative than tracebacks from the parent process. What's missing? Finally, I also recall that multiprocessing invokes pickle to pass data between processes (I still don't understand the need for pickle here). This suggests to me that the string MIGHT contain the pickled code of the method I want to run in the subprocess. But I'm not sure, until I can actually examine the task variable in my working version. > > I need to > > accomplish this WITHOUT adding a try...except block to the Python > > library file multiprocessing/pool.py. > > I do not understand this statement. You should feel free to make a > backup copy of pool.py and then modify it for debugging. Right, so, the last time I tried this with a piece of library code, I ran into some major headaches with import statements. I suppose I could have a look at Pool and see whether it can be extracted cleanly and made to run. > ?> I don't know whether this is > > > possible, because the traceback isn't clear about where my OWN code > > calls the code which is generating the error. > > It appears to be threading trying to call your code, and failing, that > is the problem. But I do not know how threading, multiprocessing.pool, > and your code are supposed to interact. It might be that pickle has somehow managed to pass a null code string to the subprocess, so it has nothing to run. > > After that, if the cause of the error still is not obvious, I might > > need to go back to the working program. ?Somehow I want to examine the > > contents of task when the program works, and no TypeError is being > > generated. > > The traceback says that it is a string. Yes, again... I want to know what that string is supposed to DO. > I would start with the line that fails 'put(task)', and work backwards > to see where 'task' comes from and how it could become None. It is even > possible that multiprocessing.pool has a bug that you ran into. Oh, please don't say that. I'm no computer scientist, and Python has been scrutinized by so many professionals. I couldn't have possibly found a language bug. From tjreedy at udel.edu Tue Dec 6 20:34:37 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 06 Dec 2011 20:34:37 -0500 Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? In-Reply-To: <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> Message-ID: On 12/6/2011 7:30 PM, John Ladasky wrote: > On Dec 6, 1:42 pm, Terry Reedy wrote: >> On 12/6/2011 2:13 PM, John Ladasky wrote: >>> I need to >>> accomplish this WITHOUT adding a try...except block to the Python >>> library file multiprocessing/pool.py. >> >> I do not understand this statement. You should feel free to make a >> backup copy of pool.py and then modify it for debugging. > > Right, so, the last time I tried this with a piece of library code, I > ran into some major headaches with import statements. I suppose I > could have a look at Pool and see whether it can be extracted cleanly > and made to run. I have patched files both in /Lib and /Lib/idle on Windows with no problems except that I had to switch to admin account to make the patch. I probably changed 'copy of x.py' to either 'x.bak' or 'x.py.bak' but I do not remember. Deleting .pyc might or might not help. For a file in Lib, I have also copied to the working directory with my script, which gets prepended to sys.path. This makes restoring the default easier. One would have to copy all of multiprocessing/ for that to work with m.../pool.py. -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Tue Dec 6 22:44:19 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Dec 2011 03:44:19 GMT Subject: Python-list Digest, Vol 99, Issue 28 References: Message-ID: <4edee113$0$30003$c3e8da3$5496439d@news.astraweb.com> On Tue, 06 Dec 2011 22:04:36 +0430, Sergi Pasoev wrote: > Thanks Steven. Maybe you mean Gnu/Linux when you say Linux. Maybe I do, maybe I don't. Since you're replying to a digest and neither adjusted the subject line nor trimmed almost a dozen pages of unnecessary quoted text, I have no idea what you are referring to, or whether you are replying to me or some other Steven. -- Steven but perhaps not the right Steven From wuwei23 at gmail.com Tue Dec 6 22:52:23 2011 From: wuwei23 at gmail.com (alex23) Date: Tue, 6 Dec 2011 19:52:23 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: Rick Johnson wrote: > Anyone who would take the time to write *articulate* suggestions for > grave deficiencies in the Python documentation must care. And you're including yourself here by implication, yes? You have _zero_ desire to see any real improvements to Python and instead want to come here on a regular basis and show us how big your brain is. Like Xah, you're unable to grasp that programming is a _communal_ exercise. You both vilify _everyone_ who doesn't loudly proclaim your brilliance. You refuse to accept that there are _ways_ of approaching the community to encourage interaction: being a smug cunt is not one of them. Even worse, the _more_ people point out to you both how wrong you are, the _more_ convinced you're correct, because of a bullshit belief in your superiority. You are both astounding exemplars of the Dunning-Kruger effect. You're a _troll_. You contribute nothing of value, you hijack threads for your own agenda, you are _nothing but a source of noise_ to this list. The months in which you don't post are an absolute goddamn _delight_ and there would be no loss to anyone if you decided to just fuck off and take your exercise in public masturbation elsewhere. From steve+comp.lang.python at pearwood.info Tue Dec 6 23:03:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Dec 2011 04:03:16 GMT Subject: Hints for writing bit-twiddling code in Python Message-ID: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> I have some bit-twiddling code written in Java which I am trying to port to Python. I'm not getting the same results though, and I think the problem is due to differences between Java's signed byte/int/long types, and Python's unified long integer type. E.g. Java's >>> is not exactly the same as Python's >> operator, and a character coerced to a byte in Java is not the same as ord(char) in Python. (The Java byte is in the range -128...127, I think, while the ord in Python is in 0...255.) Can anyone point me to some good resources to help me port the Java code to Python? If it helps, the Java code includes bits like this: long newSeed = (seed & 0xFFFFFFFFL) * 0x41A7L; while (newSeed >= 0x80000000L) { newSeed = (newSeed & 0x7FFFFFFFL) + (newSeed >>> 31L); } seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed; which I've translated into: newseed = (seed & 0xFFFFFFFF)*0x41A7 while (newseed >= 0x80000000): newseed = (newseed & 0x7FFFFFFF) + (newseed >> 31) seed = 0 if newseed == 0x7FFFFFFF else newseed & 0xFFFFFFFF -- Steven From dihedral88888 at googlemail.com Wed Dec 7 00:24:42 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 6 Dec 2011 21:24:42 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: <22982249.140.1323235482591.JavaMail.geo-discussion-forums@prix23> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral > wrote: > >> Please explain what you think a hash function is, then. ?Per > >> Wikipedia, "A hash function is any algorithm or subroutine that maps > >> large data sets to smaller data sets, called keys." > >> > >> > Are you miss-leading the power of true OOP ? > >> > >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > > v can be a tuple or a list. ?There are some restrictions on k to be an > > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > > position, then something has to be done to resolve this. > > I understand how dicts / hash tables work. I don't need you to > explain that to me. What you haven't explained is why you stated that > a hash function that operates on objects is not a hash function, or > what you meant by "misleading the power of true OOP". Do you forget the memory management of a dictionary in Python that has to be linked for a dynamical growing and shrinking number of (k,v) pairs in a long period of time? Avoiding the true non-trivial part in any implementation or use of a dictionary is miss leading ? Probing too deep in the stack or occupying too much in the heap is not bug free? From dihedral88888 at googlemail.com Wed Dec 7 00:24:42 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 6 Dec 2011 21:24:42 -0800 (PST) Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <3MKCq.194015$th7.47228@newsfe07.ams2> <26969335.380.1323013143216.JavaMail.geo-discussion-forums@prnu18> <27797359.1293.1323021983756.JavaMail.geo-discussion-forums@prfb10> <12469565.1384.1323040641935.JavaMail.geo-discussion-forums@prfj18> Message-ID: <22982249.140.1323235482591.JavaMail.geo-discussion-forums@prix23> On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote: > On Sun, Dec 4, 2011 at 4:17 PM, 88888 Dihedral > wrote: > >> Please explain what you think a hash function is, then. ?Per > >> Wikipedia, "A hash function is any algorithm or subroutine that maps > >> large data sets to smaller data sets, called keys." > >> > >> > Are you miss-leading the power of true OOP ? > >> > >> I have no idea what you are suggesting. ?I was not talking about OOP at all. > > > > In python the (k,v) pair in a dictionary k and v can be ?both an objects. > > v can be a tuple or a list. ?There are some restrictions on k to be an > > ?hashable type in python's implementation. The key is used to compute the position of the pair to be stored in a ?hash table. The hash function maps key k to the position in the hash table. If k1!=k2 are both ?mapped to the same > > position, then something has to be done to resolve this. > > I understand how dicts / hash tables work. I don't need you to > explain that to me. What you haven't explained is why you stated that > a hash function that operates on objects is not a hash function, or > what you meant by "misleading the power of true OOP". Do you forget the memory management of a dictionary in Python that has to be linked for a dynamical growing and shrinking number of (k,v) pairs in a long period of time? Avoiding the true non-trivial part in any implementation or use of a dictionary is miss leading ? Probing too deep in the stack or occupying too much in the heap is not bug free? From drsalists at gmail.com Wed Dec 7 01:08:34 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Tue, 6 Dec 2011 22:08:34 -0800 Subject: Hints for writing bit-twiddling code in Python In-Reply-To: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> References: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> Message-ID: On 12/6/11, Steven D'Aprano wrote: > I have some bit-twiddling code written in Java which I am trying to port > to Python.: > > long newSeed = (seed & 0xFFFFFFFFL) * 0x41A7L; > while (newSeed >= 0x80000000L) { > newSeed = (newSeed & 0x7FFFFFFFL) + (newSeed >>> 31L); > } > seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed; I suspect the problem lies somewhere other than the java and python code you posted. I'm having a bit of a time finding an input value of seed that gives two different results, despite throwing some hard-feeling test cases and lots of random values, using both a 32 bit and a 64 bit JVM. I believe java likes to treat strings like Python 3, but if you use "export LC_ALL=en_US.ISO-8859-1", then it'll behave a little more like Python 2 in that strings have an 8 bit encoding (or at least act like it) that's round-tripable. If worse comes to worse, you could probably write some test harness code for each of the java and python, and then feed them the same inputs - to see which pieces differ and which don't. From steve+comp.lang.python at pearwood.info Wed Dec 7 01:57:09 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Dec 2011 06:57:09 GMT Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> Message-ID: <4edf0e45$0$13933$c3e8da3$76491128@news.astraweb.com> On Tue, 06 Dec 2011 16:30:16 -0800, John Ladasky wrote: >> I would start with the line that fails 'put(task)', and work backwards >> to see where 'task' comes from and how it could become None. It is even >> possible that multiprocessing.pool has a bug that you ran into. > > Oh, please don't say that. I'm no computer scientist, and Python has > been scrutinized by so many professionals. I couldn't have possibly > found a language bug. "Professional" just means they get paid for doing it. Professionals gave us the 2008 banking collapse, the Challenger shuttle explosion, the sinking of the Titanic, trench warfare in World War I, the Chernobyl nuclear meltdown, leaded petrol, "Battlefield Earth" the movie, and the BP Gulf of Mexico oil spill. Amateurs gave us the discovery of electricity, the Roomba, the original Apple computer, GNU software, Linux, Ogg/Vorbis, and the discovery of continental drift. While your modesty is a welcome change from n00bs who imagine that anything about Python that they misunderstood is a bug, don't sell yourself short. You don't need to be a computer scientist to identify bugs in software. -- Steven From rosuav at gmail.com Wed Dec 7 02:12:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 7 Dec 2011 18:12:41 +1100 Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? In-Reply-To: <4edf0e45$0$13933$c3e8da3$76491128@news.astraweb.com> References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> <4edf0e45$0$13933$c3e8da3$76491128@news.astraweb.com> Message-ID: On Wed, Dec 7, 2011 at 5:57 PM, Steven D'Aprano wrote: > On Tue, 06 Dec 2011 16:30:16 -0800, John Ladasky wrote: > >> Oh, please don't say that. ?I'm no computer scientist, and Python has >> been scrutinized by so many professionals. ?I couldn't have possibly >> found a language bug. > > While your modesty is a welcome change from n00bs who imagine that > anything about Python that they misunderstood is a bug, don't sell > yourself short. You don't need to be a computer scientist to identify > bugs in software. Likelihood of something being a bug generally depends heavily on "eyeball density". If a piece of code gets a lot of eyeballs, chances are its bugs have been found (not always but often). Generally, the code that makes up a heavily-used open source project can be expected to have quite a few eyeballs near it as a regular thing; but there's always the obscure bits that don't. Crypto modules have fallen foul of this on occasion, with bugs lurking there far more than might otherwise be expected, on account of such a small portion of coders ever touching cryptography. Of course, it's always less embarrassing to say "I think I'm using this wrong" and have someone say "You found a language bug" than to come in guns blazing with "ur lnguage is teh buggy" (sorry, I don't speak Lame very fluently) only to learn that you spoiled the incantation in some way. The number of people who assume that their first-time code is perfect and the language is hopeless is somewhat astonishing. ChrisA From gallium.arsenide at gmail.com Wed Dec 7 02:15:46 2011 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 6 Dec 2011 23:15:46 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> <1d2b5f86-6acb-45a2-8df8-77bae32bc0e3@h5g2000yqk.googlegroups.com> Message-ID: On Dec 6, 7:30?pm, John Ladasky wrote: > On Dec 6, 1:42?pm, Terry Reedy wrote: > > It is even possible that multiprocessing.pool has a bug > > that you ran into. > > Oh, please don't say that. ?I'm no computer scientist, and > Python has been scrutinized by so many professionals. ?I > couldn't have possibly found a language bug. Scrutiny or no, Python has its fair share of bugs. I think almost all real-world implementations of almost all general-purpose programming languages do. Not long ago a friend of mine (a mathematician, but only a novice programmer) sent me some code dealing with sets that exposed a bug in Python 2.6.1. He invoked the union() method from the set class rather than a set instance, and it took us a long time to figure out why he was getting different results than I was from the same code (I was the one on 2.6.1, and my results were wrong). Fortunately, it was (a) easy enough to work around and (b) fixed in subsequent versions of Python. But it just goes to show that even unsophisticated programmers can stumble upon language bugs. This one wasn't even in the library; it was a built-in. That hasn't shaken my confidence in Python, though. (Also, for what it's worth, I use SciTE as my Python editor as well. I've also used Geany from time to time, and I have no trouble recommending it. It's a step up the IDE ladder from SciTE, but is still tons lighter than Eclipse and its brethren.) John From irmen.NOSPAM at xs4all.nl Wed Dec 7 03:13:25 2011 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Wed, 07 Dec 2011 09:13:25 +0100 Subject: why is bytearray treated so inefficiently by pickle? In-Reply-To: References: <4ed24a33$0$6869$e4fe514c@news2.news.xs4all.nl> <9675d44b-4001-4ea5-9390-74502244878d@l19g2000yqc.googlegroups.com> Message-ID: <4edf2024$0$6957$e4fe514c@news2.news.xs4all.nl> On 6-12-2011 23:24, Terry Reedy wrote: > On Nov 30, Irmen de Jong opened a tracker issue with a patch improve bytearray pickling. > http://bugs.python.org/issue13503 > > Yesterday, Dec 5, Antoine Pitrou applied a revised fix. > http://hg.python.org/cpython/rev/e2959a6a1440/ > The commit message: > "Issue #13503: Use a more efficient reduction format for bytearrays with pickle protocol >>= 3. The old reduction format is kept with older protocols in order to allow unpickling > under Python 2." > Sure, but this patch only improved the pickle behavior of the bytearray type for protocol level 3. It didn't touch Python 2.x, nor the pickling of arrays (array.array), let alone numpy arrays. Irmen From __peter__ at web.de Wed Dec 7 03:21:55 2011 From: __peter__ at web.de (Peter Otten) Date: Wed, 07 Dec 2011 09:21:55 +0100 Subject: Hints for writing bit-twiddling code in Python References: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > I have some bit-twiddling code written in Java which I am trying to port > to Python. I'm not getting the same results though, and I think the > problem is due to differences between Java's signed byte/int/long types, > and Python's unified long integer type. E.g. Java's >>> is not exactly > the same as Python's >> operator, and a character coerced to a byte in > Java is not the same as ord(char) in Python. (The Java byte is in the > range -128...127, I think, while the ord in Python is in 0...255.) > > Can anyone point me to some good resources to help me port the Java code > to Python? > > If it helps, the Java code includes bits like this: > > long newSeed = (seed & 0xFFFFFFFFL) * 0x41A7L; > while (newSeed >= 0x80000000L) { > newSeed = (newSeed & 0x7FFFFFFFL) + (newSeed >>> 31L); > } > seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed; > > > which I've translated into: > > newseed = (seed & 0xFFFFFFFF)*0x41A7 > while (newseed >= 0x80000000): > newseed = (newseed & 0x7FFFFFFF) + (newseed >> 31) > seed = 0 if newseed == 0x7FFFFFFF else newseed & 0xFFFFFFFF I think you need to take negative ints into account. Try adding if seed & 0x80000000: # 2**31 seed -= 0x100000000 # 2**32, two's complement From matteo.boscolo at boscolini.eu Wed Dec 7 03:22:44 2011 From: matteo.boscolo at boscolini.eu (Matteo Boscolo) Date: Wed, 07 Dec 2011 09:22:44 +0100 Subject: Losing com pointer Message-ID: <4EDF2254.2030809@boscolini.eu> Hi all, I need some help to a com problem.. I got this class: class foo(object): def setComObject(comObject): self.comO=comObject #This is a com object from a cad application def showForm(self) # use the self.comO to read some information from the cad application # Show the pyqt form as child of cad application # do somthing with the form # do somthing with the self.comO <----- Here if pass some time I'm not able to call any method to the com object a=foo() o="get istance of a cad application via com" a.setComObject(o) a.showForm() #<---- here if pass some time I'm not able to call any method to the com object but I I' re call the a.setComObject(o) #Faster and I take less the 30 seconds on the form object it works well It seems a problem of the garbage collector .. but I'm not sure how to debug it .. any help is really appreciated.. it's the last dangerous bug in our application ... Regards, Matteo From storchaka at gmail.com Wed Dec 7 04:33:50 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Wed, 07 Dec 2011 11:33:50 +0200 Subject: Hints for writing bit-twiddling code in Python In-Reply-To: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> References: <4edee584$0$13933$c3e8da3$76491128@news.astraweb.com> Message-ID: 07.12.11 06:03, Steven D'Aprano ???????(??): > long newSeed = (seed& 0xFFFFFFFFL) * 0x41A7L; > while (newSeed>= 0x80000000L) { > newSeed = (newSeed& 0x7FFFFFFFL) + (newSeed>>> 31L); > } > seed = (newSeed == 0x7FFFFFFFL) ? 0 : (int)newSeed; seed = (seed & 0xFFFFFFFF) * 0x41A7 % 0x7FFFFFFF From andrea.crotti.0 at gmail.com Wed Dec 7 05:14:41 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Wed, 07 Dec 2011 10:14:41 +0000 Subject: Questions about LISP and Python. In-Reply-To: <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> Message-ID: <4EDF3C91.3000605@gmail.com> On 12/06/2011 04:36 AM, Xah Lee wrote: > i don't like python, and i prefer emacs lisp. The primary reason is > that python is not functional, especially with python 3. The python > community is full of fanatics with their drivels. In that respect, > it's not unlike Common Lisp community and Scheme lisp community. > I love emacs and I love python, but saying that elisp is a better language than python and more functional is plain ridiculous. Elisp is great to extend Emacs, but with dynamic scoping and side effects used everywhere should not even be considered between the functional languages, at least for how it's mainly used. This is a typical usage taken from simple.el (defun pop-global-mark () "Pop off global mark ring and jump to the top location." (interactive) ;; Pop entries which refer to non-existent buffers. (while (and global-mark-ring (not (marker-buffer (car global-mark-ring)))) (setq global-mark-ring (cdr global-mark-ring))) (or global-mark-ring (error "No global mark set")) (let* ((marker (car global-mark-ring)) (buffer (marker-buffer marker)) (position (marker-position marker))) (setq global-mark-ring (nconc (cdr global-mark-ring) (list (car global-mark-ring)))) (set-buffer buffer) (or (and (>= position (point-min)) (<= position (point-max))) (if widen-automatically (widen) (error "Global mark position is outside accessible part of buffer"))) (goto-char position) (switch-to-buffer buffer))) Which means more or elss pop a mark from the mark ring, get the current buffer/position, then set the buffer, widen if necessary, go to some position and switch to that buffer. Nothing wrong with it, but basically it messes around with global state, as most elisp functions do. And how python 3 would be less functional? From tjreedy at udel.edu Wed Dec 7 06:17:51 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 07 Dec 2011 06:17:51 -0500 Subject: Questions about LISP and Python. In-Reply-To: <4EDF3C91.3000605@gmail.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <4EDF3C91.3000605@gmail.com> Message-ID: On 12/7/2011 5:14 AM, Andrea Crotti wrote: > On 12/06/2011 04:36 AM, Xah Lee wrote: >> i don't like python, and i prefer emacs lisp. The primary reason is >> that python is not functional, especially with python 3. The python >> community is full of fanatics with their drivels. In that respect, >> it's not unlike Common Lisp community and Scheme lisp community. >> > > I love emacs and I love python, but saying that elisp is a better language > than python and more functional is plain ridiculous. > Elisp is great to extend Emacs, but with dynamic scoping and side effects > used everywhere should not even be considered between the functional > languages, at least for how it's mainly used. > > This is a typical usage taken from simple.el > (defun pop-global-mark () > "Pop off global mark ring and jump to the top location." > (interactive) > ;; Pop entries which refer to non-existent buffers. > (while (and global-mark-ring (not (marker-buffer (car global-mark-ring)))) > (setq global-mark-ring (cdr global-mark-ring))) > (or global-mark-ring > (error "No global mark set")) > (let* ((marker (car global-mark-ring)) > (buffer (marker-buffer marker)) > (position (marker-position marker))) > (setq global-mark-ring (nconc (cdr global-mark-ring) > (list (car global-mark-ring)))) > (set-buffer buffer) > (or (and (>= position (point-min)) > (<= position (point-max))) > (if widen-automatically > (widen) > (error "Global mark position is outside accessible part of buffer"))) > (goto-char position) > (switch-to-buffer buffer))) > > Which means more or elss pop a mark from the mark ring, get the current > buffer/position, > then set the buffer, widen if necessary, go to some position and switch > to that buffer. > > Nothing wrong with it, but basically it messes around with global state, > as most elisp functions > do. > > And how python 3 would be less functional? It does not pretend that surrounding statements with parentheses turns them into expressions. ;-) -- Terry Jan Reedy From skip at pobox.com Wed Dec 7 06:30:27 2011 From: skip at pobox.com (Skip Montanaro) Date: Wed, 7 Dec 2011 11:30:27 +0000 (UTC) Subject: How to build 64-bit Python on Solaris with GCC? References: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> Message-ID: > Does anyone have a recipe for the subject build? I know Solaris is a minority platform these days, but surely someone has tackled this problem, haven't they? Thx, Skip From kliateni at gmail.com Wed Dec 7 07:47:11 2011 From: kliateni at gmail.com (Karim) Date: Wed, 07 Dec 2011 13:47:11 +0100 Subject: How to build 64-bit Python on Solaris with GCC? In-Reply-To: References: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> Message-ID: <4EDF604F.9070306@gmail.com> Le 07/12/2011 12:30, Skip Montanaro a ?crit : >> Does anyone have a recipe for the subject build? > I know Solaris is a minority platform these days, but surely someone has tackled > this problem, haven't they? > > Thx, > > Skip > > > ./configure make make install Karim From neilc at norwich.edu Wed Dec 7 08:16:15 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 7 Dec 2011 13:16:15 GMT Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <4EDF3C91.3000605@gmail.com> Message-ID: <9k978uFqo3U3@mid.individual.net> On 2011-12-07, Terry Reedy wrote: > It does not pretend that surrounding statements with > parentheses turns them into expressions. > ;-) I like being in a sexpression. Awwwwww YEAAAAH! -- (Neil Cerutti) From hniksic at xemacs.org Wed Dec 7 08:28:40 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Wed, 07 Dec 2011 14:28:40 +0100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> Message-ID: <8739cwbjef.fsf@xemacs.org> Chris Angelico writes: > 2011/12/5 Hrvoje Niksic : >> If a Python implementation tried to implement dict as a tree, >> instances of classes that define only __eq__ and __hash__ would not >> be correctly inserted in such a dict. > > Couldn't you just make a tree of hash values? Okay, that's probably > not the most useful way to do things, but technically it'd comply with > the spec. That's a neat idea. The leaves of the tree would contain a list of items with the same hash, but that's what you effectively get with a linear-probe hash table anyway. As you said, not immediately useful, but one could imagine the technique being of practical use when implementing Python or a Python-compatible language in a foreign environment that supports only tree-based collections. From massi_srb at msn.com Wed Dec 7 12:09:16 2011 From: massi_srb at msn.com (Massi) Date: Wed, 7 Dec 2011 09:09:16 -0800 (PST) Subject: Dynamic variable creation from string Message-ID: Hi everyone, in my script I have a dictionary whose items are couples in the form (string, integer values), say D = {'a':1, 'b':2, 'c':3} This dictionary is passed to a function as a parameter, e.g. : def Sum(D) : return D['a']+D['b']+D['c'] Is there a way to create three variables dynamically inside Sum in order to re write the function like this? def Sum(D) : # Here some magic to create a,b,c from D return a+b+c It is really important that the scope of a,b,c is limited to the Sum function, they must not exisit outside it or inside any other nested functions. Thanks in advance for your help! From gordon at panix.com Wed Dec 7 12:45:26 2011 From: gordon at panix.com (John Gordon) Date: Wed, 7 Dec 2011 17:45:26 +0000 (UTC) Subject: Dynamic variable creation from string References: Message-ID: In Massi writes: > in my script I have a dictionary whose items are couples in the form > (string, integer values), say > D = {'a':1, 'b':2, 'c':3} > This dictionary is passed to a function as a parameter, e.g. : > def Sum(D) : > return D['a']+D['b']+D['c'] > Is there a way to create three variables dynamically inside Sum in > order to re write the function like this? Do you want to sum all the values in D? If so, that's easy: def Sum(D): my_sum = 0 for item in D: my_sum += D[item] return my_sum -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From wm at localhost.localdomain Wed Dec 7 12:46:07 2011 From: wm at localhost.localdomain (Waldek M.) Date: Wed, 7 Dec 2011 18:46:07 +0100 Subject: Dynamic variable creation from string References: Message-ID: On Wed, 7 Dec 2011 09:09:16 -0800 (PST), Massi wrote: > def Sum(D) : > return D['a']+D['b']+D['c'] > > Is there a way to create three variables dynamically inside Sum in > order to re write the function like this? > > def Sum(D) : > # Here some magic to create a,b,c from D > return a+b+c Hello, > It is really important that the scope of a,b,c is limited to the Sum > function, they must not exisit outside it or inside any other nested > functions. > Thanks in advance for your help! Can you clarify a bit? I'm not sure why do you need to define any additional variables at all. You do not return variables from a function - you can return *a value* which may be a simple type or complex type. Or maybe you mean something like creating a global name from inside of a function? Well, that doesn't sound like a good idea, though... >>> def foo(): global a a = 5 >>> foo() >>> print("The variable is: ", a) The variable is: 5 >>> Best regards, Waldek From dihedral88888 at googlemail.com Wed Dec 7 12:48:11 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Wed, 7 Dec 2011 09:48:11 -0800 (PST) Subject: order independent hash? In-Reply-To: <8739cwbjef.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <8739cwbjef.fsf@xemacs.org> Message-ID: <14335292.152.1323280091159.JavaMail.geo-discussion-forums@prgg19> On Wednesday, December 7, 2011 9:28:40 PM UTC+8, Hrvoje Niksic wrote: > Chris Angelico writes: > > > 2011/12/5 Hrvoje Niksic : > >> If a Python implementation tried to implement dict as a tree, > >> instances of classes that define only __eq__ and __hash__ would not > >> be correctly inserted in such a dict. > > > > Couldn't you just make a tree of hash values? Okay, that's probably > > not the most useful way to do things, but technically it'd comply with > > the spec. > > That's a neat idea. The leaves of the tree would contain a list of > items with the same hash, but that's what you effectively get with a > linear-probe hash table anyway. > > As you said, not immediately useful, but one could imagine the technique > being of practical use when implementing Python or a Python-compatible > language in a foreign environment that supports only tree-based > collections. The heap as the root that could be divided like a tree of nodes to be used is funny. There are many ways to implement the heap manager in SW. From rosuav at gmail.com Wed Dec 7 12:52:11 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Dec 2011 04:52:11 +1100 Subject: Dynamic variable creation from string In-Reply-To: References: Message-ID: On Thu, Dec 8, 2011 at 4:09 AM, Massi wrote: > def Sum(D) : > # Here some magic to create a,b,c from D > return a+b+c Welcome to TMTOWTDI land! We do magic here... several different ways. You _may_ be able to do this, which is roughly equivalent to the extract() function in PHP: locals().update(D) However, this is NOT guaranteed to work. It's more likely to work with globals(), but that wouldn't restrict the scope the way you asked. One handy trick that I found on the internet while researching this: Use (or abuse!) function keyword parameters to do the dictionary extraction. You have to explicitly name your desired keys this way, but it may be suitable. (The function doesn't have to be defined inside the other, incidentally.) >>> def Sum(D): def inner_sum(a,b,c,**kwargs): # Real logic goes here. return a+b+c return inner_sum(**D) >>> a={"a":5,"b":10,"c":20} >>> Sum(a) 35 Alternatively, the exec() and eval() functions can be given dictionaries which will serve as their variable scopes, so you could possibly use that. Definitely looking like ugly code though. For something as trivial as Sum(), you'd do best to simply type D['a']+D['b']+D['c'] and be done with it. For something where you're going to use them a lot, probably easiest to be explicit: a,b,c = D['a'],D['b'],D['c'] However, this violates DRY principle, and risks hard-to-trace mismatch bugs. I'd be inclined to simply be explicit all the time. Depending on what D is, you may actually want to consider rewriting this as a class with a member function. class whatever: def Sum(self): return self.a+self.b+self.c There should be one obvious way to do it, says the zen of Python. Frankly, I'm not sure what that one obvious way is, here, although I'm pretty certain that several of the options posited would be very bad for your code! Still, down's very nice... They ARE alternative possibilities. ChrisA From python at mrabarnett.plus.com Wed Dec 7 13:07:47 2011 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 07 Dec 2011 18:07:47 +0000 Subject: Dynamic variable creation from string In-Reply-To: References: Message-ID: <4EDFAB73.7@mrabarnett.plus.com> On 07/12/2011 17:45, John Gordon wrote: > In Massi writes: > >> in my script I have a dictionary whose items are couples in the form >> (string, integer values), say > >> D = {'a':1, 'b':2, 'c':3} > >> This dictionary is passed to a function as a parameter, e.g. : > >> def Sum(D) : >> return D['a']+D['b']+D['c'] > >> Is there a way to create three variables dynamically inside Sum in >> order to re write the function like this? > > Do you want to sum all the values in D? If so, that's easy: > > def Sum(D): > my_sum = 0 > for item in D: > my_sum += D[item] > return my_sum > Or even: def Sum(D): return sum(D.values()) From mbadoiu at gmail.com Wed Dec 7 14:00:20 2011 From: mbadoiu at gmail.com (Mihai Badoiu) Date: Wed, 7 Dec 2011 14:00:20 -0500 Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Message-ID: ok, so the code is something like #process A p = Process(...) p.daemon = 1 p.start() # starts process B ... If process A dies (say error, or ctrl-c), or finishes, then process B also dies. But if process A is killed with the "kill" command, then process B soldiers on... Any idea on how to make process B die when process A gets killed by the "kill" command? thanks, --mihai On Sat, Dec 3, 2011 at 4:01 AM, Jack Keegan wrote: > I think the OP meant when the parent gets killed (by ctrl+c or similar), > not deleted. At least that's what I think when I think of a program being > killed. Is it even possible to send a signal in such a case? > > Cheers, > > Jack > > On Fri, Dec 2, 2011 at 4:27 PM, 88888 Dihedral < > dihedral88888 at googlemail.com> wrote: > >> Please check Erlang that spawn so easily. And there are Python packages >> can do the same task. >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > > > -- > The earth is a very small stage in a vast cosmic arena. Think of the > rivers of blood spilled by all those generals and emperors so that in glory > and in triumph they could become the momentary masters of a fraction of a > dot. > - Carl Sagan [Pale Blue Dot] > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From airween at gmail.com Wed Dec 7 14:41:12 2011 From: airween at gmail.com (=?utf-8?B?SGVnZWTDvHMs?= Ervin) Date: Wed, 7 Dec 2011 20:41:12 +0100 Subject: Insert trusted timestamp to PDF Message-ID: <20111207194110.GA2835@arxnet.hu> Hello Everyone, I'm looking for a tool, which can add a trusted timestamp to an existing PDF file (and can sign - but currently only have to add TS). Could anybody help? Thanks: a. From drsalists at gmail.com Wed Dec 7 14:50:01 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Wed, 7 Dec 2011 11:50:01 -0800 Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Message-ID: On 12/7/11, Mihai Badoiu wrote: > ok, so the code is something like > #process A > p = Process(...) > p.daemon = 1 > p.start() # starts process B > ... > > If process A dies (say error, or ctrl-c), or finishes, then process B also > dies. But if process A is killed with the "kill" command, then process B > soldiers on... > > Any idea on how to make process B die when process A gets killed by the > "kill" command? 1) If all you care about is SIGTERM, SIGHUP and the like (and specifically NOT SIGKILL), you could just install a signal handler that catches any catchable signals you're interested in. Then the signal either kills the children directly, or sets a flag that tells the main process to do some killing shortly. Note that threads and signal handlers don't mix very well - the combination tends to make the main thread immune to control-C, whether you want it to be or not. Also, signal handlers tend to complicate performing I/O, as you're more likely to read short blocks. 2) If you need to handle SIGKILL gracefully, and you have access to the code of the child process, you could make sure that the child isn't setting a SID (?). ssh, I believe, likes to start a new SID, making it immune to signals to the parent. Alternatively, you could add something to the child process' main loop that polls the parent, exiting if the parent no longer exists. 3) If you need to handle SIGKILL gracefully, and you don't have access to the code of the child process, you could use a single extra process that checks for the presense of the parent, and if it doesn't exist any more, then kill the children before exiting itself. From mbadoiu at gmail.com Wed Dec 7 14:53:47 2011 From: mbadoiu at gmail.com (Mihai Badoiu) Date: Wed, 7 Dec 2011 14:53:47 -0500 Subject: Multiprocessing: killing children when parent dies In-Reply-To: References: <31747430.412.1322841476524.JavaMail.geo-discussion-forums@prfx15> <27696236.393.1322843237576.JavaMail.geo-discussion-forums@prfb10> Message-ID: I like 2) the most. I do have access to the child. The child is a process started with multiprocessing.Process(function). How do I _not_ set an SID? thanks, --mihai On Wed, Dec 7, 2011 at 2:50 PM, Dan Stromberg wrote: > On 12/7/11, Mihai Badoiu wrote: > > ok, so the code is something like > > #process A > > p = Process(...) > > p.daemon = 1 > > p.start() # starts process B > > ... > > > > If process A dies (say error, or ctrl-c), or finishes, then process B > also > > dies. But if process A is killed with the "kill" command, then process B > > soldiers on... > > > > Any idea on how to make process B die when process A gets killed by the > > "kill" command? > > 1) If all you care about is SIGTERM, SIGHUP and the like (and > specifically NOT SIGKILL), you could just install a signal handler > that catches any catchable signals you're interested in. Then the > signal either kills the children directly, or sets a flag that tells > the main process to do some killing shortly. Note that threads and > signal handlers don't mix very well - the combination tends to make > the main thread immune to control-C, whether you want it to be or not. > Also, signal handlers tend to complicate performing I/O, as you're > more likely to read short blocks. > > 2) If you need to handle SIGKILL gracefully, and you have access to > the code of the child process, you could make sure that the child > isn't setting a SID (?). ssh, I believe, likes to start a new SID, > making it immune to signals to the parent. Alternatively, you could > add something to the child process' main loop that polls the parent, > exiting if the parent no longer exists. > > 3) If you need to handle SIGKILL gracefully, and you don't have access > to the code of the child process, you could use a single extra process > that checks for the presense of the parent, and if it doesn't exist > any more, then kill the children before exiting itself. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From irmen at -NOSPAM-xs4all.nl Wed Dec 7 14:59:11 2011 From: irmen at -NOSPAM-xs4all.nl (Irmen de Jong) Date: Wed, 07 Dec 2011 20:59:11 +0100 Subject: Insert trusted timestamp to PDF In-Reply-To: References: Message-ID: <4edfc58f$0$6850$e4fe514c@news2.news.xs4all.nl> On 07-12-11 20:41, Heged?s, Ervin wrote: > Hello Everyone, > > I'm looking for a tool, which can add a trusted timestamp to an > existing PDF file (and can sign - but currently only have to add > TS). Note sure what a 'trusted timestamp' is, but pdftk can manipulate pdf files. See http://www.pdflabs.com/docs/pdftk-man-page/ (nothing to do with Python btw!) Irmen From bv6bv6bv6 at hotmail.com Wed Dec 7 15:29:03 2011 From: bv6bv6bv6 at hotmail.com (bv) Date: Wed, 7 Dec 2011 12:29:03 -0800 (PST) Subject: A HOME OF TOLERANCE !!!!!!!!!!!!! Message-ID: <8f88c87a-1003-447b-be51-f98e080a6532@i6g2000vbe.googlegroups.com> A HOME OF TOLERANCE A Home Of Tolerance Media speculation since the horrific terrorist attacks on America has pointed the finger at Muslims and the Arab world, and that has meant ordinary citizens of the US and other Western countries becoming easy prey for anti-faith hooligans. Shame. Sadly, the latest horror to hit the US looks to have been caused by people of Middle Eastern origin, bearing Muslim names. Again, shame. This fuels more hatred for a religion and a people who have nothing to do with these events. This is why I want to explain some basic facts about this noble way we call Islam, before, God forbid, another disaster occurs - next time probably aimed at Muslims. I came to Islam in my late 20s, during my searching period as a wandering pop star. I found a religion that blended scientific reason with spiritual reality in a unifying faith far removed from the headlines of violence, destruction and terrorism. One of the first interesting things I learned in the Koran was that the name of the faith comes from the word salam - peace. Far from the kind of Turko-Arab-centric message I expected, the Koran presented a belief in the universal existence of God, one God for all. It does not discriminate against peoples; it says we may be different colors and from different tribes, but we are all human and "the best of people are the most God- conscious". Today, as a Muslim, I have been shattered by the horror of recent events; the display of death and indiscriminate killing we've all witnessed has dented humanity's confidence in itself. Terror on this scale affects everybody on this small planet, and no one is free from the fallout. Yet we should remember that such violence is almost an everyday occurrence in some Muslim lands: it should not be exacerbated by revenge attacks on more innocent families and communities. Along with most Muslims, I feel it a duty to make clear that such orchestrated acts of incomprehensible carnage have nothing to do with the beliefs of most Muslims. The Koran specifically declares: "If anyone murders an (innocent) person, it will be as if he has murdered the whole of humanity. And if anyone saves a person it will be as if he has saved the whole of humanity." The Koran that our young people learn is full of stories and lessons from the history of humanity as a whole. The Gospels and the Torah are referred to; Jesus and Abraham are mentioned. In fact there is more mention in the Koran of the prophet Moses than of any other. It acknowledges the coexistence of other faiths, and in doing so acknowledges that other cultures can live together in peace. "There is no compulsion in religion," it states, meaning that people should not be compelled to change their faith. Elsewhere it states, "To you, your religion; to me mine." Respect for religious values and justice is at the Koran's core. The Koranic history we teach our young provides ample examples of inter-religious and international relationships; of how to live together. But some extremists take elements of the sacred ******ures out of con****. They act as individuals, and when they can't come together as part of a political structure or consultative process, you find these dissident factions creating their own rules, contrary to the spirit of the Koran - which demands that those recognized as being in charge of Muslims must consult together regarding society's affairs. There is a whole chapter in the Koran entitled Consultation. Communal well being is central to human life, so there is a concept in Islam called Istihsan, which means "to look for the common good". Even though the Koran may lay down a diktat, scholars are also supposed to consider the circumstances prevalent at the time. Sometimes that means choosing the lesser of two evils or even suspending legislation if necessary: for instance, a person who steals bread during a famine is not treated as a thief. Once I wrote in a song, "Where do the children play?" Our sympathy and thoughts go out to the families of all those who lost their lives in this tragic act of violence, as well as all those injured. But life must go on. Children still need to play, and people need to live and learn more about their neighbors so that ignorance doesn't breed more blind fanaticism. Moderation is part of faith, so those who accuse Muslim schools of fostering fanaticism should learn a bit more about Islam. The Prophet (peace be upon him) said, "Ruined are those who insist on hardship in faith," and, "A believer remains within the scope of his religion as long as he doesn't kill another person illegally." Such knowledge and words of guidance are desperately needed at this time, to separate fact from falsehood, and to recognise the Last Prophet's own definition of that which makes a person representative, or otherwise, of the faith he lived and the one we try to teach. by Yusuf Islam (formerly the singer Cat Stevens) Published on Monday, September 24, 2001 by Al-Hewar Magazine. IF YOU WISH TO KNOW MORE ABOUT ISLAM, WE PREFER TO VISIT THE FOLLOWING WEBSITES: http://www.islam-guide.com http://www.islamhouse.com/s/9661 http://www.thisistruth.org http://www.quran-m.com/firas/en1 http://kaheel7.com/eng http://www.knowmuhammad.com http://www.rasoulallah.net/v2/index.aspx?lang=e http://imanway1.com/eng http://www.todayislam.com http://www.discoverislam.com http://www.thetruereligion.org http://www.beconvinced.com http://islamtomorrow.com http://www.usc.edu/dept/MSA/quran http://www.quranforall.org http://www.quranexplorer.com/quran http://www.prophetmuhammed.org http://www.chatislamonline.org/ar http://www.dar-us-salam.com http://youtubeislam.com From matteo.boscolo at boscolini.eu Wed Dec 7 15:57:07 2011 From: matteo.boscolo at boscolini.eu (Matteo Boscolo) Date: Wed, 07 Dec 2011 21:57:07 +0100 Subject: Insert trusted timestamp to PDF In-Reply-To: <20111207194110.GA2835@arxnet.hu> References: <20111207194110.GA2835@arxnet.hu> Message-ID: <4EDFD323.1070003@boscolini.eu> have a look at: http://www.boscolini.eu/Boscolini/index.php?option=com_content&view=article&id=64%3Anumbering-pdf-file-in-python&catid=38%3Aprogramming&Itemid=55&lang=en here I number some pdf files .. regards, Matteo Il 07/12/2011 20:41, Heged?s, Ervin ha scritto: > Hello Everyone, > > I'm looking for a tool, which can add a trusted timestamp to an > existing PDF file (and can sign - but currently only have to add > TS). > > Could anybody help? > > > Thanks: > > > a. > From tjreedy at udel.edu Wed Dec 7 17:59:33 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 07 Dec 2011 17:59:33 -0500 Subject: Dynamic variable creation from string In-Reply-To: References: Message-ID: On 12/7/2011 12:09 PM, Massi wrote: > in my script I have a dictionary whose items are couples in the form > (string, integer values), say > > D = {'a':1, 'b':2, 'c':3} > > This dictionary is passed to a function as a parameter, e.g. : > > def Sum(D) : > return D['a']+D['b']+D['c'] > > Is there a way to create three variables dynamically inside Sum in > order to re write the function like this? > > def Sum(D) : > # Here some magic to create a,b,c from D > return a+b+c No. The set of local names for a function is determined when the definition is executed and the body is compiled. Python 2 had an exception -- 'from x import *' -- that required an alternate complilation pathway. That possibility was eliminated in Python 3 and is now a syntax error. Within functions, 'locals().update(D)' is more or less guaranteed to *not* add local names to the local namespace, even if it does add keys to the locals() dict that shadows the local namespace. > It is really important that the scope of a,b,c is limited to the Sum > function, they must not exisit outside it or inside any other nested > functions. Local names, by definition, are lexically scoped to the function definition and are not visible without. Since nested definitions are part of that lexical scope, local names are always visible within nested definitions. You cannot stop that. The association of local names is usually dynamically limited to one function call. The two exceptions are enclosure by a nested function that survives the function call and generators in a paused state. -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Wed Dec 7 19:03:36 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 Dec 2011 00:03:36 GMT Subject: Dynamic variable creation from string References: Message-ID: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: > Is there a way to create three variables dynamically inside Sum in order > to re write the function like this? > > def Sum(D) : > # Here some magic to create a,b,c from D > return a+b+c No magic is needed. a, b, c = D['a'], D['b'], D['c'] However, there is no way to create variables from an *arbitrary* set of keys. And a good thing too, because how could you use them? def Sum(D): # Magic happens here return a + b + z + foo + spam + fred + ... ??? Since you don't know what variable names will be created, you don't know which variables you need to add. Dynamic creation of variables is ALMOST ALWAYS the wrong approach. In this specific case, what you should do to add up an arbitrary number of values from a dict is: sum(D.values()) > It is really important that the scope of a,b,c is limited to the Sum > function, they must not exisit outside it or inside any other nested > functions. The first part is trivially easy; since you are assigning to local variables, by definition they will be local to the Sum function and will not exist outside it. The second part is impossible, because that is not how Python works. Nested functions in Python can always see variables in their enclosing scope. If you don't want that behaviour, use another language, or don't use nested functions. -- Steven From tjreedy at udel.edu Wed Dec 7 19:27:43 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 07 Dec 2011 19:27:43 -0500 Subject: Dynamic variable creation from string In-Reply-To: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/7/2011 7:03 PM, Steven D'Aprano wrote: > On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: > >> Is there a way to create three variables dynamically inside Sum in order >> to re write the function like this? I should have mentioned in my earlier response that 'variable' is a bit vague and misleading. Python has names bound to objects. >> def Sum(D) : >> # Here some magic to create a,b,c from D >> return a+b+c > > No magic is needed. > > a, b, c = D['a'], D['b'], D['c'] This is not what most people mean by 'dynamically created variables'. The names are static, in the code, before the code is executed. In 2.x, 'from x import *' dynamically creates local names that are not in the code that contains the import. Dynamically creating objects is what Python code does all the time. -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Wed Dec 7 20:59:23 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 08 Dec 2011 01:59:23 GMT Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ee019fa$0$13933$c3e8da3$76491128@news.astraweb.com> On Wed, 07 Dec 2011 19:27:43 -0500, Terry Reedy wrote: > On 12/7/2011 7:03 PM, Steven D'Aprano wrote: >> On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: >> >>> Is there a way to create three variables dynamically inside Sum in >>> order to re write the function like this? > > I should have mentioned in my earlier response that 'variable' is a bit > vague and misleading. Python has names bound to objects. > >>> def Sum(D) : >>> # Here some magic to create a,b,c from D return a+b+c >> >> No magic is needed. >> >> a, b, c = D['a'], D['b'], D['c'] > > This is not what most people mean by 'dynamically created variables'. I know that. I'm just pointing out that the OP can solve his *stated* problem of creating a *fixed* number of variables with *known* names without any magic. I went on to discuss the case of an unknown number of unknown names, and suggested that the OP not do that, because it is much less useful than people think. > The names are static, in the code, before the code is executed. In 2.x, > 'from x import *' dynamically creates local names that are not in the > code that contains the import. Dynamically creating objects is what > Python code does all the time. Of course. And even dynamically creating names: names don't exist until they are created at runtime. Name deletion is also possible. But what isn't normally done is "dynamic variable creation" in the sense of creating arbitrary variables (names bound to objects) based on names known only at runtime. That's extremely rare, and for good reason. -- Steven From sturdyworks at yahoo.com Wed Dec 7 21:21:54 2011 From: sturdyworks at yahoo.com (B.A.S.) Date: Wed, 7 Dec 2011 18:21:54 -0800 (PST) Subject: How to get Python make to detect /usr/local/ssl Message-ID: <1323310914.67792.YahooMailClassic@web180009.mail.gq1.yahoo.com> Any Python developers out there that help me? I would like to configure/make Python so it uses my local OpenSSL-1.0.0e install in /usr/local/ssl. I have tried uncommenting the promising section of ./Python 2.7.2/Modules/Setup.dist below without success. How to do it! Would like to upgrade SQLite as well... ------ snip of Setup.dist ------------ # Socket module helper for socket(2) _socket socketmodule.c # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto Brad From rosuav at gmail.com Wed Dec 7 22:13:19 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Dec 2011 14:13:19 +1100 Subject: Dynamic variable creation from string In-Reply-To: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 8, 2011 at 11:03 AM, Steven D'Aprano wrote: >> It is really important that the scope of a,b,c is limited to the Sum >> function, they must not exisit outside it or inside any other nested >> functions. > > The second part is impossible, because that is not how Python works. > Nested functions in Python can always see variables in their enclosing > scope. If you don't want that behaviour, use another language, or don't > use nested functions. To the OP: By "nested functions", did you mean actual nested functions (those defined inside this function), or simply functions called from this one? This is a nested function, as the term is usually taken to mean: def outer_function(): a = 1 def inner_function(): b = 2 return a+b print(inner_function()) # Prints 3 The inner function has access to all of the outer function's namespace. But if you meant this: def function_1(): b = 2 return a+b def function_2(): a = 1 print(function_1()) then it's quite the other way around - Python never shares variables in this way (this would have function_1 assume that 'a' is a global name, so if it's not, you'll get a run-time NameError). As Steven says, there's no way in Python to hide variables from an actual nested function. But if you just mean a function called from this one, then what you want is the normal behaviour (and if you actually want to share variables, you pass them as arguments). ChrisA From rantingrickjohnson at gmail.com Wed Dec 7 23:10:22 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Wed, 7 Dec 2011 20:10:22 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: On Dec 6, 10:58?am, Ian Kelly wrote: (snip... beautiful retort) Ian you make some damn good points and i could not help but laugh hysterically at your dissection of Xah's tutorial. I don't 100% agree with everything Mr. Lee rants about, like for instance, documents needing to hyper link every possible sub-subject in every possible subject -- we'd end up with a spaghetti mess of links. Besides, students should follow a "linear" learning curve and not haphazardly jump from subject to subject. However, that being said, what does it prove when you can dissect someones rant with elegant irony? It simply means that no ONE person can write great documentation. They say... "it takes a village to raise a child"... Hmm, i believe it takes a community to create good docs. Q: Do we have a "community" here? Hmm, more on that later... I truly believe that Mr. Lee wants to make Python a better language, and many other languages also! However he has experienced so much resistance to his comments that he has (like many among us) developed what Psychologists call "learned helplessness". Xah: "I found a problem" Communty: "Well write a freaking patch or shut the hell up! Xah: "Why bother, you will just throw it in the trash to spite me no matter how good it is." Commuinty: "You're lazy, you just want to complain" Xah: "But, that's all you will allow me to do!" Who's fault is it that Xah is not a productive part of this community? Hmm? Do we just take the easy way out an blame Xah? Or do we need to observe our "collective" attitudes to perceived "outsiders"? I believe this community has a cancer. A cancer that is rotting us from the inside. A cancer that has metastasis and is spreading like wild fire. *Inquisitive Joe asked:* What is the source of this cancer Rick? The source is a direct result of insufficient leadership. Our current leader has failed us. Maybe he never wanted to be a leader, but when you go and declare yourself a "benevolent dictator for life" you'd damn well better act like one! Why has GvR not admonished the atrocious behavior of some people in this community? Why has GvR not admitted publicly the hideous state of IDLE and Tkinter? Where is the rally call? Where is the community spirit? The future of Pythin is in your hands Mr. Van Rossum. Will you step up and do what needs to be done? Will you have the courage? i sincerely hope so. If not, please step down and allow someone to lead. I await the supreme commanders ascent to power. From rantingrickjohnson at gmail.com Wed Dec 7 23:14:05 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Wed, 7 Dec 2011 20:14:05 -0800 (PST) Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: <16dd31a4-da6d-4e7d-a2fd-38874253bd1b@32g2000yqp.googlegroups.com> On Dec 6, 9:52?pm, alex23 wrote: > (snip rambling nonsense) Alex, i hope you are being theatrical with all this. If not, i fear you may be putting too much stress on your heart. Please calm down. > The months in which you don't post are an absolute goddamn > _delight_ I am working on Python4000 Alex. *Someone* has to re-ignite Guido's original vision. From skippy.hammond at gmail.com Wed Dec 7 23:46:59 2011 From: skippy.hammond at gmail.com (Mark Hammond) Date: Thu, 08 Dec 2011 15:46:59 +1100 Subject: Losing com pointer In-Reply-To: <4EDF2254.2030809@boscolini.eu> References: <4EDF2254.2030809@boscolini.eu> Message-ID: <4EE04143.7060001@gmail.com> On 7/12/2011 7:22 PM, Matteo Boscolo wrote: > Hi all, > I need some help to a com problem.. > > I got this class: > > class foo(object): > def setComObject(comObject): > self.comO=comObject #This is a com object from a cad application > > def showForm(self) > # use the self.comO to read some information from the cad application > # Show the pyqt form as child of cad application > # do somthing with the form > # do somthing with the self.comO <----- Here if pass some time I'm not > able to call any method to the com object What are the symptoms of that? ie, what error do you get? I suspect the remote process is going away - it almost certainly has nothing to do with Python itself deciding to make the object go away. Mark > > a=foo() > o="get istance of a cad application via com" > a.setComObject(o) > a.showForm() #<---- here if pass some time I'm not able to call any > method to the com object > > but I I' re call the > a.setComObject(o) #Faster and I take less the 30 seconds on the form > object it works well > > It seems a problem of the garbage collector .. but I'm not sure how to > debug it .. > > any help is really appreciated.. it's the last dangerous bug in our > application ... > > Regards, > Matteo From airween at gmail.com Thu Dec 8 00:09:21 2011 From: airween at gmail.com (=?utf-8?B?SGVnZWTDvHMs?= Ervin) Date: Thu, 8 Dec 2011 06:09:21 +0100 Subject: Insert trusted timestamp to PDF In-Reply-To: <4edfc58f$0$6850$e4fe514c@news2.news.xs4all.nl> References: <4edfc58f$0$6850$e4fe514c@news2.news.xs4all.nl> Message-ID: <20111208050920.GB2186@arxnet.hu> Hello Irmen, On Wed, Dec 07, 2011 at 08:59:11PM +0100, Irmen de Jong wrote: > On 07-12-11 20:41, Heged?s, Ervin wrote: > >Hello Everyone, > > > >I'm looking for a tool, which can add a trusted timestamp to an > >existing PDF file (and can sign - but currently only have to add > >TS). > > Note sure what a 'trusted timestamp' is, that's very important to understand what does it mean - otherwise it's just groping :) http://tools.ietf.org/html/rfc3161 http://en.wikipedia.org/wiki/Trusted_timestamp > but pdftk can manipulate > pdf files. See http://www.pdflabs.com/docs/pdftk-man-page/ > (nothing to do with Python btw!) looks like it couldn't, but I'll contact them. thanks: a. From alec.taylor6 at gmail.com Thu Dec 8 00:28:01 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Thu, 8 Dec 2011 16:28:01 +1100 Subject: Insert trusted timestamp to PDF In-Reply-To: <20111208050920.GB2186@arxnet.hu> References: <4edfc58f$0$6850$e4fe514c@news2.news.xs4all.nl> <20111208050920.GB2186@arxnet.hu> Message-ID: Just digitally sign the document using python-gnupg /problem-solved! On Thu, Dec 8, 2011 at 4:09 PM, Heged?s, wrote: > Hello Irmen, > > On Wed, Dec 07, 2011 at 08:59:11PM +0100, Irmen de Jong wrote: >> On 07-12-11 20:41, Heged?s, Ervin wrote: >> >Hello Everyone, >> > >> >I'm looking for a tool, which can add a trusted timestamp to an >> >existing PDF file (and can sign - but currently only have to add >> >TS). >> >> Note sure what a 'trusted timestamp' is, > that's very important to understand what does it mean - > otherwise it's just groping :) > > http://tools.ietf.org/html/rfc3161 > http://en.wikipedia.org/wiki/Trusted_timestamp > >> but pdftk can manipulate >> pdf files. See http://www.pdflabs.com/docs/pdftk-man-page/ >> (nothing to do with Python btw!) > > looks like it couldn't, but I'll contact them. > > > thanks: > > > a. > > -- > http://mail.python.org/mailman/listinfo/python-list From airween at gmail.com Thu Dec 8 02:06:16 2011 From: airween at gmail.com (=?utf-8?B?SGVnZWTDvHMs?= Ervin) Date: Thu, 8 Dec 2011 08:06:16 +0100 Subject: Insert trusted timestamp to PDF In-Reply-To: References: <4edfc58f$0$6850$e4fe514c@news2.news.xs4all.nl> <20111208050920.GB2186@arxnet.hu> Message-ID: <20111208070614.GA2572@arxnet.hu> Hello, On Thu, Dec 08, 2011 at 04:28:01PM +1100, Alec Taylor wrote: > Just digitally sign the document using python-gnupg > > /problem-solved! using gnupg to sign a document != add a timestamp to a pdf. May be this doc helps to clear what's the different, and what I want: http://learn.adobe.com/wiki/download/attachments/52658564/digital_signatures_in_acrobat_9x.pdf?version=1 thanks: a. From dihedral88888 at googlemail.com Thu Dec 8 02:18:46 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Wed, 7 Dec 2011 23:18:46 -0800 (PST) Subject: I love the decorator in Python!!! Message-ID: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> I use the @ decorator to behave exactly like a c macro that does have fewer side effects. I am wondering is there other interesting methods to do the jobs in Python? A lot people complained that no macro in Python. Cheers to the rule of Python : If there's none then just go ahead and build one! From marco.rucci at gmail.com Thu Dec 8 02:39:20 2011 From: marco.rucci at gmail.com (marco.rucci at gmail.com) Date: Wed, 7 Dec 2011 23:39:20 -0800 (PST) Subject: Insert trusted timestamp to PDF In-Reply-To: References: Message-ID: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Hi, take a look at this online tool: http://easytimestamping.com It is able to apply RFC3161 compliant trusted timestamps, issued by accredited Certification Authorities. The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. -- Marco Rucci securo.it From marco.rucci at gmail.com Thu Dec 8 02:39:20 2011 From: marco.rucci at gmail.com (marco.rucci at gmail.com) Date: Wed, 7 Dec 2011 23:39:20 -0800 (PST) Subject: Insert trusted timestamp to PDF In-Reply-To: References: Message-ID: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Hi, take a look at this online tool: http://easytimestamping.com It is able to apply RFC3161 compliant trusted timestamps, issued by accredited Certification Authorities. The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. -- Marco Rucci securo.it From alec.taylor6 at gmail.com Thu Dec 8 02:42:47 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Thu, 8 Dec 2011 18:42:47 +1100 Subject: Insert trusted timestamp to PDF In-Reply-To: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> References: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Message-ID: Or the python implementation of that RFC: http://pypi.python.org/pypi/rfc3161/0.1.3 On Thu, Dec 8, 2011 at 6:39 PM, wrote: > Hi, take a look at this online tool: http://easytimestamping.com > > It is able to apply RFC3161 compliant trusted timestamps, issued by accredited Certification Authorities. > > The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) > > If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. > > -- > Marco Rucci > securo.it > -- > http://mail.python.org/mailman/listinfo/python-list From airween at gmail.com Thu Dec 8 02:59:31 2011 From: airween at gmail.com (=?utf-8?B?SGVnZWTDvHMs?= Ervin) Date: Thu, 8 Dec 2011 08:59:31 +0100 Subject: Insert trusted timestamp to PDF In-Reply-To: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> References: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Message-ID: <20111208075930.GA3526@arxnet.hu> hello, On Wed, Dec 07, 2011 at 11:39:20PM -0800, marco.rucci at gmail.com wrote: > Hi, take a look at this online tool: http://easytimestamping.com I need to create the PDF on my server - it could be any online service, but it must to have any kind of API. > It is able to apply RFC3161 compliant trusted timestamps, issued by accredited Certification Authorities. that's no problem in Python... :) http://pypi.python.org/pypi/rfc3161/ > The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) I'm afraid that's not good for us - we need to propagate PDF files in enbedded mode. > If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. sounds good - what do you think, when will you release that about? Thanks: a. From clp2 at rebertia.com Thu Dec 8 03:04:28 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 8 Dec 2011 00:04:28 -0800 Subject: I love the decorator in Python!!! In-Reply-To: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On Wed, Dec 7, 2011 at 11:18 PM, 88888 Dihedral wrote: > I use the @ decorator to behave exactly like a c macro that > does have fewer side effects. > > I am wondering is there other interesting methods to do the > jobs in Python? * Class decorators (http://www.python.org/dev/peps/pep-3129/ ); i.e. using decorators on a class instead of a function/method * Metaclasses (http://docs.python.org/dev/reference/datamodel.html#customizing-class-creation ); a rather more complicated feature capable of serious black magic * Descriptors (http://docs.python.org/dev/reference/datamodel.html#implementing-descriptors ); probably the most rarely directly used of the three Cheers, Chris -- http://rebertia.com From jpiitula at ling.helsinki.fi Thu Dec 8 03:54:48 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 08 Dec 2011 10:54:48 +0200 Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: Terry Reedy writes: > On 12/7/2011 7:03 PM, Steven D'Aprano wrote: > > On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: > > > >> Is there a way to create three variables dynamically inside Sum > >> in order to re write the function like this? > > I should have mentioned in my earlier response that 'variable' is a > bit vague and misleading. Python has names bound to objects. The language reference at python.org uses both terms - name and variable - freely. Here is one instance. # If a name is bound in a block, it is a local variable of that block, # unless declared as nonlocal. If a name is bound at the module level, # it is a global variable. (The variables of the module code block are # local and global.) If a variable is used in a code block but not # defined there, it is a free variable. Perhaps you could consider accepting this. Or trying to change it. Or starting to tell newcomers that the official documentation of the language is, say, vague and misleading. (It seems good to me.) From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Thu Dec 8 03:59:26 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Thu, 08 Dec 2011 09:59:26 +0100 Subject: I love the decorator in Python!!! In-Reply-To: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: Am 08.12.2011 08:18 schrieb 88888 Dihedral: > I use the @ decorator to behave exactly like a c macro that > does have fewer side effects. > > I am wondering is there other interesting methods to do the > jobs in Python? In combination with a generator, you can do many funny things. For example, you can build up a string: def mkstring(f): """Turns a string generator into a string, joining with ", ". """ return ", ".join(f()) def create_answer(): @mkstring def people(): yield "Anna" yield "John" yield "Theo" return "The following people were here: " + people Many other things are thinkable... Thomas From hniksic at xemacs.org Thu Dec 8 04:30:01 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Thu, 08 Dec 2011 10:30:01 +0100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> Message-ID: <87y5un9zs6.fsf@xemacs.org> Tim Chase writes: > From an interface perspective, I suppose it would work. However one > of the main computer-science reasons for addressing by a hash is to > get O(1) access to items (modulo pessimal hash structures/algorithms > which can approach O(N) if everything hashes to the same > value/bucket), rather than the O(logN) time you'd get from a tree. So > folks reaching for a hash/map might be surprised if performance > degraded with the size of the contents. In a language like Python, the difference between O(1) and O(log n) is not the primary reason why programmers use dict; they use it because it's built-in, efficient compared to alternatives, and convenient to use. If Python dict had been originally implemented as a tree, I'm sure it would be just as popular. Omitting the factor of O(log n) as functionally equivalent to O(1) is applicable to many situations and is sometimes called "soft-O" notation. One example from practice is the pre-2011 C++, where the standardization committee failed to standardize hash tables on time for the 1998 standard. Although this was widely recognized as an oversight, a large number of programs simply used tree-based std::maps and never noticed a practical difference between between average-constant-time and logarithmic complexity lookups. From hanspeter.sloot at gmail.com Thu Dec 8 04:34:09 2011 From: hanspeter.sloot at gmail.com (HansPeter) Date: Thu, 8 Dec 2011 01:34:09 -0800 (PST) Subject: how to test attribute existence of feedparser objects Message-ID: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> Hi, While using the feedparser library for downloading RSS feeds some of the blog entries seem to have no title. File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__ AttributeError: object has no attribute 'title' Is there a way to test the existence of an attribute? I can use an exception but like below to see whether it exists but this is a clumsy way since the function has to return the title. d=feedparser.parse(url,handlers = [proxy]) try: print "TITLE ",d.feed.title except: print "HAS NO TITLE" wc={} Regards HansPeter From rosuav at gmail.com Thu Dec 8 04:37:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Dec 2011 20:37:55 +1100 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On Thu, Dec 8, 2011 at 7:59 PM, Thomas Rachel wrote: > Many other things are thinkable... And many more are unthinkable. Can we start an International Obfuscated Python Code Contest? It's the only place such... abhorrences can properly flourish. ChrisA From jimmy at retzlaff.com Thu Dec 8 04:57:16 2011 From: jimmy at retzlaff.com (Jimmy Retzlaff) Date: Thu, 8 Dec 2011 01:57:16 -0800 Subject: mrjob v0.3.0 released Message-ID: What is mrjob? ----------------------- mrjob is a Python package that helps you write and run Hadoop Streaming jobs. mrjob fully supports Amazon's Elastic MapReduce (EMR) service, which allows you to buy time on a Hadoop cluster on an hourly basis. It also works with your own Hadoop cluster. Some important features: * Run jobs on EMR, your own Hadoop cluster, or locally (for testing). * Write multi-step jobs (one map-reduce step feeds into the next) * Duplicate your production environment inside Hadoop * Upload your source tree and put it in your job's $PYTHONPATH * Run make and other setup scripts * Set environment variables (e.g. $TZ) * Easily install python packages from tarballs (EMR only) * Setup handled transparently by mrjob.conf config file * Automatically interpret error logs from EMR * SSH tunnel to hadoop job tracker on EMR * Minimal setup * To run on EMR, set $AWS_ACCESS_KEY_ID and $AWS_SECRET_ACCESS_KEY * To run on your Hadoop cluster, install simplejson and make sure $HADOOP_HOME is set. More info: * Install mrjob: pip install mrjob -OR- easy_install mrjob * Documentation: http://packages.python.org/mrjob/ * PyPI: http://pypi.python.org/pypi/mrjob * Mailing list: http://groups.google.com/group/mrjob * Development is hosted at github: http://github.com/Yelp/mrjob What's new? -------------------- mrjob v0.3.0 is a major new release. Full details are at http://packages.python.org/mrjob/whats-new.html - here are a few highlights: v0.3.0, 2011-12-07 * Combiners * *_init() and *_final() for mappers, reducers, and combiners * Custom option parsers * Job flow pooling on EMR (saves time and money!) * SSH log fetching * New EMR diagnostic tools A big thanks to the contributors to this release: Steve Johnson, Dave Marin, Wahbeh Qardaji, Derek Wilson, Jordan Andersen, and Benjamin Goldenberg! -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Thu Dec 8 05:24:30 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 8 Dec 2011 02:24:30 -0800 Subject: how to test attribute existence of feedparser objects In-Reply-To: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> References: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> Message-ID: On Thu, Dec 8, 2011 at 1:34 AM, HansPeter wrote: > Hi, > > While using the feedparser library for downloading RSS feeds some of > the blog entries seem to have no title. > > ?File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__ > AttributeError: object has no attribute 'title' > > Is there a way to test the existence of an attribute? > > I can use an exception but like below to see whether it exists but > this is a clumsy way since the function has to return the title. hasattr(obj, attr_name) See docs.python.org/dev/library/functions.html#hasattr That said, sounds like it won't make much difference in the particular case you mention. Also, never use a bare "except:" clause, unless you know what you're doing and have a really good reason. Do "except AttributeError" in this case. Cheers, Chris -- http://rebertia.com From marco.rucci at gmail.com Thu Dec 8 06:02:23 2011 From: marco.rucci at gmail.com (marco.rucci at gmail.com) Date: Thu, 8 Dec 2011 03:02:23 -0800 (PST) Subject: Insert trusted timestamp to PDF In-Reply-To: References: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Message-ID: <8925363.67.1323342143055.JavaMail.geo-discussion-forums@yqgo15> On Thursday, December 8, 2011 8:59:31 AM UTC+1, Heged?s, Ervin wrote: > > The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) > > I'm afraid that's not good for us - we need to propagate PDF > files in enbedded mode. I'll do some research and let you know if it is feasible. Anyway, it should be possible to embed it later. > > If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. > > sounds good - what do you think, when will you release that > about? The API is basically ready. We are only missing the authentication method. In less then a couple weeks it should be complete. If you want, I can give you access to a beta version of the API, so you can start integrating the service with your application and then add the required authentication process. Can I contact you privately by email? Thanks for your interest, Marco. From marco.rucci at gmail.com Thu Dec 8 06:02:23 2011 From: marco.rucci at gmail.com (marco.rucci at gmail.com) Date: Thu, 8 Dec 2011 03:02:23 -0800 (PST) Subject: Insert trusted timestamp to PDF In-Reply-To: References: <29302724.405.1323329960258.JavaMail.geo-discussion-forums@vbjs5> Message-ID: <8925363.67.1323342143055.JavaMail.geo-discussion-forums@yqgo15> On Thursday, December 8, 2011 8:59:31 AM UTC+1, Heged?s, Ervin wrote: > > The timestamp is applied to the pdf in detached mode (i.e. as a separate .tsr file) > > I'm afraid that's not good for us - we need to propagate PDF > files in enbedded mode. I'll do some research and let you know if it is feasible. Anyway, it should be possible to embed it later. > > If you need to integrate the timestamping process in a python app, be patient, we will release an API and a command line version very soon. > > sounds good - what do you think, when will you release that > about? The API is basically ready. We are only missing the authentication method. In less then a couple weeks it should be complete. If you want, I can give you access to a beta version of the API, so you can start integrating the service with your application and then add the required authentication process. Can I contact you privately by email? Thanks for your interest, Marco. From andrea.crotti.0 at gmail.com Thu Dec 8 06:10:07 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 08 Dec 2011 11:10:07 +0000 Subject: Questions about LISP and Python. In-Reply-To: References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: <4EE09B0F.4070009@gmail.com> On 12/08/2011 04:10 AM, Rick Johnson wrote: > ... > > Why has GvR not admonished the atrocious behavior of some people in > this community? Why has GvR not admitted publicly the hideous state of > IDLE and Tkinter? Where is the rally call? Where is the community > spirit? The future of Pythin is in your hands Mr. Van Rossum. Will you > step up and do what needs to be done? Will you have the courage? i > sincerely hope so. If not, please step down and allow someone to lead. > I await the supreme commanders ascent to power. This last part seems like the "lord of the rings", but I guess (even if still hope not) that he's serious :D Supposing even that Guido resigns, why do you think that the power should go to you? Power is not something that you can claim for, you have to earn the right, and ranting doesn't normally buy anything ;) From rosuav at gmail.com Thu Dec 8 06:20:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Dec 2011 22:20:56 +1100 Subject: Questions about LISP and Python. In-Reply-To: <4EE09B0F.4070009@gmail.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> <4EE09B0F.4070009@gmail.com> Message-ID: On Thu, Dec 8, 2011 at 10:10 PM, Andrea Crotti wrote: > Supposing even that Guido resigns, why do you think that the power should go > to you? > Power is not something that you can claim for, you have to earn the right, > and > ranting doesn't normally buy anything ;) Power is something you create. Did you know that you, too, can be head of an open source project the size of Python? It's easy. Just start one! Go to GitHub, Google Code, BitBucket, SourceForge (they all seem to have exactly two capital letters in them - must be important), or any other such site, and start coding your new and most awesome language. You can even make yourself a mailing list so that you can get your revenge on the Evil Python Mafia (hereunder and hereafter the EPM) by ignoring every patch ever submitted! Chris Angelico From kmichael.aye at gmail.com Thu Dec 8 06:22:27 2011 From: kmichael.aye at gmail.com (K.-Michael Aye) Date: Thu, 8 Dec 2011 12:22:27 +0100 Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On 2011-12-08 08:59:26 +0000, Thomas Rachel said: > Am 08.12.2011 08:18 schrieb 88888 Dihedral: >> I use the @ decorator to behave exactly like a c macro that >> does have fewer side effects. >> >> I am wondering is there other interesting methods to do the >> jobs in Python? > > In combination with a generator, you can do many funny things. > > > For example, you can build up a string: > > def mkstring(f): > """Turns a string generator into a string, > joining with ", ". > """ > return ", ".join(f()) > > def create_answer(): > @mkstring > def people(): > yield "Anna" > yield "John" > yield "Theo" > > return "The following people were here: " + people > > > Many other things are thinkable... > > > Thomas I am still perplexed about decorators though, am happily using Python for many years without them, but maybe i am missing something? For example in the above case, if I want the names attached to each other with a comma, why wouldn't I just create a function doing exactly this? Why would I first write a single name generator and then decorate it so that I never can get single names anymore (this is the case, isn't it? Once decorated, I can not get the original behaviour of the function anymore. So, above, why not def mkstring(mylist): with the same function declaration and then just call it with a list of names that I generate elsewhere in my program? I just can't identify the use-case for decorators, but as I said, maybe I am missing something. Michael From anacrolix at gmail.com Thu Dec 8 06:27:03 2011 From: anacrolix at gmail.com (Matt Joiner) Date: Thu, 8 Dec 2011 22:27:03 +1100 Subject: Questions about LISP and Python. In-Reply-To: <4EE09B0F.4070009@gmail.com> References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> <4EE09B0F.4070009@gmail.com> Message-ID: Guido is too busy secretly pouring his cruelty and malice into a master ring to answer trolls. Help yourself to a lesser ring on your way out. On Dec 8, 2011 10:14 PM, "Andrea Crotti" wrote: > On 12/08/2011 04:10 AM, Rick Johnson wrote: > >> ... >> >> Why has GvR not admonished the atrocious behavior of some people in >> this community? Why has GvR not admitted publicly the hideous state of >> IDLE and Tkinter? Where is the rally call? Where is the community >> spirit? The future of Pythin is in your hands Mr. Van Rossum. Will you >> step up and do what needs to be done? Will you have the courage? i >> sincerely hope so. If not, please step down and allow someone to lead. >> I await the supreme commanders ascent to power. >> > > This last part seems like the "lord of the rings", but I guess (even if > still hope not) > that he's serious :D > > Supposing even that Guido resigns, why do you think that the power should > go > to you? > Power is not something that you can claim for, you have to earn the right, > and > ranting doesn't normally buy anything ;) > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bsemene at cyanide-studio.com Thu Dec 8 06:28:21 2011 From: bsemene at cyanide-studio.com (Bastien Semene) Date: Thu, 08 Dec 2011 12:28:21 +0100 Subject: sending a variable to an imported module Message-ID: <4EE09F55.2050504@cyanide-studio.com> Hi list, I'm trying to pass a variable to an imported module without singletons. I've seen in the doc, and tested that I can't use global to do it : === module.py === def testf(): print test === main.py === global test test = 1 imported_module = __import__(module, globals(), locals(), [], -1) importmodule.testf() === output === NameError: global name 'test' is not defined While I was reading many (many) threads about singleton I read people claiming that singletons can always be avoided (I can't remeber the most relevant thread on stackoverflow). I don't want to start a new debate about singletons, I think Internet has enough debates yet. But in my case I'd like to access this variable anywhere and at anytime without having to pass it as a parameter everywhere (this variable is a configuration manager object). How can I achieve that without singletons ? I'm beginner in Python, that's why I'm maybe missing something obvious. Thanks, Bastien Semene From rosuav at gmail.com Thu Dec 8 06:43:12 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 8 Dec 2011 22:43:12 +1100 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > I am still perplexed about decorators though, am happily using Python for > many years without them, but maybe i am missing something? > For example in the above case, if I want the names attached to each other > with a comma, why wouldn't I just create a function doing exactly this? Why > would I first write a single name generator and then decorate it so that I > never can get single names anymore (this is the case, isn't it? Once > decorated, I can not get the original behaviour of the function anymore. The example given is a toy. It's hardly useful. However, there are a number of handy uses for decorators; mostly, they consist of giving a single simple keyword to a complicated set of logic. One example is the @classmethod and @staticmethod decorators - the code to implement them could be uglier than nested inline assembly, but you don't have to care, because you just type "@staticmethod" in front of your def statement and it does its magic. Here's a handy trick that I'm sure someone has done in a more sophisticated way: def trace(func): if debugmode: return lambda *a,**ka: (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] return func Then you put @trace in front of all your functions, and if debugmode is False, nothing will be done - but set it to true, and you get console output at the entry and exit of each function. >>> @trace def test(x): print("Test! "+x) return 5 >>> test("asdf") >test Test! asdf References: <4EE09F55.2050504@cyanide-studio.com> Message-ID: On 8 December 2011 11:28, Bastien Semene wrote: > Hi list, > > I'm trying to pass a variable to an imported module without singletons. > I've seen in the doc, and tested that I can't use global to do it : > > === module.py === > def testf(): > ?print test > > > === main.py === > global test > test = 1 > > imported_module = __import__(module, globals(), locals(), [], -1) > > importmodule.testf() > > === output === > NameError: global name 'test' is not defined > > > > While I was reading many (many) threads about singleton I read people > claiming that singletons can always be avoided (I can't remeber the most > relevant thread on stackoverflow). > I don't want to start a new debate about singletons, I think Internet has > enough debates yet. > > But in my case I'd like to access this variable anywhere and at anytime > without having to pass it as a parameter everywhere (this variable is a > configuration manager object). > How can I achieve that without singletons ? > I'm beginner in Python, that's why I'm maybe missing something obvious. Put it in its own module and import that module in the places where it is needed. E.g. in your example: == settings.py == test = 1 == module.py == import settings def testf(): print settings.testf == main.py == import module module.testf() In every module that you need 'test', import settings and you will be able to access test. This is why singletons are often not needed in Python (just like most other design patterns). -- Arnaud From d at davea.name Thu Dec 8 07:15:26 2011 From: d at davea.name (Dave Angel) Date: Thu, 08 Dec 2011 07:15:26 -0500 Subject: sending a variable to an imported module In-Reply-To: <4EE09F55.2050504@cyanide-studio.com> References: <4EE09F55.2050504@cyanide-studio.com> Message-ID: <4EE0AA5E.8000005@davea.name> On 12/08/2011 06:28 AM, Bastien Semene wrote: > Hi list, > > I'm trying to pass a variable to an imported module without singletons. > I've seen in the doc, and tested that I can't use global to do it : > > === module.py === > def testf(): > print test > > > === main.py === > global test > test = 1 > > imported_module = __import__(module, globals(), locals(), [], -1) > > importmodule.testf() > > === output === > NameError: global name 'test' is not defined > > Please paste your code and your stacktrace, don't retype them. In the above, you spelled 'imported_module" two different ways, and forgot the quotes around "modue", so it couldn't run. There are probably other problems, but what's the point? > > While I was reading many (many) threads about singleton I read people > claiming that singletons can always be avoided (I can't remeber the > most relevant thread on stackoverflow). > I don't want to start a new debate about singletons, I think Internet > has enough debates yet. > > But in my case I'd like to access this variable anywhere and at > anytime without having to pass it as a parameter everywhere (this > variable is a configuration manager object). > How can I achieve that without singletons ? > I'm beginner in Python, that's why I'm maybe missing something obvious. global variables are global only within their own module, but you probably knew that. And using the global keyword in main.py isn't accomplishing anything. Since you're not inside a def or a class, test is already global, as soon as you give it a value. You don't pass values to a module, you load the module. And if the module doesn't have any top-level code, you can "monkey-patch" it to your heart's content, on lines following. If mymodule.py doesn't have a global value test, and you wish it did, you can simply do something like: import mymodule mymodule.test = 42 This attribute of mymodule is totally unrelated to one of the same name in main.py. if you want to refer to it, or to change it again, from main.py, you'd have to use mymodule.test. If the module had top-level code that needed to see your new global, then you'd have a problem, because you can't put it there till after the import returns. Now, most of the time when this sort of thing happens, what you really want is to define another module whose only purpose is to supply these common values. That module should get imported by both your script and your module. -- DaveA From bsemene at cyanide-studio.com Thu Dec 8 08:47:00 2011 From: bsemene at cyanide-studio.com (Bastien Semene) Date: Thu, 08 Dec 2011 14:47:00 +0100 Subject: sending a variable to an imported module In-Reply-To: <4EE0AA5E.8000005@davea.name> References: <4EE09F55.2050504@cyanide-studio.com> <4EE0AA5E.8000005@davea.name> Message-ID: <4EE0BFD4.2020608@cyanide-studio.com> Thanks both, Putting the variable inside a module works well. As the content is an object created inside another module I'm using this trick : module.CONFIG = module.load() So the variable is handled by the module that creates/use it, easy to use and pretty "native" to understand. Le 08/12/2011 13:15, Dave Angel a ?crit : > On 12/08/2011 06:28 AM, Bastien Semene wrote: >> Hi list, >> >> I'm trying to pass a variable to an imported module without singletons. >> I've seen in the doc, and tested that I can't use global to do it : >> >> === module.py === >> def testf(): >> print test >> >> >> === main.py === >> global test >> test = 1 >> >> imported_module = __import__(module, globals(), locals(), [], -1) >> >> importmodule.testf() >> >> === output === >> NameError: global name 'test' is not defined >> >> > Please paste your code and your stacktrace, don't retype them. In the > above, you spelled 'imported_module" two different ways, and forgot > the quotes around "modue", so it couldn't run. There are probably > other problems, but what's the point? >> >> While I was reading many (many) threads about singleton I read people >> claiming that singletons can always be avoided (I can't remeber the >> most relevant thread on stackoverflow). >> I don't want to start a new debate about singletons, I think Internet >> has enough debates yet. >> >> But in my case I'd like to access this variable anywhere and at >> anytime without having to pass it as a parameter everywhere (this >> variable is a configuration manager object). >> How can I achieve that without singletons ? >> I'm beginner in Python, that's why I'm maybe missing something obvious. > global variables are global only within their own module, but you > probably knew that. > > And using the global keyword in main.py isn't accomplishing anything. > Since you're not inside a def or a class, test is already global, as > soon as you give it a value. > > You don't pass values to a module, you load the module. And if the > module doesn't have any top-level code, you can "monkey-patch" it to > your heart's content, on lines following. > > If mymodule.py doesn't have a global value test, and you wish it did, > you can simply do something like: > import mymodule > mymodule.test = 42 > This attribute of mymodule is totally unrelated to one of the same > name in main.py. if you want to refer to it, or to change it again, > from main.py, you'd have to use mymodule.test. > > If the module had top-level code that needed to see your new global, > then you'd have a problem, because you can't put it there till after > the import returns. > > Now, most of the time when this sort of thing happens, what you really > want is to define another module whose only purpose is to supply these > common values. That module should get imported by both your script > and your module. > > From sajuptpm at gmail.com Thu Dec 8 09:09:05 2011 From: sajuptpm at gmail.com (sajuptpm) Date: Thu, 8 Dec 2011 06:09:05 -0800 (PST) Subject: Documentation using Sphinx Message-ID: <6107ecd2-9acf-4334-8877-ca8b9ff90752@b14g2000prn.googlegroups.com> Hi, I am trying source code documentation using Sphinx. Here i have to copy paste all modules in to *.rst file, that is painful. Have any way to create documentation (doc for all modules, classes and methods in the project directory) from project folder quickly. I also plannig to add a code browsing facility in the document, for example: code should display if we click on a method/class name in the doc, does sphinx has this feature??? or what tool i have to choose for that ???. From solutionmanualgroup1 at gmail.com Thu Dec 8 09:22:33 2011 From: solutionmanualgroup1 at gmail.com (kevin norton) Date: Thu, 8 Dec 2011 06:22:33 -0800 (PST) Subject: Test Banks and Solution Manuals Updated List 2012 Message-ID: Hi dear students; We are SolutionManualGroup.We established SolutionManualGroup in 2004. SolutionManualGroup is the leading provider of Solution Manuals and Test Banks help for college and graduate degree students.We have solution manuals and test banks for a competitive price. We also have other manuals more than 3000..We have a lot of solutions manual and test banks in low price to get solution manual you want please send message to us. #Feel free to contact us. solutionmanualgroup [at] gmail [dot] com #If your interested do let me know at http://solutionmanualgroup.blogspot.com/ #All solution manuals are in PDF format... #Full List of Solution Manuals and Test Banks are at http://solutionmanualgroup.blogspot.com/ #Please visit our website and search click Ctrl+F Regards SolutionManualGroup ( solutionmanualgroup at gmail.com ) List of Some Testbanks & Solutions Manual: -A Transition to Advanced Mathematics by Douglas Smith, Maurice Eggen 5 Solution Manual -Accounting by Carl S. Warren, James M. Reeve 24 Instructor's Manual -Accounting by Carl S. Warren, James M. Reeve 24 Test Bank -Accounting and Auditing Research Tools and Strategies by Weirich, Pearson, Churyk 7 Solution Manual -Accounting and Auditing Research Tools and Strategies by Weirich, Pearson, Churyk 7 Cases Solutions -Accounting for Governmental and Nonprofit Entities by Wilson 15 Solution Manual -Accounting for Governmental and Nonprofit Entities by Wilson 15 Test Bank -Accounting Information Systems by George H. Bodnar ,William S. Hopwood Solution Manual -Accounting Information Systems by George H. Bodnar ,William S. Hopwood Test Bank -Accounting Information Systems by Gelinas 8 Solution Manual -Accounting Information Systems by Gelinas 8 Test Bank -Accounting Information Systems by Romney 11 Solution Manual -Accounting Information Systems by Romney 11 Test Bank -Accounting Information Systems by Romney, Steinbart 12 Instructor's Manual -Accounting Information Systems by Romney, Steinbart 12 Solution Manual -Accounting Information Systems by Romney, Steinbart 12 Test Bank -Accounting Information Systems by James A. Hall 7 Solution Manual -Accounting Information Systems by James A. Hall 7 Test Bank -Accounting Information Systems Basic Concepts & Current Issues by Robert L. Hurt 2 Solution Manual -Accounting Information Systems Basic Concepts & Current Issues by Robert L. Hurt 2 Test Bank -Accounting for Decision Making and Control by Zimmerman 6 Solution Manual -Accounting for Decision Making and Control by Zimmerman 6 Test Bank -Accounting for Decision Making and Control by Zimmerman 7 Solution Manual -Accounting for Decision Making and Control by Zimmerman 7 Test Bank ### Full List of Solution Manuals and Test Banks are at http://solutionmanualgroup.blogspot.com/ #### From roy at panix.com Thu Dec 8 09:23:33 2011 From: roy at panix.com (Roy Smith) Date: Thu, 08 Dec 2011 09:23:33 -0500 Subject: Misleading error message of the day Message-ID: I just spent a while beating my head against this one. # Python 2.6 >>> a, b = 'foo' Traceback (most recent call last): File "", line 1, in ValueError: too many values to unpack The real problem is that there's too *few* values to unpack! It should have been a, b = 'foo', 'bar' I understand why it's generating the exception it does (the string is an iterable), but man, did that message throw off my thought processes and lead me down some totally bogus debugging paths. It's an unusual case to want to unpack a string. Maybe the message should changed to "too {many, few} values to unpack (are you sure you wanted to unpack a string?)" if the RHS is a basestring? From andrea.crotti.0 at gmail.com Thu Dec 8 09:39:27 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 08 Dec 2011 14:39:27 +0000 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <4EE0CC1F.602@gmail.com> On 12/08/2011 02:23 PM, Roy Smith wrote: > I just spent a while beating my head against this one. > > # Python 2.6 >>>> a, b = 'foo' > Traceback (most recent call last): > File "", line 1, in > ValueError: too many values to unpack > > The real problem is that there's too *few* values to unpack! It should > have been > > a, b = 'foo', 'bar' > > I understand why it's generating the exception it does (the string is an > iterable), but man, did that message throw off my thought processes and > lead me down some totally bogus debugging paths. > > It's an unusual case to want to unpack a string. Maybe the message > should changed to "too {many, few} values to unpack (are you sure you > wanted to unpack a string?)" if the RHS is a basestring? I had a few errors sometimes because I thought I passed in a list while I passed only a string, which since it's still iterable would just work but explode later. A nicer message wouldn't have really actually helped though, not sure it's worth to make an exception for such a thing.. From rosuav at gmail.com Thu Dec 8 09:40:53 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 01:40:53 +1100 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: On Fri, Dec 9, 2011 at 1:23 AM, Roy Smith wrote: > I just spent a while beating my head against this one. > > # Python 2.6 >>>> a, b = 'foo' > Traceback (most recent call last): > ?File "", line 1, in > ValueError: too many values to unpack Definitely weird! I smell a job for a linter though. If you had just happened to have a two-character string there, it would have quietly succeeded, and left you wondering what was going on - imho rather worse. This isn't something for the language to solve; the same issue would come up if you had something like: a=[1,2,3] b=[4,5,6] c,d=a # oops, mucked up the "a,b" side Or any other iterable. Looks to me like a chance for an informational note from your lint facility, not a change to the language. ChrisA From robert.kern at gmail.com Thu Dec 8 09:47:02 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 08 Dec 2011 14:47:02 +0000 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: On 12/8/11 2:23 PM, Roy Smith wrote: > I just spent a while beating my head against this one. > > # Python 2.6 >>>> a, b = 'foo' > Traceback (most recent call last): > File "", line 1, in > ValueError: too many values to unpack > > The real problem is that there's too *few* values to unpack! It should > have been > > a, b = 'foo', 'bar' > > I understand why it's generating the exception it does (the string is an > iterable), but man, did that message throw off my thought processes and > lead me down some totally bogus debugging paths. > > It's an unusual case to want to unpack a string. Maybe the message > should changed to "too {many, few} values to unpack (are you sure you > wanted to unpack a string?)" if the RHS is a basestring? Would including the respective numbers help your thought processes? ValueError: too many values to unpack (expected 2, got 3) -- 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 From jeanmichel at sequans.com Thu Dec 8 10:03:38 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 08 Dec 2011 16:03:38 +0100 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <4EE0D1CA.7080901@sequans.com> Roy Smith wrote: > I just spent a while beating my head against this one. > > # Python 2.6 > >>>> a, b = 'foo' >>>> > Traceback (most recent call last): > File "", line 1, in > ValueError: too many values to unpack > > The real problem is that there's too *few* values to unpack! It should > have been > > a, b = 'foo', 'bar' > > I understand why it's generating the exception it does (the string is an > iterable), but man, did that message throw off my thought processes and > lead me down some totally bogus debugging paths. > > It's an unusual case to want to unpack a string. Maybe the message > should changed to "too {many, few} values to unpack (are you sure you > wanted to unpack a string?)" if the RHS is a basestring? > string are iterable, considering this, the error is correct. Values to unpack in 'foo' are 'f', 'o', 'o' > a,b,c = 'foo' > print a,b,c f o o JM From modelnine at modelnine.org Thu Dec 8 10:16:56 2011 From: modelnine at modelnine.org (Heiko Wundram) Date: Thu, 08 Dec 2011 16:16:56 +0100 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <4EE0D4E8.4060600@modelnine.org> Am 08.12.2011 15:47, schrieb Robert Kern: > Would including the respective numbers help your thought processes? > ValueError: too many values to unpack (expected 2, got 3) Not possible in the general case (as the right-hand side might be an arbitrary iterable/iterator...). -- --- Heiko. From andrea.crotti.0 at gmail.com Thu Dec 8 10:17:56 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 08 Dec 2011 15:17:56 +0000 Subject: Documentation using Sphinx In-Reply-To: <6107ecd2-9acf-4334-8877-ca8b9ff90752@b14g2000prn.googlegroups.com> References: <6107ecd2-9acf-4334-8877-ca8b9ff90752@b14g2000prn.googlegroups.com> Message-ID: <4EE0D524.7080909@gmail.com> On 12/08/2011 02:09 PM, sajuptpm wrote: > Hi, > I am trying source code documentation using Sphinx. Here i have to > copy paste all modules in to *.rst file, that is painful. Have any way > to create documentation (doc for all modules, classes and methods in > the project directory) from project folder quickly. I also plannig to > add a code browsing facility in the document, for example: code should > display if we click on a method/class name in the doc, does sphinx has > this feature??? or what tool i have to choose for that ???. You should never copy and paste anything ;) Have a look at the autodoc plugin. From roy at panix.com Thu Dec 8 10:30:05 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:30:05 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <32921150.1028.1323358205974.JavaMail.geo-discussion-forums@yqbz41> On Thursday, December 8, 2011 9:47:02 AM UTC-5, Robert Kern wrote: > Would including the respective numbers help your thought processes? > > ValueError: too many values to unpack (expected 2, got 3) I don't know if that would have done the trick for me on this particular one. On the other hand, adding "expected X, got Y" to the message would generally be a good thing. From roy at panix.com Thu Dec 8 10:30:05 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:30:05 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <32921150.1028.1323358205974.JavaMail.geo-discussion-forums@yqbz41> On Thursday, December 8, 2011 9:47:02 AM UTC-5, Robert Kern wrote: > Would including the respective numbers help your thought processes? > > ValueError: too many values to unpack (expected 2, got 3) I don't know if that would have done the trick for me on this particular one. On the other hand, adding "expected X, got Y" to the message would generally be a good thing. From roy at panix.com Thu Dec 8 10:33:50 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:33:50 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant wrote: > string are iterable, considering this, the error is correct. Yes, I understand that the exception is correct. I'm not saying the exception should be changed, just that we have the opportunity to produce a more useful error message. The exception would be equally correct if it was: ValueError: you did something wrong but most people would probably agree that it's not the most useful message that could have been produced. From roy at panix.com Thu Dec 8 10:33:50 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:33:50 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant wrote: > string are iterable, considering this, the error is correct. Yes, I understand that the exception is correct. I'm not saying the exception should be changed, just that we have the opportunity to produce a more useful error message. The exception would be equally correct if it was: ValueError: you did something wrong but most people would probably agree that it's not the most useful message that could have been produced. From roy at panix.com Thu Dec 8 10:42:46 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:42:46 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> On Thursday, December 8, 2011 10:16:56 AM UTC-5, Heiko Wundram wrote: > Am 08.12.2011 15:47, schrieb Robert Kern: > > Would including the respective numbers help your thought processes? > > ValueError: too many values to unpack (expected 2, got 3) > > Not possible in the general case (as the right-hand side might be an > arbitrary iterable/iterator...). Why not? Take this example: def i(): i = 0 while True: print "returning:", i yield i i += 1 a, b = i() ./iter.py returning: 0 returning: 1 returning: 2 Traceback (most recent call last): File "./iter.py", line 10, in a, b = i() ValueError: too many values to unpack The exception was raised when i() returned it's third value, so saying "expected 2, got 3" is exactly correct. Yes, it is true that it might have gotten more if it kept going, but that's immaterial; the fact that it got to 3 is what caused the Holy Hand Grenade to be thrown. From roy at panix.com Thu Dec 8 10:42:46 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 07:42:46 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: Message-ID: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> On Thursday, December 8, 2011 10:16:56 AM UTC-5, Heiko Wundram wrote: > Am 08.12.2011 15:47, schrieb Robert Kern: > > Would including the respective numbers help your thought processes? > > ValueError: too many values to unpack (expected 2, got 3) > > Not possible in the general case (as the right-hand side might be an > arbitrary iterable/iterator...). Why not? Take this example: def i(): i = 0 while True: print "returning:", i yield i i += 1 a, b = i() ./iter.py returning: 0 returning: 1 returning: 2 Traceback (most recent call last): File "./iter.py", line 10, in a, b = i() ValueError: too many values to unpack The exception was raised when i() returned it's third value, so saying "expected 2, got 3" is exactly correct. Yes, it is true that it might have gotten more if it kept going, but that's immaterial; the fact that it got to 3 is what caused the Holy Hand Grenade to be thrown. From modelnine at modelnine.org Thu Dec 8 10:50:44 2011 From: modelnine at modelnine.org (Heiko Wundram) Date: Thu, 08 Dec 2011 16:50:44 +0100 Subject: Misleading error message of the day In-Reply-To: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> Message-ID: <4EE0DCD4.30501@modelnine.org> Am 08.12.2011 16:42, schrieb Roy Smith: > The exception was raised when i() returned it's third value, so saying "expected 2, got 3" is exactly correct. Yes, it is true that it might have gotten more if it kept going, but that's immaterial; the fact that it got to 3 is what caused the Holy Hand Grenade to be thrown. Please explain how that error message (in case you're not aiming at the actual count of elements in the source) differs from the curent wording "too many values", as you're simply displaying "expected n, got n+1" where n is visible from the immediate exception output... -- --- Heiko. From andrea.crotti.0 at gmail.com Thu Dec 8 10:55:16 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 08 Dec 2011 15:55:16 +0000 Subject: Misleading error message of the day In-Reply-To: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> Message-ID: <4EE0DDE4.4080802@gmail.com> On 12/08/2011 03:42 PM, Roy Smith wrote: > > Why not? Take this example: > > def i(): > i = 0 > while True: > print "returning:", i > yield i > i += 1 > > a, b = i() > > ./iter.py > returning: 0 > returning: 1 > returning: 2 > Traceback (most recent call last): > File "./iter.py", line 10, in > a, b = i() > ValueError: too many values to unpack > > The exception was raised when i() returned it's third value, so saying "expected 2, got 3" is exactly correct. Yes, it is true that it might have gotten more if it kept going, but that's immaterial; the fact that it got to 3 is what caused the Holy Hand Grenade to be thrown. Yes but how do you know how many values you generated when it quits? I mean I don't know how it work internally, but it should keep a temporary list of the yielded values to be able to find out how many values are there.. From rosuav at gmail.com Thu Dec 8 11:21:31 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 03:21:31 +1100 Subject: Misleading error message of the day In-Reply-To: <4EE0DDE4.4080802@gmail.com> References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <4EE0DDE4.4080802@gmail.com> Message-ID: On Fri, Dec 9, 2011 at 2:55 AM, Andrea Crotti wrote: > Yes but how do you know how many values you generated when it quits? > I mean I don't know how it work internally, but it should keep a temporary > list of the yielded values to be able to find out how many values are > there.. Iterator unpacking works roughly thus: 1) Count up how many results you need (call that N) 2) N times, get a value from the iterator. If StopIteration is raised, swallow it and raise ValueError because there were too few values. 3) Attempt to get one more value from the iterator. If StopIteration is NOT raised, raise ValueError because there were too many values. At no point is the "total size" of the iterator counted (it could, after all, be infinite). When ValueError is raised, all that's known is that StopIteration wasn't raised at the end of the process. ChrisA From lie.1296 at gmail.com Thu Dec 8 11:21:52 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 09 Dec 2011 03:21:52 +1100 Subject: Need some IPC pointers In-Reply-To: <4ED69A1A.3080609@gmail.com> References: <4ED69A1A.3080609@gmail.com> Message-ID: On 12/01/2011 08:03 AM, Andrew Berg wrote: > I've done some research, but I'm not sure what's most appropriate for my > situation. What I want to do is have a long running process that spawns > processes (that aren't necessarily written in Python) and communicates > with them. The children can be spawned at any time and communicate at > any time. Being able to communicate with non-local processes would be > nice, but is not necessary. The implementation needs to be > cross-platform, but child processes will use the same OS as the parent > during runtime. > I don't think I'll ever need to transfer anything complicated or large - > just strings or possibly tuples/lists. I'd rather not go outside the > standard library (but I'd consider it). I don't need to worry about > compatibility with older Python versions; if it only works with Python > 3.2, that's not a problem. > I'm thinking sockets, but perhaps there's something simpler/easier. > Considering your requirements, I'd suggest a RESTful web service. It is fairly trivial to write HTTP clients in most languages, and python's standard library comes with a simple HTTP server so writing the server is easy as well. In context, the "long running process" will be the "server", the "children processes" will be the "client". Writing HTTP client is fairly trivial in most languages, the protocol is platform independent, and it is fairly trivial to communicate with non-local processes over the LAN or the Internet. As a plus, it's well standardized. As the data interchange format, I suggest either xml or json. There is a library for xml and json in almost any popular languages. Python comes with both. From kmichael.aye at gmail.com Thu Dec 8 11:24:11 2011 From: kmichael.aye at gmail.com (K.-Michael Aye) Date: Thu, 8 Dec 2011 17:24:11 +0100 Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On 2011-12-08 11:43:12 +0000, Chris Angelico said: > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: >> I am still perplexed about decorators though, am happily using Python for >> many years without them, but maybe i am missing something? >> For example in the above case, if I want the names attached to each other >> with a comma, why wouldn't I just create a function doing exactly this? Why >> would I first write a single name generator and then decorate it so that I >> never can get single names anymore (this is the case, isn't it? Once >> decorated, I can not get the original behaviour of the function anymore. > > The example given is a toy. It's hardly useful. However, there are a > number of handy uses for decorators; mostly, they consist of giving a > single simple keyword to a complicated set of logic. One example is > the @classmethod and @staticmethod decorators - the code to implement > them could be uglier than nested inline assembly, but you don't have > to care, because you just type "@staticmethod" in front of your def > statement and it does its magic. > > Here's a handy trick that I'm sure someone has done in a more > sophisticated way: > > def trace(func): > if debugmode: > return lambda *a,**ka: > (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] > return func > > Then you put @trace in front of all your functions, and if debugmode > is False, nothing will be done - but set it to true, and you get > console output at the entry and exit of each function. > >>>> @trace > def test(x): > print("Test! "+x) > return 5 > >>>> test("asdf") >> test > Test! asdf > 5 > > Again, it's helpful because it condenses all the logic (including the > 'debugmode' flag) down to a single high level directive: "Trace this > function". > > ChrisA I understand this one, it seems really useful. And maybe i start to sense some more applicability. Like this, with extra flags that could be set at run time, I could influence the way a function is executed without designing the function too complex, but by decorating it, which at the end could be easier to read than complicated if-then statements in the function. Thanks for your example. Michael From andrea.crotti.0 at gmail.com Thu Dec 8 11:34:54 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Thu, 08 Dec 2011 16:34:54 +0000 Subject: adding elements to set Message-ID: <4EE0E72E.2040104@gmail.com> I've wasted way too much time for this, which is surely not a Python bug, not something that surprised me a lot. I stupidly gave for granted that adding an object to a set would first check if there are equal elements inside, and then add it. As shown below this is not clearly the case.. Is it possible to get that behaviour implementing another magic method in my C class or I just have use another function to check (as I'm doing now). class C(object): def __init__(self, x): self.x = x def __eq__(self, other): return self.x == other.x if __name__ == '__main__': s = set() c1 = C(1) c2 = C(1) assert c1 == c2 s.add(c1) s.add(c2) print len(s) From rosuav at gmail.com Thu Dec 8 11:38:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 03:38:35 +1100 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: On Fri, Dec 9, 2011 at 3:24 AM, K.-Michael Aye wrote: > I understand this one, it seems really useful. And maybe i start to sense > some more applicability. Like this, with extra flags that could be set at > run time, I could influence the way a function is executed without designing > the function too complex, but by decorating it, which at the end could be > easier to read than complicated if-then statements in the function. Right, and less duplication usually results in less bugs, too. Obviously if you go to any greater level of complexity than this you'd want to break the lambda out into a proper function, and possibly play with an indent level (increment on call, decrement on return). You can also easily add the run-time check, and anything else you want to do, too. It'll still all be buried away in the definition of trace(), and on functions that you want to trace, you need still only have "@trace" and nothing else. One piece of sophistication that I would rather like to see, but don't know how to do. Instead of *args,**kwargs, is it possible to somehow copy in the function's actual signature? I was testing this out in IDLE, and the fly help for the function no longer gave useful info about its argument list. This is why I say that this has almost certainly been done before in a much better way. ChrisA From rosuav at gmail.com Thu Dec 8 11:47:24 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 03:47:24 +1100 Subject: adding elements to set In-Reply-To: <4EE0E72E.2040104@gmail.com> References: <4EE0E72E.2040104@gmail.com> Message-ID: On Fri, Dec 9, 2011 at 3:34 AM, Andrea Crotti wrote: > I've wasted way too much time for this, which is surely not a Python bug, > not something that surprised me a lot. > > I stupidly gave for granted that adding an object to a set would first > check if there are equal elements inside, and then add it. It checks for equality using hashes. By default, in Python 2, objects' hashes are their ids - meaning that no two of them hash alike, and you'll get duplicates in your set. (In Python 3, the default appears to be that they're unhashable and hence can't go into the set at all.) class C(object): def __init__(self, x): self.x = x def __eq__(self, other): return self.x == other.x def __hash__(self): return hash(self.x) This chains the hashing requirement to the child, just as it chains the equality check. You can then stuff your objects into a set with more expected results. ChrisA From __peter__ at web.de Thu Dec 8 11:49:42 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Dec 2011 17:49:42 +0100 Subject: adding elements to set References: <4EE0E72E.2040104@gmail.com> Message-ID: Andrea Crotti wrote: > I've wasted way too much time for this, which is surely not a Python bug, > not something that surprised me a lot. > > I stupidly gave for granted that adding an object to a set would first > check if there are equal elements inside, and then add it. > > As shown below this is not clearly the case.. > Is it possible to get that behaviour implementing another magic method > in my C class or I just have use another function to check (as I'm doing > now). > > class C(object): > > def __init__(self, x): > self.x = x > > def __eq__(self, other): > return self.x == other.x > > > if __name__ == '__main__': > s = set() > c1 = C(1) > c2 = C(1) > assert c1 == c2 > s.add(c1) > s.add(c2) > > print len(s) Python's sets are hash-based; you have to implement a __hash__() method for the elements that ensures that c1 == c2 implies hash(c1) == hash(c2). >>> class C(object): ... def __init__(self, x): self.x = x ... def __eq__(self, other): return self.x == other.x ... def __hash__(self): return hash(self.x) ... >>> c1 = C(1) >>> c2 = C(1) >>> c1 == c2 True >>> s = set() >>> s.add(c1) >>> s.add(c2) >>> len(s) 1 From bahamutzero8825 at gmail.com Thu Dec 8 11:54:54 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 08 Dec 2011 10:54:54 -0600 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <4EE0EBDE.6@gmail.com> On 12/8/2011 10:38 AM, Chris Angelico wrote: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I remember seeing this in a PEP that is planned to be implemented in 3.3. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From bahamutzero8825 at gmail.com Thu Dec 8 11:55:29 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 08 Dec 2011 10:55:29 -0600 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <4EE0EC01.2080803@gmail.com> Decorators are great for adding common functionality to several functions without duplicating code. For example, I have one for my IRC bot that checks that the person sending the command is authorized to use the command. It's only "if mask in owner list then execute function else say access denied", but it simplifies the code and the intent of each function that uses it becomes more obvious. I have another that puts functions in a common try/except clause, which again simplifies the functions and makes them easier to understand. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From __peter__ at web.de Thu Dec 8 12:32:04 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Dec 2011 18:32:04 +0100 Subject: adding elements to set References: <4EE0E72E.2040104@gmail.com> Message-ID: Chris Angelico wrote: > It checks for equality using hashes. By default, in Python 2, objects' > hashes are their ids - meaning that no two of them hash alike, and > you'll get duplicates in your set. (In Python 3, the default appears > to be that they're unhashable and hence can't go into the set at all.) $ python3.2 -c'print({object(), object()})' {, } The only thing that has changed (in 2.7) is the algorithm to calculate the hash value. The bits are rotated to turn the four least significant bits into the most signicant ones. According to a comment in Objects/objects.c the change leads to fewer hash collisions. $ python2.6 -c'o = object(); print hash(o) == id(o)' True $ python2.7 -c'o = object(); print hash(o) == id(o)' False $ python2.7 -c'o = object(); print hash(o) == id(o)>>4 | (id(o)&0xF)<<60' True $ python3.2 -c'o = object(); print(hash(o) == id(o)>>4 | (id(o)&0xF)<<60)' True From rosuav at gmail.com Thu Dec 8 12:37:18 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 04:37:18 +1100 Subject: adding elements to set In-Reply-To: References: <4EE0E72E.2040104@gmail.com> Message-ID: On Fri, Dec 9, 2011 at 4:32 AM, Peter Otten <__peter__ at web.de> wrote: > The only thing that has changed (in 2.7) is the algorithm to calculate the > hash value. The bits are rotated to turn the four least significant bits > into the most signicant ones. According to a comment in Objects/objects.c > the change leads to fewer hash collisions. Interesting, but what I saw was this: >>> class C(object): def __init__(self, x): self.x = x def __eq__(self, other): return self.x == other.x >>> s=set() >>> c1=C(1) >>> s.add(c1) Traceback (most recent call last): File "", line 1, in s.add(c1) TypeError: unhashable type: 'C' (This is in IDLE from Python 3.2 on Windows.) However, s.add(object()) works fine. So subclasses don't get that. Odd. Makes sense though - you can't get this unexpected behaviour as easily. ChrisA From python.list at tim.thechases.com Thu Dec 8 12:41:17 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 08 Dec 2011 11:41:17 -0600 Subject: Misleading error message of the day In-Reply-To: <32921150.1028.1323358205974.JavaMail.geo-discussion-forums@yqbz41> References: <32921150.1028.1323358205974.JavaMail.geo-discussion-forums@yqbz41> Message-ID: <4EE0F6BD.4090203@tim.thechases.com> On 12/08/11 09:30, Roy Smith wrote: > On Thursday, December 8, 2011 9:47:02 AM UTC-5, Robert Kern > wrote: > >> Would including the respective numbers help your thought >> processes? >> >> ValueError: too many values to unpack (expected 2, got 3) > > I don't know if that would have done the trick for me on this > particular one. On the other hand, adding "expected X, got Y" > to the message would generally be a good thing. given the nature of the message, and the interaction with iterators-of-arbitrary/infinite length, it might have to be reduced to "Expected N, got more" or for the case where you didn't get enough, you know how many you got: "Expected N, but only got M". But the extra information would certainly be useful in tracking it down. -tkc From ian.g.kelly at gmail.com Thu Dec 8 12:43:28 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Dec 2011 10:43:28 -0700 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I was testing this out in > IDLE, and the fly help for the function no longer gave useful info > about its argument list. You can use the decorator module in PyPI to do this. -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Dec 8 12:43:55 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 08 Dec 2011 17:43:55 +0000 Subject: Questions about LISP and Python. In-Reply-To: References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: <4EE0F75B.7030700@mrabarnett.plus.com> On 08/12/2011 04:10, Rick Johnson wrote: [snip] > I believe this community has a cancer. A cancer that is rotting us > from the inside. A cancer that has metastasis and is spreading like > wild fire. > The problem with a cancer is not that it rots, but that it grows uncontrollably. > *Inquisitive Joe asked:* What is the source of this cancer Rick? > > The source is a direct result of insufficient leadership. Our current > leader has failed us. Maybe he never wanted to be a leader, but when > you go and declare yourself a "benevolent dictator for life" you'd > damn well better act like one! > He never declared himself "benevolent dictator for life", others declared him so. > Why has GvR not admonished the atrocious behavior of some people in > this community? Why has GvR not admitted publicly the hideous state of > IDLE and Tkinter? Where is the rally call? Where is the community > spirit? The future of Pythin is in your hands Mr. Van Rossum. Will you > step up and do what needs to be done? Will you have the courage? i > sincerely hope so. If not, please step down and allow someone to lead. > I await the supreme commanders ascent to power. GvR isn't our leader, we are his followers. There's a difference. :-) From invalid at invalid.invalid Thu Dec 8 13:10:17 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 8 Dec 2011 18:10:17 +0000 (UTC) Subject: Misleading error message of the day References: Message-ID: On 2011-12-08, Roy Smith wrote: > On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant wrote: >> string are iterable, considering this, the error is correct. > > Yes, I understand that the exception is correct. I'm not saying the exception should be changed, just that we have the opportunity to produce a more useful error message. The exception would be equally correct if it was: > > ValueError: you did something wrong My favorite is still the old classic error from and old Unix printer port driver: "lp0 on fire" > but most people would probably agree that it's not the most useful > message that could have been produced. -- Grant Edwards grant.b.edwards Yow! Don't hit me!! I'm in at the Twilight Zone!!! gmail.com From roy at panix.com Thu Dec 8 13:42:45 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 10:42:45 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> Message-ID: <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> (some, very, long, list, of, variable, names, to, get, the, stuff, unpacked, into) = function_that_should_return_a_14_tuple() raises ValueError: too many values to unpack Quick, what's the bug? Did I forget a variable on the LHS, or is my function returning more things than it should? I know it's supposed to be 14, but I don't know which side is wrong. Had it said "... expected 13, got 14", I would know immediately. Error messages should be as explicit as possible. It's just like bug reports. The basic mantra of a bug report is: 1) This is what I did 2) This is what I expected to happen 3) This is what I observed happen 4) This is how what I observed differed from what I expected Saying, "expected X, got Y" is more explicit than "got too many" From roy at panix.com Thu Dec 8 13:42:45 2011 From: roy at panix.com (Roy Smith) Date: Thu, 8 Dec 2011 10:42:45 -0800 (PST) Subject: Misleading error message of the day In-Reply-To: References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> Message-ID: <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> (some, very, long, list, of, variable, names, to, get, the, stuff, unpacked, into) = function_that_should_return_a_14_tuple() raises ValueError: too many values to unpack Quick, what's the bug? Did I forget a variable on the LHS, or is my function returning more things than it should? I know it's supposed to be 14, but I don't know which side is wrong. Had it said "... expected 13, got 14", I would know immediately. Error messages should be as explicit as possible. It's just like bug reports. The basic mantra of a bug report is: 1) This is what I did 2) This is what I expected to happen 3) This is what I observed happen 4) This is how what I observed differed from what I expected Saying, "expected X, got Y" is more explicit than "got too many" From __peter__ at web.de Thu Dec 8 13:43:01 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 08 Dec 2011 19:43:01 +0100 Subject: adding elements to set References: <4EE0E72E.2040104@gmail.com> Message-ID: Chris Angelico wrote: > On Fri, Dec 9, 2011 at 4:32 AM, Peter Otten <__peter__ at web.de> wrote: >> The only thing that has changed (in 2.7) is the algorithm to calculate >> the hash value. The bits are rotated to turn the four least significant >> bits into the most signicant ones. According to a comment in >> Objects/objects.c the change leads to fewer hash collisions. > > Interesting, but what I saw was this: > >>>> class C(object): > > def __init__(self, x): > self.x = x > > def __eq__(self, other): > return self.x == other.x > >>>> s=set() >>>> c1=C(1) >>>> s.add(c1) > Traceback (most recent call last): > File "", line 1, in > s.add(c1) > TypeError: unhashable type: 'C' > > (This is in IDLE from Python 3.2 on Windows.) > > However, s.add(object()) works fine. So subclasses don't get that. > Odd. Makes sense though - you can't get this unexpected behaviour as > easily. It seems to be even subtler: you can subclass if you don't implement __eq__(): >>> class C(object): pass ... >>> {C()} {<__main__.C object at 0x17defd0>} From duncan.booth at invalid.invalid Thu Dec 8 13:54:30 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 8 Dec 2011 18:54:30 GMT Subject: adding elements to set References: <4EE0E72E.2040104@gmail.com> Message-ID: Chris Angelico wrote: > On Fri, Dec 9, 2011 at 4:32 AM, Peter Otten <__peter__ at web.de> wrote: >> The only thing that has changed (in 2.7) is the algorithm to >> calculate the hash value. The bits are rotated to turn the four least >> significant bits into the most signicant ones. According to a comment >> in Objects/objects.c the change leads to fewer hash collisions. > > Interesting, but what I saw was this: > >>>> class C(object): > > def __init__(self, x): > self.x = x > > def __eq__(self, other): > return self.x == other.x > >>>> s=set() >>>> c1=C(1) >>>> s.add(c1) > Traceback (most recent call last): > File "", line 1, in > s.add(c1) > TypeError: unhashable type: 'C' > > (This is in IDLE from Python 3.2 on Windows.) > > However, s.add(object()) works fine. So subclasses don't get that. > Odd. Makes sense though - you can't get this unexpected behaviour as > easily. > Yes, the documentation describes this although I don't think anything highlights that it is a change from Python 2.x: [http://docs.python.org/py3k/reference/datamodel.html] > If a class does not define an __eq__() method it should not define a > __hash__() operation either; if it defines __eq__() but not > __hash__(), its instances will not be usable as items in hashable > collections. If a class defines mutable objects and implements an > __eq__() method, it should not implement __hash__(), since the > implementation of hashable collections requires that a key?s hash > value is immutable (if the object?s hash value changes, it will be in > the wrong hash bucket). So in Python 2.x you could define __eq__ and get the default __hash__ which would break dictionaries. With Python 3.x defining __eq__ will disable the default __hash__ although if you subclass a class that has both methods you could still get in a mess by redefining one without the other. -- Duncan Booth http://kupuguy.blogspot.com From benjamin.kaplan at case.edu Thu Dec 8 13:57:28 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Thu, 8 Dec 2011 13:57:28 -0500 Subject: Misleading error message of the day In-Reply-To: <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> Message-ID: On Thu, Dec 8, 2011 at 1:42 PM, Roy Smith wrote: > (some, > ?very, > ?long, > ?list, > ?of, > ?variable, > ?names, > ?to, > ?get, > ?the, > ?stuff, > ?unpacked, > ?into) = function_that_should_return_a_14_tuple() > > raises > > ValueError: too many values to unpack > > Quick, what's the bug? ?Did I forget a variable on the LHS, or is my function returning more things than it should? ?I know it's supposed to be 14, but I don't know which side is wrong. ?Had it said "... expected 13, got 14", I would know immediately. > If the RHS was a tuple or a list, yes you could know immediately. But unpacking works with any iterable, so it probably doesn't special-case lists and tuples. Iterables don't have a size- they just keep going until StopIteration is raised. So in EVERY SINGLE CASE, you would get "expected n args, got n+1" even if the iterable would return 24 items instead of 14, or would never stop returning items. > Error messages should be as explicit as possible. ?It's just like bug reports. ?The basic mantra of a bug report is: > > 1) This is what I did > > 2) This is what I expected to happen > > 3) This is what I observed happen > > 4) This is how what I observed differed from what I expected > > Saying, "expected X, got Y" is more explicit than "got too many" > > > -- > http://mail.python.org/mailman/listinfo/python-list From ethan at stoneleaf.us Thu Dec 8 14:09:23 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 08 Dec 2011 11:09:23 -0800 Subject: Misleading error message of the day In-Reply-To: References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> Message-ID: <4EE10B63.2010600@stoneleaf.us> Benjamin Kaplan wrote: > On Thu, Dec 8, 2011 at 1:42 PM, Roy Smith wrote: >> (some, >> very, >> long, >> list, >> of, >> variable, >> names, >> to, >> get, >> the, >> stuff, >> unpacked, >> into) = function_that_should_return_a_14_tuple() >> >> raises >> >> ValueError: too many values to unpack >> >> Quick, what's the bug? Did I forget a variable on the LHS, or is my function returning more things than it should? I know it's supposed to be 14, but I don't know which side is wrong. Had it said "... expected 13, got 14", I would know immediately. >> > > If the RHS was a tuple or a list, yes you could know immediately. But > unpacking works with any iterable, so it probably doesn't special-case > lists and tuples. Iterables don't have a size- they just keep going > until StopIteration is raised. So in EVERY SINGLE CASE, you would get > "expected n args, got n+1" even if the iterable would return 24 items > instead of 14, or would never stop returning items. Not so. There could be fewer, in which you could see "expected 13 args, got 7." ~Ethan~ From benjamin.kaplan at case.edu Thu Dec 8 14:38:28 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Thu, 8 Dec 2011 14:38:28 -0500 Subject: Misleading error message of the day In-Reply-To: <4EE10B63.2010600@stoneleaf.us> References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> <4EE10B63.2010600@stoneleaf.us> Message-ID: On Thu, Dec 8, 2011 at 2:09 PM, Ethan Furman wrote: > Benjamin Kaplan wrote: >> >> On Thu, Dec 8, 2011 at 1:42 PM, Roy Smith wrote: >>> >>> (some, >>> ?very, >>> ?long, >>> ?list, >>> ?of, >>> ?variable, >>> ?names, >>> ?to, >>> ?get, >>> ?the, >>> ?stuff, >>> ?unpacked, >>> ?into) = function_that_should_return_a_14_tuple() >>> >>> raises >>> >>> ValueError: too many values to unpack >>> >>> Quick, what's the bug? ?Did I forget a variable on the LHS, or is my >>> function returning more things than it should? ?I know it's supposed to be >>> 14, but I don't know which side is wrong. ?Had it said "... expected 13, got >>> 14", I would know immediately. >>> >> >> If the RHS was a tuple or a list, yes you could know immediately. But >> unpacking works with any iterable, so it probably doesn't special-case >> lists and tuples. Iterables don't have a size- they just keep going >> until StopIteration is raised. So in EVERY SINGLE CASE, you would get >> "expected n args, got n+1" even if the iterable would return 24 items >> instead of 14, or would never stop returning items. > > > Not so. ?There could be fewer, in which you could see "expected 13 args, got > 7." > You mean like this? >>> a,b,c = ['a','b'] Traceback (most recent call last): File "", line 1, in ValueError: need more than 2 values to unpack From robertb at trdlnk.com Thu Dec 8 14:46:15 2011 From: robertb at trdlnk.com (Robert Boehne) Date: Thu, 08 Dec 2011 13:46:15 -0600 Subject: Sybase module 0.40pre2 released Message-ID: <4EE11407.1000609@trdlnk.com> WHAT IS IT: The Sybase module provides a Python interface to the Sybase relational database system. It supports all of the Python Database API, version 2.0 with extensions. Please downolad, test and report any problems with the pre-release. ** This version is a pre-release not intended for production use ** The module is available here: http://downloads.sourceforge.net/python-sybase/python-sybase-0.40pre2.tar.gz The module home page is here: http://python-sybase.sourceforge.net/ MAJOR CHANGES SINCE 0.40pre1: Better support for building against Sybase 15 on Windows, UNIX/Linux and 64-bit platforms. MAJOR CHANGES SINCE 0.39: Modify the DateTimeAsPython output conversion to return None when NULL is output support for Python without threads Ignore additional non-error codes from Sybase (1918 and 11932) Use outputmap in bulkcopy mode (thanks to patch by Cyrille Froehlich) Raise exception when opening a cursor on a closed connection Added unit tests Added new exception DeadLockError when Sybase is in a deadlock situation Add command properties CS_STICKY_BINDS and CS_HAVE_BINDS Added support for inputmap in bulkcopy reuse command and cursor when calling cursor.execute with same request Use ct_setparam to define ct_cursor parameters types instead of ct_param implicit conversion for CS_DATE_TYPE in CS_DATETIME_TYPE DataBuf Adding ct_cmd_props wrapper Increase DataBuf maxlength for params of a request when using CS_CHAR_TYPE params so that the buf can be reused BUGS CORRECTED SINCE 0.39: Corrected money type when using CS_MONEY4 (close bug 2615821) Corrected thread locking in ct_cmd_props (thanks to patch by Cyrille Froehlich) Corrected bug in type mapping in callproc (thanks to report by Skip Montanaro) Correct passing None in a DataBuf (thanks to patch by Bram Kuijvenhoven) The full ChangeLog is here: https://python-sybase.svn.sourceforge.net/svnroot/python-sybase/tags/r0_40pre2/ChangeLog From jeanmichel at sequans.com Thu Dec 8 14:49:54 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 08 Dec 2011 20:49:54 +0100 Subject: Misleading error message of the day In-Reply-To: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> Message-ID: <4EE114E2.6070200@sequans.com> Roy Smith wrote: > On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant wrote: > >> string are iterable, considering this, the error is correct. >> > > Yes, I understand that the exception is correct. I'm not saying the exception should be changed, just that we have the opportunity to produce a more useful error message. The exception would be equally correct if it was: > > ValueError: you did something wrong > > but most people would probably agree that it's not the most useful message that could have been produced. > You have to opportunity to not use unpacking anymore :o) There is a recent thread were the dark side of unpacking was exposed. Unpacking is a cool feautre for very small applications but should be avoided whenever possible otherwise. JM From enricong at gmail.com Thu Dec 8 14:56:42 2011 From: enricong at gmail.com (Enrico) Date: Thu, 8 Dec 2011 11:56:42 -0800 (PST) Subject: Numpy ndarray to C array Message-ID: <4ee07a56-50f9-4784-a52e-31553f0d3dc8@v29g2000yqv.googlegroups.com> I am trying to pass a multi-dimensional ndarray to C as a multi- dimensional C array for the purposes of passing it to mathematica. They already have a wrapper for a 1-D Python list. where the list is copied to "list". Shown below: static PyObject * mathlink_PutIntegerList(mathlink_Link *self, PyObject *args) { PyObject* seq; PyObject* obj; long i, len, result; int* list; len = PyObject_Length(seq); list = PyMem_New(int, len); for(i = 0; i < len; i++) { obj = PySequence_GetItem(seq, i); list[i] = PyInt_AsLong(obj); } CheckForThreadsAndRunLink(self,result = MLPutIntegerList(self->lp, list, len)); PyMem_Free(list); CHECKNOTEQUAL(result,MLSUCCESS,self); Py_INCREF(Py_None); return Py_None; } I would like to create a similar wrapper which accepts an ndarray and provides the array laid out in memory like a C array declared explicitly as "int a[m][n]...". I also need to pass the length of the array at each level i as dim[i]. Since this is pretty much the only function I plan to wrap, I'd like to avoid using boost, swig, etc. Any help would be appreciated. From robert.kern at gmail.com Thu Dec 8 14:57:15 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 08 Dec 2011 19:57:15 +0000 Subject: Misleading error message of the day In-Reply-To: References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <4EE0DDE4.4080802@gmail.com> Message-ID: On 12/8/11 4:21 PM, Chris Angelico wrote: > On Fri, Dec 9, 2011 at 2:55 AM, Andrea Crotti wrote: >> Yes but how do you know how many values you generated when it quits? >> I mean I don't know how it work internally, but it should keep a temporary >> list of the yielded values to be able to find out how many values are >> there.. > > Iterator unpacking works roughly thus: > > 1) Count up how many results you need (call that N) > 2) N times, get a value from the iterator. If StopIteration is raised, > swallow it and raise ValueError because there were too few values. > 3) Attempt to get one more value from the iterator. If StopIteration > is NOT raised, raise ValueError because there were too many values. > > At no point is the "total size" of the iterator counted (it could, > after all, be infinite). When ValueError is raised, all that's known > is that StopIteration wasn't raised at the end of the process. unpack_iterable() has the original object available to it, not just the iterator. It could opportunistically check for __len__() and fall back to the less informative message when it is absent. -- 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 From ethan at stoneleaf.us Thu Dec 8 15:09:11 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 08 Dec 2011 12:09:11 -0800 Subject: Misleading error message of the day In-Reply-To: References: <18647617.2258.1323358966076.JavaMail.geo-discussion-forums@yqf20> <3164448.1087.1323369765528.JavaMail.geo-discussion-forums@yqcm37> <4EE10B63.2010600@stoneleaf.us> Message-ID: <4EE11967.7050904@stoneleaf.us> Benjamin Kaplan wrote: > On Thu, Dec 8, 2011 at 2:09 PM, Ethan Furman wrote: >> Benjamin Kaplan wrote: >>> If the RHS was a tuple or a list, yes you could know immediately. But >>> unpacking works with any iterable, so it probably doesn't special-case >>> lists and tuples. Iterables don't have a size- they just keep going >>> until StopIteration is raised. So in EVERY SINGLE CASE, you would get >>> "expected n args, got n+1" even if the iterable would return 24 items >>> instead of 14, or would never stop returning items. >> >> Not so. There could be fewer, in which you could see "expected 13 args, got >> 7." >> > > You mean like this? > >>>> a,b,c = ['a','b'] > Traceback (most recent call last): > File "", line 1, in > ValueError: need more than 2 values to unpack This is still not as helpful as this would be: ValueError: need 3 values, received 2 ~Ethan~ From ethan at stoneleaf.us Thu Dec 8 15:13:29 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 08 Dec 2011 12:13:29 -0800 Subject: Misleading error message of the day In-Reply-To: <4EE114E2.6070200@sequans.com> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> Message-ID: <4EE11A69.9060207@stoneleaf.us> Jean-Michel Pichavant wrote: > You have to opportunity to not use unpacking anymore :o) There is a > recent thread were the dark side of unpacking was exposed. Unpacking is > a cool feautre for very small applications but should be avoided > whenever possible otherwise. Which thread was that? ~Ethan~ From einazaki668 at yahoo.com Thu Dec 8 15:16:36 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 8 Dec 2011 12:16:36 -0800 (PST) Subject: Python horks on WinXP path names Message-ID: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> I'm running Python 2.7 on WinXP (ActiveState community version) and when I try to do this: if __name__ == '__main__': root = Tkinter.Tk() root.withdraw() fileNames = tkFileDialog.askopenfilenames() root.destroy() print fileNames # windows filename gets for fileName in fileNames: print fileName file = open(fileName, 'r') for line in file.readlines(): print line.strip() I get this: C:\Documents and Settings\eric\Desktop\PythonShop>python picker.py {C:/Documents and Settings/eric/Desktop/PythonShop/cereal.py} { Traceback (most recent call last): File "picker.py", line 31, in file = open(fileName, 'r') IOError: [Errno 2] No such file or directory: u'{' That is, fileName is in a form that open() doesn't recognize. On a BSD box the code works fine. What's going on with the file name and how do I fix it? TIA, eric From Catherine.M.Moroney at jpl.nasa.gov Thu Dec 8 15:17:11 2011 From: Catherine.M.Moroney at jpl.nasa.gov (Catherine Moroney) Date: Thu, 08 Dec 2011 12:17:11 -0800 Subject: tracking variable value changes Message-ID: Hello, Is there a way to create a C-style pointer in (pure) Python so the following code will reflect the changes to the variable "a" in the dictionary "x"? For example: >>> a = 1.0 >>> b = 2.0 >>> x = {"a":a, "b":b} >>> x {'a': 1.0, 'b': 2.0} >>> a = 100.0 >>> x {'a': 1.0, 'b': 2.0} ## at this point, I would like the value ## associated with the "a" key to be 100.0 ## rather than 1.0 If I make "a" and "b" numpy arrays, then changes that I make to the values of a and b show up in the dictionary x. My understanding is that when I redefine the value of "a", that Python is creating a brand-new float with the value of 100.0, whereas when I use numpy arrays I am merely assigning a new value to the same object. Is there some way to rewrite the code above so the change of "a" from 1.0 to 100.0 is reflected in the dictionary. I would like to use simple datatypes such as floats, rather than numpy arrays or classes. I tried using weakref's, but got the error that a weak reference cannot be created to a float. Catherine From ben+python at benfinney.id.au Thu Dec 8 15:23:42 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 09 Dec 2011 07:23:42 +1100 Subject: Questions about LISP and Python. References: <6b56c68d-22ed-427d-a05e-c503e9990a20@r28g2000yqj.googlegroups.com> <3847f890-210a-4695-b37c-b82103fd20d7@v5g2000yqn.googlegroups.com> <784cee1d-bb38-4773-b465-50f4de411133@s26g2000yqd.googlegroups.com> Message-ID: <87ehwe3j8x.fsf@benfinney.id.au> MRAB writes: > GvR isn't our leader, we are his followers. There's a difference. :-) +1 QotW -- \ ?Guaranteed to work throughout its useful life.? ?packaging for | `\ clockwork toy, Hong Kong | _o__) | Ben Finney From ian.g.kelly at gmail.com Thu Dec 8 15:43:05 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Dec 2011 13:43:05 -0700 Subject: Python horks on WinXP path names In-Reply-To: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> References: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> Message-ID: On Thu, Dec 8, 2011 at 1:16 PM, Eric wrote: > I'm running Python 2.7 on WinXP (ActiveState community version) and > when I try to do this: > > if __name__ == '__main__': > ? ?root = Tkinter.Tk() > ? ?root.withdraw() > ? ?fileNames = tkFileDialog.askopenfilenames() > ? ?root.destroy() > ? ?print fileNames > # windows filename gets > for fileName in fileNames: > ? ?print fileName > ? ?file = open(fileName, 'r') > ? ?for line in file.readlines(): > ? ? ? ? ? ?print line.strip() > > > I get this: > > C:\Documents and Settings\eric\Desktop\PythonShop>python picker.py > {C:/Documents and Settings/eric/Desktop/PythonShop/cereal.py} > { > Traceback (most recent call last): > ?File "picker.py", line 31, in > ? ?file = open(fileName, 'r') > IOError: [Errno 2] No such file or directory: u'{' > > That is, fileName is in a form that open() doesn't recognize. ?On a > BSD box the code works fine. ?What's going on with the file name and > how do I fix it? http://bugs.python.org/issue5712 From malaclypse2 at gmail.com Thu Dec 8 15:45:35 2011 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 8 Dec 2011 15:45:35 -0500 Subject: Python horks on WinXP path names In-Reply-To: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> References: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> Message-ID: On Thu, Dec 8, 2011 at 3:16 PM, Eric wrote: > I'm running Python 2.7 on WinXP (ActiveState community version) and > when I try to do this: > > if __name__ == '__main__': > root = Tkinter.Tk() > root.withdraw() > fileNames = tkFileDialog.askopenfilenames() > root.destroy() > print fileNames > # windows filename gets > for fileName in fileNames: > print fileName > file = open(fileName, 'r') > for line in file.readlines(): > print line.strip() > > > I get this: > > C:\Documents and Settings\eric\Desktop\PythonShop>python picker.py > {C:/Documents and Settings/eric/Desktop/PythonShop/cereal.py} > { > Traceback (most recent call last): > File "picker.py", line 31, in > file = open(fileName, 'r') > IOError: [Errno 2] No such file or directory: u'{' > > That is, fileName is in a form that open() doesn't recognize. On a > BSD box the code works fine. What's going on with the file name and > how do I fix it? > Try replacing your current "print fileNames" line with the follwing: print type(fileNames), repr(fileNames) The behavior you're seeing seems to indicate that fileNames is really a single filename (a single unicode string), rather than a list of file names (a list containing strings). I don't know why that would be, but it would be helpful to confirm that that is, indeed, the problem. I guess you should also double check that the version of the code you're running on windows does, in fact, call "tkFileDialog.askopenfilenames()" rather than "tkFileDialog.askopenfilename()" The two calls are just one letter different, but the first one is supposed to return a list, and the second one is supposed to just return a single string. -------------- next part -------------- An HTML attachment was scrubbed... URL: From einazaki668 at yahoo.com Thu Dec 8 15:56:38 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 8 Dec 2011 12:56:38 -0800 (PST) Subject: Python horks on WinXP path names References: <265079dc-af55-45ca-a66a-1501492bd425@v11g2000yqi.googlegroups.com> Message-ID: <74af070b-78ca-487c-b6dc-fea51847f9dd@q16g2000yqn.googlegroups.com> On Dec 8, 2:43?pm, Ian Kelly wrote: > On Thu, Dec 8, 2011 at 1:16 PM, Eric wrote: > > I'm running Python 2.7 on WinXP (ActiveState community version) and > > when I try to do this: > > > if __name__ == '__main__': > > ? ?root = Tkinter.Tk() > > ? ?root.withdraw() > > ? ?fileNames = tkFileDialog.askopenfilenames() > > ? ?root.destroy() > > ? ?print fileNames > > # windows filename gets > > for fileName in fileNames: > > ? ?print fileName > > ? ?file = open(fileName, 'r') > > ? ?for line in file.readlines(): > > ? ? ? ? ? ?print line.strip() > > > I get this: > > > C:\Documents and Settings\eric\Desktop\PythonShop>python picker.py > > {C:/Documents and Settings/eric/Desktop/PythonShop/cereal.py} > > { > > Traceback (most recent call last): > > ?File "picker.py", line 31, in > > ? ?file = open(fileName, 'r') > > IOError: [Errno 2] No such file or directory: u'{' > > > That is, fileName is in a form that open() doesn't recognize. ?On a > > BSD box the code works fine. ?What's going on with the file name and > > how do I fix it? > > http://bugs.python.org/issue5712 Thanks! eric From alister.ware at ntlworld.com Thu Dec 8 15:58:56 2011 From: alister.ware at ntlworld.com (alister) Date: Thu, 08 Dec 2011 20:58:56 GMT Subject: Misleading error message of the day References: Message-ID: On Thu, 08 Dec 2011 18:10:17 +0000, Grant Edwards wrote: > On 2011-12-08, Roy Smith wrote: >> On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant >> wrote: >>> string are iterable, considering this, the error is correct. >> >> Yes, I understand that the exception is correct. I'm not saying the >> exception should be changed, just that we have the opportunity to >> produce a more useful error message. The exception would be equally >> correct if it was: >> >> ValueError: you did something wrong > > My favorite is still the old classic error from and old Unix printer > port driver: > > "lp0 on fire" > >> but most people would probably agree that it's not the most useful >> message that could have been produced. not as useless as "Keyboard Error press F1 to continue" -- "I once witnessed a long-winded, month-long flamewar over the use of mice vs. trackballs...It was very silly." (By Matt Welsh) From malaclypse2 at gmail.com Thu Dec 8 16:12:14 2011 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 8 Dec 2011 16:12:14 -0500 Subject: tracking variable value changes In-Reply-To: References: Message-ID: On Thu, Dec 8, 2011 at 3:17 PM, Catherine Moroney < Catherine.M.Moroney at jpl.nasa.gov> wrote: > Is there some way to rewrite the code above so the change of "a" from > 1.0 to 100.0 is reflected in the dictionary. I would like to use > simple datatypes such as floats, rather than numpy arrays or classes. > No, there is not. You would need to use a mutable data type, and float is not mutable (neither are ints or strings). -- Jerry -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu Dec 8 16:17:13 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 08:17:13 +1100 Subject: Misleading error message of the day In-Reply-To: References: Message-ID: On Fri, Dec 9, 2011 at 7:58 AM, alister wrote: > not as useless as "Keyboard Error press F1 to continue" If it said "press F1 to ignore" then I would agree. This, however, is more akin to "replace user and strike any key to continue", but more implicit. ChrisA From robert.kern at gmail.com Thu Dec 8 16:31:39 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 08 Dec 2011 21:31:39 +0000 Subject: Numpy ndarray to C array In-Reply-To: <4ee07a56-50f9-4784-a52e-31553f0d3dc8@v29g2000yqv.googlegroups.com> References: <4ee07a56-50f9-4784-a52e-31553f0d3dc8@v29g2000yqv.googlegroups.com> Message-ID: On 12/8/11 7:56 PM, Enrico wrote: > I am trying to pass a multi-dimensional ndarray to C as a multi- > dimensional C array for the purposes of passing it to mathematica. > They already have a wrapper for a 1-D Python list. where the list is > copied to "list". Shown below: > I would like to create a similar wrapper which accepts an ndarray and > provides the array laid out in memory like a C array declared > explicitly as "int a[m][n]...". I also need to pass the length of the > array at each level i as dim[i]. Since this is pretty much the only > function I plan to wrap, I'd like to avoid using boost, swig, etc. You will find it better to ask numpy questions on the numpy mailing list: http://www.scipy.org/Mailing_Lists In this case, you are looking for the PyArray_AsCArray() function: http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PyArray_AsCArray -- 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 From greg.ewing at canterbury.ac.nz Thu Dec 8 16:33:10 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 09 Dec 2011 10:33:10 +1300 Subject: tp_new, tp_alloc, tp_init In-Reply-To: References: Message-ID: <9kcoopFv41U1@mid.individual.net> Michael Hennebry wrote: > I've been reading about writing extension types in C and am rather > fuzzy about the relationship between tp_new, tp_alloc and tp_init. > Most especially, why tp_new? It seems to me that tp_alloc and tp_init > would be sufficient. tp_new and tp_init correspond to the Python methods __new__ and __init__, and they're separated for the same reasons they are in Python. tp_alloc is separate because it allows a type to use a custom memory allocator without disturbing the rest of the initialisation mechanism. -- Greg From greg.ewing at canterbury.ac.nz Thu Dec 8 16:46:48 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 09 Dec 2011 10:46:48 +1300 Subject: How to get a correct entry in the menu for a Python application on Mac OS X In-Reply-To: References: Message-ID: <4EE13048.8090307@canterbury.ac.nz> Detlev Offenbach wrote: > I am fairly new to Mac OS X and would like to know, what I have to do to > make my Python application show the correct name in the menu bar. What > did I do so far. I created an application package containing the .plist > file with correct entries and a shell script, that starts the correct > Python interpreter with the the main script. I don't think that will work, because the executable that your shell script is starting is in an app bundle of its own, and MacOSX will be using the plist from that bundle, which just has the generic "Python" name in it. There are a couple of things you could do: 1) Use py2app to create your app bundle. It does the right things -- not sure exactly what, but it works. 2) Hack things at run time. I use the following PyObjC code in PyGUI to set the application name: from Foundation import NSBundle ns_bundle = NSBundle.mainBundle() ns_info = ns_bundle.localizedInfoDictionary() if not ns_info: ns_info = ns_bundle.infoDictionary() ns_info['CFBundleName'] = my_application_name -- Greg From ian.g.kelly at gmail.com Thu Dec 8 16:50:38 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 8 Dec 2011 14:50:38 -0700 Subject: tracking variable value changes In-Reply-To: References: Message-ID: On Thu, Dec 8, 2011 at 1:17 PM, Catherine Moroney wrote: > Hello, > > Is there a way to create a C-style pointer in (pure) Python so the following > code will reflect the changes to the variable "a" in the > dictionary "x"? > > For example: > >>>> a = 1.0 >>>> b = 2.0 >>>> x = {"a":a, "b":b} >>>> x > {'a': 1.0, 'b': 2.0} >>>> a = 100.0 >>>> x > {'a': 1.0, 'b': 2.0} ? ## at this point, I would like the value > ? ? ? ? ? ? ? ? ? ? ? ## associated with the "a" key to be 100.0 > ? ? ? ? ? ? ? ? ? ? ? ## rather than 1.0 > > If I make "a" and "b" numpy arrays, then changes that I make to the values > of a and b show up in the dictionary x. > > My understanding is that when I redefine the value of "a", that Python > is creating a brand-new float with the value of 100.0, whereas when I use > numpy arrays I am merely assigning a new value to the same object. Sort of. In the code above, you are binding a and x["a"] to the same float object. Then when you do "a = 100.0", you are rebinding a but not x["a"]. In the case of arrays it's the same story, except that you can also *modify* the contents of the array instead of rebinding to a new array. In that case both a and x["a"] are still bound to the original array, the contents of which have changed. You can get the same effect with a float by putting it in a container object and binding both variables to the same container objects rather than to the float directly. Then, to change the value, change the contents of the container object. What you use as a container object is up to you. Some use a 1-element list, although I find that ugly. From ben+python at benfinney.id.au Thu Dec 8 17:06:00 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 09 Dec 2011 09:06:00 +1100 Subject: tracking variable value changes References: Message-ID: <87aa723eif.fsf@benfinney.id.au> Catherine Moroney writes: > Is there a way to create a C-style pointer in (pure) Python so the > following code will reflect the changes to the variable "a" in the > dictionary "x"? No, Python doesn't do pointers. Rather, objects have references and that's how the program accesses the objects. > For example: > > >>> a = 1.0 > >>> b = 2.0 > >>> x = {"a":a, "b":b} > >>> x > {'a': 1.0, 'b': 2.0} > >>> a = 100.0 > >>> x > {'a': 1.0, 'b': 2.0} ## at this point, I would like the value > ## associated with the "a" key to be 100.0 > ## rather than 1.0 You might like that, but it's just not how Python works. Python doesn't have C-style pointers. Python also doesn't have variables (even though the documentation uses that term; IMO it's a mistake, and leads to confusion similar to this). What Python has are references to objects. One kind of reference is a name; another kind of reference is a value in a dictionary. The assignment operator ?=? is the binding operator. It binds the reference on the left side to the object on the right side. > If I make "a" and "b" numpy arrays, then changes that I make to the > values of a and b show up in the dictionary x. Yes, because the objects are mutable; you can change them and existing references are still referring to the same object. They don't ?show up in the dictionary?; the dictionary item is just referring to the same object it did before you made the change. > My understanding is that when I redefine the value of "a" Please think of it, instead, as re-binding the name ?a? to a new value. > that Python is creating a brand-new float with the value of 100.0, Yes (or at least that's the abstraction being presented to you; it may not be implemented exactly that way, but it's sufficient that we Python programmers treat it that way). > whereas when I use numpy arrays I am merely assigning a new value to > the same object. No, you're modifying the object. A numpy array itself contains references. By altering one of the elements in an array, you are re-binding one of its references to a different number. > Is there some way to rewrite the code above so the change of "a" from > 1.0 to 100.0 is reflected in the dictionary. I would like to use > simple datatypes such as floats, rather than numpy arrays or classes. Please follow the Python tutorial from beginning to end. Not just read, but do it: work through the exercises to understand what each one is teaching you. That will give you a firm grounding in Python's data model, including mutable and immutable types, references and binding. Do bear in mind what I said above, though, about ?variable? being a misleading term, and ignore its implications from the C language. -- \ ?Good judgement comes from experience. Experience comes from | `\ bad judgement.? ?Frederick P. Brooks | _o__) | Ben Finney From arnodel at gmail.com Thu Dec 8 17:10:01 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Thu, 8 Dec 2011 22:10:01 +0000 Subject: tracking variable value changes In-Reply-To: References: Message-ID: On 8 December 2011 21:50, Ian Kelly wrote: > You can get the same effect with a float by putting it in a container > object and binding both variables to the same container objects rather > than to the float directly. ?Then, to change the value, change the > contents of the container object. ?What you use as a container object > is up to you. ?Some use a 1-element list, although I find that ugly. This kind of trick is not often necessary anyway. It may be a sign that there is a better approach to the problem that the OP is trying to solve. -- Arnaud From frank at am-knie.de Thu Dec 8 17:41:35 2011 From: frank at am-knie.de (Frank van den Boom) Date: Thu, 08 Dec 2011 23:41:35 +0100 Subject: subprocess.Popen under windows 7 Message-ID: Hello, i have something like this under windows 7: print("try command...") arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r", "-p",PASSWORD] p = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=srcdir) output, error = p.communicate() if output: print output print ("Eyerthing is good") press_any_key_to_continue() The script works, but there is a little problem. When I double-click the python file, then the command line will open and the script starts. I can read "try command..." in the command line window under windows 7. But then I have to enter the return key in order that the script will go on. After I had entered the return key the script completed sucessfully and I saw the output. What can I do, to prevent pressing the return key? Thanks. From ethan at stoneleaf.us Thu Dec 8 18:34:13 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 08 Dec 2011 15:34:13 -0800 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <4EE14975.40202@stoneleaf.us> Chris Angelico wrote: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I was testing this out in > IDLE, and the fly help for the function no longer gave useful info > about its argument list. If you want to roll your own, it looks something like this: 2.x code ---------------------------------------------------------------------- def trace(func, debugmode=debugmode): if debugmode: name = func.__name__ argspec = inspect.getargspec(func) signature = inspect.formatargspec( formatvalue=lambda val: "", *argspec)[1:-1] # trim parens new_func = ( 'def _wrapper_(%(signature)s):\n' ' print(">"+func.__name__)\n' ' result = func(%(signature)s)\n' ' print("<"+func.__name__)\n' ' return result\n' % {'signature':signature} ) evaldict = {'func':func} exec new_func in evaldict wrapped = evaldict['_wrapper_'] wrapped.__name__ = name wrapped.__doc__ = func.__doc__ wrapped.__module__ = func.__module__ wrapped.__dict__ = func.__dict__ wrapped.func_defaults = func.func_defaults return wrapped return func ---------------------------------------------------------------------- The downside (which you get even with Michele's decorator module) is that tracebacks aren't quite as clean. ~Ethan~ From ethan at stoneleaf.us Thu Dec 8 18:56:58 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 08 Dec 2011 15:56:58 -0800 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <4EE14ECA.8020801@stoneleaf.us> Chris Angelico wrote: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I was testing this out in > IDLE, and the fly help for the function no longer gave useful info > about its argument list. And here it is with Michele's decorator module: 2.x code (probably the same in 3.x, but haven't tested) ---------------------------------------------------------------- from decorator import decorator debugmode = True def trace(func): if debugmode: @decorator def traced(func, *a, **ka): print(">", func.__name__, a, ka) result = func(*a, **ka) print("<", func.__name__) return result return traced(func) return func @trace def test(x): "a simple test" print("Test! "+x) return 5 ---------------------------------------------------------------- ~Ethan~ From roy at panix.com Thu Dec 8 20:19:50 2011 From: roy at panix.com (Roy Smith) Date: Thu, 08 Dec 2011 20:19:50 -0500 Subject: Misleading error message of the day References: Message-ID: In article , Grant Edwards wrote: > On 2011-12-08, Roy Smith wrote: > > On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant > > wrote: > >> string are iterable, considering this, the error is correct. > > > > Yes, I understand that the exception is correct. I'm not saying the > > exception should be changed, just that we have the opportunity to produce a > > more useful error message. The exception would be equally correct if it > > was: > > > > ValueError: you did something wrong > > My favorite is still the old classic error from and old Unix printer > port driver: > > "lp0 on fire" Well, if you're going to go there, ed had (and probably still does) have but a single all-purpose error message: "?". The old v6 unix chess program was somewhat more verbose. It said, "eh?" (unless I'm mixing that up with something else). From ian at excess.org Thu Dec 8 20:43:57 2011 From: ian at excess.org (Ian Ward) Date: Thu, 08 Dec 2011 20:43:57 -0500 Subject: ANN: Speedometer 2.4 - bandwidth and download monitor Message-ID: <4EE167DD.6050209@excess.org> Announcing Speedometer 2.8 -------------------------- Speedometer home page: http://excess.org/speedometer/ Download: http://excess.org/speedometer/speedometer-2.8.tar.gz New in this release: ==================== - Added a linear scale option: -l. Best used in combination with -m (and possibly -n) to customize the range to be displayed. Thanks to jukie.net for sponsoring this feature. - Replace silly "curved" reading with a weighted moving average - New option to display all values in bits per second: -s - New options to set minimum (-n) and maximum (-m) values displayed on the graphs in bytes/s. Defaults are -n 32 and -m 2**32 - Accept shortened versions of -rx and -tx: -r and -t My intent is to drop use of the original forms in 3.0 and add --long-versions of all options - Use IEC notation of sizes MiB, GiB etc. - Install script as both speedometer.py and speedometer About Speedometer ================= Speedometer is a console bandwidth and file download progress monitor with a linear/logarithmic bandwidth display and a simple command-line interface. Speedometer requires Urwid for full-console bar graph display. Urwid may be downloaded from: http://excess.org/urwid/ Speedometer is released under the GNU LGPL. From lie.1296 at gmail.com Thu Dec 8 20:46:15 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 09 Dec 2011 12:46:15 +1100 Subject: Misleading error message of the day In-Reply-To: <4EE11A69.9060207@stoneleaf.us> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> Message-ID: On 12/09/2011 07:13 AM, Ethan Furman wrote: > Jean-Michel Pichavant wrote: >> You have to opportunity to not use unpacking anymore :o) There is a >> recent thread were the dark side of unpacking was exposed. Unpacking >> is a cool feautre for very small applications but should be avoided >> whenever possible otherwise. > > Which thread was that? perhaps the one that talks about `a, a.foo = 1, 2` blowing up? From steve+comp.lang.python at pearwood.info Thu Dec 8 21:07:30 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Dec 2011 02:07:30 GMT Subject: Misleading error message of the day References: Message-ID: <4ee16d61$0$29977$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Dec 2011 18:10:17 +0000, Grant Edwards wrote: > On 2011-12-08, Roy Smith wrote: >> On Thursday, December 8, 2011 10:03:38 AM UTC-5, Jean-Michel Pichavant >> wrote: >>> string are iterable, considering this, the error is correct. >> >> Yes, I understand that the exception is correct. I'm not saying the >> exception should be changed, just that we have the opportunity to >> produce a more useful error message. The exception would be equally >> correct if it was: >> >> ValueError: you did something wrong > > My favorite is still the old classic error from and old Unix printer > port driver: > > "lp0 on fire" > >> but most people would probably agree that it's not the most useful >> message that could have been produced. I forget where I saw this, but somebody took a screen shot of an error message from a GUI application that said something like: A fatal error occurred: no error and then aborted the app. -- Steven From lie.1296 at gmail.com Thu Dec 8 21:08:22 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 09 Dec 2011 13:08:22 +1100 Subject: subprocess.Popen under windows 7 In-Reply-To: References: Message-ID: On 12/09/2011 09:41 AM, Frank van den Boom wrote: > What can I do, to prevent pressing the return key? I didn't have Windows 7 right now, but that shouldn't happen with the code you've given; when trimming code for posting, you should check that the trimmed code still have the exact same problem. From tjreedy at udel.edu Thu Dec 8 21:33:24 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 08 Dec 2011 21:33:24 -0500 Subject: adding elements to set In-Reply-To: References: <4EE0E72E.2040104@gmail.com> Message-ID: On 12/8/2011 1:54 PM, Duncan Booth wrote: > Yes, the documentation describes this although I don't think anything > highlights that it is a change from Python 2.x: > > [http://docs.python.org/py3k/reference/datamodel.html] The Python 3 docs are 're-based' on 3.0, with change notes going forward from that new base. This change should have been in 'What's New in Python 3.0', though I did not find it. -- Terry Jan Reedy From miki.tebeka at gmail.com Thu Dec 8 22:25:34 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 8 Dec 2011 19:25:34 -0800 (PST) Subject: [OT] Book authoring Message-ID: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> Greetings, Any recommendations for a book authoring system that supports the following: 1. Code examples (with syntax highlighting and line numbers) 2. Output HTML, PDF, ePub ... 3. Automatic TOC and index 4. Search (in HTML) - this is a "nice to have" Can I somehow use Sphinx? Thanks, -- Miki From rosuav at gmail.com Thu Dec 8 22:46:45 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 14:46:45 +1100 Subject: Misleading error message of the day In-Reply-To: <4ee16d61$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <4ee16d61$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 9, 2011 at 1:07 PM, Steven D'Aprano wrote: > I forget where I saw this, but somebody took a screen shot of an error > message from a GUI application that said something like: > > A fatal error occurred: no error > > and then aborted the app. An errant error! Sounds like the stuff that happens here... http://thedailywtf.com/Series/Error_0x27_d.aspx This is getting quite off-topic though. ChrisA From roy at panix.com Thu Dec 8 23:44:40 2011 From: roy at panix.com (Roy Smith) Date: Thu, 08 Dec 2011 23:44:40 -0500 Subject: Misleading error message of the day References: <4ee16d61$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Chris Angelico wrote: > http://thedailywtf.com/Series/Error_0x27_d.aspx > > This is getting quite off-topic though. Getting off-topic, perhaps, but your comment really does bring some closure. When I was pondering the original, "too many values to unpack" message, I did indeed utter a few WTFs :-) From wuwei23 at gmail.com Thu Dec 8 23:57:16 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 8 Dec 2011 20:57:16 -0800 (PST) Subject: Misleading error message of the day References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> Message-ID: <36bf64b8-d27f-4f9d-9cf7-c425d4b1d214@q27g2000prh.googlegroups.com> On Dec 9, 11:46?am, Lie Ryan wrote: > perhaps the one that talks about `a, a.foo = 1, 2` blowing up? Are you sure you're not confusing this with the recent thread on 'x = x.thing = 1'? From wuwei23 at gmail.com Fri Dec 9 00:02:44 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 8 Dec 2011 21:02:44 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> On Dec 9, 2:38?am, Chris Angelico wrote: > One piece of sophistication that I would rather like to see, but don't > know how to do. Instead of *args,**kwargs, is it possible to somehow > copy in the function's actual signature? I was testing this out in > IDLE, and the fly help for the function no longer gave useful info > about its argument list. The 3rd party 'decorator' module takes care of issues like docstrings & function signatures. I'd really like to see some of that functionality in the stdlib though. From ulrich.eckhardt at dominolaser.com Fri Dec 9 03:32:00 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 09 Dec 2011 09:32:00 +0100 Subject: subprocess.Popen under windows 7 In-Reply-To: References: Message-ID: <1lt8r8-rel.ln1@satorlaser.homedns.org> Am 08.12.2011 23:41, schrieb Frank van den Boom: > arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r", > "-p",PASSWORD] The "*" is resolved by the shell, this is not a wildcard that gets passed to the program. At least not normally, your case might be different. > if output: > print output > > print ("Eyerthing is good") Is that Python 2 or 3? That said, if you reduced it to something that doesn't e.g. require 7zip I'd happily run it on an XP system with Python 2.7 to tell you if it works there or not. Doing so would also rule out any influence by 7zip, just in case. Uli From frank at am-knie.de Fri Dec 9 03:42:21 2011 From: frank at am-knie.de (Frank van den Boom) Date: Fri, 09 Dec 2011 09:42:21 +0100 Subject: subprocess.Popen under windows 7 In-Reply-To: References: Message-ID: > > I didn't have Windows 7 right now, but that shouldn't happen with the > code you've given; when trimming code for posting, you should check that > the trimmed code still have the exact same problem. > Here is the hole code: #!/usr/bin/env python # little script to backup recursive a folder with 7zip SOURCE_DIR = "C:/Users/yoicks/Desktop/source" DEST_DIR = "C:/Users/yoicks/Desktop/dest" BACKUP_NAME_PREFIX = "BACKUP" BACKUP_NAME_DELIMITER = "_" METHOD = '7zip' PATH_TO_7ZIP = "C:/Program Files/7-Zip/7z.exe" PASSWORD = "1234" import os, time, shutil, sys, tarfile, subprocess, traceback try: # win32 from msvcrt import getch except ImportError: # unix def getch(): import sys, tty, termios fd = sys.stdin.fileno() old = termios.tcgetattr(fd) try: tty.setraw(fd) return sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old) def press_any_key(): print "Press any key to continue." getch() def exit_with_string(exit_string): print exit_string press_any_key() sys.exit(exit_string) def backup_directory_7zip(srcdir,archive_name): if os.path.exists(archive_name): exit_stop("backup path %s already exists!" % arcpath) try: # see 7zip help arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r", "-p",PASSWORD] print ("try running cmd:\n %s\nin directory\n %s" % (' '.join(arglist),srcdir)) # join because i don't want [ ] sp = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=srcdir) #output, error = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=srcdir).communicate() except: print "Error while running 7zip subprocess.\n" print "Traceback:\n%s"%traceback.format_exc() return False output, error = sp.communicate() #something i tried output = sp.stdout.read() #somtehing i tried error = sp.stderr.read() if output: print output if error: print error return False return archive_name # build backup name print "start backup with python-script...\n" timestr = time.strftime("%Y%m%d_%H%M%S",time.localtime()) if METHOD not in ["7zip"]: exit_stop("METHOD not '7zip'") if not os.path.exists(SOURCE_DIR): exit_stop("SOURCE_DIR: %s doesn't exists" % os.path.abspath(SOURCE_DIR)) if not os.path.exists(DEST_DIR): exit_stop("DEST_DIR: %s doesn't exists" % os.path.abspath(DEST_DIR)) else: print("write backup from %s to %s \n using the %s method...\n" % (os.path.abspath(SOURCE_DIR), os.path.abspath(DEST_DIR), METHOD)) if METHOD == "7zip": try: if not os.path.exists(PATH_TO_7ZIP): exit_stop("Path to 7ZIP %s doesn't exist." % PATH_TO_7ZIP) except NameError: exit_stop("variable PATH_TO_7ZIP not defined") return_value = backup_directory_7zip(srcdir=os.path.abspath(SOURCE_DIR), archive_name=os.path.abspath(os.path.join( DEST_DIR, BACKUP_NAME_PREFIX + BACKUP_NAME_DELIMITER + timestr + ".exe"))) if return_value: print("Backup successfully written.") else: print("FAILURE during the backup") press_any_key() From ladasky at my-deja.com Fri Dec 9 04:10:48 2011 From: ladasky at my-deja.com (John Ladasky) Date: Fri, 9 Dec 2011 01:10:48 -0800 (PST) Subject: Multiprocessing bug, is my editor (SciTE) impeding my progress? References: <01516096-c485-451d-8770-d2026e4b93ed@d17g2000yql.googlegroups.com> Message-ID: <9d24a287-1e24-456d-9b3f-73ca272618e3@u10g2000prl.googlegroups.com> Thanks once again to everyone for their recommendations, here's a follow-up. In summary, I'm still baffled. I tried ipython, as Marco Nawijn suggested. If there is some special setting which returns control to the interpreter when a subprocess crashes, I haven't found it yet. Yes, I'm RTFM. As with SciTE, everything just hangs. So I went back to SciTE for now. And I'm doing what Terry Reedy suggested -- I am editing multiprocess.Pool in place. I made a backup, of course. I am using sudo to run SciTE so that I can edit the system files, and not have to worry about chasing path and import statement problems. What I have found, so far, is no evidence that a string is needed in any of the code. What's the task variable? It's a deeply-nested tuple, containing no strings, not even in the WORKING code. This makes me wonder whether that traceback is truly complete. I wrote a routine to display the contents of task, immediately before the offending put(). Here's a breakdown. In the WORKING version: task: 0 0 (see below) {} task[3]: (see below) task[3][0]: (see below) task[3][0][1]: (see below) task[3][0][1][0]: net shape=(2, 3) inp shape=(307, 2) tgt shape=(307, 2) By watching this run, I've learned that task[0] and task[1] are counters for groups of subprocesses and individual subprocesses, respectively. Suppose we have four subprocesses. When everything is working, task[:2] = [0,0] for the first call, then [0,1], [0,2], [0,3]; then, [1,0], [1,1], [1,2], etc. task[2] points to multiprocessing.Pool.mapstar, a one-line function that I never modify. task[4] is an empty dictionary. So it looks like everything that I provide appears in task[3]. task[3] is just a tuple inside a tuple (which is weird). task[3][0] contains the function to be called (in this case, my function, mean_square_error), and then a tuple containing all of the arguments to be passed to that function. The docs say that the function in question must be defined at the top level of the code so that it's importable (it is), and that all the arguments to be sent to that function will be wrapped up in a single tuple -- that is presumably task[3][0][1]. But that presumption is wrong. I wrote a function which creates a collections.namedtuple object of the type SplitData, which contains the function's arguments. It's not task[3][0][1] itself, but the tuple INSIDE it, namely task[3][0][1][0]. More weirdness. You don't need to worry about task[3][0][1][0], other than to note that these are my neural network objects, they are intact, they are the classes I expect, and they are named as I expect -- and that there are NO STRING objects. Now, are there any differences between the working version of my code and the buggy version? Other than a few trivial name changes that I made deliberately, the structure of task looks the SAME... task: 0 0 (see below) {} task[3]: (see below) task[3][0]: (see below) task[3][0][1]: (see below) task[3][0][1][0]: func shape=(2, 3) inp shape=(307, 2) tgt shape=(307, 2) Again, all the action is in task[3]. I was worried about the empty dictionary in task[4] at first, but I've seen this {} in the working program, too. I'm not sure what it does. For completeness, here's mean_square_error() from the working program: def mean_square_error(b): out = array([b.net(i) for i in b.inp]) return sum((out-b.tgt)**2) And, here's error() from the buggy program. def error(b): out = array([b.func(i) for i in b.inp]) return sum((out-b.tgt)**2) I renamed mean_square_error(), because I realized that the mean-square error is the only kind of error I'll ever be computing. I also renamed "net" to "func", in SplitData, reflecting the more general nature of the Cascade class I'm developing. So I mirror that name change here. Other than that, I trust you can see that error() and mean_square_error() are identical. I can call mean_square_error directly with a SplitData tuple and it works. I can call error directly with a SplitData tuple in the broken program, and it ALSO works. I'm only having problems when I try to submit the job through Pool. I tried putting a print trap in error(). When I use Pool then error() never gets called. I suppose that the logical next step is to compare the two Pool instances... onward... :^P From mail at timgolden.me.uk Fri Dec 9 04:28:07 2011 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 09 Dec 2011 09:28:07 +0000 Subject: subprocess.Popen under windows 7 In-Reply-To: <1lt8r8-rel.ln1@satorlaser.homedns.org> References: <1lt8r8-rel.ln1@satorlaser.homedns.org> Message-ID: <4EE1D4A7.2050401@timgolden.me.uk> On 09/12/2011 08:32, Ulrich Eckhardt wrote: > Am 08.12.2011 23:41, schrieb Frank van den Boom: >> arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r", >> "-p",PASSWORD] > > The "*" is resolved by the shell, this is not a wildcard that gets > passed to the program. At least not normally, your case might be different. "... not normally" == "... not on Unix". On Windows, the shell doesn't do any wildcard expansion. The OP is asking about behaviour on Windows 7. TJG From frank at am-knie.de Fri Dec 9 04:47:14 2011 From: frank at am-knie.de (Frank van den Boom) Date: Fri, 09 Dec 2011 10:47:14 +0100 Subject: subprocess.Popen under windows 7 In-Reply-To: References: <1lt8r8-rel.ln1@satorlaser.homedns.org> Message-ID: Thank you very much. Now I have written a little c++ programm which produces some ouput. And now it works fine. There is something wrong with 7zip.exe and the arglist with *. Tonight I will go on and hunt the error. It should be Python 2.7 #!/usr/bin/env python PATH_TO_EXE = "C:/Users/yoicks/Desktop/ausgabe.exe" import os, shutil, sys, subprocess, traceback try: # win32 from msvcrt import getch except ImportError: # unix def getch(): import sys, tty, termios fd = sys.stdin.fileno() old = termios.tcgetattr(fd) try: tty.setraw(fd) return sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old) def press_any_key(): print "Press any key to continue." getch() def exit_with_string(exit_string): print exit_string press_any_key() sys.exit(exit_string) def start_exe (PATH_TO_EXE): try: arglist = [PATH_TO_EXE] print ("try running cmd:\n %s\n" % (' '.join(arglist))) sp = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) except: print "Error while running subprocess.\n" print "Traceback:\n%s"%traceback.format_exc() return False output, error = sp.communicate() if output: print output if error: print error return False return True return_value = start_exe(PATH_TO_EXE) if return_value: print("Backup successfully written.") else: print("FAILURE during the backup") press_any_key() * Englisch - erkannt * Englisch * Deutsch * Englisch * Deutsch From massi_srb at msn.com Fri Dec 9 04:55:28 2011 From: massi_srb at msn.com (Massi) Date: Fri, 9 Dec 2011 01:55:28 -0800 (PST) Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Thank you all for your replies, first of all my Sum function was an example simplifying what I have to do in my real funciton. In general the D dictionary is complex, with a lot of keys, so I was searching for a quick method to access all the variables in it without doing the explicit creation: a, b, c = D['a'], D['b'], D['c'] and without using directly the D dictionary (boring...). When I talked about nested function I meant both cases Chris, but this is not a really tighten bound. I tried to follow the hints of Chris together with some help by google and used the following code: for k in D : exec "%s = D[k]" %k That seems to do the trick, but someone speaks about "dirty code", can anyone point me out which problems this can generate? Again, thank you for your help! From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Fri Dec 9 04:57:59 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Fri, 09 Dec 2011 10:57:59 +0100 Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: Am 08.12.2011 12:43 schrieb Chris Angelico: > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: >> I am still perplexed about decorators though, am happily using Python for >> many years without them, but maybe i am missing something? >> For example in the above case, if I want the names attached to each other >> with a comma, why wouldn't I just create a function doing exactly this? Why >> would I first write a single name generator and then decorate it so that I >> never can get single names anymore (this is the case, isn't it? Once >> decorated, I can not get the original behaviour of the function anymore. > > The example given is a toy. It's hardly useful. Right. It was supposed to be an example. In my case, I work with a script used to build a XML file. I change this script from time to time in order to match the requirements. Here I find it useful just to add some more yield statements for adding entries. But now that I think again about it, it's more an example for generators, not so much for decorators - which I like as well. ***** But some useful examples for decorators include 1. "Threadifying" a function, i.e. turning it into a Thread object, or into a callable which in turn starts a thread according to the given parameters. 2. Automatically calling a function if the given module is executed as a script, a kind of replacement for the "if __name__ == '__main__':" stuff. 3. Meta decorators: I find it annoying to have to wrap the function given to the decorator into another one, modifying its properties and returning that in turn. def wrapfunction(decorated): """Wrap a function taking (f, *a, **k) and replace it with a function taking (f) and returning a function taking (*a, **k) which calls our decorated function. """ from functools import wraps @wraps(decorated) def wrapped_outer(f): @wraps(f) def wrapped_inner(*a, **k): return decorated(f, *a, **k) return wrapped_inner return wrapped_outer makes it much easier to create decorators which just wrap a function into another, extending its funtionality: @wrapfunction def return_list(f, *a, **k) return list(f(*a, **k)) is much easier and IMHO much better to read than def return_list(f): """Wrap a function taking (f, *a, **k) and replace it with a function taking (f) and returning a function taking (*a, **k) which calls our decorated function. """ from functools import wraps @wraps(f) def wrapped(*a, **k): return list(f, *a, **k) return wrapped - especially if used multiple times. 3a. This is a modified case of my first example: If you want a function to assemble and return a list instead of a generator object, but prefer "yield" over "ret=[]; ret.append();...", you can do that with this @return_list. 4. So-called "indirect decorators": @spam(eggs) def foo(bar): pass are as well quite tricky to build when taking def indirdeco(ind): from functools import update_wrapper, wraps upd=wraps(ind) # outer wrapper: replaces a call with *a, **k with an updated # lambda, getting the function to be wrapped and applying it and # *a, **k to ind. outerwrapper=lambda *a, **k: upd(lambda f: ind(f, *a, **k)) # We update this as well: return upd(outerwrapper) # We don't update f nor the result of ind() - it is the callee's # business. It is kind of reverse to 3. @indirdeco def addingdeco(f, offset): return lambda *a, **k: f(*a, **k) + offset # Here should maybe be wrapped - it is just supposed to be an # example. 5. Creating a __all__ for a module. Instead of maintaining it somewhere centrally, you can take a class AllList(list): """list which can be called in order to be used as a __all__-adding decorator""" def __call__(self, obj): """for decorators""" self.append(obj.__name__) return obj , do a __all__ = AllList() and subsequently decorate each function with @__all__ 6. Re-use a generator: A generator object is creted upon calling the generator function with parameters and can be used only once. A object wrapping this generator might be useful. # Turn a generator into a iterable object calling the generator. class GeneratorIterable(object): """Take a parameterless generator function and call it on every iteration.""" def __init__(self, gen): # Set object attribute. self.gen = gen def __iter__(self): # Class attribute calls object attribute in order to keep # namespace variety small. return self.gen() @GeneratorIterable def mygen(): yield 1 yield 2 list(mygen) -> [1, 2] list(mygen) -> [1, 2] # again, without the () Might be useful if the object is to be transferred to somewhere else. ***** Some of these decorators are more useful, some less if seen standalone, but very handy if creating other decorators. HTH nevertheless, Thomas From jeanmichel at sequans.com Fri Dec 9 05:03:53 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Fri, 09 Dec 2011 11:03:53 +0100 Subject: Misleading error message of the day In-Reply-To: <4EE11A69.9060207@stoneleaf.us> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> Message-ID: <4EE1DD09.4030707@sequans.com> Ethan Furman wrote: > Jean-Michel Pichavant wrote: >> You have to opportunity to not use unpacking anymore :o) There is a >> recent thread were the dark side of unpacking was exposed. Unpacking >> is a cool feautre for very small applications but should be avoided >> whenever possible otherwise. > > Which thread was that? > > > ~Ethan~ "A tuple in order to pass returned values ?" was the thread. JM From robert.kern at gmail.com Fri Dec 9 05:08:08 2011 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 09 Dec 2011 10:08:08 +0000 Subject: I love the decorator in Python!!! In-Reply-To: <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> Message-ID: On 12/9/11 5:02 AM, alex23 wrote: > On Dec 9, 2:38 am, Chris Angelico wrote: >> One piece of sophistication that I would rather like to see, but don't >> know how to do. Instead of *args,**kwargs, is it possible to somehow >> copy in the function's actual signature? I was testing this out in >> IDLE, and the fly help for the function no longer gave useful info >> about its argument list. > > The 3rd party 'decorator' module takes care of issues like docstrings > & function signatures. I'd really like to see some of that > functionality in the stdlib though. Much of it is: http://docs.python.org/library/functools#functools.update_wrapper -- 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 From steve+comp.lang.python at pearwood.info Fri Dec 9 06:27:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Dec 2011 11:27:16 GMT Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> Message-ID: <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Dec 2011 10:30:01 +0100, Hrvoje Niksic wrote: > In a language like Python, the difference between O(1) and O(log n) is > not the primary reason why programmers use dict; they use it because > it's built-in, efficient compared to alternatives, and convenient to > use. If Python dict had been originally implemented as a tree, I'm sure > it would be just as popular. Except for people who needed dicts with tens of millions of items. Remember also that dicts are used for looking up names in Python. Nearly all method calls, attribute accesses, global name lookups, function calls, etc. go through at least one and potentially multiple dict lookups. The simple statement: n = len(x.y) + len(z) likely requires nine dict lookups, and potentially more. In even a small application, there could be tens of millions of dict lookups; changing each of them from O(1) to O(log N) could result in a measurable slowdown to Python code in real applications. That is why dicts are highly optimized for speed. As fast as dicts are, sometimes they aren't fast enough. One common micro- optimization for tight loops and time-critical code is to create local variables from globals or builtins, because local variable access bypasses dict lookup. So people would notice if dicts were slower. -- Steven From __peter__ at web.de Fri Dec 9 06:27:47 2011 From: __peter__ at web.de (Peter Otten) Date: Fri, 09 Dec 2011 12:27:47 +0100 Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Message-ID: Massi wrote: > for k in D : exec "%s = D[k]" %k > > That seems to do the trick, but someone speaks about "dirty code", can > anyone point me out which problems this can generate? exec can run arbitrary code, so everybody reading the above has to go back to the definition of D to verify that it can only contain "safe" keys. Filling D with user-input is right out because a malicious user could do anything he likes. Here's a harmless demo that creates a file: >>> d = {"x = 42\nwith open('tmp.txt', 'w') as f:\n f.write('whatever')\nx": 123} >>> for k in d: exec "%s = d[k]" % k ... >>> x 123 >>> open("tmp.txt").read() 'whatever' From steve+comp.lang.python at pearwood.info Fri Dec 9 06:59:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Dec 2011 11:59:16 GMT Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Message-ID: <4ee1f814$0$29977$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Dec 2011 01:55:28 -0800, Massi wrote: > for k in D : exec "%s = D[k]" %k > > That seems to do the trick, but someone speaks about "dirty code", can > anyone point me out which problems this can generate? Again, thank you > for your help! Just the second-most common source of viruses, malware and security vulnerabilities (behind buffer overflows): code injection attacks. Code injection attacks make up at least three of the top 25 security vulnerabilities on the CWE/SANS list: http://cwe.mitre.org/top25/index.html including the top 2 most dangerous threats (beating even our old friend, the buffer overflow): SQL injection and OS command injection. Your use of exec is vulnerable to attack if a hostile user can fool you into using a dict like this one: D = {'a': '42', 'import os;'\ ' os.system("""echo "ha ha i ownz ur system rm-rf/" """); b': '23', } for k in D : exec "%s = D[k]" % k You might think you're safe from such attacks, but (1) it is MUCH harder to protect against them than you might think; and (2) code has a habit of being re-used. Today your application might only be used by you; next week your code might find itself embedded in a web-application where hostile script kiddies can destroy your server with a single upload. My advice is: (1) If you need to ask why exec is dangerous, you shouldn't touch it. (2) If you're sure you can protect against code injection, you can't. (3) If you think you need exec, you probably don't. (4) If you think you can make exec safe with a prohibited list of dangerous strings, you probably can't. -- Steven From bieffe62 at gmail.com Fri Dec 9 07:00:06 2011 From: bieffe62 at gmail.com (Francesco Bochicchio) Date: Fri, 9 Dec 2011 04:00:06 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <8e5b46e4-86a9-4192-ac99-7872d08677fc@x7g2000yqb.googlegroups.com> On 8 Dic, 12:22, K.-Michael Aye wrote: > On 2011-12-08 08:59:26 +0000, Thomas Rachel said: > > > > > Am 08.12.2011 08:18 schrieb 88888 Dihedral: > >> I use the @ decorator to behave exactly like a c macro that > >> does have fewer side effects. > > >> I am wondering is there other interesting methods to do the > >> jobs in Python? > > > In combination with a generator, you can do many funny things. > > > For example, you can build up a string: > > > def mkstring(f): > > ? ? ?"""Turns a string generator into a string, > > ? ? ?joining with ", ". > > ? ? ?""" > > ? ? ?return ", ".join(f()) > > > def create_answer(): > > ? ? ?@mkstring > > ? ? ?def people(): > > ? ? ? ? ?yield "Anna" > > ? ? ? ? ?yield "John" > > ? ? ? ? ?yield "Theo" > > > ? ? ?return "The following people were here: " + people > > > Many other things are thinkable... > > > Thomas > > I am still perplexed about decorators though, am happily using Python > for many years without them, but maybe i am missing something? > For example in the above case, if I want the names attached to each > other with a comma, why wouldn't I just create a function doing exactly > this? Why would I first write a single name generator and then decorate > it so that I never can get single names anymore (this is the case, > isn't it? Once decorated, I can not get the original behaviour of the > function anymore. > So, above, why not > def mkstring(mylist): > with the same function declaration and then just call it with a list of > names that I generate elsewhere in my program? > I just can't identify the use-case for decorators, but as I said, maybe > I am missing something. > > Michael I had/have similar feelings. For instance, this is something that I tought useful, but then I never used in real code. The idea was to find a way to automate this code pattern, which I do a lot: class SomeClass: def __init__(self, some, attribute, here ): self.some, self.attribute, self.here = some, attribute, here In other words, I often define classes in which the constructor list of arguments corresponds one-to-one to class attributes. So I thought of this (it uses class decorators so it only works with Python 3.x ) : class FieldsDecorator: def __init__(self, *names): self.names = names def __call__(self, cls): def constructor(instance, **kwds): for n,v in kwds.items(): if n in self.names: setattr(instance, n, v) else: raise TypeError("%s is not a valid field" % s ) setattr(cls, '__init__', constructor ) return cls @FieldsDecorator("uno", "due") class Prova: pass p = Prova(uno=12, due=9) print (p.uno, p.due ) It works and it is nice, but I don't find it compelling enough to use it. I keep assigning directly the attributes, which is more readable. Decorators are really useful when you have lot of repetitive boilercode that you _want_ to hide, since it has little to do with the problem logic and more to to with the technicalities of the programming language or of some framework that you are using. It is called "separating of concerns" I think, and is one of the principles of Aspect-Oriented Programming (and with decorators you can do some nice AOP exercises ... ). Ciao --- FB From steve+comp.lang.python at pearwood.info Fri Dec 9 07:03:19 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Dec 2011 12:03:19 GMT Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> <4ee1f814$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ee1f907$0$29977$c3e8da3$5496439d@news.astraweb.com> On Fri, 09 Dec 2011 11:59:16 +0000, Steven D'Aprano wrote: > Just the second-most common source of viruses, malware and security > vulnerabilities (behind buffer overflows): code injection attacks. Oops, I forgot to go back and revise this sentence. Code injection attacks are now the most common, not second-most common, source of security vulnerabilities. http://cwe.mitre.org/top25/index.html -- Steven From rosuav at gmail.com Fri Dec 9 07:08:23 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 9 Dec 2011 23:08:23 +1100 Subject: Dynamic variable creation from string In-Reply-To: <4ee1f814$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> <4ee1f814$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 9, 2011 at 10:59 PM, Steven D'Aprano wrote: > (4) If you think you can make exec safe with a prohibited list of > dangerous strings, you probably can't. If you think that it's even _possible_ to make exec safe with a blacklist, I have a nice padded cell for you over here. Security is NEVER achieved with blacklists, ONLY whitelists. ChrisA From andrea.crotti.0 at gmail.com Fri Dec 9 07:43:07 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Fri, 09 Dec 2011 12:43:07 +0000 Subject: tracking variable value changes In-Reply-To: References: Message-ID: <4EE2025B.3050905@gmail.com> On 12/08/2011 08:17 PM, Catherine Moroney wrote: > Hello, > > Is there a way to create a C-style pointer in (pure) Python so the > following code will reflect the changes to the variable "a" in the > dictionary "x"? > > For example: > > >>> a = 1.0 > >>> b = 2.0 > >>> x = {"a":a, "b":b} > >>> x > {'a': 1.0, 'b': 2.0} > >>> a = 100.0 > >>> x > {'a': 1.0, 'b': 2.0} ## at this point, I would like the value > ## associated with the "a" key to be 100.0 > ## rather than 1.0 > > If I make "a" and "b" numpy arrays, then changes that I make to the > values of a and b show up in the dictionary x. > > My understanding is that when I redefine the value of "a", that Python > is creating a brand-new float with the value of 100.0, whereas when I > use numpy arrays I am merely assigning a new value to the same object. > > Is there some way to rewrite the code above so the change of "a" from > 1.0 to 100.0 is reflected in the dictionary. I would like to use > simple datatypes such as floats, rather than numpy arrays or classes. > I tried using weakref's, but got the error that a weak reference cannot > be created to a float. > > Catherine Not sure if it's exactly pure python but Traits can actually do this https://github.com/enthought/traits From tinnews at isbd.co.uk Fri Dec 9 09:41:56 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Fri, 9 Dec 2011 14:41:56 +0000 Subject: Contacts/Addressbook application - any good Python ones out there? Message-ID: I'm after an application for managing Contacts (i.e. an Address Book) and as I suspect I will want to 'tune' it a bit Python would be my preferred language. So far I have found :- pycocuma - reasonable but rather old and a bit clunky (uses TCL/Tk) pyaddressbook - newer but very minimal Does anyone have any other suggestions? I'd prefer an application which uses vCards as its native data storage format but that's not vital and I'd also like to have a GUI but again that's not vital, a well designed curses/terminal application would be OK too. -- Chris Green From roy at panix.com Fri Dec 9 09:43:22 2011 From: roy at panix.com (Roy Smith) Date: Fri, 09 Dec 2011 09:43:22 -0500 Subject: Misleading error message of the day References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> Message-ID: In article , Jean-Michel Pichavant wrote: > Ethan Furman wrote: > > Jean-Michel Pichavant wrote: > >> You have to opportunity to not use unpacking anymore :o) There is a > >> recent thread were the dark side of unpacking was exposed. Unpacking > >> is a cool feautre for very small applications but should be avoided > >> whenever possible otherwise. > > > > Which thread was that? > > > > > > ~Ethan~ > "A tuple in order to pass returned values ?" was the thread. > > JM To save everybody the effort of finding it, I think he's talking about https://groups.google.com/d/topic/comp.lang.python/2vcwYfIQSOM/discussion From alec.taylor6 at gmail.com Fri Dec 9 10:12:39 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Sat, 10 Dec 2011 02:12:39 +1100 Subject: Contacts/Addressbook application - any good Python ones out there? In-Reply-To: References: Message-ID: Wammu? On Sat, Dec 10, 2011 at 1:41 AM, wrote: > I'm after an application for managing Contacts (i.e. an Address Book) > and as I suspect I will want to 'tune' it a bit Python would be my > preferred language. > > So far I have found :- > > ? ?pycocuma - reasonable but rather old and a bit clunky (uses TCL/Tk) > ? ?pyaddressbook - newer but very minimal > > Does anyone have any other suggestions? ?I'd prefer an application > which uses vCards as its native data storage format but that's not > vital and I'd also like to have a GUI but again that's not vital, a > well designed curses/terminal application would be OK too. > > -- > Chris Green > -- > http://mail.python.org/mailman/listinfo/python-list From invalid at invalid.invalid Fri Dec 9 10:13:46 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 9 Dec 2011 15:13:46 +0000 (UTC) Subject: [OT] Book authoring References: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> Message-ID: On 2011-12-09, Miki Tebeka wrote: > Greetings, > > Any recommendations for a book authoring system that supports the following: > 1. Code examples (with syntax highlighting and line numbers) > 2. Output HTML, PDF, ePub ... > 3. Automatic TOC and index > 4. Search (in HTML) - this is a "nice to have" http://en.wikipedia.org/wiki/Lightweight_markup_language I've used asciidoc extensively and reStructuredText a little. Asciidoc will produce all the formats you mentioned (though I've only refularly used HTML and PDF). reStructuredText is what's used for Python docs isn't it? > Can I somehow use Sphinx? Don't know what Sphinx is. And there's always the old stand-by LaTeX, but it's a bit more heavyweight with more of a learning curve. OTOH, it does produce text-book quality output. -- Grant Edwards grant.b.edwards Yow! BELA LUGOSI is my at co-pilot ... gmail.com From nicholas.dokos at hp.com Fri Dec 9 10:43:27 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Fri, 09 Dec 2011 10:43:27 -0500 Subject: [OT] Book authoring In-Reply-To: Message from Grant Edwards of "Fri, 09 Dec 2011 15:13:46 GMT." References: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> Message-ID: <9925.1323445407@alphaville.dokosmarshall.org> Grant Edwards wrote: > On 2011-12-09, Miki Tebeka wrote: > > Greetings, > > > > Any recommendations for a book authoring system that supports the following: > > 1. Code examples (with syntax highlighting and line numbers) > > 2. Output HTML, PDF, ePub ... > > 3. Automatic TOC and index > > 4. Search (in HTML) - this is a "nice to have" > > http://en.wikipedia.org/wiki/Lightweight_markup_language > > I've used asciidoc extensively and reStructuredText a little. Asciidoc > will produce all the formats you mentioned (though I've only refularly > used HTML and PDF). reStructuredText is what's used for Python docs > isn't it? > > > Can I somehow use Sphinx? > > Don't know what Sphinx is. > I think Sphinx is used for the python docs: it sits atop rST and does all the transformations/processing to produce the desired output ( http://sphinx.pocoo.org ) > And there's always the old stand-by LaTeX, but it's a bit more > heavyweight with more of a learning curve. OTOH, it does produce > text-book quality output. > There is also orgmode, which has been used for a few books (http://orgmode.org ). I know it does HTML and PDF (the latter through latex), but I'm not sure about ePub: ISTR somebody actually did ePub for his book but I don't remember details. The indexing is manual: add #+index: foo entries as required. But in general, imo, automatic indexing for books sucks raw eggs (it works much better for highly regular source code like the python source base). Nick From nicholas.dokos at hp.com Fri Dec 9 11:18:40 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Fri, 09 Dec 2011 11:18:40 -0500 Subject: [OT] Book authoring In-Reply-To: Message from Nick Dokos of "Fri, 09 Dec 2011 10:43:27 EST." <9925.1323445407@alphaville.dokosmarshall.org> References: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> <9925.1323445407@alphaville.dokosmarshall.org> Message-ID: <10973.1323447520@alphaville.dokosmarshall.org> Nick Dokos wrote: > There is also orgmode, which has been used for a few books > (http://orgmode.org ). I know it does HTML and PDF (the latter through > latex), but I'm not sure about ePub: ISTR somebody actually did ePub for > his book but I don't remember details. Avdi Grimm produced his book "Exceptional Ruby" (http://exceptionalruby.com ) this way, including ePub formats (I hope mentioning Ruby in this context is not a punishable offense...) Apparently, there is calibre (http://calibre-ebook.com/ ) that will take you from HTML to ePub. See this orgmode list article e.g. http://thread.gmane.org/gmane.emacs.orgmode/41826 Nick From skip at pobox.com Fri Dec 9 11:19:12 2011 From: skip at pobox.com (Skip Montanaro) Date: Fri, 9 Dec 2011 16:19:12 +0000 (UTC) Subject: How to build 64-bit Python on Solaris with GCC? References: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> <4EDF604F.9070306@gmail.com> Message-ID: Karim gmail.com> writes: > ./configure > make > make install Thanks. I have several different versions in my local sandbox. None are 64-bit ELFs. Just to make sure I hadn't missed some new development in this area, I cloned the hg repository and build the trunk version from scratch. I get a 32-bit executable on Solaris: % file ./python ./python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), not stripped Skip From andrea.crotti.0 at gmail.com Fri Dec 9 11:25:28 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Fri, 09 Dec 2011 16:25:28 +0000 Subject: [OT] Book authoring In-Reply-To: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> References: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> Message-ID: <4EE23678.5060409@gmail.com> On 12/09/2011 03:25 AM, Miki Tebeka wrote: > Greetings, > > Any recommendations for a book authoring system that supports the following: > 1. Code examples (with syntax highlighting and line numbers) > 2. Output HTML, PDF, ePub ... > 3. Automatic TOC and index > 4. Search (in HTML) - this is a "nice to have" > > Can I somehow use Sphinx? > > Thanks, > -- > Miki I think it depends on what you want exactly. If it's a nice book with a scientific look and many complicated tables/figures than I think that LaTeX is the way to go (maybe even org-mode but it's mainly for emacs-fans). The problem with LaTeX is that it's quite tricky to export to other formats, harder to learn and not as flexible as a python-based solution as Sphinx. I would suggest to try Sphinx and see if you're missing something.. From stefan-usenet at bytereef.org Fri Dec 9 11:41:37 2011 From: stefan-usenet at bytereef.org (Stefan Krah) Date: Fri, 9 Dec 2011 17:41:37 +0100 Subject: How to build 64-bit Python on Solaris with GCC? In-Reply-To: References: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> <4EDF604F.9070306@gmail.com> Message-ID: <20111209164137.GA4912@sleipnir.bytereef.org> Skip Montanaro wrote: > Thanks. I have several different versions in my local sandbox. None > are 64-bit ELFs. Just to make sure I hadn't missed some new development > in this area, I cloned the hg repository and build the trunk version > from scratch. I get a 32-bit executable on Solaris: > > % file ./python > ./python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), > dynamically linked (uses shared libs), not stripped ./configure CFLAGS=-m64 LDFLAGS=-m64 should work with a reasonably recent revision. Stefan Krah From hniksic at xemacs.org Fri Dec 9 11:51:06 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Fri, 09 Dec 2011 17:51:06 +0100 Subject: order independent hash? References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87ty59adtx.fsf@xemacs.org> Steven D'Aprano writes: > Except for people who needed dicts with tens of millions of items. Huge tree-based dicts would be somewhat slower than today's hash-based dicts, but they would be far from unusable. Trees are often used to organize large datasets for quick access. The case of dicts which require frequent access, such as those used to implement namespaces, is different, and more interesting. Those dicts are typically quite small, and for them the difference between O(log n) and O(1) is negligible in both theory (since n is "small", i.e. bounded) and practice. In fact, depending on the details of the implementation, the lookup in a small tree could even be marginally faster. From tinnews at isbd.co.uk Fri Dec 9 11:59:34 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Fri, 9 Dec 2011 16:59:34 +0000 Subject: Contacts/Addressbook application - any good Python ones out there? References: Message-ID: Alec Taylor wrote: > Wammu? > I hadn't really considered gammu/wammu as I saw it as a mobile phone synchrinsation tool, but I've looked a bit harder and it might very well be what I need - thank you! > On Sat, Dec 10, 2011 at 1:41 AM, wrote: > > I'm after an application for managing Contacts (i.e. an Address Book) > > and as I suspect I will want to 'tune' it a bit Python would be my > > preferred language. > > > > So far I have found :- > > > > ? ?pycocuma - reasonable but rather old and a bit clunky (uses TCL/Tk) > > ? ?pyaddressbook - newer but very minimal > > > > Does anyone have any other suggestions? ?I'd prefer an application > > which uses vCards as its native data storage format but that's not > > vital and I'd also like to have a GUI but again that's not vital, a > > well designed curses/terminal application would be OK too. > > > > -- > > Chris Green > > -- > > http://mail.python.org/mailman/listinfo/python-list -- Chris Green From andrecrash3r at gmail.com Fri Dec 9 12:12:01 2011 From: andrecrash3r at gmail.com (=?ISO-8859-1?Q?Andr=E9_Lopes?=) Date: Fri, 9 Dec 2011 17:12:01 +0000 Subject: Execute python within Oracle Message-ID: Hi all, I wrote a simple Java program to be called within an Oracle database. The goal is to execute a Python program within the DB itself, by the means of a Java program. The problem is that when I execute the procedure inside the DB, nothing happens? If I create the same Java class outside the DB and execute it, the python program works perfectly, only inside the DB nothing happens. The program is the following. CREATE OR REPLACE AND COMPILE java source named "OSCommand" as import java.io.*; public class OSCommand{ public static void Run(){ try { Runtime r = Runtime.getRuntime(); Process p = r.exec("cmd /c C:\\Python32\\python.exe C:\\Ficheiros\\SAP\\Novos\\xls2csv.py C:\\Ficheiros\\SAP\\Novos\\20111020_ListagemSAP.xlsx"); } catch (Exception e) { e.printStackTrace(); } } } / Can anyone help? Thanks, Andr? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Fri Dec 9 12:13:21 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Dec 2011 04:13:21 +1100 Subject: order independent hash? In-Reply-To: <87ty59adtx.fsf@xemacs.org> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> <87ty59adtx.fsf@xemacs.org> Message-ID: On Sat, Dec 10, 2011 at 3:51 AM, Hrvoje Niksic wrote: > The case of dicts which require frequent access, such as those used to > implement namespaces, is different, and more interesting. ?Those dicts > are typically quite small, and for them the difference between O(log n) > and O(1) is negligible in both theory (since n is "small", i.e. bounded) > and practice. ?In fact, depending on the details of the implementation, > the lookup in a small tree could even be marginally faster. This is something where, I am sure, far greater minds than mine delve... but, would a splay tree be effective for name lookups? In most cases, you'll have a huge puddle of names of which you use the tiniest fraction; and a splay tree would, in effect, automatically optimize itself to handle tight loops. ChrisA From ethan at stoneleaf.us Fri Dec 9 12:39:13 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 09 Dec 2011 09:39:13 -0800 Subject: Misleading error message of the day In-Reply-To: <4EE1DD09.4030707@sequans.com> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> <4EE1DD09.4030707@sequans.com> Message-ID: <4EE247C1.2040107@stoneleaf.us> Jean-Michel Pichavant wrote: > Ethan Furman wrote: >> Jean-Michel Pichavant wrote: >>> You have to opportunity to not use unpacking anymore :o) There is a >>> recent thread were the dark side of unpacking was exposed. Unpacking >>> is a cool feautre for very small applications but should be avoided >>> whenever possible otherwise. >> >> Which thread was that? >> >> >> ~Ethan~ > "A tuple in order to pass returned values ?" was the thread. Thanks. ~Ethan~ From ian.g.kelly at gmail.com Fri Dec 9 12:50:31 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 9 Dec 2011 10:50:31 -0700 Subject: Execute python within Oracle In-Reply-To: References: Message-ID: 2011/12/9 Andr? Lopes : > Hi all, > > > > ?? I wrote a simple Java program to be called within an Oracle database. The > goal is to execute a Python program within the DB itself, by the means of a > Java program. The problem is that when I execute the procedure inside the > DB, nothing happens? > > > > ?? If I create the same Java class outside the DB and execute it, the python > program works perfectly, only inside the DB nothing happens. The program is > the following. Have you granted the necessary permissions to execute programs from Java? http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:952229840241 Note that article is for Oracle 8, I'm not sure whether the permissions might have changed since then. Cheers, Ian From dihedral88888 at googlemail.com Fri Dec 9 13:28:49 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 9 Dec 2011 10:28:49 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> On Thursday, December 8, 2011 7:43:12 PM UTC+8, Chris Angelico wrote: > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > > I am still perplexed about decorators though, am happily using Python for > > many years without them, but maybe i am missing something? > > For example in the above case, if I want the names attached to each other > > with a comma, why wouldn't I just create a function doing exactly this? Why > > would I first write a single name generator and then decorate it so that I > > never can get single names anymore (this is the case, isn't it? Once > > decorated, I can not get the original behaviour of the function anymore. > > The example given is a toy. It's hardly useful. However, there are a > number of handy uses for decorators; mostly, they consist of giving a > single simple keyword to a complicated set of logic. One example is > the @classmethod and @staticmethod decorators - the code to implement > them could be uglier than nested inline assembly, but you don't have > to care, because you just type "@staticmethod" in front of your def > statement and it does its magic. > > Here's a handy trick that I'm sure someone has done in a more sophisticated way: > > def trace(func): > if debugmode: > return lambda *a,**ka: > (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] > return func > > Then you put @trace in front of all your functions, and if debugmode > is False, nothing will be done - but set it to true, and you get > console output at the entry and exit of each function. > > >>> @trace > def test(x): > print("Test! "+x) > return 5 > > >>> test("asdf") > >test > Test! asdf > 5 > > Again, it's helpful because it condenses all the logic (including the > 'debugmode' flag) down to a single high level directive: "Trace this > function". > > ChrisA I did use decorators to turn functions into iterables to be traced. From dihedral88888 at googlemail.com Fri Dec 9 13:28:49 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 9 Dec 2011 10:28:49 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Message-ID: <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> On Thursday, December 8, 2011 7:43:12 PM UTC+8, Chris Angelico wrote: > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > > I am still perplexed about decorators though, am happily using Python for > > many years without them, but maybe i am missing something? > > For example in the above case, if I want the names attached to each other > > with a comma, why wouldn't I just create a function doing exactly this? Why > > would I first write a single name generator and then decorate it so that I > > never can get single names anymore (this is the case, isn't it? Once > > decorated, I can not get the original behaviour of the function anymore. > > The example given is a toy. It's hardly useful. However, there are a > number of handy uses for decorators; mostly, they consist of giving a > single simple keyword to a complicated set of logic. One example is > the @classmethod and @staticmethod decorators - the code to implement > them could be uglier than nested inline assembly, but you don't have > to care, because you just type "@staticmethod" in front of your def > statement and it does its magic. > > Here's a handy trick that I'm sure someone has done in a more sophisticated way: > > def trace(func): > if debugmode: > return lambda *a,**ka: > (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] > return func > > Then you put @trace in front of all your functions, and if debugmode > is False, nothing will be done - but set it to true, and you get > console output at the entry and exit of each function. > > >>> @trace > def test(x): > print("Test! "+x) > return 5 > > >>> test("asdf") > >test > Test! asdf > 5 > > Again, it's helpful because it condenses all the logic (including the > 'debugmode' flag) down to a single high level directive: "Trace this > function". > > ChrisA I did use decorators to turn functions into iterables to be traced. From geoff.bache at gmail.com Fri Dec 9 14:32:29 2011 From: geoff.bache at gmail.com (Geoff Bache) Date: Fri, 9 Dec 2011 11:32:29 -0800 (PST) Subject: Buffering of sys.stdout and sys.stderr in python3 (and documentation) Message-ID: Hi all, Short version: I'm a bit confused in general as to the changes between python2 and python3 regarding how standard output and standard error do buffering. A few things seem to have changed and I've failed to find any documentation of how and why. Also, the meaning of "python -u" seems to have changed and the docs don't seem to reflect the new behaviour (and I can't find any docs about the change either)... Long version: >From rude experiment it seems that: 1) In Python 2.x, standard error was always unbuffered while standard output was buffered by default. In python3, both are buffered. In both cases, "buffered" means line-buffered when writing to the console and not line-buffered when redirected to files. 2) In Python 2.x, the "-u" flag meant everything was totally unbuffered. In Python 3.x, it means that both stdout and stderr are line-buffered also when redirected to files. Are either of these changes documented anywhere? (1) seems important : it can lead to not seeing exception printouts, if stderr is redirected to a file and the program is subsequently terminated with SIGTERM. I just wasted quite a bit of time due to this situation... This is what the Python 3 docs have to say about the -u flag: "Force the binary layer of the stdin, stdout and stderr streams (which is available as their buffer attribute) to be unbuffered. The text I/O layer will still be line-buffered." The "still" seems misleading to me, as it is only relevant if writing to the console. It would be useful to contrast the behaviour with and without "-u" when writing to files I would say. Regards, Geoff Bache From ethan at stoneleaf.us Fri Dec 9 15:01:01 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 09 Dec 2011 12:01:01 -0800 Subject: Dynamic variable creation from string In-Reply-To: <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Message-ID: <4EE268FD.4090806@stoneleaf.us> Massi wrote: > Thank you all for your replies, first of all my Sum function was an > example simplifying what I have to do in my real funciton. In general > the D dictionary is complex, with a lot of keys, so I was searching > for a quick method to access all the variables in it without doing the > explicit creation: > > a, b, c = D['a'], D['b'], D['c'] > > and without using directly the D dictionary (boring...). > When I talked about nested function I meant both cases Chris, but this > is not a really tighten bound. > I tried to follow the hints of Chris together with some help by google > and used the following code: > > for k in D : exec "%s = D[k]" %k > > That seems to do the trick, but someone speaks about "dirty code", can > anyone point me out which problems this can generate? > Again, thank you for your help! Besides the serious security issues, this method won't make the problem any better in Python 3. ~Ethan~ From tinnews at isbd.co.uk Fri Dec 9 17:06:20 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Fri, 9 Dec 2011 22:06:20 +0000 Subject: Contacts/Addressbook application - any good Python ones out there? References: Message-ID: tinnews at isbd.co.uk wrote: > Alec Taylor wrote: > > Wammu? > > > I hadn't really considered gammu/wammu as I saw it as a mobile phone > synchrinsation tool, but I've looked a bit harder and it might very > well be what I need - thank you! > Well one problem with wammu is that you can't do anything with the program unless there's a phone connected. -- Chris Green From ladasky at my-deja.com Fri Dec 9 18:14:30 2011 From: ladasky at my-deja.com (John Ladasky) Date: Fri, 9 Dec 2011 15:14:30 -0800 (PST) Subject: Multiprocessing bug, is information ever omitted from a traceback? Message-ID: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> Hi folks, A tangent off of this thread: http://groups.google.com/group/comp.lang.python/browse_frm/thread/751b7050c756c995# I'm programming in Python 2.6 on Ubuntu Linux 10.10, if it matters. I'm trying to track down a multiprocessing bug. Here's my traceback. All lines of code referenced in the traceback are in the standard library code: Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner self.run() File "/usr/lib/python2.6/threading.py", line 484, in run self.__target(*self.__args, **self.__kwargs) File "/usr/lib/python2.6/multiprocessing/pool.py", line 284, in _handle_tasks put(task) TypeError: expected string or Unicode object, NoneType found Fortunately, I have a working version of my code. I was trying to add new features, and only my new code is causing trouble. This has allowed me to examine the contexts of task when everything works. Task is not a string when the program works. Task is not None when the program doesn't work. In fact, task is a deeply-nested tuple. NO PART of this tuple ever contains any strings, as far as I can tell. More details in my original thread. Now, of course I've seen that the standard traceback shows you the lines where various steps in a chain of function calls were taken. The traceback skips over any lines in the code between successive function calls, and assumes that you can follow along. No problem, I can do that. But when multiprocessing is involved, can this traceback be truncated in some way, for example when code execution switches over to a subprocess? I'm wondering if more code is getting executed after "put(task)" that I'm not seeing. Thanks for any information! From henryx_b at yahoo.it Fri Dec 9 18:16:30 2011 From: henryx_b at yahoo.it (Enrico 'Henryx' Bianchi) Date: Sat, 10 Dec 2011 00:16:30 +0100 Subject: Python 2 or 3 References: Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Tobiah wrote: > Use the newer version and don't look back. Interesting reply, but if I have a platform wich doesn't support Python 3 (e.g. RHEL 5.x)? ]:) Enrico P.S. note that: I *don't* want to recompile Python in production environment -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQEcBAEBAgAGBQJO4pbOAAoJED3SMOGZLYdYQ7sIAI3vfvOyQc5Gx205cDMS7bPK uXxZI7ShqybyEv0NMDapxURQhz59Kc9zh8E/OKDiXohjmkE1YA78K7qSKyrtXTMy ppcGUU5USaQhPZ+RqOEj95aTxQj3CW/8w74rNEirIMn6+yGt4QjWRuGT1K6aUM51 BXF9I22f37z/sJ7x+fZUL9R7G1HA4saRGEiQGxBgkmt6gi28nboOibdxfw9bmP5x aHbpVYQ6yo+7nOf0XZno/pl0zkpDvhS/tNvvuH8kYQIvMLyQZ/f+xZJ6yj58S5Se AGSGXEDRemw0Ge83HjJvmQE3JXjy1fc1gCQSnmqQifXW7h18q99L3okJds+uHnE= =PwK5 -----END PGP SIGNATURE----- From steve+comp.lang.python at pearwood.info Fri Dec 9 19:19:48 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Dec 2011 00:19:48 GMT Subject: Python 2 or 3 References: Message-ID: <4ee2a5a4$0$29977$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Dec 2011 00:16:30 +0100, Enrico 'Henryx' Bianchi wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Tobiah wrote: > >> Use the newer version and don't look back. > > Interesting reply, but if I have a platform wich doesn't support Python > 3 (e.g. RHEL 5.x)? ]:) RHEL supports Python 3, it just doesn't provide Python 3. It almost certainly will work if you install from source. I haven't tried it on RHEL myself, but I have done so on Fedora and Centos, and there's no problem. But be warned that you should not replace the system python with Python 3. When installing, don't use "make install", as that will replace the system Python, instead use "make altinstall". Then the command "python" will still refer to the system Python (probably Python 2.4 or 2.5?), and "python3" should refer to Python 3.x. > Enrico > P.S. note that: I *don't* want to recompile Python in production > environment You shouldn't be learning programming on a production server :) -- Steven From dihedral88888 at googlemail.com Fri Dec 9 19:21:11 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 9 Dec 2011 16:21:11 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> Message-ID: <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> On Saturday, December 10, 2011 2:28:49 AM UTC+8, 88888 Dihedral wrote: > On Thursday, December 8, 2011 7:43:12 PM UTC+8, Chris Angelico wrote: > > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > > > I am still perplexed about decorators though, am happily using Python for > > > many years without them, but maybe i am missing something? > > > For example in the above case, if I want the names attached to each other > > > with a comma, why wouldn't I just create a function doing exactly this? Why > > > would I first write a single name generator and then decorate it so that I > > > never can get single names anymore (this is the case, isn't it? Once > > > decorated, I can not get the original behaviour of the function anymore. > > > > The example given is a toy. It's hardly useful. However, there are a > > number of handy uses for decorators; mostly, they consist of giving a > > single simple keyword to a complicated set of logic. One example is > > the @classmethod and @staticmethod decorators - the code to implement > > them could be uglier than nested inline assembly, but you don't have > > to care, because you just type "@staticmethod" in front of your def > > statement and it does its magic. > > > > Here's a handy trick that I'm sure someone has done in a more sophisticated way: > > > > def trace(func): > > if debugmode: > > return lambda *a,**ka: > > (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] > > return func > > > > Then you put @trace in front of all your functions, and if debugmode > > is False, nothing will be done - but set it to true, and you get > > console output at the entry and exit of each function. > > > > >>> @trace > > def test(x): > > print("Test! "+x) > > return 5 > > > > >>> test("asdf") > > >test > > Test! asdf > > > 5 > > > > Again, it's helpful because it condenses all the logic (including the > > 'debugmode' flag) down to a single high level directive: "Trace this > > function". > > > > ChrisA > > I did use decorators to turn functions into iterables to be traced. It is easy to use decorators in python to mimic those programs in Erlang. From dihedral88888 at googlemail.com Fri Dec 9 19:21:11 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 9 Dec 2011 16:21:11 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> Message-ID: <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> On Saturday, December 10, 2011 2:28:49 AM UTC+8, 88888 Dihedral wrote: > On Thursday, December 8, 2011 7:43:12 PM UTC+8, Chris Angelico wrote: > > On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > > > I am still perplexed about decorators though, am happily using Python for > > > many years without them, but maybe i am missing something? > > > For example in the above case, if I want the names attached to each other > > > with a comma, why wouldn't I just create a function doing exactly this? Why > > > would I first write a single name generator and then decorate it so that I > > > never can get single names anymore (this is the case, isn't it? Once > > > decorated, I can not get the original behaviour of the function anymore. > > > > The example given is a toy. It's hardly useful. However, there are a > > number of handy uses for decorators; mostly, they consist of giving a > > single simple keyword to a complicated set of logic. One example is > > the @classmethod and @staticmethod decorators - the code to implement > > them could be uglier than nested inline assembly, but you don't have > > to care, because you just type "@staticmethod" in front of your def > > statement and it does its magic. > > > > Here's a handy trick that I'm sure someone has done in a more sophisticated way: > > > > def trace(func): > > if debugmode: > > return lambda *a,**ka: > > (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] > > return func > > > > Then you put @trace in front of all your functions, and if debugmode > > is False, nothing will be done - but set it to true, and you get > > console output at the entry and exit of each function. > > > > >>> @trace > > def test(x): > > print("Test! "+x) > > return 5 > > > > >>> test("asdf") > > >test > > Test! asdf > > > 5 > > > > Again, it's helpful because it condenses all the logic (including the > > 'debugmode' flag) down to a single high level directive: "Trace this > > function". > > > > ChrisA > > I did use decorators to turn functions into iterables to be traced. It is easy to use decorators in python to mimic those programs in Erlang. From steve+comp.lang.python at pearwood.info Fri Dec 9 19:41:25 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Dec 2011 00:41:25 GMT Subject: tracking variable value changes References: Message-ID: <4ee2aab4$0$29977$c3e8da3$5496439d@news.astraweb.com> On Thu, 08 Dec 2011 12:17:11 -0800, Catherine Moroney wrote: > Hello, > > Is there a way to create a C-style pointer in (pure) Python so the > following code will reflect the changes to the variable "a" in the > dictionary "x"? Strictly speaking, no, but there may be a way to get something close. See below. > For example: > > >>> a = 1.0 > >>> b = 2.0 > >>> x = {"a":a, "b":b} > >>> x > {'a': 1.0, 'b': 2.0} > >>> a = 100.0 > >>> x > {'a': 1.0, 'b': 2.0} ## at this point, I would like the value > ## associated with the "a" key to be 100.0 ## > rather than 1.0 The line "a = 100" is a rebinding, and so what you are asking for isn't directly possible. But if you are willing to live with an explicit redirection, you can somewhat simulate a pointer with a list: py> aPtr = [1.0] # not really a pointer, but let's pretend it is py> bPtr = [2.0] py> x = {'a': aPtr, 'b': bPtr} py> x {'a': [1.0], 'b': [2.0]} py> aPtr[0] = 100.0 py> x {'a': [100.0], 'b': [2.0]} If you prefer, you can write a simple class to handle the redirection with the interface of your choice. Something like this might be a good start: class SharedValue: def set(self, value): self.value = value def get(self): return self.value def __repr__(self): # Somewhat dubious. return str(self.value) py> a = SharedValue() # create a pointer py> a.set(1.0) py> x = {'a': a} py> x {'a': 1.0} py> a.set(100.0) py> x {'a': 100.0} Look at the automatic delegation pattern for a way to have operations on "a" automatically apply to the object being pointed to. (This will be *much* simpler if you don't inherit from object.) But be warned, whatever you do, rebinding will behave in the standard Python way. E.g.: py> aPtr = [2000.0] # Oops, rebound the name to something else! py> x # and the connection is lost {'a': [100.0], 'b': [2.0]} > If I make "a" and "b" numpy arrays, then changes that I make to the > values of a and b show up in the dictionary x. Yes, because numpy arrays are mutable objects. In this case, you have two (or more) references to a single object: the name "a", and the entry in dict "x". When you modify the object in either place, the change is visible in both places because they are the same object. But when you rebind the name "a" to another object -- not necessarily a *new* object, just a different one -- there is no way for the dict "x" to notice this change and follow along. > My understanding is that when I redefine the value of "a", that Python > is creating a brand-new float with the value of 100.0, whereas when I > use numpy arrays I am merely assigning a new value to the same object. Correct. Although the float need not be brand-new. Python could (but probably doesn't) re-use an existing float object. -- Steven From python.list at tim.thechases.com Fri Dec 9 20:44:48 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 09 Dec 2011 19:44:48 -0600 Subject: Obtaining user information Message-ID: <4EE2B990.2030908@tim.thechases.com> Currently I can get the currently-logged-in-userid via getpass.getuser() which would yield something like "tchase". Is there a cross-platform way to get the full username (such as from the GECOS field of /etc/passed or via something like NetUserGetInfo on Win32 so I'd get "Tim Chase" instead? Thanks, -tkc From skip at pobox.com Fri Dec 9 22:19:07 2011 From: skip at pobox.com (Skip Montanaro) Date: Sat, 10 Dec 2011 03:19:07 +0000 (UTC) Subject: How to build 64-bit Python on Solaris with GCC? References: <20111206142616.9D8B32169BC2@montanaro.dyndns.org> <4EDF604F.9070306@gmail.com> <20111209164137.GA4912@sleipnir.bytereef.org> Message-ID: > ./configure CFLAGS=-m64 LDFLAGS=-m64 should work with a reasonably > recent revision. Thanks, that did, indeed work with CPython trunk. I eventually switched from gcc to Sun's compiler though because I was getting link warnings. Skip From tjreedy at udel.edu Fri Dec 9 23:43:39 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 09 Dec 2011 23:43:39 -0500 Subject: Buffering of sys.stdout and sys.stderr in python3 (and documentation) In-Reply-To: References: Message-ID: On 12/9/2011 2:32 PM, Geoff Bache wrote: > Hi all, > > Short version: > > I'm a bit confused in general as to the changes between python2 and > python3 regarding how standard output and standard error do buffering. > A few things seem to have changed and I've failed to find any > documentation of how and why. Also, the meaning of "python -u" seems > to have changed and the docs don't seem to reflect the new behaviour > (and I can't find any docs about the change either)... > > Long version: > >> From rude experiment it seems that: > 1) In Python 2.x, standard error was always unbuffered while standard > output was buffered by default. In python3, both are buffered. In > both cases, "buffered" means line-buffered when writing to the console > and not line-buffered when redirected to files. > 2) In Python 2.x, the "-u" flag meant everything was totally > unbuffered. In Python 3.x, it means that both stdout and stderr are > line-buffered also when redirected to files. > > Are either of these changes documented anywhere? (1) seems important : > it can lead to not seeing exception printouts, if stderr is redirected > to a file and the program is subsequently terminated with SIGTERM. I > just wasted quite a bit of time due to this situation... > > This is what the Python 3 docs have to say about the -u flag: > > "Force the binary layer of the stdin, stdout and stderr streams (which > is available as their buffer attribute) to be unbuffered. The text I/O > layer will still be line-buffered." > > The "still" seems misleading to me, as it is only relevant if writing > to the console. It would be useful to contrast the behaviour with and > without "-u" when writing to files I would say. The difference from 2.x should be in What's New in 3.0, except that the new i/o module is in 2.6, so it was not exactly new. You might be able to find more in http://python.org/dev/peps/pep-3116/ You *should* be able to find sufficient info in the 3.x docs. If, after you get other responses (or not), you think the docs need upgrading, open an issue on the tracker at bugs.python.org with suggestions as specific as possible, including changed or new lines of text based on your experience and experiments. -- Terry Jan Reedy From tjreedy at udel.edu Sat Dec 10 00:00:05 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Dec 2011 00:00:05 -0500 Subject: Multiprocessing bug, is information ever omitted from a traceback? In-Reply-To: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> Message-ID: On 12/9/2011 6:14 PM, John Ladasky wrote: > http://groups.google.com/group/comp.lang.python/browse_frm/thread/751b7050c756c995# > > I'm programming in Python 2.6 on Ubuntu Linux 10.10, if it matters. It might, as many bugs have been fixed since. Can you try the same code with the most recent 2.x release, 2.7.2? Do you have working and non-working code that you can publicly release? Can you reduce the size and dependencies so the examples are closer to 'small' than 'large'? And in any case, self-contained? In my first response, I said you might have found a bug. A bogus exception message qualifies. But to do much, we need minimal good/bad examples that run or not on a current release (2.7.2 or 3.2.2). -- Terry Jan Reedy From cosmo_general at yahoo.com Sat Dec 10 00:05:07 2011 From: cosmo_general at yahoo.com (Muddy Coder) Date: Fri, 9 Dec 2011 21:05:07 -0800 (PST) Subject: How to move scrollbar by code? Message-ID: <0a0679bc-796f-42a7-99c6-d2199057f4d8@r6g2000yqr.googlegroups.com> Hi Folks, I am trying to make a listbox that will contain a looong data list, sorted, so I will be able to pre-select a data line by coding. I have done it. Say my listbox contains 1000 data lines, and my program has figured out the data line 321 is needed, so just put the cursor on data line 321. However, my scrollbar is still seating on the top, so I just can view the data line from line 0 to 30 or 40, since I can't see the desired data line 321. I still need to manually pull the scrollbar down to display the data line 321. What I want to do is to grab the adjacent data lines, say from line 300 to 340, and display this lines, while my cursor seating in the middle. I consulted the Python Docs, but did not find such details. Can somebody give an idea? Thanks! Cosmo From rosuav at gmail.com Sat Dec 10 02:30:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 10 Dec 2011 18:30:56 +1100 Subject: How to move scrollbar by code? In-Reply-To: <0a0679bc-796f-42a7-99c6-d2199057f4d8@r6g2000yqr.googlegroups.com> References: <0a0679bc-796f-42a7-99c6-d2199057f4d8@r6g2000yqr.googlegroups.com> Message-ID: On Sat, Dec 10, 2011 at 4:05 PM, Muddy Coder wrote: > I am trying to make a listbox that will contain a looong data list, > sorted, so I will be able to pre-select a data line by coding. I have > done it. Which GUI toolkit are you using? What you want is not the Python language docs, but the docs for that toolkit (GTK, Qt, Tk, etc). ChrisA From cs at zip.com.au Sat Dec 10 02:37:23 2011 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 10 Dec 2011 18:37:23 +1100 Subject: Obtaining user information In-Reply-To: <4EE2B990.2030908@tim.thechases.com> References: <4EE2B990.2030908@tim.thechases.com> Message-ID: <20111210073723.GA939@cskk.homeip.net> On 09Dec2011 19:44, Tim Chase wrote: | Currently I can get the currently-logged-in-userid via | getpass.getuser() which would yield something like "tchase". _If_ you're on a terminal. _And_ that's exactly what you want. Personally I need to the name of geteuid() or getuid() more often. | Is there a cross-platform way to get the full username (such as from | the GECOS field of /etc/passed or via something like NetUserGetInfo | on Win32 so I'd get "Tim Chase" instead? Hmm. Doesn't windows have a posix layer? pwd.getpwnam(os.getuid())[4].split(',')[0] is the best I've got. ANd it probably doesn't work in Windows:-( -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ There's not a woman in his book, the plot hinges on unkindness to animals, and the black characters mostly drown by chapter 29. - P J O'Rourke parodying a PC review of Moby Dick From hansmu at xs4all.nl Sat Dec 10 02:43:59 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Sat, 10 Dec 2011 08:43:59 +0100 Subject: Obtaining user information In-Reply-To: References: Message-ID: <4ee30dbf$0$6933$e4fe514c@news2.news.xs4all.nl> On 10/12/11 02:44:48, Tim Chase wrote: > Currently I can get the currently-logged-in-userid via getpass.getuser() > which would yield something like "tchase". > > Is there a cross-platform way to get the full username (such as from the > GECOS field of /etc/passed or via something like NetUserGetInfo on Win32 > so I'd get "Tim Chase" instead? How about: pwd.getpwuid(os.getuid()).pw_gecos This will give you the GECOS field of /etc/passed. I'd assume it contains "Tim Chase" for your account. Hope this helps, -- HansM From cs at zip.com.au Sat Dec 10 02:59:11 2011 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 10 Dec 2011 18:59:11 +1100 Subject: Obtaining user information In-Reply-To: <4ee30dbf$0$6933$e4fe514c@news2.news.xs4all.nl> References: <4ee30dbf$0$6933$e4fe514c@news2.news.xs4all.nl> Message-ID: <20111210075911.GA3651@cskk.homeip.net> On 10Dec2011 08:43, Hans Mulder wrote: | On 10/12/11 02:44:48, Tim Chase wrote: | >Currently I can get the currently-logged-in-userid via getpass.getuser() | >which would yield something like "tchase". | > | >Is there a cross-platform way to get the full username (such as from the | >GECOS field of /etc/passed or via something like NetUserGetInfo on Win32 | >so I'd get "Tim Chase" instead? | | How about: | pwd.getpwuid(os.getuid()).pw_gecos | This will give you the GECOS field of /etc/passed. | I'd assume it contains "Tim Chase" for your account. Up to a comma is the convention. Eg "Cameron Simpson, x2983". -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ Microsoft - where "cross platform" means "runs in both Win95 and WinNT". - Andy Newman From geoff.bache at gmail.com Sat Dec 10 05:03:10 2011 From: geoff.bache at gmail.com (Geoff Bache) Date: Sat, 10 Dec 2011 02:03:10 -0800 (PST) Subject: Buffering of sys.stdout and sys.stderr in python3 (and documentation) References: Message-ID: <6152c707-8bbf-4e78-a4ec-34651a5ccbfb@p9g2000vbb.googlegroups.com> Hi Terry, > The difference from 2.x should be in What's New in 3.0, except that the > new i/o module is in 2.6, so it was not exactly new. The io module existed in 2.6, but it was not used by default for standard output and standard error. The only mention of this in "What's New in 3.0" is in the section marked for changes that were already in 2.6 (which is wrong in this case), and it notes only that io.TextIOWrapper is now used, but not what implications that has for its behaviour and backward compatibility. > You might be able > to find more inhttp://python.org/dev/peps/pep-3116/ I skimmed through it but couldn't find anything relevant. It seems more "advanced" and implementation-focussed. > > You *should* be able to find sufficient info in the 3.x docs. If, after > you get other responses (or not), you think the docs need upgrading, > open an issue on the tracker at bugs.python.org with suggestions as > specific as possible, including changed or new lines of text based on > your experience and experiments. OK, I'll do that if nobody points me at some existing docs here. Regards, Geoff Bache From henryx_b at yahoo.it Sat Dec 10 06:49:27 2011 From: henryx_b at yahoo.it (Enrico 'Henryx' Bianchi) Date: Sat, 10 Dec 2011 12:49:27 +0100 Subject: Python 2 or 3 References: <4ee2a5a4$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Steven D'Aprano wrote: > RHEL supports Python 3, it just doesn't provide Python 3. True, but as you say later, the only method is to recompile. So, if I want to use Python 3 in a production environment like RHEL, I need: - A development environment similar to production (e.g. if I use RHEL 5 in production, I need at least a CentOS 5.x); - Compile Python 3 in a development environment; - Write the python app; - Release a *huge* package to install. The only bright side is to freeze version of Python and the libraries, but every update (e.g. bug fixing on a library) is by hand > When installing, don't use "make install", as that will replace the > system Python, instead use "make altinstall". Good, I didn't know this option > Then the command "python" > will still refer to the system Python (probably Python 2.4 or 2.5?), and > "python3" should refer to Python 3.x. RHEL (and CentOS) 5.x use Python 2.4 > You shouldn't be learning programming on a production server :) Of course, but if I want to use an application written in Python 3 on a production environment which doesn't support it, I have to prepare at least a development environment similar to production (ok, ok, with a VM is simple, but I need to track the exception) Enrico P.S. an alternative may be cx_freeze, but I don't know exactly hot it works P.P.S. I'm boring, but I would like my point of view because I've found precisely in this case -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQEcBAEBAgAGBQJO40dHAAoJED3SMOGZLYdYIPoH/1J6OljjCJQnmR/uwjEFCrHy TEMpfKodD40gL7lOZLMHnpJrs+Ct2Vo/1+mtAIi+vZ6rkhFG50ykBJMAlMgXkCjt I6fkp9YLKmFX9OjRuJ9qE+2P5PliyNDPKVljyfaXMhalbrtHnn7mrL9524TzhcoG +Ape1U9MPTu3naVULKWK0FjGa/RwhbSOdDOKX2IBmRHFEgtf3dZJ2xNYXUJnhnT8 fbfD87ykXyyVg6LS8c14PPeWnpFeeZBQappjoHg9+XZd8/Y1uH1NuP7k4cepzJB2 Car4lucChW9+llM4mz1BADQZuo4J1v71K5DR8mVXyM2usUlNWelkgR6GVWUmXE0= =trwj -----END PGP SIGNATURE----- From dsdale24 at gmail.com Sat Dec 10 07:41:13 2011 From: dsdale24 at gmail.com (Darren Dale) Date: Sat, 10 Dec 2011 04:41:13 -0800 (PST) Subject: WeakValueDict and threadsafety Message-ID: I am using a WeakValueDict in a way that is nearly identical to the example at the end of http://docs.python.org/library/weakref.html?highlight=weakref#example , where "an application can use objects IDs to retrieve objects that it has seen before. The IDs of the objects can then be used in other data structures without forcing the objects to remain alive, but the objects can still be retrieved by ID if they do." My program is multithreaded, so I added the necessary check for liveliness that was discussed at http://docs.python.org/library/weakref.html?highlight=weakref#weak-reference-objects . Basically, I have: import threading import weakref registry = weakref.WeakValueDictionary() reglock = threading.Lock() def get_data(oid): with reglock: data = registry.get(oid, None) if data is None: data = make_data() registry[id(data)] = data return data I'm concerned that this is not actually thread-safe. When I no longer hold strong references to an instance of data, at some point the garbage collector will kick in and remove that entry from my registry. How can I ensure the garbage collection process does not modify the registry while I'm holding the lock? Thanks, Darren From python.list at tim.thechases.com Sat Dec 10 08:31:41 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 10 Dec 2011 07:31:41 -0600 Subject: Obtaining user information In-Reply-To: <20111210073723.GA939@cskk.homeip.net> References: <4EE2B990.2030908@tim.thechases.com> <20111210073723.GA939@cskk.homeip.net> Message-ID: <4EE35F3D.70706@tim.thechases.com> On 12/10/11 01:37, Cameron Simpson wrote: > On 09Dec2011 19:44, Tim Chase wrote: > | Currently I can get the currently-logged-in-userid via > | getpass.getuser() which would yield something like "tchase". > > _If_ you're on a terminal. _And_ that's exactly what you want. > Personally I need to the name of geteuid() or getuid() more often. yes, it deals with emailing so the local userid and full-name are what I want. > | Is there a cross-platform way to get the full username (such as from > | the GECOS field of /etc/passed or via something like NetUserGetInfo > | on Win32 so I'd get "Tim Chase" instead? > > Hmm. Doesn't windows have a posix layer? > > pwd.getpwnam(os.getuid())[4].split(',')[0] > > is the best I've got. ANd it probably doesn't work in Windows:-( well, that's a more readable version of my hand-crafted opening of /etc/passwd and parsing by hand, so thanks! As you mention, the pwd module isn't available on Win32 so I still have to branch my code. I found Tim Golden's suggestion in a comment on ActiveState[1] that gave this one-liner for Win32: win32net.NetUserGetInfo (win32net.NetGetAnyDCName (), win32api.GetUserName (), 1) By changing the "1" to a "20", one of the returned key/value pairs was "full_name" and the username, so my code currently reads: def get_user_info(): "Return (userid, username) e.g. ('jsmith', 'John Smith')" userid = username = getpass.getuser() if sys.platform.lower().startswith("win"): try: import win32net, win32api USER_INFO_2 = 2 username = win32net.NetUserGetInfo( win32net.NetGetAnyDCName(), win32api.GetUserName(), USER_INFO_2, )["full_name"] or username except ImportError: pass # no win32* module, so default to userid else: import pwd username = pwd.getpwnam(userid).pw_gecos.split(',',1)[0] return userid, username It only addresses Win32 and Posix, but that's what I need for now. Thanks again. -tkc [1] http://code.activestate.com/recipes/66314-get-user-info-on-windows-for-current-user/ From dihedral88888 at googlemail.com Sat Dec 10 09:04:17 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 10 Dec 2011 06:04:17 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> Message-ID: <30440550.1225.1323525857598.JavaMail.geo-discussion-forums@prie17> Just wrap the exec() to spawn for fun. From dihedral88888 at googlemail.com Sat Dec 10 09:04:17 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 10 Dec 2011 06:04:17 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> Message-ID: <30440550.1225.1323525857598.JavaMail.geo-discussion-forums@prie17> Just wrap the exec() to spawn for fun. From dihedral88888 at googlemail.com Sat Dec 10 09:09:47 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 10 Dec 2011 06:09:47 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> Message-ID: <28734265.1226.1323526187915.JavaMail.geo-discussion-forums@prie17> Wrap functions to yield is somewhat like a sub-threading in Erlang. From dihedral88888 at googlemail.com Sat Dec 10 09:09:47 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 10 Dec 2011 06:09:47 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> Message-ID: <28734265.1226.1323526187915.JavaMail.geo-discussion-forums@prie17> Wrap functions to yield is somewhat like a sub-threading in Erlang. From duncan.booth at invalid.invalid Sat Dec 10 11:19:36 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 10 Dec 2011 16:19:36 GMT Subject: WeakValueDict and threadsafety References: Message-ID: Darren Dale wrote: > I'm concerned that this is not actually thread-safe. When I no longer > hold strong references to an instance of data, at some point the > garbage collector will kick in and remove that entry from my registry. > How can I ensure the garbage collection process does not modify the > registry while I'm holding the lock? You can't, but it shouldn't matter. So long as you have a strong reference in 'data' that particular object will continue to exist. Other entries in 'registry' might disappear while you are holding your lock but that shouldn't matter to you. What is concerning though is that you are using `id(data)` as the key and then presumably storing that separately as your `oid` value. If the lifetime of the value stored as `oid` exceeds the lifetime of the strong references to `data` then you might get a new data value created with the same id as some previous value. In other words I think there's a problem here, but nothing to do with the lock. -- Duncan Booth From thudfoo at gmail.com Sat Dec 10 12:19:31 2011 From: thudfoo at gmail.com (xDog Walker) Date: Sat, 10 Dec 2011 09:19:31 -0800 Subject: how to test attribute existence of feedparser objects In-Reply-To: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> References: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> Message-ID: <201112100919.31782.thudfoo@gmail.com> On Thursday 2011 December 08 01:34, HansPeter wrote: > Hi, > > While using the feedparser library for downloading RSS feeds some of > the blog entries seem to have no title. > > File "build\bdist.win32\egg\feedparser.py", line 382, in __getattr__ > AttributeError: object has no attribute 'title' > > Is there a way to test the existence of an attribute? > From the Fine Manual for feedparser 5.1: Testing for Existence? Feeds in the real world may be missing elements, even elements that are required by the specification. You should always test for the existence of an element before getting its value. Never assume an element is present. Use standard Python dictionary functions such as has_key to test whether an element exists. Testing if elements are present? >>> import feedparser >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml') >>> d.feed.has_key('title') True >>> d.feed.has_key('ttl') False >>> d.feed.get('title', 'No title') u'Sample feed' >>> d.feed.get('ttl', 60) 60 -- I have seen the future and I am not in it. From steve+comp.lang.python at pearwood.info Sat Dec 10 12:51:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Dec 2011 17:51:16 GMT Subject: how to test attribute existence of feedparser objects References: <2e457709-b40a-4763-8a3b-0e0560e98ee1@n10g2000vbg.googlegroups.com> Message-ID: <4ee39c14$0$29977$c3e8da3$5496439d@news.astraweb.com> On Sat, 10 Dec 2011 09:19:31 -0800, xDog Walker wrote: > Use standard Python dictionary functions such as has_key to test whether > an element exists. Idiomatic Python code today no longer uses has_key. # Was: d.feed.has_key('title') # Now preferred 'title' in d.feed -- Steven From ladasky at my-deja.com Sat Dec 10 12:53:53 2011 From: ladasky at my-deja.com (John Ladasky) Date: Sat, 10 Dec 2011 09:53:53 -0800 (PST) Subject: Multiprocessing bug, is information ever omitted from a traceback? References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> Message-ID: <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> On Dec 9, 9:00?pm, Terry Reedy wrote: > On 12/9/2011 6:14 PM, John Ladasky wrote: > > >http://groups.google.com/group/comp.lang.python/browse_frm/thread/751... > > > I'm programming in Python 2.6 on Ubuntu Linux 10.10, if it matters. > > It might, as many bugs have been fixed since. > Can you try the same code with the most recent 2.x release, 2.7.2? > Do you have working and non-working code that you can publicly release? > Can you reduce the size and dependencies so the examples are closer to > 'small' than 'large'? And in any case, self-contained? > > In my first response, I said you might have found a bug. A bogus > exception message qualifies. But to do much, we need minimal good/bad > examples that run or not on a current release (2.7.2 or 3.2.2). > > -- > Terry Jan Reedy All right, Terry, you've convinced me to look. This will take some time. I'll hack away at the two versions of my 500-line programs, until I have a minimal example. Why did you specify Python 2.7.2, instead of the 2.7.6 version that is being offered to me by Ubuntu Software Center? Does it matter? From dsdale24 at gmail.com Sat Dec 10 12:56:38 2011 From: dsdale24 at gmail.com (Darren Dale) Date: Sat, 10 Dec 2011 09:56:38 -0800 (PST) Subject: WeakValueDict and threadsafety References: Message-ID: On Dec 10, 11:19?am, Duncan Booth wrote: > Darren Dale wrote: > > I'm concerned that this is not actually thread-safe. When I no longer > > hold strong references to an instance of data, at some point the > > garbage collector will kick in and remove that entry from my registry. > > How can I ensure the garbage collection process does not modify the > > registry while I'm holding the lock? > > You can't, but it shouldn't matter. > > So long as you have a strong reference in 'data' that particular object > will continue to exist. Other entries in 'registry' might disappear while > you are holding your lock but that shouldn't matter to you. > > What is concerning though is that you are using `id(data)` as the key and > then presumably storing that separately as your `oid` value. If the > lifetime of the value stored as `oid` exceeds the lifetime of the strong > references to `data` then you might get a new data value created with the > same id as some previous value. > > In other words I think there's a problem here, but nothing to do with the > lock. Thank you for the considered response. In reality, I am not using id(data). I took that from the example in the documentation at python.org in order to illustrate the basic approach, but it looks like I introduced an error in the code. It should read: def get_data(oid): with reglock: data = registry.get(oid, None) if data is None: data = make_data(oid) registry[oid] = data return data Does that look better? I am actually working on the h5py project (bindings to hdf5), and the oid is an hdf5 object identifier. make_data(oid) creates a proxy object that stores a strong reference to oid. My concern is that the garbage collector is modifying the dictionary underlying WeakValueDictionary at the same time that my multithreaded code is trying to access it, producing a race condition. This morning I wrote a synchronized version of WeakValueDictionary (actually implemented in cython): class _Registry: def __cinit__(self): def remove(wr, selfref=ref(self)): self = selfref() if self is not None: self._delitem(wr.key) self._remove = remove self._data = {} self._lock = FastRLock() __hash__ = None def __setitem__(self, key, val): with self._lock: self._data[key] = KeyedRef(val, self._remove, key) def _delitem(self, key): with self._lock: del self._data[key] def get(self, key, default=None): with self._lock: try: wr = self._data[key] except KeyError: return default else: o = wr() if o is None: return default else: return o Now that I am using this _Registry class instead of WeakValueDictionary, my test scripts and my actual program are no longer producing segfaults. From bahamutzero8825 at gmail.com Sat Dec 10 13:38:30 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 10 Dec 2011 12:38:30 -0600 Subject: Multiprocessing bug, is information ever omitted from a traceback? In-Reply-To: <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> Message-ID: <4EE3A726.6010201@gmail.com> On 12/10/2011 11:53 AM, John Ladasky wrote: > Why did you specify Python 2.7.2, instead of the 2.7.6 version that is > being offered to me by Ubuntu Software Center? Does it matter? There is no Python 2.7.6. I think you have it confused with the version 2.7.2-6. If I'm not mistaken, that appended 6 has to do with packaging and nothing at all to do with the software itself. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From dihedral88888 at googlemail.com Sat Dec 10 13:44:22 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 10 Dec 2011 10:44:22 -0800 (PST) Subject: WeakValueDict and threadsafety In-Reply-To: References: Message-ID: <30381924.754.1323542662530.JavaMail.geo-discussion-forums@prol32> On Sunday, December 11, 2011 1:56:38 AM UTC+8, Darren Dale wrote: > On Dec 10, 11:19?am, Duncan Booth > wrote: > > Darren Dale wrote: > > > I'm concerned that this is not actually thread-safe. When I no longer > > > hold strong references to an instance of data, at some point the > > > garbage collector will kick in and remove that entry from my registry. > > > How can I ensure the garbage collection process does not modify the > > > registry while I'm holding the lock? > > > > You can't, but it shouldn't matter. > > > > So long as you have a strong reference in 'data' that particular object > > will continue to exist. Other entries in 'registry' might disappear while > > you are holding your lock but that shouldn't matter to you. > > > > What is concerning though is that you are using `id(data)` as the key and > > then presumably storing that separately as your `oid` value. If the > > lifetime of the value stored as `oid` exceeds the lifetime of the strong > > references to `data` then you might get a new data value created with the > > same id as some previous value. > > > > In other words I think there's a problem here, but nothing to do with the > > lock. > > Thank you for the considered response. In reality, I am not using > id(data). I took that from the example in the documentation at > python.org in order to illustrate the basic approach, but it looks > like I introduced an error in the code. It should read: > > def get_data(oid): > with reglock: > data = registry.get(oid, None) > if data is None: > data = make_data(oid) > registry[oid] = data > return data > > Does that look better? I am actually working on the h5py project > (bindings to hdf5), and the oid is an hdf5 object identifier. > make_data(oid) creates a proxy object that stores a strong reference > to oid. > > My concern is that the garbage collector is modifying the dictionary > underlying WeakValueDictionary at the same time that my multithreaded > code is trying to access it, producing a race condition. This morning > I wrote a synchronized version of WeakValueDictionary (actually > implemented in cython): > > class _Registry: > > def __cinit__(self): > def remove(wr, selfref=ref(self)): > self = selfref() > if self is not None: > self._delitem(wr.key) > self._remove = remove > self._data = {} > self._lock = FastRLock() > > __hash__ = None > > def __setitem__(self, key, val): > with self._lock: > self._data[key] = KeyedRef(val, self._remove, key) > > def _delitem(self, key): > with self._lock: > del self._data[key] > > def get(self, key, default=None): > with self._lock: > try: > wr = self._data[key] > except KeyError: > return default > else: > o = wr() > if o is None: > return default > else: > return o > > Now that I am using this _Registry class instead of > WeakValueDictionary, my test scripts and my actual program are no > longer producing segfaults. I'll prefer to get iterators and iterables that can accept a global signal called a clock to replace these CS mess. From ladasky at my-deja.com Sat Dec 10 14:02:35 2011 From: ladasky at my-deja.com (John Ladasky) Date: Sat, 10 Dec 2011 11:02:35 -0800 (PST) Subject: Multiprocessing bug, is information ever omitted from a traceback? References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> Message-ID: On Dec 10, 10:38?am, Andrew Berg wrote: > On 12/10/2011 11:53 AM, John Ladasky wrote:> Why did you specify Python 2.7.2, instead of the 2.7.6 version that is > > being offered to me by Ubuntu Software Center? ?Does it matter? > > There is no Python 2.7.6. I think you have it confused with the version > 2.7.2-6. If I'm not mistaken, that appended 6 has to do with packaging > and nothing at all to do with the software itself. > > -- > CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 How annoying, here's how Ubuntu Software Center described it: "Version: 2.7-6 (python2.7)" And here it is in the Synaptic Package Manager: "package: python2.7-minimal, installed version: 2.7-6" At first I considered that this truncated name might be the consequence of a Linux bug. But looking up and down my list of installed software in both Ubuntu Software Center and Synaptic, I can find version names which extend arbitrarily, such as my wxPython installation, for which the version number reads "2.8.11.0-0ubuntu4.1". Now my hypothesis is that someone manually enters the revision numbers into the Linux database, and they made a typo. From rosuav at gmail.com Sat Dec 10 14:02:48 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Dec 2011 06:02:48 +1100 Subject: Multiprocessing bug, is information ever omitted from a traceback? In-Reply-To: <4EE3A726.6010201@gmail.com> References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> <4EE3A726.6010201@gmail.com> Message-ID: On Sun, Dec 11, 2011 at 5:38 AM, Andrew Berg wrote: > On 12/10/2011 11:53 AM, John Ladasky wrote: >> Why did you specify Python 2.7.2, instead of the 2.7.6 version that is >> being offered to me by Ubuntu Software Center? ?Does it matter? > There is no Python 2.7.6. I think you have it confused with the version > 2.7.2-6. If I'm not mistaken, that appended 6 has to do with packaging > and nothing at all to do with the software itself. On 12/9/2011 6:14 PM, John Ladasky wrote: > I'm programming in Python 2.6 on Ubuntu Linux 10.10, if it matters. As with my Ubuntu 10.10 - it's version 2.6.6. (I also altinstalled 3.3 built straight from Mercurial, which is approximately as new a Python as one can have, but that doesn't count.) There's a few differences between 2.6 and 2.7; not usually enough to be concerned about in daily use, but when dealing with weird issues, it helps to have the latest release. ChrisA From duncan.booth at invalid.invalid Sat Dec 10 14:09:50 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 10 Dec 2011 19:09:50 GMT Subject: WeakValueDict and threadsafety References: Message-ID: Darren Dale wrote: > On Dec 10, 11:19?am, Duncan Booth > wrote: >> Darren Dale wrote: > def get_data(oid): > with reglock: > data = registry.get(oid, None) > if data is None: > data = make_data(oid) > registry[oid] = data > return data > > Does that look better? I am actually working on the h5py project > (bindings to hdf5), and the oid is an hdf5 object identifier. > make_data(oid) creates a proxy object that stores a strong reference > to oid. Yes, that looks better. > > Now that I am using this _Registry class instead of > WeakValueDictionary, my test scripts and my actual program are no > longer producing segfaults. > I think that so far as multi-thread race conditions are concerned Python usually tries to guarantee that you won't get seg faults. So if you were getting seg faults my guess would be that either you've found a bug in the WeakValueDictionary implementation or you've got a bug in some of your code outside Python. For example if your proxy object has a __del__ method to clean up the object it is proxying then you could be creating a new object with the same oid as one that is in the process of being destroyed (the object disappears from the WeakValueDictionary before the __del__ method is actually called). Without knowing anything about HDF5 I don't know if that's a problem but I could imagine you could end up creating a new proxy object that references something in the HDF5 library which you then destroy as part of cleaning up a previous incarnation of the object but continue to access through the new proxy. -- Duncan Booth http://kupuguy.blogspot.com From dsdale24 at gmail.com Sat Dec 10 15:14:23 2011 From: dsdale24 at gmail.com (Darren Dale) Date: Sat, 10 Dec 2011 12:14:23 -0800 (PST) Subject: WeakValueDict and threadsafety References: Message-ID: <8d89b83e-59a2-428c-a067-ffe6660729e1@d17g2000yql.googlegroups.com> On Dec 10, 2:09?pm, Duncan Booth wrote: > Darren Dale wrote: > > On Dec 10, 11:19?am, Duncan Booth > > wrote: > >> Darren Dale wrote: > > def get_data(oid): > > ? ? with reglock: > > ? ? ? ? data = registry.get(oid, None) > > ? ? ? ? if data is None: > > ? ? ? ? ? ? data = make_data(oid) > > ? ? ? ? ? ? registry[oid] = data > > ? ? return data > > > Does that look better? I am actually working on the h5py project > > (bindings to hdf5), and the oid is an hdf5 object identifier. > > make_data(oid) creates a proxy object that stores a strong reference > > to oid. > > Yes, that looks better. > > > > > Now that I am using this _Registry class instead of > > WeakValueDictionary, my test scripts and my actual program are no > > longer producing segfaults. > > I think that so far as multi-thread race conditions are concerned Python > usually tries to guarantee that you won't get seg faults. So if you were > getting seg faults my guess would be that either you've found a bug in the > WeakValueDictionary implementation or you've got a bug in some of your code > outside Python. Have you seen Alex Martelli's answer at http://stackoverflow.com/questions/3358770/python-dictionary-is-thread-safe ? The way I read that, it seems pretty clear that deleting items from a dict can lead to crashes in threaded code. (Well, he says as long as you don't performing an assignment or a deletion in threaded code, there may be issues, but at least it shouldn't crash.) > For example if your proxy object has a __del__ method to clean up the > object it is proxying then you could be creating a new object with the same > oid as one that is in the process of being destroyed (the object disappears > from the WeakValueDictionary before the __del__ method is actually called). > > Without knowing anything about HDF5 I don't know if that's a problem but I > could imagine you could end up creating a new proxy object that references > something in the HDF5 library which you then destroy as part of cleaning up > a previous incarnation of the object but continue to access through the new > proxy. We started having problems when HDF5 began recycling oids as soon as their reference count went to zero, which was why we began using IDProxy and the registry. The IDProxy implementation below does have a __dealloc__ method, which we use to decrease the HDF5's internal reference count to the oid. Adding these proxies and registry dealt with the issue of creating a new proxy that references an old oid (even in non-threaded code), but it created a rare (though common enough) segfault in multithreaded code. This synchronized registry is the best I have been able to do, and it seems to address the problem. Could you suggest another approach? cdef IDProxy getproxy(hid_t oid): # Retrieve an IDProxy object appropriate for the given object identifier cdef IDProxy proxy proxy = registry.get(oid, None) if proxy is None: proxy = IDProxy(oid) registry[oid] = proxy return proxy cdef class IDProxy: property valid: def __get__(self): return H5Iget_type(self.id) > 0 def __cinit__(self, id): self.id = id self.locked = 0 def __dealloc__(self): if self.id > 0 and (not self.locked) and H5Iget_type(self.id) > 0 \ and H5Iget_type(self.id) != H5I_FILE: H5Idec_ref(self.id) From roy at panix.com Sat Dec 10 15:47:54 2011 From: roy at panix.com (Roy Smith) Date: Sat, 10 Dec 2011 15:47:54 -0500 Subject: Overriding a global Message-ID: I've got a code pattern I use a lot. In each module, I create a logger for the entire module and log to it all over: logger = logging.getLogger('my.module.name') class Foo: def function(self): logger.debug('stuff') logger.debug('other stuff') and so on. This works, but every once in a while I decide that a particular function needs a more specific logger, so I can adjust the logging level for that function independent of the rest of the module. What I really want to do is: def function(self): logger = logger.getChild('function') logger.debug('stuff') logger.debug('other stuff') which lets me not have to change any lines of code other than inserting the one to redefine logger. Unfortunately, that's not legal Python (it leads to "UnboundLocalError: local variable 'logger' referenced before assignment"). Any ideas on the best way to implement this? From python at mrabarnett.plus.com Sat Dec 10 16:07:18 2011 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 10 Dec 2011 21:07:18 +0000 Subject: Overriding a global In-Reply-To: References: Message-ID: <4EE3CA06.50503@mrabarnett.plus.com> On 10/12/2011 20:47, Roy Smith wrote: > I've got a code pattern I use a lot. In each module, I create a logger > for the entire module and log to it all over: > > logger = logging.getLogger('my.module.name') > > class Foo: > def function(self): > logger.debug('stuff') > logger.debug('other stuff') > > and so on. This works, but every once in a while I decide that a > particular function needs a more specific logger, so I can adjust the > logging level for that function independent of the rest of the module. > What I really want to do is: > > def function(self): > logger = logger.getChild('function') > logger.debug('stuff') > logger.debug('other stuff') > > which lets me not have to change any lines of code other than inserting > the one to redefine logger. Unfortunately, that's not legal Python (it > leads to "UnboundLocalError: local variable 'logger' referenced before > assignment"). > > Any ideas on the best way to implement this? You could use a different name: def function(self): logger2 = logger.getChild('function') logger2.debug('stuff') logger2.debug('other stuff') or use 'globals': def function(self): logger = globals()['logger'].getChild('function') logger.debug('stuff') logger.debug('other stuff') From roy at panix.com Sat Dec 10 16:10:53 2011 From: roy at panix.com (Roy Smith) Date: Sat, 10 Dec 2011 16:10:53 -0500 Subject: Overriding a global References: Message-ID: MRAB wrote: > or use 'globals': > > def function(self): > logger = globals()['logger'].getChild('function') > logger.debug('stuff') > logger.debug('other stuff') Ah-ha! That's precisely what I was looking for. Much appreciated. From lie.1296 at gmail.com Sat Dec 10 18:41:59 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 11 Dec 2011 10:41:59 +1100 Subject: Misleading error message of the day In-Reply-To: <36bf64b8-d27f-4f9d-9cf7-c425d4b1d214@q27g2000prh.googlegroups.com> References: <27000242.1109.1323358430515.JavaMail.geo-discussion-forums@yqny18> <4EE114E2.6070200@sequans.com> <4EE11A69.9060207@stoneleaf.us> <36bf64b8-d27f-4f9d-9cf7-c425d4b1d214@q27g2000prh.googlegroups.com> Message-ID: On 12/09/2011 03:57 PM, alex23 wrote: > On Dec 9, 11:46 am, Lie Ryan wrote: >> perhaps the one that talks about `a, a.foo = 1, 2` blowing up? > > Are you sure you're not confusing this with the recent thread on 'x = > x.thing = 1'? Ah, yes I do From tjreedy at udel.edu Sat Dec 10 18:57:00 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Dec 2011 18:57:00 -0500 Subject: Multiprocessing bug, is information ever omitted from a traceback? In-Reply-To: References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> <41081049-1047-41ca-b7c1-5e5c3c00e94b@f33g2000prh.googlegroups.com> <4EE3A726.6010201@gmail.com> Message-ID: On 12/10/2011 2:02 PM, Chris Angelico wrote: > There's a few differences between 2.6 and 2.7; not usually enough to > be concerned about in daily use, but when dealing with weird issues, > it helps to have the latest release. There are 2 issues. First, 2.7.2 has perhaps a couple hundred bug fixes since 2.7.0/2.6.6 were released. So it is possible that this particular problem was fixed, or at least changed. Second (leaving security issues aside), a Py2 bug fix will only be applied to the post-2.7.2 tip and only after the bug has been demonstrated to exist in the post-2.7.2 tip. About the request for minimal code exhibiting the problem: each bug exposes a hole in the test suite. We try to add a new test case with each bug fix so fixed bugs stay fixed. So code examples not only show that the bug is in the latest version, but also serve as the basis for new tests. -- Terry Jan Reedy From lie.1296 at gmail.com Sat Dec 10 18:58:43 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 11 Dec 2011 10:58:43 +1100 Subject: order independent hash? In-Reply-To: <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/09/2011 10:27 PM, Steven D'Aprano wrote: > On Thu, 08 Dec 2011 10:30:01 +0100, Hrvoje Niksic wrote: > >> In a language like Python, the difference between O(1) and O(log n) is >> not the primary reason why programmers use dict; they use it because >> it's built-in, efficient compared to alternatives, and convenient to >> use. If Python dict had been originally implemented as a tree, I'm sure >> it would be just as popular. > > Except for people who needed dicts with tens of millions of items. who should be using a proper DBMS in any case. From tjreedy at udel.edu Sat Dec 10 19:14:16 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Dec 2011 19:14:16 -0500 Subject: Overriding a global In-Reply-To: References: Message-ID: On 12/10/2011 3:47 PM, Roy Smith wrote: > > What I really want to do is: > > def function(self): Add a global statement to rebind a global name: global logger > logger = logger.getChild('function') > logger.debug('stuff') > logger.debug('other stuff') > > which lets me not have to change any lines of code other than inserting > the one to redefine logger. Unfortunately, that's not legal Python (it > leads to "UnboundLocalError: local variable 'logger' referenced before > assignment"). > > Any ideas on the best way to implement this? -- Terry Jan Reedy From rosuav at gmail.com Sat Dec 10 19:17:48 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Dec 2011 11:17:48 +1100 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 11, 2011 at 10:58 AM, Lie Ryan wrote: > On 12/09/2011 10:27 PM, Steven D'Aprano wrote: >> Except for people who needed dicts with tens of millions of items. > > who should be using a proper DBMS in any case. Not necessarily. "Database" usually implies disk-based and relational, features that may well be quite superfluous; and a pure-memory database with no relational facilities... is basically a dict. So why not use one? ChrisA From tjreedy at udel.edu Sat Dec 10 19:19:27 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 10 Dec 2011 19:19:27 -0500 Subject: Overriding a global In-Reply-To: References: Message-ID: On 12/10/2011 7:14 PM, Terry Reedy wrote: > On 12/10/2011 3:47 PM, Roy Smith wrote: >> >> What I really want to do is: >> >> def function(self): > > Add a global statement to rebind a global name: > global logger But I see that that is not what you want to do, which is to override the global name just within the function while still accessing the global name. MRAB's solution does that nicely. >> logger = logger.getChild('function') >> logger.debug('stuff') >> logger.debug('other stuff') >> >> which lets me not have to change any lines of code other than inserting >> the one to redefine logger. Unfortunately, that's not legal Python (it >> leads to "UnboundLocalError: local variable 'logger' referenced before >> assignment"). >> >> Any ideas on the best way to implement this? > > -- Terry Jan Reedy From lie.1296 at gmail.com Sat Dec 10 22:04:55 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 11 Dec 2011 14:04:55 +1100 Subject: order independent hash? In-Reply-To: References: <30715729.411.1322753732534.JavaMail.geo-discussion-forums@pret21> <4ED7A2CE.6070306@davea.name> <6366868.86.1322800180523.JavaMail.geo-discussion-forums@pret21> <87k46fb8pg.fsf@xemacs.org> <87aa78bfvv.fsf@xemacs.org> <87y5un9zs6.fsf@xemacs.org> <4ee1f093$0$29977$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/11/2011 11:17 AM, Chris Angelico wrote: > On Sun, Dec 11, 2011 at 10:58 AM, Lie Ryan wrote: >> On 12/09/2011 10:27 PM, Steven D'Aprano wrote: >>> Except for people who needed dicts with tens of millions of items. >> >> who should be using a proper DBMS in any case. > > Not necessarily. "Database" usually implies disk-based and relational, > features that may well be quite superfluous; and a pure-memory > database with no relational facilities... is basically a dict. So why > not use one? It is very unlikely you'd have millions of items in a dict and you're not planning to do any data processing at all. In any case, there are very few use cases which requires the use of a dict with millions of items that wouldn't be better served by a proper database. From nobody at nowhere.com Sun Dec 11 01:42:13 2011 From: nobody at nowhere.com (Nobody) Date: Sun, 11 Dec 2011 06:42:13 +0000 Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Message-ID: On Fri, 09 Dec 2011 01:55:28 -0800, Massi wrote: > Thank you all for your replies, first of all my Sum function was an > example simplifying what I have to do in my real funciton. In general > the D dictionary is complex, with a lot of keys, so I was searching > for a quick method to access all the variables in it without doing the > explicit creation: > > a, b, c = D['a'], D['b'], D['c'] > > and without using directly the D dictionary (boring...). If just you're trying to avoid getting a repetitive strain injury in your right-hand little finger from typing all the [''], you could turn the keys into object attributes, e.g.: class DictObject: def __init__(self, d): for key, value in d.iteritems(): setattr(self, key, value) ... o = DictObject(D) # use o.a, o.b, etc From __peter__ at web.de Sun Dec 11 03:14:43 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 11 Dec 2011 09:14:43 +0100 Subject: Overriding a global References: Message-ID: Roy Smith wrote: > I've got a code pattern I use a lot. In each module, I create a logger > for the entire module and log to it all over: > > logger = logging.getLogger('my.module.name') > > class Foo: > def function(self): > logger.debug('stuff') > logger.debug('other stuff') > > and so on. This works, but every once in a while I decide that a > particular function needs a more specific logger, so I can adjust the > logging level for that function independent of the rest of the module. > What I really want to do is: > > def function(self): > logger = logger.getChild('function') > logger.debug('stuff') > logger.debug('other stuff') > > which lets me not have to change any lines of code other than inserting > the one to redefine logger. Unfortunately, that's not legal Python (it > leads to "UnboundLocalError: local variable 'logger' referenced before > assignment"). > > Any ideas on the best way to implement this? def function(self, logger=logger.getChild("function")): logger.debug("stuff") But the "best way" is to use another name. From hoogendoorn.eelco at gmail.com Sun Dec 11 05:49:23 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Sun, 11 Dec 2011 11:49:23 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE48AB3.7070807@gmail.com> Throwing an idea for a PEP out there: It strikes me that the def func(*args, **kwargs) syntax is rather unpytonic. It certainly did not have that 'for line in file' pythonic obviousness for me as a beginner. Plus, asterikses are impossible to google for, so finding out what exactly they do more or less forces you to write a forum post about it. A more readable form occurred to me, which also happens to be more flexible, and which I think is fully compatible with the syntax of the language: def func(parg, list(args), dict(kwargs)) Perhaps this is considered abuse of notation; dict(kwargs) already has a meaning rather different from the one we try to give it here; but then again the context (being inside a function definition) is unique and easily recognizable. An added advantage would be the possibility of using subclasses of dict and list as well; imagine how much more beautiful a lot of code would be if one could say def func(attrdict(kwargs)) Problems im still wrestling with: the same syntax could not be used when calling a function; that lack of symmetry would make things more confusing, not less. Thoughts? From rosuav at gmail.com Sun Dec 11 06:15:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 11 Dec 2011 22:15:41 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE48AB3.7070807@gmail.com> References: <4EE48AB3.7070807@gmail.com> Message-ID: On Sun, Dec 11, 2011 at 9:49 PM, Eelco Hoogendoorn wrote: > Problems im still wrestling with: the same syntax could not be used when > calling a function; that lack of symmetry would make things more confusing, > not less. That symmetry is a large factor, IMHO. I can write a wrapper function like this: def fixedargs(x,y,z): return wrappedfunc(x,y,z) Or like this: def anyargs(*args,**kwargs): return wrappedfunc(*args,**kwargs) Either way, it's a perfect parallel, and that's very useful for noticing errors. With a keyworded syntax, that's going to be a lot harder. Another issue: You suggest being able to use "attrdict" or some other dict subclass. This means that, rather than being a language construct, this will involve a name lookup. And what happens if you have a class that subclasses both list and dict? Will it get the positional args, the keyword args, or both? Laudable notion, but I'm not sure that it'll actually work in practice. ChrisA From detlev at die-offenbachs.de Sun Dec 11 06:20:20 2011 From: detlev at die-offenbachs.de (Detlev Offenbach) Date: Sun, 11 Dec 2011 12:20:20 +0100 Subject: How to get a correct entry in the menu for a Python application on Mac OS X References: <4EE13048.8090307@canterbury.ac.nz> Message-ID: I got it working by creating a symbolic link to the Python interpreter to be used in my application package and using this symbolic link to start the main Python script. Gregory Ewing wrote: > Detlev Offenbach wrote: >> I am fairly new to Mac OS X and would like to know, what I have to do >> to make my Python application show the correct name in the menu bar. >> What did I do so far. I created an application package containing the >> .plist file with correct entries and a shell script, that starts the >> correct Python interpreter with the the main script. > > I don't think that will work, because the executable that > your shell script is starting is in an app bundle of its > own, and MacOSX will be using the plist from that bundle, > which just has the generic "Python" name in it. > > There are a couple of things you could do: > > 1) Use py2app to create your app bundle. It does the > right things -- not sure exactly what, but it works. > > 2) Hack things at run time. I use the following PyObjC > code in PyGUI to set the application name: > > from Foundation import NSBundle > > ns_bundle = NSBundle.mainBundle() > ns_info = ns_bundle.localizedInfoDictionary() > if not ns_info: > ns_info = ns_bundle.infoDictionary() > ns_info['CFBundleName'] = my_application_name > -- Detlev Offenbach detlev at die-offenbachs.de From duncan.booth at invalid.invalid Sun Dec 11 07:39:44 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 11 Dec 2011 12:39:44 GMT Subject: Verbose and flexible args and kwargs syntax References: <4EE48AB3.7070807@gmail.com> Message-ID: Chris Angelico wrote: > Either way, it's a perfect parallel, and that's very useful for > noticing errors. > > With a keyworded syntax, that's going to be a lot harder. If it used keywords then you could keep symmetry quite easily: def anyargs(arglist args, argdict kwargs): return wrappedfunc(arglist args, argdict kwargs) and you would have the advantage of two new keywords that people could actually search on. > > Another issue: You suggest being able to use "attrdict" or some other > dict subclass. This means that, rather than being a language > construct, this will involve a name lookup. And what happens if you > have a class that subclasses both list and dict? Will it get the > positional args, the keyword args, or both? Irrelevant, you can't subclass both list and dict: >>> class C(list, dict): pass Traceback (most recent call last): File "", line 1, in class C(list, dict): pass TypeError: multiple bases have instance lay-out conflict -- Duncan Booth http://kupuguy.blogspot.com From rosuav at gmail.com Sun Dec 11 08:09:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 12 Dec 2011 00:09:56 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE48AB3.7070807@gmail.com> Message-ID: On Sun, Dec 11, 2011 at 11:39 PM, Duncan Booth wrote: > Chris Angelico wrote: > If it used keywords then you could keep symmetry quite easily: > > ? ?def anyargs(arglist args, argdict kwargs): > ? ? ? ?return wrappedfunc(arglist args, argdict kwargs) > > and you would have the advantage of two new keywords that people could > actually search on. Yes, that's just a strict keywordification of the * and ** symbols. The same argument could be made for eliminating the standard algebraic + operator and replacing it with a keyword "__add__". I don't think that's worthwhile. The OP suggested using 'dict' and 'list' themselves as the keywords, thus allowing the use of subclasses. This would make it unsearchable, or else rather verbose: def anyargs(pack list args, pack dict kwargs): return wrappedfunc(pack list args, pack dict kwargs) With this syntax, what happens if you muck up list/dict? Or alternatively, the briefer syntax: def anyargs(pack list args, pack dict kwargs): return wrappedfunc(pack args, pack kwargs) which breaks the symmetry, and doesn't say which one you're doing - for instance, if you only use one out of list and dict args, it would make sense to have a variable "options" or "args" that gets unpacked to the function's arguments - and there's no way to see whether it's going to be keyword or positional. The verbose syntax has a lot going for it, but it's rather verbose. Why say "pack list args" when you can just say "*args"? Compare initializer syntax between Python and PHP: foo = ["asdf", "qwer", {1:2, 3:4}] $foo = array("asdf", "qwer", array(1=>2, 3=>4)) Is it more Pythonic to use explicitly-named types, or to have simple notation that's clear and easy to read? Or is this a matter for personal preference? >> Another issue: You suggest being able to use "attrdict" or some other >> dict subclass. This means that, rather than being a language >> construct, this will involve a name lookup. And what happens if you >> have a class that subclasses both list and dict? Will it get the >> positional args, the keyword args, or both? > > Irrelevant, you can't subclass both list and dict: > TypeError: multiple bases have instance lay-out conflict Ah. Curious. I've not done much with multiple inheritance in Python (come to think of it, I don't recall when I last used MI in _any_ language). In any case, there's still the potential unclarity as to _which_ of dict and list is the one that's been inherited, which requires a run-time lookup to solve. ChrisA From lists at cheimes.de Sun Dec 11 08:14:13 2011 From: lists at cheimes.de (Christian Heimes) Date: Sun, 11 Dec 2011 14:14:13 +0100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE48AB3.7070807@gmail.com> Message-ID: Am 11.12.2011 13:39, schrieb Duncan Booth: >> Another issue: You suggest being able to use "attrdict" or some other >> dict subclass. This means that, rather than being a language >> construct, this will involve a name lookup. And what happens if you >> have a class that subclasses both list and dict? Will it get the >> positional args, the keyword args, or both? > > Irrelevant, you can't subclass both list and dict: But you can fake it with __instancecheck__ and __subclasscheck__ methods. Christian From hoogendoorn.eelco at gmail.com Sun Dec 11 09:44:37 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Sun, 11 Dec 2011 15:44:37 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE4C1D5.8050203@gmail.com> > Yes, that's just a strict keywordification of the * and ** symbols. > The same argument could be made for eliminating the standard algebraic > + operator and replacing it with a keyword "__add__". I don't think > that's worthwhile. Well, its not quite the same in the sense that algebraic operators are essentially part of 'natural language', or at least extremely widely adopted. They have earned their own special symbols. Argument packing/unpacking is a very specific thing; a small corner of a particular programming language. However, as seen in the light of python 3 head-tail syntax, perhaps the above is not quite true, and one could argue that packing/unpacking of collections is indeed a quite general concept, deserving of its own symbols. Breaking uniformity with that use case would also be a bad thing; ideally, a verbose alternative to all occurances of collection packing/unpacking would be available. That said, a more verbose and flexible syntax would be desirable there too; as of now, the tail is always a list. I havnt read the discussions leading up to those design decisions, but that seems like a compromise to me; something like head,tuple(tail) = someiterable would be preferrable there too, id say From hoogendoorn.eelco at gmail.com Sun Dec 11 09:58:18 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Sun, 11 Dec 2011 15:58:18 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE4C50A.3010205@gmail.com> As for syntax; what about coopting/integrating with the function annotation syntax? so: def func(args: list, kwargs: attrdict) and correspondingly in the function call? a, b:tuple = someiterable? I guess a rule that interprets every function argument annotated as a subclass of list or dict as a special case would severely restrict its intended use though... From johann.spies at gmail.com Sun Dec 11 13:44:38 2011 From: johann.spies at gmail.com (Johann Spies) Date: Sun, 11 Dec 2011 20:44:38 +0200 Subject: Using python on OS X Lion Message-ID: I have recently upgraded my Snow Leopard to Lion and now I am having all problems with python on my system e.g.: I have install PIL with easy_install but when I do import PIL Python cannot find the pil library. Installing psycopg2 in the same way was successful and I can use psycopg2 without a problem. Further, the new version of Python is 2.7.1 in Lion. But when I just run 'python' it calls up 2.6.5 - even after I have moved the application python2.6 to Trash. I see several guidelines for using python on OS X: some using macports, others homebrew and others 'pip' and 'virtualenv' . I come from a Linux background where a proper packaging system like apt (on Debian) prevent these types of problems. So my question: What is the recommended way of setting up a python environment in OS X? And how do I get rid of legacy python libraries on my system? Regards Johann -- Because experiencing your loyal love is better than life itself, my lips will praise you. (Psalm 63:3) -------------- next part -------------- An HTML attachment was scrubbed... URL: From johann.spies at gmail.com Sun Dec 11 13:57:38 2011 From: johann.spies at gmail.com (Johann Spies) Date: Sun, 11 Dec 2011 20:57:38 +0200 Subject: [OT] Book authoring In-Reply-To: <10973.1323447520@alphaville.dokosmarshall.org> References: <14110199.662.1323401134730.JavaMail.geo-discussion-forums@vbbeq1> <9925.1323445407@alphaville.dokosmarshall.org> <10973.1323447520@alphaville.dokosmarshall.org> Message-ID: Have a look at Markdown + pandoc. Regards Johann -- Because experiencing your loyal love is better than life itself, my lips will praise you. (Psalm 63:3) -------------- next part -------------- An HTML attachment was scrubbed... URL: From nad at acm.org Sun Dec 11 16:00:37 2011 From: nad at acm.org (Ned Deily) Date: Sun, 11 Dec 2011 13:00:37 -0800 Subject: Using python on OS X Lion References: Message-ID: In article , Johann Spies wrote: > I have recently upgraded my Snow Leopard to Lion and now I am having all > problems with python on my system e.g.: > > I have install PIL with easy_install but when I do > > import PIL > > Python cannot find the pil library. Installing psycopg2 in the same way was > successful and I can use psycopg2 without a problem. > > Further, the new version of Python is 2.7.1 in Lion. But when I just run > 'python' it calls up 2.6.5 - even after I have moved the application > python2.6 to Trash. > > I see several guidelines for using python on OS X: some using macports, > others homebrew and others 'pip' and 'virtualenv' . I come from a Linux > background where a proper packaging system like apt (on Debian) prevent > these types of problems. > > So my question: What is the recommended way of setting up a python > environment in OS X? > > And how do I get rid of legacy python libraries on my system? First, determine which python is being loaded. In OS X 10.7 (Lion), Apple ships 2.7.1, 2.6.7, and 2.5.6 version of Python (all in /usr/bin) so the 2.6.5 is coming from elsewhere. If you previously installed a Python from a python.org installer, the framework will be in /Library/Frameworks/Python.framework (and *not* /System/Library/Frameworks, which is where Apple's Pythons are and should not be modified). The python.org installers, by default, may modify your shell profiles (i.e. .bash_profile, .profile, et al) to prepend its framework bin directory to your shell PATH. Look for something like this: # Setting PATH for MacPython 2.6 # The orginal version is saved in .profile.pysave PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}" export PATH There are more details here http://bugs.python.org/issue7107 Also understand that if you use easy_install or pip, each Python instance needs to have its own version of either one. Apple supplies versions of easy_install in /usr/bin for its Pythons. If you install another version of Python, you'll need to install another easy_install and pip for it as well. -- Ned Deily, nad at acm.org From gelonida at gmail.com Sun Dec 11 16:27:23 2011 From: gelonida at gmail.com (Gelonida N) Date: Sun, 11 Dec 2011 22:27:23 +0100 Subject: unittest. customizing tstloaders / discover() Message-ID: Hi, I'd like to have a custom test loder, which will filter out certain tests or which just searches tests in certain directories. I'd like to use regular expresions as include / exclude rules and I would like to have another filter function, which would check for the existence of certain metavariabels in test suite files WHat would be the best starting point for such an example. Thanks a lot in advance for any info / pointer /tutorial . . .. From steve+comp.lang.python at pearwood.info Sun Dec 11 17:19:42 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Dec 2011 22:19:42 GMT Subject: Verbose and flexible args and kwargs syntax References: Message-ID: <4ee52c7e$0$11091$c3e8da3@news.astraweb.com> On Sun, 11 Dec 2011 11:49:23 +0100, Eelco Hoogendoorn wrote: > Throwing an idea for a PEP out there: > > It strikes me that the def func(*args, **kwargs) syntax is rather > unpytonic. It certainly did not have that 'for line in file' pythonic > obviousness for me as a beginner. Plus, asterikses are impossible to > google for, so finding out what exactly they do more or less forces you > to write a forum post about it. No more so than any other form of punctuation. Plus and minus + - may be so common that just about everyone knows it, but how about | == @ % and even . (dot)? None of these things will be obvious to newbies who have never programmed before. Oh well. Some things you just have to learn. As for impossibility of googling for asterisks, it might help if you spell it correctly http://duckduckgo.com/?q=python+asterisk Fourth hit says: *vargs puts all left-over non-keyword arguments into a tuple called vargs. **kargs puts all left-over keyword arguments into a dictionary called kargs. Even better, this works amazingly well: http://duckduckgo.com/?q=python+* Or you can Read The Fine Manual, which has a section for special symbols: http://docs.python.org/genindex-Symbols.html which is moderately easy to discover from the main page: Main page => Documentation => Browse Current Documentation => General Index => Symbols. Not ideal, but not too bad. > A more readable form occurred to me, which also happens to be more > flexible, and which I think is fully compatible with the syntax of the > language: > > def func(parg, list(args), dict(kwargs)) > > Perhaps this is considered abuse of notation; dict(kwargs) already has a > meaning rather different from the one we try to give it here; but then > again the context (being inside a function definition) is unique and > easily recognizable. I consider that excessively verbose as well as misleading. It is excessively verbose for the same reason as: let y equal x plus 1 would be considered excessively verbose. I'm very fond of some languages with verbose syntax, like Hypertalk and OpenXION where you might say "add 1 to x", but I don't want Python to become them. It's a judgement call as to where a language divides "cryptic punctuation line noise" and "useful short operators", and in my opinion * and ** tuple and dict unpacking fall strongly on the "useful short operators" side. Your opinion may differ, but luckily for me, the BDFL agrees with me :) It is also misleading because args are not collected into a list, but into a tuple. Worse, it suggests that one should be able to generalise to something like this: def func(parg, str(args), int(kwargs), my_func(more_args)): which is incoherent. And lastly, this would require either promoting list and dict to proper keywords, which will not happen, or worse, making them semi-keywords (treated as keywords in some contexts, but not in others), which also will not happen. > Problems im still wrestling with: the same syntax could not be used when > calling a function; that lack of symmetry would make things more > confusing, not less. Precisely. > Thoughts? People learn to spell strings "like this" instead of str(like this), and to use # for comments instead of REMARK or REM. It's not that much of a burden on them to learn to use * and ** -- Steven From tinnews at isbd.co.uk Sun Dec 11 17:28:19 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Sun, 11 Dec 2011 22:28:19 +0000 Subject: Documentation for python-evolution - where? Message-ID: <3dnfr8-h6a.ln1@chris.zbmc.eu> I'm trying to use the python evolution (as in Gnome Evolution) module but I can't find any documetation beyond the odd trivial example and the API documentation at http://doc.conduit-project.org/evolution-python/ (or similar places presumably). The trouble with the API documentation is that it tells me that evolution.ebook has three functions:- get_self_contact(...) list_addressbooks(...) open_addressbook(...) but then there is no documentation at all that I can find which tells me what the functions return and the functions/methods that can be called on the returned data. I know that open_addressbook() returns an address book (surprise!) but then, except for what I can glean from the examples I can find, there doesn't seem to any documentation on what methods I can call on the returned address book to extract data from it. Can anyone point me at anything with a bit of detail about the evolution module? -- Chris Green From tjreedy at udel.edu Sun Dec 11 18:26:39 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 11 Dec 2011 18:26:39 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE48AB3.7070807@gmail.com> References: <4EE48AB3.7070807@gmail.com> Message-ID: On 12/11/2011 5:49 AM, Eelco Hoogendoorn wrote: > Plus, asterikses are impossible to google for, so finding out what exactly > they do more or less forces you to write a forum post about it. There are other means of finding information than Google. Really. 1. A couple of years ago, I wrote a very complete list of symbol syntax uses in Python 3. You can get is at https://xploro.googlecode.com/files/PySymbols.html Unfortunately, searching for 'python syntax symbols' does not show this on the first few pages. 2. The python docs have an index. While the Symbols page does not have most of the entries in the above doc, it does have '* in function calls' and '** in function calls'. 3. The meaning of * and ** in function calls is explained, surprise, in the reference manual section (5.3.4 for Py3) on function calls, which is in the chapter on expressions. -- Terry Jan Reedy From bachth at uni-mainz.de Sun Dec 11 18:27:19 2011 From: bachth at uni-mainz.de (Thomas Bach) Date: Mon, 12 Dec 2011 00:27:19 +0100 Subject: unittest. customizing tstloaders / discover() References: Message-ID: <87y5uiu1t4.fsf@jubold.box> Gelonida N writes: > I'd like to use regular expresions as include / exclude rules > and I would like to have another filter function, which would check for > the existence of certain metavariabels in test suite files Did you have a look at nose? I'm using it and it supports include/exclude rules via RE and lets you select directories to run tests from. I'm not sure about the meta-variable thing, but it supports plug ins that could do the trick? From hoogendoorn.eelco at gmail.com Sun Dec 11 18:44:38 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 00:44:38 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE54066.3020901@gmail.com> > No more so than any other form of punctuation. Plus and minus + - may be > so common that just about everyone knows it, but how about | == @ % and > even . (dot)? None of these things will be obvious to newbies who have > never programmed before. Oh well. > Some things you just have to learn. Yes, some things you just have to learn. Nonetheless, I strongly prefer explicit logical operators over |, would much rather have 'equals' instead of ==, which is stylistic in line with 'is' and explicitly distinguishes between equality and identity comparisons. As for %; it is entirely unclear to me why that obscure operation ever got its own one-character symbol. Ill take 'mod', or even better, 'modulus' any day of the week. The dot is clearly quantitatively in another realm here. 90% of typical python code is attribute accesses. The dot is entirely unambigious and cannot be mistaken for anything else. It reads like a book. > It's a judgement call as to where a language divides "cryptic punctuation > line noise" and "useful short operators", and in my opinion * and ** tuple > and dict unpacking fall strongly on the "useful short operators" side. > Your opinion may differ, but luckily for me, the BDFL agrees with me :) I also agree that it is a value judgement as to which constructs get their own cryptic symbols and which do not, but the are some reasonable guidelines we should be able to agree upon. Obscure operations should not reserve any of the few available characters. Furthermore, the language should not just be formally consistent, but also easy to grasp at a glance, without deciphering subtle semantics of a blurb of weird characters. (some programming languages obviously disagree, but python, as far as I am allowed to speak for it, does not). And most importantly, if you cant come up with a terse syntax that does everything you want to do, the terse syntax should at best be an alternative to the verbose one. > It is also misleading because args are not collected into a list, but > into a tuple. In case you wanted a tuple youd write tuple(args), obviously. Exactly that added flexibility is half of my case in favor. Why shouldnt it be a list when I want it to? > Worse, it suggests that one should be able to generalise to > something like this: > def func(parg, str(args), int(kwargs), my_func(more_args)): > which is incoherent. Sorry, but I dont get this point at all. Does ** suggests one should be able to generalize to ***? The rules are the rules. The real questions, in my mind, are: 1) How useful is this added flexibility? Not insanely, but I can see it making a lot of code significantly more clean. And: 2) How fundamental is collection packing/unpacking? One can easily argue that it is indeed quite fundamental and therefore deserves its own terse symbols, I feel. However, if more flexibility is indeed deemed desirable, such terse syntax quickly gives way to a more verbose one. Can you come up with some terse symbols that will be able to express all of the below and dont make you wish you hadnt rather typed out the names? head, tuple(tail) = iterable head, list(tail) = iterable head, str(tail) = somestring head, generator(tail) = mygenerator And so on. If not, one has to admit that functionality is being sacrificed on the alter of terseness, which seems like a raw deal to me. From hoogendoorn.eelco at gmail.com Sun Dec 11 18:53:22 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 00:53:22 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE54272.8000005@gmail.com> > There are other means of finding information than Google. Really. This is really only a very minor point in my argument, so I dont want to put the focus on this. But really, no. Googling 'myprogramminglanguage conceptimtryingtofigureout' is my first, second and third line of defence. Yes, I could read the reference manual from top to bottom, and if I already knew about the existence of your article then im sure that would be a great help too. But the situation one finds oneself in is seeing two asterikses and not even being aware they are particular to function definitions/invocations. Im fluent in many different languages and well versed in CS concepts and jargon, but I had no idea what to search for when first trying to figure out the meaning of these symbols, and that does not happen often to me. From steve+comp.lang.python at pearwood.info Sun Dec 11 19:21:44 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Dec 2011 00:21:44 GMT Subject: What happened to module.__file__? Message-ID: <4ee54918$0$11091$c3e8da3@news.astraweb.com> I've just started using a Debian system, instead of the usual RedHat based systems I'm used to, and module.__file__ appears to have disappeared for some (but not all) modules. On Fedora: [steve at orac ~]$ python -E Python 2.6.2 (r262:71600, Aug 21 2009, 12:22:21) [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import math >>> math.__file__ '/usr/lib/python2.6/lib-dynload/mathmodule.so' and on Debian squeeze: steve at runes:~$ python -E Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import math >>> math.__file__ Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute '__file__' What's going on? -- Steven From rosuav at gmail.com Sun Dec 11 19:37:26 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 12 Dec 2011 11:37:26 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE54272.8000005@gmail.com> References: <4EE54272.8000005@gmail.com> Message-ID: On Mon, Dec 12, 2011 at 10:53 AM, Eelco Hoogendoorn wrote: > Googling 'myprogramminglanguage conceptimtryingtofigureout' is my first, > second and third line of defence. Yes, I could read the reference manual > from top to bottom, and if I already knew about the existence of your > article then im sure that would be a great help too. Python in particular has very poor internal search. I don't recommend searching the Python docs for anything that you can search for externally using Google, duckduckgo, etc, etc. ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 11 20:11:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Dec 2011 01:11:00 GMT Subject: Verbose and flexible args and kwargs syntax References: Message-ID: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> On Mon, 12 Dec 2011 00:44:38 +0100, Eelco Hoogendoorn wrote: >> No more so than any other form of punctuation. Plus and minus + - may >> be so common that just about everyone knows it, but how about | == @ % >> and even . (dot)? None of these things will be obvious to newbies who >> have never programmed before. Oh well. > >> Some things you just have to learn. > > > Yes, some things you just have to learn. Nonetheless, I strongly prefer > explicit logical operators over |, would much rather have 'equals' > instead of ==, which is stylistic in line with 'is' and explicitly > distinguishes between equality and identity comparisons. No more, or less, explicit than the difference between "==" and "is". > As for %; it is > entirely unclear to me why that obscure operation ever got its own > one-character symbol. Ill take 'mod', or even better, 'modulus' any day > of the week. Modulo is hardly an obscure operation. "What's the remainder...?" is a simple question that people learn about in primary school. And you can blame C for the use of % instead of mod or modulo. > The dot is clearly quantitatively in another realm here. 90% of typical > python code is attribute accesses. I can't imagine what sort of Python code you have seen that you consider 90% attribute access "typical". I've just run the Python tokenizer over my startup.py file, and I get these results: {'COMMENT': 24, 'DEDENT': 29, 'NL': 46, 'NAME': 256, "':'": 30, 'NEWLINE': 83, "'-'": 1, 'NUMBER': 1, "'['": 1, "','": 17, "')'": 37, "'('": 37, "'%'": 2, "'.'": 48, "'=='": 1, "'*'": 1, 'INDENT': 29, "']'": 1, "'='": 28, 'ENDMARKER': 1, 'STRING': 19} That gives attribute access being a little less than 7% of the source code. For the decimal module, the figure is a little less than 5%. > The dot is entirely unambigious and > cannot be mistaken for anything else. It reads like a book. The dot can be easily mistaken for a comma, or for a bit of grit on the monitor, especially at smaller type sizes, or for those with poor eyesight. [...] >> It is also misleading because args are not collected into a list, but >> into a tuple. > > In case you wanted a tuple youd write tuple(args), obviously. Exactly > that added flexibility is half of my case in favor. Why shouldnt it be a > list when I want it to? What sort of list? A built-in list, or whatever sort of object you get when you call the thing currently bound to the name "list"? If you can supply any function at all, what happens if I write this: def func(parg, dict(foo), list(bar)): ... How about this? def func(parg, myfunc(x)): ... What is x now? Should Python try to accumulate arguments by position, or by keyword, or try both and hope one will succeed? Which order should it try first? I believe that your proposal leads to an over-generalisation "call arbitrary functions when handling parameter lists". I don't believe you need this added complication. If you want to your var args as a list, call list(args) inside your function. >> Worse, it suggests that one should be able to generalise to something >> like this: > >> def func(parg, str(args), int(kwargs), my_func(more_args)): > >> which is incoherent. > > Sorry, but I dont get this point at all. Does ** suggests one should be > able to generalize to ***? The rules are the rules. You have missed that the generalization is not just to multiple "chunks" of arguments, but also to arbitrary functions. I thought that both ideas were equally incoherent, but ironically you suggest about that you should be able to call arbitrary functions: tuple, list, attrdict. What else? str? int? > The real questions, in my mind, are: > > 1) How useful is this added flexibility? Not insanely, but I can see it > making a lot of code significantly more clean. I don't. I see it making a small amount of code more verbose and less clean. > And: > > 2) How fundamental is collection packing/unpacking? One can easily argue > that it is indeed quite fundamental and therefore deserves its own terse > symbols, I feel. In Python, tuple unpacking and packing (actually applies to any collection, not just tuples) is *very* fundamental. That's why we can do things like this: a, b, c = my_list x, y = y, x > However, if more flexibility is indeed deemed > desirable, such terse syntax quickly gives way to a more verbose one. > Can you come up with some terse symbols that will be able to express all > of the below and dont make you wish you hadnt rather typed out the > names? > > head, tuple(tail) = iterable In Python 3, that is spelled: head, *tail = iterable tail = tuple(tail) > head, list(tail) = iterable head, *tail = iterable > head, str(tail) = somestring This is ambiguous, I'm not sure what exactly you expect to get as the string. It could arguable be any of: tail = ''.join(map(repr, tail)) tail = ''.join(map(str, tail)) tail = str(tail) or even head, tail = somestring[0], somestring[1:] > head, generator(tail) = mygenerator And this is most easily spelled: head, tail = next(mygenerator), mygenerator -- Steven From rosuav at gmail.com Sun Dec 11 20:13:32 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 12 Dec 2011 12:13:32 +1100 Subject: Documentation for python-evolution - where? In-Reply-To: <3dnfr8-h6a.ln1@chris.zbmc.eu> References: <3dnfr8-h6a.ln1@chris.zbmc.eu> Message-ID: On Mon, Dec 12, 2011 at 9:28 AM, wrote: > I'm trying to use the python evolution (as in Gnome Evolution) module > but I can't find any documetation beyond the odd trivial example and > the API documentation at http://doc.conduit-project.org/evolution-python/ > (or similar places presumably). One of the downsides of software you don't pay money for is that, all too often, there's little or no documentation. One of the upsides of free software is that you can look at the source code. I don't know anything about Python-Evolution, but if you're prepared to do a bit of digging, you can probably find what you want here: http://git.gnome.org/browse/gnome-python-desktop/tree/evolution/ (You probably have a copy of the same content on your own hard disk somewhere, too.) Once you figure out what you wanted, do consider sending a patch to the maintainer(s), improving the docstrings and/or external documentation, so the next person has an easier task. :) Chris Angelico From newsboost at gmail.com Sun Dec 11 20:20:15 2011 From: newsboost at gmail.com (someone) Date: Mon, 12 Dec 2011 02:20:15 +0100 Subject: calling python (matplotlib) from c++ Message-ID: Hi all, I want to call python (matplotlib) from C++. I use ubuntu linux and compile with "-lpython2.7". Besides that, I have this line included: #include I can then do something like this: Py_Initialize(); PyRun_SimpleString("print ' '"); PyRun_SimpleString("print ' '"); PyRun_SimpleString("print ' '"); PyRun_SimpleString("print 'PYTHON RUNNING =============='"); PyRun_SimpleString("from time import time,ctime\n" "print 'Today is',ctime(time())\n"); // Py_Main(argc, argv); // start up the python interpreter Py_Finalize(); However - I cannot seem to pass two arrays of data, so I can plot the data using matplotlib... I cannot even start matplotlib... Please help/advice/come up with suggestions, someone who knows what to do :-) From python at mrabarnett.plus.com Sun Dec 11 20:25:10 2011 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 12 Dec 2011 01:25:10 +0000 Subject: What happened to module.__file__? In-Reply-To: <4ee54918$0$11091$c3e8da3@news.astraweb.com> References: <4ee54918$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4EE557F6.5040900@mrabarnett.plus.com> On 12/12/2011 00:21, Steven D'Aprano wrote: > I've just started using a Debian system, instead of the usual RedHat > based systems I'm used to, and module.__file__ appears to have > disappeared for some (but not all) modules. > > On Fedora: > > [steve at orac ~]$ python -E > Python 2.6.2 (r262:71600, Aug 21 2009, 12:22:21) > [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import math >>>> math.__file__ > '/usr/lib/python2.6/lib-dynload/mathmodule.so' > > > > and on Debian squeeze: > > steve at runes:~$ python -E > Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) > [GCC 4.4.5] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import math >>>> math.__file__ > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'module' object has no attribute '__file__' > > > What's going on? > The documentation for __file__ says: """The __file__ attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.""" Interestingly, none of the versions on Windows that I've tried have that attribute for the math module. Is it platform-dependent? From gelonida at gmail.com Sun Dec 11 20:32:14 2011 From: gelonida at gmail.com (Gelonida N) Date: Mon, 12 Dec 2011 02:32:14 +0100 Subject: unittest. customizing tstloaders / discover() In-Reply-To: <87y5uiu1t4.fsf@jubold.box> References: <87y5uiu1t4.fsf@jubold.box> Message-ID: On 12/12/2011 12:27 AM, Thomas Bach wrote: > Gelonida N writes: > >> I'd like to use regular expresions as include / exclude rules >> and I would like to have another filter function, which would check for >> the existence of certain metavariabels in test suite files > > Did you have a look at nose? I'm using it and it supports > include/exclude rules via RE and lets you select directories to run > tests from. > > I'm not sure about the meta-variable thing, but it supports plug ins > that could do the trick? > I looked at nose very briefly, but too short to form a real opinion and to understand whether nose would have any negative impact on existing unit tests. Do I loose anything if using nose. or example can all unit tests / doc tests still be run from nose? From nfirvine at nfirvine.com Sun Dec 11 21:14:58 2011 From: nfirvine at nfirvine.com (Nick Irvine) Date: Sun, 11 Dec 2011 18:14:58 -0800 (PST) Subject: Outputting raw MIDI in realtime on Linux Message-ID: <77fe6b9f-b148-4855-b238-9089678908de@n22g2000prh.googlegroups.com> I'm trying to output raw, realtime MIDI data (as in note on, note off, etc) in Linux from Python, and I want the easiest way to do it. I've been banging my head on the wall about this for quite some time. The reason for this is to reinvent the old vkeybd application, but it's just me screwing around, so not worth the time doing it in C or creating a whole whack of supporting libraries. What I have so far is vkeybd outputting to Qsynth (via an ALSA VirMidi) to pulseaudio, playing some lovely grand piano. I want to replace vkeybd with a Python program of my own design. Using Qsynth's Messages window, I can monitor the rawish MIDI signals flowing. According to http://www.tldp.org/HOWTO/MIDI-HOWTO-10.html , I should be able to just dump MIDI bytes into /dev/snd/midiC3D0. However, this yields nothing: Qsynth does not show receiving it. I even tried this C example from the OSS docs, with the same result: http://manuals.opensound.com/developer/midi.c.html (I updated the /dev line.) What do people use to output live MIDI on Linux, assuming it's possible? Thanks, Nick Irvine From wuwei23 at gmail.com Sun Dec 11 22:17:41 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 19:17:41 -0800 (PST) Subject: Outputting raw MIDI in realtime on Linux References: <77fe6b9f-b148-4855-b238-9089678908de@n22g2000prh.googlegroups.com> Message-ID: <1683c805-7e4a-4ab4-8b6b-e825b09a9252@v24g2000prn.googlegroups.com> On Dec 12, 12:14?pm, Nick Irvine wrote: > What do people use to output live MIDI on Linux, assuming it's > possible? Hey Nick, I've yet to try this myself although it's long been on my to-do list. There are a couple of packages on PyPI that emit MIDI: http://pypi.python.org/pypi?%3Aaction=search&term=midi There is also an older project that provides a basic midi step sequencer. I can't find a proper package or installer, but you might find something useful in the 'midi_functions.py' file here: http://www.akjmusic.com/software/ From wuwei23 at gmail.com Sun Dec 11 22:31:41 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 19:31:41 -0800 (PST) Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> Message-ID: <5e253143-2dd6-4cd7-821d-311ffa7971ec@h37g2000pri.googlegroups.com> On Dec 11, 4:42?pm, Nobody wrote: > If just you're trying to avoid getting a repetitive strain injury in your > right-hand little finger from typing all the [''], you could turn > the keys into object attributes, e.g.: > > ? ? ? ? class DictObject: > ? ? ? ? ? ? def __init__(self, d): > ? ? ? ? ? ? ? ? for key, value in d.iteritems(): > ? ? ? ? ? ? ? ? ? ? setattr(self, key, value) > ? ? ? ? ... > ? ? ? ? o = DictObject(D) > ? ? ? ? # use o.a, o.b, etc I hate this kind of laziness. I'd spend at least 5 minutes trying to work out _why_ someone felt this was necessary and then get annoyed that it was just to avoid typing. From wuwei23 at gmail.com Sun Dec 11 22:35:03 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 19:35:03 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <17457040.1331.1323455329763.JavaMail.geo-discussion-forums@pret21> <6527773.1257.1323476471157.JavaMail.geo-discussion-forums@prkz16> Message-ID: <37dda2ec-0ff7-4df0-96a7-182656cefede@f36g2000prj.googlegroups.com> On Dec 11, 12:09?am, 88888 Dihedral wrote: > Wrap functions to yield is somewhat like ?a sub-threading in Erlang. Okay, now that you've replied to yourself several times, I'm convinced you're really a markov chainer. You might want to get your owner to tweak your response filter. From electronixtar at gmail.com Sun Dec 11 22:35:10 2011 From: electronixtar at gmail.com (est) Date: Sun, 11 Dec 2011 19:35:10 -0800 (PST) Subject: db agnostic admin for Flask? Message-ID: <21fa456b-67ef-4c00-af59-e3d238302b2e@t36g2000prj.googlegroups.com> Hi guys Are there any db agnostic admin for Flask/Django? Imagine you need CRUD lots of data from Mongodb, Redis, ini file or even remote sources, if such admin allows aggregate data regardless of DB types or syntax, it would be super useful. Anything like this exists? or should I start build one from scratch? From wuwei23 at gmail.com Sun Dec 11 22:36:07 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 19:36:07 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> Message-ID: <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> On Dec 9, 8:08?pm, Robert Kern wrote: > On 12/9/11 5:02 AM, alex23 wrote: > > The 3rd party 'decorator' module takes care of issues like docstrings > > & ?function signatures. I'd really like to see some of that > > functionality in the stdlib though. > > Much of it is: > > ? ?http://docs.python.org/library/functools#functools.update_wrapper Ah, cheers :) Is that a recent addition? The lack of note makes it seem like it was there from the beginning? From emekamicro at gmail.com Sun Dec 11 23:32:00 2011 From: emekamicro at gmail.com (Emeka) Date: Mon, 12 Dec 2011 06:32:00 +0200 Subject: Working with Descriptors Message-ID: Hello All, How do I get the __set__ to work here? import random class Die(object): def __init__(self, sides=6): self.sides = sides def __get__(self, instance, owner): return int(random.random() * self.sides) + 1 def __set__(self, instance, value): instance.__dict__[self.side] = value class Game(object): d6 = Die() d10 = Die(sides=10) d20 = Die(sides=20) Game.d3 = 90 (This failed) Regards, Emeka -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From jyoung79 at kc.rr.com Sun Dec 11 23:39:33 2011 From: jyoung79 at kc.rr.com (jyoung79 at kc.rr.com) Date: Mon, 12 Dec 2011 4:39:33 +0000 Subject: subprocess question Message-ID: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> Wondering if anyone could shed some light on the subprocess module? I'll admit I'm not that great at the shell. If I was wanting to get the size of the trash (on OS X), I could use: >>> os.system('du -sh ~/.Trash/') 11M /Users/jay/.Trash/ 0 Which gives me what I want. However, I've been reading that it's better to use subprocess. I did a test like so, but is this a good way to do this? >>> import subprocess >>> p = subprocess.Popen(['du', '-sh'], cwd='/Users/jay/.Trash/', stdout=subprocess.PIPE) >>> out, err = p.communicate() >>> out ' 11M\t.\n' >>> err >>> And another question - why can't I use the tilde as a shortcut to the home directory? >>> p = subprocess.Popen(['du', '-sh'], cwd='~/.Trash/', stdout=subprocess.PIPE) Traceback (most recent call last): File "", line 1, in File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in __init__ errread, errwrite) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1202, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory: '~/.Trash/' Thanks for looking at my questions. Jay From dihedral88888 at googlemail.com Sun Dec 11 23:51:58 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 11 Dec 2011 20:51:58 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> Message-ID: <13587584.2147.1323665518804.JavaMail.geo-discussion-forums@prnx7> On Monday, December 12, 2011 11:36:07 AM UTC+8, alex23 wrote: > On Dec 9, 8:08?pm, Robert Kern wrote: > > On 12/9/11 5:02 AM, alex23 wrote: > > > The 3rd party 'decorator' module takes care of issues like docstrings > > > & ?function signatures. I'd really like to see some of that > > > functionality in the stdlib though. > > > > Much of it is: > > > > ? ?http://docs.python.org/library/functools#functools.update_wrapper > > Ah, cheers :) Is that a recent addition? The lack of note makes it > seem like it was there from the beginning? To wrap a function properly is different from the 1-line lampda. This is really functional programming. Every function can be decorated to change into a different one easily. There is a method to replace every return action of a python function into an yield action without the source code. From ethan at stoneleaf.us Mon Dec 12 00:00:40 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 11 Dec 2011 21:00:40 -0800 Subject: Dynamic variable creation from string In-Reply-To: <5e253143-2dd6-4cd7-821d-311ffa7971ec@h37g2000pri.googlegroups.com> References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> <5e253143-2dd6-4cd7-821d-311ffa7971ec@h37g2000pri.googlegroups.com> Message-ID: <4EE58A78.7060500@stoneleaf.us> alex23 wrote: > On Dec 11, 4:42 pm, Nobody wrote: >> If just you're trying to avoid getting a repetitive strain injury in your >> right-hand little finger from typing all the [''], you could turn >> the keys into object attributes, e.g.: >> >> class DictObject: >> def __init__(self, d): >> for key, value in d.iteritems(): >> setattr(self, key, value) >> ... >> o = DictObject(D) >> # use o.a, o.b, etc > > I hate this kind of laziness. I'd spend at least 5 minutes trying to > work out _why_ someone felt this was necessary and then get annoyed > that it was just to avoid typing. For me, half of it is to avoid the typing, the other half to avoid the reading. ;) ~Ethan~ From ian.g.kelly at gmail.com Mon Dec 12 00:05:27 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 11 Dec 2011 22:05:27 -0700 Subject: Working with Descriptors In-Reply-To: References: Message-ID: On Sun, Dec 11, 2011 at 9:32 PM, Emeka wrote: > > Hello All, > > How do I get the __set__ to work here? > > import random > > class Die(object): > ??? def __init__(self, sides=6): > ??????? self.sides = sides > > ??? def __get__(self, instance, owner): > ??????? return int(random.random() * self.sides) + 1 > > ??? def __set__(self, instance, value): > ??????? instance.__dict__[self.side] = value > > > > class Game(object): > ??? d6 = Die() > ??? d10 = Die(sides=10) > ??? d20 = Die(sides=20) > > > Game.d3 = 90 (This failed) I'm not sure exactly what it is you're trying to do with this, but there are a couple problems. First of all, at "Game.d3 = 90" you appear to be trying to set the value of a descriptor on a class object. This doesn't work, because __set__ doesn't get called when you try to set the descriptor on the class object directly. It only reassigns the attribute and replaces the descriptor. You need to either create an instance of Game and use the descriptors on that, or put the descriptor in a metaclass. Second, you're assigning to the d3 descriptor, but you never created a descriptor for the d3 attribute. The only descriptors on the Game class are d6, d10, and d20. If you're trying to assign to the descriptor, it would need to exist first. If you're trying to add a new descriptor, you would need to do something like "Game.d3 = Die(sides=3)". From bahamutzero8825 at gmail.com Mon Dec 12 00:24:14 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sun, 11 Dec 2011 23:24:14 -0600 Subject: subprocess question In-Reply-To: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> References: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> Message-ID: <4EE58FFE.1060408@gmail.com> On 12/11/2011 10:39 PM, jyoung79 at kc.rr.com wrote: > And another question - why can't I use the tilde as a shortcut to the home directory? Because subprocess doesn't use the shell (which is what expands the tilde to the invoking user's home directory). I recommend using os.path.join and os.environ anyway. p = subprocess.Popen(['du', '-sh', os.path.join(os.environ['home'], '.Trash')], stdout=subprocess.PIPE) -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From wuwei23 at gmail.com Mon Dec 12 00:45:31 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 21:45:31 -0800 (PST) Subject: tracking variable value changes References: Message-ID: <21825f6f-debe-4c9c-8efb-3f1d557cfe8a@d12g2000prg.googlegroups.com> Andrea Crotti wrote: > Not sure if it's exactly pure python but Traits can actually do thishttps://github.com/enthought/traits At an attribute level, absolutely, but not at the variable level like the OP is requesting. It's a great package, though :) From wuwei23 at gmail.com Mon Dec 12 00:47:52 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 21:47:52 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> <13587584.2147.1323665518804.JavaMail.geo-discussion-forums@prnx7> Message-ID: <4fa80db8-deec-4632-8402-f63f3fb91740@h37g2000pri.googlegroups.com> On Dec 12, 2:51?pm, 88888 Dihedral wrote: > To wrap a function properly is different from the 1-line lampda. > > This is really functional programming. > > Every function can be decorated to change into a different one easily. > > There is ?a method to replace every return action ?of a python function > into an ?yield action without the source code. How does this have _anything_ to do with my exchange with Robert? If you're _not_ a markov chainer, you're trying way too hard to show off what you know, and very little of it seems relevant to the thread. From clp2 at rebertia.com Mon Dec 12 01:02:23 2011 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 11 Dec 2011 22:02:23 -0800 Subject: subprocess question In-Reply-To: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> References: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> Message-ID: On Sun, Dec 11, 2011 at 8:39 PM, wrote: > Wondering if anyone could shed some light on the subprocess module? ?I'll admit I'm not that great at the shell. > > If I was wanting to get the size of the trash (on OS X), I could use: > >>>> os.system('du -sh ~/.Trash/') > ?11M ? ?/Users/jay/.Trash/ > 0 > > Which gives me what I want. ?However, I've been reading that it's better to use subprocess. ?I did a test like so, but is this a good way to do this? > >>>> import subprocess >>>> p = subprocess.Popen(['du', '-sh'], cwd='/Users/jay/.Trash/', stdout=subprocess.PIPE) >>>> out, err = p.communicate() >>>> out > ' 11M\t.\n' You might prefer to use subprocess.check_output(); it slightly simplifies your code: http://docs.python.org/library/subprocess.html#subprocess.check_output > And another question - why can't I use the tilde as a shortcut to the home directory? Because ~ is interpolated by the shell and `subprocess` does not use the shell by default for reasons that include efficiency and security. You can expand ~ yourself using os.path.expanduser(): http://docs.python.org/library/os.path.html#os.path.expanduser Alternatively, you can opt to use the shell by passing shell=True as an argument. Cheers, Chris -- http://rebertia.com From wuwei23 at gmail.com Mon Dec 12 01:50:32 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 22:50:32 -0800 (PST) Subject: Dynamic variable creation from string References: <4edffed8$0$29988$c3e8da3$5496439d@news.astraweb.com> <8ebfe32c-46a5-4bdc-a853-018d7f72d3d3@y18g2000yqy.googlegroups.com> <5e253143-2dd6-4cd7-821d-311ffa7971ec@h37g2000pri.googlegroups.com> Message-ID: <627c1eb0-ff06-4108-a2df-38a0207bdacc@z22g2000prd.googlegroups.com> On Dec 12, 3:00?pm, Ethan Furman wrote: > For me, half of it is to avoid the typing, the other half to avoid the > reading. ?;) Believe me, I get that, I've been more than guilty of it in the past :) But the reading here is important. If I see a dictionary lookup, I expect it to be accessing a dictionary; if I see an attribute reference, it might be wrapped in a descriptor. (And yes, I'm very much aware that ultimately an attribute lookup _is_ a dictionary lookup, but it's everything else that potentially happens after that point that is important here.) Changing the visual short circuits any understandings; then again, I tend to hate pesudo/mini-DSLs for the same reason... I can't just rely on my understanding of the language, I need to understand a specific implementation and mentally flag whether it's relevant or not. I also don't understand the original desire to create dynamic variables either. Either you know the names in advance, in which case just set up the necessary scaffolding to inject them where you need them, or you _don't_ and there's no value in creating them anyway because you don't know how to refer to them in your code. So if you know the names, use dictionary unpacking: userData = dict(a = 1, b = 2, c = 3) def func(a,b,c): return a + b + c >>> func(**userData) 6 If the dictionary has more values than you need, catch & discard the ones you don't want in **kwargs. From python.leojay at gmail.com Mon Dec 12 02:07:48 2011 From: python.leojay at gmail.com (Leo Jay) Date: Mon, 12 Dec 2011 15:07:48 +0800 Subject: Cannot use multiprocessing and zip together on windows Message-ID: Hi All, I have a file p.zip, there is a __main__.py in it, and the content of __main__.py is: from multiprocessing import Process import os def f(): print 'in f, pid:', os.getpid() if __name__ == '__main__': print 'pid:', os.getpid() p = Process(target=f) p.start() p.join() On linux, I can get expected result for running "python p.zip" But on windows xp, I got: Traceback (most recent call last): File "", line 1, in File "C:\python27\lib\multiprocessing\forking.py", line 346, in main prepare(preparation_data) File "C:\python27\lib\multiprocessing\forking.py", line 454, in prepare assert main_name not in sys.modules, main_name AssertionError: __main__ It seems that the situation described here is similar: http://bugs.python.org/issue10128 But the patch doesn't work for me. Anybody knows how to fix this? Thanks. -- Best Regards, Leo Jay From wuwei23 at gmail.com Mon Dec 12 02:11:18 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 11 Dec 2011 23:11:18 -0800 (PST) Subject: Dynamic variable creation from string References: Message-ID: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> On Dec 8, 3:09?am, Massi wrote: > in my script I have a dictionary whose items are couples in the form > (string, integer values), say > > D = {'a':1, 'b':2, 'c':3} > > This dictionary is passed to a function as a parameter, e.g. : > > def Sum(D) : > ? ? return D['a']+D['b']+D['c'] > > Is there a way to create three variables dynamically inside Sum in > order to re write the function like this? > > def Sum(D) : > ? ? # Here some magic to create a,b,c from D > ? ? return a+b+c Okay, here's a possible solution that doesn't rely on exec, but does use the third-party module byteplay (which I believe limits it to Python 2.5-2.7) and tries to retain as much as possible your syntax (with some slight adjustments): from byteplay import Code, opmap class VariableInjector(dict): def transmute(self, opcode, arg): if (opcode == opmap['LOAD_GLOBAL']) and (arg in self): self._transmuted.append(arg) return opmap['LOAD_FAST'], arg return opcode, arg def make_locals(self, args): locals = [] for arg in args: locals.append((opmap['LOAD_CONST'], self[arg])) locals.append((opmap['STORE_FAST'], arg)) return locals def bind_to(self, function): function.ofunc_code = function.func_code def _(*args, **kwargs): self._transmuted = [] code = Code.from_code(function.ofunc_code) code.code = [self.transmute(op, arg) for op, arg in code.code] code.code = self.make_locals(self._transmuted) + code.code function.func_code = code.to_code() return function(*args, **kwargs) return _ For your example, you'd use it like this: >>> def sum(): ... return a + b + c ... >>> def product(): ... return a * b * c ... >>> data = VariableInjector(a=1,b=2,c=3) >>> sum = data.bind_to(sum) >>> product = data.bind_to(product) >>> sum() 6 >>> product() 6 >>> data {'a': 1, 'c': 3, 'b': 2} >>> data['a'] = 100 >>> sum() 105 >>> product() 600 I'm not sure how rigorous this would be in real use but it's passed the few quick toy cases I've tried it out on. Any thanks should go to Michael Foord, as this borrows heavily from his self-less metaclass example: http://www.voidspace.org.uk/python/articles/metaclasses.shtml From huisky at gmx.com Mon Dec 12 02:22:00 2011 From: huisky at gmx.com (huisky) Date: Sun, 11 Dec 2011 23:22:00 -0800 (PST) Subject: Plot all contents in a two dimension list in a loop? Message-ID: <49b903d3-2b75-4be5-b9e6-f314b66f0906@t16g2000vba.googlegroups.com> Hi, say I have two lists a and b, which are both in two dimensions. I wanna to plot all curves by using pylab.plot(a[i],b[i]) in a loop for the range of the length(a). I got quite a lot of figures, but only the first one with the plot i want. how to eliminate those blank figures poping out? Rgds Huisky From ethan at stoneleaf.us Mon Dec 12 02:47:54 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 11 Dec 2011 23:47:54 -0800 Subject: I love the decorator in Python!!! In-Reply-To: <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> Message-ID: <4EE5B1AA.80909@stoneleaf.us> alex23 wrote: > On Dec 9, 8:08 pm, Robert Kern wrote: >> On 12/9/11 5:02 AM, alex23 wrote: >>> The 3rd party 'decorator' module takes care of issues like docstrings >>> & function signatures. I'd really like to see some of that >>> functionality in the stdlib though. >> Much of it is: >> >> http://docs.python.org/library/functools#functools.update_wrapper > > Ah, cheers :) Is that a recent addition? The lack of note makes it > seem like it was there from the beginning? Not sure how recent it is, but I do know it does *not* handle the signature. ~Ethan~ From greg.ewing at canterbury.ac.nz Mon Dec 12 03:09:43 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 12 Dec 2011 21:09:43 +1300 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> Message-ID: <9klr6aF2ovU1@mid.individual.net> Steven D'Aprano wrote: > Modulo is hardly an obscure operation. "What's the remainder...?" is a > simple question that people learn about in primary school. Well, sort of. The way I remember it, the remainder was just something that fell out as a side effect of division -- the annoying bit left over that you didn't know what to do with. We weren't taught to think of "finding the remainder" as a distinct operation that's useful in its own right. Once we were taught to do proper division with decimal points and everything, the concept of a remainder seemed to get discarded and was never mentioned again. A couple of years later we were briefly introduced to the concept of modulo arithmetic, but as far as I remember, the connection with division and remainders wasn't pointed out. Also, it was presented in a very abstract way, and I couldn't see any practical application for it at the time. (At that age, it hadn't occurred to me that some of the stuff we were being shown might be just pure mathematics for its own sake, and I was often thinking to myself, "Why am I being taught this?") It wasn't until much later when I got into programming that I began to see all the connections and applications. For people who don't become programmers, I suspect they never have much use for remainders in everyday life. Now, in a desperate attempt to stop rambling and get back on topic... > Eelco Hoogendoorn wrote: >>The dot is clearly quantitatively in another realm here. Also it has almost unchallenged supremacy as the attribute access operator in just about every language having some form of struct concept, going back to around Algol 68. Spelling of the mod operator is much more variable. > {'COMMENT': 24, 'DEDENT': 29, 'NL': 46, 'NAME': 256, "':'": 30, > 'NEWLINE': 83, "'-'": 1, 'NUMBER': 1, "'['": 1, "','": 17, "')'": 37, > "'('": 37, "'%'": 2, "'.'": 48, "'=='": 1, "'*'": 1, 'INDENT': 29, "']'": > 1, "'='": 28, 'ENDMARKER': 1, 'STRING': 19} > > That gives attribute access being a little less than 7% of the source > code. However, it's clearly the most commonly used *operator* by a large margin. > The dot can be easily mistaken for a comma, Not in my code, because I always put a space after a comma, and never after an attribute-access dot. (And if you can't tell whether there's a space there or not, you need a bigger font or better glasses. :-) Also, dots sit nicely under my little finger where they're easy to type. I like dots. Dots are a great goodness. -- Greg From greg.ewing at canterbury.ac.nz Mon Dec 12 03:25:40 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 12 Dec 2011 21:25:40 +1300 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE54272.8000005@gmail.com> Message-ID: <9kls47Fa7uU1@mid.individual.net> For what it's worth, googling for "python asterisk" gives this as the very first result: http://www.technovelty.org/code/python/asterisk.html which tells you exactly what you're probably wanting to know if you ask that. To check that this phenomemon isn't restricted to asterisks in particular, I also tried "python plus equals" and got http://stackoverflow.com/questions/2347265/what-does-plus-equals-do-in-python which is also a pretty good result. So the rule of thumb seems to be: if you're trying to google for punctuation, try spelling it out. -- Greg From tjreedy at udel.edu Mon Dec 12 03:29:53 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 03:29:53 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE54272.8000005@gmail.com> References: <4EE54272.8000005@gmail.com> Message-ID: On 12/11/2011 6:53 PM, Eelco Hoogendoorn wrote: >> There are other means of finding information than Google. Really. > > This is really only a very minor point in my argument, so I dont want to > put the focus on this. On the contrary, it is a major point. You want us to change the language so you can program by Google. Sorry, aint't gonna happen. > Googling 'myprogramminglanguage conceptimtryingtofigureout' is my first, > second and third line of defence. Yes, I could read the reference manual > from top to bottom, and if I already knew about the existence of your > article then im sure that would be a great help too. You left out skimming the table of contents and using the index. On the Windows version of the docs, one can just type the entry wanted in the entry box on the Index tab and the lookup is done for you. Two chars to type for '**'. > But the situation > one finds oneself in is seeing two asterikses and not even being aware > they are particular to function definitions/invocations. If you find a symbol in a particular context, the entry for the context seems a reasonable place to start. -- Terry Jan Reedy From tjreedy at udel.edu Mon Dec 12 03:44:51 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 03:44:51 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE54066.3020901@gmail.com> References: <4EE54066.3020901@gmail.com> Message-ID: On 12/11/2011 6:44 PM, Eelco Hoogendoorn wrote: > Can you come up with some terse symbols that will be able to express all > of the below and dont make you wish you hadnt rather typed out the names? > > head, tuple(tail) = iterable > head, list(tail) = iterable > head, str(tail) = somestring > head, generator(tail) = mygenerator The above examples are seldom needed in Python because we have one general method to repeatedly split a sequence into head and tail. it = iter(iterable) # 'it' now represents the sequenced iterable head = next(it) # 'it' now represents the tail after removing the head In other words, next(it) encompasses all of your examples and many more. Because 'it' is mutated to represent the tail, it does not need to be rebound and therefore is not. Iterable unpacking with a *target for leftovers is an entirely different use case. -- Terry Jan Reedy From e.hoogendoorn at uva.nl Mon Dec 12 04:08:04 2011 From: e.hoogendoorn at uva.nl (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 10:08:04 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE5C474.3070500@uva.nl> > On the contrary, it is a major point. Sorry, but im affraid it is up to ME to decide which point I feel are important. No, this is a minor point to me, and one that has been admirably put to rest by pointing out that spelling out the name of the symbol in google directly leads you to the information you are looking for. From hoogendoorn.eelco at gmail.com Mon Dec 12 04:12:22 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 10:12:22 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE5C576.2000307@gmail.com> > The above examples are seldom needed in Python because we have one > general method to repeatedly split a sequence into head and tail. > it = iter(iterable) # 'it' now represents the sequenced iterable > head = next(it) # 'it' now represents the tail after removing the head > In other words, next(it) encompasses all of your examples and many more. > Because 'it' is mutated to represent the tail, it does not need to be > rebound and therefore is not. The question in language design is never 'could we do these things before'. The answer is obvious: yes our CPUs are turing complete; we can do anything. The question is; how would we like to do them? So do you think the new head/tail unpacking features in python 3 are entirely uncalled for? I personally quite like them, but I would like them to be more general. From hoogendoorn.eelco at gmail.com Mon Dec 12 04:32:16 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 10:32:16 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE5CA20.7060502@gmail.com> > No more, or less, explicit than the difference between "==" and "is". == may be taken to mean identity comparison; 'equals' can only mean one thing. Of course 'formally' these symbols are well defined, but so is brainf*ck > Modulo is hardly an obscure operation. "What's the remainder...?" is a > simple question that people learn about in primary school. So is 'how much wood would a woodchucker chuck if a woodchucker could chuck wood?'. But how often does that concept turn up in your code? > And you can blame C for the use of % instead of mod or modulo. I didnt know one of Python's design goals was backwards compatibility with C. > I can't imagine what sort of Python code you have seen that you consider > 90% attribute access "typical". I've just run the Python tokenizer over > my startup.py file, and I get these results: Yes, that was a hyperbole; but quite an often used construct, is it not? > If you can supply any function at all, what happens if I write this: You cannot; only constructors modelling a sequence or a dict, and only in that order. Is that rule clear enough? > I believe that your proposal leads to an over-generalisation "call > arbitrary functions when handling parameter lists". I hope the above clears that up. It is as much about calling functions as ** is about raising kwargs to the power of. > I don't believe you > need this added complication. If you want to your var args as a list, > call list(args) inside your function. We dont strictly 'need' any language construct. Real men use assembler, right? > >/ head, tuple(tail) = iterable /> In Python 3, that is spelled: > head, *tail = iterable > tail = tuple(tail) Yes, I know. How is that not a lot more verbose and worse than what I have proposed in all possible ways? > head, tail = somestring[0], somestring[1:] Well yes, splendid; we can do that with lists too since the dawn of Python. What you are saying here in effect is that you think the head/tail syntax is superfluous; that youd rather see it eliminated than generalized. > head, tail = next(mygenerator), mygenerator Which again of course works, but is yet again of entirely different form than any of the above solutions, while conceptually doing the same thing. Certainly, there is room for improved elegance here? -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Mon Dec 12 04:37:40 2011 From: hoogendoorn.eelco at gmail.com (Eelco Hoogendoorn) Date: Mon, 12 Dec 2011 10:37:40 +0100 Subject: Verbose and flexible args and kwargs syntax Message-ID: <4EE5CB64.7060300@gmail.com> > On the contrary, it is a major point. You want us to change the language > so you can program by Google. Sorry, aint't gonna happen. On the contrary; I believe I get to decide which points I consider important. This one, I do not. Sorry for putting it in the first paragraph. From bachth at uni-mainz.de Mon Dec 12 05:44:07 2011 From: bachth at uni-mainz.de (Thomas Bach) Date: Mon, 12 Dec 2011 11:44:07 +0100 Subject: unittest. customizing tstloaders / discover() References: <87y5uiu1t4.fsf@jubold.box> Message-ID: <8739cqf4so.fsf@jubold.box> Gelonida N writes: > Do I loose anything if using nose. or example can all unit tests / doc > tests still be run from nose? AFAIK you don't loose anything by using nose ? the unittests should all be found and doctests can be run via `--with-doctest', I never used doctests though. regards From jpiitula at ling.helsinki.fi Mon Dec 12 05:59:05 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 12 Dec 2011 12:59:05 +0200 Subject: Verbose and flexible args and kwargs syntax References: Message-ID: Eelco Hoogendoorn writes: > As for %; it is entirely unclear to me why that obscure operation > ever got its own one-character symbol. Ill take 'mod', or even > better, 'modulus' any day of the week. The modulus is not the result but one of the arguments: when numbers x and y are congruent modulo n (stated in terms of the modulo operation: x mod n = y mod n), the modulus is n. A word for x mod n is remainder. I agree about the obscurity of using the percent sign as the operator. A quick google suggests that your use of 'modulus' is now popular among programmers. Past experience in mathematics newsgroups tells me that some mathematicians do not accept the existence of any remainder operator at all. Honest. (I see them but I cannot understand them.) From jeanmichel at sequans.com Mon Dec 12 06:13:33 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 12 Dec 2011 12:13:33 +0100 Subject: Overriding a global In-Reply-To: References: Message-ID: <4EE5E1DD.6040101@sequans.com> Roy Smith wrote: > MRAB wrote: > > >> or use 'globals': >> >> def function(self): >> logger = globals()['logger'].getChild('function') >> logger.debug('stuff') >> logger.debug('other stuff') >> > > Ah-ha! That's precisely what I was looking for. Much appreciated. > Using the same name for 2 different objects is a bad idea in general. In debug mode, i.e. the logger is configured with the debug level, you could simply write down the filename and the line number on your log events. formatter = logging.Formatter('%(name)s : %(filename)s %(lineno)s %(message)s') So you can easily see who logged what (if I'm not wrong, that's what you're trying to do) JM From hoogendoorn.eelco at gmail.com Mon Dec 12 06:18:42 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 03:18:42 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: Message-ID: By the way... Is there any particular reason why some of my replies do not show up on groups.google, and some of them do not show up on mail.python.org? Sorry to annoy people with reposting, but im going to be forced to do some of that until this is cleared up.... From hoogendoorn.eelco at gmail.com Mon Dec 12 06:22:25 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 03:22:25 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: Message-ID: <95663c15-804c-49f3-8168-023a5c93bc5e@w3g2000vbw.googlegroups.com> > The modulus is not the result but one of the arguments: when numbers x > and y are congruent modulo n (stated in terms of the modulo operation: > x mod n = y mod n), the modulus is n. A word for x mod n is remainder. > > I agree about the obscurity of using the percent sign as the operator. > > A quick google suggests that your use of 'modulus' is now popular > among programmers. Past experience in mathematics newsgroups tells me > that some mathematicians do not accept the existence of any remainder > operator at all. Honest. (I see them but I cannot understand them.) You are correct; the thing it computes is the remainder, not the modulus. Nonetheless, 'x modulus y' is how it is put in natural language, but I suppose math.remainder would be my preferred place to put this. From jpiitula at ling.helsinki.fi Mon Dec 12 06:46:56 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 12 Dec 2011 13:46:56 +0200 Subject: Verbose and flexible args and kwargs syntax References: <95663c15-804c-49f3-8168-023a5c93bc5e@w3g2000vbw.googlegroups.com> Message-ID: Eelco writes: > > The modulus is not the result but one of the arguments: when numbers x > > and y are congruent modulo n (stated in terms of the modulo operation: > > x mod n = y mod n), the modulus is n. A word for x mod n is remainder. > > > > I agree about the obscurity of using the percent sign as the operator. > > > > A quick google suggests that your use of 'modulus' is now popular > > among programmers. Past experience in mathematics newsgroups tells me > > that some mathematicians do not accept the existence of any remainder > > operator at all. Honest. (I see them but I cannot understand them.) > > You are correct; the thing it computes is the remainder, not the > modulus. Nonetheless, 'x modulus y' is how it is put in natural > language, but I suppose math.remainder would be my preferred place to > put this. I think it's 'x modulo y', which matches 'x and y are congruent modulo z', but now I fear that programming people have been developing a different habit. From dihedral88888 at googlemail.com Mon Dec 12 07:16:33 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 12 Dec 2011 04:16:33 -0800 (PST) Subject: I love the decorator in Python!!! In-Reply-To: <4fa80db8-deec-4632-8402-f63f3fb91740@h37g2000pri.googlegroups.com> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> <13587584.2147.1323665518804.JavaMail.geo-discussion-forums@prnx7> <4fa80db8-deec-4632-8402-f63f3fb91740@h37g2000pri.googlegroups.com> Message-ID: <7918199.244.1323692193737.JavaMail.geo-discussion-forums@prlu4> On Monday, December 12, 2011 1:47:52 PM UTC+8, alex23 wrote: > On Dec 12, 2:51?pm, 88888 Dihedral > wrote: > > To wrap a function properly is different from the 1-line lampda. > > > > This is really functional programming. > > > > Every function can be decorated to change into a different one easily. > > > > There is ?a method to replace every return action ?of a python function > > into an ?yield action without the source code. > > How does this have _anything_ to do with my exchange with Robert? > > If you're _not_ a markov chainer, you're trying way too hard to show > off what you know, and very little of it seems relevant to the thread. I think in the CS way in the 5th generation computer languages. But I also think in the hardware way in the 2,3,4th generations. Why do you need to spawn a thread or a process that can be decorated by wrapping an yield for all kinds of parameter checking of functions? A lousy written function in any computer language indicates a bad taste of art. From hoogendoorn.eelco at gmail.com Mon Dec 12 07:21:15 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 04:21:15 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> Message-ID: <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> > No more, or less, explicit than the difference between "==" and "is". == may be taken to mean identity comparison; 'equals' can only mean one thing. Of course 'formally' these symbols are well defined, but so is brainf*ck > Modulo is hardly an obscure operation. "What's the remainder...?" is a > simple question that people learn about in primary school. So is 'how much wood would a woodchucker chuck if a woodchucker could chuck wood?'. But how often does that concept turn up in your code? > And you can blame C for the use of % instead of mod or modulo. I didnt know one of Python's design goals was backwards compatibility with C. > I can't imagine what sort of Python code you have seen that you consider > 90% attribute access "typical". I've just run the Python tokenizer over > my startup.py file, and I get these results: Yes, that was a hyperbole; but quite an often used construct, is it not? > If you can supply any function at all, what happens if I write this: You cannot; only constructors modelling a sequence or a dict, and only in that order. Is that rule clear enough? > I believe that your proposal leads to an over-generalisation "call > arbitrary functions when handling parameter lists". I hope the above clears that up. It is as much about calling functions as ** is about raising kwargs to the power of. > I don't believe you > need this added complication. If you want to your var args as a list, > call list(args) inside your function. We dont strictly 'need' any language construct. Real men use assembler, right? > > head, tuple(tail) = iterable > In Python 3, that is spelled: > head, *tail = iterable > tail = tuple(tail) Yes, I know. How is that not a lot more verbose and worse than what I have proposed in all possible ways? > head, tail = somestring[0], somestring[1:] Well yes, splendid; we can do that with lists too since the dawn of Python. What you are saying here in effect is that you think the head/tail syntax is superfluous; that youd rather see it eliminated than generalized. > head, tail = next(mygenerator), mygenerator Which again of course works, but is yet again of entirely different form than any of the above solutions, while conceptually doing the same thing. Certainly, there is room for improved elegance here? From dihedral88888 at googlemail.com Mon Dec 12 07:43:20 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 12 Dec 2011 04:43:20 -0800 (PST) Subject: Dynamic variable creation from string In-Reply-To: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> References: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> Message-ID: <21495782.256.1323693800361.JavaMail.geo-discussion-forums@prmw6> On Monday, December 12, 2011 3:11:18 PM UTC+8, alex23 wrote: > On Dec 8, 3:09?am, Massi wrote: > > in my script I have a dictionary whose items are couples in the form > > (string, integer values), say > > > > D = {'a':1, 'b':2, 'c':3} > > > > This dictionary is passed to a function as a parameter, e.g. : > > > > def Sum(D) : > > ? ? return D['a']+D['b']+D['c'] > > > > Is there a way to create three variables dynamically inside Sum in > > order to re write the function like this? > > > > def Sum(D) : > > ? ? # Here some magic to create a,b,c from D > > ? ? return a+b+c > > Okay, here's a possible solution that doesn't rely on exec, but does > use the third-party module byteplay (which I believe limits it to > Python 2.5-2.7) and tries to retain as much as possible your syntax > (with some slight adjustments): > > from byteplay import Code, opmap > > class VariableInjector(dict): > def transmute(self, opcode, arg): > if (opcode == opmap['LOAD_GLOBAL']) and (arg in self): > self._transmuted.append(arg) > return opmap['LOAD_FAST'], arg > return opcode, arg > > def make_locals(self, args): > locals = [] > for arg in args: > locals.append((opmap['LOAD_CONST'], self[arg])) > locals.append((opmap['STORE_FAST'], arg)) > return locals > > def bind_to(self, function): > function.ofunc_code = function.func_code > def _(*args, **kwargs): > self._transmuted = [] > code = Code.from_code(function.ofunc_code) > code.code = [self.transmute(op, arg) for op, arg in > code.code] > code.code = self.make_locals(self._transmuted) + > code.code > function.func_code = code.to_code() > return function(*args, **kwargs) > return _ > > For your example, you'd use it like this: > > >>> def sum(): > ... return a + b + c > ... > >>> def product(): > ... return a * b * c > ... > >>> data = VariableInjector(a=1,b=2,c=3) > >>> sum = data.bind_to(sum) > >>> product = data.bind_to(product) > >>> sum() > 6 > >>> product() > 6 > >>> data > {'a': 1, 'c': 3, 'b': 2} > >>> data['a'] = 100 > >>> sum() > 105 > >>> product() > 600 > > I'm not sure how rigorous this would be in real use but it's passed > the few quick toy cases I've tried it out on. > > Any thanks should go to Michael Foord, as this borrows heavily from > his self-less metaclass example: > http://www.voidspace.org.uk/python/articles/metaclasses.shtml From dihedral88888 at googlemail.com Mon Dec 12 07:49:10 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Mon, 12 Dec 2011 04:49:10 -0800 (PST) Subject: Dynamic variable creation from string In-Reply-To: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> References: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> Message-ID: <24728986.18.1323694150411.JavaMail.geo-discussion-forums@prfc16> On Monday, December 12, 2011 3:11:18 PM UTC+8, alex23 wrote: > On Dec 8, 3:09?am, Massi wrote: > > in my script I have a dictionary whose items are couples in the form > > (string, integer values), say > > > > D = {'a':1, 'b':2, 'c':3} > > > > This dictionary is passed to a function as a parameter, e.g. : > > > > def Sum(D) : > > ? ? return D['a']+D['b']+D['c'] > > > > Is there a way to create three variables dynamically inside Sum in > > order to re write the function like this? > > > > def Sum(D) : > > ? ? # Here some magic to create a,b,c from D > > ? ? return a+b+c > > Okay, here's a possible solution that doesn't rely on exec, but does > use the third-party module byteplay (which I believe limits it to > Python 2.5-2.7) and tries to retain as much as possible your syntax > (with some slight adjustments): > > from byteplay import Code, opmap > > class VariableInjector(dict): > def transmute(self, opcode, arg): > if (opcode == opmap['LOAD_GLOBAL']) and (arg in self): > self._transmuted.append(arg) > return opmap['LOAD_FAST'], arg > return opcode, arg > > def make_locals(self, args): > locals = [] > for arg in args: > locals.append((opmap['LOAD_CONST'], self[arg])) > locals.append((opmap['STORE_FAST'], arg)) > return locals > > def bind_to(self, function): > function.ofunc_code = function.func_code > def _(*args, **kwargs): > self._transmuted = [] > code = Code.from_code(function.ofunc_code) > code.code = [self.transmute(op, arg) for op, arg in > code.code] > code.code = self.make_locals(self._transmuted) + > code.code > function.func_code = code.to_code() > return function(*args, **kwargs) > return _ > > For your example, you'd use it like this: > > >>> def sum(): > ... return a + b + c > ... > >>> def product(): > ... return a * b * c > ... > >>> data = VariableInjector(a=1,b=2,c=3) > >>> sum = data.bind_to(sum) > >>> product = data.bind_to(product) > >>> sum() > 6 > >>> product() > 6 > >>> data > {'a': 1, 'c': 3, 'b': 2} > >>> data['a'] = 100 > >>> sum() > 105 > >>> product() > 600 > > I'm not sure how rigorous this would be in real use but it's passed > the few quick toy cases I've tried it out on. > > Any thanks should go to Michael Foord, as this borrows heavily from > his self-less metaclass example: > http://www.voidspace.org.uk/python/articles/metaclasses.shtml This is the way to write an assembler or to roll out a script language to be included in an app by users. From hfaber at invalid.net Mon Dec 12 08:27:40 2011 From: hfaber at invalid.net (Henrik Faber) Date: Mon, 12 Dec 2011 14:27:40 +0100 Subject: Confusion about decorators Message-ID: Hi group, I'm a bit confused regarding decorators. Recently started playing with them with Python3 and wanted (as an excercise) to implement a simple type checker first: I know there are lots of them out there, this is actually one of the reasons I chose that particular function (to compare my solution against other, proven solutions). Starting with a blank slate, I did something along the lines of: class _TypeCheckedFunction(): def __init__(self, decoratedfunction): self._decoratedfunction = decoratedfunction def __call__(self, *args, **kwargs): [...] Actual checking def typecheck(wrappedfunction): checkfunction = _TypeCheckedFunction(wrappedfunction) functools.update_wrapper(checkfunction, wrappedfunction) return checkfunction And decorate my methods like @typecheck def setbar(self, bar: str): This works somewhat. The problem is, however, when the method is actually called. This is what happens: 1. The decorator is called upon import of the decorated class. It creates a _TypeCheckedFunction(setbar) object. 2. When setbar is actually called (blubb.setbar("fooobar")), the __call__ method of the previously created _TypeCheckedFunction is invoked. 3. When trying to call self._decoratedfunction from within that object, this fails: "self" is missing! self._decoratedfunction is only the *function*, not the bound function of the object that contains setbar(). Therefore I cannot proceed here. Solutions that I have seen working usually consist of two functions wrapped in each other, but I do not know why the additional introduction of a class makes everything fail. Can someone please enlighten me? Best regards, Henrik From andrea.crotti.0 at gmail.com Mon Dec 12 08:37:32 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Mon, 12 Dec 2011 13:37:32 +0000 Subject: Confusion about decorators In-Reply-To: References: Message-ID: <4EE6039C.2000205@gmail.com> On 12/12/2011 01:27 PM, Henrik Faber wrote: > Hi group, > > I'm a bit confused regarding decorators. Recently started playing with > them with Python3 and wanted (as an excercise) to implement a simple > type checker first: I know there are lots of them out there, this is > actually one of the reasons I chose that particular function (to compare > my solution against other, proven solutions). > > Starting with a blank slate, I did something along the lines of: > > class _TypeCheckedFunction(): > def __init__(self, decoratedfunction): > self._decoratedfunction = decoratedfunction > > def __call__(self, *args, **kwargs): > [...] Actual checking > > def typecheck(wrappedfunction): > checkfunction = _TypeCheckedFunction(wrappedfunction) > functools.update_wrapper(checkfunction, wrappedfunction) > return checkfunction > > And decorate my methods like > > @typecheck > def setbar(self, bar: str): > > This works somewhat. The problem is, however, when the method is > actually called. This is what happens: > > 1. The decorator is called upon import of the decorated class. It > creates a _TypeCheckedFunction(setbar) object. > 2. When setbar is actually called (blubb.setbar("fooobar")), the > __call__ method of the previously created _TypeCheckedFunction is invoked. > 3. When trying to call self._decoratedfunction from within that object, > this fails: "self" is missing! self._decoratedfunction is only the > *function*, not the bound function of the object that contains setbar(). > Therefore I cannot proceed here. > > Solutions that I have seen working usually consist of two functions > wrapped in each other, but I do not know why the additional introduction > of a class makes everything fail. > > Can someone please enlighten me? > > Best regards, > Henrik Not sure how that could work in general, what does "bar: str" should do? Is that a dictionary? Anyway there is already an implementation if you're interested for type checking: http://oakwinter.com/code/typecheck/ You can have a look at how they do it. From hfaber at invalid.net Mon Dec 12 08:45:11 2011 From: hfaber at invalid.net (Henrik Faber) Date: Mon, 12 Dec 2011 14:45:11 +0100 Subject: Confusion about decorators References: Message-ID: On 12.12.2011 14:37, Andrea Crotti wrote: > On 12/12/2011 01:27 PM, Henrik Faber wrote: >> Hi group, >> >> I'm a bit confused regarding decorators. Recently started playing with >> them with Python3 and wanted (as an excercise) to implement a simple >> type checker first: I know there are lots of them out there, this is >> actually one of the reasons I chose that particular function (to compare >> my solution against other, proven solutions). > > Not sure how that could work in general, what does "bar: str" should do? > Is that a dictionary? No. It's PEP 3107 function annotations. > Anyway there is already an implementation if you're interested for type > checking: > http://oakwinter.com/code/typecheck/ *sigh* no, not really -- this is exactly why I wrote "I know there are lots of them out there". I've actually seen and run http://code.activestate.com/recipes/577299-method-signature-type-checking-decorator-for-pytho/ However, this doesn't do it for me -- I want to know why my solution fails, not just use some other solution without really understanding it. I really would like to understand what's going on. I'm especially puzzled about the fact that in my solution, __call__ is called with only the method's arguments (i.e. "fooobar") in my example instead of two arguments (self, "fooobar"). Best regards, Henrik From arnodel at gmail.com Mon Dec 12 08:45:20 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 12 Dec 2011 13:45:20 +0000 Subject: Confusion about decorators In-Reply-To: References: Message-ID: On 12 December 2011 13:27, Henrik Faber wrote: > Hi group, > > I'm a bit confused regarding decorators. Recently started playing with > them with Python3 and wanted (as an excercise) to implement a simple > type checker first: I know there are lots of them out there, this is > actually one of the reasons I chose that particular function (to compare > my solution against other, proven solutions). > > Starting with a blank slate, I did something along the lines of: > > class _TypeCheckedFunction(): > ? ? ? ?def __init__(self, decoratedfunction): > ? ? ? ? ? ? ? ?self._decoratedfunction = decoratedfunction > > ? ? ? ?def __call__(self, *args, **kwargs): > ? ? ? ? ? ? ? ?[...] Actual checking > > def typecheck(wrappedfunction): > ? ? ? ?checkfunction = _TypeCheckedFunction(wrappedfunction) > ? ? ? ?functools.update_wrapper(checkfunction, wrappedfunction) > ? ? ? ?return checkfunction > > And decorate my methods like > > ? ? ? ?@typecheck > ? ? ? ?def setbar(self, bar: str): > > This works somewhat. The problem is, however, when the method is > actually called. This is what happens: > > 1. The decorator is called upon import of the decorated class. It > creates a _TypeCheckedFunction(setbar) object. > 2. When setbar is actually called (blubb.setbar("fooobar")), the > __call__ method of the previously created _TypeCheckedFunction is invoked. > 3. When trying to call self._decoratedfunction from within that object, > this fails: "self" is missing! self._decoratedfunction is only the > *function*, not the bound function of the object that contains setbar(). > Therefore I cannot proceed here. > > Solutions that I have seen working usually consist of two functions > wrapped in each other, but I do not know why the additional introduction > of a class makes everything fail. > > Can someone please enlighten me? You can (need to?) use the descriptor protocol to deal with methods. from functools import partial class _TypeCheckedFunction(): def __init__(self, decoratedfunction): self._decoratedfunction = decoratedfunction def __call__(self, *args, **kwargs): [...] Actual checking def __get__(self, obj, objtype): return partial(self, obj) (Untested) HTH -- Arnaud From hfaber at invalid.net Mon Dec 12 08:52:21 2011 From: hfaber at invalid.net (Henrik Faber) Date: Mon, 12 Dec 2011 14:52:21 +0100 Subject: Confusion about decorators References: Message-ID: On 12.12.2011 14:45, Arnaud Delobelle wrote: >> Can someone please enlighten me? > > You can (need to?) use the descriptor protocol to deal with methods. > > from functools import partial [...] > def __get__(self, obj, objtype): > return partial(self, obj) Whoa. This is absolutely fantastic, it now works as expected (I get a reference to "self"). I am very amazed -- I've been programming Python for about 5 years now and have never even come close to something as a "descriptor protocol". Python never ceases to amaze me. Do you have any beginners guide how this works? The Pydoc ("Data Model") is comprehensive, but I really don't know where to start to look. Still amazed! Best regards, Henrik From arnodel at gmail.com Mon Dec 12 09:01:31 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 12 Dec 2011 14:01:31 +0000 Subject: Confusion about decorators In-Reply-To: References: Message-ID: On 12 December 2011 13:52, Henrik Faber wrote: > On 12.12.2011 14:45, Arnaud Delobelle wrote: > >>> Can someone please enlighten me? >> >> You can (need to?) use the descriptor protocol to deal with methods. >> >> from functools import partial > [...] >> ? ? ? ?def __get__(self, obj, objtype): >> ? ? ? ? ? ? ? ?return partial(self, obj) > > Whoa. This is absolutely fantastic, it now works as expected (I get a > reference to "self"). > > I am very amazed -- I've been programming Python for about 5 years now > and have never even come close to something as a "descriptor protocol". > Python never ceases to amaze me. Do you have any beginners guide how > this works? The Pydoc ("Data Model") is comprehensive, but I really > don't know where to start to look. Well, I've been using Python for 10 years :) The best reference I know is: http://users.rcn.com/python/download/Descriptor.htm -- Arnaud From jyoung79 at kc.rr.com Mon Dec 12 09:49:06 2011 From: jyoung79 at kc.rr.com (jyoung79 at kc.rr.com) Date: Mon, 12 Dec 2011 14:49:06 +0000 Subject: subprocess question Message-ID: <20111212144906.3F3ZX.24725.root@cdptpa-web36-z01.mail.rr.com> >>>>> import subprocess >>>>> p = subprocess.Popen(['du', '-sh'], cwd='/Users/jay/.Trash/', >> stdout=subprocess.PIPE) >>>>> out, err = p.communicate() >>>>> out >> ' 11M\t.\n' > You might prefer to use subprocess.check_output(); it slightly > simplifies your code: > http://docs.python.org/library/subprocess.html#subprocess.check_output >> And another question - why can't I use the tilde as a shortcut to the home >> directory? > Because ~ is interpolated by the shell and `subprocess` does not use > the shell by default for reasons that include efficiency and security. > You can expand ~ yourself using os.path.expanduser(): > http://docs.python.org/library/os.path.html#os.path.expanduser > Alternatively, you can opt to use the shell by passing shell=True as > an argument. > Cheers, > Chris Thank you very much, Chris and Andrew, for your help. I appreciate it! :-) Jay From nobody at nowhere.com Mon Dec 12 10:16:58 2011 From: nobody at nowhere.com (Nobody) Date: Mon, 12 Dec 2011 15:16:58 +0000 Subject: subprocess question References: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> Message-ID: On Sun, 11 Dec 2011 22:02:23 -0800, Chris Rebert wrote: >>>>> p = subprocess.Popen(['du', '-sh'], cwd='/Users/jay/.Trash/', stdout=subprocess.PIPE) > Alternatively, you can opt to use the shell by passing shell=True as > an argument. Except that the OP is talking about a directory passed to the cwd= parameter, rather than as part of the command, and shell= doesn't affect that. But even if the directory was part of the command, just setting shell=True won't work. On Unix (including MacOSX) the call: subprocess.Popen(['du', '-sh'], shell=True) is equivalent to: subprocess.Popen(['/bin/sh', '-c', 'du', '-sh'], shell=False) This will result in the shell executing "du" with no arguments. The variable expansion "$1" will evaluate to "-sh", but that's meaningless as "$1" doesn't occur in the argument to "-c". The combination of using a list for the "args" parameter along with shell=True is rarely useful on Unix. And using a string for the "args" parameter introduces all of the associated reliability and security issues. The situation is different on Windows, where list/string and shell= are orthogonal. A list is always converted to a string according to the rules by which MSVCRT parses the command line into argv[]. Then, if shell=True, "cmd.exe /c " is prepended to the string (actually, the value of the COMSPEC environment variable is used in place of cmd.exe if it is defined). Setting shell=True allows the "program" to be any file with a registered extension, rather than just an executable. From hfaber at invalid.net Mon Dec 12 10:35:19 2011 From: hfaber at invalid.net (Henrik Faber) Date: Mon, 12 Dec 2011 16:35:19 +0100 Subject: Confusion about decorators References: Message-ID: On 12.12.2011 15:01, Arnaud Delobelle wrote: >> I am very amazed -- I've been programming Python for about 5 years now >> and have never even come close to something as a "descriptor protocol". >> Python never ceases to amaze me. Do you have any beginners guide how >> this works? The Pydoc ("Data Model") is comprehensive, but I really >> don't know where to start to look. > > Well, I've been using Python for 10 years :) The best reference I know is: > > http://users.rcn.com/python/download/Descriptor.htm Everyone starts out as a Padawan and I am no exception :-) Maybe five years from now I'll also have made my way to be a Python Jedi and also shake the ins and outs of descriptors out of my sleeve :-) But I can only repeat myself: Python is such an exceptional language, the more and more I know about it, the more I fall in love! Fantastic. I wish we had these types of language when I was a kid! Best regards and thanks again, Henrik From tjreedy at udel.edu Mon Dec 12 10:36:36 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 10:36:36 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <9klr6aF2ovU1@mid.individual.net> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <9klr6aF2ovU1@mid.individual.net> Message-ID: On 12/12/2011 3:09 AM, Gregory Ewing wrote: > people who don't become programmers, I suspect they never > have much use for remainders in everyday life. Huh? Funny you should say 'everyday'. It is now 10 o'clock. In 20 hours, it will be (10+20) % 12 == 6 o'clock. It is now day 1 of the week. In 9 days it will be day (1+9) % 7 == 3 of the week. Mental calculations are helped by the fact that (a+b) % c == a%c + b%c, so that would actually be 1+2==3. Timekeeping is mostly remaindering, slightly obscured by using 12 instead of 0. -- Terry Jan Reedy From tjreedy at udel.edu Mon Dec 12 10:39:48 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 10:39:48 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: Message-ID: On 12/12/2011 5:59 AM, Jussi Piitulainen wrote: > Past experience in mathematics newsgroups tells me > that some mathematicians do not accept the existence of any remainder > operator at all. Even though they carry hour/minute/second remindering devices on their bodies and put year/month/day remaindering devices on their wall? 'Twould be strange indeed! -- Terry Jan Reedy From tjreedy at udel.edu Mon Dec 12 10:52:25 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 10:52:25 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4EE5C576.2000307@gmail.com> References: <4EE5C576.2000307@gmail.com> Message-ID: On 12/12/2011 4:12 AM, Eelco Hoogendoorn wrote: >> The above examples are seldom needed in Python because we have one >> general method to repeatedly split a sequence into head and tail. > >> it = iter(iterable) # 'it' now represents the sequenced iterable >> head = next(it) # 'it' now represents the tail after removing the head > >> In other words, next(it) encompasses all of your examples and many more. >> Because 'it' is mutated to represent the tail, it does not need to be >> rebound and therefore is not. > > > The question in language design is never 'could we do these things > before'. The answer is obvious: yes our CPUs are turing complete; we can > do anything. The question is; how would we like to do them? > > So do you think the new head/tail unpacking features in python 3 are > entirely uncalled for? No, *target unpacking (singular) is quite useful in specialized cases. But it is not specifically head/tail unpacking. >>> a,*b,c = 1,2,3,4,5,6 >>> a,b,c (1, [2, 3, 4, 5], 6) >>> *a,b,c = 1,2,3,4,5 >>> a,b,c ([1, 2, 3], 4, 5) > I personally quite like them, but I would like them to be more general. It already is. The *target can be anywhere in the sequence. -- Terry Jan Reedy From nicholas.dokos at hp.com Mon Dec 12 10:55:44 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Mon, 12 Dec 2011 10:55:44 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: Message from Terry Reedy of "Mon, 12 Dec 2011 10:36:36 EST." References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <9klr6aF2ovU1@mid.individual.net> Message-ID: <26632.1323705344@alphaville.dokosmarshall.org> Terry Reedy wrote: > calculations are helped by the fact that (a+b) % c == a%c + b%c, so As long as we understand that == here does not mean "equal", only "congruent modulo c", e.g try a = 13, b = 12, c = 7. Nick From tylershemwell01 at gmail.com Mon Dec 12 10:56:49 2011 From: tylershemwell01 at gmail.com (Tyla (Tyler Shemwell) G-ESTEEM) Date: Mon, 12 Dec 2011 07:56:49 -0800 (PST) Subject: G-ESTEEM-PEOPLE WHO EDUCATED THEMSELVES & EVOLVED INTO THE MOST HAPPY AMERICANS USUALLY RED NECKS & PRO-BLACKS (STAY BOW-LEGGED & THICK). Message-ID: G-ESTEEM-PEOPLE WHO EDUCATED THEMSELVES & EVOLVED INTO THE MOST HAPPY AMERICANS USUALLY RED NECKS & PRO-BLACKS (STAY BOW-LEGGED & THICK). (Article w/ pic of President Obama & ex-Presidents.Bush,Bill Clinton,Bush Sr. & Jimmy Carter). STAY BOW-LEGGED & THICK G-ESTEEM-A tenacious confidence;mental toughness "If god is with me than who can be against me? I can do anything. G- ESTEEM Go hard,we dying soon. G-ESTEEM "The game is in belief" /I'm i doing too much/Or losing my touch/ Text 3060402 to 69937 for 'Night Time by Tyler Shemwell http://binged.it/v9LWsf STAY BOW-LEGGED & THICK Teach me it's more to being a man than feeling up your thigh. For Breast Cancer. G-ESTEEM (w/ pic of President Obama & Wife) http://tyler-shemwell.blogspot.com/2010/11/esteem.html ELECTION 2012 (For Freedom & Watch OUT FOR G-STINKY BAD PEOPLE) From tylershemwell01 at gmail.com Mon Dec 12 10:59:06 2011 From: tylershemwell01 at gmail.com (Tyla (Tyler Shemwell) G-ESTEEM) Date: Mon, 12 Dec 2011 07:59:06 -0800 (PST) Subject: Chew, they smelling like JUMBO BEAN BANQUET in there & they humming in that pic too. By G-ESTEEM Message-ID: They Smelling Like Naked Running Butt in that room. By G-ESTEEM Chew,they smelling like JUMBO BEAN BANQUET in there & they humming in that pic too below... http://www.myspace.com/tylershemwell/blog/544928378 /I'm i doing too much/Or losing my touch/ Text 3060402 to 69937 for 'Night Time by Tyler Shemwell http://binged.it/v9LWsf STAY BOW-LEGGED & THICK. G-ESTEEM They house so funky the pet bird even has the smell From dmitrey15 at gmail.com Mon Dec 12 11:00:07 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Mon, 12 Dec 2011 08:00:07 -0800 (PST) Subject: multiprocessing module question Message-ID: hi all, suppose I have a func F, list [args1,args2,args3,...,argsN] and want to obtain r_i = F(args_i) in parallel mode. My difficulty is: if F returns not None, than I should break calculations, and I can't dig in multiprocessing module documentation how to do it. Order doesn't matter for me (I have to find 1st suitable, where result of F is not None) Could anyone scribe me an example? Thank you in advance, D. From arnodel at gmail.com Mon Dec 12 11:10:00 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 12 Dec 2011 16:10:00 +0000 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE5C576.2000307@gmail.com> Message-ID: On 12 December 2011 15:52, Terry Reedy wrote: > No, *target unpacking (singular) is quite useful in specialized cases. But > it is not specifically head/tail unpacking. > >>>> a,*b,c = 1,2,3,4,5,6 >>>> a,b,c > (1, [2, 3, 4, 5], 6) >>>> *a,b,c = 1,2,3,4,5 >>>> a,b,c > ([1, 2, 3], 4, 5) > >> I personally quite like them, but I would like them to be more general. > > > It already is. The *target can be anywhere in the sequence. > > -- > Terry Jan Reedy You can even have nested sequences! >>> a, (b, *c), *d = 1, "two", 3, 4 -- Arnaud From info at wingware.com Mon Dec 12 11:13:40 2011 From: info at wingware.com (Wingware) Date: Mon, 12 Dec 2011 11:13:40 -0500 Subject: Wing IDE 4.1.2 released Message-ID: <4EE62834.4080506@wingware.com> Hi, Wingware has released version 4.1.2 of Wing IDE, an integrated development environment designed specifically for the Python programming language. Wing IDE is a cross-platform Python IDE that provides a professional code editor with vi, emacs, and other key bindings, auto-completion, call tips, refactoring, context-aware auto-editing, a powerful graphical debugger, version control, unit testing, search, and many other features. **Changes in Version 4.1.2** Highlights of this release include: * Added Create New Package to the Project context menu * Added Open Files tool for navigating to and closing open files * Added new code selection commands in the Edit > Select menu to select current/next/previous statement or scope * Many auto-editing, auto-indent, and auto-completion improvements * Fixed evaluation in shells of files with a coding comment * Several VI mode fixes * About 15 other bug fixes and minor improvements Complete change log: http://wingware.com/pub/wingide/4.1.2/CHANGELOG.txt **New Features in Version 4** Version 4 adds the following new major features: * Refactoring -- Rename/move symbols, extract to function/method, and introduce variable * Find Uses -- Find all points of use of a symbol * Auto-Editing -- Reduce typing burden by auto-entering expected code * Diff/Merge -- Graphical file and repository comparison and merge * Django Support -- Debug Django templates, run Django unit tests, and more * matplotlib Support -- Maintains live-updating plots in shell and debugger * Simplified Licensing -- Includes all OSes and adds Support+Upgrades subscriptions Details on licensing changes: http://wingware.com/news/2011-02-16 **About Wing IDE** Wing IDE is an integrated development environment designed specifically for the Python programming language. It provides powerful editing, testing, and debugging features that help reduce development and debugging time, cut down on coding errors, and make it easier to understand and navigate Python code. Wing IDE can be used to develop Python code for web, GUI, and embedded scripting applications. Wing IDE is available in three product levels: Wing IDE Professional is the full-featured Python IDE, Wing IDE Personal offers a reduced feature set at a low price, and Wing IDE 101 is a free simplified version designed for teaching beginning programming courses with Python. Version 4.0 of Wing IDE Professional includes the following major features: * Professional quality code editor with vi, emacs, and other keyboard personalities * Code intelligence for Python: Auto-completion, call tips, find uses, goto-definition, error indicators, refactoring, context-aware auto-editing, smart indent and rewrapping, and source navigation * Advanced multi-threaded debugger with graphical UI, command line interaction, conditional breakpoints, data value tooltips over code, watch tool, and externally launched and remote debugging * Powerful search and replace options including keyboard driven and graphical UIs, multi-file, wild card, and regular expression search and replace * Version control integration for Subversion, CVS, Bazaar, git, Mercurial, and Perforce * Integrated unit testing with unittest, nose, and doctest frameworks * Django support: Debugs Django templates, provides project setup tools, and runs Django unit tests * Many other features including project manager, bookmarks, code snippets, diff/merge tool, OS command integration, indentation manager, PyLint integration, and perspectives * Extremely configurable and may be extended with Python scripts * Extensive product documentation and How-Tos for Django, matplotlib, Plone, wxPython, PyQt, mod_wsgi, Autodesk Maya, and many other frameworks Please refer to http://wingware.com/wingide/features for a detailed listing of features by product level. System requirements are Windows 2000 or later, OS X 10.3.9 or later (requires X11 Server), or a recent Linux system (either 32 or 64 bit). Wing IDE supports Python versions 2.0.x through 3.2.x and Stackless Python. For more information, see the http://wingware.com/ **Downloads** Wing IDE Professional and Wing IDE Personal are commercial software and require a license to run. A free trial can be obtained directly from the product when launched. Wing IDE Pro -- Full-featured product: http://wingware.com/downloads/wingide/4.1 Wing IDE Personal -- A simplified IDE: http://wingware.com/downloads/wingide-personal/4.1 Wing IDE 101 -- For teaching with Python: http://wingware.com/downloads/wingide-101/4.1 **Purchasing and Upgrading** Wing 4.x requires an upgrade for Wing IDE 2.x and 3.x users at a cost of 1/2 the full product pricing. Upgrade a license: https://wingware.com/store/upgrade Purchase a new license: https://wingware.com/store/purchase Optional Support+Upgrades subscriptions are available for expanded support coverage and free upgrades to new major releases: http://wingware.com/support/agreement Thanks! -- The Wingware Team Wingware | Python IDE Advancing Software Development www.wingware.com From arnodel at gmail.com Mon Dec 12 11:15:05 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Mon, 12 Dec 2011 16:15:05 +0000 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <9klr6aF2ovU1@mid.individual.net> Message-ID: On 12 December 2011 15:36, Terry Reedy wrote: > Huh? Funny you should say 'everyday'. It is now 10 o'clock. In 20 hours, it > will be (10+20) % 12 == 6 o'clock. It is now day 1 of the week. In 9 days it > will be day (1+9) % 7 == 3 of the week. Mental calculations are helped by > the fact that (a+b) % c == a%c + b%c You mean (a + b) % c == (a%c + b%c) % c :) -- Arnaud From rosuav at gmail.com Mon Dec 12 11:16:08 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 03:16:08 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <26632.1323705344@alphaville.dokosmarshall.org> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <9klr6aF2ovU1@mid.individual.net> <26632.1323705344@alphaville.dokosmarshall.org> Message-ID: On Tue, Dec 13, 2011 at 2:55 AM, Nick Dokos wrote: > Terry Reedy wrote: >> calculations are helped by the fact that (a+b) % c == a%c + b%c, so > > As long as we understand that == here does not mean "equal", only > "congruent modulo c", e.g try a = 13, b = 12, c = 7. This is the basis of the grade-school "casting out nines" method of checking arithmetic. Set c=9 and you can calculate N%c fairly readily (digit sum - I'm assuming here that the arithmetic is being done in decimal); the sum of the remainders should equal the remainder of the sum, but there's the inherent assumption that if the remainders sum to something greater than nine, you digit-sum it to get the true remainder. (Technically the sum of the digits of a base-10 number is not the same as that number mod 9, but if you accept that 0 == 9, it works fine.) ChrisA From rosuav at gmail.com Mon Dec 12 11:19:39 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 03:19:39 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <9klr6aF2ovU1@mid.individual.net> Message-ID: On Tue, Dec 13, 2011 at 3:15 AM, Arnaud Delobelle wrote: > > You mean (a + b) % c == (a%c + b%c) % c > > :) It's just integer wraparound. Modulo 9 is the same as "render this number in base 9 and take the last digit" (and printing a number in base 9 would normally be done with mod 9 division), and most people can wrap their heads around the way an odometer wraps around. ChrisA From robert.kern at gmail.com Mon Dec 12 11:27:40 2011 From: robert.kern at gmail.com (Robert Kern) Date: Mon, 12 Dec 2011 16:27:40 +0000 Subject: I love the decorator in Python!!! In-Reply-To: <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> Message-ID: On 12/12/11 3:36 AM, alex23 wrote: > On Dec 9, 8:08 pm, Robert Kern wrote: >> On 12/9/11 5:02 AM, alex23 wrote: >>> The 3rd party 'decorator' module takes care of issues like docstrings >>> & function signatures. I'd really like to see some of that >>> functionality in the stdlib though. >> >> Much of it is: >> >> http://docs.python.org/library/functools#functools.update_wrapper > > Ah, cheers :) Is that a recent addition? The lack of note makes it > seem like it was there from the beginning? The module was added in Python 2.5 as noted at the top of the page. -- 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 From massi_srb at msn.com Mon Dec 12 11:30:54 2011 From: massi_srb at msn.com (Massi) Date: Mon, 12 Dec 2011 08:30:54 -0800 (PST) Subject: PyDev and multithreaded application debug Message-ID: <3f193cae-deab-4d3f-a7d6-01ae4611515d@t16g2000vba.googlegroups.com> Hi everyone, I've just started to use pydev to develop my python application and I'm encountering some problems to debug it. The application I'm dealing with is a multithreaded application; when I try to debug it with pydev, it seems not to be able to handle the execution of multiple threads. The problem is that when the execution reaches a breakpoint in one of the threads, all the other ones don't stop, but they continue their execution. Have I to make some specific settings or am I missing something? Thanks in advance! From robert.kern at gmail.com Mon Dec 12 11:38:00 2011 From: robert.kern at gmail.com (Robert Kern) Date: Mon, 12 Dec 2011 16:38:00 +0000 Subject: What happened to module.__file__? In-Reply-To: <4EE557F6.5040900@mrabarnett.plus.com> References: <4ee54918$0$11091$c3e8da3@news.astraweb.com> <4EE557F6.5040900@mrabarnett.plus.com> Message-ID: On 12/12/11 1:25 AM, MRAB wrote: > On 12/12/2011 00:21, Steven D'Aprano wrote: >> I've just started using a Debian system, instead of the usual RedHat >> based systems I'm used to, and module.__file__ appears to have >> disappeared for some (but not all) modules. >> >> On Fedora: >> >> [steve at orac ~]$ python -E >> Python 2.6.2 (r262:71600, Aug 21 2009, 12:22:21) >> [GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. >>>>> import math >>>>> math.__file__ >> '/usr/lib/python2.6/lib-dynload/mathmodule.so' >> >> >> >> and on Debian squeeze: >> >> steve at runes:~$ python -E >> Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) >> [GCC 4.4.5] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. >>>>> import math >>>>> math.__file__ >> Traceback (most recent call last): >> File "", line 1, in >> AttributeError: 'module' object has no attribute '__file__' >> >> >> What's going on? >> > The documentation for __file__ says: """The __file__ attribute is not > present for C modules that are statically linked into the interpreter; > for extension modules loaded dynamically from a shared library, it is > the pathname of the shared library file.""" > > Interestingly, none of the versions on Windows that I've tried have > that attribute for the math module. Is it platform-dependent? It is build-dependent. Windows builds typically have math and several other stdlib "extension" modules built into the PythonXY.dll . Unix builds typically, but apparently not always, leave mathmodule.so and others as separate extension modules. -- 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 From tinnews at isbd.co.uk Mon Dec 12 11:40:43 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Mon, 12 Dec 2011 16:40:43 +0000 Subject: Documentation for python-evolution - where? References: <3dnfr8-h6a.ln1@chris.zbmc.eu> Message-ID: Chris Angelico wrote: > On Mon, Dec 12, 2011 at 9:28 AM, wrote: > > I'm trying to use the python evolution (as in Gnome Evolution) module > > but I can't find any documetation beyond the odd trivial example and > > the API documentation at http://doc.conduit-project.org/evolution-python/ > > (or similar places presumably). > > One of the downsides of software you don't pay money for is that, all > too often, there's little or no documentation. One of the upsides of > free software is that you can look at the source code. I don't know > anything about Python-Evolution, but if you're prepared to do a bit of > digging, you can probably find what you want here: > > http://git.gnome.org/browse/gnome-python-desktop/tree/evolution/ > Yes, thank you, I couldn't even find that. > (You probably have a copy of the same content on your own hard disk > somewhere, too.) > > Once you figure out what you wanted, do consider sending a patch to > the maintainer(s), improving the docstrings and/or external > documentation, so the next person has an easier task. :) > Actually I'm not sure if it's down to the docstrings because the help available from Python itself stops (not unreasonably) at the interface to the C library code. What I was after (and you have told me where it is) was the functions/methods available from the C library. -- Chris Green From corleone82 at gmail.com Mon Dec 12 11:41:00 2011 From: corleone82 at gmail.com (Juan Perez) Date: Mon, 12 Dec 2011 16:41:00 +0000 Subject: automate commands to an .exe console program through python Message-ID: Hi, I'm new to this mailing list and new in python as well. I need to automate a .exe console program in windows, and send key characters like 'a', 'M' ... I don't need to take the output as is printed in a different text log, but I'll have to send the messages to the program. I had this running in an autoIT script which with only activating cmd window, and with the "send" parameter I had all already done. But I'm having a lot of problems with python, I've tried with process.call, communicate(), os.popen ... but no results. I can't comunicate with the program and even i can't maintain it opened more than a few seconds. If I open a program like notepad.exe it remains opened but when I try to comunicate with the stdin pipe just don't write anything to the program. Someone has any idea about that, Thanks and sorry for my english level, Juan -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Mon Dec 12 11:50:55 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Mon, 12 Dec 2011 11:50:55 -0500 Subject: unittest. customizing tstloaders / discover() In-Reply-To: <8739cqf4so.fsf@jubold.box> References: <87y5uiu1t4.fsf@jubold.box> <8739cqf4so.fsf@jubold.box> Message-ID: Nose is absolutely the way to go for your testing needs. You can put "__test__ = False" in modules or classes to stop test collection. On Mon, Dec 12, 2011 at 5:44 AM, Thomas Bach wrote: > Gelonida N writes: > >> Do I loose anything if using nose. or example can all unit tests / doc >> tests still be run from nose? > > AFAIK you don't loose anything by using nose ? the unittests should all > be found and doctests can be run via `--with-doctest', I never used > doctests though. > > regards > -- > http://mail.python.org/mailman/listinfo/python-list From jpiitula at ling.helsinki.fi Mon Dec 12 11:52:49 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 12 Dec 2011 18:52:49 +0200 Subject: Verbose and flexible args and kwargs syntax References: Message-ID: Terry Reedy writes: > On 12/12/2011 5:59 AM, Jussi Piitulainen wrote: > > > Past experience in mathematics newsgroups tells me > > that some mathematicians do not accept the existence of any remainder > > operator at all. > > Even though they carry hour/minute/second remindering devices on their > bodies and put year/month/day remaindering devices on their wall? > 'Twould be strange indeed! They recognize modular arithmetic but for some reason insist that there is no such _binary operation_. But as I said, I don't understand their concern. (Except the related concern about some programming languages, not Python, where the remainder does not behave well with respect to division.) From hoogendoorn.eelco at gmail.com Mon Dec 12 12:12:10 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 09:12:10 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: > > I personally quite like them, but I would like them to be more general. > > It already is. The *target can be anywhere in the sequence. Im not sure if this is a genuine understanding, or trollish obtuseness. Yes, the target can be anywhere in the sequence. And yes, the resulting list can contain objects of any type, so its very flexible in that regard too. But to relate it to the topic of this thread: no, the syntax does not allow one to select the type of the resulting sequence. It always constructs a list. Yes, we can cast the list to be whatever we want on the next line, but the question is whether this language design can be improved upon. The choice of a list feels arbitrary, adding another line to cast it to something else would be even more verbose, and whats more, there would be serious performance implications if one should seek to apply this pattern to a deque/linkedlist; it would make taking off the head/tail of the list from a constant to a linear operation. That is: >>> head, deque(tail) = somedeque Is better in every way I can think of (readability, consistence, performance) than: >>> head, *tail = somedeque >>> tail = deque(tail) From hoogendoorn.eelco at gmail.com Mon Dec 12 12:29:11 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 09:29:11 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: Message-ID: > They recognize modular arithmetic but for some reason insist that > there is no such _binary operation_. But as I said, I don't understand > their concern. (Except the related concern about some programming > languages, not Python, where the remainder does not behave well with > respect to division.) They might not be willing to define it, but as soon as we programmers do, well, we did. Having studied the contemporary philosophy of mathematics, their concern is probably that in their minds, mathematics is whatever some dead guy said it was, and they dont know of any dead guy ever talking about a modulus operation, so therefore it 'does not exist'. Whatever you want to call the concept we are talking about, or whether you care to talk about it at all, it is most certainly a binary operation, since there are two arguments involved. There is no way around that. From gheskett at wdtv.com Mon Dec 12 12:46:07 2011 From: gheskett at wdtv.com (gene heskett) Date: Mon, 12 Dec 2011 12:46:07 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <26632.1323705344@alphaville.dokosmarshall.org> Message-ID: <201112121246.07854.gheskett@wdtv.com> On Monday, December 12, 2011 12:44:27 PM Chris Angelico did opine: > On Tue, Dec 13, 2011 at 2:55 AM, Nick Dokos wrote: > > Terry Reedy wrote: > >> calculations are helped by the fact that (a+b) % c == a%c + b%c, so > > > > As long as we understand that == here does not mean "equal", only > > "congruent modulo c", e.g try a = 13, b = 12, c = 7. > > This is the basis of the grade-school "casting out nines" method of > checking arithmetic. Set c=9 and you can calculate N%c fairly readily > (digit sum - I'm assuming here that the arithmetic is being done in > decimal); the sum of the remainders should equal the remainder of the > sum, but there's the inherent assumption that if the remainders sum to > something greater than nine, you digit-sum it to get the true > remainder. > > (Technically the sum of the digits of a base-10 number is not the same > as that number mod 9, but if you accept that 0 == 9, it works fine.) > > ChrisA And that is precisely the reason I have failed to understand why the 1-10 decimal system seems to have hung on for several hundred years when it is clearly broken. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: Grub first, then ethics. -- Bertolt Brecht From nicholas.dokos at hp.com Mon Dec 12 12:58:09 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Mon, 12 Dec 2011 12:58:09 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: Message from Jussi Piitulainen of "12 Dec 2011 18:52:49 +0200." References: Message-ID: <29931.1323712689@alphaville.dokosmarshall.org> Jussi Piitulainen wrote: > Terry Reedy writes: > > On 12/12/2011 5:59 AM, Jussi Piitulainen wrote: > > > > > Past experience in mathematics newsgroups tells me > > > that some mathematicians do not accept the existence of any remainder > > > operator at all. > > > > Even though they carry hour/minute/second remindering devices on their > > bodies and put year/month/day remaindering devices on their wall? > > 'Twould be strange indeed! > > They recognize modular arithmetic but for some reason insist that > there is no such _binary operation_. But as I said, I don't understand > their concern. (Except the related concern about some programming > languages, not Python, where the remainder does not behave well with > respect to division.) They are probably arguing that it's uniquely defined only on ZxN and that there are different conventions to extend it to ZxZ (the programming languages problem that you allude to above - although I don't know what you mean by "does not behave well wrt division"). See http://en.wikipedia.org/wiki/Remainder If you choose one convention and stick to it, it becomes a well-defined binary operation. C99 goes one way, python goes a different way (and mathematics textbooks generally go a third way) and they are all happy, as long as they don't try to talk to each other (e.g., porting C99 programs to python unthinkingly leads to trouble - duh). It was implementation dependent in old C (whatever the hardware would give you), which predictably - with 20-20 hindsight - turned out to be a Very Bad Idea. Nick PS Z = integers, N = non-negative integers From nicholas.dokos at hp.com Mon Dec 12 13:02:13 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Mon, 12 Dec 2011 13:02:13 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: Message from gene heskett of "Mon, 12 Dec 2011 12:46:07 EST." <201112121246.07854.gheskett@wdtv.com> References: <26632.1323705344@alphaville.dokosmarshall.org> <201112121246.07854.gheskett@wdtv.com> Message-ID: <30011.1323712933@alphaville.dokosmarshall.org> gene heskett wrote: > On Monday, December 12, 2011 12:44:27 PM Chris Angelico did opine: > > > On Tue, Dec 13, 2011 at 2:55 AM, Nick Dokos > wrote: > > > Terry Reedy wrote: > > >> calculations are helped by the fact that (a+b) % c == a%c + b%c, so > > > > > > As long as we understand that == here does not mean "equal", only > > > "congruent modulo c", e.g try a = 13, b = 12, c = 7. > > > > This is the basis of the grade-school "casting out nines" method of > > checking arithmetic. Set c=9 and you can calculate N%c fairly readily > > (digit sum - I'm assuming here that the arithmetic is being done in > > decimal); the sum of the remainders should equal the remainder of the > > sum, but there's the inherent assumption that if the remainders sum to > > something greater than nine, you digit-sum it to get the true > > remainder. > > > > (Technically the sum of the digits of a base-10 number is not the same > > as that number mod 9, but if you accept that 0 == 9, it works fine.) > > > > ChrisA > > And that is precisely the reason I have failed to understand why the 1-10 It's not clear from the above what you mean by "that is presicely the reason": what is "that"? > decimal system seems to have hung on for several hundred years when it is > clearly broken. > "broken" how? Thanks, Nick From d at davea.name Mon Dec 12 13:04:34 2011 From: d at davea.name (Dave Angel) Date: Mon, 12 Dec 2011 13:04:34 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <201112121246.07854.gheskett@wdtv.com> References: <26632.1323705344@alphaville.dokosmarshall.org> <201112121246.07854.gheskett@wdtv.com> Message-ID: <4EE64232.8080301@davea.name> On 12/12/2011 12:46 PM, gene heskett wrote: > On Monday, December 12, 2011 12:44:27 PM Chris Angelico did opine: > >> This is the basis of the grade-school "casting out nines" method of >> checking arithmetic. Set c=9 and you can calculate N%c fairly readily >> (digit sum - I'm assuming here that the arithmetic is being done in >> decimal); the sum of the remainders should equal the remainder of the >> sum, but there's the inherent assumption that if the remainders sum to >> something greater than nine, you digit-sum it to get the true >> remainder. >> >> (Technically the sum of the digits of a base-10 number is not the same >> as that number mod 9, but if you accept that 0 == 9, it works fine.) >> >> ChrisA > And that is precisely the reason I have failed to understand why the 1-10 > decimal system seems to have hung on for several hundred years when it is > clearly broken. > I assume this was facetious, but in case not, I'd point out that any other number base will have similar modulo characteristics, except for base 2, where all numbers are congruent modulo 1, so it doesn't do much for checking values. For example, if you were using a number system of base 8, you could do "casting out sevens" by adding the digits together. -- DaveA From ian.g.kelly at gmail.com Mon Dec 12 13:09:04 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 11:09:04 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> Message-ID: On Mon, Dec 12, 2011 at 5:21 AM, Eelco wrote: > You cannot; only constructors modelling a sequence or a dict, and > only > in that order. Is that rule clear enough? The dict constructor can receive either a sequence or a mapping, so if I write this: def func(a, b, dict(c)): what will I get? Probably I would want the equivalent of: def func(a, b, **c): but you seem to be saying that I would actually get the equivalent of this: def func(a, b, *c): c = dict(c) Cheers, Ian From jpiitula at ling.helsinki.fi Mon Dec 12 13:09:48 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 12 Dec 2011 20:09:48 +0200 Subject: Verbose and flexible args and kwargs syntax References: Message-ID: Eelco writes: > > They recognize modular arithmetic but for some reason insist that > > there is no such _binary operation_. But as I said, I don't understand > > their concern. (Except the related concern about some programming > > languages, not Python, where the remainder does not behave well with > > respect to division.) > > They might not be willing to define it, but as soon as we programmers > do, well, we did. > > Having studied the contemporary philosophy of mathematics, their > concern is probably that in their minds, mathematics is whatever some > dead guy said it was, and they dont know of any dead guy ever talking > about a modulus operation, so therefore it 'does not exist'. > > Whatever you want to call the concept we are talking about, or whether > you care to talk about it at all, it is most certainly a binary > operation, since there are two arguments involved. There is no way > around that. Yes, I think you nailed it. But I guess I'll still be confused the next time I meet one of them. Happens to me. :) From hoogendoorn.eelco at gmail.com Mon Dec 12 13:17:11 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 10:17:11 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> Message-ID: <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> On Dec 12, 7:09?pm, Ian Kelly wrote: > On Mon, Dec 12, 2011 at 5:21 AM, Eelco wrote: > > You cannot; only constructors modelling a sequence or a dict, and > > only > > in that order. Is that rule clear enough? > > The dict constructor can receive either a sequence or a mapping, so if > I write this: > > ? ? ? ? def func(a, b, dict(c)): > > what will I get? ?Probably I would want the equivalent of: > > ? ? ? ? def func(a, b, **c): > > but you seem to be saying that I would actually get the equivalent of this: > > ? ? ? ? def func(a, b, *c): > ? ? ? ? ? ? c = dict(c) > > Cheers, > Ian Im not sure if I was clear on that, but I dont care what the constructors accept; I meant to overload on the concept the underlying type models. Dicts model a mapping, lists/tuples model a sequence. MI deriving from both these models is illegal anyway, so one can unambigiously overload on that trait. The syntax only superficially resembles 'call the dict constructor with the object passed into kwargs'. What its supposed to mean is exactly the same as **kwargs, but with added flexibility. From ian.g.kelly at gmail.com Mon Dec 12 13:35:28 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 11:35:28 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> Message-ID: On Mon, Dec 12, 2011 at 11:17 AM, Eelco wrote: > Im not sure if I was clear on that, but I dont care what the > constructors accept; I meant to overload on the concept the underlying > type models. Dicts model a mapping, lists/tuples model a sequence. MI > deriving from both these models is illegal anyway, so one can > unambigiously overload on that trait. False. >>> from collections import * >>> class Foo(Sequence, Mapping): ... def __init__(self, items): ... self._items = items ... def __getitem__(self, item): ... return self._items[item] ... def __len__(self): ... return len(self._items) ... >>> foo1 = Foo(range(5, 10)) >>> foo2 = Foo({'one': 1, 'two': 2}) >>> foo1[3] 8 >>> foo2['one'] 1 Or are you saying that only classes specifically derived from list, tuple, or dict should be considered, and custom containers that are not derived from any of those but implement the correct protocols should be excluded? If so, that sounds less than ideal. Cheers, Ian From hoogendoorn.eelco at gmail.com Mon Dec 12 13:51:24 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 10:51:24 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> Message-ID: > False. I stand corrected. > Or are you saying that only classes specifically derived from list, > tuple, or dict should be considered, and custom containers that are > not derived from any of those but implement the correct protocols > should be excluded? ?If so, that sounds less than ideal. That might be a desirable constraint from an implementational/ performance aspect anyway, but I agree, less than ideal. Either way, its not hard to add some detail to the semantics to allow all this. Even this function definition: def func(Foo(args), Foo(kwargs)) ...could even be defined unambigiously by overloading first on base type, and if that does not uniquely determine the args and kwargs, fall back on positionality, so that: def func(Foo(args), dict(kwargs)) def func(list(args), Foo(kwargs)) would be uniquely defined as well. From hoogendoorn.eelco at gmail.com Mon Dec 12 14:05:26 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 11:05:26 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> Message-ID: <406c1cbb-2a8c-40d9-a76e-7329ebaaa2cd@l24g2000yqm.googlegroups.com> To get back on topic a little bit, lets get back to the syntax of all this: I think we all agree that recycling the function call syntax is less than ideal, since while it works in special contexts like a function signature, its symmetric counterpart inside a function call already has the meaning of a function call. In general, we face the problem of specifying metadata about a variable, or a limited form of type constraint. What we want is similar to function annotations in python 3; in line with that, we could have more general variable annotations. With an important conceptual distinction; function annotations are meaningless to python, but the annotations I have in mind should modify semantics directly. However, its still conceptually close enough that we might want to use the colon syntax here too. To distinguish it from function annotations, we could use a double colon (double colon is an annotation with non-void semantics; quite a simple rule); or to maintain an historic link with the existing packing/unpacking syntax, we could look at an augmented form of the asteriks notation. For instance: def func(list*args, dict*kwargs) <- list-of-args, dict-of-kwargs def func(args::list, kwargs::dict) <- I like the readability of this one even better; args-list and kwargs-dict And: head, deque*tail = somedeque head, tail::deque = somedeque Or some variants thereof From hoogendoorn.eelco at gmail.com Mon Dec 12 14:20:51 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 11:20:51 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> <406c1cbb-2a8c-40d9-a76e-7329ebaaa2cd@l24g2000yqm.googlegroups.com> Message-ID: <79502f0f-bf90-4629-96cb-e50ca42125c9@r6g2000yqr.googlegroups.com> On Dec 12, 8:05?pm, Eelco wrote: > To get back on topic a little bit, lets get back to the syntax of all > this: I think we all agree that recycling the function call syntax is > less than ideal, since while it works in special contexts like a > function signature, its symmetric counterpart inside a function call > already has the meaning of a function call. > > In general, we face the problem of specifying metadata about a > variable, or a limited form of type constraint. > > What we want is similar to function annotations in python 3; in line > with that, we could have more general variable annotations. With an > important conceptual distinction; function annotations are meaningless > to python, but the annotations I have in mind should modify semantics > directly. However, its still conceptually close enough that we might > want to use the colon syntax here too. To distinguish it from function > annotations, we could use a double colon (double colon is an > annotation with non-void semantics; quite a simple rule); or to > maintain an historic link with the existing packing/unpacking syntax, > we could look at an augmented form of the asteriks notation. > > For instance: > > def func(list*args, dict*kwargs) <- list-of-args, dict-of-kwargs > def func(args::list, kwargs::dict) <- I like the readability of this > one even better; args-list and kwargs-dict > > And: > > head, deque*tail = somedeque > head, tail::deque = somedeque > > Or some variants thereof As for calling functions; calling a function with the content of a collection type rather than the collection as an object itself is a rather weird special case operation I suppose, but we can cover it with the same syntax: def func(args::tuple, kwargs::dict): funccall(args::, kwargs::) <- void type constraint means unpacking, for symmetry with args/kwargs aggregation funccall(::args, ::kwargs) <- I like this better, to emphasize it being 'the other side' of the same coin, and quite close to ** syntax Sequence and Mapping unpacking dont need their own symbols, if things are done like this, since in the function declaration the meaning is clear from the type of the annotations used, plus their position, and in the call the meaning is clear from the type of the object undergoing to unpacking operation. From still_jagmaster at yahoo.com Mon Dec 12 14:33:20 2011 From: still_jagmaster at yahoo.com (J A) Date: Mon, 12 Dec 2011 19:33:20 GMT Subject: executable builder References: <26288884-27b8-4f48-8ee6-af4be4364f6e@d16g2000yqb.googlegroups.com> Message-ID: <20111212143311usenet@terrranews.com> I am having some issues compiling an exe myself but most of that stems from the version of python I am using. That being said, try using GUI2EXE http://code.google.com/p/gui2exe this program makes the setup file go a lot easier, also it will tie in a few different tools like py2exe which is what I have been working with, also it does include py2app (the mac version). If you decide to go this route please pay careful attention to http://py2exe.org/index.cgi/Tutorial#Step52 if using python26, this bit of instruction could have saved me hours of banging my head on my desk. > On Thursday, July 01, 2010 3:16 AM King wrote: > Hi, > > I am trying to build python a cross platform python executable builder > to deploy python app. I tried various tools such as py2exe, > pyinstaller, cx_freeze but some how they are not upto the mark except > py2exe. Unfortunately py2exe is working only on windows based systems. > > The task is divide into 3 steps: > 1. Collect all the modules(.pyo, .pyd, .dll, etc) required for your > project. > 2. set up temporary environment for python > 3. run main.py using :python.exe main.py > > Initially I will not be creating an executable using main.py. I would be > using shell scripting to execute the main.py. > > Q1. Which is most efficient way to fine all the modules required by > your "main.py". > Q2. For example, I have copied all the modules in "d:\project\lib\*.*" > python.exe, python.dll, ms*.dll, main.py are copied to "d: > \project". > Before executing "python.exe main.py", I have to set up an > environment var and point it to "d:\project\lib", so > that python.exe can find the path for searching/loading > modules. How do I do that? > Q3. How do you convert your "main.py" into an executable? > > Prashant From brenNOSPAMbarn at NObrenSPAMbarn.net Mon Dec 12 14:45:10 2011 From: brenNOSPAMbarn at NObrenSPAMbarn.net (OKB (not okblacke)) Date: Mon, 12 Dec 2011 19:45:10 +0000 (UTC) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > And you can blame C for the use of % instead of mod or modulo. Anytime you can blame C for something, you can also blame a bunch of other languages for foolishly perpetuating the inanities of C. -- --OKB (not okblacke) Brendan Barnwell "Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail." --author unknown From rosuav at gmail.com Mon Dec 12 15:49:27 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 07:49:27 +1100 Subject: Documentation for python-evolution - where? In-Reply-To: References: <3dnfr8-h6a.ln1@chris.zbmc.eu> Message-ID: On Tue, Dec 13, 2011 at 3:40 AM, wrote: > Actually I'm not sure if it's down to the docstrings because the help > available from Python itself stops (not unreasonably) at the interface > to the C library code. ?What I was after (and you have told me where > it is) was the functions/methods available from the C library. Ah, yes, I know that problem! Let's see, how many high level languages/libraries have I used that have simply exposed a lower-level API without documenting it? REXX/REXXUtil and SysSetObjectData was the first. More recently, Pike and the GTK/GTK2 modules. In between, oh so many others. Depending on how much is exposed and how transparent the local layer, it may still be worth writing up some better documentation. Or if not, it may be of value for the docs to incorporate a link to some "upstream documentation", which would accomplish the same thing. (It's a shot at immortality - get your name in a big project's revision history!) ChrisA From rosuav at gmail.com Mon Dec 12 15:53:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 07:53:35 +1100 Subject: automate commands to an .exe console program through python In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 3:41 AM, Juan Perez wrote: > I need to automate a .exe console program in windows, and send key > characters like 'a', 'M' ... > I had this running in an autoIT script which with only activating cmd > window, and with the "send" parameter I had all already done. But I'm having > a lot of problems with python, I've tried with process.call, communicate(), > os.popen ... but no results. I can't comunicate with the program and even i > can't maintain it opened more than? a few seconds. If I open a program like > notepad.exe it remains opened but when I try to comunicate with the stdin > pipe just don't write anything to the program. GUI programs such as Notepad usually don't read from STDIN, which is where text goes if you write to a pipe. You may have to check out how, exactly, the program accepts commands; your autoIT script is probably sending keystrokes using the Wndows GUI, so it works as long as the program has focus. Ideally, look into whether the program has an actual automation mode - and if it doesn't have one, ask the programmer to expose his code directly to a Python script. Hey, it's worth a shot! :) ChrisA From einazaki668 at yahoo.com Mon Dec 12 16:15:11 2011 From: einazaki668 at yahoo.com (Eric) Date: Mon, 12 Dec 2011 13:15:11 -0800 (PST) Subject: curses (or something) for windows Message-ID: <7a4bc959-bd45-49aa-9061-459cac28e2ca@a17g2000yqj.googlegroups.com> Is there something similar to curses available for the Windows version of Python (2.7, community edition)? Preferably something built-in. In general, I'm looking to do gui-ish things from within the command window. Also, in particular, is there a way to get the console size (rows, cols). I've seen how to do it in unix land but not for windows. TIA, eric From steve+comp.lang.python at pearwood.info Mon Dec 12 16:28:22 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Dec 2011 21:28:22 GMT Subject: Overriding a global References: Message-ID: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: > Using the same name for 2 different objects is a bad idea in general. We have namespaces precisely so you don't need to care about making names globally unique. -- Steven From d at davea.name Mon Dec 12 16:43:54 2011 From: d at davea.name (Dave Angel) Date: Mon, 12 Dec 2011 16:43:54 -0500 Subject: Overriding a global In-Reply-To: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EE6759A.8090102@davea.name> On 12/12/2011 04:28 PM, Steven D'Aprano wrote: > On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: > >> Using the same name for 2 different objects is a bad idea in general. > We have namespaces precisely so you don't need to care about making names > globally unique. > > True, but in this code, the function is trying to both use the global value, but also a local that deliberately has the same name, but a different meaning and "value". The CPython compiler doesn't make this easy, and I think the globals() technique is unnecessarily obscure, as is the default-argument trick. If a function knows of the presence of a global, it's not asking too much for it to not re-use the same name in local scope. Since it seems to be in vogue to propose language changes, how about a new place for 'as' ? def myfunc(): global logger as g_logger logger = g_logger.debug('stuff').getChild('function') -- DaveA From joshua.landau.ws at gmail.com Mon Dec 12 16:47:23 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 12 Dec 2011 21:47:23 +0000 Subject: Overriding a global In-Reply-To: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: Wouldn't this be nicer, though?: def getChildLogger(id): return logger.getChild(id) def someFunc(): logger = getChildLogger("someFunc") -- UNTESTED -- No messing around with globals this way, and it's more extendable. And 'globals()["logger"].getChild("someFunc")' reads like a brick. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Mon Dec 12 16:58:33 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 12 Dec 2011 16:58:33 -0500 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE5C576.2000307@gmail.com> Message-ID: On 12/12/2011 12:12 PM, Eelco wrote: > Im not sure if this is a genuine understanding, or trollish > obtuseness. If you are referring to what I write, it is based on genuine understanding of Python. > Yes, the target can be anywhere in the sequence. And yes, the > resulting list can contain objects of any type, so its very flexible > in that regard too. > > But to relate it to the topic of this thread: no, the syntax does not > allow one to select the type of the resulting sequence. It always > constructs a list. One use case of *target is to ignore the stuff collected in the target because one only wants a few end values from the iterable. Another is to pull stuff out because one wants to iterate through the rest. For both uses, a list is as good as anything. > Yes, we can cast the list to be whatever we want on the next line, Convert. For the very few cases one wants to do this, it is quite adequate. > but the question is whether this language design can be improved upon. Not easily. > The choice of a list feels arbitrary, On the contrary, a list is precisely what is needed to collect an indefinite number of leftovers. > adding another line to cast it to > something else would be even more verbose, and whats more, there would > be serious performance implications if one should seek to apply this > pattern to a deque/linkedlist; it would make taking off the head/tail > of the list from a constant to a linear operation. For a linked list, no *target and no copying is needed: head, tail = llist >>>> head, deque(tail) = somedeque > > Is better in every way I can think of (readability, consistence, > performance) than: >>>> head, *tail = somedeque >>>> tail = deque(tail) But your suggestion is much worse in each way than head = somedeque.popleft() To repeat, there is no reason to copy even once. If one does not want to mutate the deque, then one mutates an iterator view of the deque. -- Terry Jan Reedy From ben+python at benfinney.id.au Mon Dec 12 17:27:09 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 13 Dec 2011 09:27:09 +1100 Subject: Overriding a global References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <87k4611l4y.fsf@benfinney.id.au> Dave Angel writes: > True, but in this code, the function is trying to both use the global > value, but also a local that deliberately has the same name, but a > different meaning and "value". The CPython compiler doesn't make this > easy, and I think the globals() technique is unnecessarily obscure, as > is the default-argument trick. I disagree. The language makes it difficult, and it *should* be difficult to do what you describe. The tricks to achieve it are obscure and ugly, which is a good thing IMO: they're a code smell that the design of the code needs changing. Or, in brief: they're not unnecessarily obscure, they're as obscure as they need to be. > If a function knows of the presence of a global, it's not asking too > much for it to not re-use the same name in local scope. Yes. -- \ ?Airports are ugly. Some are very ugly. Some attain a degree of | `\ ugliness that can only be the result of a special effort.? | _o__) ?Douglas Adams, _The Long Dark Tea-Time Of The Soul_ | Ben Finney From hoogendoorn.eelco at gmail.com Mon Dec 12 18:40:38 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 12 Dec 2011 15:40:38 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: > > Im not sure if this is a genuine understanding, or trollish > > obtuseness. > > If you are referring to what I write, it is based on genuine > understanding of Python. This is getting 'interesting'. In a way. I meant to write 'misunderstanding', as I think the context made quite clear. So again this adds another layer of doubt as to whether you are being obtuse for its own sake, or if there is yet another layer of misunderstanding stacked on top of the others. Either way, lets continue with the benefit of the doubt. > One use case of *target is to ignore the stuff collected in the target > because one only wants a few end values from the iterable. Another is to > pull stuff out because one wants to iterate through the rest. For both > uses, a list is as good as anything. So what is the point of having different sequence types, if a list can do anything? I would argue that the different performance characteristics and differences in semantics give each sequence type ample reason for existence. > Convert. For the very few cases one wants to do this, it is quite adequate. Or so you opine. As you may recall, the original idea was motivated by args/kwargs syntactic clarity and flexibility; I merely shifted focus to this use case of collection unpacking since it is somewhat simpler and easier to discuss. Keep that in mind, should you seek to build a case for that assertion in the future. > ?> but the question is whether this language design can be improved upon. > > Not easily. Again, so you opine. Care to give a refutation of my proposed double colon syntax? Ignoring for a moment whether or not it is useful; does it clash with anything? I havnt been able to think of anything in the past few hours, but im not taking that to mean much; there are probably some subtleties I am overlooking. I think it dos not clash with slicing syntax for instance, but im not sure. > For a linked list, no *target and no copying is needed: > > head, tail = llist I have no idea what this means. > >>>> head, deque(tail) = somedeque > > > Is better in every way I can think of (readability, consistence, > > performance) than: > >>>> head, *tail = somedeque > >>>> tail = deque(tail) > > But your suggestion is much worse in each way than > > head = somedeque.popleft() No its not. First of all, its not semantically equivalent; popleft mutates a collection type, and collection unpacking does not; it creates a set of disjoint views of the collection's data. Whether its worse in terms of readability is debatable, but in terms of the other two stated metrics, your claim of it being any kind of worse is objectively false. Furthermore, this brings us back again to the point I raised several times before. Yes, obviously you can already DO these things, but NOT within the same uniform collection unpacking syntactic construct. Again, you have failed to point out what is wrong with supplying a type constrain to python and let it do the right thing based on that; to reiterate: head, tail::deque = deque No need to mutate anything; python can create the view of the linkedlist internally. A single unambigious syntax, and single unambigious semantics, for all sequence types. Whats not to like? If you dont like the extra characters you have to type; there is of course such a thing as defaults. You can choose: head, tail:: = deque; if the type constraint is omitted, we could make tail a list by default, or my preferred solution, infer it from the right hand side type. In case of the former, all you had to do was type :: instead of *, and your python universe would otherwise be completely unchanged. If thats 'not easily', I dont know what is. > To repeat, there is no reason to copy even once. If one does not want to > mutate the deque, then one mutates an iterator view of the deque. And arrive at yet another construction to do the same thing. From wuwei23 at gmail.com Mon Dec 12 18:45:06 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 12 Dec 2011 15:45:06 -0800 (PST) Subject: Dynamic variable creation from string References: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> <24728986.18.1323694150411.JavaMail.geo-discussion-forums@prfc16> Message-ID: <2b414cd2-0f0a-45bf-b61e-9205704ac03a@u10g2000prl.googlegroups.com> On Dec 12, 10:49?pm, 88888 Dihedral wrote: > This is the way to write an assembler or > to roll out a script language to be included in an app > by users. This is a garbage comment that has absolutely nothing to do with the topic at hand _at all_. From wuwei23 at gmail.com Mon Dec 12 18:47:42 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 12 Dec 2011 15:47:42 -0800 (PST) Subject: curses (or something) for windows References: <7a4bc959-bd45-49aa-9061-459cac28e2ca@a17g2000yqj.googlegroups.com> Message-ID: <2d8d5df7-d1e0-4f2a-943f-5b7335ab8608@l18g2000pro.googlegroups.com> On Dec 13, 7:15?am, Eric wrote: > Is there something similar to curses available for the Windows version > of Python (2.7, community edition)? ?Preferably something built-in. > In general, I'm looking to do gui-ish things from within the command > window. > > Also, in particular, is there a way to get the console size (rows, > cols). ?I've seen how to do it in unix land but not for windows. > > TIA, > eric Check out http://www.lfd.uci.edu/~gohlke/pythonlibs/ It's a fantastic resource, Win32 & 64 versions of a lot of packages. From joshua.landau.ws at gmail.com Mon Dec 12 18:48:09 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Mon, 12 Dec 2011 23:48:09 +0000 Subject: Overriding a global In-Reply-To: <87k4611l4y.fsf@benfinney.id.au> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: > > > If a function knows of the presence of a global, it's not asking too > > much for it to not re-use the same name in local scope. > > Yes. It's just a function wanting to act as-if it were in a different environment than its default. By that same reasoning you could state that "If a function knows of the presence of a built-in, it's not asking too much for it to not re-use the same name in local scope." Yet if rebinding "id" is such a crime, why is it so oft done? Rebinding logger locally in a function is really no different to a subclass rebinding a variable from its main class using that class' value. *The only difference is that, in that case, you have an alternate binding to the original value.* * * >>> class A(): ... val = 1 ... >>> class B(A): ... val = str(val) # Obviously, this doesn't work ... NameError: name 'val' is not defined >>> class B(A): ... val = str(A.val) # But it's OK as we can just do this ^^ ... >>> B().val '1' >>> The only reason it's not minded with classes is because there's a good way to do it. I get that my analogy doesn't use globals, but the idea of extending a more-global attribute locally is shared between the concepts. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wuwei23 at gmail.com Mon Dec 12 18:54:09 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 12 Dec 2011 15:54:09 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> Message-ID: <4588e1ef-6eec-45ff-abcd-d414f2a787d6@r16g2000prr.googlegroups.com> On Dec 12, 10:21?pm, Eelco wrote: > > ?Modulo is hardly an obscure operation. "What's the remainder...?" is a > > ?simple question that people learn about in primary school. > > So is 'how much wood would a woodchucker chuck if a woodchucker could > chuck wood?'. But how often does that concept turn up in your code? That comment right there? That's the moment every serious coder stopped paying attention to a single word you say. From wuwei23 at gmail.com Mon Dec 12 19:27:45 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 12 Dec 2011 16:27:45 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: On Dec 13, 3:12?am, Eelco wrote: > But to relate it to the topic of this thread: no, the syntax does not > allow one to select the type of the resulting sequence. It always > constructs a list. So by this argument, _every_ function that returns a list should take an optional argument to specify an alternative form of sequence. What, exactly, is so onerous about coercing your list to _whatever_ type you want? You know, like everybody else has been. What does this _gain_ you other than one less line of code? From ian.g.kelly at gmail.com Mon Dec 12 19:34:25 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 17:34:25 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> Message-ID: On Mon, Dec 12, 2011 at 11:51 AM, Eelco wrote: > Either way, its not hard to add some detail to the semantics to allow > all this. Even this function definition: > > def func(Foo(args), Foo(kwargs)) > > ...could even be defined unambigiously by overloading first on base > type, and if that does not uniquely determine the args and kwargs, > fall back on positionality, so that: > > def func(Foo(args), dict(kwargs)) > def func(list(args), Foo(kwargs)) > > would be uniquely defined as well. That solves some of the problems, but if I just have: def func(SequenceOrMappingType(args)): That's going to unpack positionally. If I want it to unpack keywords instead, how would I change the definition to indicate that? From einazaki668 at yahoo.com Mon Dec 12 19:45:24 2011 From: einazaki668 at yahoo.com (Eric) Date: Mon, 12 Dec 2011 16:45:24 -0800 (PST) Subject: curses (or something) for windows References: <7a4bc959-bd45-49aa-9061-459cac28e2ca@a17g2000yqj.googlegroups.com> <2d8d5df7-d1e0-4f2a-943f-5b7335ab8608@l18g2000pro.googlegroups.com> Message-ID: On Dec 12, 5:47?pm, alex23 wrote: > On Dec 13, 7:15?am, Eric wrote: > > > Is there something similar to curses available for the Windows version > > of Python (2.7, community edition)? ?Preferably something built-in. > > In general, I'm looking to do gui-ish things from within the command > > window. > > > Also, in particular, is there a way to get the console size (rows, > > cols). ?I've seen how to do it in unix land but not for windows. > > > TIA, > > eric > > Check outhttp://www.lfd.uci.edu/~gohlke/pythonlibs/ > > It's a fantastic resource, Win32 & 64 versions of a lot of packages. Wowee that's a lot of stuff. Thanks for the tip. eric From wuwei23 at gmail.com Mon Dec 12 19:46:52 2011 From: wuwei23 at gmail.com (alex23) Date: Mon, 12 Dec 2011 16:46:52 -0800 (PST) Subject: I love the decorator in Python!!! References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <0aae460e-caf1-44ce-b228-e6943fc75b7f@24g2000prd.googlegroups.com> <82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com> Message-ID: On Dec 13, 2:27?am, Robert Kern wrote: > On 12/12/11 3:36 AM, alex23 wrote: > > > On Dec 9, 8:08 pm, Robert Kern ?wrote: > >> On 12/9/11 5:02 AM, alex23 wrote: > >>> The 3rd party 'decorator' module takes care of issues like docstrings > >>> & ? ?function signatures. I'd really like to see some of that > >>> functionality in the stdlib though. > > >> Much of it is: > > >> ? ?http://docs.python.org/library/functools#functools.update_wrapper > > > Ah, cheers :) Is that a recent addition? The lack of note makes it > > seem like it was there from the beginning? > > The module was added in Python 2.5 as noted at the top of the page. I had thought you meant it now included function signature handling, as that was the context at the time. No biggie. From cpblpublic at gmail.com Mon Dec 12 20:00:08 2011 From: cpblpublic at gmail.com (C Barrington-Leigh) Date: Mon, 12 Dec 2011 17:00:08 -0800 (PST) Subject: Simple legend code no longer works after upgrade to Ubuntu 11.10 Message-ID: <35d63565-3439-480e-bf05-964acf1e318a@13g2000vbu.googlegroups.com> rom pylab import * plot([0,0],[1,1],label='Ubuntu 11.10') Before I upgraded to 2.7.2+ / 4 OCt 2011, the following code added a comment line with a legend. Now, the same code makes the legend appear "off-screen", ie way outside the axes limits. Can anyone help? And/or is there a new way to add a title and footer to the legend? Thanks! lh=legend(fancybox=True,shadow=False) lh.get_frame().set_alpha(0.5) from matplotlib.offsetbox import TextArea, VPacker fontsize=lh.get_texts()[0].get_fontsize() legendcomment=TextArea('extra comments here', textprops=dict(size=fontsize)) show() # Looks fine here lh._legend_box = VPacker(pad=5, sep=0, children=[lh._legend_box,legendcomment], align="left") lh._legend_box.set_figure(gcf()) draw() From cpblpublic at gmail.com Mon Dec 12 20:04:27 2011 From: cpblpublic at gmail.com (C Barrington-Leigh) Date: Mon, 12 Dec 2011 17:04:27 -0800 (PST) Subject: Simple legend code no longer works after upgrade to Ubuntu 11.10 Message-ID: <5845f1b6-cb4e-49b3-9e4d-9d290a76ae68@u32g2000yqe.googlegroups.com> """ Before I upgraded to 2.7.2+ / 4 OCt 2011, the following code added a comment line to an axis legend using matplotlib / pylab. Now, the same code makes the legend appear "off-screen", ie way outside the axes limits. Can anyone help? And/or is there a new way to add a title and footer to the legend? Thanks! """ from pylab import * plot([0,0],[1,1],label='Ubuntu 11.10') lh=legend(fancybox=True,shadow=False) lh.get_frame().set_alpha(0.5) from matplotlib.offsetbox import TextArea, VPacker fontsize=lh.get_texts()[0].get_fontsize() legendcomment=TextArea('extra comments here', textprops=dict(size=fontsize)) show() # Looks fine here lh._legend_box = VPacker(pad=5, sep=0, children=[lh._legend_box,legendcomment], align="left") lh._legend_box.set_figure(gcf()) draw() From steve+comp.lang.python at pearwood.info Mon Dec 12 20:34:37 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 01:34:37 GMT Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: <4ee6abac$0$11091$c3e8da3@news.astraweb.com> On Mon, 12 Dec 2011 16:58:33 -0500, Terry Reedy wrote: > On 12/12/2011 12:12 PM, Eelco wrote: >> Yes, we can cast the list to be whatever we want on the next line, > > Convert. For the very few cases one wants to do this, it is quite > adequate. +1 with the small proviso that there's not much you can usefully do with a dict. If you convert to an ordered dict, the initial order is lost by the time you see it; if you want to allow duplicates, duplicates are likewise lost (or in a function call, an exception is raised). In Python 4000, I'd like to see some way to pass arbitrary parameters to functions, including duplicates. That is, emulating the richness of command line argument handling in functions. In Python 3.x, I'd like to see OrderedDict become a built-in, and then **kwargs can collect named arguments in an ordered dict without losing the order. -- Steven From steve+comp.lang.python at pearwood.info Mon Dec 12 20:35:52 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 01:35:52 GMT Subject: Dynamic variable creation from string References: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> <24728986.18.1323694150411.JavaMail.geo-discussion-forums@prfc16> <2b414cd2-0f0a-45bf-b61e-9205704ac03a@u10g2000prl.googlegroups.com> Message-ID: <4ee6abf8$0$11091$c3e8da3@news.astraweb.com> On Mon, 12 Dec 2011 15:45:06 -0800, alex23 wrote: > On Dec 12, 10:49?pm, 88888 Dihedral > wrote: >> This is the way to write an assembler or to roll out a script language >> to be included in an app by users. > > This is a garbage comment that has absolutely nothing to do with the > topic at hand _at all_. Please stop responding to the bot. -- Steven From ian.g.kelly at gmail.com Mon Dec 12 20:41:34 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 18:41:34 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4EE5C576.2000307@gmail.com> Message-ID: On Mon, Dec 12, 2011 at 4:40 PM, Eelco wrote: >> For a linked list, no *target and no copying is needed: >> >> head, tail = llist > > I have no idea what this means. Each node of a linked list consists of a data member and a "next" member, that holds the next node in the list. The natural container for this and the way it is usually implemented in Python is a 2-element tuple. For example: llist = ('one', ('two', ('three', ('four', None)))) would be a linked list as typically constructed in Python, and it can be used in pretty much the same way that lists are used in Lisp. The result of the operation: head, tail = llist is: head = 'one' tail = ('two', ('three', ('four', None))) and as Terry pointed out, no copying is required to do this. I believe deque is also implemented as a doubly-linked list, which is probably why you keep referring to it as such, but that is an implementation detail. When you say "linked list" in relation to Python, the preceding is what comes to mind. >> >>>> head, deque(tail) = somedeque >> >> > Is better in every way I can think of (readability, consistence, >> > performance) than: >> >>>> head, *tail = somedeque >> >>>> tail = deque(tail) >> >> But your suggestion is much worse in each way than >> >> head = somedeque.popleft() > > No its not. First of all, its not semantically equivalent; popleft > mutates a collection type, and collection unpacking does not; it > creates a set of disjoint views of the collection's data. Whether its > worse in terms of readability is debatable, but in terms of the other > two stated metrics, your claim of it being any kind of worse is > objectively false. I definitely disagree on readability. Skimming this thread as I have been, it took me a while to get that your proposed syntax would create a second deque sharing internal state with the original, rather than creating a simple copy of the deque, which is what it looks like it should do. Incidentally, while that change is not really central to your syntax proposal, I think it would be very messy. For example, how do you propose keeping the length elements in sync? Inserting an item in one may or may not affect the length of the other. If I append an item to the end of one deque, should the other automatically be extended as well? What if the tail node of the second deque occurs after the tail node of the deque being appended? Does the appended element then get inserted into the middle of the second deque (I think it would have to be)? If I insert an element into the longer (second) deque that just happens to be immediately after the tail of the shorter deque, does *that* cause the shorter deque to be automatically extended? And likewise for operations at the head of the deque. None of these questions have obvious answers as to the "right" way to it, and for that reason I think this is probably best left to the user to implement a custom deque view class with whatever semantics they prefer. > Furthermore, this brings us back again to the point I raised several > times before. Yes, obviously you can already DO these things, but NOT > within the same uniform collection unpacking syntactic construct. > Again, you have failed to point out what is wrong with supplying a > type constrain to python and let it do the right thing based on that; > to reiterate: > > head, tail::deque = deque Python users generally follow the rule "explicit is better than implicit". Setting a general constraint and letting the language "do the right thing" is a kind of black magic that feels off because it tends to break that rule. But that's not to say that black magic never wins -- just look at super() and the MRO. > If you dont like the extra characters you have to type; there is of > course such a thing as defaults. You can choose: > head, tail:: = deque; if the type constraint is omitted, we could make > tail a list by default, or my preferred solution, infer it from the > right hand side type. I prefer the former. If it always creates a list by default, then the programmer reading this code three years later knows exactly what the type of tail is. If it instead infers it from the sequence being unpacked, then the programmer is forced to infer it as well, and it won't necessarily be obvious or even consistent. Cheers, Ian From d at davea.name Mon Dec 12 20:46:14 2011 From: d at davea.name (Dave Angel) Date: Mon, 12 Dec 2011 20:46:14 -0500 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: <4EE6AE66.5080605@davea.name> On 12/12/2011 06:48 PM, Joshua Landau wrote: >>> If a function knows of the presence of a global, it's not asking too >>> much for it to not re-use the same name in local scope. >> Yes. > > It's just a function wanting to act as-if it were in a different > environment than its default. By that same reasoning you could state that > "If a function knows of the presence of a built-in, it's not asking > too much for it to not re-use the same name in local scope." > It's entirely different. It's only the same if the function actually tries to call the built-in, then also wants a local variable with a different purpose. Think a little about what I mean that the function "knows of the presence." I did not say the programmer knows of the presence. -- DaveA From steve+comp.lang.python at pearwood.info Mon Dec 12 20:48:43 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 01:48:43 GMT Subject: Overriding a global References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: <4ee6aefa$0$11091$c3e8da3@news.astraweb.com> On Tue, 13 Dec 2011 09:27:09 +1100, Ben Finney wrote: > Dave Angel writes: > >> True, but in this code, the function is trying to both use the global >> value, but also a local that deliberately has the same name, but a >> different meaning and "value". The CPython compiler doesn't make this >> easy, and I think the globals() technique is unnecessarily obscure, as >> is the default-argument trick. > > I disagree. The language makes it difficult, and it *should* be > difficult to do what you describe. > > The tricks to achieve it are obscure and ugly, which is a good thing > IMO: they're a code smell that the design of the code needs changing. Devil's Advocate: perhaps not. Think of local and global names as analogous to instance and class attributes. There are good use cases for making something a class attribute, while allowing instances to override that name with an instance attribute. I see a reasonable case for saying "use this global, unless a local overrides it". Similarly, globals override built-ins with the same name; while monkeypatching needs to be used with care, it is a legitimate technique. To a human reader, the following pseudocode might be ambiguous, but either case makes sense: x = 1 def spam(): print x # prints 1 x = 2 # does this create a new local x, or modify the old global x? print x # unambiguously prints 2 print x # prints 1 or 2 Python doesn't allow this, but another language might; in Python, a reasonable way to get similar behaviour might be: x = 1 def spam(): print globals()['x'] x = 2 # unambiguously creates a new local x print x # unambiguously prints 2 print x # unambiguously prints 1 -- Steven From steve+comp.lang.python at pearwood.info Mon Dec 12 21:43:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 02:43:00 GMT Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> Message-ID: <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> On Mon, 12 Dec 2011 04:21:15 -0800, Eelco wrote: >> No more, or less, explicit than the difference between "==" and "is". > > == may be taken to mean identity comparison; 'equals' can only mean one > thing. Nonsense. "Equals" can be taken to mean anything the language designer chooses, same as "==". There is no language police that enforces The One True Meaning Of Equals. In fact, there is no one true meaning of equals. Even my tiny Pocket Oxford dictionary lists five definitions. It is ironic that the example you give, that of identity, is the standard definition of equals in mathematics. 2*2 = 4 does not merely say that "there is a thing, 2*2, which has the same value as a different thing, 4", but that both sides are the same thing. Two times two *is* four. All numbers are, in some sense, singletons and equality implies identity. A language designer might choose to define equals as an identity test, or as a looser "values are the same" test where the value of an object or variable is context dependent, *regardless* of how they are spelled: = == === "is" "equals" or even "flibbertigibbet" if they wanted to be whimsical. The design might allow types to define their own sense of equality. Triangle.equals(other_triangle) might be defined to treat any two congruent triangles as equal; set equality could be defined as an isomorphism relation; string equality could be defined as case- insensitive, or to ignore leading and trailing whitespace. Regardless of whether you or I *would* make those choices, we *could* make those choices regardless of how our language spells the equality test. > Of course 'formally' these symbols are well defined, but so is > brainf*ck I don't understand your point here. >> Modulo is hardly an obscure operation. "What's the remainder...?" is a >> simple question that people learn about in primary school. > > > So is 'how much wood would a woodchucker chuck if a woodchucker could > chuck wood?'. But how often does that concept turn up in your code? You didn't make a statement about how often modulo turns up in code (which is actually quite frequently, and possibly more frequently than regular division), but about the obscurity of the operation. Taking the remainder is not an obscure operation. The names "modulo" and "modulus" may be obscure to those who haven't done a lot of mathematics, but the concept of remainder is not. "How many pieces are left over after dividing into equal portions" is something which even small children get. >> And you can blame C for the use of % instead of mod or modulo. > > I didnt know one of Python's design goals was backwards compatibility > with C. Don't be silly. You know full well Python is not backwards compatible with C, even if they do share some syntactical features. C is merely one of many languages which have influenced Python, as are Haskell, ABC, Pascal, Algol 68, Perl (mostly in the sense of "what not to do" ), Lisp, and probably many others. It merely happens that C's use of the notation % for the remainder operation likely influenced Python's choice of the same notation. I note that the *semantics* of the operation differs in the two languages, as I understand that the behaviour of % with negative arguments is left undefined by the C standard, while Python does specify the behaviour. >> I can't imagine what sort of Python code you have seen that you >> consider 90% attribute access "typical". I've just run the Python >> tokenizer over my startup.py file, and I get these results: > > Yes, that was a hyperbole; but quite an often used construct, is it not? It's hard, but not quite impossible, to write useful Python code without it, so yes. >> If you can supply any function at all, what happens if I write this: > > > You cannot; only constructors modelling a sequence or a dict, and only > in that order. Is that rule clear enough? But why limit yourself to those restrictive rules? If I want to collect a sequence of arguments into a string, why shouldn't I be allowed to write this? def func(parg, str(args)): ... If I want to sum a collection of arguments, why not write this? def func(pargs, sum(args)): ... Isn't that better than this? def func(pargs, *args): args = sum(args) ... But no. I don't mean those examples to be taken seriously: when you answer to your own satisfaction why they are bad ideas, you may be closer to understanding why I believe your idea is also a bad idea. >> I believe that your proposal leads to an over-generalisation "call >> arbitrary functions when handling parameter lists". > > I hope the above clears that up. It is as much about calling functions > as ** is about raising kwargs to the power of. I don't understand this comment. Nobody has suggested that ** in function parameter lists is the exponentiation operator. As for "calling functions", how else do you expect to generate a type if you don't call the type constructor? One of your early examples was something like: def func(parg, attrdict(kwargs)): ... If you expect kwargs to be an attrdict, which is not a built-in, presumably you will have needed to have defined attrdict as a type or function, and this type or function will get called at run time to collect the kwargs. That is all. >> I don't believe you >> need this added complication. If you want to your var args as a list, >> call list(args) inside your function. > > We dont strictly 'need' any language construct. Real men use assembler, > right? "We're not using assembly" is not a reason to add a feature to a language. Every feature adds cost to the language: * harder to implement; * harder to maintainer; * larger code base; * more documentation to be written; * more tests to be written; * more for users to learn etc. >> > head, tuple(tail) = iterable >> In Python 3, that is spelled: >> head, *tail = iterable >> tail = tuple(tail) > > Yes, I know. How is that not a lot more verbose and worse than what I > have proposed in all possible ways? That *specific* syntax, outside of function declarations, is something I've often thought might be useful. But if I were to argue its case, I would allow arbitrary functions, and treat it as syntactic sugar for: head, *tail = iterable tail = func(tail) # or possibly func(*tail) But that's pie-in-the-sky musing. I certainly wouldn't put it in function parameter lists. Param lists should be declarations, not code. Apart from the most limited collation of args, code belongs inside the body of the function, not the param list: def foo(a, 2*b+1, c): # double the second arg and add 1 >> head, tail = somestring[0], somestring[1:] > > Well yes, splendid; we can do that with lists too since the dawn of > Python. What you are saying here in effect is that you think the > head/tail syntax is superfluous; that youd rather see it eliminated than > generalized. No. It is not "head/tail" syntax, but sequence unpacking, and it has been nicely generalised to allow things like this in Python 3: a, b, c, d, *lump, x, y z = any_iterable any_iterable isn't limited to a list, str, or other object which supports slicing. It can be any object supporting the iteration protocol. What I'm saying is that there is no need to OVER-generalise this to specify the type of *lump within the packing operation. If you want lump to be something other that Python's choice, perform the conversion yourself. -- Steven From rosuav at gmail.com Mon Dec 12 22:08:28 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 14:08:28 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Tue, Dec 13, 2011 at 1:43 PM, Steven D'Aprano wrote: > It merely happens that C's > use of the notation % for the remainder operation likely influenced > Python's choice of the same notation. Considering that Python also had the notion that "integer divided by integer yields integer" until Py3, I would say it's extremely likely that most of Python's division facilities were modelled off C. That's not a bad thing; gives you a set of operations that a large number of people will grok, and only a small number of oddities. > I note that the *semantics* of the operation differs in the two > languages, as I understand that the behaviour of % with negative > arguments is left undefined by the C standard, while Python does specify > the behaviour. ... and there's the proof that "modelled off" does not mean "slavishly follows". This lack of definition is a weakness in C. > def foo(a, 2*b+1, c): ?# double the second arg and add 1 No, that should subtract 1 from the second arg and halve it. The expression you give there has to match the value from the parameter list. This syntax would be a huge boon to Python. Imagine how much easier this could make things: def foo(sum(x)): return x print(foo(120)) # will print a list of numbers that sum to 120 ChrisA From ian.g.kelly at gmail.com Mon Dec 12 23:52:11 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 21:52:11 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Mon, Dec 12, 2011 at 7:43 PM, Steven D'Aprano wrote: > If I want to collect a sequence of arguments into a string, why shouldn't > I be allowed to write this? > > ? ?def func(parg, str(args)): ... Obviously, because the correct syntax would be: def func(parg, ''.join(args)): ... :-P From ian.g.kelly at gmail.com Tue Dec 13 00:50:44 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 12 Dec 2011 22:50:44 -0700 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On Mon, Dec 12, 2011 at 4:48 PM, Joshua Landau wrote: > Rebinding logger locally in a function is really no > different to a subclass rebinding a variable from its main class using that > class' value. The only difference is that, in that case, you have an > alternate binding to the original value. No, there is another difference, the reason for rebinding the name. In a subclass, you would rebind a class attribute because that particular attribute, which you need to change, is used and expected by external code, either in the base class or in code that uses its API (or both). Local variables in functions, on the other hand, are not externally visible, so there is no need to do this in order to conform to the expectations of external code. All it does in that case is to sow potential confusion. From hoogendoorn.eelco at gmail.com Tue Dec 13 03:30:06 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 00:30:06 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: On Dec 13, 1:27?am, alex23 wrote: > On Dec 13, 3:12?am, Eelco wrote: > > > But to relate it to the topic of this thread: no, the syntax does not > > allow one to select the type of the resulting sequence. It always > > constructs a list. > > So by this argument, _every_ function that returns a list should take > an optional argument to specify an alternative form of sequence. > > What, exactly, is so onerous about coercing your list to _whatever_ > type you want? You know, like everybody else has been. > > What does this _gain_ you other than one less line of code? 1) Turning two lines of code into a single more readable one is nothing to scoff at 2) After-the-fact conversion is O(n), getting the view you want right away is O(1) Not every function needs this flexibility; many specialized functions could not care less. But collection unpacking is quite a general thing, and for the record; slicing a tuple returns a tuple. Would you rather have that return a list too? From hoogendoorn.eelco at gmail.com Tue Dec 13 03:31:21 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 00:31:21 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <1a159184-9262-4b66-88e8-a5b68964d8d7@q16g2000yqn.googlegroups.com> Message-ID: On Dec 13, 1:34?am, Ian Kelly wrote: > On Mon, Dec 12, 2011 at 11:51 AM, Eelco wrote: > > Either way, its not hard to add some detail to the semantics to allow > > all this. Even this function definition: > > > def func(Foo(args), Foo(kwargs)) > > > ...could even be defined unambigiously by overloading first on base > > type, and if that does not uniquely determine the args and kwargs, > > fall back on positionality, so that: > > > def func(Foo(args), dict(kwargs)) > > def func(list(args), Foo(kwargs)) > > > would be uniquely defined as well. > > That solves some of the problems, but if I just have: > > ? ? ? ? def func(SequenceOrMappingType(args)): > > That's going to unpack positionally. ?If I want it to unpack keywords > instead, how would I change the definition to indicate that? That should raise an ambiguity error. But honestly, how often have you worked with SequenceOrMappingType's? I think this is a rather palatable constraint. From joshua.landau.ws at gmail.com Tue Dec 13 03:34:59 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 13 Dec 2011 08:34:59 +0000 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On 13/12/2011, Ian Kelly wrote: > On Mon, Dec 12, 2011 at 4:48 PM, Joshua Landau > wrote: >> Rebinding logger locally in a function is really no >> different to a subclass rebinding a variable from its main class using >> that >> class' value. The only difference is that, in that case, you have an >> alternate binding to the original value. > > No, there is another difference, the reason for rebinding the name. > In a subclass, you would rebind a class attribute because that > particular attribute, which you need to change, is used and expected > by external code, either in the base class or in code that uses its > API (or both). Local variables in functions, on the other hand, are > not externally visible, so there is no need to do this in order to > conform to the expectations of external code. All it does in that > case is to sow potential confusion. > So you're saying you should never extend methods or attributes that aren't meant to be used as part of of the API? Because I can claim guilty on this point. I'd make this longer, but I've got class now :( From hoogendoorn.eelco at gmail.com Tue Dec 13 03:44:20 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 00:44:20 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> Message-ID: <2ff41b2e-90e3-4fae-9951-67bb257ed6ed@m10g2000vbc.googlegroups.com> On Dec 13, 2:41?am, Ian Kelly wrote: > On Mon, Dec 12, 2011 at 4:40 PM, Eelco wrote: > >> For a linked list, no *target and no copying is needed: > > >> head, tail = llist > > > I have no idea what this means. > > Each node of a linked list consists of a data member and a "next" > member, that holds the next node in the list. ?The natural container > for this and the way it is usually implemented in Python is a > 2-element tuple. ?For example: > > llist = ('one', ('two', ('three', ('four', None)))) > > would be a linked list as typically constructed in Python, and it can > be used in pretty much the same way that lists are used in Lisp. ?The > result of the operation: > > head, tail = llist > > is: > > head = 'one' > tail = ('two', ('three', ('four', None))) > > and as Terry pointed out, no copying is required to do this. ?I > believe deque is also implemented as a doubly-linked list, which is > probably why you keep referring to it as such, but that is an > implementation detail. ?When you say "linked list" in relation to > Python, the preceding is what comes to mind. 'for i in llist' is not quite going to fly is it? Thats probably the reason noone ever uses that construct; its not a proper sequence type. > >> >>>> head, deque(tail) = somedeque > > >> > Is better in every way I can think of (readability, consistence, > >> > performance) than: > >> >>>> head, *tail = somedeque > >> >>>> tail = deque(tail) > > >> But your suggestion is much worse in each way than > > >> head = somedeque.popleft() > > > No its not. First of all, its not semantically equivalent; popleft > > mutates a collection type, and collection unpacking does not; it > > creates a set of disjoint views of the collection's data. Whether its > > worse in terms of readability is debatable, but in terms of the other > > two stated metrics, your claim of it being any kind of worse is > > objectively false. > > I definitely disagree on readability. ?Skimming this thread as I have > been, it took me a while to get that your proposed syntax would create > a second deque sharing internal state with the original, rather than > creating a simple copy of the deque, which is what it looks like it > should do. Thats a matter of reading up on the semantics of collection unpacking. To be honest I dont even know what they are for lists, since I dont use python 3. But whatever is the case, the important thing is that the semantics should be uniform regardless of the type to be unpacked. > Incidentally, while that change is not really central to your syntax > proposal, I think it would be very messy. ?For example, how do you > propose keeping the length elements in sync? ?Inserting an item in one > may or may not affect the length of the other. ?If I append an item to > the end of one deque, should the other automatically be extended as > well? ?What if the tail node of the second deque occurs after the tail > node of the deque being appended? ?Does the appended element then get > inserted into the middle of the second deque (I think it would have to > be)? ?If I insert an element into the longer (second) deque that just > happens to be immediately after the tail of the shorter deque, does > *that* cause the shorter deque to be automatically extended? ?And > likewise for operations at the head of the deque. > > None of these questions have obvious answers as to the "right" way to > it, and for that reason I think this is probably best left to the user > to implement a custom deque view class with whatever semantics they > prefer. Good point. Copy-on-write semantics could be used, but really one should have several linked list types reflecting the underlying implementations. I would like to have an immutable singly linked list builtin of the standard functional type, which you can only unpack from one end and renders these issues moot, plus a builtin doubly linked list with copy-on-write or copy-on-unpacking semantics. > > Furthermore, this brings us back again to the point I raised several > > times before. Yes, obviously you can already DO these things, but NOT > > within the same uniform collection unpacking syntactic construct. > > Again, you have failed to point out what is wrong with supplying a > > type constrain to python and let it do the right thing based on that; > > to reiterate: > > > head, tail::deque = deque > > Python users generally follow the rule "explicit is better than > implicit". ?Setting a general constraint and letting the language "do > the right thing" is a kind of black magic that feels off because it > tends to break that rule. ?But that's not to say that black magic > never wins -- just look at super() and the MRO. We are not talking black magic here; we are talking about an EXPLICIT type constraint provided on the very same line. > > If you dont like the extra characters you have to type; there is of > > course such a thing as defaults. You can choose: > > head, tail:: = deque; if the type constraint is omitted, we could make > > tail a list by default, or my preferred solution, infer it from the > > right hand side type. > > I prefer the former. ?If it always creates a list by default, then the > programmer reading this code three years later knows exactly what the > type of tail is. ?If it instead infers it from the sequence being > unpacked, then the programmer is forced to infer it as well, and it > won't necessarily be obvious or even consistent. Well perhaps, but not always knowing the type of your objects at write- time is inherent to weakly typed languages; this happens all the time. Not knowing the type of the sequence to be unpacked is in a sense an asset; I can use this construct in a function, and unpack any sequence type in a manner appropriate for it. About the result of the unpacking I will know just as much as about the input to it; that they are the same type. From antoon.pardon at rece.vub.ac.be Tue Dec 13 04:15:41 2011 From: antoon.pardon at rece.vub.ac.be (Antoon Pardon) Date: Tue, 13 Dec 2011 10:15:41 +0100 Subject: Overriding a global In-Reply-To: References: Message-ID: <4EE717BD.10301@rece.vub.ac.be> On 12/10/2011 09:47 PM, Roy Smith wrote: > I've got a code pattern I use a lot. In each module, I create a logger > for the entire module and log to it all over: > > logger = logging.getLogger('my.module.name') > > class Foo: > def function(self): > logger.debug('stuff') > logger.debug('other stuff') > > and so on. This works, but every once in a while I decide that a > particular function needs a more specific logger, so I can adjust the > logging level for that function independent of the rest of the module. > What I really want to do is: > > def function(self): > logger = logger.getChild('function') > logger.debug('stuff') > logger.debug('other stuff') > > which lets me not have to change any lines of code other than inserting > the one to redefine logger. Unfortunately, that's not legal Python (it > leads to "UnboundLocalError: local variable 'logger' referenced before > assignment"). > > Any ideas on the best way to implement this? > How about two global references: globallogger = logger = logging.getLogger('my.module.name') def function(self): logger = globallogger.getChild('function') logger.debug('stuff') logger.debug('other stuff') -- Antoon Pardon From hoogendoorn.eelco at gmail.com Tue Dec 13 04:15:46 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 01:15:46 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> Message-ID: <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> On Dec 13, 3:43?am, Steven D'Aprano wrote: > On Mon, 12 Dec 2011 04:21:15 -0800, Eelco wrote: > >> No more, or less, explicit than the difference between "==" and "is". > > > == may be taken to mean identity comparison; 'equals' can only mean one > > thing. > > Nonsense. "Equals" can be taken to mean anything the language designer > chooses, same as "==". There is no language police that enforces The One > True Meaning Of Equals. In fact, there is no one true meaning of equals. > Even my tiny Pocket Oxford dictionary lists five definitions. > > It is ironic that the example you give, that of identity, is the standard > definition of equals in mathematics. 2*2 = 4 does not merely say that > "there is a thing, 2*2, which has the same value as a different thing, > 4", but that both sides are the same thing. Two times two *is* four. All > numbers are, in some sense, singletons and equality implies identity. We are not talking mathemathics, we are talking programming languages. Identity versus value equality is a well established concept within its jargon. Within this context, 'equals' and 'is' have clearly defined meanings. Indeed within broader use, including everyday language, the distinction is more subtle and therefore less useful, but obeying a linguistic rule that holds within computer science is better than making something up just for python, even though its not the yet better rule that any five year old might grasp. > > Of course 'formally' these symbols are well defined, but so is > > brainf*ck > > I don't understand your point here. Hence the above. > >> ?Modulo is hardly an obscure operation. "What's the remainder...?" is a > >> ?simple question that people learn about in primary school. > > > So is 'how much wood would a woodchucker chuck if a woodchucker could > > chuck wood?'. But how often does that concept turn up in your code? > > You didn't make a statement about how often modulo turns up in code > (which is actually quite frequently, and possibly more frequently than > regular division), but about the obscurity of the operation. Taking the > remainder is not an obscure operation. The names "modulo" and "modulus" > may be obscure to those who haven't done a lot of mathematics, but the > concept of remainder is not. "How many pieces are left over after > dividing into equal portions" is something which even small children get. So 'frequency of use' is no valid interpretation of 'obscurity'? Im not a native speaker, but im pretty sure it is. > >> And you can blame C for the use of % instead of mod or modulo. > > > I didnt know one of Python's design goals was backwards compatibility > > with C. > > Don't be silly. You know full well Python is not backwards compatible > with C, even if they do share some syntactical features. Of course I was being silly; I know this use is following a historical precedent; but id rather see it rectified in the previous version of python rather than the next. My sillyness was prompted by the percieved pointlessness of your remark. Of course python is not trying to be backwards compatible with C; so why bring it up then? > >> ?If you can supply any function at all, what happens if I write this: > > > You cannot; only constructors modelling a sequence or a dict, and only > > in that order. Is that rule clear enough? > > But why limit yourself to those restrictive rules? > > If I want to collect a sequence of arguments into a string, why shouldn't > I be allowed to write this? > > ? ? def func(parg, str(args)): ... > > If I want to sum a collection of arguments, why not write this? > > ? ? def func(pargs, sum(args)): ... > > Isn't that better than this? > > ? ? def func(pargs, *args): > ? ? ? ? args = sum(args) > ? ? ? ? ... > > But no. I don't mean those examples to be taken seriously: when you > answer to your own satisfaction why they are bad ideas, you may be closer > to understanding why I believe your idea is also a bad idea. They are bad ideas because they truely do not lead to the execution of different code, but are merely a reordering, mixing statements in with a function declaration. I am proposing no such thing; again, the type(arg) notation I have dropped, and never was meant to have anything to do with function calling; it is a way of supplying an optional type constraint, so in analogy with function annotations, I changed that to arg::type. Again, this has nothing to do with calling functions on arguments. First off, type constraints must have some use; all those languages cant be wrong. Ofcourse, no type constraints also can not be wrong; see all those other languages. And I obviously dont mean to make type constraits mandatory in python, all im saying is that optionally allowing them can open some doors in the right places. The * and ** syntax are also in effect type constraints, saying 'this is a dict to collect the remaining kwargs in', but in a rather cryptic and needlessly ungeneral method. #define a function with args-list and kwargs-attrdict def func(args::list, kwargs::attrdict) #unpack the sequence args into positional arguments, and the mapping kwargs into keyword arguments func(::args, ::kwargs) So now that I have explained what my idea actually is, perhaps youd like to explain again why it is a bad idea? > >> I believe that your proposal leads to an over-generalisation "call > >> arbitrary functions when handling parameter lists". > > > I hope the above clears that up. It is as much about calling functions > > as ** is about raising kwargs to the power of. > > I don't understand this comment. Nobody has suggested that ** in function > parameter lists is the exponentiation operator. No more or less than I suggested this is about calling functions on input parameters. The syntax might be superficially similar, but thats all. > As for "calling functions", how else do you expect to generate a type if > you don't call the type constructor? One of your early examples was > something like: > > def func(parg, attrdict(kwargs)): ... > > If you expect kwargs to be an attrdict, which is not a built-in, > presumably you will have needed to have defined attrdict as a type or > function, and this type or function will get called at run time to > collect the kwargs. That is all. I would like attrdict to be a builtin, and as long as it isnt, that specific example probably wouldnt work. But all I wnat python to do is whatever it is doing to build a dict, but then for another type instead. Hence, a typeconstraint. > > >> ?> ?head, tuple(tail) = iterable > >> ?In Python 3, that is spelled: > >> ?head, *tail = iterable > >> ?tail = tuple(tail) > > > Yes, I know. How is that not a lot more verbose and worse than what I > > have proposed in all possible ways? > > That *specific* syntax, outside of function declarations, is something > I've often thought might be useful. But if I were to argue its case, I > would allow arbitrary functions, and treat it as syntactic sugar for: > > head, *tail = iterable > tail = func(tail) ?# or possibly func(*tail) > > But that's pie-in-the-sky musing. I certainly wouldn't put it in function > parameter lists. Param lists should be declarations, not code. Apart from > the most limited collation of args, code belongs inside the body of the > function, not the param list: I agree that this is a bad idea; see the above. > >> head, tail = somestring[0], somestring[1:] > > > Well yes, splendid; we can do that with lists too since the dawn of > > Python. What you are saying here in effect is that you think the > > head/tail syntax is superfluous; that youd rather see it eliminated than > > generalized. > > No. > > It is not "head/tail" syntax, but sequence unpacking, and it has been > nicely generalised to allow things like this in Python 3: > > a, b, c, d, *lump, x, y z = any_iterable > > any_iterable isn't limited to a list, str, or other object which supports > slicing. It can be any object supporting the iteration protocol. > > What I'm saying is that there is no need to OVER-generalise this to > specify the type of *lump within the packing operation. If you want lump > to be something other that Python's choice, perform the conversion > yourself. Oh, so your primary objection is a semantic nitpick, and the latter is an OVERgeneralization? Why not restrict unpacking only to lists as RHS, and convert all your iterables to lists first? Who needs this overgeneralization of allowing any sequence type to be unpacked, right? To answer that question: for the same reasons. The conversion is wasteful; allowing python to do the right thing based on a typeconstraint is not. Plus, it is less code, and more readable code; the only rule you have to learn is quite general, which is that :: is a type constraint annotation; no need to remember specifics, like 'unpacking always returns lists for some arbitrary reason'. From hoogendoorn.eelco at gmail.com Tue Dec 13 04:26:02 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 01:26:02 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> <2ff41b2e-90e3-4fae-9951-67bb257ed6ed@m10g2000vbc.googlegroups.com> Message-ID: <824d74b4-5495-447a-a9cb-addc22092a44@da3g2000vbb.googlegroups.com> > > Python users generally follow the rule "explicit is better than > > implicit". ?Setting a general constraint and letting the language "do > > the right thing" is a kind of black magic that feels off because it > > tends to break that rule. ?But that's not to say that black magic > > never wins -- just look at super() and the MRO. > > We are not talking black magic here; we are talking about an EXPLICIT > type constraint provided on the very same line. To hammer that point home: would you say the following C# code performs 'black magic'? float x = 3; After all, exactly the same thing is going on; C# will 'do the right thing'; convert the integer literal 3 to a floating point value, based on the type constraint placed on x. From fabiofz at gmail.com Tue Dec 13 04:33:38 2011 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Tue, 13 Dec 2011 07:33:38 -0200 Subject: PyDev and multithreaded application debug In-Reply-To: <3f193cae-deab-4d3f-a7d6-01ae4611515d@t16g2000vba.googlegroups.com> References: <3f193cae-deab-4d3f-a7d6-01ae4611515d@t16g2000vba.googlegroups.com> Message-ID: On Mon, Dec 12, 2011 at 2:30 PM, Massi wrote: > Hi everyone, I've just started to use pydev to develop my python > application and I'm encountering some problems to debug it. The > application I'm dealing with is a multithreaded application; when I > try to debug it with pydev, it seems not to be able to handle the > execution of multiple threads. The problem is that when the execution > reaches a breakpoint in one of the threads, all the other ones don't > stop, but they continue their execution. > Have I to make some specific settings or am I missing something? > Thanks in advance! Actually, that's expected, as you've only hit the breakpoint at that thread... if you want, you can press the pause button to stop the other threads. Cheers, Fabio From hoogendoorn.eelco at gmail.com Tue Dec 13 04:50:18 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 01:50:18 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> Message-ID: > To answer that question: for the same reasons. The conversion is > wasteful; allowing python to do the right thing based on a > typeconstraint is not. Plus, it is less code, and more readable code; > the only rule you have to learn is quite general, which is that :: is > a type constraint annotation; no need to remember specifics, like > 'unpacking always returns lists for some arbitrary reason'. Oh my bad; actually, that should be: 'collecting the remainder of an unpacked iterable using * will always yield a list. That is, unless the construct appears inside a function definition; then somehow a tuple is always the right choice' From peter at www.pjb.com.au Tue Dec 13 04:53:32 2011 From: peter at www.pjb.com.au (Peter Billam) Date: 13 Dec 2011 09:53:32 GMT Subject: Outputting raw MIDI in realtime on Linux References: <77fe6b9f-b148-4855-b238-9089678908de@n22g2000prh.googlegroups.com> <1683c805-7e4a-4ab4-8b6b-e825b09a9252@v24g2000prn.googlegroups.com> Message-ID: On 2011-12-12, alex23 wrote: > On Dec 12, 12:14?pm, Nick Irvine wrote: >> What do people use to output live MIDI on Linux, assuming it's >> possible? > I've yet to try this myself although it's long been on my to-do list. > There are a couple of packages on PyPI that emit MIDI: > http://pypi.python.org/pypi?%3Aaction=search&term=midi Check out http://www.pjb.com.au/midi/index.html http://www.pjb.com.au/midi/MIDI.html but what you _really_ want is Patricio Paez' alsaseq and alsamidi http://pp.com.mx/python/alsaseq which I translated into Lua and Perl: http://www.pjb.com.au/comp/lua/midialsa.html http://search.cpan.org/perldoc?MIDI::ALSA although by now my translations have developed slightly more features and fewer quirks than the original... Hope this helps, Peter -- Peter Billam www.pjb.com.au www.pjb.com.au/comp/contact.html From jeanmichel at sequans.com Tue Dec 13 04:54:51 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 13 Dec 2011 10:54:51 +0100 Subject: Overriding a global In-Reply-To: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EE720EB.40301@sequans.com> Steven D'Aprano wrote: > On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: > > >> Using the same name for 2 different objects is a bad idea in general. >> > > We have namespaces precisely so you don't need to care about making names > globally unique. > > > I don't get your point, namespaced names are unique, by definition. foo.aname <> bar.aname The OP showed a code where there was a confusion between a global name and a local one. There's no namespace involved. Having a local name identical to a global one is a bad idea, def. JM From arnodel at gmail.com Tue Dec 13 05:15:33 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Tue, 13 Dec 2011 10:15:33 +0000 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> Message-ID: On 13 December 2011 09:50, Eelco wrote: >> To answer that question: for the same reasons. The conversion is >> wasteful; allowing python to do the right thing based on a >> typeconstraint is not. Plus, it is less code, and more readable code; >> the only rule you have to learn is quite general, which is that :: is >> a type constraint annotation; no need to remember specifics, like >> 'unpacking always returns lists for some arbitrary reason'. > > Oh my bad; actually, that should be: > > 'collecting the remainder of an unpacked iterable using * will always > yield a list. That is, unless the construct appears inside a function > definition; then somehow a tuple is always the right choice' When you quote somebody (even yourself), it would be helpful if you attributed your quote. -- Arnaud From hoogendoorn.eelco at gmail.com Tue Dec 13 05:39:52 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 02:39:52 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> Message-ID: On 13 dec, 11:15, Arnaud Delobelle wrote: > On 13 December 2011 09:50, Eelco wrote: > > >> To answer that question: for the same reasons. The conversion is > >> wasteful; allowing python to do the right thing based on a > >> typeconstraint is not. Plus, it is less code, and more readable code; > >> the only rule you have to learn is quite general, which is that :: is > >> a type constraint annotation; no need to remember specifics, like > >> 'unpacking always returns lists for some arbitrary reason'. > > > Oh my bad; actually, that should be: > > > 'collecting the remainder of an unpacked iterable using * will always > > yield a list. That is, unless the construct appears inside a function > > definition; then somehow a tuple is always the right choice' > > When you quote somebody (even yourself), it would be helpful if you > attributed your quote. > > -- > Arnaud Ah yes; im more used to proper forums, still getting used to these mailing-list things. But point taken. From hoogendoorn.eelco at gmail.com Tue Dec 13 05:46:13 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 02:46:13 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> Message-ID: <1b95ae2c-1e94-4860-9dc8-1e5a2ff0537d@i8g2000vbh.googlegroups.com> With all this being said, I must say that the notion of indtroducing type constraints into Python is quite a radical one*, and one that should not be taken lightly, so I understand the general conservative vibe the notion is getting. It probably has implications beyond just collection types, and if youd introduce such a feature, you would like to introduce it only once, and correctly the first time around. Ill probably start a new thread soon, recapping the accumulated insight, and capping all the OT threads that have spawned. *even though the asteriks syntax is infact a limited form of exactly that From andrea.crotti.0 at gmail.com Tue Dec 13 05:59:00 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 10:59:00 +0000 Subject: change a file with a context manager Message-ID: <4EE72FF4.8010203@gmail.com> So I have the following problem, I need something to copy a file to a certain position and restore it after. So I wrote this simple context manager: class WithCorrectEasyInstall(object): def __enter__(self): import pkg_resources from shutil import copyfile easy_install_file = pkg_resources.resource_filename('psi.devsonly', 'windows_easy_install.pth') self.global_easy_install = 'c:/python25/Lib/site-packages/easy-install.pth' # can use mmap, a temporary file or just write to string self.stored = open(self.global_easy_install).read() logger.debug("copying the correct easy_install.pth file to the global position") copyfile(easy_install_file, self.global_easy_install) def __exit__(self, type, value, traceback): #TODO: make sure it's restored also when quitting in some other ways # restore the old file open(self.global_easy_install, 'w').write(self.stored) logger.debug("restoring the old easy_install.pth file") The problem is that this is not executed for example when the program is interrupted with for example a KeyboardInterrupt. But that's also an exception, why is it not caught by the context manager? And if there are better way to do what I'm trying to achieve they are welcome :) From gawali.jitesh at gmail.com Tue Dec 13 06:08:10 2011 From: gawali.jitesh at gmail.com (Jitesh) Date: Tue, 13 Dec 2011 03:08:10 -0800 (PST) Subject: NumPy book review copies Message-ID: Hi, As a part of our reviewing program, we are giving away limited number of copies (print & electronic) of our recent publication NumPy 1.5 Beginner's Guide to people interested in reviewing the book. Book-link: http://link.packtpub.com/sibctR You need to publish your review/feedback on either your blog or on websites like Barnes and Noble, Slashdot and so on as per your choice. We also encourage uploading the review on Amazon since it gives buyers a chance to know about the book through your perspective. For more information, please get in touch with me on my email jiteshg AT packtpub DOT com Thanks, Jitesh From steve+comp.lang.python at pearwood.info Tue Dec 13 06:13:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 11:13:16 GMT Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> Message-ID: <4ee7334c$0$29979$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Dec 2011 01:15:46 -0800, Eelco wrote: > On Dec 13, 3:43?am, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Mon, 12 Dec 2011 04:21:15 -0800, Eelco wrote: >> >> No more, or less, explicit than the difference between "==" and >> >> "is". >> >> > == may be taken to mean identity comparison; 'equals' can only mean >> > one thing. [...] > We are not talking mathemathics, we are talking programming languages. What *I* am talking about is your assertion that there is only one possible meaning for "equals" in the context of a programing language. This is *simply not correct*. You don't have to believe me, just look at the facts. It's hard to find languages that use the word "equals" (or very close to it) rather than equals signs, but here are four languages which do: (1) OpenXION: Equals in OpenXION is weakly typed, like Perl: >1 + 1.0 equals "2" true (2) C# uses method notation: a.Equals(b) can be overridden, but for many types it defaults to value equality, that is, the equivalent to Python's a == b. (3) Ruby uses a.equal?(b) for "reference equality", that is, the equivalent of Python's "is" operator: irb(main):001:0> a = "abc" => "abc" irb(main):002:0> b = "abc" => "abc" irb(main):003:0> a.equal?(b) => false irb(main):004:0> a == b => true (4) Mathematica's Equal[x, y] can test values and expressions for equality. It may return true, false, or unevaluated (i.e. itself). Four languages that use Equals (or close to it) with four different behaviours. > Identity versus value equality is a well established concept within its > jargon. Within this context, 'equals' and 'is' have clearly defined > meanings. Incorrect. Most programming languages do not even have a concept of identity: identity is only(?) relevant to reference languages, like Python, where variables are references to objects. Even for languages that have a concept of identity, most don't don't call it "is". Objective-C calls it "==", PHP calls it "===", C# calls it object.ReferenceEquals. (Python, Algol 68, and VB .NET are three which do call it "is".) For stack-based languages like Forth, it doesn't even make sense to talk about identity, since values aren't variables: they're just values on a stack, not values in a fixed location, or bound to a known name. Again, all this goes to demonstrate that the language designer is free to choose any behaviour they like, and give it any name they like. [...] > So 'frequency of use' is no valid interpretation of 'obscurity'? Im not > a native speaker, but im pretty sure it is. No. Things which are obscure are used in language infrequently, because if they were common they would not be obscure. But things which are used infrequently are not necessarily obscure. An example in common language: "Napoleon Bonaparte" does not come up in conversation very frequently, but he is not an obscure historical figure. An example from programming: very few people need to use the trigonometric functions sin, cos, tan in their code. But they are not obscure functions: most people remember them from school. People who have forgotten almost everything about mathematics except basic arithmetic probably remember sin, cos and tan. But they never use them. >> >> And you can blame C for the use of % instead of mod or modulo. >> >> > I didnt know one of Python's design goals was backwards compatibility >> > with C. >> >> Don't be silly. You know full well Python is not backwards compatible >> with C, even if they do share some syntactical features. > > Of course I was being silly; I know this use is following a historical > precedent; but id rather see it rectified in the previous version of > python rather than the next. My sillyness was prompted by the percieved > pointlessness of your remark. Of course python is not trying to be > backwards compatible with C; so why bring it up then? Because you asked why Python uses the % operator for remainder. [...] > They are bad ideas because they truely do not lead to the execution of > different code, but are merely a reordering, mixing statements in with a > function declaration. I am proposing no such thing; again, the type(arg) > notation I have dropped, and never was meant to have anything to do with > function calling; it is a way of supplying an optional type constraint, > so in analogy with function annotations, I changed that to arg::type. > Again, this has nothing to do with calling functions on arguments. You have not thought about this carefully enough. Consider what happens when this code gets called: def f(*args): pass f(a, b, c) The Python virtual machine (interpreter, if you prefer) must take three arguments a, b, c and create a tuple from them. This must happen at runtime, because the value of the objects is not known at compile time. So at some point between f(a, b, c) being called and the body of f being entered, a tuple must be created, and the values of a, b, c must be collated into a single tuple. Now extend this reasoning to your proposal: def f(args:FOO): pass At runtime, the supplied arguments must be collated into a FOO, whatever FOO happens to be. Hence, the function that creates FOO objects must be called before the body of f can be entered. This doesn't happen for free. Whether you do it manually, or have the Python interpreter do it, it still needs to be done. > First off, type constraints must have some use; all those languages cant > be wrong. But you're not talking about type constraints. You're not instructing the function to reject arguments which have the wrong type, you are instructing it to collate multiple arguments into a list (instead of a tuple like Python currently does). def f(*args) *constructs* a tuple, it doesn't perform a type-check. -- Steven From steve+comp.lang.python at pearwood.info Tue Dec 13 06:15:32 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 11:15:32 GMT Subject: Overriding a global References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Dec 2011 10:54:51 +0100, Jean-Michel Pichavant wrote: > Steven D'Aprano wrote: >> On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: >> >> >>> Using the same name for 2 different objects is a bad idea in general. >>> >>> >> We have namespaces precisely so you don't need to care about making >> names globally unique. >> >> >> > I don't get your point, namespaced names are unique, by definition. > > foo.aname <> bar.aname Assuming foo and bar are not names for the same object, there are at least three namespaces here: the local namespace, where foo and bar can be found, the foo.__dict__ namespace, and the bar.__dict__ namespace. > The OP showed a code where there was a confusion between a global name > and a local one. There's no namespace involved. Having a local name > identical to a global one is a bad idea, def. Of course there are namespaces involved. There is the global namespace, and the local namespace. That's how you can have x inside a function without it overwriting global x outside of it, because they are different namespaces. Which is my point. When I write this: x = 1 def spam(): x = 2 def ham(): x = 3 The three x's don't clash because they are in three separate namespaces. -- Steven From corleone82 at gmail.com Tue Dec 13 06:21:07 2011 From: corleone82 at gmail.com (Juan Perez) Date: Tue, 13 Dec 2011 11:21:07 +0000 Subject: automate commands to an .exe console program through python In-Reply-To: References: Message-ID: Well, if developers have to change something to the code, I'd continue with autoit, since is closer to real enviroment. You are right, autoit work by send parametres to GUI focused, but really I don't think it's something necessary for my purposes as I need only send parameters to a windows cmd console. Do you know if is that possible by piping standard input? I've tried but with no results. What I'd need is open a new windows console get the pid and send messages to it through stdin pipe. But I've been unable to do it so far. Thanks, Juan 2011/12/12 Chris Angelico > On Tue, Dec 13, 2011 at 3:41 AM, Juan Perez wrote: > > I need to automate a .exe console program in windows, and send key > > characters like 'a', 'M' ... > > I had this running in an autoIT script which with only activating cmd > > window, and with the "send" parameter I had all already done. But I'm > having > > a lot of problems with python, I've tried with process.call, > communicate(), > > os.popen ... but no results. I can't comunicate with the program and > even i > > can't maintain it opened more than a few seconds. If I open a program > like > > notepad.exe it remains opened but when I try to comunicate with the stdin > > pipe just don't write anything to the program. > > GUI programs such as Notepad usually don't read from STDIN, which is > where text goes if you write to a pipe. You may have to check out how, > exactly, the program accepts commands; your autoIT script is probably > sending keystrokes using the Wndows GUI, so it works as long as the > program has focus. > > Ideally, look into whether the program has an actual automation mode - > and if it doesn't have one, ask the programmer to expose his code > directly to a Python script. Hey, it's worth a shot! :) > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue Dec 13 06:24:59 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 22:24:59 +1100 Subject: automate commands to an .exe console program through python In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 10:21 PM, Juan Perez wrote: > Well, if developers have to change something to the code, I'd continue with > autoit, since is closer to real enviroment. You are right, autoit work by > send parametres to GUI focused, but really I don't think it's something > necessary for my purposes as I need only send parameters to a windows cmd > console. Do you know if is that possible by piping standard input? I've > tried but with no results. What I'd need is open a new windows console get > the pid and send messages to it through stdin pipe. But I've been unable to > do it so far. The easiest way to see whether a pipe will work or not is to put the commands into a file and use redirection: NameOfProgram <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> <1b95ae2c-1e94-4860-9dc8-1e5a2ff0537d@i8g2000vbh.googlegroups.com> Message-ID: <4ee736d6$0$29979$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Dec 2011 02:46:13 -0800, Eelco wrote: > With all this being said, I must say that the notion of indtroducing > type constraints into Python is quite a radical one*, Not that radical. Here's the creator of Python musing about adding optional type checks to Python: http://www.artima.com/weblogs/viewpost.jsp?thread=85551 http://www.artima.com/weblogs/viewpost.jsp?thread=86641 http://www.artima.com/weblogs/viewpost.jsp?thread=87182 [...] > *even though the asteriks syntax is infact a limited form of exactly > that It absolutely is not. def f(*args, **kwargs) constructs a tuple and a dict, it does not type-check that the function is passed a tuple and a dict as arguments. These are completely different things. -- Steven From mail at timgolden.me.uk Tue Dec 13 06:34:31 2011 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 13 Dec 2011 11:34:31 +0000 Subject: automate commands to an .exe console program through python In-Reply-To: References: Message-ID: <4EE73847.30404@timgolden.me.uk> On 13/12/2011 11:21, Juan Perez wrote: > Well, if developers have to change something to the code, I'd continue > with autoit, since is closer to real enviroment. You are right, autoit > work by send parametres to GUI focused, but really I don't think it's > something necessary for my purposes as I need only send parameters to a > windows cmd console. Do you know if is that possible by piping standard > input? I've tried but with no results. What I'd need is open a new > windows console get the pid and send messages to it through stdin pipe. > But I've been unable to do it so far. I think that you're thinking about this the wrong way. You're talking about automating a Console program as though it were notepad: by sending keystrokes. Mixed in with that you've got some kind of notion of piping stdin/stdout. If you've got a (non-GUI) program which you usually run in a Console context, it will either take its input from stdin -- in which case, ignore the fact it runs in a console and feed it via subprocess -- or it will take input from some kind of Console-level getch method, in which case you'll have to use something like SendKeys [1] or perhaps WinPexpect [2] Don't know if that helps at all... TJG [1] http://www.rutherfurd.net/python/sendkeys/ [2] https://bitbucket.org/geertj/winpexpect/wiki/Home From storchaka at gmail.com Tue Dec 13 07:17:30 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 13 Dec 2011 14:17:30 +0200 Subject: Fixing the XML batteries In-Reply-To: References: Message-ID: <4EE7425A.2090006@gmail.com> 09.12.11 17:09, Dirkjan Ochtman wrote: > On Fri, Dec 9, 2011 at 09:02, Stefan Behnel wrote: > An at least somewhat informed +1 from me. The ElementTree API is a > very good way to deal with XML from Python, and it deserves to be > promoted over the included alternatives. > > Let's deprecate the NiCad batteries and try to guide users toward the > Li-Ion ones. I use xml.dom.minidom for XML canonization and convertion: doc = xml.dom.minidom.parse( io.BytesIO( data0 ) ) encoding = forceEncoding or doc.encoding or str( 'UTF-8' ) writer = io.BytesIO() writer = codecs.getwriter( encoding )( writer, 'xmlcharrefreplace' ) doc.writexml( writer, encoding = encoding ) data = writer.getvalue() How to do this with xml.etree.ElementTree? From storchaka at gmail.com Tue Dec 13 07:18:26 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 13 Dec 2011 14:18:26 +0200 Subject: Fixing the XML batteries In-Reply-To: References: Message-ID: <4EE74292.4080708@gmail.com> 09.12.11 17:09, Dirkjan Ochtman wrote: > On Fri, Dec 9, 2011 at 09:02, Stefan Behnel wrote: > An at least somewhat informed +1 from me. The ElementTree API is a > very good way to deal with XML from Python, and it deserves to be > promoted over the included alternatives. > > Let's deprecate the NiCad batteries and try to guide users toward the > Li-Ion ones. I use xml.dom.minidom for XML canonization and convertion: doc = xml.dom.minidom.parse( io.BytesIO( data0 ) ) encoding = forceEncoding or doc.encoding or str( 'UTF-8' ) writer = io.BytesIO() writer = codecs.getwriter( encoding )( writer, 'xmlcharrefreplace' ) doc.writexml( writer, encoding = encoding ) data = writer.getvalue() How to do this with xml.etree.ElementTree? From hoogendoorn.eelco at gmail.com Tue Dec 13 07:19:26 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 04:19:26 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> <1b95ae2c-1e94-4860-9dc8-1e5a2ff0537d@i8g2000vbh.googlegroups.com> <4ee736d6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13 dec, 12:28, Steven D'Aprano wrote: > On Tue, 13 Dec 2011 02:46:13 -0800, Eelco wrote: > > With all this being said, I must say that the notion of indtroducing > > type constraints into Python is quite a radical one*, > > Not that radical. Here's the creator of Python musing about adding > optional type checks to Python: > > http://www.artima.com/weblogs/viewpost.jsp?thread=85551http://www.artima.com/weblogs/viewpost.jsp?thread=86641http://www.artima.com/weblogs/viewpost.jsp?thread=87182 Good find; but still radical enough that it hasnt been implemented. Note that these musing are trying to adress a yet far more general problem of specifying arbitrary types constraints on anything; I am primarily interested in specifying container types in the special case of collection packing/unpacking syntax, with further extensions nothing but a welcome addon. The fact that the former was judged infeasible does not mean the more modest goal of the latter might not be attainable. > > *even though the asteriks syntax is infact a limited form of exactly > > that > > It absolutely is not. def f(*args, **kwargs) constructs a tuple and a > dict, it does not type-check that the function is passed a tuple and a > dict as arguments. These are completely different things. Which is of course not something I ever proposed; I never said anything about checking types of existing data; im talking about coercing types of newly created data, like the target of a collection packing. That is exactly what *args and **kwargs also do. From stefan_ml at behnel.de Tue Dec 13 07:32:53 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 13 Dec 2011 13:32:53 +0100 Subject: Fixing the XML batteries In-Reply-To: <4EE7425A.2090006@gmail.com> References: <4EE7425A.2090006@gmail.com> Message-ID: Serhiy Storchaka, 13.12.2011 13:17: > 09.12.11 17:09, Dirkjan Ochtman wrote: >> An at least somewhat informed +1 from me. The ElementTree API is a >> very good way to deal with XML from Python, and it deserves to be >> promoted over the included alternatives. >> >> Let's deprecate the NiCad batteries and try to guide users toward the >> Li-Ion ones. I stripped my name from the quoted context because I didn't say this. > I use xml.dom.minidom for XML canonization and convertion: Do you mean "canonicalisation"? I.e. C14N? That's not what the code below is doing, not at all. > doc = xml.dom.minidom.parse( io.BytesIO( data0 ) ) > encoding = forceEncoding or doc.encoding or str( 'UTF-8' ) > writer = io.BytesIO() > writer = codecs.getwriter( encoding )( writer, 'xmlcharrefreplace' ) > doc.writexml( writer, encoding = encoding ) > data = writer.getvalue() > > How to do this with xml.etree.ElementTree? In Python 2.7/3.2, ElementTree has support for C14N serialisation, just pass the option method="c14n". Stefan From corleone82 at gmail.com Tue Dec 13 07:35:28 2011 From: corleone82 at gmail.com (Juan Perez) Date: Tue, 13 Dec 2011 12:35:28 +0000 Subject: automate commands to an .exe console program through python In-Reply-To: <4EE73847.30404@timgolden.me.uk> References: <4EE73847.30404@timgolden.me.uk> Message-ID: Thank you for your responses, My problem is thinking in windows console somewhat like linux shell, and do same things I did with pipes in C programming. But it seems not to be the case. At a glance I dare say that sendkeys module is useful enough, but if I've understood well, you only can send messages to focused window, that will report some inestabilities if windows focused is changed. You won't be able to touch anything in the current laptop. So, is there any way to identify the a process/window and send keystrokes in a smarter way and/or is it possible to identify a window an get the focus on it? I'm worried that pexpect is a too dificult way... Thanks, Juan 2011/12/13 Tim Golden > On 13/12/2011 11:21, Juan Perez wrote: > >> Well, if developers have to change something to the code, I'd continue >> with autoit, since is closer to real enviroment. You are right, autoit >> work by send parametres to GUI focused, but really I don't think it's >> something necessary for my purposes as I need only send parameters to a >> windows cmd console. Do you know if is that possible by piping standard >> input? I've tried but with no results. What I'd need is open a new >> windows console get the pid and send messages to it through stdin pipe. >> But I've been unable to do it so far. >> > > I think that you're thinking about this the wrong way. You're talking > about automating a Console program as though it were notepad: by sending > keystrokes. Mixed in with that you've got some kind of notion of piping > stdin/stdout. > > If you've got a (non-GUI) program which you usually run in a Console > context, it will either take its input from stdin -- in which case, > ignore the fact it runs in a console and feed it via subprocess -- > or it will take input from some kind of Console-level getch method, > in which case you'll have to use something like SendKeys [1] or perhaps > WinPexpect [2] > > Don't know if that helps at all... > > TJG > > > [1] http://www.rutherfurd.net/**python/sendkeys/ > > [2] https://bitbucket.org/geertj/**winpexpect/wiki/Home > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue Dec 13 07:40:50 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 13 Dec 2011 23:40:50 +1100 Subject: automate commands to an .exe console program through python In-Reply-To: References: <4EE73847.30404@timgolden.me.uk> Message-ID: On Tue, Dec 13, 2011 at 11:35 PM, Juan Perez wrote: > My problem is thinking in windows console somewhat like linux shell, and do > same things I did with pipes in C programming. But it seems not to be the > case. It is (at least, it supports the basics of pipes and redirection); but what you're looking at here is an application that appears not to use STDIN. > At a glance I dare say that sendkeys module is useful? enough, but if I've > understood well, you only can send messages to focused window, that will > report some inestabilities if windows focused is changed. You won't be able > to touch anything in the current laptop. So, is there any way to identify > the a process/window and send keystrokes in a smarter way and/or is it > possible to identify a window an get the focus on it? You may be able to put focus to the window before sending it keys, but this is sounding extremely hackish (nearly as bad as the Magic: The Gathering Online trade-bot system). I'm thinking here the best option may be to give this a dedicated virtual machine and let it do whatever it likes - effectively, have an entire "computer" dedicated to just this application and its automation harness. That would probably make things simple enough that you don't need to worry too much about focus, sending keystrokes to other windows, etc. ChrisA From hoogendoorn.eelco at gmail.com Tue Dec 13 07:47:58 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 04:47:58 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> <4ee7334c$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13 dec, 12:13, Steven D'Aprano wrote: > On Tue, 13 Dec 2011 01:15:46 -0800, Eelco wrote: > > On Dec 13, 3:43?am, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> wrote: > >> On Mon, 12 Dec 2011 04:21:15 -0800, Eelco wrote: > >> >> No more, or less, explicit than the difference between "==" and > >> >> "is". > > >> > == may be taken to mean identity comparison; 'equals' can only mean > >> > one thing. > [...] > > We are not talking mathemathics, we are talking programming languages. > > What *I* am talking about is your assertion that there is only one > possible meaning for "equals" in the context of a programing language. > This is *simply not correct*. > > You don't have to believe me, just look at the facts. It's hard to find > languages that use the word "equals" (or very close to it) rather than > equals signs, but here are four languages which do: That python is not the only language to not get this quite as right as could be is known to me. But within computer science as a discipline, equality and identity comparisons have a clear enough meaning. 'is' has a narrower meaning than 'equals'. '==' has no meaning whatsoever in computer science. > Again, all this goes to demonstrate that the language designer is free to > choose any behaviour they like, and give it any name they like. Certainly you demonstrated as much. Programming languages are created by people, and they have a tendency to make mistakes, and to have to compromise (backwards compatibility, and so on). Thats a seperate matter from 'what ought to be done', when discussing optimal language design. > > So 'frequency of use' is no valid interpretation of 'obscurity'? Im not > > a native speaker, but im pretty sure it is. > > No. Things which are obscure are used in language infrequently, because > if they were common they would not be obscure. But things which are used > infrequently are not necessarily obscure. > > An example in common language: "Napoleon Bonaparte" does not come up in > conversation very frequently, but he is not an obscure historical figure. > > An example from programming: very few people need to use the > trigonometric functions sin, cos, tan in their code. But they are not > obscure functions: most people remember them from school. People who have > forgotten almost everything about mathematics except basic arithmetic > probably remember sin, cos and tan. But they never use them. I dont think its terribly interesting to debate whether the term obscure applies to trigonometric functions or not: the important matter is that they are where they should be; under math.cos, etc. They dont have their own special character, and I hope you agree that is as it should be. I use trig far more often than modulus, so that argues in favor of modulus being under math too; infact I used modulus quite recently, but naturally it was in a piece of code that should be done in C eventually anyway (evaluating subdivision surfaces) > Because you asked why Python uses the % operator for remainder. So you ARE implying python has backwards compatibility with C as a design goal? Otherwise the given answer to this question is nonsensical. > > [...] > > > They are bad ideas because they truely do not lead to the execution of > > different code, but are merely a reordering, mixing statements in with a > > function declaration. I am proposing no such thing; again, the type(arg) > > notation I have dropped, and never was meant to have anything to do with > > function calling; it is a way of supplying an optional type constraint, > > so in analogy with function annotations, I changed that to arg::type. > > Again, this has nothing to do with calling functions on arguments. > > You have not thought about this carefully enough. Consider what happens > when this code gets called: > > def f(*args): pass > > f(a, b, c) > > The Python virtual machine (interpreter, if you prefer) must take three > arguments a, b, c and create a tuple from them. This must happen at > runtime, because the value of the objects is not known at compile time. > So at some point between f(a, b, c) being called and the body of f being > entered, a tuple must be created, and the values of a, b, c must be > collated into a single tuple. > > Now extend this reasoning to your proposal: > > def f(args:FOO): pass > > At runtime, the supplied arguments must be collated into a FOO, whatever > FOO happens to be. Hence, the function that creates FOO objects must be > called before the body of f can be entered. This doesn't happen for free. > Whether you do it manually, or have the Python interpreter do it, it > still needs to be done. Of course the python interpreted needs to do this; and in case non- builtin types are allowed, the mechanism is going to be through their constructor. But thats a detail; the syntax doesnt say: 'please call this constructor for me', any more than **kwargs says 'please call a dict constructor for me', even though equivalent operations are obviously going on under the hood as part of the process. Yes, whatever type you have needs to be constructed, and its not magically going to happen, its going to cost CPU cycles. The question is, do you end up constructing both a tuple and a list, or do you construct the list directly? > > First off, type constraints must have some use; all those languages cant > > be wrong. > > But you're not talking about type constraints. You're not instructing the > function to reject arguments which have the wrong type, you are > instructing it to collate multiple arguments into a list (instead of a > tuple like Python currently does). def f(*args) *constructs* a tuple, it > doesn't perform a type-check. I am talking about type constraints, but as seems to be the usual pattern in our miscommunications, you seek to attach a specific far fetched meaning to it that I never intended. Insofar as I understand these terms, a type-constraint is part of a declaration; float x; in C- family languages, args::list in python 4 perhaps. It narrows down the semantics for further usage of that symbol. A type-check is something along the lines of type(args)==list, a runtime thing and something completely different. I havnt mentioned the latter at all, explicitly or implicitly, as far as im aware. From rosuav at gmail.com Tue Dec 13 08:14:33 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Dec 2011 00:14:33 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> <4ee7334c$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Dec 13, 2011 at 11:47 PM, Eelco wrote: >> def f(*args) *constructs* a tuple, it >> doesn't perform a type-check. > > I am talking about type constraints... A type-check is something > along the lines of type(args)==list, a runtime thing and something > completely different. I havnt mentioned the latter at all, explicitly > or implicitly, as far as im aware. I'm not sure what you mean by a "type constraint". Here's how I understand such: float|int foobar; //Declare that the variable 'foobar' is allowed to hold a float or an int foobar = 3.2; //Legal. foobar = 1<<200; //Also legal (a rather large integer value) foobar = "hello"; //Not legal Python doesn't have any such thing (at least, not in-built). Any name may be bound to any value - or if you like, any variable can contain any type. Same applies to argument passing - you can even take an unbound method and call it with some completely different type of object as its first parameter. (I can't think of ANY situation in which this would not be insanely confusing, tbh.) When you gather a function's arguments into a tuple, list, or any other such container object, what you're doing is constructing another object and stuffing it with references to the original arguments. That involves building a new object, where otherwise you simply bind the arguments' names to the original objects directly. Now, it is perfectly conceivable to have designed Python to _always_ pass tuples around. Instead of a set of arguments, you just always pass exactly one tuple to a function, and that function figures out what to do with the args internally. If that's the way the language is built, then it is the _caller_, not the callee, who builds that tuple; and we still have the same consideration if we want a list instead. So suppose you can have a user-defined object type instead of list/dict. How are you going to write that type's __init__ function? Somewhere along the way, you need to take a variable number of arguments and bundle them up into a single one... so somewhere, you need the interpreter to build it for you. This is going to end up exactly the same as just accepting the tuple and then passing that to a constructor, like the list example. Keep things transparent and you make debugging a LOT easier. ChrisA From jeanmichel at sequans.com Tue Dec 13 08:30:42 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 13 Dec 2011 14:30:42 +0100 Subject: Overriding a global In-Reply-To: <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EE75382.9060104@sequans.com> Steven D'Aprano wrote: > On Tue, 13 Dec 2011 10:54:51 +0100, Jean-Michel Pichavant wrote: > > >> Steven D'Aprano wrote: >> >>> On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: >>> >>> >>> >>>> Using the same name for 2 different objects is a bad idea in general. >>>> >>>> >>>> >>> We have namespaces precisely so you don't need to care about making >>> names globally unique. >>> >>> >>> >>> >> I don't get your point, namespaced names are unique, by definition. >> >> foo.aname <> bar.aname >> > > Assuming foo and bar are not names for the same object, there are at > least three namespaces here: the local namespace, where foo and bar can > be found, the foo.__dict__ namespace, and the bar.__dict__ namespace. > > > >> The OP showed a code where there was a confusion between a global name >> and a local one. There's no namespace involved. Having a local name >> identical to a global one is a bad idea, def. >> > > Of course there are namespaces involved. There is the global namespace, > and the local namespace. That's how you can have x inside a function > without it overwriting global x outside of it, because they are different > namespaces. Which is my point. > > When I write this: > > x = 1 > > def spam(): > x = 2 > > def ham(): > x = 3 > > The three x's don't clash because they are in three separate namespaces. > > I know that, why do I have the feel this is juste a semantic issue ? Aren't you/I/we confusion namespaces & scopes ? Anyway, semantic is not what we're interested in here. writing x = 1 def spam(): x = 2 is in general a bad idea. That was my point. JM From hoogendoorn.eelco at gmail.com Tue Dec 13 08:35:27 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 05:35:27 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4ee554a3$0$11091$c3e8da3@news.astraweb.com> <5de94e20-314e-4185-ba8f-75e54f87968e@d10g2000vbk.googlegroups.com> <4ee6bbb3$0$11091$c3e8da3@news.astraweb.com> <5634cbdc-51dc-450b-aa74-2172173fac2d@y12g2000vba.googlegroups.com> <4ee7334c$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 13 dec, 14:14, Chris Angelico wrote: > On Tue, Dec 13, 2011 at 11:47 PM, Eelco wrote: > >> def f(*args) *constructs* a tuple, it > >> doesn't perform a type-check. > > > I am talking about type constraints... A type-check is something > > along the lines of type(args)==list, a runtime thing and something > > completely different. I havnt mentioned the latter at all, explicitly > > or implicitly, as far as im aware. > > I'm not sure what you mean by a "type constraint". Here's how I understand such: > > float|int foobar; //Declare that the variable 'foobar' is allowed to > hold a float or an int > foobar = 3.2; //Legal. > foobar = 1<<200; //Also legal (a rather large integer value) > foobar = "hello"; //Not legal > > Python doesn't have any such thing (at least, not in-built). Agreed on what a type constraint is, and that python does not really have any, unless one counts the current use of asterikses, which are infact a limited form of type constrait (*tail is not just any object, but one holding a list, which modifies the semantics of the assignment statement 'head,*tail=sequence' from a regular tuple unpacking to a specific form of the more general collection unpacking syntax); The idea is to enrich this syntax; to add optional and limited type constraints to python, specifically to enrich collection packing/ unpacking syntax, but perhaps the concept can be further generalized. > So suppose you can have a user-defined object type instead of > list/dict. How are you going to write that type's __init__ function? > Somewhere along the way, you need to take a variable number of > arguments and bundle them up into a single one... so somewhere, you > need the interpreter to build it for you. This is going to end up > exactly the same as just accepting the tuple and then passing that to > a constructor, like the list example. Keep things transparent and you > make debugging a LOT easier. Agreed; for user defined collection types there would not be a performance benefit over converting a tuple, since this is exactly what will happen anyway, but for collection types derived from any of the builtins, python could optimize away the intermediate and construct the desired collection type directly from the available information. From hfaber at invalid.net Tue Dec 13 08:36:39 2011 From: hfaber at invalid.net (Henrik Faber) Date: Tue, 13 Dec 2011 14:36:39 +0100 Subject: Signature-preserving decorators Message-ID: Hi group, when decorating a method in Python3, by use of the functools.update_wrapper function, it can be achieved that the docstring and name of the original function is preseverved. However, the prototype is lost: When looking into the Python help, I have lots of entries that look like: getfoo(*args, **kwargs) -> int setbar(*args, **kwargs) As you can imagine, this is really not very self-explanatory. I've seen a solution which constructs a wrapper's wrapper function using inspection and eval -- this looks really dirty to me, however. Then there's the "decorator" external module -- but I'd like to do it with on-board tools. Is this possible in Python3 with too much of a hassle? Best regards, Joe From andrea.crotti.0 at gmail.com Tue Dec 13 09:21:33 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 14:21:33 +0000 Subject: change a file with a context manager In-Reply-To: <4EE72FF4.8010203@gmail.com> References: <4EE72FF4.8010203@gmail.com> Message-ID: <4EE75F6D.5070604@gmail.com> On 12/13/2011 10:59 AM, Andrea Crotti wrote: > So I have the following problem, I need something to copy a file to a > certain position > and restore it after. > So I wrote this simple context manager: > > class WithCorrectEasyInstall(object): > > def __enter__(self): > import pkg_resources > from shutil import copyfile > easy_install_file = > pkg_resources.resource_filename('psi.devsonly', > 'windows_easy_install.pth') > self.global_easy_install = > 'c:/python25/Lib/site-packages/easy-install.pth' > # can use mmap, a temporary file or just write to string > self.stored = open(self.global_easy_install).read() > logger.debug("copying the correct easy_install.pth file to the > global position") > copyfile(easy_install_file, self.global_easy_install) > > def __exit__(self, type, value, traceback): > #TODO: make sure it's restored also when quitting in some > other ways > # restore the old file > open(self.global_easy_install, 'w').write(self.stored) > logger.debug("restoring the old easy_install.pth file") > > > The problem is that this is not executed for example when the program > is interrupted > with for example a KeyboardInterrupt. > But that's also an exception, why is it not caught by the context > manager? > > And if there are better way to do what I'm trying to achieve they are > welcome :) Alright it was probably a stupid idea to start with. The problem is that if the easy_install is wrong even the imports in the module will not work, so I can't even start to do something. Another possible option is to just add to the path everything contained in that correct easy_install.pth file. Or is there a way to reload the path settings? From dihedral88888 at googlemail.com Tue Dec 13 09:21:58 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 13 Dec 2011 06:21:58 -0800 (PST) Subject: Dynamic variable creation from string In-Reply-To: <4ee6abf8$0$11091$c3e8da3@news.astraweb.com> References: <28e7a11b-f61b-443b-85b4-adaa6ba5bd21@k5g2000pra.googlegroups.com> <24728986.18.1323694150411.JavaMail.geo-discussion-forums@prfc16> <2b414cd2-0f0a-45bf-b61e-9205704ac03a@u10g2000prl.googlegroups.com> <4ee6abf8$0$11091$c3e8da3@news.astraweb.com> Message-ID: <14074134.993.1323786118117.JavaMail.geo-discussion-forums@pruu5> On Tuesday, December 13, 2011 9:35:52 AM UTC+8, Steven D'Aprano wrote: > On Mon, 12 Dec 2011 15:45:06 -0800, alex23 wrote: > > > On Dec 12, 10:49?pm, 88888 Dihedral > > wrote: > >> This is the way to write an assembler or to roll out a script language > >> to be included in an app by users. > > > > This is a garbage comment that has absolutely nothing to do with the > > topic at hand _at all_. > > Please stop responding to the bot. > > > > -- > Steven Do we need to talk aout auto, extern, static in python? Of course not for an interpreter? From storchaka at gmail.com Tue Dec 13 09:27:56 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 13 Dec 2011 16:27:56 +0200 Subject: Fixing the XML batteries In-Reply-To: References: <4EE7425A.2090006@gmail.com> Message-ID: 13.12.11 14:32, Stefan Behnel ???????(??): > I stripped my name from the quoted context because I didn't say this. I am glad to hear this. ;) >> I use xml.dom.minidom for XML canonization and convertion: > Do you mean "canonicalisation"? I.e. C14N? That's not what the code > below is doing, not at all. No, only converting to certain textual representation. Converting entities to text (if possible), converting "'" to '"' for quoting, sorting attributes, etc. >> How to do this with xml.etree.ElementTree? > In Python 2.7/3.2, ElementTree has support for C14N serialisation, just > pass the option method="c14n". Thanks, I will try this. But I need 2.6+ compatibility. From stefan_ml at behnel.de Tue Dec 13 09:59:52 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 13 Dec 2011 15:59:52 +0100 Subject: Fixing the XML batteries In-Reply-To: References: <4EE7425A.2090006@gmail.com> Message-ID: Serhiy Storchaka, 13.12.2011 15:27: > 13.12.11 14:32, Stefan Behnel ???????(??): >> I stripped my name from the quoted context because I didn't say this. > > I am glad to hear this. ;) It matches my opinion though. >>> I use xml.dom.minidom for XML canonization and convertion: >> Do you mean "canonicalisation"? I.e. C14N? That's not what the code >> below is doing, not at all. > > No, only converting to certain textual representation. Converting entities > to text (if possible), converting "'" to '"' for quoting, sorting > attributes, etc. Yes, that's what C14N is there for, typically used for cryptography, hashing, etc. However, MiniDOM doesn't implement that standard, so you're on your own here. >>> How to do this with xml.etree.ElementTree? >> In Python 2.7/3.2, ElementTree has support for C14N serialisation, just >> pass the option method="c14n". > > Thanks, I will try this. But I need 2.6+ compatibility. The ET module is actually quite short (<1700 lines), so you can just copy the Py2.7 version into your sources and optionally import it on older Python releases. Since you only seem to depend on the serialiser (which is worth using anyway because it is much faster in the Py2.7 version), older platform versions of cET should also work just fine with that module copy, so you can basically just import everything from xml.etree.cElementTree and use the ElementTree class and the tostring() function from your own local version if the platform version is too old. Note that ET is also still available as a separately installable package, may or may not be simpler to use for you. Stefan From andrea.crotti.0 at gmail.com Tue Dec 13 10:42:49 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 15:42:49 +0000 Subject: logging issues Message-ID: <4EE77279.10104@gmail.com> I think is simple but I can't get it to work as I wish. Suppose I have a big application, my idea is that the running script sets a global logging level and then all the imported modules would act consequently. In my codebase, however, unless I set the level for each of the loggers I don't get the debug messages, not really sure why yet. To check if I understood, logging.getLogger() returns the root logger, and every other logger should inherit its settings, is that correct? What happens if there is another logging.getLogger() somewhere else in the code after the initialization? Does that become the root logger overriding the old one? The example below instead works as expected, so maybe is something with my codebase... # m1.py import logging logging.basicConfig() # if no name is specified return the root logger, that's how it works logger = logging.getLogger() logger.setLevel(logging.DEBUG) class ToLogEvents(object): def __init__(self): self.logger = logging.getLogger('ToLogEvents') def important(self): self.logger.warning("This is an important message") self.logger.debug("debug message") if __name__ == '__main__': logger.debug("debug from m1") from m2 import logging_function logging_function() t = ToLogEvents() t.important() # m2.py import logging logger = logging.getLogger(__name__) def logging_function(): logger.debug("debug message") logger.info("info message") From tinnews at isbd.co.uk Tue Dec 13 10:48:01 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Tue, 13 Dec 2011 15:48:01 +0000 Subject: Using methodcaller in a list sort - any examples anywhere? Message-ID: I want to sort a list of 'things' (they're fairly complex objects) by the contents of one of the fields I can extract from the 'things' using a Python function. So I have a list L which is a list of objects of some sort. I can output the contents of a field in the list as follows:- for k in L: print k.get_property('family-name') How can I sort the list first? As I said it seems like a methodcaller is the answer but I don't see how. I want to sort the list of objects not just produce a sorted list of names. -- Chris Green From gialloporpora at gmail.com Tue Dec 13 11:04:55 2011 From: gialloporpora at gmail.com (gialloporpora) Date: Tue, 13 Dec 2011 17:04:55 +0100 Subject: Getting the current directory path and the path where python script is saved Message-ID: Dear all, I would like to create a little script to rename my files, I think to use argparse module to create it. I would like to know how to obtain the path where script is executed and the path where the file script is stored. For example, if I save my script in c:\python27 and I put the directory in %path% I can execute it from any folder, I would like to get the two directory path. Thanks Sandro -- *gialloporpora: RT @themitcho: telnet miku.acm.uiuc.edu Do it now. You will not regret it. #nyan /via @beaulebens * - http://bit.ly/u2boVC Sto ascoltando: *Pink Floyd ? Echoe Part I * - http://bit.ly/vgIGQe From python.list at tim.thechases.com Tue Dec 13 11:08:06 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 13 Dec 2011 10:08:06 -0600 Subject: Using methodcaller in a list sort - any examples anywhere? In-Reply-To: References: Message-ID: <4EE77866.8060103@tim.thechases.com> On 12/13/11 09:48, tinnews at isbd.co.uk wrote: > I want to sort a list of 'things' (they're fairly complex objects) by > the contents of one of the fields I can extract from the 'things' > using a Python function. > > So I have a list L which is a list of objects of some sort. I can > output the contents of a field in the list as follows:- > > for k in L: > print k.get_property('family-name') > > How can I sort the list first? As I said it seems like a methodcaller > is the answer but I don't see how. I want to sort the list of objects > not just produce a sorted list of names. You want either sorted(..., key=...) to sort and return a copy (leaving the original unmodified) or .sort(key=...) to sort the list in-place: class MyObj(object): def __init__(self, fn): self.fn = fn def get_property(self, s): return "%s: %s" % (s, self.fn) def __str__(self): return self.fn __repr__ = __str__ source = [ MyObj("Doug"), MyObj("Carol"), MyObj("Bill"), MyObj("Adam"), ] print "Unsorted source before:" print repr(source) print "Using a lambda:" print repr(sorted(source, key=lambda mo: mo.get_property("family-name"))) print "Using methodcaller:" from operator import methodcaller print repr(sorted(source, key=methodcaller("get_property", "family-name"))) print "Source still unsorted after:" print repr(source) source.sort(key=lambda mo: mo.get_property("family-name")) print "Source now sorted:" print repr(source) yields the following: Unsorted source before: [Doug, Carol, Bill, Adam] Using a lambda: [Adam, Bill, Carol, Doug] Using methodcaller: [Adam, Bill, Carol, Doug] Source still unsorted after: [Doug, Carol, Bill, Adam] Source now sorted: [Adam, Bill, Carol, Doug] I'm partial to the lambda version over the methodcaller version unless there's a reason to dynamically get the method-name as a string. But that's just a personal preference. -tkc From jeanmichel at sequans.com Tue Dec 13 11:11:31 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 13 Dec 2011 17:11:31 +0100 Subject: logging issues In-Reply-To: <4EE77279.10104@gmail.com> References: <4EE77279.10104@gmail.com> Message-ID: <4EE77933.2060705@sequans.com> Andrea Crotti wrote: > I think is simple but I can't get it to work as I wish. > Suppose I have a big application, my idea is that the running > script sets a global logging level and then all the imported modules > would act consequently. > > In my codebase, however, unless I set the level for each of the loggers > I don't get the debug messages, not really sure why yet. > > To check if I understood, > logging.getLogger() returns the root logger, and every other logger > should inherit its settings, is that correct? Not really, if you use the classic way, your subloggers should be created an never configured. Their default behavior is to raise any log event to its parent. That way, all event logs end up being handled by the root logger. > > What happens if there is another logging.getLogger() somewhere else > in the code after the initialization? Does that become the root logger > overriding the old one? multiple calls to logging.getLogger() always return the very same root logger (the root one). Loggers are static objects handled by the loggin module, once a logger is created (identified by its name, the root logger's name is "") it will never be destroyed. Multiple call the getLogger(name) with the same name returns the same object. Logger names are of the form "grandparent.parent.child" > > The example below instead works as expected, so maybe is something > with my codebase... your codebase which is ??? [snip some working code] As a general advices - try to never configure a child logger, you should only need to create one whithout setting its level nor its handler. A logger is responsible of raising log events, not handling them. The root logger is supposed to handle them. - Configure only the root logger and do it in the main function, and only there. Configuring means calling basicConfig, setting the level, adding a handler etc... everything except the creation. JM From __peter__ at web.de Tue Dec 13 11:13:50 2011 From: __peter__ at web.de (Peter Otten) Date: Tue, 13 Dec 2011 17:13:50 +0100 Subject: Using methodcaller in a list sort - any examples anywhere? References: Message-ID: tinnews at isbd.co.uk wrote: > I want to sort a list of 'things' (they're fairly complex objects) by > the contents of one of the fields I can extract from the 'things' > using a Python function. > > So I have a list L which is a list of objects of some sort. I can > output the contents of a field in the list as follows:- > > for k in L: > print k.get_property('family-name') > > How can I sort the list first? As I said it seems like a methodcaller > is the answer but I don't see how. I want to sort the list of objects > not just produce a sorted list of names. The most obvious way is to use a custom function def get_family_name(obj): return obj.get_property("family-name") L.sort(key=get_family_name) However, since you already know about methodcaller """ class methodcaller(builtins.object) | methodcaller(name, ...) --> methodcaller object | | Return a callable object that calls the given method on its operand. | After, f = methodcaller('name'), the call f(r) returns r.name(). | After, g = methodcaller('name', 'date', foo=1), the call g(r) returns | r.name('date', foo=1). """" L.sort(key=methodcaller("get_property", "family-name")) From andrea.crotti.0 at gmail.com Tue Dec 13 11:21:15 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 16:21:15 +0000 Subject: logging issues In-Reply-To: <4EE77933.2060705@sequans.com> References: <4EE77279.10104@gmail.com> <4EE77933.2060705@sequans.com> Message-ID: <4EE77B7B.9090408@gmail.com> On 12/13/2011 04:11 PM, Jean-Michel Pichavant wrote: > Not really, if you use the classic way, your subloggers should be > created an never configured. Their default behavior is to raise any > log event to its parent. That way, all event logs end up being handled > by the root logger. > Ok thanks now it's more clear.your codebase which is ??? > > [snip some working code] > > As a general advices > - try to never configure a child logger, you should only need to > create one whithout setting its level nor its handler. A logger is > responsible of raising log events, not handling them. The root logger > is supposed to handle them. > - Configure only the root logger and do it in the main function, and > only there. Configuring means calling basicConfig, setting the level, > adding a handler etc... everything except the creation. > > JM It turned out that I was calling logger = logging.getLogger() In other modules too, and then apparently the logging level was modified automatically to the default one, is that possible? changing with logger = logging.getLogger(__name__) everywhere solved my problems.. From andrea.crotti.0 at gmail.com Tue Dec 13 11:24:05 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 16:24:05 +0000 Subject: boolean from a function Message-ID: <4EE77C25.1060600@gmail.com> I'm not sure for how long I had this bug, and I could not understand the problem. I had a function which would return a boolean def func_bool(): if x: return True else: return False Now somewhere else I had if func_bool: # do something I could not quite understand why it was always true, until I finally noticed that the () were missing. Is there some tool to avoid these stupid mistakes? (pylint doesn't warn me on that) I don't think I will ever (or almost) have to use a function as a boolean, instead of its return value... From ian.g.kelly at gmail.com Tue Dec 13 11:26:58 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Dec 2011 09:26:58 -0700 Subject: Signature-preserving decorators In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 6:36 AM, Henrik Faber wrote: > Hi group, > > when decorating a method in Python3, by use of the > functools.update_wrapper function, it can be achieved that the docstring > and name of the original function is preseverved. > > However, the prototype is lost: When looking into the Python help, I > have lots of entries that look like: > > getfoo(*args, **kwargs) -> int > > setbar(*args, **kwargs) > > As you can imagine, this is really not very self-explanatory. I've seen > a solution which constructs a wrapper's wrapper function using > inspection and eval -- this looks really dirty to me, however. Then > there's the "decorator" external module -- but I'd like to do it with > on-board tools. > > Is this possible in Python3 with too much of a hassle? The decorator module also uses inspection and eval to do it, by the way. Currently there is no pretty way to do it that I know of, but see PEP 362: http://www.python.org/dev/peps/pep-0362/ That PEP has unfortunately been in the "Open" state for quite a long time now, but it seems to me that a lot of people are starting to get interested in this issue, so maybe it will start to pick up some steam before too long. From neilc at norwich.edu Tue Dec 13 11:29:08 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 13 Dec 2011 16:29:08 GMT Subject: Getting the current directory path and the path where python script is saved References: Message-ID: <9kpcqkFcoU1@mid.individual.net> On 2011-12-13, gialloporpora wrote: > Dear all, > I would like to create a little script to rename my files, I think to > use argparse module to create it. > I would like to know how to obtain the path where script is executed and > the path where the file script is stored. > > For example, if I save my script in c:\python27 and I put the > directory in %path% I can execute it from any folder, I would > like to get the two directory path. Current working directory is available with os.getcwd() The location of the script you are running may be available as os.path.basename(sys.argv[0]). -- Neil Cerutti From bachth at uni-mainz.de Tue Dec 13 11:38:10 2011 From: bachth at uni-mainz.de (Thomas Bach) Date: Tue, 13 Dec 2011 17:38:10 +0100 Subject: Getting the current directory path and the path where python script is saved References: Message-ID: <87y5ug2zrh.fsf@jubold.box> gialloporpora writes: > I would like to know how to obtain the path where script is executed > and the path where the file script is stored. Does this help: In [16]: import os In [19]: os.getcwd() Out[19]: '/home/vince' In [21]: os.__file__ Out[21]: '/home/vince/src/pyramid_install/lib/python2.7/os.pyc' regards From tinnews at isbd.co.uk Tue Dec 13 11:48:51 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Tue, 13 Dec 2011 16:48:51 +0000 Subject: Using methodcaller in a list sort - any examples anywhere? References: Message-ID: Peter Otten <__peter__ at web.de> wrote: > tinnews at isbd.co.uk wrote: > > > I want to sort a list of 'things' (they're fairly complex objects) by > > the contents of one of the fields I can extract from the 'things' > > using a Python function. > > > > So I have a list L which is a list of objects of some sort. I can > > output the contents of a field in the list as follows:- > > > > for k in L: > > print k.get_property('family-name') > > > > How can I sort the list first? As I said it seems like a methodcaller > > is the answer but I don't see how. I want to sort the list of objects > > not just produce a sorted list of names. > > The most obvious way is to use a custom function > > def get_family_name(obj): > return obj.get_property("family-name") > L.sort(key=get_family_name) > > However, since you already know about methodcaller > > """ > class methodcaller(builtins.object) > | methodcaller(name, ...) --> methodcaller object > | > | Return a callable object that calls the given method on its operand. > | After, f = methodcaller('name'), the call f(r) returns r.name(). > | After, g = methodcaller('name', 'date', foo=1), the call g(r) returns > | r.name('date', foo=1). > """" > > L.sort(key=methodcaller("get_property", "family-name")) > OK, thanks, just what I wanted. -- Chris Green From sagysrael at gmail.com Tue Dec 13 11:50:11 2011 From: sagysrael at gmail.com (Sagy Drucker) Date: Tue, 13 Dec 2011 08:50:11 -0800 (PST) Subject: icmp and raw sockets in python Message-ID: hello i am relatively new to python, so please be considerate... i'm implementing a server and a client via raw_sockets. i have the necessary privileges. now, the server i defined so: host = socket.gethostbyname(socket.gethostname()) address = (host, 22224) sockSer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) sockSer.bind(address) sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) packet, addr = sockSer .recvfrom(4096) # wait for packet from client Q1) why can't i simply type: hosts = 'localhost'. if i do so, it doesn't allow me to write the line: sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON). only when doing gethostbyname(socket.gethostname()) i get 192.168.1.101 and then it works. in a different class: the client socket: host = socket.gethostbyname(socket.gethostname()) address = (host, 22224) sockCli = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) Q2) do i also need to type: sockCli.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) or maybe sockCli.connect(address) for the client socket? now, the problems arise when i do the following: 1) send a packet from client to socket: header=... payload='a' sockCli.sendto(header + payload, address) 2) receive packet in server and send file to client: while(true): data, addr = sockSer.recvfrom(4096) header2=... payload2='b' sockSer.sendto(header2 + payload2, addr) now, my important question is: Q3) the server sent only 1 packet to client, with payload 'b'. what happens is, my client actually receives 2 packets in the while loop: first packet is what the client itself sent to server, and the other packet is from the client got from the server. hence my output is 'ab' instead of simply 'b' why is this happening??? NOTE: i didn't type the entire code, but i think my syntax,parsing,header composition etc.. are correct. is there an obvious problem in my code? if necessary i'll upload the entire code. thanks From andrea.crotti.0 at gmail.com Tue Dec 13 12:03:01 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Tue, 13 Dec 2011 17:03:01 +0000 Subject: logging issues In-Reply-To: <4EE77B7B.9090408@gmail.com> References: <4EE77279.10104@gmail.com> <4EE77933.2060705@sequans.com> <4EE77B7B.9090408@gmail.com> Message-ID: <4EE78545.8010309@gmail.com> And by the way suppose I have a script which takes as input the log level and a list of possible filters. In another file I have a couple of functions as below which are called in that order. Is that supposed to work? In theory I'm getting both times the same logger. The set_verbosity seems to do its job, the second is also called and the filters are added but I don't see the output formatted as it should.. Maybe I can only set it up once? def set_verbosity(log_level): """Set the desired log level """ root_logger = logging.getLogger() root_logger.setLevel(LOG_LEVELS[log_level]) def setup_logging(to_filter): root_logger = logging.getLogger() hdlr = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) # a list of regular expressions to filter out? if to_filter: for f in to_filter: logger.debug("adding filter for name %s" % f) filt = logging.Filter(f) hdlr.addFilter(filt) root_logger.addHandler(hdlr) From invalid at invalid.invalid Tue Dec 13 12:20:35 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 13 Dec 2011 17:20:35 +0000 (UTC) Subject: boolean from a function References: Message-ID: On 2011-12-13, Andrea Crotti wrote: > Now somewhere else I had > > if func_bool: > # do something > > I could not quite understand why it was always true, until I finally > noticed that the () were missing. Is there some tool to avoid these > stupid mistakes? (pylint doesn't warn me on that) I don't think I > will ever (or almost) have to use a function as a boolean, instead of > its return value... FWIW, I have do use the truth value of a function (rather than it's return value) when writing code that uses callbacks: def foo(callback=None): # do some stuff if callback: callback() # do some more stuff It probably would be better to use "if callback is not None:", but I find I usually skip "is not None". -- Grant Edwards grant.b.edwards Yow! Will it improve my at CASH FLOW? gmail.com From prakash.stack at gmail.com Tue Dec 13 12:22:54 2011 From: prakash.stack at gmail.com (prakash jp) Date: Tue, 13 Dec 2011 22:52:54 +0530 Subject: text to html Message-ID: Hi All, Want to publish a log file as a web page, is there a parser to retain the format of the text as is and then convert to html. Please provide the relevant pointers Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From pedro.h.souto at gmail.com Tue Dec 13 12:30:40 2011 From: pedro.h.souto at gmail.com (Pedro Henrique Guedes Souto) Date: Tue, 13 Dec 2011 15:30:40 -0200 Subject: text to html In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 3:22 PM, prakash jp wrote: > > Hi All, > > Want to publish a log file as a web page, is there a parser to retain the format of the text as is and then convert to html. Please provide the relevant pointers Hey, You can use this:?http://txt2tags.org/ Att; Pedro Henrique Guedes Souto [pedro.h.souto at gmail.com] From ethan at stoneleaf.us Tue Dec 13 12:42:05 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 13 Dec 2011 09:42:05 -0800 Subject: boolean from a function In-Reply-To: <4EE77C25.1060600@gmail.com> References: <4EE77C25.1060600@gmail.com> Message-ID: <4EE78E6D.6060505@stoneleaf.us> Andrea Crotti wrote: > I'm not sure for how long I had this bug, and I could not understand the > problem. > > I had a function which would return a boolean > > def func_bool(): > if x: > return True > else: return False > > Now somewhere else I had > > if func_bool: > # do something > > I could not quite understand why it was always true, until I finally > noticed that the () were missing. > Is there some tool to avoid these stupid mistakes? (pylint doesn't warn > me on that) > I don't think I will ever (or almost) have to use a function as a > boolean, instead of its return value... Heh, I do believe I've been bitten by that a couple times. The only defense I'm aware of is good unit tests. ~Ethan~ From duncan.booth at invalid.invalid Tue Dec 13 12:42:51 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 13 Dec 2011 17:42:51 GMT Subject: boolean from a function References: Message-ID: Andrea Crotti wrote: > I'm not sure for how long I had this bug, and I could not understand > the problem. > > I had a function which would return a boolean > > def func_bool(): > if x: > return True > else: return False > > Now somewhere else I had > > if func_bool: > # do something > > I could not quite understand why it was always true, until I finally > noticed that the () were missing. > Is there some tool to avoid these stupid mistakes? (pylint doesn't > warn me on that) > I don't think I will ever (or almost) have to use a function as a > boolean, instead of its return value... For this particular example why don't you just write 'if x: # do something'? More generally rather than having a global function make it a property on an object. class Snark: def __init__(self, something): self.boojum = ... @property def is_a_bookum(self): return self.boojum hunted = Snark(whatever) Then you can write: if hunted.is_a_boojum: self.vanish_away() -- Duncan Booth http://kupuguy.blogspot.com From invalid at invalid.invalid Tue Dec 13 13:14:06 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 13 Dec 2011 18:14:06 +0000 (UTC) Subject: text to html References: Message-ID: On 2011-12-13, Pedro Henrique Guedes Souto wrote: > On Tue, Dec 13, 2011 at 3:22 PM, prakash jp wrote: > >> Want to publish a log file as a web page, is there a parser to retain >> the format of the text as is and then convert to html. Please provide >> the relevant pointers > > > Hey, You can use this:?http://txt2tags.org/ I think thats a bit too likely to interpret stuff in the log messages as formatting directives. Two options come to mind: 1. Don't use HTML. Return the logfile as-is with Content-Type: text/plain. [That way the user can also save it if needed.] 2. Escape the file contents using cgi.escape() and then stick the escaped contents inside
 
tags. That's not as nice, since saving it is no longer a useful option. -- Grant Edwards grant.b.edwards Yow! Now KEN and BARBIE at are PERMANENTLY ADDICTED to gmail.com MIND-ALTERING DRUGS ... From ian.g.kelly at gmail.com Tue Dec 13 13:15:21 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Dec 2011 11:15:21 -0700 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: <2ff41b2e-90e3-4fae-9951-67bb257ed6ed@m10g2000vbc.googlegroups.com> References: <4EE5C576.2000307@gmail.com> <2ff41b2e-90e3-4fae-9951-67bb257ed6ed@m10g2000vbc.googlegroups.com> Message-ID: On Tue, Dec 13, 2011 at 1:44 AM, Eelco wrote: > 'for i in llist' is not quite going to fly is it? Thats probably the > reason noone ever uses that construct; its not a proper sequence type. Not really a problem, because fortunately Python makes it super-easy to create custom iterators. def listiter(llist): while llist: head, llist = llist yield head And you're done. If you like, you could also wrap this up in a class with all the sequence-related magic methods you want, although you lose the simplicity of the literal syntax by doing so. If this is rarely used, it is more likely because custom containers are going to be less efficient than built-ins, but if you want to do functional programming and are not overly concerned with the speed of iteration, this is not a bad way to do it. > Good point. Copy-on-write semantics could be used, but really one > should have several linked list types reflecting the underlying > implementations. I would like to have an immutable singly linked list > builtin of the standard functional type, which you can only unpack > from one end and renders these issues moot, plus a builtin doubly > linked list with copy-on-write or copy-on-unpacking semantics. Copy-on-write could be implemented with any type. You don't need a doubly linked list for that. > We are not talking black magic here; we are talking about an EXPLICIT > type constraint provided on the very same line. An explicit type constraint with very different semantics depending on what particular type you specify and what particular type you're unpacking from, as I had understood it before. Now you seem to be saying that it would always be a copy, but sharing state with copy-on-write possible, which is a different situation. > Well perhaps, but not always knowing the type of your objects at write- > time is inherent to weakly typed languages; this happens all the time. > Not knowing the type of the sequence to be unpacked is in a sense an > asset; I can use this construct in a function, and unpack any sequence > type in a manner appropriate for it. About the result of the unpacking > I will know just as much as about the input to it; that they are the > same type. Just because the issue is inherent doesn't mean we should contribute to it. Knowing that an object is an arbitrary sequence is fine if all you want to do is iterate and index it. If you want to do anything else, then it's important to know the type. The copy-on-write suggestion does make the type-matching approach a bit more attractive. On the other hand, it's also more fragile (what if the type being unpacked can't be constructed from an iterable? For example, a database cursor), so that approach potentially needs additional error-handling. Anyway, the more I think about it, that concern is really more of an issue for straight copying. One of my pet peeves is that I prefer list(x) for copying sequences rather than the more common x[::]. The latter is fine if all I need is an immutable sequence of uncertain type to iterate and index over -- but then why did I need to make a copy? Unpacking implies different use cases, though, and maybe a good argument can be made for it to match type. From storchaka at gmail.com Tue Dec 13 13:57:40 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 13 Dec 2011 20:57:40 +0200 Subject: Fixing the XML batteries In-Reply-To: References: <4EE7425A.2090006@gmail.com> Message-ID: 13.12.11 16:59, Stefan Behnel ???????(??): > It matches my opinion though. I would be glad to divide your intentions, however ElementTree looks less documented than minidom, and is not full replacement. For example, I haven't found how to get XML encoding. Also, at use of ElementTree instead of minidom the suffix "ns0:" is added to each element. I do not see how to _create_ a new element and to write it with header. And DOM interface is more habitual for those who works with some other languages. > Yes, that's what C14N is there for, typically used for cryptography, > hashing, etc. However, MiniDOM doesn't implement that standard, so > you're on your own here. MiniDOM quite suited me earlier in this respect. I will pass to C14N as soon as I will be can. > The ET module is actually quite short (<1700 lines), so you can just > copy the Py2.7 version into your sources and optionally import it on > older Python releases. Since you only seem to depend on the serialiser > (which is worth using anyway because it is much faster in the Py2.7 > version), older platform versions of cET should also work just fine with > that module copy, so you can basically just import everything from > xml.etree.cElementTree and use the ElementTree class and the tostring() > function from your own local version if the platform version is too old. > > Note that ET is also still available as a separately installable > package, may or may not be simpler to use for you. I thank, it is too bulky for my small scripts (which I have decided to update from Python 2.3 or 2.4 to modern Python 3 and 2.6+). I will better postpone high-grade migration for half-year or year while the Python 2.7 and 3.2 won't appear in stable versions of popular distributives. I thank you for ET, it really is more convenient at some applications (especially at work with the text in elements). From joshua.landau.ws at gmail.com Tue Dec 13 14:01:42 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 13 Dec 2011 19:01:42 +0000 Subject: Overriding a global In-Reply-To: <4EE75382.9060104@sequans.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> Message-ID: On 13 December 2011 13:30, Jean-Michel Pichavant wrote: > > writing > > x = 1 > > def spam(): > x = 2 > > is in general a bad idea. That was my point. Why? I have a few (probably wrong) guesses. Because you expect it to be the same every time you use it? Well, then this should be "in general a bad idea": x = 1; print(x); x = 2; print(x) Even though it makes total sense to me. Is it because it's used to different purpose between similarly-looking functions? This looks fine, though: def func1(): x=1; print(x) def func2(): x=2; print(x) Is it because it looks like a reassignment of the more global x? I don't have an example here but, simply put, I don't believe this. We can use "id" as our own local variable without thinking that we're tampering with "__builtins__.id". I don't see it as much of a leap from builtin to global (except that you **can** do "dir = 1; del dir; dir" without error). That said, I'm sorta' just guessing the reason you might think it's a bad idea. -------------- next part -------------- An HTML attachment was scrubbed... URL: From cs at zip.com.au Tue Dec 13 14:11:27 2011 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 14 Dec 2011 06:11:27 +1100 Subject: Verbose and flexible args and kwargs syntax In-Reply-To: References: Message-ID: <20111213191126.GA1363@cskk.homeip.net> On 13Dec2011 00:30, Eelco wrote: | On Dec 13, 1:27?am, alex23 wrote: | > On Dec 13, 3:12?am, Eelco wrote: | > > But to relate it to the topic of this thread: no, the syntax does not | > > allow one to select the type of the resulting sequence. It always | > > constructs a list. | > | > So by this argument, _every_ function that returns a list should take | > an optional argument to specify an alternative form of sequence. | > | > What, exactly, is so onerous about coercing your list to _whatever_ | > type you want? You know, like everybody else has been. | > | > What does this _gain_ you other than one less line of code? | | 1) Turning two lines of code into a single more readable one is | nothing to scoff at | 2) After-the-fact conversion is O(n), getting the view you want right | away is O(1) Regarding (2), it has already cost you O(n) to get there. So your O(1) is a little ingenuous. -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ I'm a volunteer EMT-I on our local ambulance service; one of our Paramedics calls squid style motorcycle accidents "ZoomSplats", as in "we had a good ZoomSplat the other night". ;-) - Scott From ian.g.kelly at gmail.com Tue Dec 13 14:34:41 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Dec 2011 12:34:41 -0700 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau wrote: >> No, there is another difference, the reason for rebinding the name. >> In a subclass, you would rebind a class attribute because that >> particular attribute, which you need to change, is used and expected >> by external code, either in the base class or in code that uses its >> API (or both). ?Local variables in functions, on the other hand, are >> not externally visible, so there is no need to do this in order to >> conform to the expectations of external code. ?All it does in that >> case is to sow potential confusion. >> > So you're saying you should never extend methods or attributes that > aren't meant to be used as part of of the API? Because I can claim > guilty on this point. No, I'm only saying that replacing attributes in subclasses is accepted because it is necessary due to external dependencies, and that local variables in functions don't have that excuse. From rurpy at yahoo.com Tue Dec 13 14:37:40 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 13 Dec 2011 11:37:40 -0800 (PST) Subject: Fixing the XML batteries References: <4EE7425A.2090006@gmail.com> Message-ID: <3defcdde-f93a-48ec-9fcc-129d684d711b@o9g2000yqa.googlegroups.com> On Dec 13, 5:32?am, Stefan Behnel wrote: ... > In Python 2.7/3.2, ElementTree has support for C14N serialisation, just > pass the option method="c14n". Where does one find information in the Python documentation about this? From stefan_ml at behnel.de Tue Dec 13 14:38:54 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 13 Dec 2011 20:38:54 +0100 Subject: Fixing the XML batteries In-Reply-To: References: <4EE7425A.2090006@gmail.com> Message-ID: Serhiy Storchaka, 13.12.2011 19:57: > 13.12.11 16:59, Stefan Behnel ???????(??): >> It matches my opinion though. > > I would be glad to divide your intentions, however ElementTree looks less > documented than minidom It's certainly a lot smaller, which makes its API easier to learn and remember. > and is not full replacement. It's good enough for a surprisingly large part of all XML processing needs, and if you need more, there's lxml for that. > For example, I haven't found how to get XML encoding. True - lxml provides it, but plain ET doesn't. However, I can't think of any major use cases where you'd care about the encoding of the original input file. Just use what suites your needs on the way back out. UTF-8 will usually do just fine. > Also, at use of ElementTree instead > of minidom the suffix "ns0:" is added to each element. That's a "prefix", not a suffix. And since prefixes are basically useless for XML processing, it isn't commonly a problem whether they are called 'nsXY' or 'abcdefg'. It's the parser's duty to handle them for you. > I do not see how to _create_ a new element element = Element('tagname') > and to write it with header. That's called a "declaration". You can get it with, e.g., ElementTree(element).write(encoding='utf8') By default, ET doesn't write it unless it can put useful information into it. (Note that the XML spec makes the declaration optional for XML 1.0 serialisation as UTF-8.) > And DOM interface is more habitual for those who works with some other > languages. Not really. DOM is also considered unwieldy in many other languages. Even in a language as unwieldy as Java it's being frowned upon these days. In JavaScript, it has basically been replaced by jQuery, and many other languages also have substantially more "natural" ways to deal with XML than the DOM. It's true, though, that ElementTree isn't a widely known interface outside of the Python world. >> Yes, that's what C14N is there for, typically used for cryptography, >> hashing, etc. However, MiniDOM doesn't implement that standard, so >> you're on your own here. > > MiniDOM quite suited me earlier in this respect. I will pass to C14N as > soon as I will be can. > >> The ET module is actually quite short (<1700 lines), so you can just >> copy the Py2.7 version into your sources and optionally import it on >> older Python releases. Since you only seem to depend on the serialiser >> (which is worth using anyway because it is much faster in the Py2.7 >> version), older platform versions of cET should also work just fine with >> that module copy, so you can basically just import everything from >> xml.etree.cElementTree and use the ElementTree class and the tostring() >> function from your own local version if the platform version is too old. >> >> Note that ET is also still available as a separately installable >> package, may or may not be simpler to use for you. > > I thank, it is too bulky for my small scripts (which I have decided to > update from Python 2.3 or 2.4 to modern Python 3 and 2.6+). I will better > postpone high-grade migration for half-year or year while the Python 2.7 > and 3.2 won't appear in stable versions of popular distributives. In case you are only dealing with small in-house scripts, I'd suggest installing ET 1.3 (or, even better, lxml) on the machines where you want to use it. Then you no longer have to care about those dependencies. > I thank you for ET, it really is more convenient at some applications > (especially at work with the text in elements). Careful. ;) I'm just the author of lxml, not of ET. That would be Fredrik Lundh. Stefan From joshua.landau.ws at gmail.com Tue Dec 13 14:43:54 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 13 Dec 2011 19:43:54 +0000 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On 13 December 2011 19:34, Ian Kelly wrote: > On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau > wrote: > >> No, there is another difference, the reason for rebinding the name. > >> In a subclass, you would rebind a class attribute because that > >> particular attribute, which you need to change, is used and expected > >> by external code, either in the base class or in code that uses its > >> API (or both). Local variables in functions, on the other hand, are > >> not externally visible, so there is no need to do this in order to > >> conform to the expectations of external code. All it does in that > >> case is to sow potential confusion. > >> > > So you're saying you should never extend methods or attributes that > > aren't meant to be used as part of of the API? Because I can claim > > guilty on this point. > > No, I'm only saying that replacing attributes in subclasses is > accepted because it is necessary due to external dependencies, and > that local variables in functions don't have that excuse. > But they aren't needed due to external dependencies if they're implementation-specific and not part of the API, no? -------------- next part -------------- An HTML attachment was scrubbed... URL: From richard.dijkstra at planet.nl Tue Dec 13 14:46:55 2011 From: richard.dijkstra at planet.nl (Richard) Date: Tue, 13 Dec 2011 11:46:55 -0800 (PST) Subject: pyside dynamic buttons Message-ID: <57fb3cd8-fae3-4818-a76d-ccd8ebd27c5b@c13g2000vbh.googlegroups.com> Hi, We are making a screen with data driven buttons. I can add buttons but can't redraw the buttons for new values. The buttuns are placed in a frame. I draw the buttons on the screen in a for loop: def drawS1Butons(self): n=0 for self.S1 in self.Org.getVpuChilds(self.Focus): self.S1.button = QtGui.QPushButton(self.frame_2) # Add method to the button class !?!? self.S1.button.setGeometry(QtCore.QRect(80, n*30, 161, 23)) self.S1.button.setObjectName(self.S1.name) self.S1.button.setText(QtGui.QApplication.translate("MainWindow", self.S1.name, None, QtGui.QApplication.UnicodeUTF8)) n=n+1 How can I delete buttons? Thanks, Richard From rurpy at yahoo.com Tue Dec 13 14:50:41 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 13 Dec 2011 11:50:41 -0800 (PST) Subject: Fixing the XML batteries References: <4EE7425A.2090006@gmail.com> Message-ID: <3306306d-c701-40ed-b6f4-847773857db5@c18g2000yqj.googlegroups.com> On Dec 13, 5:32?am, Stefan Behnel wrote: ... > In Python 2.7/3.2, ElementTree has support for C14N serialisation, just > pass the option method="c14n". Where in the Python docs can one find information about this? [previous post disappeared, sorry if I double posted or replied to author inadvertently.] From ian.g.kelly at gmail.com Tue Dec 13 14:54:09 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 13 Dec 2011 12:54:09 -0700 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On Tue, Dec 13, 2011 at 12:43 PM, Joshua Landau wrote: > On 13 December 2011 19:34, Ian Kelly wrote: >> >> On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau >> wrote: >> >> No, there is another difference, the reason for rebinding the name. >> >> In a subclass, you would rebind a class attribute because that >> >> particular attribute, which you need to change, is used and expected >> >> by external code, either in the base class or in code that uses its >> >> API (or both). ?Local variables in functions, on the other hand, are >> >> not externally visible, so there is no need to do this in order to >> >> conform to the expectations of external code. ?All it does in that >> >> case is to sow potential confusion. >> >> >> > So you're saying you should never extend methods or attributes that >> > aren't meant to be used as part of of the API? Because I can claim >> > guilty on this point. >> >> No, I'm only saying that replacing attributes in subclasses is >> accepted because it is necessary due to external dependencies, and >> that local variables in functions don't have that excuse. > > > But they aren't needed due to external dependencies if they're > implementation-specific and not part of the API, no? By "external dependencies" I mean anything that's not specifically part of the subclass. This includes the base class. If they're not part of the API, then the base class presumably uses them for something, and by replacing them, you change the behavior of that base functionality. That's an external dependency. From hoogendoorn.eelco at gmail.com Tue Dec 13 15:14:15 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 12:14:15 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: Message-ID: <0c87865e-d8a2-41b6-91b4-09278ee2ffcf@cs7g2000vbb.googlegroups.com> On Dec 13, 8:11?pm, Cameron Simpson wrote: > On 13Dec2011 00:30, Eelco wrote: > | On Dec 13, 1:27?am, alex23 wrote: > | > On Dec 13, 3:12?am, Eelco wrote: > | > > But to relate it to the topic of this thread: no, the syntax does not > | > > allow one to select the type of the resulting sequence. It always > | > > constructs a list. > | > > | > So by this argument, _every_ function that returns a list should take > | > an optional argument to specify an alternative form of sequence. > | > > | > What, exactly, is so onerous about coercing your list to _whatever_ > | > type you want? You know, like everybody else has been. > | > > | > What does this _gain_ you other than one less line of code? > | > | 1) Turning two lines of code into a single more readable one is > | nothing to scoff at > | 2) After-the-fact conversion is O(n), getting the view you want right > | away is O(1) > > Regarding (2), it has already cost you O(n) to get there. So your O(1) > is a little ingenuous. Well, yes, but if one takes a given sequence as input (at least O(n) complexity to obtain it in the first place, indeed), and then wants to, say, recursively unwind it, the cost of the total operation is O(n) versus O(n^2) And besides, O(n) < 2*O(n); perhaps of lesser concern than different orders, but still. From stefan_ml at behnel.de Tue Dec 13 15:21:46 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 13 Dec 2011 21:21:46 +0100 Subject: Fixing the XML batteries In-Reply-To: <3defcdde-f93a-48ec-9fcc-129d684d711b@o9g2000yqa.googlegroups.com> References: <4EE7425A.2090006@gmail.com> <3defcdde-f93a-48ec-9fcc-129d684d711b@o9g2000yqa.googlegroups.com> Message-ID: rurpy at yahoo.com, 13.12.2011 20:37: > On Dec 13, 5:32 am, Stefan Behnel wrote: >> In Python 2.7/3.2, ElementTree has support for C14N serialisation, just >> pass the option method="c14n". > > Where does one find information in the Python documentation about > this? Hmm, interesting. I though it had, but now when I click on the stdlib doc link to read the module source (hint, hint), I can see that it only has the hooks. The C14N support module of ET 1.3 was not integrated into the stdlib. Sorry for not verifying this earlier. So you actually need the external package for C14N support. See here: http://effbot.org/zone/elementtree-13-intro.htm http://hg.effbot.org/et-2009-provolone/src/tip/elementtree/elementtree/ElementC14N.py Just to emphasize this once again: it's not more than a single module that you can copy into your own code as a fallback import, or deploy in your local installations. Stefan From joshua.landau.ws at gmail.com Tue Dec 13 15:24:29 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 13 Dec 2011 20:24:29 +0000 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <87k4611l4y.fsf@benfinney.id.au> Message-ID: On 13 December 2011 19:54, Ian Kelly wrote: > On Tue, Dec 13, 2011 at 12:43 PM, Joshua Landau > wrote: > > On 13 December 2011 19:34, Ian Kelly wrote: > >> > >> On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau > >> wrote: > >> >> No, there is another difference, the reason for rebinding the name. > >> >> In a subclass, you would rebind a class attribute because that > >> >> particular attribute, which you need to change, is used and expected > >> >> by external code, either in the base class or in code that uses its > >> >> API (or both). Local variables in functions, on the other hand, are > >> >> not externally visible, so there is no need to do this in order to > >> >> conform to the expectations of external code. All it does in that > >> >> case is to sow potential confusion. > >> >> > >> > So you're saying you should never extend methods or attributes that > >> > aren't meant to be used as part of of the API? Because I can claim > >> > guilty on this point. > >> > >> No, I'm only saying that replacing attributes in subclasses is > >> accepted because it is necessary due to external dependencies, and > >> that local variables in functions don't have that excuse. > > > > > > But they aren't needed due to external dependencies if they're > > implementation-specific and not part of the API, no? > > By "external dependencies" I mean anything that's not specifically > part of the subclass. This includes the base class. If they're not > part of the API, then the base class presumably uses them for > something, and by replacing them, you change the behavior of that base > functionality. That's an external dependency. > Touch?. On this point, I yield. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Tue Dec 13 15:32:43 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 12:32:43 -0800 (PST) Subject: Verbose and flexible args and kwargs syntax References: <4EE5C576.2000307@gmail.com> <2ff41b2e-90e3-4fae-9951-67bb257ed6ed@m10g2000vbc.googlegroups.com> Message-ID: <26b97534-1624-4afb-8869-f04afc800e22@4g2000yqu.googlegroups.com> On Dec 13, 7:15?pm, Ian Kelly wrote: > On Tue, Dec 13, 2011 at 1:44 AM, Eelco wrote: > > 'for i in llist' is not quite going to fly is it? Thats probably the > > reason noone ever uses that construct; its not a proper sequence type. > > Not really a problem, because fortunately Python makes it super-easy > to create custom iterators. > > def listiter(llist): > ? ? while llist: > ? ? ? ? head, llist = llist > ? ? ? ? yield head > > And you're done. ?If you like, you could also wrap this up in a class > with all the sequence-related magic methods you want, although you > lose the simplicity of the literal syntax by doing so. ?If this is > rarely used, it is more likely because custom containers are going to > be less efficient than built-ins, but if you want to do functional > programming and are not overly concerned with the speed of iteration, > this is not a bad way to do it. Fair enough. Still, I dont readily see myself using the construct, and I do feel myself longing for something like that to be included as a builtin collection type. Im not sure why python is so stingy with the collections it provides. > > Good point. Copy-on-write semantics could be used, but really one > > should have several linked list types reflecting the underlying > > implementations. I would like to have an immutable singly linked list > > builtin of the standard functional type, which you can only unpack > > from one end and renders these issues moot, plus a builtin doubly > > linked list with copy-on-write or copy-on-unpacking semantics. > > Copy-on-write could be implemented with any type. ?You don't need a > doubly linked list for that. True > > We are not talking black magic here; we are talking about an EXPLICIT > > type constraint provided on the very same line. > > An explicit type constraint with very different semantics depending on > what particular type you specify and what particular type you're > unpacking from, as I had understood it before. ?Now you seem to be > saying that it would always be a copy, but sharing state with > copy-on-write possible, which is a different situation. Yes, I think consistent semantics over all sequence types is very important. Although, returning a view for immutable collections like tuples and 'functional lists' (for lack of a better term), and always returning a copy for mutable container types (lists and doubly-linked- lists / deques) is not so bad either, imo. Just one extra simple rule that is clear and obvious enough. > > Well perhaps, but not always knowing the type of your objects at write- > > time is inherent to weakly typed languages; this happens all the time. > > Not knowing the type of the sequence to be unpacked is in a sense an > > asset; I can use this construct in a function, and unpack any sequence > > type in a manner appropriate for it. About the result of the unpacking > > I will know just as much as about the input to it; that they are the > > same type. > > Just because the issue is inherent doesn't mean we should contribute > to it. How are we contributing to it? If we dont know the type of the RHS, we dont know the type of the LHS either, but its not like we lost any information. If we do know the type of the RHS, then we do know the type of the LHS as well; its conserved. > Anyway, the more I think about it, that concern is really more of an > issue for straight copying. ?One of my pet peeves is that I prefer > list(x) for copying sequences rather than the more common x[::]. ?The > latter is fine if all I need is an immutable sequence of uncertain > type to iterate and index over -- but then why did I need to make a > copy? ?Unpacking implies different use cases, though, and maybe a good > argument can be made for it to match type. Thanks for the constructive feedback; something to think about. From rurpy at yahoo.com Tue Dec 13 16:02:18 2011 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 13 Dec 2011 13:02:18 -0800 (PST) Subject: Fixing the XML batteries References: <4EE7425A.2090006@gmail.com> <3defcdde-f93a-48ec-9fcc-129d684d711b@o9g2000yqa.googlegroups.com> Message-ID: <0b1c9504-7b2c-4c13-8ffc-a91edcfa4a93@f39g2000yqn.googlegroups.com> On Dec 13, 1:21?pm, Stefan Behnel wrote: > ru... at yahoo.com, 13.12.2011 20:37: > > > On Dec 13, 5:32 am, Stefan Behnel wrote: > >> In Python 2.7/3.2, ElementTree has support for C14N serialisation, just > >> pass the option method="c14n". > > > Where does one find information in the Python documentation about > > this? > > Hmm, interesting. I though it had, but now when I click on the stdlib doc > link to read the module source (hint, hint), I realize the source is available (having had to use it way too many time in the past, not just with ET), but that does not justify omission from the docs. However the point is moot since (as you say) it seems the Python-distributed ET doesn't contain the c14n feature. > I can see that it only has the > hooks. The C14N support module of ET 1.3 was not integrated into the > stdlib. Sorry for not verifying this earlier. > > So you actually need the external package for C14N support. See here: > > http://effbot.org/zone/elementtree-13-intro.htm > > http://hg.effbot.org/et-2009-provolone/src/tip/elementtree/elementtre... > > Just to emphasize this once again: it's not more than a single module that > you can copy into your own code as a fallback import, or deploy in your > local installations. Right, but many times I try to avoid external dependencies when feasible. Thanks for the clarifications. From gialloporpora at gmail.com Tue Dec 13 16:39:44 2011 From: gialloporpora at gmail.com (gialloporpora) Date: Tue, 13 Dec 2011 22:39:44 +0100 Subject: Getting the current directory path and the path where python script is saved In-Reply-To: <9kpcqkFcoU1@mid.individual.net> References: <9kpcqkFcoU1@mid.individual.net> Message-ID: Risposta al messaggio di Neil Cerutti : > Current working directory is available with os.getcwd() > > The location of the script you are running may be available as > os.path.basename(sys.argv[0]). Thank you very much, Neil, is exactly what I wanted. Sandro -- *gialloporpora: RT @themitcho: telnet miku.acm.uiuc.edu Do it now. You will not regret it. #nyan /via @beaulebens * - http://bit.ly/u2boVC Sto ascoltando: *Pink Floyd ? Echoe Part I * - http://bit.ly/vgIGQe From tjreedy at udel.edu Tue Dec 13 16:57:31 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 13 Dec 2011 16:57:31 -0500 Subject: change a file with a context manager In-Reply-To: <4EE75F6D.5070604@gmail.com> References: <4EE72FF4.8010203@gmail.com> <4EE75F6D.5070604@gmail.com> Message-ID: On 12/13/2011 9:21 AM, Andrea Crotti wrote: > Another possible option is to just add to the path everything contained > in that > correct easy_install.pth file. > Or is there a way to reload the path settings? If you are talking about sys.path, you can certainly copy, mutate or replace, and restore sys.path. The details depend on whether you mutate or replace the original. -- Terry Jan Reedy From ethan at stoneleaf.us Tue Dec 13 18:21:12 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 13 Dec 2011 15:21:12 -0800 Subject: xml, minidom, ElementTree In-Reply-To: <4EE528BD.2040102@v.loewis.de> References: <4EE1C9AB.2040301@v.loewis.de> <4EE528BD.2040102@v.loewis.de> Message-ID: <4EE7DDE8.2050706@stoneleaf.us> In the near future I will need to parse and rewrite parts of a xml files created by a third-party program (PrintShopMail, for the curious). It contains both binary and textual data. There has been some strong debate about the merits of minidom vs ElementTree. Recommendations? ~Ethan~ From martin.hellwig at gmail.com Tue Dec 13 18:21:30 2011 From: martin.hellwig at gmail.com (Martin P. Hellwig) Date: Tue, 13 Dec 2011 23:21:30 +0000 Subject: icmp and raw sockets in python In-Reply-To: References: Message-ID: On 13/12/2011 16:50, Sagy Drucker wrote: > hello Hi > i am relatively new to python, so please be considerate... As I am only responding to one of your questions, perhaps it would be best if you don't get any other more helpful replies to split your questions up and post them separately. > > i'm implementing a server and a client via raw_sockets. > i have the necessary privileges. > > now, the server i defined so: > host = socket.gethostbyname(socket.gethostname()) > address = (host, 22224) > sockSer = socket.socket(socket.AF_INET, socket.SOCK_RAW, > socket.IPPROTO_ICMP) > sockSer.bind(address) > sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) > packet, addr = sockSer .recvfrom(4096) # wait for packet from client > > Q1) why can't i simply type: hosts = 'localhost'. > if i do so, it doesn't allow me to write the line: > sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON). > only when doing gethostbyname(socket.gethostname()) i get > 192.168.1.101 > and then it works. > Well localhost should resolve to 127.0.0.1/8 which is attached to the loopback interface, my gut feeling is that this interface has a particular set of restrictions which you are encountering. Sorry I can't be more helpful. -- mph From steve+comp.lang.python at pearwood.info Tue Dec 13 18:37:08 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2011 23:37:08 GMT Subject: boolean from a function References: Message-ID: <4ee7e1a4$0$29979$c3e8da3$5496439d@news.astraweb.com> On Tue, 13 Dec 2011 16:24:05 +0000, Andrea Crotti wrote: > I'm not sure for how long I had this bug, and I could not understand the > problem. > > I had a function which would return a boolean > > def func_bool(): > if x: > return True > else: return False x is a global? Poor design. But in any case, instead of an explicit if...else block, the canonical way to convert an arbitrary object to True/ False is with bool: def func_bool(): return bool(x) But you don't need it. See below. > Now somewhere else I had > > if func_bool: > # do something That would be better written as: if x: ... since func_bool always refers to x, it is just a needless level of indirection that doesn't buy you anything. > I could not quite understand why it was always true, until I finally > noticed that the () were missing. > Is there some tool to avoid these stupid mistakes? (pylint doesn't warn > me on that) Can't help you with that. Have you tried pychecker? I can't help you with that either :) -- Steven From vincent.vandevyvre at swing.be Tue Dec 13 21:37:35 2011 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Wed, 14 Dec 2011 03:37:35 +0100 Subject: pyside dynamic buttons In-Reply-To: <57fb3cd8-fae3-4818-a76d-ccd8ebd27c5b@c13g2000vbh.googlegroups.com> References: <57fb3cd8-fae3-4818-a76d-ccd8ebd27c5b@c13g2000vbh.googlegroups.com> Message-ID: <4EE80BEF.8070201@swing.be> An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Tue Dec 13 22:18:42 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 03:18:42 GMT Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: Message-ID: <4ee81592$0$11091$c3e8da3@news.astraweb.com> On Mon, 12 Dec 2011 09:29:11 -0800, Eelco wrote: [quoting Jussi Piitulainen ] >> They recognize modular arithmetic but for some reason insist that there >> is no such _binary operation_. But as I said, I don't understand their >> concern. (Except the related concern about some programming languages, >> not Python, where the remainder does not behave well with respect to >> division.) I've never come across this, and frankly I find it implausible that *actual* mathematicians would say that. Likely you are misunderstanding a technical argument about remainder being a relation rather than a bijunction. The argument would go something like this: "Remainder is not uniquely defined. For example, the division of -42 by -5 can be written as either: 9*-5 + 3 = -42 8*-5 + -2 = -42 so the remainder is either 3 or -2. Hence remainder is not a bijection (1:1 function)." The existence of two potential answers for the remainder is certainly correct, but the conclusion that remainder is not a binary operation doesn't follow. It is a binary relation. Mathematicians are well able to deal with little inconveniences like this, e.g. consider the square root: 10**2 = 100 (-10)**2 = 100 therefore the square root of 100 is ?10 Mathematicians get around this by defining the square root operator ? as *only* the principle value of the square root relation, that is, the positive root. Hence: ?100 = 10 only If you want both roots, you have to explicitly ask for them both: ??100 Similarly, we can sensibly define the remainder or modulus operator to consistently return a non-negative remainder, or to do what Python does, which is to return a remainder with the same sign as the divisor: >>> 42 % 5 2 >>> -42 % 5 3 >>> 42 % -5 -3 >>> -42 % -5 -2 There may be practical or logical reasons for preferring one over the other, but either choice would make remainder a bijection. One might even define two separate functions/operators, one for each behaviour. > They might not be willing to define it, but as soon as we programmers > do, well, we did. > > Having studied the contemporary philosophy of mathematics, their concern > is probably that in their minds, mathematics is whatever some dead guy > said it was, and they dont know of any dead guy ever talking about a > modulus operation, so therefore it 'does not exist'. You've studied the contemporary philosophy of mathematics huh? How about studying some actual mathematics before making such absurd pronouncements on the psychology of mathematicians? -- Steven From tjreedy at udel.edu Wed Dec 14 00:01:06 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Dec 2011 00:01:06 -0500 Subject: xml, minidom, ElementTree In-Reply-To: <4EE7DDE8.2050706@stoneleaf.us> References: <4EE1C9AB.2040301@v.loewis.de> <4EE528BD.2040102@v.loewis.de> <4EE7DDE8.2050706@stoneleaf.us> Message-ID: On 12/13/2011 6:21 PM, Ethan Furman wrote: > In the near future I will need to parse and rewrite parts of a xml files > created by a third-party program (PrintShopMail, for the curious). > It contains both binary and textual data. > > There has been some strong debate about the merits of minidom vs > ElementTree. > > Recommendations? People's reaction to the DOM interface seem quite varied, with a majority, perhaps, being negative. I personally would look at both enough to understand the basic API model to see where *I* fit. -- Terry Jan Reedy From showell30 at yahoo.com Wed Dec 14 00:42:52 2011 From: showell30 at yahoo.com (Steve Howell) Date: Tue, 13 Dec 2011 21:42:52 -0800 (PST) Subject: AttributeError in "with" statement (3.2.2) Message-ID: I'm using Python 3.2.2, and the following program gives me an error that I don't understand: class Foo: pass foo = Foo() foo.name = "Steve" def add_goodbye_function(obj): def goodbye(): print("goodbye " + obj.name) obj.goodbye = goodbye add_goodbye_function(foo) foo.goodbye() # outputs goodbye Steve foo.__exit__ = foo.goodbye foo.__exit__() # outputs goodbye Steve with foo: # fails with AttributeError: __exit__ print("doing stuff") I am dynamically adding an attribute __exit__ to the variable foo, which works fine when I call it directly, but it fails when I try to use foo as the expression in the with statement. Here is the full output: > python3 with.coffee goodbye Steve goodbye Steve Traceback (most recent call last): File "with.coffee", line 17, in with foo: # fails with AttributeError: AttributeError: __exit__ What am I doing wrong? From ericsnowcurrently at gmail.com Wed Dec 14 01:05:53 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Tue, 13 Dec 2011 23:05:53 -0700 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 10:42 PM, Steve Howell wrote: > I'm using Python 3.2.2, and the following program gives me an error > that I don't understand: > > class Foo: > ?pass > > foo = Foo() > foo.name = "Steve" > > def add_goodbye_function(obj): > ?def goodbye(): > ? ?print("goodbye " + obj.name) > ?obj.goodbye = goodbye > > add_goodbye_function(foo) > foo.goodbye() # outputs goodbye Steve > foo.__exit__ = foo.goodbye > foo.__exit__() # outputs goodbye Steve > > with foo: # fails with AttributeError: ?__exit__ > ?print("doing stuff") > > I am dynamically adding an attribute __exit__ to the variable foo, > which works fine when I call it directly, but it fails when I try to > use foo as the expression in the with statement. ?Here is the full > output: > >> python3 with.coffee > goodbye Steve > goodbye Steve > Traceback (most recent call last): > ?File "with.coffee", line 17, in > ? ?with foo: # fails with AttributeError: > AttributeError: __exit__ > > What am I doing wrong? That is a tricky one. As with many of the special methods (start and end with __) in Python, the underlying mechanism in the interpreter is directly pulling the function from the class object. It does not look to the instance object for the function at any time. See http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes. -eric From tjreedy at udel.edu Wed Dec 14 01:29:13 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Dec 2011 01:29:13 -0500 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: Message-ID: On 12/14/2011 1:05 AM, Eric Snow wrote: > On Tue, Dec 13, 2011 at 10:42 PM, Steve Howell wrote: >> I'm using Python 3.2.2, and the following program gives me an error >> that I don't understand: >> >> class Foo: >> pass >> >> foo = Foo() >> foo.name = "Steve" >> >> def add_goodbye_function(obj): >> def goodbye(): >> print("goodbye " + obj.name) >> obj.goodbye = goodbye >> >> add_goodbye_function(foo) >> foo.goodbye() # outputs goodbye Steve >> foo.__exit__ = foo.goodbye >> foo.__exit__() # outputs goodbye Steve foo.goodbye, aliased as foo.__exit__, is a *function* attribute of the foo *instance*. >> with foo: # fails with AttributeError: __exit__ >> print("doing stuff") >> >> I am dynamically adding an attribute __exit__ to the variable foo, >> which works fine when I call it directly, but it fails when I try to >> use foo as the expression in the with statement. Here is the full >> output: >> >>> python3 with.coffee >> goodbye Steve >> goodbye Steve >> Traceback (most recent call last): >> File "with.coffee", line 17, in >> with foo: # fails with AttributeError: >> AttributeError: __exit__ >> >> What am I doing wrong? To complement what Eric says below: The with statement is looking for an instance *method*, which by definition, is a function attribute of a *class* (the class of the context manager) that takes an instance of the class as its first parameter. Notice that goodbye does not take any parameter. The first parameter is conventionally called 'self', but that is not a requirement. That it be there, that it expect to be bound to an instance, and that the function be bound to the class are requirements. > That is a tricky one. > > As with many of the special methods (start and end with __) in Python, > the underlying mechanism in the interpreter is directly pulling the > function from the class object. It does not look to the instance > object for the function at any time. See > http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes. -- Terry Jan Reedy From stefan_ml at behnel.de Wed Dec 14 02:22:25 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 14 Dec 2011 08:22:25 +0100 Subject: xml, minidom, ElementTree In-Reply-To: References: <4EE1C9AB.2040301@v.loewis.de> <4EE528BD.2040102@v.loewis.de> <4EE7DDE8.2050706@stoneleaf.us> Message-ID: Terry Reedy, 14.12.2011 06:01: > On 12/13/2011 6:21 PM, Ethan Furman wrote: >> In the near future I will need to parse and rewrite parts of a xml files >> created by a third-party program (PrintShopMail, for the curious). >> It contains both binary and textual data. >> >> There has been some strong debate about the merits of minidom vs >> ElementTree. >> >> Recommendations? > > People's reaction to the DOM interface seem quite varied, with a majority, > perhaps, being negative. I personally would look at both enough to > understand the basic API model to see where *I* fit. The API is one thing, yes, but there's also the fact that MiniDOM doesn't scale. If your XML files are of a notable size (a couple of MB), MiniDOM may simply not be able to handle them. I collected some numbers in a blog post. Note that this is using a recent CPython 3.3 build which has an optimised Unicode string implementation, thus yielding lower memory requirements on average than Py2.x. http://blog.behnel.de/index.php?p=197 The memory consumption makes a difference of a factor of 5-10 compared to cElementTree, which makes it two orders of magnitude larger than the size of the serialised file. You may be able to stuff one such file into memory, but you'll quickly get into trouble when you try to do parallel processing or otherwise use more than one document at a time. Stefan From hoogendoorn.eelco at gmail.com Wed Dec 14 02:49:43 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 13 Dec 2011 23:49:43 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Dec 14, 4:18?am, Steven D'Aprano wrote: > > They might not be willing to define it, but as soon as we programmers > > do, well, we did. > > > Having studied the contemporary philosophy of mathematics, their concern > > is probably that in their minds, mathematics is whatever some dead guy > > said it was, and they dont know of any dead guy ever talking about a > > modulus operation, so therefore it 'does not exist'. > > You've studied the contemporary philosophy of mathematics huh? > > How about studying some actual mathematics before making such absurd > pronouncements on the psychology of mathematicians? The philosophy was just a sidehobby to the study of actual mathematics; and you are right, studying their works is the best way to get to know them. Speaking from that vantage point, I can say with certainty that the vast majority of mathematicians do not have a coherent philosophy, and they adhere to some loosely defined form of platonism. Indeed that is absurd in a way. Even though you may trust these people to be perfectly functioning deduction machines, you really shouldnt expect them to give sensible answers to the question of which are sensible axioms to adopt. They dont have a reasoned answer to this, they will by and large defer to authority. From steve+comp.lang.python at pearwood.info Wed Dec 14 03:01:24 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 08:01:24 GMT Subject: AttributeError in "with" statement (3.2.2) References: Message-ID: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > To complement what Eric says below: The with statement is looking for an > instance *method*, which by definition, is a function attribute of a > *class* (the class of the context manager) that takes an instance of the > class as its first parameter. I'm not sure that is correct... I don't think that there is anything "by definition" about where methods live. Particularly not in Python where instance methods can be attributes of the instance itself. >>> class Test(object): ... def method(self): ... print("This method is an attribute of the class.") ... >>> t = Test() >>> t.method() This method is an attribute of the class. >>> >>> import types >>> t.method = types.MethodType( ... lambda self: print( ... "This method is an attribute of the instance."), t) >>> t.method() This method is an attribute of the instance. So the normal lookup rules that apply to data attributes, namely instance, then class, then superclasses, also applies to methods in Python. In languages that don't allow that sort of thing, like Java, you need to use convoluted design patterns like Dynamic Proxy to make it work. In Python, you just create a method and attach it on the instance. http://stackoverflow.com/questions/8260740/override-a-method-for-an- instance-of-a-class But this doesn't apply for special dunder attributes like __exit__, for speed reasons. (For new-style classes only, classic classes have no such special casing. This makes automatic delegation a breeze in Python 2 with classic classes, and a PITA in Python 3. Boo hiss.) -- Steven From pip.261 at gmail.com Wed Dec 14 03:28:45 2011 From: pip.261 at gmail.com (Felipe O) Date: Wed, 14 Dec 2011 03:28:45 -0500 Subject: Property Abuse Message-ID: Hi All, I was wondering what everyone's thought process was regarding properties. Lately I find I've been binging on them and have classes with > 10 properties. While pylint doesn't complain (yet), it tends to be picky about keeping instance attribute counts low, so I figure there's something against that. How do you guys decide between using properties versus getter methods, or how do you refactor them if neither? Cheers! -Felipe -------------- next part -------------- An HTML attachment was scrubbed... URL: From jpiitula at ling.helsinki.fi Wed Dec 14 03:56:02 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2011 10:56:02 +0200 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano writes: > On Mon, 12 Dec 2011 09:29:11 -0800, Eelco wrote: > > [quoting Jussi Piitulainen ] > >> They recognize modular arithmetic but for some reason insist that > >> there is no such _binary operation_. But as I said, I don't > >> understand their concern. (Except the related concern about some > >> programming languages, not Python, where the remainder does not > >> behave well with respect to division.) > > I've never come across this, and frankly I find it implausible that > *actual* mathematicians would say that. Likely you are > misunderstanding a technical argument about remainder being a > relation rather than a bijunction. The argument would go something > like this: (For 'bijunction', read 'function'.) I'm not misunderstanding any argument. There was no argument. There was a blanket pronouncement that _in mathematics_ mod is not a binary operator. I should learn to challenge such pronouncements and ask what the problem is. Maybe next time. But you are right that I don't know how actual mathematicians these people are. I'm not a mathematician. I don't know where to draw the line. A Finnish actual mathematician stated a similar prejudice towards mod as a binary operator in a Finnish group. I asked him what is wrong with Knuth's definition (remainder after flooring division), and I think he conceded that it's not wrong. Number theorists just choose to work with congruence relations. I have no problem with that. He had experience with students who confused congruences modulo some modulus with a binary operation, and mixed up their notations because of that. That is a reason to be suspicious, but it is a confusion on the part of the students. Graham, Knuth, Patashnik contrast the two concepts explicitly, no confusion there. And I know that there are many ways to define division and remainder so that x div y + x rem y = x. Boute's paper cited in [1] advocates a different one and discusses others. [1] But I think the argument "there are several such functions, therefore, _in mathematics_, there is no such function" is its own caricature. > "Remainder is not uniquely defined. For example, the division of -42 > by -5 can be written as either: > > 9*-5 + 3 = -42 > 8*-5 + -2 = -42 > > so the remainder is either 3 or -2. Hence remainder is not a bijection > (1:1 function)." Is someone saying that _division_ is not defined because -42 div -5 is somehow both 9 and 8? Hm, yes, I see that someone might. The two operations, div and rem, need to be defined together. (There is no way to make remainder a bijection. You mean it is not a function if it is looked at in a particular way.) [The square root was relevant but I snipped it.] > Similarly, we can sensibly define the remainder or modulus operator > to consistently return a non-negative remainder, or to do what > Python does, which is to return a remainder with the same sign as > the divisor: ... > There may be practical or logical reasons for preferring one over > the other, but either choice would make remainder a bijection. One > might even define two separate functions/operators, one for each > behaviour. Scheme is adopting flooring division, ceiling-ing division, rounding division, truncating division, centering division, and the Euclidean division advocated by Boute, and the corresponding remainders. There is no better way to bring home to a programmer the points that there are different ways to define these, and they come as div _and_ rem. From jpiitula at ling.helsinki.fi Wed Dec 14 04:04:04 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2011 11:04:04 +0200 Subject: Verbose and flexible args and kwargs syntax References: Message-ID: Nick Dokos writes: > Jussi Piitulainen wrote: > > They recognize modular arithmetic but for some reason insist that > > there is no such _binary operation_. But as I said, I don't > > understand their concern. (Except the related concern about some > > programming languages, not Python, where the remainder does not > > behave well with respect to division.) > > They are probably arguing that it's uniquely defined only on ZxN and > that there are different conventions to extend it to ZxZ (the > programming languages problem that you allude to above - although I > don't know what you mean by "does not behave well wrt division"). I think Boute [1] says Standard Pascal or some such language failed to have x div y + x rem y = x, but I can't check the reference now. That at least waes what I had in mind. Having x rem y but leaving it underspecified is another such problem: then it is unspecified whether the equation holds. [1] > If you choose one convention and stick to it, it becomes a > well-defined binary operation. That's what I'd like to think. From paul.nospam at rudin.co.uk Wed Dec 14 04:38:55 2011 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 14 Dec 2011 09:38:55 +0000 Subject: xml, minidom, ElementTree References: <4EE1C9AB.2040301@v.loewis.de> <4EE528BD.2040102@v.loewis.de> Message-ID: <87vcpjqyq8.fsf@no-fixed-abode.cable.virginmedia.net> Ethan Furman writes: > In the near future I will need to parse and rewrite parts of a xml files > created by a third-party program (PrintShopMail, for the curious). > It contains both binary and textual data. > > There has been some strong debate about the merits of minidom vs > ElementTree. > > Recommendations? I tend to start with BeautifulSoup, and think about other things if that doesn't work out. It might just be me, but I find it easier to get stuff done using BeautifulSoup than either minidom or ElementTree. From andrea.crotti.0 at gmail.com Wed Dec 14 04:42:08 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Wed, 14 Dec 2011 09:42:08 +0000 Subject: boolean from a function In-Reply-To: <4ee7e1a4$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee7e1a4$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EE86F70.1090305@gmail.com> On 12/13/2011 11:37 PM, Steven D'Aprano wrote: > > x is a global? Poor design. But in any case, instead of an explicit > if...else block, the canonical way to convert an arbitrary object to True/ > False is with bool: > > def func_bool(): > return bool(x) > > But you don't need it. See below. > > > No no it was just to show the pattern, it wasn't the actual code. I don't like to have useless indirections, so I wouldn't do that... I like the idea of the property (from Duncan Booth) but the thing is that that function looks like it's doing something (from its name), it's not just a simple property. In the case of the square class Sq(object): def __init__(self, x, y): self.x = x self.y It makes perfect sense to have "area" as a property, because you can either compute it and cache it or compute it on demand. It feels a bit less natural to create a property on something that is less simple than that imho.. From paul.nospam at rudin.co.uk Wed Dec 14 04:43:36 2011 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Wed, 14 Dec 2011 09:43:36 +0000 Subject: % is not an operator References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: <87r507qyif.fsf@no-fixed-abode.cable.virginmedia.net> Steven D'Aprano writes: > The existence of two potential answers for the remainder is certainly > correct, but the conclusion that remainder is not a binary operation > doesn't follow. It is a binary relation. This depends on your definition of "operation". Normally an operation is a function, rather than just a relation. From __peter__ at web.de Wed Dec 14 05:02:26 2011 From: __peter__ at web.de (Peter Otten) Date: Wed, 14 Dec 2011 11:02:26 +0100 Subject: AttributeError in "with" statement (3.2.2) References: Message-ID: Steve Howell wrote: > I'm using Python 3.2.2, and the following program gives me an error > that I don't understand: > > class Foo: > pass > > foo = Foo() > foo.name = "Steve" > > def add_goodbye_function(obj): > def goodbye(): > print("goodbye " + obj.name) > obj.goodbye = goodbye > > add_goodbye_function(foo) > foo.goodbye() # outputs goodbye Steve > foo.__exit__ = foo.goodbye > foo.__exit__() # outputs goodbye Steve > > with foo: # fails with AttributeError: __exit__ > print("doing stuff") > > I am dynamically adding an attribute __exit__ to the variable foo, > which works fine when I call it directly, but it fails when I try to > use foo as the expression in the with statement. Here is the full > output: > >> python3 with.coffee > goodbye Steve > goodbye Steve > Traceback (most recent call last): > File "with.coffee", line 17, in > with foo: # fails with AttributeError: > AttributeError: __exit__ If you don't know it yet, contextlib.contextmanager offers a highlevel alternative: >>> @contextmanager ... def goodbye(obj): ... try: ... yield obj ... finally: ... print("goodbye", obj.name) ... >>> with goodbye(Foo()) as foo: ... print("hello", foo.name) ... hello Steve goodbye Steve From hoogendoorn.eelco at gmail.com Wed Dec 14 05:09:32 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 14 Dec 2011 02:09:32 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> On 14 dec, 09:56, Jussi Piitulainen wrote: > Steven D'Aprano writes: > > On Mon, 12 Dec 2011 09:29:11 -0800, Eelco wrote: > > > [quoting Jussi Piitulainen ] > > >> They recognize modular arithmetic but for some reason insist that > > >> there is no such _binary operation_. But as I said, I don't > > >> understand their concern. (Except the related concern about some > > >> programming languages, not Python, where the remainder does not > > >> behave well with respect to division.) > > > I've never come across this, and frankly I find it implausible that > > *actual* mathematicians would say that. Likely you are > > misunderstanding a technical argument about remainder being a > > relation rather than a bijunction. The argument would go something > > like this: > > (For 'bijunction', read 'function'.) > > I'm not misunderstanding any argument. There was no argument. There > was a blanket pronouncement that _in mathematics_ mod is not a binary > operator. I should learn to challenge such pronouncements and ask what > the problem is. Maybe next time. > > But you are right that I don't know how actual mathematicians these > people are. I'm not a mathematician. I don't know where to draw the > line. > > A Finnish actual mathematician stated a similar prejudice towards mod > as a binary operator in a Finnish group. I asked him what is wrong > with Knuth's definition (remainder after flooring division), and I > think he conceded that it's not wrong. Number theorists just choose to > work with congruence relations. I have no problem with that. > > He had experience with students who confused congruences modulo some > modulus with a binary operation, and mixed up their notations because > of that. That is a reason to be suspicious, but it is a confusion on > the part of the students. Graham, Knuth, Patashnik contrast the two > concepts explicitly, no confusion there. > > And I know that there are many ways to define division and remainder > so that x div y + x rem y = x. Boute's paper cited in [1] advocates a > different one and discusses others. > > [1] > > But I think the argument "there are several such functions, therefore, > _in mathematics_, there is no such function" is its own caricature. Indeed. Obtaining a well defined function is just a matter of picking a convention and sticking with it. Arguably, the most elegant thing to do is to define integer division and remainder as a single operation; which is not only the logical thing to do mathematically, but might work really well programmatically too. The semantics of python dont really allow for this though. One could have: d, r = a // b But it wouldnt work that well in composite expressions; selecting the right tuple index would be messy and a more verbose form would be preferred. However, performance-wise its also clearly the best solution, as one often needs both output arguments and computing them simultaniously is most efficient. At least numpy should have something like: d, r = np.integer_division(a, b) And something similar in the math module for scalars. > > "Remainder is not uniquely defined. For example, the division of -42 > > by -5 can be written as either: > > > ? ? 9*-5 + 3 = -42 > > ? ? 8*-5 + -2 = -42 > > > so the remainder is either 3 or -2. Hence remainder is not a bijection > > (1:1 function)." > > Is someone saying that _division_ is not defined because -42 div -5 is > somehow both 9 and 8? Hm, yes, I see that someone might. The two > operations, div and rem, need to be defined together. > > (There is no way to make remainder a bijection. You mean it is not a > function if it is looked at in a particular way.) Surjection is the word you are looking for That is, if one buys the philosophy of modernists like bourbaki in believing there is much to be gained by such pedantry. From jeanmichel at sequans.com Wed Dec 14 05:14:53 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 14 Dec 2011 11:14:53 +0100 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> Message-ID: <4EE8771D.1050902@sequans.com> Joshua Landau wrote: > On 13 December 2011 13:30, Jean-Michel Pichavant > > wrote: > > writing > > x = 1 > > def spam(): > x = 2 > > is in general a bad idea. That was my point. > > > Why? I have a few (probably wrong) guesses. > > Because you expect it to be the same every time you use it? > Well, then this should be "in general a bad idea": > x = 1; print(x); x = 2; print(x) you're changing the value of x, that's fine. In the example above, the assignement is not the problem. The problem is that you create 2 different 'x', one in globals(), and one in locals(). Using the same name within 2 implicit namespaces is a bad idead in general because it can be difficult to know which one is used. If you want to have fun, try this code, prepared to be amazed. There's something funny with the global statement, it's applied on the whole block no matter where it is stated in the block. x=1 # global def spam(): x = 2 # local (or so you may think) print x global x # I need to use the global one now print x print locals() For more fun you could create a 'x' name in __builtin__ and import it so that people never know which x you are using. > Even though it makes total sense to me. > > Is it because it's used to different purpose between similarly-looking > functions? > This looks fine, though: > def func1(): x=1; print(x) > def func2(): x=2; print(x) > > Is it because it looks like a reassignment of the more global x? > I don't have an example here but, simply put, I don't believe this. We > can use "id" as our own local variable without thinking that we're > tampering with "__builtins__.id". I don't see it as much of a leap > from builtin to global (except that you /*can*/ do "dir = 1; del dir; > dir" without error). > > That said, I'm sorta' just guessing the reason you might think it's a > bad idea. The problem makes little sense when using names like x or func1. Besides namespace issues, naming 2 *different objects* with the same meaningful name is usually a bad idea and points the fact that your names are no that meaningful. To go back to the original post, having a 'logger' that may name 2 different logger object during the execution is a bad idea. One quick way to fix it is to name the logger 'currentLogger', this way you warn the reader that the logger named by curentLogger may change over time. As someone sugggested in this thread one other option is to use a different name for the second logger. JM From rosuav at gmail.com Wed Dec 14 05:32:11 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Dec 2011 21:32:11 +1100 Subject: Overriding a global In-Reply-To: <4EE8771D.1050902@sequans.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> Message-ID: On Wed, Dec 14, 2011 at 9:14 PM, Jean-Michel Pichavant wrote: > The problem makes little sense when using names like x or func1. Besides > namespace issues, naming 2 *different objects* with the same meaningful name > is usually a bad idea and points the fact that your names are no that > meaningful. So... it's a bad idea for me to use 'i' many times in my code, with the same name having different meanings in different places? In languages with infinitely-nesting scopes (one of Python's great lacks, imho), I've often had three different variables with the same names, all perfectly valid, and all doing what they should. It's not just loop indices - I used to have a piece of code in which 'res' was a MySQL resource being processed in a loop, and I had three nested loops. Each time I referenced 'res', it used the innermost available resource, which was precisely what I wanted. If I'd arbitrarily had to guarantee that all variable names were unique, I would have had completely unnecessary fiddling around. Python wouldn't let you do that with three nested 'res'es in one function, but you can perfectly reasonably have a global and a local. It makes perfect sense... which is a good reason for keeping it legal. ChrisA From candide at free.invalid Wed Dec 14 06:12:07 2011 From: candide at free.invalid (candide) Date: Wed, 14 Dec 2011 12:12:07 +0100 Subject: Regexp : repeated group identification Message-ID: <4ee88488$0$27871$426a74cc@news.free.fr> Consider the following code # ---------------------------- import re z=re.match('(Spam\d)+', 'Spam4Spam2Spam7Spam8') print z.group(0) print z.group(1) # ---------------------------- outputting : ---------------------------- Spam4Spam2Spam7Spam8 Spam8 ---------------------------- The '(Spam\d)+' regexp is tested against 'Spam4Spam2Spam7Spam8' and the regexp matches the string. Group numbered one within the regex '(Spam\d)+' refers to Spam\d The fours substrings Spam4 Spam2 Spam7 and Spam8 match the group numbered 1. So I don't understand why z.group(1) gives the last substring (ie Spam8 as the output shows), why not an another one, Spam4 for example ? From narges.javaheri at gmail.com Wed Dec 14 06:15:53 2011 From: narges.javaheri at gmail.com (narges javaheri) Date: Wed, 14 Dec 2011 12:15:53 +0100 Subject: Question (PDE solvers in Python) Message-ID: Hello there, Based on your experiences, what's the best (the most reliable) package for solving a system of nonlinear coupled PDEs (partial differential equations) in 3D using Python, preferably by FEM (finite element method)? Regards, Narges -------------- next part -------------- An HTML attachment was scrubbed... URL: From vlastimil.brom at gmail.com Wed Dec 14 06:34:16 2011 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Wed, 14 Dec 2011 12:34:16 +0100 Subject: Regexp : repeated group identification In-Reply-To: <4ee88488$0$27871$426a74cc@news.free.fr> References: <4ee88488$0$27871$426a74cc@news.free.fr> Message-ID: 2011/12/14 candide : > Consider the following code > > # ---------------------------- > import re > > z=re.match('(Spam\d)+', 'Spam4Spam2Spam7Spam8') > print z.group(0) > print z.group(1) > # ---------------------------- > > outputting : > > ---------------------------- > Spam4Spam2Spam7Spam8 > Spam8 > ---------------------------- > > The '(Spam\d)+' regexp is tested against 'Spam4Spam2Spam7Spam8' and the > regexp matches the string. > > Group numbered one within the regex '(Spam\d)+' refers to Spam\d > > The fours substrings > > Spam4 ? Spam2 ? Spam7 ?and ?Spam8 > > match the group numbered 1. > > So I don't understand why z.group(1) gives the last substring (ie Spam8 as > the output shows), why not an another one, Spam4 for example ? > -- > http://mail.python.org/mailman/listinfo/python-list Hi, you may find a tiny notice in the re docs on this: http://docs.python.org/library/re.html#re.MatchObject.group "If a group is contained in a part of the pattern that matched multiple times, the last match is returned." If you need to work with the content captured in the repeated group, you may check the new regex implementation: http://pypi.python.org/pypi/regex Which has a special "captures" method of the match object for this (beyond many other improvements): >>> import regex >>> m=regex.match('(Spam\d)+', 'Spam4Spam2Spam7Spam8') >>> m.captures(1) ['Spam4', 'Spam2', 'Spam7', 'Spam8'] >>> hth, vbr From rustompmody at gmail.com Wed Dec 14 06:47:28 2011 From: rustompmody at gmail.com (rusi) Date: Wed, 14 Dec 2011 03:47:28 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: <851cbabf-60b6-4806-bef5-e16f6089b08d@h37g2000pri.googlegroups.com> On Dec 14, 1:56?pm, Jussi Piitulainen wrote: > > Is someone saying that _division_ is not defined because -42 div -5 is > somehow both 9 and 8? Hm, yes, I see that someone might. The two > operations, div and rem, need to be defined together. ----------------------------- Haskell defines a quot-rem pair and a div-mod pair as follows: (from http://www.haskell.org/onlinereport/basic.html) (x `quot` y)*y + (x `rem` y) == x (x `div` y)*y + (x `mod` y) == x `quot` is integer division truncated toward zero, while the result of `div` is truncated toward negative infinity. From rosuav at gmail.com Wed Dec 14 06:53:38 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Dec 2011 22:53:38 +1100 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <851cbabf-60b6-4806-bef5-e16f6089b08d@h37g2000pri.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <851cbabf-60b6-4806-bef5-e16f6089b08d@h37g2000pri.googlegroups.com> Message-ID: On Wed, Dec 14, 2011 at 10:47 PM, rusi wrote: > `quot` is integer division truncated toward zero, while the result of > `div` is truncated toward negative infinity. All these problems just because of negative numbers. They ought never to have been invented. At least nobody rounds toward positive infinity... oh wait, that's legal too. ChrisA From arnodel at gmail.com Wed Dec 14 06:55:34 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Wed, 14 Dec 2011 11:55:34 +0000 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: On 14 December 2011 07:49, Eelco wrote: > On Dec 14, 4:18?am, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> > They might not be willing to define it, but as soon as we programmers >> > do, well, we did. >> >> > Having studied the contemporary philosophy of mathematics, their concern >> > is probably that in their minds, mathematics is whatever some dead guy >> > said it was, and they dont know of any dead guy ever talking about a >> > modulus operation, so therefore it 'does not exist'. >> >> You've studied the contemporary philosophy of mathematics huh? >> >> How about studying some actual mathematics before making such absurd >> pronouncements on the psychology of mathematicians? > > The philosophy was just a sidehobby to the study of actual > mathematics; and you are right, studying their works is the best way > to get to know them. Speaking from that vantage point, I can say with > certainty that the vast majority of mathematicians do not have a > coherent philosophy, and they adhere to some loosely defined form of > platonism. Indeed that is absurd in a way. Even though you may trust > these people to be perfectly functioning deduction machines, you > really shouldnt expect them to give sensible answers to the question > of which are sensible axioms to adopt. They dont have a reasoned > answer to this, they will by and large defer to authority. Please come down from your vantage point for a few moments and consider how insulting your remarks are to people who have devoted most of their intellectual energy to the study of mathematics. So you've studied a bit of mathematics and a bit of philosophy? Good start, keep working at it. You think that every mathematician should be preoccupied with what axioms to adopt, and why? Mathematics is a very large field of study and yes, some mathematicians are concerned with these issues (they are called logicians) but for most it isn't really about axioms. Mathematics is bigger than the axioms that we use to formalise it. Most mathematicians do not need to care about what precise axiomatisation underlies the mathematics that they practise because they are thinking on a much higher level. Just like we do not worry about what machine language instruction actually performs each step of the Python program we are writing. You say that mathematicians defer to authority, but do you really think that thousands of years of evolution and refinement in mathematics are to be discarded lightly? I think not. It's good to have original ideas, to pursue them and to believe in them, but it would be foolish to think that they are superior to knowledge which has been accumulated over so many generations. You claim that mathematicians have a poor understanding of philosophy. It may be so for many of them, but how is this a problem? I doesn't prevent them from having a deep understanding of their field of mathematics. Do philosophers have a good understanding of mathematics? Cheers, -- Arnaud From jeanmichel at sequans.com Wed Dec 14 07:05:19 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 14 Dec 2011 13:05:19 +0100 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> Message-ID: <4EE890FF.3080708@sequans.com> Chris Angelico wrote: > On Wed, Dec 14, 2011 at 9:14 PM, Jean-Michel Pichavant > wrote: > >> The problem makes little sense when using names like x or func1. Besides >> namespace issues, naming 2 *different objects* with the same meaningful name >> is usually a bad idea and points the fact that your names are no that >> meaningful. >> > > So... it's a bad idea for me to use 'i' many times in my code, with > the same name having different meanings in different places? In > languages with infinitely-nesting scopes (one of Python's great lacks, > imho), I've often had three different variables with the same names, > all perfectly valid, and all doing what they should. It's not just > loop indices - I used to have a piece of code in which 'res' was a > MySQL resource being processed in a loop, and I had three nested > loops. Each time I referenced 'res', it used the innermost available > resource, which was precisely what I wanted. If I'd arbitrarily had to > guarantee that all variable names were unique, I would have had > completely unnecessary fiddling around. > > Python wouldn't let you do that with three nested 'res'es in one > function, but you can perfectly reasonably have a global and a local. > It makes perfect sense... which is a good reason for keeping it legal. > > ChrisA > Bad ideas : i = 5 def spam(): for i,v in enumerate([1,2,3,4]): for i,v in enumerate(['a','b', 'c']): print i, v print i,v # bad surprise good ideas : # global nameThatWillNotBeUsedlocally = 'foo' def spam(): for qtyIndex, quantity in enumerate([5,6,3,1]): for fruitIndex, fruit in enumerate(['orange', 'banana']): print fruitIndex, fruit print qtyIndex, quantity While a lot of people still use i,j,k,v to handler values and indexes, I think it's a bad idea. I'm just stating an opinion from my personnal python experience. I know some people can successfully use the hard way. JM From rosuav at gmail.com Wed Dec 14 07:21:29 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Dec 2011 23:21:29 +1100 Subject: Overriding a global In-Reply-To: <4EE890FF.3080708@sequans.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> <4EE890FF.3080708@sequans.com> Message-ID: On Wed, Dec 14, 2011 at 11:05 PM, Jean-Michel Pichavant wrote: > Chris Angelico wrote: >> >> So... it's a bad idea for me to use 'i' many times in my code, with >> the same name having different meanings in different places? In >> languages with infinitely-nesting scopes... > Bad ideas : > > i = 5 > > def spam(): > ?for i,v in enumerate([1,2,3,4]): > ? for i,v in enumerate(['a','b', 'c']): > ? ? print i, v > ? print i,v # bad surprise That's my point. It's not safe to do it in Python, because the "inner" local i is the same as the "outer" local i. Python doesn't have block scope the way most C-like languages do. int spam() { for (int i=0;i<5;++i) { for (int i=2;i<4;++i) write("inner "+i+"\n"); write("outer "+i+"\n"); } } Works perfectly, and the only cost is that variables must be formally declared. In Python, you can kinda fudge that sort of thing with nested functions. def spam(): q=2 # just to prove that the scopes really are nested for i in range(5): def ham(): for i in range(2,4): print("q = %d, i = %d"%(q,i)) ham() print("i = %d"%i) It's somewhat clunky, but it does give the illusion of block scope. Inners mask outers, outers are visible to inner functions. It's an odd construct though. Very odd. So... I think I've figured out how to implement from __future__ import braces. #define { def innerfunc(): #define } innerfunc() And there you are, out of your difficulty at once! ChrisA From jpiitula at ling.helsinki.fi Wed Dec 14 07:22:27 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2011 14:22:27 +0200 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> Message-ID: Eelco writes: > On 14 dec, 09:56, Jussi Piitulainen wrote: > > But I think the argument "there are several such functions, > > therefore, _in mathematics_, there is no such function" is its own > > caricature. > > Indeed. Obtaining a well defined function is just a matter of > picking a convention and sticking with it. > > Arguably, the most elegant thing to do is to define integer division > and remainder as a single operation; which is not only the logical > thing to do mathematically, but might work really well > programmatically too. > > The semantics of python dont really allow for this though. One could > have: > > d, r = a // b > > But it wouldnt work that well in composite expressions; selecting the > right tuple index would be messy and a more verbose form would be > preferred. However, performance-wise its also clearly the best > solution, as one often needs both output arguments and computing them > simultaniously is most efficient. The current Scheme draft does this. For each rounding method, it provides an operation that provides both the quotient and the remainder, an operation that provides the quotient, and an operation that provides the remainder. The both-values operation is more awkward to compose, as you rightly say. It's just a matter of naming them all. Python has a good default integer division as the pair of operators // and %. Python also supports the returning of several values from functions as tuples. It can be done. > > Is someone saying that _division_ is not defined because -42 div > > -5 is somehow both 9 and 8? Hm, yes, I see that someone might. The > > two operations, div and rem, need to be defined together. > > > > (There is no way to make remainder a bijection. You mean it is not > > a function if it is looked at in a particular way.) > > Surjection is the word you are looking for Um, no, I mean function. The allegedly alleged problem is that there may be two (or more) different values for f(x,y), which makes f not a _function_ (and the notation f(x,y) maybe inappropriate). Surjectivity is as much beside the point as bijectivity, but I think we have surjectivity for rem: Z * Z -> Z if we use a definition that produces both positive and negative remainders, or rem: Z * Z -> N if we have non-negative remainders (and include 0 in N, which is another bone of contention). We may or may not want to exclude 0 as the modulus, or divisor if you like. It is at least a special case. It's injectivity that fails: 9 % 4 == 6 % 5 == 3 % 2, while Python quite sensibly has (9, 4) != (6, 5) != (3, 2). (How I love the chaining of the comparisons.) > That is, if one buys the philosophy of modernists like bourbaki in > believing there is much to be gained by such pedantry. I think something is gained. Not sure I would call it philosophy. From corleone82 at gmail.com Wed Dec 14 07:31:21 2011 From: corleone82 at gmail.com (Juan Perez) Date: Wed, 14 Dec 2011 12:31:21 +0000 Subject: automate commands to an .exe console program through python In-Reply-To: References: <4EE73847.30404@timgolden.me.uk> Message-ID: Hi, Well if I can remmeber since last time I did something similar in C, it was close stdin channel in the open devices table (I don't know if it is the correct name in english, I learnt it in spanish) and put a pipe on it and then create a fork, parent it comunicates to the child through the pipe, so if child run a shell program, through exec, you should be able to comunicate to his stdin through his parent... I think that was correct, if I remmember well... My app is console app, working by basic commands like send cd or dir to the console. I thought redirecting stdin (keypad) I could send this basic comands to it... but obviously I was completely wrong... I can not use dedicated virtual machines, and it has to be as similar as user environment as possible, and it can appear new windows that takes focus, so I have to assure i can get the focus every time before sending commands.... Quite tricky stuff.... if it is possible to do something like that in Python I can struggle a bit more, if not better leave right now before wasting time with nonsenses... Thanks, Juan 2011/12/13 Chris Angelico > On Tue, Dec 13, 2011 at 11:35 PM, Juan Perez wrote: > > My problem is thinking in windows console somewhat like linux shell, and > do > > same things I did with pipes in C programming. But it seems not to be the > > case. > > It is (at least, it supports the basics of pipes and redirection); but > what you're looking at here is an application that appears not to use > STDIN. > > > At a glance I dare say that sendkeys module is useful enough, but if > I've > > understood well, you only can send messages to focused window, that will > > report some inestabilities if windows focused is changed. You won't be > able > > to touch anything in the current laptop. So, is there any way to identify > > the a process/window and send keystrokes in a smarter way and/or is it > > possible to identify a window an get the focus on it? > > You may be able to put focus to the window before sending it keys, but > this is sounding extremely hackish (nearly as bad as the Magic: The > Gathering Online trade-bot system). I'm thinking here the best option > may be to give this a dedicated virtual machine and let it do whatever > it likes - effectively, have an entire "computer" dedicated to just > this application and its automation harness. That would probably make > things simple enough that you don't need to worry too much about > focus, sending keystrokes to other windows, etc. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Dec 14 07:32:34 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 12:32:34 GMT Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Dec 2011 10:56:02 +0200, Jussi Piitulainen wrote: > Steven D'Aprano writes: >> On Mon, 12 Dec 2011 09:29:11 -0800, Eelco wrote: >> >> [quoting Jussi Piitulainen ] >> >> They recognize modular arithmetic but for some reason insist that >> >> there is no such _binary operation_. But as I said, I don't >> >> understand their concern. (Except the related concern about some >> >> programming languages, not Python, where the remainder does not >> >> behave well with respect to division.) >> >> I've never come across this, and frankly I find it implausible that >> *actual* mathematicians would say that. Likely you are misunderstanding >> a technical argument about remainder being a relation rather than a >> bijunction. The argument would go something like this: > > (For 'bijunction', read 'function'.) Oops, you're right of course. It's been about 20 years since I've needed to care about the precise difference between a bijection and a function, and I made a mistake. And then to add to my shame, I also misspelt bijection. > I'm not misunderstanding any argument. There was no argument. There was > a blanket pronouncement that _in mathematics_ mod is not a binary > operator. I should learn to challenge such pronouncements and ask what > the problem is. Maybe next time. So this was *one* person making that claim? I understand that, in general, mathematicians don't have much need for a remainder function in the same way programmers do -- modulo arithmetic is far more important. But there's a world of difference between saying "In mathematics, extracting the remainder is not important enough to be given a special symbol and treated as an operator" and saying "remainder is not a binary operator". The first is reasonable; the second is not. > But you are right that I don't know how actual mathematicians these > people are. I'm not a mathematician. I don't know where to draw the > line. > > A Finnish actual mathematician stated a similar prejudice towards mod as > a binary operator in a Finnish group. I asked him what is wrong with > Knuth's definition (remainder after flooring division), and I think he > conceded that it's not wrong. Number theorists just choose to work with > congruence relations. I have no problem with that. Agreed. [...] > (There is no way to make remainder a bijection. You mean it is not a > function if it is looked at in a particular way.) You're right, of course -- remainder cannot be 1:1. I don't know what I was thinking. -- Steven From hoogendoorn.eelco at gmail.com Wed Dec 14 07:33:06 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 14 Dec 2011 04:33:06 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> Message-ID: <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> On 14 dec, 12:55, Arnaud Delobelle wrote: > On 14 December 2011 07:49, Eelco wrote: > > On Dec 14, 4:18?am, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> wrote: > >> > They might not be willing to define it, but as soon as we programmers > >> > do, well, we did. > > >> > Having studied the contemporary philosophy of mathematics, their concern > >> > is probably that in their minds, mathematics is whatever some dead guy > >> > said it was, and they dont know of any dead guy ever talking about a > >> > modulus operation, so therefore it 'does not exist'. > > >> You've studied the contemporary philosophy of mathematics huh? > > >> How about studying some actual mathematics before making such absurd > >> pronouncements on the psychology of mathematicians? > > > The philosophy was just a sidehobby to the study of actual > > mathematics; and you are right, studying their works is the best way > > to get to know them. Speaking from that vantage point, I can say with > > certainty that the vast majority of mathematicians do not have a > > coherent philosophy, and they adhere to some loosely defined form of > > platonism. Indeed that is absurd in a way. Even though you may trust > > these people to be perfectly functioning deduction machines, you > > really shouldnt expect them to give sensible answers to the question > > of which are sensible axioms to adopt. They dont have a reasoned > > answer to this, they will by and large defer to authority. > > Please come down from your vantage point for a few moments and > consider how insulting your remarks are to people who have devoted > most of their intellectual energy to the study of mathematics. ?So > you've studied a bit of mathematics and a bit of philosophy? ?Good > start, keep working at it. Thanks, I intend to. > You think that every mathematician should be preoccupied with what > axioms to adopt, and why? Of course I dont. If you wish to restrict your attention to the exploration of the consequences of axioms others throw at you, that is a perfectly fine specialization. Most mathematicians do exactly that, and thats fine. But that puts them in about as ill a position to judged what is, or shouldnt be defined, as the average plumber. Compounding the problem is not just that they do not wish to concern themselves with the inductive aspect of mathematics, they would like to pretend it does not exist at all. For instance, if you point out to them a 19th century mathematician used very different axioms than a 20th century one, (and point out they were both fine mathematicians that attained results universally celebrated), they will typically respond emotionally; get angry or at least annoyed. According to their pseudo-Platonist philosophy, mathematics should not have an inductive side, axioms are set in stone and not a human affair, and the way they answer the question as to where knowledge about the 'correct' mathematical axioms comes from is by an implicit or explicit appeal to authority. They dont explain how it is that they can see 'beyond the platonic cave' to find the 'real underlying truth', they quietly assume somebody else has figured it out in the past, and leave it at that. > You say that mathematicians defer to authority, but do you really > think that thousands of years of evolution and refinement in > mathematics are to be discarded lightly? ?I think not. ?It's good to > have original ideas, to pursue them and to believe in them, but it > would be foolish to think that they are superior to knowledge which > has been accumulated over so many generations. For what its worth; insofar as my views can be pidgeonholed, im with the classicists (pre-20th century), which indeed has a long history. Modernists in turn discard large swaths of that. Note that its largely an academic debate though; everybody agrees that 1+1=2. But there are some practical consequences; if I were the designated science-Tsar, all transfinite-analysist would be out on the street together with the homeopaths, for instance. > You claim that mathematicians have a poor understanding of philosophy. > ?It may be so for many of them, but how is this a problem? ?I doesn't > prevent them from having a deep understanding of their field of > mathematics. ?Do philosophers have a good understanding of > mathematics? As a rule of thumb: absolutely not, no. I dont think I can think of any philosopher who turned his attention to mathematics that ever wrote anything interesting. All the interesting writers had their boots on mathematical ground; Quine, Brouwer, Weyl and the earlier renaissance men like Gauss and contemporaries. The fragmentation of disciplines is infact a major problem in my opinion though. Most physicists take their mathematics from the ivory- math tower, and the mathematicians shudder at the idea of listning back to see which of what they cooked up is actually anything but mental masturbation, in the meanwhile cranking out more gibberish about alephs. If any well-reasoned philosophy enters into the mix, its usually in the spare time of one of the physicists, but it is assuredly not coming out of the philosophy department. There is something quite wrong with that state of affairs. From steve+comp.lang.python at pearwood.info Wed Dec 14 07:38:31 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 12:38:31 GMT Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> Message-ID: <4ee898c6$0$29979$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Dec 2011 02:09:32 -0800, Eelco wrote: > Arguably, the most elegant thing to do is to define integer division and > remainder as a single operation; which is not only the logical thing to > do mathematically, but might work really well programmatically too. > > The semantics of python dont really allow for this though. One could > have: > > d, r = a // b That would be: >>> divmod(17, 5) (3, 2) > But it wouldnt work that well in composite expressions; selecting the > right tuple index would be messy and a more verbose form would be > preferred. However, performance-wise its also clearly the best solution, > as one often needs both output arguments and computing them > simultaniously is most efficient. Premature optimization. -- Steven From hoogendoorn.eelco at gmail.com Wed Dec 14 07:41:23 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 14 Dec 2011 04:41:23 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> Message-ID: <0dd668cd-815c-4027-9421-5fc22ed89b68@n6g2000vbg.googlegroups.com> On 14 dec, 13:22, Jussi Piitulainen wrote: > > > Is someone saying that _division_ is not defined because -42 div > > > -5 is somehow both 9 and 8? Hm, yes, I see that someone might. The > > > two operations, div and rem, need to be defined together. > > > > (There is no way to make remainder a bijection. You mean it is not > > > a function if it is looked at in a particular way.) > > > Surjection is the word you are looking for > > Um, no, I mean function. The allegedly alleged problem is that there > may be two (or more) different values for f(x,y), which makes f not a > _function_ (and the notation f(x,y) maybe inappropriate). > > Surjectivity is as much beside the point as bijectivity, but I think > we have surjectivity for rem: Z * Z -> Z if we use a definition that > produces both positive and negative remainders, or rem: Z * Z -> N if > we have non-negative remainders (and include 0 in N, which is another > bone of contention). We may or may not want to exclude 0 as the > modulus, or divisor if you like. It is at least a special case. > > It's injectivity that fails: 9 % 4 == 6 % 5 == 3 % 2, while Python > quite sensibly has (9, 4) != (6, 5) != (3, 2). (How I love the > chaining of the comparisons.) My reply was more to the statement you quoted than to yours; sorry for the confusion. Yes, we have surjectivity and not injectivity, thats all I was trying to say. > > That is, if one buys the philosophy of modernists like bourbaki in > > believing there is much to be gained by such pedantry. > > I think something is gained. Not sure I would call it philosophy. Agreed; its more the notion that one stands to gain much real knowledge by writing volumnius books about these matters that irks me, but I guess thats more a matter of taste than philosophy. From steve+comp.lang.python at pearwood.info Wed Dec 14 07:53:22 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 12:53:22 GMT Subject: Overriding a global References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> Message-ID: <4ee89c41$0$29979$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Dec 2011 13:05:19 +0100, Jean-Michel Pichavant wrote: > Bad ideas : > > i = 5 > > def spam(): > for i,v in enumerate([1,2,3,4]): > for i,v in enumerate(['a','b', 'c']): > print i, v > print i,v # bad surprise The bad surprise happens because you are using the same name twice in *one* namespace, the local scope. This example has nothing to do with local/global name clashes: the existence of global i is irrelevant. Python's scoping rules work correctly, and global i is not affected by the local i. Programming languages use multiple namespaces so that you don't need to make your variable names globally unique. There are languages that don't distinguish between local and global. Python is not one of them. The programmer should feel free to use local names without worrying too much if they accidentally use a global name. Having said that, re-using names isn't *entirely* risk free, because if you use a global name locally, and then try to *also* access the global name, you will fail. This is called shadowing, and the problem with shadowing is when you do it by accident. (Newbies are particularly prone to this, especially when they call variables "str", "list", etc.) But it is the "by accident" part that is dangerous: there is nothing wrong with shadowing globals or builtins when you do it by design. > good ideas : > > # global > nameThatWillNotBeUsedlocally = 'foo' Oh please. Names can be too long as well as too short. > def spam(): > for qtyIndex, quantity in enumerate([5,6,3,1]): > for fruitIndex, fruit in enumerate(['orange', 'banana']): > print fruitIndex, fruit > print qtyIndex, quantity More sensible naming conventions are to be encouraged, but verbose names just for the sake of verbosity is not. spam() is a five line function; if the programmer can't keep track of the meaning of loop variables i and j over five lines, perhaps they should consider a change of career and get a job more suited to their intellectual prowess. I hear McDonalds is hiring. If spam() were larger and more complex, then more expressive names would be valuable. But in the simple example you give, it just adds noise. -- Steven From rosuav at gmail.com Wed Dec 14 07:54:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 14 Dec 2011 23:54:35 +1100 Subject: [GENERAL] Philosophical question In-Reply-To: <4EE8976F.7000204@gmx.net> References: <4EE8976F.7000204@gmx.net> Message-ID: On Wed, Dec 14, 2011 at 11:32 PM, Andreas wrote: > Hi, > > I asked elsewhere about the best way to store db credentials within a > user-session of a web-app. > > It appeared that it was for everybody but me evident that instead of heaving > a db-role+passwd for every user of an application it was better to have just > 1 set of db-credentials for the application and recreate a user management > within the app instead using the existing user handling of the dbms. > > That way the app checks the user's password as a md5 in some table and > remembers "user is logged in" for later. The actual queries would be done > with a common set of real db credentials. This is I think the most common way to do things in web apps these days. It's viable, at least; whether or not it's the best option is another question. As a side point, simply MD5'ing a user's password is sadly insufficient for proper security. (But at least you're not considering storing plain-text passwords.) If anyone managed to get hold of your users table, they'd be able to recognize any of the most common passwords. You'll want to use some kind of salt; and preferably, a newer and stronger algorithm than MD5. One simple way to do this is to concatenate the user name or numeric ID with a constant string of your own invention, and then put the password after that - so if user 'fred' signs up with password 'barney', you might hash 'fredNaClbarney', which has a SHA-1 of 2DC074250DDA7A903FE6A11B1AEC1EF0A80A0408. Without knowing that you use "NaCl" as your salt, an attacker would have some difficulty brute-forcing the passwords; and including the username means that if someone else uses the same password, the hashes will differ. That aside, your idea more or less matches up with what a large number of web sites do. ChrisA From candide at free.invalid Wed Dec 14 07:57:33 2011 From: candide at free.invalid (candide) Date: Wed, 14 Dec 2011 13:57:33 +0100 Subject: Regexp : repeated group identification In-Reply-To: References: <4ee88488$0$27871$426a74cc@news.free.fr> Message-ID: <4ee89d3d$0$7725$426a74cc@news.free.fr> Le 14/12/2011 12:34, Vlastimil Brom a ?crit : > "If a group is contained in a part of the pattern that matched > multiple times, the last match is returned." > I missed this point, your answer matches my question ;) thanks. > If you need to work with the content captured in the repeated group, > you may check the new regex implementation: > http://pypi.python.org/pypi/regex > > Which has a special "captures" method of the match object for this > (beyond many other improvements): > >>>> import regex >>>> m=regex.match('(Spam\d)+', 'Spam4Spam2Spam7Spam8') >>>> m.captures(1) > ['Spam4', 'Spam2', 'Spam7', 'Spam8'] >>>> > Thanks for the reference and the example. I didn't know of this reimplementation, hoping it offers the Aho-Corasick algorithm allowing multiple keys search. From jpiitula at ling.helsinki.fi Wed Dec 14 08:09:48 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2011 15:09:48 +0200 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <851cbabf-60b6-4806-bef5-e16f6089b08d@h37g2000pri.googlegroups.com> Message-ID: rusi writes: > On Dec 14, 1:56?pm, Jussi Piitulainen > wrote: > > > > Is someone saying that _division_ is not defined because -42 div -5 is > > somehow both 9 and 8? Hm, yes, I see that someone might. The two > > operations, div and rem, need to be defined together. > ----------------------------- > Haskell defines a quot-rem pair and a div-mod pair as follows: > (from http://www.haskell.org/onlinereport/basic.html) > > (x `quot` y)*y + (x `rem` y) == x > (x `div` y)*y + (x `mod` y) == x > > `quot` is integer division truncated toward zero, while the result of > `div` is truncated toward negative infinity. Exactly what I mean. (I gave an incorrect equation but meant this.) From jpiitula at ling.helsinki.fi Wed Dec 14 08:21:38 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2011 15:21:38 +0200 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano writes: > On Wed, 14 Dec 2011 10:56:02 +0200, Jussi Piitulainen wrote: > > I'm not misunderstanding any argument. There was no > > argument. There was a blanket pronouncement that _in mathematics_ > > mod is not a binary operator. I should learn to challenge such > > pronouncements and ask what the problem is. Maybe next time. > > So this was *one* person making that claim? I've seen it a few times from a few different posters, all on Usenet or whatever this thing is nowadays called. I think I was careful to say _some_ mathematicians, but not careful to check that any of them were actually mathematicians speaking as mathematicians. The context seems to be a cultural divide between maths and cs. Too much common ground yet very different interests? > I understand that, in general, mathematicians don't have much need > for a remainder function in the same way programmers do -- modulo > arithmetic is far more important. But there's a world of difference > between saying "In mathematics, extracting the remainder is not > important enough to be given a special symbol and treated as an > operator" and saying "remainder is not a binary operator". The first > is reasonable; the second is not. Yes. From ramapraba2653 at gmail.com Wed Dec 14 08:28:07 2011 From: ramapraba2653 at gmail.com (SUPREME) Date: Wed, 14 Dec 2011 05:28:07 -0800 (PST) Subject: HOT ACTRESS Message-ID: FOR GOOD JOBS SITES TO YOU http://goodjobssites.blogspot.com/ DEEPIKA PADUKONE SPICY WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/deepika-padukone.html KATRINA KAIF LATEST HOT WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/katrina-kaif-wallpapers.html LOVE FAILURE MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/love-failure-movie-stills.html 4FRIENDS MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/4-friends-movie-stills.html NANDEESWARUDU MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/nandeeswarudu-movie-stills.html HARI PRIYA HOT PHOTO STILLS http://actressgallery-kalyani.blogspot.com/2011/12/haripariya-actress.html SHRUTI HASSAN HOT IN 3 MOVIE http://actressgallery-kalyani.blogspot.com/2011/11/shruti-hassan-in-3-movie.html PANJA MOVIE LATEST STILLS http://actressgallery-kalyani.blogspot.com/2011/11/panja-movie-stills.html URIMI LATEST MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/11/urimi-movie-stills.html NIPPU MOVIE WORKING STILLS http://actressgallery-kalyani.blogspot.com/2011/11/nippu-movie-stills.html FOR FAST UPDATES IN FILM INDUSTRY DEEKSHA SETH HOT PHOTO STILLS http://allyouwants.blogspot.com/2011/01/deeksha-seth-hot-photo-shoot-stills.html KATRINA KAIF RARE PHOTOS http://allyouwants.blogspot.com/2011/12/katrina-kaif.html PARVATHI MELTON LATEST PHOTOSHOOT http://allyouwants.blogspot.com/2011/11/pravthi-melton-latest-photoshoot.html PANJAA FAME HOT PHOTOS http://allyouwants.blogspot.com/2011/11/panja-fame-sarah-jane-dias-hot.html NIKITHA IN HOT BLUE SAREE http://allyouwants.blogspot.com/2011/01/nikitha-hot-stills-in-blue-saree.html TRISHA LATEST PHOTO STILLS http://allyouwants.blogspot.com/2011/10/trisha-gorgeous-photos-on-jfw-magazine.html FOR ONLY HOT GUYS SEE THIS LATEST HOT KATRINA KAIF PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/katrina-kaif-hot.html ANUSHKA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/anushka-hot.html TAMANNA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/tamanna-hot.html PARUL HOT PHOTO STILLS http://hotactress-kalyani.blogspot.com/2011/12/parul-hot.html ADITI AGARWAL NEW ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/12/aditi-agarwal-hot.html PAYAL GHOSH HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/payal-ghosh-hot.html RAGINI DWIVEDI HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/12/ragini-dwivedi.html PARVATHI MELTON LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/parvathi-melton-hot.html SARAH JANE DIAS HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/sarah-jane-dias-hot.html KAJAL AGARWAL HOT SAREE STILLS http://hotactress-kalyani.blogspot.com/2011/11/kajal-agarwal-hot-in-saree.html POONAM KAUR HOT ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/11/poonam-kaur-hot.html From hoogendoorn.eelco at gmail.com Wed Dec 14 08:29:35 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 14 Dec 2011 05:29:35 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> <4ee898c6$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <0d38ea3b-46ff-4fbc-9a1c-8e028c3a7d8b@n10g2000vbg.googlegroups.com> On Dec 14, 1:38?pm, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 02:09:32 -0800, Eelco wrote: > > Arguably, the most elegant thing to do is to define integer division and > > remainder as a single operation; which is not only the logical thing to > > do mathematically, but might work really well programmatically too. > > > The semantics of python dont really allow for this though. One could > > have: > > > d, r = a // b > > That would be: > > >>> divmod(17, 5) > > (3, 2) Cool; if only it were in the math module id be totally happy. > > But it wouldnt work that well in composite expressions; selecting the > > right tuple index would be messy and a more verbose form would be > > preferred. However, performance-wise its also clearly the best solution, > > as one often needs both output arguments and computing them > > simultaniously is most efficient. > > Premature optimization. We are talking language design here, not language use. Whether or not this is premature is a decision that should be left to the user, if at all possible, which in this case it very well is; just provide multiple functions to cover all use cases (only return divisor, only return remainder, or both) From jeanmichel at sequans.com Wed Dec 14 08:35:14 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 14 Dec 2011 14:35:14 +0100 Subject: Overriding a global In-Reply-To: <4ee89c41$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> <4ee89c41$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EE8A612.1010504@sequans.com> Steven D'Aprano wrote: > On Wed, 14 Dec 2011 13:05:19 +0100, Jean-Michel Pichavant wrote: > > >> Bad ideas : >> >> i = 5 >> >> def spam(): >> for i,v in enumerate([1,2,3,4]): >> for i,v in enumerate(['a','b', 'c']): >> print i, v >> print i,v # bad surprise >> > > The bad surprise happens because you are using the same name twice in > *one* namespace, the local scope. This example has nothing to do with > local/global name clashes: the existence of global i is irrelevant. > Python's scoping rules work correctly, and global i is not affected by > the local i. > > Programming languages use multiple namespaces so that you don't need to > make your variable names globally unique. There are languages that don't > distinguish between local and global. Python is not one of them. The > programmer should feel free to use local names without worrying too much > if they accidentally use a global name. > > Having said that, re-using names isn't *entirely* risk free, because if > you use a global name locally, and then try to *also* access the global > name, you will fail. This is called shadowing, and the problem with > shadowing is when you do it by accident. (Newbies are particularly prone > to this, especially when they call variables "str", "list", etc.) But it > is the "by accident" part that is dangerous: there is nothing wrong with > shadowing globals or builtins when you do it by design. > > > >> good ideas : >> >> # global >> nameThatWillNotBeUsedlocally = 'foo' >> > > Oh please. Names can be too long as well as too short. > > > >> def spam(): >> for qtyIndex, quantity in enumerate([5,6,3,1]): >> for fruitIndex, fruit in enumerate(['orange', 'banana']): >> print fruitIndex, fruit >> print qtyIndex, quantity >> > > More sensible naming conventions are to be encouraged, but verbose names > just for the sake of verbosity is not. spam() is a five line function; if > the programmer can't keep track of the meaning of loop variables i and j > over five lines, perhaps they should consider a change of career and get > a job more suited to their intellectual prowess. I hear McDonalds is > hiring. > > If spam() were larger and more complex, then more expressive names would > be valuable. But in the simple example you give, it just adds noise. > > The next time I'll illustrate meaningful names, I'll write a 3000 lines function, just to be sure no one states that my point does'nt apply to a function named spam which only counts from 1 to 3. And don't answer that the spam function above does not count from 1 to 3, I know it doesn't. For anyone interested in the actual topic, a good reading is http://tottinge.blogsome.com/meaningfulnames/#Mult_Meanings JM From vlastimil.brom at gmail.com Wed Dec 14 08:38:36 2011 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Wed, 14 Dec 2011 14:38:36 +0100 Subject: Regexp : repeated group identification In-Reply-To: <4ee89d3d$0$7725$426a74cc@news.free.fr> References: <4ee88488$0$27871$426a74cc@news.free.fr> <4ee89d3d$0$7725$426a74cc@news.free.fr> Message-ID: 2011/12/14 candide : ... > > Thanks for the reference and the example. I didn't know of this > reimplementation, hoping it offers the Aho-Corasick algorithm allowing > multiple keys search. > -- > http://mail.python.org/mailman/listinfo/python-list Hi, I am not sure about the underlying algorithm (it could as well be an internal expansion of the alternatives like ...|...|...), but you can use a list (set, actually) of alternatives to search for. check the "named lists" feature, \L<...> hth, vbr From rosuav at gmail.com Wed Dec 14 08:39:07 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Dec 2011 00:39:07 +1100 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <0d38ea3b-46ff-4fbc-9a1c-8e028c3a7d8b@n10g2000vbg.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> <4ee898c6$0$29979$c3e8da3$5496439d@news.astraweb.com> <0d38ea3b-46ff-4fbc-9a1c-8e028c3a7d8b@n10g2000vbg.googlegroups.com> Message-ID: On Thu, Dec 15, 2011 at 12:29 AM, Eelco wrote: > On Dec 14, 1:38?pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> That would be: >> >> >>> divmod(17, 5) >> >> (3, 2) > > Cool; if only it were in the math module id be totally happy. That's easily solved. import math math.divmod=divmod del __builtins__.divmod :) ChrisA From prologic at shortcircuit.net.au Wed Dec 14 08:54:52 2011 From: prologic at shortcircuit.net.au (James Mills) Date: Wed, 14 Dec 2011 05:54:52 -0800 (PST) Subject: text to html References: Message-ID: <53d45c20-26f7-475d-99a6-adf3a206470b@s10g2000prs.googlegroups.com> On Dec 14, 3:30?am, Pedro Henrique Guedes Souto wrote: > On Tue, Dec 13, 2011 at 3:22 PM, prakash jp wrote: > > Want to publish a log file as a web page, is there a parser to retain the format of the text as is and then convert to html. Please provide the relevant pointers Hey Pedro, You could also use pygments to nicely format the logfile perhaps even with Syntax Highlighting. https://bitbucket.org/freecodeteam/hpaste/src/749c36d184a6/hpaste.circuits/paste.py You could also provide an "Download" link that services the file up as Content-Type: text/plain cheers James From ian.g.kelly at gmail.com Wed Dec 14 10:45:04 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 14 Dec 2011 08:45:04 -0700 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <0d38ea3b-46ff-4fbc-9a1c-8e028c3a7d8b@n10g2000vbg.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> <4ee898c6$0$29979$c3e8da3$5496439d@news.astraweb.com> <0d38ea3b-46ff-4fbc-9a1c-8e028c3a7d8b@n10g2000vbg.googlegroups.com> Message-ID: On Wed, Dec 14, 2011 at 6:29 AM, Eelco wrote: > On Dec 14, 1:38?pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Wed, 14 Dec 2011 02:09:32 -0800, Eelco wrote: >> > Arguably, the most elegant thing to do is to define integer division and >> > remainder as a single operation; which is not only the logical thing to >> > do mathematically, but might work really well programmatically too. >> >> > The semantics of python dont really allow for this though. One could >> > have: >> >> > d, r = a // b >> >> That would be: >> >> >>> divmod(17, 5) >> >> (3, 2) > > Cool; if only it were in the math module id be totally happy. Probably it's not in math because it's not a thin wrapper around a C math library function, which is how the module was conceived. There are already some exceptions in the math module, but I think they are all newer than divmod. From dihedral88888 at googlemail.com Wed Dec 14 11:08:32 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Wed, 14 Dec 2011 08:08:32 -0800 (PST) Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> Message-ID: <26514903.334.1323878912493.JavaMail.geo-discussion-forums@pruu23> On Wednesday, December 14, 2011 4:01:24 PM UTC+8, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > > > To complement what Eric says below: The with statement is looking for an > > instance *method*, which by definition, is a function attribute of a > > *class* (the class of the context manager) that takes an instance of the > > class as its first parameter. > > I'm not sure that is correct... I don't think that there is anything "by > definition" about where methods live. Particularly not in Python where > instance methods can be attributes of the instance itself. > > >>> class Test(object): > ... def method(self): > ... print("This method is an attribute of the class.") > ... > >>> t = Test() > >>> t.method() > This method is an attribute of the class. > >>> > >>> import types > >>> t.method = types.MethodType( > ... lambda self: print( > ... "This method is an attribute of the instance."), t) > >>> t.method() > This method is an attribute of the instance. > > > So the normal lookup rules that apply to data attributes, namely > instance, then class, then superclasses, also applies to methods in > Python. In languages that don't allow that sort of thing, like Java, you > need to use convoluted design patterns like Dynamic Proxy to make it > work. In Python, you just create a method and attach it on the instance. > > http://stackoverflow.com/questions/8260740/override-a-method-for-an- > instance-of-a-class > > But this doesn't apply for special dunder attributes like __exit__, for > speed reasons. (For new-style classes only, classic classes have no such > special casing. This makes automatic delegation a breeze in Python 2 with > classic classes, and a PITA in Python 3. Boo hiss.) > > > > -- > Steven In Python an instance of an object of a class can have its own method. A living object can use those methods in the class definition and can acquire a new method at runtime. From arnodel at gmail.com Wed Dec 14 11:13:59 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Wed, 14 Dec 2011 16:13:59 +0000 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> Message-ID: On 14 December 2011 12:33, Eelco wrote: > On 14 dec, 12:55, Arnaud Delobelle wrote: >> On 14 December 2011 07:49, Eelco wrote: >> > On Dec 14, 4:18?am, Steven D'Aprano > > +comp.lang.pyt... at pearwood.info> wrote: >> >> > They might not be willing to define it, but as soon as we programmers >> >> > do, well, we did. >> >> >> > Having studied the contemporary philosophy of mathematics, their concern >> >> > is probably that in their minds, mathematics is whatever some dead guy >> >> > said it was, and they dont know of any dead guy ever talking about a >> >> > modulus operation, so therefore it 'does not exist'. >> >> >> You've studied the contemporary philosophy of mathematics huh? >> >> >> How about studying some actual mathematics before making such absurd >> >> pronouncements on the psychology of mathematicians? >> >> > The philosophy was just a sidehobby to the study of actual >> > mathematics; and you are right, studying their works is the best way >> > to get to know them. Speaking from that vantage point, I can say with >> > certainty that the vast majority of mathematicians do not have a >> > coherent philosophy, and they adhere to some loosely defined form of >> > platonism. Indeed that is absurd in a way. Even though you may trust >> > these people to be perfectly functioning deduction machines, you >> > really shouldnt expect them to give sensible answers to the question >> > of which are sensible axioms to adopt. They dont have a reasoned >> > answer to this, they will by and large defer to authority. >> >> Please come down from your vantage point for a few moments and >> consider how insulting your remarks are to people who have devoted >> most of their intellectual energy to the study of mathematics. ?So >> you've studied a bit of mathematics and a bit of philosophy? ?Good >> start, keep working at it. > > Thanks, I intend to. > >> You think that every mathematician should be preoccupied with what >> axioms to adopt, and why? > > Of course I dont. If you wish to restrict your attention to the > exploration of the consequences of axioms others throw at you, that is > a perfectly fine specialization. Most mathematicians do exactly that, > and thats fine. But that puts them in about as ill a position to > judged what is, or shouldnt be defined, as the average plumber. You are completely mistaken. Whatever the axiomatisation of the mathematics that we do, we can still do the same mathematics. We don't even need an axiomatic basis to do mathematics. In fact, the formalisation of mathematics has always come after the mathematics were well established. Euclid, Dedekind, Peano, Zermelo, Frankael, didn't create axiomatic systems out of nothing. They axiomatised pre-existing theories. Axiomatising a theory is just one way of exploring it. > Compounding the problem is not just that they do not wish to concern > themselves with the inductive aspect of mathematics, they would like > to pretend it does not exist at all. For instance, if you point out to > them a 19th century mathematician used very different axioms than a > 20th century one, (and point out they were both fine mathematicians > that attained results universally celebrated), they will typically > respond emotionally; get angry or at least annoyed. According to their > pseudo-Platonist philosophy, mathematics should not have an inductive > side, axioms are set in stone and not a human affair, and the way they > answer the question as to where knowledge about the 'correct' > mathematical axioms comes from is by an implicit or explicit appeal to > authority. They dont explain how it is that they can see 'beyond the > platonic cave' to find the 'real underlying truth', they quietly > assume somebody else has figured it out in the past, and leave it at > that. Again, you are completely mis-representing the situation. In my experience, most mathematicians (I'm not talking about undergraduate students here) do not see the axioms are the root of the mathematics that they do. Formal systems are just one way to explore mathematics. Of course they can in some cases be very useful and enlightening. As for inductive reasoning, I really can't understand your point. Of course mathematicians use inductive reasoning all the time. Where do you think the Riemann Hypothesis comes from? Or Fermat's last theorem? Do you think that mathematicians prove results before they even think about them? On the other hand, a result needs to be proved to be accepted by the mathematical community, and inductive reasoning is not valid in proofs. That's in the nature of mathematics. >> You say that mathematicians defer to authority, but do you really >> think that thousands of years of evolution and refinement in >> mathematics are to be discarded lightly? ?I think not. ?It's good to >> have original ideas, to pursue them and to believe in them, but it >> would be foolish to think that they are superior to knowledge which >> has been accumulated over so many generations. > > For what its worth; insofar as my views can be pidgeonholed, im with > the classicists (pre-20th century), which indeed has a long history. > Modernists in turn discard large swaths of that. Note that its largely > an academic debate though; everybody agrees that 1+1=2. But there are > some practical consequences; if I were the designated science-Tsar, > all transfinite-analysist would be out on the street together with the > homeopaths, for instance. It's telling that on the one hand you criticise mathematicians for not questioning the "axioms which are thrown at them", on the other hand you feel able to discard a perfectly fine piece of mathematics, that of the study of transfinite numbers, because it doesn't fit nicely with traditional views. The fact is that at the end of the 19th century mathematics had reached a crisis point. >> You claim that mathematicians have a poor understanding of philosophy. >> ?It may be so for many of them, but how is this a problem? ?I doesn't >> prevent them from having a deep understanding of their field of >> mathematics. ?Do philosophers have a good understanding of >> mathematics? > > As a rule of thumb: absolutely not, no. I dont think I can think of > any philosopher who turned his attention to mathematics that ever > wrote anything interesting. All the interesting writers had their > boots on mathematical ground; Quine, Brouwer, Weyl and the earlier > renaissance men like Gauss and contemporaries. > > The fragmentation of disciplines is infact a major problem in my > opinion though. Most physicists take their mathematics from the ivory- > math tower, and the mathematicians shudder at the idea of listning > back to see which of what they cooked up is actually anything but > mental masturbation, in the meanwhile cranking out more gibberish > about alephs. Only a minority of mathematicians have an interest in "alephs", as you call them. IMHO, the mathematics they do is perfectly valid. The exploration of the continuum hypothesis has led to the creation of very powerful mathematical techniques and gives an insight into the very foundations of mathematics. Again, on the one hand you criticise mathematicians for not questioning the axioms they work with, but those who investigate the way these axioms interact you accuse of "mental masturbation". -- Arnaud From ian.g.kelly at gmail.com Wed Dec 14 11:27:35 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 14 Dec 2011 09:27:35 -0700 Subject: Property Abuse In-Reply-To: References: Message-ID: On Wed, Dec 14, 2011 at 1:28 AM, Felipe O wrote: > Hi All, > I was wondering what everyone's thought process was regarding properties. > Lately I find I've been binging on them and have classes with > 10 > properties. While pylint doesn't complain (yet), it tends to be picky about > keeping instance attribute counts low, so I figure there's something against > that. How do you guys decide between using properties versus getter methods, > or how do you refactor them if neither? I prefer direct instance attribute access where possible*, properties where necessary, and methods where an argument is needed or the relationship is more complex than get/set/delete. * One of the strengths of Python's property system** is that you can switch between plain attributes and mutable properties as needed without breaking dependent code. Often I see people doing this, which drives me nuts with its useless verbosity, when a plain instance attribute would have sufficed: @property def foo(self): return self._foo @foo.setter def foo(self, value): self._foo = value ** As opposed, for instance, to the .NET property system. You can't arbitrarily switch between public member variables and public properties in .NET, because it breaks ABI. Cheers, Ian From dihedral88888 at googlemail.com Wed Dec 14 11:28:57 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Wed, 14 Dec 2011 08:28:57 -0800 (PST) Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <26514903.334.1323878912493.JavaMail.geo-discussion-forums@pruu23> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <26514903.334.1323878912493.JavaMail.geo-discussion-forums@pruu23> Message-ID: <13752584.332.1323880138063.JavaMail.geo-discussion-forums@pruu5> On Thursday, December 15, 2011 12:08:32 AM UTC+8, 88888 Dihedral wrote: > On Wednesday, December 14, 2011 4:01:24 PM UTC+8, Steven D'Aprano wrote: > > On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > > > > > To complement what Eric says below: The with statement is looking for an > > > instance *method*, which by definition, is a function attribute of a > > > *class* (the class of the context manager) that takes an instance of the > > > class as its first parameter. > > > > I'm not sure that is correct... I don't think that there is anything "by > > definition" about where methods live. Particularly not in Python where > > instance methods can be attributes of the instance itself. > > > > >>> class Test(object): > > ... def method(self): > > ... print("This method is an attribute of the class.") > > ... > > >>> t = Test() > > >>> t.method() > > This method is an attribute of the class. > > >>> > > >>> import types > > >>> t.method = types.MethodType( > > ... lambda self: print( > > ... "This method is an attribute of the instance."), t) > > >>> t.method() > > This method is an attribute of the instance. > > > > > > So the normal lookup rules that apply to data attributes, namely > > instance, then class, then superclasses, also applies to methods in > > Python. In languages that don't allow that sort of thing, like Java, you > > need to use convoluted design patterns like Dynamic Proxy to make it > > work. In Python, you just create a method and attach it on the instance. > > > > http://stackoverflow.com/questions/8260740/override-a-method-for-an- > > instance-of-a-class > > > > But this doesn't apply for special dunder attributes like __exit__, for > > speed reasons. (For new-style classes only, classic classes have no such > > special casing. This makes automatic delegation a breeze in Python 2 with > > classic classes, and a PITA in Python 3. Boo hiss.) > > > > > > > > -- > > Steven > > In Python an instance of an object of a class can have its own method. > A living object can use those methods in the class definition and > can acquire a new method at runtime. Therefore, it is possible for an object to build its decision tree of actions toward a problem of various parameters in the run time. From jeanmichel at sequans.com Wed Dec 14 11:37:15 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 14 Dec 2011 17:37:15 +0100 Subject: Property Abuse In-Reply-To: References: Message-ID: <4EE8D0BB.6020004@sequans.com> Ian Kelly wrote: > On Wed, Dec 14, 2011 at 1:28 AM, Felipe O wrote: > >> Hi All, >> I was wondering what everyone's thought process was regarding properties. >> Lately I find I've been binging on them and have classes with > 10 >> properties. While pylint doesn't complain (yet), it tends to be picky about >> keeping instance attribute counts low, so I figure there's something against >> that. How do you guys decide between using properties versus getter methods, >> or how do you refactor them if neither? >> > > I prefer direct instance attribute access where possible*, properties > where necessary, and methods where an argument is needed or the > relationship is more complex than get/set/delete. > > * One of the strengths of Python's property system** is that you can > switch between plain attributes and mutable properties as needed > without breaking dependent code. Often I see people doing this, which > drives me nuts with its useless verbosity, when a plain instance > attribute would have sufficed: > > @property > def foo(self): > return self._foo > > @foo.setter > def foo(self, value): > self._foo = value > > ** As opposed, for instance, to the .NET property system. You can't > arbitrarily switch between public member variables and public > properties in .NET, because it breaks ABI. > > Cheers, > Ian > I second this opinion, plain attributes are what's required most of the time. JM From joshua.landau.ws at gmail.com Wed Dec 14 11:54:09 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 14 Dec 2011 16:54:09 +0000 Subject: Overriding a global In-Reply-To: <4EE8771D.1050902@sequans.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> Message-ID: On 14 December 2011 10:14, Jean-Michel Pichavant wrote: > Joshua Landau wrote: > > On 13 December 2011 13:30, Jean-Michel Pichavant > jeanmichel at sequans.com**>> wrote: >> >> writing >> >> x = 1 >> >> def spam(): >> x = 2 >> >> is in general a bad idea. That was my point. >> >> >> Why? I have a few (probably wrong) guesses. >> >> Because you expect it to be the same every time you use it? >> Well, then this should be "in general a bad idea": >> x = 1; print(x); x = 2; print(x) >> > you're changing the value of x, that's fine. In the example above, the > assignement is not the problem. The problem is that you create 2 different > 'x', one in globals(), and one in locals(). Using the same name within 2 > implicit namespaces is a bad idead in general because it can be difficult > to know which one is used. > > If you want to have fun, try this code, prepared to be amazed. There's > something funny with the global statement, it's applied on the whole block > no matter where it is stated in the block. > x=1 # global > > def spam(): > x = 2 # local (or so you may think) > print x > global x # I need to use the global one now > print x > print locals() > :3: SyntaxWarning: name 'x' is assigned to before global declaration I think it's a null point. We're not talking about "globals" being wrong (or was this poor design) for efficiency reasons. There's an error there for reason. > For more fun you could create a 'x' name in __builtin__ and import it so > that people never know which x you are using. > > Even though it makes total sense to me. >> >> Is it because it's used to different purpose between similarly-looking >> functions? >> This looks fine, though: >> def func1(): x=1; print(x) >> def func2(): x=2; print(x) >> >> Is it because it looks like a reassignment of the more global x? >> I don't have an example here but, simply put, I don't believe this. We >> can use "id" as our own local variable without thinking that we're >> tampering with "__builtins__.id". I don't see it as much of a leap from >> builtin to global (except that you /*can*/ do "dir = 1; del dir; dir" >> without error). >> >> >> That said, I'm sorta' just guessing the reason you might think it's a bad >> idea. >> > > The problem makes little sense when using names like x or func1. Besides > namespace issues, naming 2 *different objects* with the same meaningful > name is usually a bad idea and points the fact that your names are no that > meaningful. To go back to the original post, having a 'logger' that may > name 2 different logger object during the execution is a bad idea. One > quick way to fix it is to name the logger 'currentLogger', this way you > warn the reader that the logger named by curentLogger may change over time. > > As someone sugggested in this thread one other option is to use a > different name for the second logger. So the only problem is that "x" isn't meaningful? def countToTen(): for number in range(1, 10): print(number) def countToTwenty(): for number in range(1, 20): print(number) .. Looks fine to me. Using currentLogger is just padding, in my opinion. *Every *value is "current". That changes nothing. As long as you never infer that "logger" is static or totally global (no "global logger" in global namespace or terming it "LOGGER") I would never assume as much, as with every other variable I see. Unless I see "global " in a function, I need to hold the belief that can change. In regards to a second name - yes this could work and in many cases would be desirable, but it doesn't really help this circumstance. [assume, for a moment, a lot of functions used a local logger] "localLogger" would tell you very little about what the logger actually *is*. You still have to look that up. [end assumption] Additionally, this would make changing a logger that uses the default to a local one *much* harder. And don't say "but you can just always make a local copy", as then you lose the advantage of a global. Typing something like "logger = childLogger(id)" to the start of a function call *is explicit*, it's clean, and it makes sense to have a default that's global. You're not appending cruft ("current") and you have consistency. If you added "logger = globalLogger" to every function start as well you can argue that it's better. I agree it's more explicit. But then you lose unneeded if only a small portion of your code localises logger. But I would recommend it if a large portion of code used local variants. AND: > The next time I'll illustrate meaningful names, I'll write a 3000 lines > function, just to be sure no one states that my point does'nt apply to a > function named spam which only counts from 1 to 3. > And don't answer that the spam function above does not count from 1 to 3, > I know it doesn't. You're acting in sarcasm to a comment on scale, when you yourself said that one of my comments was invalid due to names that were scaled down for exampling. It seems a bit hypocritical to me. That said, not all functions are long. If the short ones use short names that's fine: I'm pretty sure you said it's not. And in regards to the link: 1) __add__ says otherwise (technically, the operator "+"). It's rarely confused me. 2) That's not what we're discussing. As it said: "As long as the parameter lists are semantically equal and the desired result is the same, all is well." They're doing semantically the same thing (to different log levels) with the same parameter lists and they're not class methods. You /could/ say that the semantics are different, but classes act in a context in the same way local variables can be thought of doing, and semantics are the same for them. Instead of a different self, it's a different log file/level. Same semantics. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ericsnowcurrently at gmail.com Wed Dec 14 11:56:10 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Wed, 14 Dec 2011 09:56:10 -0700 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: Message-ID: On Tue, Dec 13, 2011 at 11:05 PM, Eric Snow wrote: > On Tue, Dec 13, 2011 at 10:42 PM, Steve Howell wrote: >> I'm using Python 3.2.2, and the following program gives me an error >> that I don't understand: >> >> class Foo: >> ?pass >> >> foo = Foo() >> foo.name = "Steve" >> >> def add_goodbye_function(obj): >> ?def goodbye(): >> ? ?print("goodbye " + obj.name) >> ?obj.goodbye = goodbye >> >> add_goodbye_function(foo) >> foo.goodbye() # outputs goodbye Steve >> foo.__exit__ = foo.goodbye >> foo.__exit__() # outputs goodbye Steve >> >> with foo: # fails with AttributeError: ?__exit__ >> ?print("doing stuff") >> >> I am dynamically adding an attribute __exit__ to the variable foo, >> which works fine when I call it directly, but it fails when I try to >> use foo as the expression in the with statement. ?Here is the full >> output: >> >>> python3 with.coffee >> goodbye Steve >> goodbye Steve >> Traceback (most recent call last): >> ?File "with.coffee", line 17, in >> ? ?with foo: # fails with AttributeError: >> AttributeError: __exit__ >> >> What am I doing wrong? > > That is a tricky one. > > As with many of the special methods (start and end with __) in Python, > the underlying mechanism in the interpreter is directly pulling the > function from the class object. ?It does not look to the instance > object for the function at any time. ?See > http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes. > > -eric Incidently, if you add the function directly to the class, everything works out: class Foo(object): # or "class Foo:" under Python 3 pass foo = Foo() foo.name = "Steve" def add_goodbye_function(cls): def goodbye(self, *args, **kwargs): print("goodbye " + self.name) cls.goodbye = goodbye add_goodbye_function(type(foo)) foo.goodbye() # outputs goodbye Steve Foo.__exit__ = foo.goodbye foo.__exit__() # outputs goodbye Steve Foo.__enter__ = (lambda self: None) with foo: print("doing stuff") However, perhaps a better approach would be to put the different pieces directly into the class: class Foo: # python 3 def __init__(self, name): self.name = name def goodbye(self): print("goodbye " + self.name) def __enter__(self): pass def __exit__(self, *args, **kwargs): self.goodbye() foo = Foo("Steve") foo.goodbye() # outputs goodbye Steve foo.__exit__() # outputs goodbye Steve with foo: print("doing stuff") If you want to be more dynamic about it you can do it, but it involves black magic. Chances are really good that being explicit through your class definition is the right approach. -eric From jeanmichel at sequans.com Wed Dec 14 12:06:19 2011 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Wed, 14 Dec 2011 18:06:19 +0100 Subject: Overriding a global In-Reply-To: References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> Message-ID: <4EE8D78B.108@sequans.com> Joshua Landau wrote: > [snip] > Using currentLogger is just padding, in my opinion. *Every *value is > "current". Not always. I try to keep names on the same object because that object is supposed to be named that way. I can change one of the object attribute, but the object named that way keep being the same. Class Foo: self.__init__(self): self.banana = 5 myFoo = Foo() Now there's a slight difference between myFoo = Exception() and myFoo.banana = 4 The first statement rebind myFoo to something complitely different. the second statement change one of the object rightfully named myFoo .. attribute (not sure about this construct :D ) Int being inmutable, you can rebind a name without changing its meaning. > > In regards to a second name - yes this could work and in many cases > would be desirable, but it doesn't really help this circumstance. > [assume, for a moment, a lot of functions used a local logger] > "localLogger" would tell you very little about what the logger > actually /is/. You still have to look that up. [end assumption] > Additionally, this would make changing a logger that uses the default > to a local one /much/ harder. And don't say "but you can just always > make a local copy", as then you lose the advantage of a global. > > Typing something like "logger = childLogger(id)" to the start of a > function call *is explicit*, it's clean, and it makes sense to have a > default that's global. You're not appending cruft ("current") and you > have consistency. If you added "logger = globalLogger" to every > function start as well you can argue that it's better. I agree it's > more explicit. But then you lose unneeded if only a small portion of > your code localises logger. But I would recommend it if a large > portion of code used local variants. > > AND: > > The next time I'll illustrate meaningful names, I'll write a 3000 > lines function, just to be sure no one states that my point > does'nt apply to a function named spam which only counts from 1 to 3. > And don't answer that the spam function above does not count from > 1 to 3, I know it doesn't. > > > You're acting in sarcasm to a comment on scale, when you yourself said > that one of my comments was invalid due to names that were scaled down > for exampling. It seems a bit hypocritical to me. That said, not all > functions are long. If the short ones use short names that's fine: I'm > pretty sure you said it's not. > > And in regards to the link: > 1) __add__ says otherwise (technically, the operator "+"). It's rarely > confused me. > 2) That's not what we're discussing. As it said: "As long as the > parameter lists are semantically equal and the desired result is the > same, all is well." They're doing semantically the same thing (to > different log levels) with the same parameter lists and they're not > class methods. You /could/ say that the semantics are different, but > classes act in a context in the same way local variables can be > thought of doing, and semantics are the same for them. Instead of a > different self, it's a different log file/level. Same semantics. I'd like to argue about that but I won't cause I have the feeling my lack of ultra precise english would cause me more trouble. Note that I'm not blaming anyone but me, no sarcasm inside. JM From hoogendoorn.eelco at gmail.com Wed Dec 14 12:15:47 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 14 Dec 2011 09:15:47 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> Message-ID: <91f6f2ec-25ac-4ca8-b654-f605a7c8a53e@c13g2000vbh.googlegroups.com> 'Kindof' off-topic, but what the hell :). On Dec 14, 5:13?pm, Arnaud Delobelle wrote: > On 14 December 2011 12:33, Eelco wrote: > > On 14 dec, 12:55, Arnaud Delobelle wrote: > >> On 14 December 2011 07:49, Eelco wrote: > >> > On Dec 14, 4:18?am, Steven D'Aprano >> > +comp.lang.pyt... at pearwood.info> wrote: > >> >> > They might not be willing to define it, but as soon as we programmers > >> >> > do, well, we did. > > >> >> > Having studied the contemporary philosophy of mathematics, their concern > >> >> > is probably that in their minds, mathematics is whatever some dead guy > >> >> > said it was, and they dont know of any dead guy ever talking about a > >> >> > modulus operation, so therefore it 'does not exist'. > > >> >> You've studied the contemporary philosophy of mathematics huh? > > >> >> How about studying some actual mathematics before making such absurd > >> >> pronouncements on the psychology of mathematicians? > > >> > The philosophy was just a sidehobby to the study of actual > >> > mathematics; and you are right, studying their works is the best way > >> > to get to know them. Speaking from that vantage point, I can say with > >> > certainty that the vast majority of mathematicians do not have a > >> > coherent philosophy, and they adhere to some loosely defined form of > >> > platonism. Indeed that is absurd in a way. Even though you may trust > >> > these people to be perfectly functioning deduction machines, you > >> > really shouldnt expect them to give sensible answers to the question > >> > of which are sensible axioms to adopt. They dont have a reasoned > >> > answer to this, they will by and large defer to authority. > > >> Please come down from your vantage point for a few moments and > >> consider how insulting your remarks are to people who have devoted > >> most of their intellectual energy to the study of mathematics. ?So > >> you've studied a bit of mathematics and a bit of philosophy? ?Good > >> start, keep working at it. > > > Thanks, I intend to. > > >> You think that every mathematician should be preoccupied with what > >> axioms to adopt, and why? > > > Of course I dont. If you wish to restrict your attention to the > > exploration of the consequences of axioms others throw at you, that is > > a perfectly fine specialization. Most mathematicians do exactly that, > > and thats fine. But that puts them in about as ill a position to > > judged what is, or shouldnt be defined, as the average plumber. > > You are completely mistaken. ?Whatever the axiomatisation of the > mathematics that we do, we can still do the same mathematics. ?We > don't even need an axiomatic basis to do mathematics. ?In fact, the > formalisation of mathematics has always come after the mathematics > were well established. ? ?Euclid, Dedekind, Peano, Zermelo, Frankael, > didn't create axiomatic systems out of nothing. ?They axiomatised > pre-existing theories. > > Axiomatising a theory is just one way of exploring it. Yes, axiomization is to some extent a side-show. We know what it is that we want mathematics to be, and we try to find the axioms that lead to those conclusions. Not qualitatively different from any other form of induction (of the epistemological rather than mathematical kind). Still, different axioms or meta-mathematics give subtly different results, not to mention are as different to work with as assembler and haskell. There are no alephs if you start from a constructive basis, for instance. Im not sure what 'Axiomatising a theory is just one way of exploring it' means. One does not axiomatize a single theory; that would be trivial (A is true because thats what I define A to be). One constructs a single set of axioms from which a nontrivial set of theorems follow. The way id put it, is that axiomazation is about being explicit in what it is that you assume, trying to minimalize that, and being systematic about what conclusions that forces you to embrace. Could you be more precise as to how I am 'completely mistaken'? I acknowledge that my views are outside the mainstream, so its no news to me many would think so, but it would be nice to know what im arguing against in this thread precisely. > > Compounding the problem is not just that they do not wish to concern > > themselves with the inductive aspect of mathematics, they would like > > to pretend it does not exist at all. For instance, if you point out to > > them a 19th century mathematician used very different axioms than a > > 20th century one, (and point out they were both fine mathematicians > > that attained results universally celebrated), they will typically > > respond emotionally; get angry or at least annoyed. According to their > > pseudo-Platonist philosophy, mathematics should not have an inductive > > side, axioms are set in stone and not a human affair, and the way they > > answer the question as to where knowledge about the 'correct' > > mathematical axioms comes from is by an implicit or explicit appeal to > > authority. They dont explain how it is that they can see 'beyond the > > platonic cave' to find the 'real underlying truth', they quietly > > assume somebody else has figured it out in the past, and leave it at > > that. > > Again, you are completely mis-representing the situation. ?In my > experience, most mathematicians (I'm not talking about undergraduate > students here) do not see the axioms are the root of the mathematics > that they do. ?Formal systems are just one way to explore mathematics. > ?Of course they can in some cases be very useful and enlightening. Its your word versus mine I suppose. > As for inductive reasoning, I really can't understand your point. ?Of > course mathematicians use inductive reasoning all the time. ?Where do > you think the Riemann Hypothesis comes from? Or Fermat's last theorem? > ?Do you think that mathematicians prove results before they even think > about them? ?On the other hand, a result needs to be proved to be > accepted by the mathematical community, and inductive reasoning is not > valid in proofs. ?That's in the nature of mathematics. We mean something different by the term it seems. What you describe, I would call intuition. Which is indeed very important in mathematics, and indeed no substitute for deduction. By induction, I mean the process of reducing particular facts/observations/theorems to a more compact body of theory/axioms that imply the same. In a way, its the inverse of deduction (seeing which body of conclusions follows from a given set of axioms) > >> You say that mathematicians defer to authority, but do you really > >> think that thousands of years of evolution and refinement in > >> mathematics are to be discarded lightly? ?I think not. ?It's good to > >> have original ideas, to pursue them and to believe in them, but it > >> would be foolish to think that they are superior to knowledge which > >> has been accumulated over so many generations. > > > For what its worth; insofar as my views can be pidgeonholed, im with > > the classicists (pre-20th century), which indeed has a long history. > > Modernists in turn discard large swaths of that. Note that its largely > > an academic debate though; everybody agrees that 1+1=2. But there are > > some practical consequences; if I were the designated science-Tsar, > > all transfinite-analysist would be out on the street together with the > > homeopaths, for instance. > > It's telling that on the one hand you criticise mathematicians for not > questioning the "axioms which are thrown at them", on the other hand > you feel able to discard a perfectly fine piece of mathematics, that > of the study of transfinite numbers, because it doesn't fit nicely > with traditional views. ?The fact is that at the end of the 19th > century mathematics had reached a crisis point. It is a shame you both fail to specify by what metric it is a perfectly fine piece of mathematics, and yet more egregiously, put words into my mouth as to what I think is wrong with it. That makes for slow and painful debating. My objection to transfinite analysis is that it is not scientific, in the sense that I judge most parts of mathematics to be. I am not questioning its deductive validity; that something mathematicians can generally be trusted with. My contention is that the axioms that give rise to transfinite analysis are of the same 'validity' as any random set of axioms you could pull from a random number generator. All sets of axioms have implications, but we dont study all possible sets of axioms. Studying a random set of axioms leads to an arbitrary number of nonsensical results; nonsensical in the common day useage of the word, and nonsensical in a philosophical sense; as not relating to any sense-impressions, or synthetic propositions. Transfinite analysis does not give any results of any relevance that im aware of, but id love to be proven wrong. The fact that we do get this cancerous outgrowth of implications called transfinite analysis is a hint that these axioms are borked; not a beautiful view on a world of truth beyond our senses that only mathematics can give us. (again, in my minority opinion). Id love to debate you as to where exactly I suspect things went wrong, but its a lengthy story, and its really not the right place I suppose; nor the right time, I have to cook. > >> You claim that mathematicians have a poor understanding of philosophy. > >> ?It may be so for many of them, but how is this a problem? ?I doesn't > >> prevent them from having a deep understanding of their field of > >> mathematics. ?Do philosophers have a good understanding of > >> mathematics? > > > As a rule of thumb: absolutely not, no. I dont think I can think of > > any philosopher who turned his attention to mathematics that ever > > wrote anything interesting. All the interesting writers had their > > boots on mathematical ground; Quine, Brouwer, Weyl and the earlier > > renaissance men like Gauss and contemporaries. > > > The fragmentation of disciplines is infact a major problem in my > > opinion though. Most physicists take their mathematics from the ivory- > > math tower, and the mathematicians shudder at the idea of listning > > back to see which of what they cooked up is actually anything but > > mental masturbation, in the meanwhile cranking out more gibberish > > about alephs. > > Only a minority of mathematicians have an interest in "alephs", as you > call them. I know. > IMHO, the mathematics they do is perfectly valid. ?The > exploration of the continuum hypothesis has led to the creation of > very powerful mathematical techniques and gives an insight into the > very foundations of mathematics. Like I said, I dont question its deductive validity. As for providing insight into the deductive process; probably, but so do Sudoku's, and I dont see them being state-sponsored across the globe. Any self- created puzzle will do for that purpose; and I suspect the same time spent on real puzzles has the same effect, plus more. > Again, on the one hand you criticise > mathematicians for not questioning the axioms they work with, but > those who investigate the way these axioms interact you accuse of > "mental masturbation". In my, admittedly outsider view of things, transfinite analysis are rather sad deduction machines. Id be delighted if you could show me one that has done some kind of reflection as to why they so fervently keep chasing the ghosts that the likes of Hilbert and Russel conjured up for them, or that have even bothered to expose themselves to the mockery that the likes of Gauss would have showered upon them (or Feynmann, for a more recent but imperfect analog). From showell30 at yahoo.com Wed Dec 14 12:16:43 2011 From: showell30 at yahoo.com (Steve Howell) Date: Wed, 14 Dec 2011 09:16:43 -0800 (PST) Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> Message-ID: <9440c6ea-f844-460a-ab88-a8890648c70e@t38g2000prg.googlegroups.com> On Dec 14, 12:01?am, Steven D'Aprano wrote: > [...] > > So the normal lookup rules that apply to data attributes, namely > instance, then class, then superclasses, also applies to methods in > Python. In languages that don't allow that sort of thing, like Java, you > need to use convoluted design patterns like Dynamic Proxy to make it > work. In Python, you just create a method and attach it on the instance. > > http://stackoverflow.com/questions/8260740/override-a-method-for-an- > instance-of-a-class > > But this doesn't apply for special dunder attributes like __exit__, for > speed reasons. (For new-style classes only, classic classes have no such > special casing. This makes automatic delegation a breeze in Python 2 with > classic classes, and a PITA in Python 3. Boo hiss.) Thanks to all who responded. Basically, I think the special casing of the "dunder" attributes threw me off. Typically, I would just build context managers from a class, but I wanted to experiment with instances that were built up without the standard Python class mechanisms, instead following a Javascript-like closure-based object creation model. This is what I came up with: class WithWrapper: def __init__(self, obj): self.obj = obj def __enter__(self): self.obj['enter']() def __exit__(self, *args): self.obj['exit'](*args) def greeter_context(hello, goodbye): return { 'enter': lambda: print("---\n" + hello), 'exit': lambda *args: print(goodbye) } gc_french = greeter_context("salut", "a plus tard") with WithWrapper(gc_french): print("doing stuff") gc_slang = greeter_context("yo", "later") with WithWrapper(gc_slang): print("doing stuff") From as at sci.fi Wed Dec 14 13:40:28 2011 From: as at sci.fi (Anssi Saari) Date: Wed, 14 Dec 2011 20:40:28 +0200 Subject: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2) References: <0da3ce5f-72ca-43a2-a2ae-18fd1dd2c46d@e2g2000vbb.googlegroups.com> <4edceff9$0$29988$c3e8da3$5496439d@news.astraweb.com> Message-ID: Christian Heimes writes: >> Mea culpa, forgot that. Yes, use altinstall. Although it's probably >> not a problem to replace 2.6.6 with 2.7.2 - I doubt that'll break many >> things. > > Except that all 3rd party extensions and packages are missing if you > install Python manually. True, they would have to be built or at least installed manually also. Major work, especially for a beginner. Shouldn't pick Debian Stable and then want current software... Then again, even the standard install of Python has plenty of stuff. I installed 2.7.2 on my Debian system just to try out some of the new Tk stuff. > Debian's backports should provide a well integrated Python 2.7 > package. But it doesn't. Python 2.7.2 is in Wheezy, which is the current testing version of Debian. Looks like it has about 700 release critical bugs, so it'll be a while until release. From cosmo_general at yahoo.com Wed Dec 14 13:47:24 2011 From: cosmo_general at yahoo.com (Muddy Coder) Date: Wed, 14 Dec 2011 10:47:24 -0800 (PST) Subject: What is this widget? Message-ID: Hi Folks, I am trying to write letters on a photo that is opened in a canvas. So I think I must need a widget to contain the letters I will type in. I tried to use a Label, it worked. But, a Label covered part of the photo underneath, so I can't use it. I saw some software did such a thing nicely: a box popped on a photo, with dotted lines as borders, expandable. When such a box was re-sized with a mouse, the font size in the box also got changed. The box has no background color, so it does not cover a rectangle area on the photo. I need such a widget, but I don't know what is this one. I tried Text, Label, but they all come with Window-like stuff, so they cover some photo content. Can somebody points me a direction? Thanks! Cosmo From lie.1296 at gmail.com Wed Dec 14 14:14:19 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 15 Dec 2011 06:14:19 +1100 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: Message-ID: On 12/15/2011 03:56 AM, Eric Snow wrote: > On Tue, Dec 13, 2011 at 11:05 PM, Eric Snow wrote: > > If you want to be more dynamic about it you can do it, but it involves > black magic. Chances are really good that being explicit through your > class definition is the right approach. Note that the black spice is to use the __class__ attribute: foo.__class__.__exit__ = foo.goodbye From ericsnowcurrently at gmail.com Wed Dec 14 14:46:58 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Wed, 14 Dec 2011 12:46:58 -0700 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: Message-ID: On Wed, Dec 14, 2011 at 12:14 PM, Lie Ryan wrote: > On 12/15/2011 03:56 AM, Eric Snow wrote: >> >> On Tue, Dec 13, 2011 at 11:05 PM, Eric Snow >> ?wrote: >> >> If you want to be more dynamic about it you can do it, but it involves >> black magic. ?Chances are really good that being explicit through your >> class definition is the right approach. > > > Note that the black spice is to use the __class__ attribute: > > foo.__class__.__exit__ = foo.goodbye Good point. 'type(foo).__exit__ ...' might be even better. Regardless, to get the dynamicism to which Steve originally referred (make it work when added directly to the instance), you have to use a metaclass, which is black magic[1]. However, rarely is that sort of thing needed, so the relatively superfluous details would likely only cloud the question at hand. -eric [1] I will point out that metaclasses aren't really all that bad. They are a tool worth understanding, even if you don't use them all the time. Understanding them also opens up a whole new world of understanding how Python works, particularly regarding name lookup. From d at davea.name Wed Dec 14 14:48:52 2011 From: d at davea.name (Dave Angel) Date: Wed, 14 Dec 2011 14:48:52 -0500 Subject: What is this widget? In-Reply-To: References: Message-ID: <4EE8FDA4.10704@davea.name> On 12/14/2011 01:47 PM, Muddy Coder wrote: > Hi Folks, > > I am trying to write letters on a photo that is opened in a canvas. So > I think I must need a widget to contain the letters I will type in. I > tried to use a Label, it worked. But, a Label covered part of the > photo underneath, so I can't use it. I saw some software did such a > thing nicely: a box popped on a photo, with dotted lines as borders, > expandable. When such a box was re-sized with a mouse, the font size > in the box also got changed. The box has no background color, so it > does not cover a rectangle area on the photo. I need such a widget, > but I don't know what is this one. I tried Text, Label, but they all > come with Window-like stuff, so they cover some photo content. Can > somebody points me a direction? Thanks! > > > Cosmo I don't have an answer, but it'd be much easier for others if you specified what gui toolkit you're using, and probably also the OS and Python versions. -- DaveA From nukeymusic at gmail.com Wed Dec 14 15:32:52 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Wed, 14 Dec 2011 12:32:52 -0800 (PST) Subject: replacing timestamps in file [newbie] Message-ID: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> I have a file which has the data in the following format: Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 . . the first field is a timestamp, I'd like to replace it with the time in seconds starting from the first one like this: 0 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 27 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 54 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 Is there a function in python to achieve this? thanks in advance nukey From miki.tebeka at gmail.com Wed Dec 14 15:56:01 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 14 Dec 2011 12:56:01 -0800 (PST) Subject: replacing timestamps in file [newbie] In-Reply-To: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> References: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> Message-ID: <22395461.354.1323896161063.JavaMail.geo-discussion-forums@prez15> You can either work with datetime module (see datetime.datetime.strptime) or with the time module (see time.strptime and time.mktime) From tjreedy at udel.edu Wed Dec 14 15:57:40 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Dec 2011 15:57:40 -0500 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <78d2ba1b-0ea2-49a8-88f4-d85186bb2317@f11g2000yql.googlegroups.com> Message-ID: On 12/14/2011 5:09 AM, Eelco wrote: > Arguably, the most elegant thing to do is to define integer division > and remainder as a single operation; It actually is, as quotient and remainder are calculated together. The microprocessors I know of expose this (as does Python). 'a divmod b' puts the quotient in one register and the remainder in another. If you ask for just one of the two values, both are calculated and one is grabbed while the other is returned. > which is not only the logical > thing to do mathematically, but might work really well > programmatically too. > > The semantics of python dont really allow for this though. One could > have: > > d, r = a // b >>> a,b = divmod(10,3) >>> a,b (3, 1) With CPython, int.__divmod__ lightly wraps and exposes the processor operation. > But it wouldnt work that well in composite expressions; selecting the > right tuple index would be messy and a more verbose form would be > preferred. That is why we have >>> a == 10 // 3 True >>> b == 10 % 3 True In both cases, I believe CPython calls int.__divmod__ (or the lower level equivalent) to calculate both values, and one is returned while the other is ignored. It it the same when one does long division by hand. > However, performance-wise its also clearly the best > solution, as one often needs both output arguments and computing them > simultaniously is most efficient. As indicated above, there is really no choice but to calculate both at once. If one needs both a//b and a%b, one should explicitly call divmod once and save (name) both values, instead of calling it implicitly twice and tossing half the answer each time. -- Terry Jan Reedy From rosuav at gmail.com Wed Dec 14 16:07:10 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Dec 2011 08:07:10 +1100 Subject: replacing timestamps in file [newbie] In-Reply-To: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> References: <0d492ee6-bb77-462c-8298-bb969b0e944f@b32g2000yqn.googlegroups.com> Message-ID: On Thu, Dec 15, 2011 at 7:32 AM, nukeymusic wrote: > I have a file which has the data in the following format: > Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > > the first field is a timestamp, I'd like to replace it with the time > in seconds starting from the first one like this: > 27 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 You're looking for several things here, all of which Python can do. I'll give you a few pointers - have a browse of the documentation. 1) Reading a file line-by-line is usually best done with the iterator idiom. Open your file with the "open()" function, read in any headers, and then use "for line in inputfile:" to loop over the data. 2) Parse the line into separate pieces using the split() and join() methods of the string object 3) Use the time.strptime() function to convert the string date into something more usable 4) Maintain a variable with "previous time" and take the difference each time. 5) Write the modified lines to another file. Python's pretty good with this sort of job. It won't let you down! Chris Angelico From einazaki668 at yahoo.com Wed Dec 14 17:20:40 2011 From: einazaki668 at yahoo.com (Eric) Date: Wed, 14 Dec 2011 14:20:40 -0800 (PST) Subject: file data => array(s) Message-ID: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> I'm trying to read some file data into a set of arrays. The file data is just four columns of numbers, like so: 1.2 2.2 3.3 0.5 0.1 0.2 1.0 10.1 ... and so on I'd like to read this into four arrays, one array for each column. Alternatively, I guess something like this is okay too: [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on] I came up with the following for the four array option: file = open(fileName, 'r') for line in file.readlines(): d1, e1, d2, e2 = map(float, line.split()) data1.append(d1) # where data1, err1, data2, err2 are init-ed as empty lists err1.append(e1) data2.append(d2) err2.append(e2) file.close() But somehow it doesn't seem very python-esque (I'm thinking there's a more elegant and succinct way to do it in python). I've also tried replacing the above "map" line with: d = d + map(float, line.split()) # where d is initialized as d = [] But all I get is one long flat list, not what I want. So is the map and append method the best I can do or is there a slicker way? One more thing, no numpy. Nothing against numpy but I'm curious to see what can be done with just the box stock python install. TIA, eric From d at davea.name Wed Dec 14 17:59:25 2011 From: d at davea.name (Dave Angel) Date: Wed, 14 Dec 2011 17:59:25 -0500 Subject: file data => array(s) In-Reply-To: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> Message-ID: <4EE92A4D.3070602@davea.name> On 12/14/2011 05:20 PM, Eric wrote: > I'm trying to read some file data into a set of arrays. The file data > is just four columns of numbers, like so: > > 1.2 2.2 3.3 0.5 > 0.1 0.2 1.0 10.1 > ... and so on > > I'd like to read this into four arrays, one array for each column. > Alternatively, I guess something like this is okay too: > > [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on] > > I came up with the following for the four array option: > > file = open(fileName, 'r') > for line in file.readlines(): The readlines() call is a waste of time/space. file is already an iterator that'll return lines for you. > d1, e1, d2, e2 = map(float, line.split()) > data1.append(d1) # where data1, err1, data2, err2 are init-ed > as empty lists > err1.append(e1) > data2.append(d2) > err2.append(e2) > file.close() > > But somehow it doesn't seem very python-esque (I'm thinking there's a > more elegant and succinct way to do it in python). I've also tried > replacing the above "map" line with: > > d = d + map(float, line.split()) # where d is initialized as d > = [] > > But all I get is one long flat list, not what I want. > > So is the map and append method the best I can do or is there a > slicker way? > > One more thing, no numpy. Nothing against numpy but I'm curious to > see what can be done with just the box stock python install. > > TIA, > eric When I see a problem like this, I turn to zip(). It's got some powerful uses when rows and columns need inverting. I didn't try it on an actual file, but the following works: linedata = [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1] ] data, err1, data2, err2 = zip(*linedata) print data print err1 print data2 print err2 So you could try (untested) file = open(filename, "r") linedata = [ map(float, line) for line in file] data, err1, data2, err2 = zip(*linedata) file.close() Note that your code won't work (and mine probably won't either) if one of the lines has 3 or 5 items. Or if one of the numbers isn't legal format for a float. So you need to think about error checking, or decide whether a partial result is important. -- DaveA From tjreedy at udel.edu Wed Dec 14 18:13:36 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Dec 2011 18:13:36 -0500 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> Message-ID: On 12/14/2011 3:01 AM, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > >> To complement what Eric says below: The with statement is looking for an >> instance *method*, which by definition, is a function attribute of a >> *class* (the class of the context manager) that takes an instance of the >> class as its first parameter. > > I'm not sure that is correct... I don't think that there is anything "by > definition" about where methods live. From the Python glossary: "method: A function which is defined inside a class body." That is actually a bit too narrow, as a function can be added to the class after it is defined. But the point then is that it is treated as if defined inside the class body. > Particularly not in Python where > instance methods can be attributes of the instance itself. This is access, not definition or actual location. The glossary entry go on to say: "If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self)." This does *not* happen if a callable is found in the instance-specific dictionary. An instance method is a function (callable) attribute of a class that gets special treatment when accessed (indirectly) through an instance of that class (or subclass thereof). >>>> class Test(object): > ... def method(self): > ... print("This method is an attribute of the class.") > ... >>>> t = Test() >>>> t.method() > This method is an attribute of the class. The bound method t.method is an instance the class exposed as types.MethodType. In other words, isinstance(t.method, types.MethodType) == True >>>> import types >>>> t.method = types.MethodType( > ... lambda self: print( > ... "This method is an attribute of the instance."), t) Calling any old fruit an apple does not make it one. Calling any old function a method does not make it one. 'types.MethodType' is the exposed name of the class the interpreter uses to create bound methods from a method and an instance of the class containing the method. I believe the interpreter does an isinstance check, but it must do that before calling the class, and not in the bound method constructor itself. In any case, a bound method is not a method. So the printed statement is not true. In this case, the result is not really even a bound method, as the function argument is not a method, so we cannot even ask if the second arg is an instance of the function class container. MethodType is a special case of functools.partial, which was added later. You could have used the latter to the same effect. Or you could have used any old function that printed the same thing. There is no relation between the object passed as the second arg of MethodType and what you do with the resulting callable. Either 't' could be something else. See below. >>>> t.method() > This method is an attribute of the instance. Yes, the callable (which is not a method) is (currently) an attribute of the instance. But that is irrelevant to its operation. t.method is just a callable, in particular, a pseudo bound method, not a method. It is *not* supplying the instance it is called on as the first parameter of the callable. The arguemnt (which is not used) has already been supplied. These produce the same output: class B: pass b = B() b.method = t.method b.method() f = t.method f() t.method = lambda: print("This method is an attribute of the instance.") t.method() > So the normal lookup rules that apply to data attributes, namely > instance, then class, then superclasses, also applies to methods in > Python. When you ask the interpreter to resolve a.x, x is just a supposed attribute, and the interpreter has no idea what class the result should be. > But this doesn't apply for special dunder attributes like __exit__, for > speed reasons. It does not apply to dunder *methods* because they are reserved names defined to be (bound to) methods. So the interpreter knowing that it is looking for a method and that methods have to be attributes of classes, goes directly to the class. -- Terry Jan Reedy From tjreedy at udel.edu Wed Dec 14 18:23:44 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 14 Dec 2011 18:23:44 -0500 Subject: What is this widget? In-Reply-To: References: Message-ID: On 12/14/2011 1:47 PM, Muddy Coder wrote: > Hi Folks, > > I am trying to write letters on a photo that is opened in a canvas. So > I think I must need a widget to contain the letters I will type in. I > tried to use a Label, it worked. But, a Label covered part of the > photo underneath, so I can't use it. You could use a entry widget over or outside the canvas for the user to enter the text, and then close that box and write the entered text to the canvas. Whether you can make the background of the entry box transparent probably depends on the gui toolkit. -- Terry Jan Reedy From einazaki668 at yahoo.com Wed Dec 14 18:25:25 2011 From: einazaki668 at yahoo.com (Eric) Date: Wed, 14 Dec 2011 15:25:25 -0800 (PST) Subject: file data => array(s) References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> Message-ID: On Dec 14, 4:59?pm, Dave Angel wrote: > Note that your code won't work (and mine probably won't either) if one > of the lines has 3 or 5 items. ?Or if one of the numbers isn't legal > format for a float. ?So you need to think about error checking, or > decide whether a partial result is important. > > -- > > DaveA Haven't tried your suggestion yet, I just wanted to comment on this last part real quick. I have the same concern, my plan is to wrap all that stuff up in a "try:" construct. In such cases the program only has to kick out a simple, sensible (for non-programmers) error message and quit. BTW, I didn't say it originally, but this is for 2.7 and hopefully it'll be easy to carry over to 3.2. Thanks, eric From steve+comp.lang.python at pearwood.info Wed Dec 14 18:27:58 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2011 23:27:58 GMT Subject: file data => array(s) References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> Message-ID: <4ee930fd$0$29979$c3e8da3$5496439d@news.astraweb.com> On Wed, 14 Dec 2011 14:20:40 -0800, Eric wrote: > I'm trying to read some file data into a set of arrays. The file data > is just four columns of numbers, like so: > > 1.2 2.2 3.3 0.5 > 0.1 0.2 1.0 10.1 > ... and so on > > I'd like to read this into four arrays, one array for each column. > Alternatively, I guess something like this is okay too: > > [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on] First thing: due to the fundamental nature of binary floating point numbers, if you convert text like "0.1" to a float, you don't get 0.1, you get 0.10000000000000001. That is because 0.1000...01 is the closest possible combination of fractions of 1/2, 1/4, 1/8, ... that adds up to 1/10. If this fact disturbs you, you can import the decimal module and use decimal.Decimal instead; otherwise forget I said anything and continue using float. I will assume you're happy with floats. Assuming the file is small, say, less than 50MB, I'd do it like this: # Version for Python 2.x f = open(filename, 'r') text = f.read() # Grab the whole file at once. numbers = map(float, text.split()) f.close() That gives you a single list [1.2, 2.2, 3.3, 0.5, 0.1, 0.2, ...] which you can now split into groups of four. There are lots of ways to do this. Here's an inefficient way which hopefully will be simple to understand: result = [] while numbers != []: result.append(numbers[0:4]) del numbers[0:4] Here is a much more efficient method which is only a tad harder to understand: result = [] for start in range(0, len(numbers), 4): result.append(numbers[start:start+4]) And just for completeness, here is an advanced technique using itertools: n = len(numbers)//4 numbers = iter(numbers) from itertools import islice result = [list(islice(numbers, 4)) for i in range(n)] Be warned that this version throws away any partial group left over at the end; if you don't want that, change the line defining n to this instead: n = len(numbers)//4 + (len(numbers)%4 > 0) -- Steven From wuwei23 at gmail.com Wed Dec 14 19:45:08 2011 From: wuwei23 at gmail.com (alex23) Date: Wed, 14 Dec 2011 16:45:08 -0800 (PST) Subject: PyCharm, .idea, and source control Message-ID: <8a4e9467-60a9-425c-bbdd-6bd48b7b7bc8@f30g2000pri.googlegroups.com> Hey everyone, I've been using PyCharm for the past month and only just hit an issue that I'm hoping someone else may have some experience with resolving. My problem has to do with PyCharm storing project configuration files in its .idea folder inside the project. This is both a mix of project-level settings and individual customisations. The recommendation is to check this folder into the repo as well - excluding the files of individual settings - but I'm the only developer here using PyCharm and polluting the project with one user's settings would not be received well. More so, even if I _did_ this, I'd still need to be storing my individual settings elsewhere. So what I'm currently doing is having git ignore the .idea folder, and then subsequently turning the folder into its own repo and storing it separately. Ideally I'd just like to be able to have all of the project-config folders in one user-definable location, but I haven't found any way to do that. Has any one else hit this issue, and if so, any tips of resolving it elegantly? From cosmo_general at yahoo.com Wed Dec 14 21:17:00 2011 From: cosmo_general at yahoo.com (Muddy Coder) Date: Wed, 14 Dec 2011 18:17:00 -0800 (PST) Subject: What is this widget? -- again Message-ID: <2766f2ed-8033-4a0e-b29e-1f4797c55af2@u5g2000vbd.googlegroups.com> Hi Folks, Sorry for the unclear question in last post. Well, I am using Tkinter to do GUI, and I just don't know what kind of widget can let me do annotation on an image being displayed. An example is the Paint of Windows: a dotted line box appearing on a image to hold a typed in text. I just can't figure out what widget in Tkinter is equivalent to that dotted line box. Please help, thanks again! Cosmo From cosmo_general at yahoo.com Wed Dec 14 21:26:56 2011 From: cosmo_general at yahoo.com (Muddy Coder) Date: Wed, 14 Dec 2011 18:26:56 -0800 (PST) Subject: How to use a color value returned by colorChooser? Message-ID: Hi Folks, This should be a simple question, but I just can't get an answer from Phython Docs. You see, when we created a widget, and need to tweak the color, we just simply configure it, such as: abutton = Button(root, text='Foo') abutton.config(fg='blue') so we can make the Button color in blue. However, when I choose a color by a color Dialog, askcolor, and then I will get a color value as: (0, 0, 255) ..... So, I want to use this returned color value to configure my existing widgets, with widget.config() syntax. The Python interpreter does not take it. Therefore, I think I must need to covert this returned color value into a symbolic name, such as 'blue', 'red' and so on, then the widget.config() syntax can be used. How can I convert this color value? Please help, thanks folks! Cosmo From rustompmody at gmail.com Wed Dec 14 22:43:21 2011 From: rustompmody at gmail.com (rusi) Date: Wed, 14 Dec 2011 19:43:21 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> <91f6f2ec-25ac-4ca8-b654-f605a7c8a53e@c13g2000vbh.googlegroups.com> Message-ID: <359cdc5e-c955-42fc-a419-3722bae45850@k5g2000pra.googlegroups.com> On Dec 14, 10:15?pm, Eelco wrote: > 'Kindof' off-topic, but what the hell :). We keep having these debates -- so I wonder how off-topic it is... And so do famous CSists: http://research.microsoft.com/en-us/um/people/gurevich/opera/123.pdf : : > > Again, you are completely mis-representing the situation. ?In my > > experience, most mathematicians (I'm not talking about undergraduate > > students here) do not see the axioms are the root of the mathematics > > that they do. ?Formal systems are just one way to explore mathematics. > > ?Of course they can in some cases be very useful and enlightening. > > Its your word versus mine I suppose. Some older discussions: http://groups.google.com/group/comp.lang.python/browse_thread/thread/46435c36f3a13621/896579b757126243?lnk=gst&q=rusi+steven+platonism#896579b757126243 http://groups.google.com/group/comp.lang.python/browse_thread/thread/d36dcd2e2e175d1e/45dd596bc050ac2d?lnk=gst&q=rusi+steven+platonism#45dd596bc050ac2d From python at mrabarnett.plus.com Wed Dec 14 23:10:55 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 04:10:55 +0000 Subject: How to use a color value returned by colorChooser? In-Reply-To: References: Message-ID: <4EE9734F.90109@mrabarnett.plus.com> On 15/12/2011 02:26, Muddy Coder wrote: > Hi Folks, > > This should be a simple question, but I just can't get an answer from > Phython Docs. You see, when we created a widget, and need to tweak the > color, we just simply configure it, such as: > > abutton = Button(root, text='Foo') > abutton.config(fg='blue') > > so we can make the Button color in blue. However, when I choose a > color by a color Dialog, askcolor, and then I will get a color value > as: (0, 0, 255) ..... So, I want to use this returned color value to > configure my existing widgets, with widget.config() syntax. The Python > interpreter does not take it. Therefore, I think I must need to covert > this returned color value into a symbolic name, such as 'blue', 'red' > and so on, then the widget.config() syntax can be used. How can I > convert this color value? Please help, thanks folks! > Look here: http://effbot.org/tkinterbook/tkinter-widget-styling.htm From steve+comp.lang.python at pearwood.info Thu Dec 15 00:01:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 Dec 2011 05:01:21 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4ee97f20$0$11091$c3e8da3@news.astraweb.com> On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: > On 12/14/2011 3:01 AM, Steven D'Aprano wrote: >> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: >> >>> To complement what Eric says below: The with statement is looking for >>> an instance *method*, which by definition, is a function attribute of >>> a *class* (the class of the context manager) that takes an instance of >>> the class as its first parameter. >> >> I'm not sure that is correct... I don't think that there is anything >> "by definition" about where methods live. > > From the Python glossary: > "method: A function which is defined inside a class body." > > That is actually a bit too narrow, as a function can be added to the > class after it is defined. But the point then is that it is treated as > if defined inside the class body. First off, let me preface this by saying that I'm not necessarily saying that the above glossary definition needs to be changed. For most purposes, it is fine, since *nearly always* methods are created as functions defined inside the class body. But it needs to be understood in context as a simplified, hand-wavy definition which covers 99% of the common cases, and not a precise, definitive technical definition. To give an analogy, it is like defining mammals as "hairy animals which give birth to live young", which is correct for all mammals except for monotremes, which are mammals which lay eggs. So I'm happy for the glossary entry to stay as is, because complicating it would confuse the average coder more than it would educate them. But the definition as given is strictly wrong, because it fails to capture the essence of what distinguishes a method from other objects. It mistakes *how* you normally create a method for *what* a method is, a little like defining "a hamburger is a foodstuff you get from McDonalds by giving them money". Here are three ways that the definition fails: (1) You can create a function inside a class, and it remains a function, so long as the class constructor (metaclass) never gets to build a method object from the function. It is easy to do: just hide it inside a wrapper object. >>> class FunctionInsideClass(object): ... def func(x, y): # define a function inside a class ... return x + 2*y ... print(type(func)) # confirm is actually is a function ... attr = (func,) # hide it from the metaclass ... del func ... >>> print(type(FunctionInsideClass.attr[0])) (2) Instead of hiding the function from the metaclass, you can change the metaclass to something which doesn't make methods out of functions. I won't show an example, because it's tricky to get right (or at least *I* find metaclasses tricky). (3) So the definition is too broad: you can have functions defined inside classes that are not methods. But it is also too narrow: you can have methods outside of classes. I'm not talking about bound and unbound methods, but about *creating* the method from scratch outside of a class. When you call the method constructor directly, you can create a method from a function defined outside of a class. >>> def func(self, a): ... return self + a ... >>> type(func) # Definitely a function. >>> obj = types.MethodType(func, 42) >>> obj(23) # Works as expected. 65 >>> type(obj) So there's a method which has never been inside a class, and couldn't even if you tried: int is a built-in immutable type. So what are methods? In Python, methods are wrappers around functions which automatically pass the instance to the inner function object. Under normal circumstances, you create methods by declaring functions inside a class, but that's not the only way to create methods, and it is not the only place they can be found. Note that this definition can easily be adapted to describe class methods and static methods too: class methods are wrappers around functions that automatically pass the class to the inner function, and static methods are wrappers which don't pass any automatic arguments. >> Particularly not in Python where >> instance methods can be attributes of the instance itself. > > This is access, not definition or actual location. Not so. In the example I gave, the method *really is* inside the instance, stored in the instance __dict__ and not the class __dict__. > The glossary entry go > on to say: "If called as an attribute of an instance of that class, the > method will get the instance object as its first argument (which is > usually called self)." This does *not* happen if a callable is found in > the instance-specific dictionary. That's right. Methods are special not because of where they are, but because of what they are. > An instance method is a function > (callable) attribute of a class that gets special treatment when > accessed (indirectly) through an instance of that class (or subclass > thereof). Methods aren't functions at all, not in the isinstance sense. They are functions in the sense that any callable object is a function, i.e. they are callable sub-routines. But they're not functions in the sense of being instances of type "function". They are wrappers around functions. Other languages may do things differently, but that's what Python does. You can even retrieve the function object from the wrapper: >>> obj.__func__ >>>>> class Test(object): >> ... def method(self): >> ... print("This method is an attribute of the class.") ... >>>>> t = Test() >>>>> t.method() >> This method is an attribute of the class. > > The bound method t.method is an instance the class exposed as > types.MethodType. In other words, isinstance(t.method, types.MethodType) > == True > >>>>> import types >>>>> t.method = types.MethodType( >> ... lambda self: print( >> ... "This method is an attribute of the instance."), t) > > Calling any old fruit an apple does not make it one. Calling any old > function a method does not make it one. I'm not *calling* a function a method, I'm *creating* a method from a function object. There is no difference between a method created with types.MethodType and a method automagically created inside a class except for the location where the object is stored. The location is irrelevant. > 'types.MethodType' is the exposed name of the class the interpreter uses > to create bound methods from a method and an instance of the class > containing the method. I believe the interpreter does an isinstance > check, but it must do that before calling the class, and not in the > bound method constructor itself. In any case, a bound method is not a > method. That's an astonishing statement. What is your evidence for this amazing claim that bound methods are not actually methods? What are they then, if not methods? > In this case, the result is not really even a bound method, as the > function argument is not a method, so we cannot even ask if the second > arg is an instance of the function class container. MethodType is a > special case of functools.partial, which was added later. You could have > used the latter to the same effect. Or you could have used any old > function that printed the same thing. Good grief. Is it really your argument that the types.MethodType isn't actually the type of methods, but a fake that lies about returning methods? Well, that's easy enough to test: >>> class K(object): ... def f(self): ... pass ... >>> k = K() >>> type(k.f) is types.MethodType True Unless you are going to accuse me of faking the interpreter output (perhaps I monkey-patched the type built-in?) that is definitive proof that types.MethodType is not fake, it actually is the method type used in classes. > There is no relation between the object passed as the second arg of > MethodType and what you do with the resulting callable. Either 't' could > be something else. See below. > >>>>> t.method() >> This method is an attribute of the instance. > > Yes, the callable (which is not a method) is (currently) an attribute of > the instance. But that is irrelevant to its operation. t.method is just > a callable, in particular, a pseudo bound method, not a method. It is > *not* supplying the instance it is called on as the first parameter of > the callable. Of course it is. It is because I made it to be that way. I encourage you to experiment with Python's introspection tools, perhaps put a few calls to print inside a "pseudo bound method" (your words, there's nothing pseudo about it) and satisfy yourself that the instance passed is the same instance as it is called from *under the circumstances shown*. > The arguemnt (which is not used) has already been > supplied. These produce the same output: > > class B: pass > b = B() > b.method = t.method > b.method() Yes. So what? You can take a bound object and attach it to any other object and see the same results -- this doesn't mean it isn't a bound object. That's how bound objects work! Your objection fails because any method will work the same way: >>> class K: pass ... >>> class Example: ... def foo(self): ... print(self) ... >>> x = Example() >>> k = K() >>> k.attr = x.foo >>> k.attr() <__main__.Example object at 0xb70fcc0c> No trickery with types.MethodType, no metaclass magic, no sleight of hand, just stock standard Python behaviour. -- Steven From python at mrabarnett.plus.com Thu Dec 15 00:15:58 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 05:15:58 +0000 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4ee97f20$0$11091$c3e8da3@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4EE9828E.8010202@mrabarnett.plus.com> On 15/12/2011 05:01, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: > >> On 12/14/2011 3:01 AM, Steven D'Aprano wrote: >>> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: >>> >>>> To complement what Eric says below: The with statement is looking for >>>> an instance *method*, which by definition, is a function attribute of >>>> a *class* (the class of the context manager) that takes an instance of >>>> the class as its first parameter. >>> >>> I'm not sure that is correct... I don't think that there is anything >>> "by definition" about where methods live. >> >> From the Python glossary: >> "method: A function which is defined inside a class body." >> >> That is actually a bit too narrow, as a function can be added to the >> class after it is defined. But the point then is that it is treated as >> if defined inside the class body. > > First off, let me preface this by saying that I'm not necessarily saying > that the above glossary definition needs to be changed. For most > purposes, it is fine, since *nearly always* methods are created as > functions defined inside the class body. But it needs to be understood in > context as a simplified, hand-wavy definition which covers 99% of the > common cases, and not a precise, definitive technical definition. > > To give an analogy, it is like defining mammals as "hairy animals which > give birth to live young", which is correct for all mammals except for > monotremes, which are mammals which lay eggs. > [snip] Or the naked mole-rat. Or cetaceans (whales). From tyler at tysdomain.com Thu Dec 15 02:14:48 2011 From: tyler at tysdomain.com (Littlefield, Tyler) Date: Thu, 15 Dec 2011 00:14:48 -0700 Subject: using twisted as a client-server hybrid Message-ID: <4EE99E68.2060104@tysdomain.com> Hello all: I have a quick question--I am working on a project where a system will connect to me to get commands. The idea is to make the server the "client," used for dispatching commands, so I'm trying to find a way that I can set it up to listen, but poll stdin somehow for input. Is this a possibility? -- Take care, Ty Web: http://tds-solutions.net The Aspen project: a light-weight barebones mud engine http://code.google.com/p/aspenmud Sent from my toaster. From steve+comp.lang.python at pearwood.info Thu Dec 15 02:21:31 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 Dec 2011 07:21:31 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4ee99ffa$0$11091$c3e8da3@news.astraweb.com> On Thu, 15 Dec 2011 05:15:58 +0000, MRAB wrote: > On 15/12/2011 05:01, Steven D'Aprano wrote: >> On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: >> >>> On 12/14/2011 3:01 AM, Steven D'Aprano wrote: >>>> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: >>>> >>>>> To complement what Eric says below: The with statement is looking >>>>> for an instance *method*, which by definition, is a function >>>>> attribute of a *class* (the class of the context manager) that >>>>> takes an instance of the class as its first parameter. >>>> >>>> I'm not sure that is correct... I don't think that there is anything >>>> "by definition" about where methods live. >>> >>> From the Python glossary: >>> "method: A function which is defined inside a class body." >>> >>> That is actually a bit too narrow, as a function can be added to the >>> class after it is defined. But the point then is that it is treated >>> as if defined inside the class body. >> >> First off, let me preface this by saying that I'm not necessarily >> saying that the above glossary definition needs to be changed. For most >> purposes, it is fine, since *nearly always* methods are created as >> functions defined inside the class body. But it needs to be understood >> in context as a simplified, hand-wavy definition which covers 99% of >> the common cases, and not a precise, definitive technical definition. >> >> To give an analogy, it is like defining mammals as "hairy animals which >> give birth to live young", which is correct for all mammals except for >> monotremes, which are mammals which lay eggs. >> > [snip] > Or the naked mole-rat. Or cetaceans (whales). Naked mole-rats and cetaceans do have hair, just not very much of it. E.g. http://marinelife.about.com/od/cetaceans/f/whaleshair.htm -- Steven From rosuav at gmail.com Thu Dec 15 02:34:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Dec 2011 18:34:22 +1100 Subject: using twisted as a client-server hybrid In-Reply-To: <4EE99E68.2060104@tysdomain.com> References: <4EE99E68.2060104@tysdomain.com> Message-ID: On Thu, Dec 15, 2011 at 6:14 PM, Littlefield, Tyler wrote: > Hello all: > I have a quick question--I am working on a project where a system will > connect to me to get commands. The idea is to make the server the "client," > used for dispatching commands, so ?I'm trying to find a way that I can set > it up to listen, but poll stdin somehow for input. Is this a possibility? One easy way to do that is to use separate threads, one to watch stdin (preferably the main thread) and one to listen on the socket or whatever the other end uses. Another option is to use async calls and callbacks. But to answer your simplest question, yes, it most definitely is. ChrisA From steve+comp.lang.python at pearwood.info Thu Dec 15 02:47:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 Dec 2011 07:47:05 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4ee9a5f9$0$11091$c3e8da3@news.astraweb.com> On Thu, 15 Dec 2011 05:01:21 +0000, Steven D'Aprano wrote: >> From the Python glossary: >> "method: A function which is defined inside a class body." >> >> That is actually a bit too narrow, as a function can be added to the >> class after it is defined. But the point then is that it is treated as >> if defined inside the class body. [...] > But the definition as given is strictly wrong, because it fails to > capture the essence of what distinguishes a method from other objects. [...] TL;DR http://users.rcn.com/python/download/Descriptor.htm#functions-and-methods Short and sweet. -- Steven From mohandayalank at gmail.com Thu Dec 15 03:28:33 2011 From: mohandayalank at gmail.com (D. Mohan M. Dayalan) Date: Thu, 15 Dec 2011 00:28:33 -0800 (PST) Subject: combuter is use full message Message-ID: <96723de3-cf3c-446f-8659-833215c6342b@z22g2000prd.googlegroups.com> http;//123maza.com/48/moon670/ From hoogendoorn.eelco at gmail.com Thu Dec 15 04:44:56 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Thu, 15 Dec 2011 01:44:56 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> <91f6f2ec-25ac-4ca8-b654-f605a7c8a53e@c13g2000vbh.googlegroups.com> <359cdc5e-c955-42fc-a419-3722bae45850@k5g2000pra.googlegroups.com> Message-ID: On Dec 15, 4:43?am, rusi wrote: > On Dec 14, 10:15?pm, Eelco wrote: > > > 'Kindof' off-topic, but what the hell :). > > > We keep having these debates -- so I wonder how off-topic it is... > And so do famous CSists:http://research.microsoft.com/en-us/um/people/gurevich/opera/123.pdf > Well, you are right, there are some deep links here. My view of what is wrong with mainstream mathematics is its strange interpretation of the semantics of classical logic. (And I dont think any other schools get it quite right either; I think finitists may avoid the mistakes of others, but are rightfully accussed of being needlessly restrictive, for instance) This is best illustrated by means of the principle of explosion. It rests on assuming a contradiction, and then assigning rather peculiar semantics to them. What is typically left unstated are the semantics of symbol lookup, but apparently it is implicitly understood one can pick whatever value upon encountering a contradicting symbol. There is no well defined rule for the lookup of a twice-defined symbol. Of course the sane thing to do, to a mind grown up around computer languages, upon encountering a twice defined symbol, is not to continue to generate deductions from both branches, but to throw an exception and interrupt the specific line of reasoning that depends on this contradicting symbol right then and there. Conceptually, we can see something is wrong with these undefined semantics right away. A logical system that allows you to draw conclusions as to where the pope shits from assertions about natural numbers could not more obviously be broken. If you dont have this broken way of dealing with contradictions, one does not have to do one of many silly and arbitrary things to make infinity work, such as making a choice between one-to-one correspondence and subset-relations for determining the cardinality of a set; one can simply admit the concept of infinity, while useful, is not consistent, keep the contradiction well handled instead of having it explode in your face (or explode into the field of transfinite analysis; a consequece of 'dealing' with these issues by rejecting the intuitively obviously true relation between subset relations and cardinality), and continue reasoning with the branches of your argument that you are interested in. In other words, what logic needs is a better exception-handling system, which completes the circle with programming languages quite nicely. :) From robert.kern at gmail.com Thu Dec 15 05:47:49 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 15 Dec 2011 10:47:49 +0000 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/14/11 12:32 PM, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 10:56:02 +0200, Jussi Piitulainen wrote: >> I'm not misunderstanding any argument. There was no argument. There was >> a blanket pronouncement that _in mathematics_ mod is not a binary >> operator. I should learn to challenge such pronouncements and ask what >> the problem is. Maybe next time. > > So this was *one* person making that claim? > > I understand that, in general, mathematicians don't have much need for a > remainder function in the same way programmers do -- modulo arithmetic is > far more important. But there's a world of difference between saying "In > mathematics, extracting the remainder is not important enough to be given > a special symbol and treated as an operator" and saying "remainder is not > a binary operator". The first is reasonable; the second is not. The professional mathematicians that I know personally don't say that "remainder is not a binary operator". They *do* say that "modulo is not an operator" in mathematics just because they have reserved that word and the corresponding notation to define the congruence relations. So for example, the following two statements are equivalent: 42 = 2 mod 5 2 = 42 mod 5 The "mod 5" notation modifies the entire equation (or perhaps the = sign if you like to think about it like that), not the term it is immediately next to. Python's % operator is a binary operator that binds to a particular term, not the whole equation. The following two are not equivalent statements: 42 == 2 % 5 2 == 42 % 5 It's mostly kvetching on their part that programming language designers misunderstood the notation and applied the name to something that is confusingly almost, but not quite, the same thing. They aren't saying that you couldn't *define* such an operator; they would just prefer that we didn't abuse the name. But really, it's their fault for using notation that looks like an operator. -- 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 From rustompmody at gmail.com Thu Dec 15 05:56:19 2011 From: rustompmody at gmail.com (rusi) Date: Thu, 15 Dec 2011 02:56:19 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> <91f6f2ec-25ac-4ca8-b654-f605a7c8a53e@c13g2000vbh.googlegroups.com> <359cdc5e-c955-42fc-a419-3722bae45850@k5g2000pra.googlegroups.com> Message-ID: On Dec 15, 2:44?pm, Eelco wrote: > In other words, what logic needs is a better exception-handling > system, which completes the circle with programming languages quite > nicely. :) Cute... but dangerously recursive (if taken literally) Remember that logic is the foundation of programming language semantics. And your idea (suggests) that programming language semantics be made (part of) the foundation of logic. Of course I assume you are not being very literal. Still the dangers of unnoticed circularity are often... well unnoticed :-) eg. McCarthy gave the semantics of lisp in lisp -- a lisp interpreter in lisp is about a page of code. It probably was a decade before someone realized that the same semantics would 'work' for lazy or applicative (eager) order evaluation. This then begs the question what exactly it means for that semantics to 'work'... From rosuav at gmail.com Thu Dec 15 05:58:30 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 15 Dec 2011 21:58:30 +1100 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 15, 2011 at 9:47 PM, Robert Kern wrote: > 42 = 2 mod 5 > ?2 = 42 mod 5 It might make more sense to programmers if you think of it as written: 42 = 2, mod 5 2 = 42, mod 5 ChrisA From y.turgut at gmail.com Thu Dec 15 05:59:04 2011 From: y.turgut at gmail.com (yeet) Date: Thu, 15 Dec 2011 02:59:04 -0800 (PST) Subject: Screen Control Fullscreen ON/OFF Message-ID: Hi all, I would like to make fullscreen white and fullscreen black using Python on Linux. With in the specs of the LCD, I want to be able to display fullscreen white and black approximately at 30Hz. Frequency (on/off per second) will be input manually which is between 1-40Hz. Any idea where to start ? Can Python perform such fast thing ? From hoogendoorn.eelco at gmail.com Thu Dec 15 05:59:07 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Thu, 15 Dec 2011 02:59:07 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 15, 11:47?am, Robert Kern wrote: > On 12/14/11 12:32 PM, Steven D'Aprano wrote: > > > On Wed, 14 Dec 2011 10:56:02 +0200, Jussi Piitulainen wrote: > >> I'm not misunderstanding any argument. There was no argument. There was > >> a blanket pronouncement that _in mathematics_ mod is not a binary > >> operator. I should learn to challenge such pronouncements and ask what > >> the problem is. Maybe next time. > > > So this was *one* person making that claim? > > > I understand that, in general, mathematicians don't have much need for a > > remainder function in the same way programmers do -- modulo arithmetic is > > far more important. But there's a world of difference between saying "In > > mathematics, extracting the remainder is not important enough to be given > > a special symbol and treated as an operator" and saying "remainder is not > > a binary operator". The first is reasonable; the second is not. > > The professional mathematicians that I know personally don't say that "remainder > is not a binary operator". They *do* say that "modulo is not an operator" in > mathematics just because they have reserved that word and the corresponding > notation to define the congruence relations. So for example, the following two > statements are equivalent: > > ? ?42 = 2 mod 5 > ? ?2 = 42 mod 5 > > The "mod 5" notation modifies the entire equation (or perhaps the = sign if you > like to think about it like that), not the term it is immediately next to. > Python's % operator is a binary operator that binds to a particular term, not > the whole equation. The following two are not equivalent statements: > > ? ?42 == 2 % 5 > ? ?2 == 42 % 5 > > It's mostly kvetching on their part that programming language designers > misunderstood the notation and applied the name to something that is confusingly > almost, but not quite, the same thing. They aren't saying that you couldn't > *define* such an operator; they would just prefer that we didn't abuse the name. > But really, it's their fault for using notation that looks like an operator. > > -- > 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 Thanks Robert, I think you cut right through the confusion there. To tie it back in with python language design; all the more reason not to opt for pseudo-backwards compatibility. If python wants a remainder function, call it 'remainder'. Not 'rem', not 'mod', and certainly not '%'. Its the more pythonic way; a self-describing name, rather than poorly defined or poorly understood cryptology. From rustompmody at gmail.com Thu Dec 15 06:04:40 2011 From: rustompmody at gmail.com (rusi) Date: Thu, 15 Dec 2011 03:04:40 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4f2f6742-b1b2-4605-8004-b6aa431b8513@k5g2000pra.googlegroups.com> On Dec 15, 3:58?pm, Chris Angelico wrote: > On Thu, Dec 15, 2011 at 9:47 PM, Robert Kern wrote: > > ?42 = 2 mod 5 > > ?2 = 42 mod 5 > > It might make more sense to programmers if you think of it as written: > > 42 = 2, mod 5 > 2 = 42, mod 5 > > ChrisA For the record I should say that the guy who taught me abstract algebra, said about as much: He said that the notation a == b mod n should be written as a ==n b (read the == as 3 horizontal lines and the n as a subscript) From y.turgut at gmail.com Thu Dec 15 06:12:10 2011 From: y.turgut at gmail.com (yeet) Date: Thu, 15 Dec 2011 03:12:10 -0800 (PST) Subject: Screen Control Fullscreen ON/OFF References: Message-ID: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> On Dec 15, 12:59?pm, yeet wrote: > Hi all, > > I would like to make fullscreen white and fullscreen black using > Python on Linux. With in the specs of the LCD, I want to be able to > display fullscreen white and black approximately at 30Hz. Frequency > (on/off per second) will be input manually which is between 1-40Hz. > Any idea where to start ? Can Python perform such fast thing ? Forgot to mention and sorry for the pollution ; My LCD has 2ms respond time thus it can handle a maximum of 50Hz ON/ OFF (white/black) thus seems to fit my 1-40Hz range. I am not asking or looking for a fully working code, I just need some directions. From hoogendoorn.eelco at gmail.com Thu Dec 15 06:37:35 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Thu, 15 Dec 2011 03:37:35 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <98b76f0a-41bc-4864-8d9f-94d9a3a1d7b1@b32g2000yqn.googlegroups.com> <91f6f2ec-25ac-4ca8-b654-f605a7c8a53e@c13g2000vbh.googlegroups.com> <359cdc5e-c955-42fc-a419-3722bae45850@k5g2000pra.googlegroups.com> Message-ID: On Dec 15, 11:56?am, rusi wrote: > On Dec 15, 2:44?pm, Eelco wrote: > > > In other words, what logic needs is a better exception-handling > > system, which completes the circle with programming languages quite > > nicely. :) > > Cute... but dangerously recursive (if taken literally) > Remember that logic is the foundation of programming language > semantics. > And your idea (suggests) that programming language semantics be made > (part of) the foundation of logic. > > Of course I assume you are not being very literal. > Still the dangers of unnoticed circularity are often... well > unnoticed :-) Well, logic as a language has semantics, one way or the other. This circularity is a general theme in epistemology, and one that fits well with the view of deduction-induction as a closed loop cycle. Knowledge does not flow from axioms to theorems; axioms without an encompassing context are meaningless symbols. Its a body of knowledge as a whole that should be put to the test; the language and the things we express in it are inseperable. (the not-quite-famous-enough Quine in a nutshell) The thing is that our semantics of logic are quite primitive; cooked up in a time where people spent far less time thinking about these things, and having a far narrower base of experience to draw ideas from. They didnt have the luxury of already having grown up studying a dozen formal languages before embarking on creating their own. It other words, the semantics of logic is a legacy piece of crap, but an insanely firmly entrenched one. I mean, there are many sensible ways of defining semantics of conflicting symbols, but you'll find on studying these things that the guys who (often implicitly) laid down these rules didnt even seemed to have consciously thought about them. Not because they were stupid; far from it, but for similar reasons as to why the x86 architecture wasnt concieved of the day after the invention of the transistor. From nizamov.shawkat at gmail.com Thu Dec 15 06:44:46 2011 From: nizamov.shawkat at gmail.com (Nizamov Shawkat) Date: Thu, 15 Dec 2011 12:44:46 +0100 Subject: Screen Control Fullscreen ON/OFF In-Reply-To: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> References: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> Message-ID: >> >> I would like to make fullscreen white and fullscreen black using >> Python on Linux. With in the specs of the LCD, I want to be able to >> display fullscreen white and black approximately at 30Hz. Frequency >> (on/off per second) will be input manually which is between 1-40Hz. >> Any idea where to start ? Can Python perform such fast thing ? > > Forgot to mention and sorry for the pollution ; > > My LCD has 2ms respond time thus it can handle a maximum of 50Hz ON/ > OFF (white/black) thus seems to fit my 1-40Hz range. I am not asking > or looking for a fully working code, I just need some directions. It depends on whether you want sync to vblank or not. If not, that is pretty easy - use sleep() or something similar. If you have to use sync (screen is always either black or white, never partly black and white) then it is much much more difficult. Actually I do not know of any way to sync to it. Once upon a time I had a P-3/500 linux box with homemade python program that would wait for RTC interrupts (which was set to 8192 Hz frequency) and performed different combinations of ADC/DAC/Digital-IO at required timesteps . It worked really very well and with submillisecond resolution. Hope it helps, S.Nizamov From nizamov.shawkat at gmail.com Thu Dec 15 07:18:10 2011 From: nizamov.shawkat at gmail.com (Nizamov Shawkat) Date: Thu, 15 Dec 2011 13:18:10 +0100 Subject: Screen Control Fullscreen ON/OFF In-Reply-To: References: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> Message-ID: > It depends on whether you want sync to vblank or not. If not, that is > pretty easy - use sleep() or something similar. If you have to use > sync (screen is always either black or white, never partly black and > white) then it is much much more difficult. Actually I do not know of > any way to sync to it. But you do not have to sync to vblank anyway. So you can turn on vblank sync for the videocard and then you will have either completely black or completely white screen at each single time point, but this will be delayed in regard to what you set in python. Hope this helps, S.Nizamov From jpiitula at ling.helsinki.fi Thu Dec 15 07:48:06 2011 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 15 Dec 2011 14:48:06 +0200 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4f2f6742-b1b2-4605-8004-b6aa431b8513@k5g2000pra.googlegroups.com> Message-ID: rusi writes: > On Dec 15, 3:58?pm, Chris Angelico wrote: > > On Thu, Dec 15, 2011 at 9:47 PM, Robert Kern wrote: > > > ?42 = 2 mod 5 > > > ?2 = 42 mod 5 > > > > It might make more sense to programmers if you think of it as > > written: > > > > 42 = 2, mod 5 > > 2 = 42, mod 5 > > > > ChrisA > > For the record I should say that the guy who taught me abstract > algebra, said about as much: > He said that the notation > a == b mod n > should be written as > a ==n b > (read the == as 3 horizontal lines and the n as a subscript) I think the modulus is usually given in parentheses and preferably some whitespace: in text, a == b (mod n), using == for the triple -, and in a display: a == b (mod n). I think even a == b == c (mod n), without repeating the modulus every time. (A subscript sounds good if the modulus is simple. Perhaps it often is.) That way it does not even look like a binary operator. I think Graham, Knuth, and Patashnik play it nicely in their book Concrete Mathematics, where they have both mods: the congruence relation, and the binary operator. The book is targeted for computer scientists. As if mathematicians didn't use the exact same notations for different purposes, even in the same context, and often with no problems whatsoever as long as all parties happen to know what they are talking about. Often the uses are analogous, but at least the two main uses of (x,y) differ wildly. (So Knuth uses (x .. y) for the interval, but he is a programmer.) From showell30 at yahoo.com Thu Dec 15 08:35:55 2011 From: showell30 at yahoo.com (Steve Howell) Date: Thu, 15 Dec 2011 05:35:55 -0800 (PST) Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Dec 14, 9:01?pm, Steven D'Aprano wrote: > [...] > So what are methods? In Python, methods are wrappers around functions > which automatically pass the instance to the inner function object. Under > normal circumstances, you create methods by declaring functions inside a > class, but that's not the only way to create methods, and it is not the > only place they can be found. > I've always understood methods as basically function wrappers that pass in the instance, so it's good to hear somebody else formulate it that way. For the special methods like __enter__ and __exit__, the tricky part isn't understanding what would happen once the methods were called; the tricky part is getting them to be called in the first place, if they were not declared inside the class or attached to the class. import types class Blank: pass foo = Blank() foo.name = "foo1" foo.__exit__ = types.MethodType(lambda self, *args: print(self.name), foo) foo.__exit__() # works like a method in python3, prints foo1 with foo: pass From ulrich.eckhardt at dominolaser.com Thu Dec 15 09:19:13 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Thu, 15 Dec 2011 15:19:13 +0100 Subject: Screen Control Fullscreen ON/OFF In-Reply-To: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> References: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> Message-ID: <28cpr8-rt.ln1@satorlaser.homedns.org> Am 15.12.2011 12:12, schrieb yeet: > My LCD has 2ms respond time thus it can handle a maximum of 50Hz ON/ > OFF (white/black) thus seems to fit my 1-40Hz range. You might want to ask Santa for a new calculator, as in my book a response time of 2ms would be enough for 250Hz (period = 2 * 2ms). Reminds me of a hack that used a special pattern on a CRT to emit DCF77 signals, reprogramming any suitable radio-controlled clock in range. What are you trying to do, just out of curiosity? (c: Uli From nathan.alexander.rice at gmail.com Thu Dec 15 09:34:29 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Thu, 15 Dec 2011 09:34:29 -0500 Subject: Odd behavior of object equality/identity in the context of relative vs fully qualified imports Message-ID: I just ran into this yesterday, and I am curious if there is a rational behind it... I have a class that uses a dictionary to dispatch from other classes (k) to functions for those classes (v). I recently ran into a bug where the dictionary would report that a class which was clearly in the dictionary's keys was giving a KeyError. id() produced two distinct values, which I found to be curious, and issubclass/isinstance tests also failed. When I inspected the two classes, I found that the only difference between the two was the __module__ variable, which in one case had a name relative to the current module (foo), and in another case had the fully qualified name (bar.foo). When I went ahead and changed the import statement for the module to import bar.foo rather than import foo, everything worked as expected. My first thought was that I had another foo module in an old version of the bar package somewhere on my pythonpath; After a thorough search this proved not to be the case. Has anyone else run into this? Is this intended behavior? If so, why? Nathan From dihedral88888 at googlemail.com Thu Dec 15 09:40:57 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 15 Dec 2011 06:40:57 -0800 (PST) Subject: Screen Control Fullscreen ON/OFF In-Reply-To: References: Message-ID: <20668600.306.1323960057492.JavaMail.geo-discussion-forums@pruu23> On Thursday, December 15, 2011 6:59:04 PM UTC+8, yeet wrote: > Hi all, > > I would like to make fullscreen white and fullscreen black using > Python on Linux. With in the specs of the LCD, I want to be able to > display fullscreen white and black approximately at 30Hz. Frequency > (on/off per second) will be input manually which is between 1-40Hz. > Any idea where to start ? Can Python perform such fast thing ? Check pygame, pyimg and sdllib and wxpython. Python is a high level language that was not bound to hardware. One of the major strengths of Python is very easy to be supported by programs in other languages, especially in C, Pascal and Fortran. Also programs in Python are easy to debug and maintain. From alec.taylor6 at gmail.com Thu Dec 15 09:43:21 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 16 Dec 2011 01:43:21 +1100 Subject: =?windows-1252?Q?Re=3A_ERP_with_Supply_chain_management_=28incl=2E_POS=29_and?= =?windows-1252?Q?_Customer_relationship_management_=97_What=27s_available=3F?= In-Reply-To: References: Message-ID: On Thu, Dec 15, 2011 at 5:54 AM, Anurag Chourasia wrote: > Hi Alec, > > I am building a POS/CRM (Loyalty Management) system as well. > > So far, the best I could find to use as a base is this one > > https://github.com/rosarior/django-inventory > > In any case, I felt that even starting from scratch is not a bad > idea.....Please let me know if you get more pointers. We can even work on > this one together. > > Regards, > Anurag > Thanks Anurag, I will look into it, however I'm thinking of writing my whole project as extensions to satchmo. (modularised though, so you'd be able to enable only a few features, and even disable ecommerce) Would be great to have you on board (if you're still interested, contact me off-list) From d at davea.name Thu Dec 15 10:08:51 2011 From: d at davea.name (Dave Angel) Date: Thu, 15 Dec 2011 10:08:51 -0500 Subject: Odd behavior of object equality/identity in the context of relative vs fully qualified imports In-Reply-To: References: Message-ID: <4EEA0D83.6070807@davea.name> On 12/15/2011 09:34 AM, Nathan Rice wrote: > I just ran into this yesterday, and I am curious if there is a > rational behind it... > > I have a class that uses a dictionary to dispatch from other classes > (k) to functions for those classes (v). I recently ran into a bug > where the dictionary would report that a class which was clearly in > the dictionary's keys was giving a KeyError. id() produced two > distinct values, which I found to be curious, and > issubclass/isinstance tests also failed. When I inspected the two > classes, I found that the only difference between the two was the > __module__ variable, which in one case had a name relative to the > current module (foo), and in another case had the fully qualified name > (bar.foo). When I went ahead and changed the import statement for the > module to import bar.foo rather than import foo, everything worked as > expected. My first thought was that I had another foo module in an > old version of the bar package somewhere on my pythonpath; After a > thorough search this proved not to be the case. > > Has anyone else run into this? Is this intended behavior? If so, why? > > Nathan Hard to tell with such generic information. But I'm guessing you imported your script from some other module, creating a circular import sequence. The circular can be a problem in its own right. But even worse, if the script is part of the chain is that it's loaded twice, with different names. And any top-level items, such as classes, will be instantiated twice as well. is your script called foo.py by any chance? -- DaveA From rantingrickjohnson at gmail.com Thu Dec 15 10:33:49 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Thu, 15 Dec 2011 07:33:49 -0800 (PST) Subject: What is this widget? References: Message-ID: <0c31c34c-e446-420e-8aa8-f5313612463f@g41g2000yqa.googlegroups.com> On Dec 14, 12:47?pm, Muddy Coder wrote: > Hi Folks, > > I am trying to write letters on a photo that is opened in a canvas. So > I think I must need a widget to contain the letters I will type in. I > tried to use a Label, it worked. But, a Label covered part of the > photo underneath, so I can't use it. I saw some software did such a > thing nicely: a box popped on a photo, with dotted lines as borders, > expandable. When such a box was re-sized with a mouse, the font size > in the box also got changed. The box has no background color, so it > does not cover a rectangle area on the photo. I need such a widget, > but I don't know what is this one. I tried Text, Label, but they all > come with Window-like stuff, so they cover some photo content. Can > somebody points me a direction? Thanks! Listen Muddy, this is not first time that you've come here seeking advice with a cryptic and ambiguous post. People have been very patient and even asked you to be more explicit with your questions. I highly advise you to re-write your original post and include which GUI library you are using and any other pertinent info. My guess is that you are using Tkinter. If so, i believe the canvas has a method called "create_text" which will do what you want. To make the input easier to code, use a Tkinter Entry widget stuffed into a dialog -- tkSimpleDialog makes this, well, simple. PS: Clean up you act Muddy! From passiday at gmail.com Thu Dec 15 10:49:57 2011 From: passiday at gmail.com (Passiday) Date: Thu, 15 Dec 2011 07:49:57 -0800 (PST) Subject: Can't get Apache2 tp process py (Ubuntu 11.10) Message-ID: <1544851.511.1323964197163.JavaMail.geo-discussion-forums@vbxw7> Hello, Sorry if this is not exactly appropriate forum where to ask Apache question, but I though't here would some Apache-experienced people probably hang out. Well, I can't get my Apache2 to process Python *.py files. I checked numerous tutorials how to enable it, and did the install steps: - Installed the libapache2-mod-python package - Added the following lines to the section of /etc/apache2/sites-available/default file: AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On I made this simple test.py file: def index(req): return "Test successful"; When I request the file, ie, [url]http://localhost/test.py[/url], I get the download prompt, and the Python script is just downloaded, not processed. The error log /var/log/apache2 shows these lines that kind of confirm that the module is loaded: [Thu Dec 15 15:12:38 2011] [notice] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads. [Thu Dec 15 15:12:38 2011] [notice] mod_python: using mutex_directory /tmp [Thu Dec 15 15:12:38 2011] [notice] Apache/2.2.20 (Ubuntu) PHP/5.3.6-13ubuntu3.3 with Suhosin-Patch mod_python/3.3.1 Python/2.7.2+ configured -- resuming normal operations As one can see, there are no any errors that would tell there's something wrong happening. Any ideas what installation step did I do wrong or skipped? From rantingrickjohnson at gmail.com Thu Dec 15 10:53:55 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Thu, 15 Dec 2011 07:53:55 -0800 (PST) Subject: What is this widget? -- again References: <2766f2ed-8033-4a0e-b29e-1f4797c55af2@u5g2000vbd.googlegroups.com> Message-ID: <587e3547-11b1-477f-8e9e-aa3caf60dfa5@c18g2000yqj.googlegroups.com> On Dec 14, 8:17?pm, Muddy Coder wrote: > Hi Folks, > > Sorry for the unclear question in last post. Well, I am using Tkinter > to do GUI, and I just don't know what kind of widget can let me do > annotation on an image being displayed. An example is the Paint of > Windows: a dotted line box appearing on a image to hold a typed in > text. I just can't figure out what widget in Tkinter is equivalent to > that dotted line box. Please help, thanks again! > > Cosmo No such widget exists in Tkinter. As Dennis said the code must be written. I can assure you that this functionality can be created with Tkinter (because i've done it myself) however it requires an in-depth knowledge of Tkinter -- which apparently you don't have since you are asking the question. Waring: NEVER USE ANY MICROSOFT INTERFACE AS INSPIRATION!!! Whist you ponder the intricacies professional of image annotation, i suggest you follow my advice in your OTHER post (of the same title) and use the "canvas.create_text" method to draw text on top of images THEN you need to learn how to move "canvas items" around the canvas using the mouse THEN you need to intercept right clicks on the text item and show a dialog with an entry widget stuffed inside so the user can edit the text THEN update the canvas text from user input. When solving a problem you need to follow a linear path. The first step in this path is to draw text on a canvas. 1. Draw text to a canvas. 2. Intercept right click events on a "canvas text". 3. Build a custom dialog using tkSimpleDialog (or use a floating "canvas window") to display a Tkinter Entry widget so the user can edit the current value of the "canvas text". 4. Fetch the value of Tkinter "canvas text" 5. Load the value into the entry widget. 6. Update the value of the canvas text item based on user input returned from the dialog. Extra Credit: * capture mouse drag events and allow user manipulation of "canvas text" x-y position. http://infohost.nmt.edu/tcc/help/pubs/tkinter/ http://effbot.org/tkinterbook/ ...may the source be with you. From dihedral88888 at googlemail.com Thu Dec 15 11:32:28 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Thu, 15 Dec 2011 08:32:28 -0800 (PST) Subject: What is this widget? -- again In-Reply-To: <587e3547-11b1-477f-8e9e-aa3caf60dfa5@c18g2000yqj.googlegroups.com> References: <2766f2ed-8033-4a0e-b29e-1f4797c55af2@u5g2000vbd.googlegroups.com> <587e3547-11b1-477f-8e9e-aa3caf60dfa5@c18g2000yqj.googlegroups.com> Message-ID: <17035503.315.1323966748123.JavaMail.geo-discussion-forums@prix23> On Thursday, December 15, 2011 11:53:55 PM UTC+8, Rick Johnson wrote: > On Dec 14, 8:17?pm, Muddy Coder wrote: > > Hi Folks, > > > > Sorry for the unclear question in last post. Well, I am using Tkinter > > to do GUI, and I just don't know what kind of widget can let me do > > annotation on an image being displayed. An example is the Paint of > > Windows: a dotted line box appearing on a image to hold a typed in > > text. I just can't figure out what widget in Tkinter is equivalent to > > that dotted line box. Please help, thanks again! > > > > Cosmo > > No such widget exists in Tkinter. As Dennis said the code must be > written. I can assure you that this functionality can be created with > Tkinter (because i've done it myself) however it requires an in-depth > knowledge of Tkinter -- which apparently you don't have since you are > asking the question. > > Waring: NEVER USE ANY MICROSOFT INTERFACE AS INSPIRATION!!! > > Whist you ponder the intricacies professional of image annotation, i > suggest you follow my advice in your OTHER post (of the same title) > and use the "canvas.create_text" method to draw text on top of images > THEN you need to learn how to move "canvas items" around the canvas > using the mouse THEN you need to intercept right clicks on the text > item and show a dialog with an entry widget stuffed inside so the user > can edit the text THEN update the canvas text from user input. When > solving a problem you need to follow a linear path. The first step in > this path is to draw text on a canvas. > > 1. Draw text to a canvas. > 2. Intercept right click events on a "canvas text". > 3. Build a custom dialog using tkSimpleDialog (or use a floating > "canvas window") to display a Tkinter Entry widget so the user can > edit the current value of the "canvas text". > 4. Fetch the value of Tkinter "canvas text" > 5. Load the value into the entry widget. > 6. Update the value of the canvas text item based on user input > returned from the dialog. > > Extra Credit: > * capture mouse drag events and allow user manipulation of "canvas > text" x-y position. > > http://infohost.nmt.edu/tcc/help/pubs/tkinter/ > http://effbot.org/tkinterbook/ > > ...may the source be with you. Check BOA and wxpython and work out the examples. It is kind of boring and not too easy to be accustomed to use. A simpler set of the toolkit pygame and pyimg to explore is another way to advance. Study the source of pyimg is very helpful. From jenn.duerr at gmail.com Thu Dec 15 11:36:37 2011 From: jenn.duerr at gmail.com (noydb) Date: Thu, 15 Dec 2011 08:36:37 -0800 (PST) Subject: test for list equality Message-ID: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> I want to test for equality between two lists. For example, if I have two lists that are equal in content but not in order, I want a return of 'equal' -- dont care if they are not in the same order. In order to get that equality, would I have to sort both lists regardless? if yes, how (having issues with list.sort)? Another way i tried, that I think is kind-of roundabout is like x = [2, 5, 1, 88, 9] y = [5, 2, 9, 1, 88] inBoth = list(set(x) & set(y)) and then test that list.count is equal between inBoth and x and/or y. Any better suggestions? Thanks for any help! From dmitrey15 at gmail.com Thu Dec 15 11:45:31 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Thu, 15 Dec 2011 08:45:31 -0800 (PST) Subject: Ann: OpenOpt and FuncDesigner 0.37 Message-ID: <73f2f0e5-761a-45c1-a4dc-245e7088f53d@g41g2000yqa.googlegroups.com> Hi all, I'm glad to inform you about new release 0.37 (2011-Dec-15) of our free software: OpenOpt (numerical optimization): IPOPT initialization time gap (time till first iteration) for FuncDesigner models has been decreased Some improvements and bugfixes for interalg, especially for "search all SNLE solutions" mode (Systems of Non Linear Equations) Eigenvalue problems (EIG) (in both OpenOpt and FuncDesigner) Equality constraints for GLP (global) solver de Some changes for goldenSection ftol stop criterion GUI func "manage" - now button "Enough" works in Python3, but "Run/ Pause" not yet (probably something with threading and it will be fixed in Python instead) FuncDesigner: Major sparse Automatic differentiation improvements for badly- vectorized or unvectorized problems with lots of constraints (except of box bounds); some problems now work many times or orders faster (of course not faster than vectorized problems with insufficient number of variable arrays). It is recommended to retest your large-scale problems with useSparse = 'auto' | True| False Two new methods for splines to check their quality: plot and residual Solving ODE dy/dt = f(t) with specifiable accuracy by interalg Speedup for solving 1-dimensional IP by interalg SpaceFuncs and DerApproximator: Some code cleanup You may trace OpenOpt development information in our recently created entries in Twitter and Facebook, see http://openopt.org for details. For more information visit http://openopt.org Regards, D. From roy at panix.com Thu Dec 15 11:48:23 2011 From: roy at panix.com (Roy Smith) Date: Thu, 15 Dec 2011 08:48:23 -0800 (PST) Subject: How to generate "a, b, c, and d"? Message-ID: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some standard way to do this, handling all the special cases? [] ==> '' ['a'] ==> 'a' ['a', 'b'] ==> 'a and b' ['a', 'b', 'c', 'd'] ==> 'a, b, and c' It seems like the kind of thing django.contrib.humanize would handle, but alas, it doesn't. From y.turgut at gmail.com Thu Dec 15 11:57:13 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Thu, 15 Dec 2011 08:57:13 -0800 (PST) Subject: Screen Control Fullscreen ON/OFF References: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> <28cpr8-rt.ln1@satorlaser.homedns.org> Message-ID: On Dec 15, 4:19?pm, Ulrich Eckhardt wrote: > Am 15.12.2011 12:12, schrieb yeet: > > > My LCD has 2ms respond time thus it can handle a maximum of 50Hz ON/ > > OFF (white/black) thus seems to fit my 1-40Hz range. > > You might want to ask Santa for a new calculator, as in my book a > response time of 2ms would be enough for 250Hz (period = 2 * 2ms). > > Reminds me of a hack that used a special pattern on a CRT to emit DCF77 > signals, reprogramming any suitable radio-controlled clock in range. > What are you trying to do, just out of curiosity? > > (c: > > Uli Yes that's correct, 50Hz limit is the limit of NVIDIA CUDA Linux drivers limit. Screen can go higher rates on sucky windows. I am trying to generate a visual stimulus that will be used for fNIR and EEG captures. On Dec 15, 2:18 pm, Nizamov Shawkat wrote: > > It depends on whether you want sync to vblank or not. If not, that is > > pretty easy - use sleep() or something similar. If you have to use > > sync (screen is always either black or white, never partly black and > > white) then it is much much more difficult. Actually I do not know of > > any way to sync to it. > > But you do not have to sync to vblank anyway. So you can turn on > vblank sync for the videocard and then you will have either completely > black or completely white screen at each single time point, but this > will be delayed in regard to what you set in python. > > Hope this helps, > S.Nizamov It's not easy to do this basing on time, I think doing per frames is a much better option. It's sounded like a very simple task at first but I realize it's not that easy.Maybe I should write the screen blinker in C or Assembly then call it from Python. From nathan.alexander.rice at gmail.com Thu Dec 15 12:11:45 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Thu, 15 Dec 2011 12:11:45 -0500 Subject: Odd behavior of object equality/identity in the context of relative vs fully qualified imports In-Reply-To: <4EEA0D83.6070807@davea.name> References: <4EEA0D83.6070807@davea.name> Message-ID: It would be more work than I want to go into to provide full context (unless it is to file a bug report, if it actually is a bug). I verified that there are no cyclical dependency issues using snakefood, and I doublechecked that just changing the import from full to partial name is sufficient to reintroduce the bug. Can I get confirmation that this is not expected behavior? I will go ahead and file a bug report it that is the case. Nathan On Thu, Dec 15, 2011 at 10:08 AM, Dave Angel wrote: > On 12/15/2011 09:34 AM, Nathan Rice wrote: >> >> I just ran into this yesterday, and I am curious if there is a >> rational behind it... >> >> I have a class that uses a dictionary to dispatch from other classes >> (k) to functions for those classes (v). ?I recently ran into a bug >> where the dictionary would report that a class which was clearly in >> the dictionary's keys was giving a KeyError. ?id() produced two >> distinct values, which I found to be curious, and >> issubclass/isinstance tests also failed. ?When I inspected the two >> classes, I found that the only difference between the two was the >> __module__ variable, which in one case had a name relative to the >> current module (foo), and in another case had the fully qualified name >> (bar.foo). ?When I went ahead and changed the import statement for the >> module to import bar.foo rather than import foo, everything worked as >> expected. ?My first thought was that I had another foo module in an >> old version of the bar package somewhere on my pythonpath; ?After a >> thorough search this proved not to be the case. >> >> Has anyone else run into this? ?Is this intended behavior? ?If so, why? >> >> Nathan > > Hard to tell with such generic information. ?But I'm guessing you imported > your script from some other module, creating a circular import sequence. > ?The circular can be a problem in its own right. ?But even worse, if the > script is part of the chain is that it's loaded twice, with different names. > ?And any top-level items, such as classes, will be instantiated twice as > well. ?is your script called foo.py by any chance? > > -- > > DaveA > From python at mrabarnett.plus.com Thu Dec 15 12:27:14 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 17:27:14 +0000 Subject: How to generate "a, b, c, and d"? In-Reply-To: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> Message-ID: <4EEA2DF2.9080107@mrabarnett.plus.com> On 15/12/2011 16:48, Roy Smith wrote: > I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some standard way to do this, handling all the special cases? > > [] ==> '' > ['a'] ==> 'a' > ['a', 'b'] ==> 'a and b' > ['a', 'b', 'c', 'd'] ==> 'a, b, and c' > > It seems like the kind of thing django.contrib.humanize would handle, but alas, it doesn't. How about this: def and_list(items): if len(items) <= 2: return " and ".join(items) return ", ".join(items[ : -1]) + ", and " + items[-1] print(and_list([])) print(and_list(['a'])) print(and_list(['a', 'b'])) print(and_list(['a', 'b', 'c'])) print(and_list(['a', 'b', 'c', 'd'])) From jenn.duerr at gmail.com Thu Dec 15 12:49:27 2011 From: jenn.duerr at gmail.com (noydb) Date: Thu, 15 Dec 2011 09:49:27 -0800 (PST) Subject: test for list equality References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> Message-ID: <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> On Dec 15, 11:36?am, noydb wrote: > I want to test for equality between two lists. ?For example, if I have > two lists that are equal in content but not in order, I want a return > of 'equal' -- dont care if they are not in the same order. ?In order > to get that equality, would I have to sort both lists regardless? ?if > yes, how (having issues with list.sort)? > > Another way i tried, that I think is kind-of roundabout is like > x = [2, 5, 1, 88, 9] > y = [5, 2, 9, 1, 88] > inBoth = list(set(x) & set(y)) > > and then test that list.count is equal between inBoth and x and/or y. > > Any better suggestions? > > Thanks for any help! My sort issue... as in this doesn't work >>> if x.sort == y.sort: ... print 'equal' ... else: ... print 'not equal' ... not equal ??? From python at mrabarnett.plus.com Thu Dec 15 12:54:21 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 17:54:21 +0000 Subject: test for list equality In-Reply-To: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> Message-ID: <4EEA344D.1050601@mrabarnett.plus.com> On 15/12/2011 16:36, noydb wrote: > I want to test for equality between two lists. For example, if I have > two lists that are equal in content but not in order, I want a return > of 'equal' -- dont care if they are not in the same order. In order > to get that equality, would I have to sort both lists regardless? if > yes, how (having issues with list.sort)? > > Another way i tried, that I think is kind-of roundabout is like > x = [2, 5, 1, 88, 9] > y = [5, 2, 9, 1, 88] > inBoth = list(set(x)& set(y)) > > and then test that list.count is equal between inBoth and x and/or y. > > > Any better suggestions? > > Thanks for any help! You could count the number of times each item occurs using the Counter class in the collections module: >>> x = [2, 5, 1, 88, 9] >>> y = [5, 2, 9, 1, 88] >>> from collections import Counter >>> cx = Counter(x) >>> cy = Counter(y) >>> cx Counter({88: 1, 1: 1, 2: 1, 5: 1, 9: 1}) >>> cx == cy True From gordon at panix.com Thu Dec 15 12:57:02 2011 From: gordon at panix.com (John Gordon) Date: Thu, 15 Dec 2011 17:57:02 +0000 (UTC) Subject: test for list equality References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: In <61edc02c-4f86-45ef-82a1-61c7013003b4 at t38g2000yqe.googlegroups.com> noydb writes: > My sort issue... as in this doesn't work > >>> if x.sort =3D=3D y.sort: > ... print 'equal' > ... else: > ... print 'not equal' > ... > not equal > ??? Use x.sort() instead of x.sort . -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From miki.tebeka at gmail.com Thu Dec 15 12:59:33 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 15 Dec 2011 09:59:33 -0800 (PST) Subject: test for list equality In-Reply-To: <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> > My sort issue... as in this doesn't work > >>> if x.sort == y.sort: You're missing the () to make it a function call. Also list.sort() returns none, it mutates the original list. You can either sorted(x) == sorted(y) or set(x) == set(y) From stefan_ml at behnel.de Thu Dec 15 13:01:12 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 15 Dec 2011 19:01:12 +0100 Subject: test for list equality In-Reply-To: <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: noydb, 15.12.2011 18:49: > On Dec 15, 11:36 am, noydb wrote: >> I want to test for equality between two lists. For example, if I have >> two lists that are equal in content but not in order, I want a return >> of 'equal' -- dont care if they are not in the same order. In order >> to get that equality, would I have to sort both lists regardless? if >> yes, how (having issues with list.sort)? >> >> Another way i tried, that I think is kind-of roundabout is like >> x = [2, 5, 1, 88, 9] >> y = [5, 2, 9, 1, 88] >> inBoth = list(set(x)& set(y)) >> >> and then test that list.count is equal between inBoth and x and/or y. >> >> Any better suggestions? >> >> Thanks for any help! > > My sort issue... as in this doesn't work > >>> if x.sort == y.sort: > ... print 'equal' > ... else: > ... print 'not equal' > ... > not equal alist.sort() is a method, so you have to call it in order to execute it. alist.sort will only give you a reference to the method. Comparing that to another method reference returns False, as expected. Also, calling it does not return anything (useful), it modifies the list in place. If you want to create a new list (which you don't want in this case, but anyway), you can use the sorted() builtin function. Stefan From python.list at tim.thechases.com Thu Dec 15 13:01:28 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 15 Dec 2011 12:01:28 -0600 Subject: How to generate "a, b, c, and d"? In-Reply-To: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> Message-ID: <4EEA35F8.4090502@tim.thechases.com> On 12/15/11 10:48, Roy Smith wrote: > I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some standard way to do this, handling all the special cases? > > [] ==> '' > ['a'] ==> 'a' > ['a', 'b'] ==> 'a and b' > ['a', 'b', 'c', 'd'] ==> 'a, b, and c' > > It seems like the kind of thing django.contrib.humanize would handle, but alas, it doesn't. If you have a list, it's pretty easy as MRAB suggests. For arbitrary iterators, it's a bit more complex. Especially with the odd edge-case of 2 items where there's no comma before the conjunction (where >2 has the comma before the conjunction). If you were willing to forgo the Oxford comma, it would tidy up the code a bit. Sample code below -tkc def gen_list(i, conjunction="and"): i = iter(i) first = i.next() try: prev = i.next() except StopIteration: yield first else: more_than_two = False for item in i: if not more_than_two: yield first yield prev prev = item more_than_two = True if more_than_two: yield "%s %s" % (conjunction, prev) else: yield "%s %s %s" % (first, conjunction, prev) def listify(lst, conjunction="and"): return ', '.join(gen_list(lst, conjunction)) for test, expected in ( ([], ''), (['a'], 'a'), (['a', 'b'], 'a and b'), (['a', 'b', 'c'], 'a, b, and c'), (['a', 'b', 'c', 'd'], 'a, b, c, and d'), ): result = listify(test) print "%r -> %r (got %r) %s" % ( test, expected, result, result == expected and "PASSED" or "FAILED" ) From ian.g.kelly at gmail.com Thu Dec 15 13:03:40 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 15 Dec 2011 11:03:40 -0700 Subject: Odd behavior of object equality/identity in the context of relative vs fully qualified imports In-Reply-To: References: Message-ID: On Thu, Dec 15, 2011 at 7:34 AM, Nathan Rice wrote: > I just ran into this yesterday, and I am curious if there is a > rational behind it... > > I have a class that uses a dictionary to dispatch from other classes > (k) to functions for those classes (v). ?I recently ran into a bug > where the dictionary would report that a class which was clearly in > the dictionary's keys was giving a KeyError. ?id() produced two > distinct values, which I found to be curious, and > issubclass/isinstance tests also failed. ?When I inspected the two > classes, I found that the only difference between the two was the > __module__ variable, which in one case had a name relative to the > current module (foo), and in another case had the fully qualified name > (bar.foo). ?When I went ahead and changed the import statement for the > module to import bar.foo rather than import foo, everything worked as > expected. ?My first thought was that I had another foo module in an > old version of the bar package somewhere on my pythonpath; ?After a > thorough search this proved not to be the case. > > Has anyone else run into this? ?Is this intended behavior? ?If so, why? I think this may be the same problem that the poster in this thread encountered: http://groups.google.com/group/comp.lang.python/browse_thread/thread/961a90219a61e19d/9ed06601603b58d4 Normally I believe the __module__ attribute should be fully qualified regardless of whether the import was absolute or relative. That you're not seeing this suggests that the "relative" import may actually be an absolute import starting from a different sys.path entry. From ckaynor at zindagigames.com Thu Dec 15 13:04:01 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Thu, 15 Dec 2011 10:04:01 -0800 Subject: test for list equality In-Reply-To: References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: On Thu, Dec 15, 2011 at 9:57 AM, John Gordon wrote: > In <61edc02c-4f86-45ef-82a1-61c7013003b4 at t38g2000yqe.googlegroups.com> > noydb writes: > > > My sort issue... as in this doesn't work > > >>> if x.sort =3D=3D y.sort: > > ... print 'equal' > > ... else: > > ... print 'not equal' > > ... > > not equal > > > ??? > > Use x.sort() instead of x.sort . > And you cannot use the method in-line - it mutates the list in place, returning None. If you either do not wish to mutate the list, or you absolutely want to do the sort in-line, you need to use the sorted built-in: if sorted(x) == sorted(y): ... However, this will, temporary, use double the memory. > > -- > John Gordon A is for Amy, who fell down the stairs > gordon at panix.com B is for Basil, assaulted by bears > -- Edward Gorey, "The Gashlycrumb Tinies" > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Dec 15 13:06:01 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 18:06:01 +0000 Subject: test for list equality In-Reply-To: <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: <4EEA3709.5080606@mrabarnett.plus.com> On 15/12/2011 17:49, noydb wrote: > On Dec 15, 11:36 am, noydb wrote: >> I want to test for equality between two lists. For example, if I have >> two lists that are equal in content but not in order, I want a return >> of 'equal' -- dont care if they are not in the same order. In order >> to get that equality, would I have to sort both lists regardless? if >> yes, how (having issues with list.sort)? >> >> Another way i tried, that I think is kind-of roundabout is like >> x = [2, 5, 1, 88, 9] >> y = [5, 2, 9, 1, 88] >> inBoth = list(set(x)& set(y)) >> >> and then test that list.count is equal between inBoth and x and/or y. >> >> Any better suggestions? >> >> Thanks for any help! > > My sort issue... as in this doesn't work >>>> if x.sort == y.sort: > ... print 'equal' > ... else: > ... print 'not equal' > ... > not equal > > > ??? .sort is a method which sorts the list in-place and returns None. You must provide the () if you want to call it, otherwise you just get a reference to the method: >>> x [2, 5, 1, 88, 9] >>> x.sort >>> x.sort() >>> x [1, 2, 5, 9, 88] There's also a function "sorted" which returns its argument as a sorted list. The argument itself isn't altered: >>> y = [5, 2, 9, 1, 88] >>> sorted(y) [1, 2, 5, 9, 88] >>> y [5, 2, 9, 1, 88] It's all in the documentation! :-) From jenn.duerr at gmail.com Thu Dec 15 13:07:39 2011 From: jenn.duerr at gmail.com (noydb) Date: Thu, 15 Dec 2011 10:07:39 -0800 (PST) Subject: test for list equality References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> Message-ID: <18c38490-c667-4774-ad26-c68ab042f72b@q16g2000yqn.googlegroups.com> Ahh, I see (on the sort issue), thanks All! Still, any other slicker ways to do this? Just for learning. From python at mrabarnett.plus.com Thu Dec 15 13:12:33 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 18:12:33 +0000 Subject: test for list equality In-Reply-To: <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: <4EEA3891.4060004@mrabarnett.plus.com> On 15/12/2011 17:59, Miki Tebeka wrote: >> My sort issue... as in this doesn't work >> >>> if x.sort == y.sort: > You're missing the () to make it a function call. > Also list.sort() returns none, it mutates the original list. > You can either > sorted(x) == sorted(y) > or > set(x) == set(y) But don't use sets if there may be duplicates. From darnold992000 at yahoo.com Thu Dec 15 13:12:39 2011 From: darnold992000 at yahoo.com (darnold) Date: Thu, 15 Dec 2011 10:12:39 -0800 (PST) Subject: test for list equality References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: On Dec 15, 11:59?am, Miki Tebeka wrote: > > My sort issue... as in this doesn't work > > >>> if x.sort == y.sort: > > You're missing the () to make it a function call. > Also list.sort() returns none, it mutates the original list. > You can either > ? ? sorted(x) == sorted(y) > or > ? ? set(x) == set(y) I'm pretty sure we don't want to use set() since it throws away duplicates: >>> x = [1,2,3,4] >>> y = [1,1,2,2,3,3,4] >>> sorted(x) == sorted(y) False >>> set(x) == set(y) True From nagle at animats.com Thu Dec 15 13:15:27 2011 From: nagle at animats.com (John Nagle) Date: Thu, 15 Dec 2011 10:15:27 -0800 Subject: MySQLdb for Python 2.7.2 for Windows 32 bit? Message-ID: <4eea393b$0$1665$742ec2ed@news.sonic.net> Is there a MySQLdb binary for Python 2.7.2 for Windows 32 bit? There's a 2.7 binary at "http://www.lfd.uci.edu/~gohlke/pythonlibs/" but not a 2.7.2 binary. (Also, using modules from untrusted sites is riskier than it used to be. GPL code for major packages with added spyware and adware has appeared on major sites. CNet was heavily criticized for bundling NMap with adware on "download.com") John Nagle From ethan at stoneleaf.us Thu Dec 15 13:19:51 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 15 Dec 2011 10:19:51 -0800 Subject: How to generate "a, b, c, and d"? In-Reply-To: <4EEA35F8.4090502@tim.thechases.com> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> Message-ID: <4EEA3A47.5020106@stoneleaf.us> Tim Chase wrote: > On 12/15/11 10:48, Roy Smith wrote: >> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, >> "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some >> standard way to do this, handling all the special cases? >> >> [] ==> '' >> ['a'] ==> 'a' >> ['a', 'b'] ==> 'a and b' >> ['a', 'b', 'c', 'd'] ==> 'a, b, and c' >> >> It seems like the kind of thing django.contrib.humanize would handle, >> but alas, it doesn't. > > If you have a list, it's pretty easy as MRAB suggests. For arbitrary > iterators, it's a bit more complex. Especially with the odd edge-case > of 2 items where there's no comma before the conjunction (where >2 has > the comma before the conjunction). If you were willing to forgo the > Oxford comma, it would tidy up the code a bit. Sample code below Why go through all that instead of just converting the iterator into a list at the beginning of MRAB's solution and then running with it? ~Ethan~ From python.list at tim.thechases.com Thu Dec 15 13:20:10 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 15 Dec 2011 12:20:10 -0600 Subject: test for list equality In-Reply-To: <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: <4EEA3A5A.3060705@tim.thechases.com> On 12/15/11 11:59, Miki Tebeka wrote: >> My sort issue... as in this doesn't work >>>>> if x.sort == y.sort: > You're missing the () to make it a function call. > Also list.sort() returns none, it mutates the original list. > You can either > sorted(x) == sorted(y) > or > set(x) == set(y) Duplicates cause issues in the set() version: a = [1,2,3,4] b = a + a print sorted(a) == sorted(b) # False print set(a) == set(b) # True They mean different things, and the OP may want one or the other depending on how they want to consider duplicates. -tkc From __peter__ at web.de Thu Dec 15 13:23:37 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 15 Dec 2011 19:23:37 +0100 Subject: Odd behavior of object equality/identity in the context of relative vs fully qualified imports References: Message-ID: Nathan Rice wrote: > I just ran into this yesterday, and I am curious if there is a > rational behind it... > > I have a class that uses a dictionary to dispatch from other classes > (k) to functions for those classes (v). I recently ran into a bug > where the dictionary would report that a class which was clearly in > the dictionary's keys was giving a KeyError. id() produced two > distinct values, which I found to be curious, and > issubclass/isinstance tests also failed. When I inspected the two > classes, I found that the only difference between the two was the > __module__ variable, which in one case had a name relative to the > current module (foo), and in another case had the fully qualified name > (bar.foo). When I went ahead and changed the import statement for the > module to import bar.foo rather than import foo, everything worked as > expected. My first thought was that I had another foo module in an > old version of the bar package somewhere on my pythonpath; After a > thorough search this proved not to be the case. > > Has anyone else run into this? Is this intended behavior? If so, why? Not exactly intended, but a logical side effect of how Python identifies its modules. The problem is that you have an entry in sys.path that reaches into the bar package. When you import foo Python does not check whether [path1]/bar/foo.py points to the same file as [path2]/foo.py, it just verifies that the name "foo" is not in the sys.modules cache before it physically imports the file. Therefore you get two distinct imports of "foo.py" stored in the cache as "foo" and "bar.foo". As Python's classes are not declarations, but objects themselves you get distinct classes just as with the following >>> classes = set() >>> for i in range(3): ... class A: pass ... >>> classes = set() >>> for i in range(3): ... class A: pass ... classes.add(A) ... >>> classes set([, , ]) To avoid the problem just remove the offending [path2] from sys.path and always import the foo subpackage with import bar.foo # everywhere or from . import foo # inside bar PS: You may run into similar problems when you import the main module of a program. It will end up in the module cache as filename_sans_extension and "__main__". From miki.tebeka at gmail.com Thu Dec 15 13:25:42 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 15 Dec 2011 10:25:42 -0800 (PST) Subject: test for list equality In-Reply-To: References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: <30504278.623.1323973542217.JavaMail.geo-discussion-forums@yqbo42> > > set(x) == set(y) > > Duplicates cause issues in the set() version: You're right, I stand corrected. From miki.tebeka at gmail.com Thu Dec 15 13:25:42 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 15 Dec 2011 10:25:42 -0800 (PST) Subject: test for list equality In-Reply-To: References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: <30504278.623.1323973542217.JavaMail.geo-discussion-forums@yqbo42> > > set(x) == set(y) > > Duplicates cause issues in the set() version: You're right, I stand corrected. From ian.g.kelly at gmail.com Thu Dec 15 13:28:21 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 15 Dec 2011 11:28:21 -0700 Subject: test for list equality In-Reply-To: <18c38490-c667-4774-ad26-c68ab042f72b@q16g2000yqn.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <18c38490-c667-4774-ad26-c68ab042f72b@q16g2000yqn.googlegroups.com> Message-ID: On Thu, Dec 15, 2011 at 11:07 AM, noydb wrote: > Ahh, I see (on the sort issue), thanks All! > > Still, any other slicker ways to do this? ?Just for learning. MRAB's collections.Counter suggestion is what I would do. Very tidy, and also more efficient I think: O(n) instead of O(n log n). From jenn.duerr at gmail.com Thu Dec 15 13:32:30 2011 From: jenn.duerr at gmail.com (noydb) Date: Thu, 15 Dec 2011 10:32:30 -0800 (PST) Subject: test for list equality References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <18c38490-c667-4774-ad26-c68ab042f72b@q16g2000yqn.googlegroups.com> Message-ID: <60b72ba6-5456-4e8a-99f2-da2879641f70@r28g2000yqj.googlegroups.com> Thanks All, Thanks MRAB -- good note on the sort() vs sorted(). From einazaki668 at yahoo.com Thu Dec 15 13:37:59 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 15 Dec 2011 10:37:59 -0800 (PST) Subject: file data => array(s) References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> Message-ID: <388939ad-b465-4dd0-8eb8-51395ee643a7@o7g2000yqk.googlegroups.com> On Dec 14, 4:59?pm, Dave Angel wrote: > On 12/14/2011 05:20 PM, Eric wrote: > > > > > > > > > I'm trying to read some file data into a set of arrays. ?The file data > > is just four columns of numbers, like so: > > > ? ? 1.2 ? ?2.2 ? 3.3 ?0.5 > > ? ? 0.1 ? 0.2 ? ?1.0 ?10.1 > > ? ? ... and so on > > > I'd like to read this into four arrays, one array for each column. > > Alternatively, I guess something like this is okay too: > > > ? ? [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on] > > > I came up with the following for the four array option: > > > ? ? file = open(fileName, 'r') > > ? ? for line in file.readlines(): > > The readlines() call is a waste of time/space. ?file is already an > iterator that'll return lines for you. > > > > > > > > > ? ? ? ?d1, e1, d2, e2 = map(float, line.split()) > > ? ? ? ?data1.append(d1) ?# where data1, err1, data2, err2 are init-ed > > as empty lists > > ? ? ? ?err1.append(e1) > > ? ? ? ?data2.append(d2) > > ? ? ? ?err2.append(e2) > > ? ? file.close() > > > But somehow it doesn't seem very python-esque (I'm thinking there's a > > more elegant and succinct way to do it in python). ?I've also tried > > replacing the above "map" line with: > > > ? ? ? ?d = d + map(float, line.split()) ?# where d is initialized as d > > = [] > > > But all I get is one long flat list, not what I want. > > > So is the map and append method the best I can do or is there a > > slicker way? > > > One more thing, no numpy. ?Nothing against numpy but I'm curious to > > see what can be done with just the box stock python install. > > > TIA, > > eric > > When I see a problem like this, I turn to zip(). ?It's got some powerful > uses when rows and columns need inverting. > > I didn't try it on an actual file, but the following works: > linedata = ? ?[[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1] ] > > data, err1, data2, err2 = zip(*linedata) > > print data > print err1 > print data2 > print err2 > > So you could try (untested) > > file = open(filename, "r") > linedata = [ map(float, line) for line in file] > data, err1, data2, err2 = zip(*linedata) > file.close() > > DaveA Neat. This is what I had in mind for a python-esque solution. Only thing is "map(float,line)" should be "map(float,line.split()). Looks like it should be easy enough to weed out any funky data sets because between map() and zip() it's fairly picky about the amount and type of data. Finally, the input files I'll be using for real aren't just four columns of data. The beginning of the file may have comments (optional) and will have two lines of text to identify the data. Maybe I can still do it w/o readlines. Thanks, eric From einazaki668 at yahoo.com Thu Dec 15 13:51:03 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 15 Dec 2011 10:51:03 -0800 (PST) Subject: file data => array(s) References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> <4ee930fd$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1744e277-69ba-4648-a86f-023af83ca939@u32g2000yqe.googlegroups.com> On Dec 14, 5:27?pm, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 14:20:40 -0800, Eric wrote: > > I'm trying to read some file data into a set of arrays. ?The file data > > is just four columns of numbers, like so: > > > ? ?1.2 ? ?2.2 ? 3.3 ?0.5 > > ? ?0.1 ? 0.2 ? ?1.0 ?10.1 > > ? ?... and so on > > > I'd like to read this into four arrays, one array for each column. > > Alternatively, I guess something like this is okay too: > > > ? ?[[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on] > > First thing: due to the fundamental nature of binary floating point > numbers, if you convert text like "0.1" to a float, you don't get 0.1, > you get 0.10000000000000001. That is because 0.1000...01 is the closest > possible combination of fractions of 1/2, 1/4, 1/8, ... that adds up to > 1/10. > > If this fact disturbs you, you can import the decimal module and use > decimal.Decimal instead; otherwise forget I said anything and continue > using float. I will assume you're happy with floats. > Yeah, I don't think it'll be a problem. As I understand it a float in python is a double in C and all our old C programs used doubles. From PDP-11 to MIPS3k to P2 I've seen what I think may have been rounding or precision errors but I haven't heard any complaints because of them. Thanks, eric From python.list at tim.thechases.com Thu Dec 15 13:51:49 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 15 Dec 2011 12:51:49 -0600 Subject: How to generate "a, b, c, and d"? In-Reply-To: <4EEA3A47.5020106@stoneleaf.us> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> <4EEA3A47.5020106@stoneleaf.us> Message-ID: <4EEA41C5.60107@tim.thechases.com> On 12/15/11 12:19, Ethan Furman wrote: > Tim Chase wrote: >> On 12/15/11 10:48, Roy Smith wrote: >>> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, >>> "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some >>> standard way to do this, handling all the special cases? >> >> If you have a list, it's pretty easy as MRAB suggests. For arbitrary >> iterators, it's a bit more complex. Especially with the odd edge-case >> of 2 items where there's no comma before the conjunction (where>2 has >> the comma before the conjunction). If you were willing to forgo the >> Oxford comma, it would tidy up the code a bit. > > Why go through all that instead of just converting the iterator into a > list at the beginning of MRAB's solution and then running with it? For the fun/challenge? Because you have a REALLY big data source that you don't want to keep in memory (in addition the resulting string)? Yeah, for most non-pathological cases, it would make more sense to just make it a list and then deal with the 4 cases (no elements, one element, 2 elements, and >2 elements) individually. -tkc From roy at panix.com Thu Dec 15 14:01:14 2011 From: roy at panix.com (Roy Smith) Date: Thu, 15 Dec 2011 11:01:14 -0800 (PST) Subject: How to generate "a, b, c, and d"? In-Reply-To: References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> <4EEA3A47.5020106@stoneleaf.us> Message-ID: <19419800.394.1323975674835.JavaMail.geo-discussion-forums@vbyc2> FWIW, I ended up with: n = len(names) if n == 0: return '' if n == 1: return names[0] pre = ', '.join(names[:-1]) post = names[-1] return '%s, and %s' (pre, post) the slice-and-join() takes care of both the 2 and >2 element cases at the same time :) It would be nice if there were some standard way to do this. I'm sure I've seen something that was essentially a join() that took two delimiters; one for most elements, the other a special-case for the last one. I can't remember where I saw it. I'm guessing in some web framework. From roy at panix.com Thu Dec 15 14:01:14 2011 From: roy at panix.com (Roy Smith) Date: Thu, 15 Dec 2011 11:01:14 -0800 (PST) Subject: How to generate "a, b, c, and d"? In-Reply-To: References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> <4EEA3A47.5020106@stoneleaf.us> Message-ID: <19419800.394.1323975674835.JavaMail.geo-discussion-forums@vbyc2> FWIW, I ended up with: n = len(names) if n == 0: return '' if n == 1: return names[0] pre = ', '.join(names[:-1]) post = names[-1] return '%s, and %s' (pre, post) the slice-and-join() takes care of both the 2 and >2 element cases at the same time :) It would be nice if there were some standard way to do this. I'm sure I've seen something that was essentially a join() that took two delimiters; one for most elements, the other a special-case for the last one. I can't remember where I saw it. I'm guessing in some web framework. From waitmeforever at hotmail.com Thu Dec 15 14:09:55 2011 From: waitmeforever at hotmail.com (Yang Chun-Kai) Date: Fri, 16 Dec 2011 03:09:55 +0800 Subject: Localhost client-server simple ssl socket test program problems Message-ID: Hello,everyone!! I am writing a simple ssl client-server test program on my personal laptop. And I encounter some problems with my simple programs. Please give me some helps.-------------------------------------------------------------------------------------------------------------------------------------------------------- My server code: import socketimport sslbindsocket = socket.socket()bindsocket.bind(('127.0.0.1', 1234))bindsocket.listen(5)print 'server is waiting for connection...'newsocket, fromaddr = bindsocket.accept()print 'start ssl socket...'connstream = ssl.wrap_socket(newsocket, server_side=True, certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", ssl_version=ssl.PROTOCOL_SSLv23)data = connstream.read()print 'connected from address', fromaddrprint 'received data as', repr(data)connstream.close() My client code: import socketimport ssls = socket.socket(socket.AF_INET, socket.SOCK_STREAM)ssl_sock = ssl.wrap_socket(s, ca_certs="/home/ckyang/PHA/testsslsocket/myCA.crt", cert_reqs=ssl.CERT_REQUIRED)ssl_sock.connect(("127.0.0.1", 1234))ssl_sock.write("hello")ssl_sock.close() -----------------------------------------------------------------------------------------------------------------------------------------------------------Server side error: File "views.py", line 17, in connstream = ssl.wrap_socket(newsocket, server_side=True, certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", ssl_version=ssl.PROTOCOL_SSLv23) File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket ciphers=ciphers) File "/usr/lib/python2.7/ssl.py", line 119, in __init__ ciphers)ssl.SSLError: [Errno 336265218] _ssl.c:347: error:140B0002:SSL routines:SSL_CTX_use_PrivateKey_file:system lib Client side error: File "client.py", line 10, in ssl_sock.connect(("127.0.0.1", 1234)) File "/usr/lib/python2.7/ssl.py", line 299, in connect self.do_handshake() File "/usr/lib/python2.7/ssl.py", line 283, in do_handshake self._sslobj.do_handshake()socket.error: [Errno 104] Connection reset by peer ------------------------------------------------------------------------------------------------------------------------------------------------------------So what is wrong with my code? The codes are so simple and so much like python official site sample demonstration, but I still cant get it work, so frustrating. Seems the problem happened on server side then cause client side cant connect well, is that right? My platform is ubuntu, with openssl 0.9.8 and python 2.7. All certificates and keys self-signed by openssl for test convenience. This is the site for referrence : http://andyjeffries.co.uk/articles/x509-encrypted-authenticated-socket-ruby-client Or should I need a real certificate issued by a real CA to let things work? Any tips or suggestions welcomed, thank you very much~ Good day. Kay -------------- next part -------------- An HTML attachment was scrubbed... URL: From rhettnaxel at gmail.com Thu Dec 15 14:24:37 2011 From: rhettnaxel at gmail.com (Alexander) Date: Thu, 15 Dec 2011 14:24:37 -0500 Subject: [Tutor] Localhost client-server simple ssl socket test program problems In-Reply-To: References: Message-ID: 2011/12/15 Yang Chun-Kai > Hello,everyone!! > > I am writing a simple ssl client-server test program on my personal laptop. > > And I encounter some problems with my simple programs. > > Please give me some helps. > > -------------------------------------------------------------------------------------------------------------------------------------------------------- > > My server code: > > import socket > import ssl > bindsocket = socket.socket() > bindsocket.bind(('127.0.0.1', 1234)) > bindsocket.listen(5) > print 'server is waiting for connection...' > newsocket, fromaddr = bindsocket.accept() > print 'start ssl socket...' > connstream = ssl.wrap_socket(newsocket, server_side=True, > certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", > keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", > ssl_version=ssl.PROTOCOL_SSLv23) > data = connstream.read() > print 'connected from address', fromaddr > print 'received data as', repr(data) > connstream.close() > > My client code: > > import socket > import ssl > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) > ssl_sock = ssl.wrap_socket(s, > ca_certs="/home/ckyang/PHA/testsslsocket/myCA.crt", > cert_reqs=ssl.CERT_REQUIRED) > ssl_sock.connect(("127.0.0.1", 1234)) > ssl_sock.write("hello") > ssl_sock.close() > > > ----------------------------------------------------------------------------------------------------------------------------------------------------------- > Server side error: > > File "views.py", line 17, in & lt;module> > connstream = ssl.wrap_socket(newsocket, server_side=True, > certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", > keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", > ssl_version=ssl.PROTOCOL_SSLv23) > File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket > ciphers=ciphers) > File "/usr/lib/python2.7/ssl.py", line 119, in __init__ > ciphers) > ssl.SSLError: [Errno 336265218] _ssl.c:347: error:140B0002:SSL > routines:SSL_CTX_use_PrivateKey_file:system lib > > Client side error: > > File "client.py", line 10, in > ssl_sock.connect(("127.0.0.1", 1234)) > File "/usr/lib/python2.7/ssl.py", line 299, in connect** > self.do_handshake() > File "/usr/lib/python2.7/ssl.py", line 283, in do_handshake > self._sslobj.do_handshake() > socket.error: [Errno 104] Connection reset by peer > > > ------------------------------------------------------------------------------------------------------------------------------------------------------------ > So what is wrong with my code? > > The codes are so simple and so much like python official site sample > demonstration, but I still cant get it work, so frustrating. > > Seems the problem happened on server side then cause client side cant > connect well, is that right? > > ** > My platform is ubuntu, with openssl 0.9.8 and python 2.7. > > All certificates and keys self-signed by openssl for test convenience. > > This is the site for referrence : > http://andyjeffries.co.uk/articles/x509-encrypted-authenticated-socket-ruby-client > > Or should I need a real certificate issued by a real CA to let things work? > > Any tips or suggestions welcomed, thank you very much~ > > Good day. > > Kay > > ** > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > You're trying to connect to the same port on localhost as a client and a server? I don't know for certain but I don't think that should work. Two computers? -- Alexander 7D9C597B -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Dec 15 14:27:09 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 19:27:09 +0000 Subject: How to generate "a, b, c, and d"? In-Reply-To: <4EEA41C5.60107@tim.thechases.com> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> <4EEA3A47.5020106@stoneleaf.us> <4EEA41C5.60107@tim.thechases.com> Message-ID: <4EEA4A0D.5030406@mrabarnett.plus.com> On 15/12/2011 18:51, Tim Chase wrote: > On 12/15/11 12:19, Ethan Furman wrote: >> Tim Chase wrote: >>> On 12/15/11 10:48, Roy Smith wrote: >>>> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the >>>> string, "a, b, c, and d" (I'll settle for no comma after 'c'). >>>> Is there some standard way to do this, handling all the special >>>> cases? >>> >>> If you have a list, it's pretty easy as MRAB suggests. For >>> arbitrary iterators, it's a bit more complex. Especially with >>> the odd edge-case of 2 items where there's no comma before the >>> conjunction (where>2 has the comma before the conjunction). If >>> you were willing to forgo the Oxford comma, it would tidy up the >>> code a bit. >> >> Why go through all that instead of just converting the iterator >> into a list at the beginning of MRAB's solution and then running >> with it? > > For the fun/challenge? Because you have a REALLY big data source > that you don't want to keep in memory (in addition the resulting > string)? > > Yeah, for most non-pathological cases, it would make more sense to > just make it a list and then deal with the 4 cases (no elements, one > element, 2 elements, and>2 elements) individually. > I was going to question it too, but then I wondered what would happen if there were a very large number of items and the string would be too big for memory, for example, writing a list of all the numbers from one to a billion to a file. From python at mrabarnett.plus.com Thu Dec 15 14:34:04 2011 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 15 Dec 2011 19:34:04 +0000 Subject: file data => array(s) In-Reply-To: <388939ad-b465-4dd0-8eb8-51395ee643a7@o7g2000yqk.googlegroups.com> References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> <388939ad-b465-4dd0-8eb8-51395ee643a7@o7g2000yqk.googlegroups.com> Message-ID: <4EEA4BAC.3070806@mrabarnett.plus.com> On 15/12/2011 18:37, Eric wrote: [snip] > > Neat. This is what I had in mind for a python-esque solution. [snip] FYI, the word is "Pythonic" when talking about the programming language. The word "Pythonesque" refers to Monty Python. From lists at cheimes.de Thu Dec 15 14:45:43 2011 From: lists at cheimes.de (Christian Heimes) Date: Thu, 15 Dec 2011 20:45:43 +0100 Subject: Localhost client-server simple ssl socket test program problems In-Reply-To: References: Message-ID: Am 15.12.2011 20:09, schrieb Yang Chun-Kai: > Server side error: > > File "views.py", line 17, in > connstream = ssl.wrap_socket(newsocket, server_side=True, > certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", > keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", > ssl_version=ssl.PROTOCOL_SSLv23) > File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket > ciphers=ciphers) > File "/usr/lib/python2.7/ssl.py", line 119, in __init__ > ciphers) > ssl.SSLError: [Errno 336265218] _ssl..c:347: error:140B0002:SSL > routines:SSL_CTX_use_PrivateKey_file:system lib This error is most likely caused by an encrypted private key. Python's SSL lib doesn't support encrypted private keys for sockets. You can encrypt the private key with openssl rsa -in /etc/home/ckyang/PHA/testsslsocket/mypha.key -out /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key Christian From invalid at invalid.invalid Thu Dec 15 14:50:27 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Thu, 15 Dec 2011 19:50:27 +0000 (UTC) Subject: Screen Control Fullscreen ON/OFF References: <376e5d38-4384-4453-9719-302150827c58@a17g2000yqj.googlegroups.com> Message-ID: On 2011-12-15, Nizamov Shawkat wrote: >>> >>> I would like to make fullscreen white and fullscreen black using >>> Python on Linux. With in the specs of the LCD, I want to be able to >>> display fullscreen white and black approximately at 30Hz. Frequency >>> (on/off per second) will be input manually which is between 1-40Hz. >>> Any idea where to start ? Can Python perform such fast thing ? >> >> Forgot to mention and sorry for the pollution ; >> >> My LCD has 2ms respond time thus it can handle a maximum of 50Hz ON/ >> OFF (white/black) thus seems to fit my 1-40Hz range. I am not asking >> or looking for a fully working code, I just need some directions. > > > It depends on whether you want sync to vblank or not. If not, that is > pretty easy - use sleep() or something similar. If you have to use > sync (screen is always either black or white, never partly black and > white) then it is much much more difficult. Actually I do not know of > any way to sync to it. I'm _guessing_ the results won't be acceptible unless you switch during the veritical blanking period. (_is_ there a vertical blanking period with a DVI or HDMI output?). I have vague recollections that I think opengl has methods to do that. If there is an easy way to do it, it'll probably be using pygame: http://pygame.org/news.html If pygame can't do it, try pyopengl: http://pyopengl.sourceforge.net/ Here's a thread on how to do it on OSX: http://www.idevgames.com/forums/thread-2974.html -- Grant Edwards grant.b.edwards Yow! My face is new, my at license is expired, and I'm gmail.com under a doctor's care!!!! From waitmeforever at hotmail.com Thu Dec 15 15:09:14 2011 From: waitmeforever at hotmail.com (Yang Chun-Kai) Date: Fri, 16 Dec 2011 04:09:14 +0800 Subject: Localhost client-server simple ssl socket test program problems In-Reply-To: References: , Message-ID: Thanks for tips. But I dont understand one thing is if Python's SSL lib doesn't support encrypted private keys for sockets. Then why should we "encrypt" the private key with "openssl rsa -in /etc/home/ckyang/PHA/testsslsocket/mypha.key -out /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key" again? Shouldn't that be decrypted? And also this solution is not the right one, I use mypha-nopasswd.key replace the original one, still not work. So sad. But thanks. ^ ^ Kay > To: python-list at python.org > From: lists at cheimes.de > Subject: Re: Localhost client-server simple ssl socket test program problems > Date: Thu, 15 Dec 2011 20:45:43 +0100 > > Am 15.12.2011 20:09, schrieb Yang Chun-Kai: > > Server side error: > > > > File "views.py", line 17, in > > connstream = ssl.wrap_socket(newsocket, server_side=True, > > certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", > > keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", > > ssl_version=ssl.PROTOCOL_SSLv23) > > File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket > > ciphers=ciphers) > > File "/usr/lib/python2.7/ssl.py", line 119, in __init__ > > ciphers) > > ssl.SSLError: [Errno 336265218] _ssl..c:347: error:140B0002:SSL > > routines:SSL_CTX_use_PrivateKey_file:system lib > > This error is most likely caused by an encrypted private key. Python's > SSL lib doesn't support encrypted private keys for sockets. You can > encrypt the private key with > > openssl rsa -in /etc/home/ckyang/PHA/testsslsocket/mypha.key -out > /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key > > Christian > > > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From fabiofz at gmail.com Thu Dec 15 15:18:14 2011 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Thu, 15 Dec 2011 18:18:14 -0200 Subject: PyDev 2.3.0 Released Message-ID: Hi All, PyDev 2.3.0 has been released Details on PyDev: http://pydev.org Details on its development: http://pydev.blogspot.com Release Highlights: ------------------------------- * Pep8.py integrated (must be enabled in PyDev > Editor > Code Analysis > pep8.py). * Faster PyDev startup (internal Jython upgraded to version 2.2.1 -- and also optimized for PyDev). * Action to select/deselect scope (Shift+Alt+Up/Down). * Fix: cache issue where the PYTHONPATH in memory became different from the PYTHONPATH configured for a project. * Fix: OutOfMemoryError when dealing with PyOpenGL. * Fix: deadlock (could occur in a race condition when importing a project with an existing Python configuration). * Fix: code-completion integration issue with IPython 011 (patch from jonahkichwacoders). * Fix: annotation could remain in editor after removing a marker. * Fix: BadLocationException on extract local refactoring. What is PyDev? --------------------------- PyDev is a plugin that enables users to use Eclipse for Python, Jython and IronPython development -- making Eclipse a first class Python IDE -- It comes with many goodies such as code completion, syntax highlighting, syntax analysis, refactor, debug and many others. Cheers, -- Fabio Zadrozny ------------------------------------------------------ Software Developer Appcelerator http://appcelerator.com/ Aptana http://aptana.com/ PyDev - Python Development Environment for Eclipse http://pydev.org http://pydev.blogspot.com From lists at cheimes.de Thu Dec 15 15:19:14 2011 From: lists at cheimes.de (Christian Heimes) Date: Thu, 15 Dec 2011 21:19:14 +0100 Subject: Localhost client-server simple ssl socket test program problems In-Reply-To: References: , Message-ID: Am 15.12.2011 21:09, schrieb Yang Chun-Kai: > Thanks for tips. > > But I dont understand one thing is if Python's SSL lib doesn't support > encrypted private keys for sockets. > > Then why should we "encrypt" the private key with "openssl rsa -in > /etc/home/ckyang/PHA/testsslsocket/mypha.key -out > > /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key" again? > > Shouldn't that be decrypted? > > And also this solution is not the right one , I use mypha-nopasswd.key > replace the original one, still not work. IIRC the command should decrypt the key. Did it prompt for a password? The error could be caused by other issues. For example the key and cert must be in PEM Format. The PKS#12 isn't supported. I'm not sure if Python's builtin SSL module loads DER certs. You may also missing a valid CA cert chain. Python 2.x's SSL module doesn't support cert directories so you have to provide a chain file. The certs in the chain file must be in the right order, too. Christian From greg.ewing at canterbury.ac.nz Thu Dec 15 15:34:52 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 16 Dec 2011 09:34:52 +1300 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <9kv3vhFn8U1@mid.individual.net> MRAB wrote: >> To give an analogy, it is like defining mammals as "hairy animals which >> give birth to live young", which is correct for all mammals except for >> monotremes, which are mammals which lay eggs. > > Or the naked mole-rat. Or cetaceans (whales). The way I understand it, the main characteristic shared by all mammals is the presence of mammary glands in females. To wrest this back on topic, sometimes a definition can be improved without making it any more complicated. In the case of methods, perhaps instead of "defined inside a class body" it could say "bound to a name in a class namespace". That's what really matters, not how it came to be there. -- Greg From bouleetbil at gmail.com Thu Dec 15 15:36:07 2011 From: bouleetbil at gmail.com (bouleetbil bouleetbil) Date: Thu, 15 Dec 2011 21:36:07 +0100 Subject: LoadLibrary and extern ctype Message-ID: Hi, I've a little problem for define this : extern enum __pmerrno_t { PM_ERR_MEMORY = 1, PM_ERR_SYSTEM, .... PM_ERR_WRONG_ARCH } pm_errno; I've write : pacman=cdll.LoadLibrary("libpacman.so") ... #errors ( PM_ERR_MEMORY , PM_ERR_SYSTEM, ... PM_ERR_WRONG_ARCH )=map(ctypes.c_int, xrange(1,63)) I would use pacman.pm_errno. That should be a integer but if I print the value it I've : <_FuncPtr object at 0xb73ce984> Howto find the value (or define ) pacman.pm_errno ? The complete code can be find here : http://dors.frugalware.org/tmpgit/frugal-tweak/py-pacman/py-pacman.py and the .h is here http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=blob_plain;f=lib/libpacman/pacman.h;h=0f2a191681085dee99f485bad43a620043528904;hb=HEAD -- #--------------------------------------------------------------------------- # Bouleetbil # #--------------------------------------------------------------------------- # _ _ # (o)--(o) # /.______.\ # \_______/ # ./ \. # ( . , ) # \ \_\\//_/ / # ~~ ~~ ~~ #--------------------------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu Dec 15 15:36:25 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Dec 2011 07:36:25 +1100 Subject: [Tutor] Localhost client-server simple ssl socket test program problems In-Reply-To: References: Message-ID: On Fri, Dec 16, 2011 at 6:24 AM, Alexander wrote: > You're trying to connect to the same port on localhost as a client and a > server? I don't know for certain but I don't think that should work. > Two computers? That's bind() on the server and connect() on the client - the normal way to do things. That part of the code is fine (far as I can tell - haven't actually run the code, just eyeballed it). Chris Angelico From joshua.landau.ws at gmail.com Thu Dec 15 15:42:37 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 15 Dec 2011 20:42:37 +0000 Subject: Overriding a global In-Reply-To: <4EE8D78B.108@sequans.com> References: <4ee671f6$0$29979$c3e8da3$5496439d@news.astraweb.com> <4ee733d4$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EE75382.9060104@sequans.com> <4EE8771D.1050902@sequans.com> <4EE8D78B.108@sequans.com> Message-ID: On 14 December 2011 17:06, Jean-Michel Pichavant wrote: > Joshua Landau wrote: > >> [snip] >> Using currentLogger is just padding, in my opinion. *Every *value is >> "current". >> > Not always. I try to keep names on the same object because that object is > supposed to be named that way. > I can change one of the object attribute, but the object named that way > keep being the same. > > Class Foo: > self.__init__(self): > self.banana = 5 > > myFoo = Foo() > > Now there's a slight difference between > > myFoo = Exception() > and > myFoo.banana = 4 > > The first statement rebind myFoo to something complitely different. > the second statement change one of the object rightfully named myFoo .. > attribute (not sure about this construct :D ) > > Int being inmutable, you can rebind a name without changing its meaning. > type(myFoo) before != type(myFoo) after type(myFoo.bannana) before == type(myFoo.bannana) after type(logger) before == type(logger) after You are changing the int to a new value, but it's OK for you - if I understand rightly - because it's not changing its "meaning". The "meaning" of logger is the same too, as it does the same thing, in approximately the same way. I agree that "logger = Exception()" would be vary bad in that sense, too. The point is you use it the same way and it's not going to cause problems from misunderstanding what the object type is. And because "logger = childLogger('this_function')" is explicit and hopefully at the top 'o the function, any conceivable errors should be pretty obvious. Note that I used quotation marks around "meaning" not to mock the use or whatnot, but because I was trying to show that I was trying to use the same version of the word you did. I don't know if that was clear from the passage itself. That said, you did keep the myFoo the same, even with the int change. That makes the change global to all myFoo instances. But I didn't get that as a main point from your argument, and if it was I fear we're too close to recursive arguments. And in regards to your own commentary on your English, I wouldn't like you to see it as trouble. I'm currently thoroughly enjoying the debate, and wouldn't like to see myself scaring you off... -------------- next part -------------- An HTML attachment was scrubbed... URL: From waitmeforever at hotmail.com Thu Dec 15 15:58:01 2011 From: waitmeforever at hotmail.com (Yang Chun-Kai) Date: Fri, 16 Dec 2011 04:58:01 +0800 Subject: Localhost client-server simple ssl socket test program problems In-Reply-To: References: , , , , Message-ID: Hello~ Thanks for your fast reply. No, it doesn't ask for password, just a single line with "writing RSA kay", then mypha-nopasswd.key appeared. If my key is not in PEM Format, can openssl with simple commands to switch it to? Or I should re-do the self-signed process with some certain key-words / parameters? And what you mean about Python 2.x's SSL module doesn't support cert directories ? Can you be more specific about that ^^. Do you mean parameters with certfile and keyfile those two should put together or CA certificate need to be chained with other CA? Thanks. Kay > To: python-list at python.org > From: lists at cheimes.de > Subject: Re: Localhost client-server simple ssl socket test program problems > Date: Thu, 15 Dec 2011 21:19:14 +0100 > > Am 15.12.2011 21:09, schrieb Yang Chun-Kai: > > Thanks for tips. > > > > But I dont understand one thing is if Python's SSL lib doesn't support > > encrypted private keys for sockets. > > > > Then why should we "encrypt" the private key with "openssl rsa -in > > /etc/home/ckyang/PHA/testsslsocket/mypha.key -out > > > > /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key" again? > > > > Shouldn't that be decrypted? > > > > And also this solution is not the right one , I use mypha-nopasswd.key > > replace the original one, still not work. > > IIRC the command should decrypt the key. Did it prompt for a password? > > The error could be caused by other issues. For example the key and cert > must be in PEM Format. The PKS#12 isn't supported. I'm not sure if > Python's builtin SSL module loads DER certs. > > You may also missing a valid CA cert chain. Python 2.x's SSL module > doesn't support cert directories so you have to provide a chain file. > The certs in the chain file must be in the right order, too. > > Christian > > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From icanbob at gmail.com Thu Dec 15 16:17:29 2011 From: icanbob at gmail.com (bobicanprogram) Date: Thu, 15 Dec 2011 13:17:29 -0800 (PST) Subject: Ann: Python-SIMPL online tutorial updated to include examples of cloud interfacing Message-ID: The Python-SIMPL tutorial is at: http://www.icanprogram.com/06py/lesson1/lesson1.html Several lessons (3,4, 6 &7) have been enhanced to include examples of Python apps interfacing to the cloud housed on a Linode. The open source SIMPL toolkit provides a very lean Send/Receive/Reply (QNX style) messaging library. A SIMPL application consists of 2 or more interacting SIMPL modules. A SIMPL module can be written in any number of supported languages including Python, C, C++, JAVA, Tcl/Tk or PHP. More importantly they can be mixed in any given SIMPL application. Through the use of generic surrogates, SIMPL modules are "naturally" cloud aware often without even a recompile. In this tutorial a SIMPL receiver module (written in C) is exported unchanged to the Linode cloud. Examples of Python modules connecting to this Linode hosted module via the SIMPL TCP/IP surrogates, SIMPL tclSurrogate daemon, and Python-CGI are all illustrated. bob From ian.g.kelly at gmail.com Thu Dec 15 16:22:37 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 15 Dec 2011 14:22:37 -0700 Subject: How to generate "a, b, c, and d"? In-Reply-To: <4EEA41C5.60107@tim.thechases.com> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA35F8.4090502@tim.thechases.com> <4EEA3A47.5020106@stoneleaf.us> <4EEA41C5.60107@tim.thechases.com> Message-ID: On Thu, Dec 15, 2011 at 11:51 AM, Tim Chase wrote: > For the fun/challenge? ?Because you have a REALLY big data source that you > don't want to keep in memory (in addition the resulting string)? If you have that much data, then I question why you would want to build such a large human-readable list in the first place. Nobody is going to want to read that no matter how you format it. From tjreedy at udel.edu Thu Dec 15 18:15:01 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Dec 2011 18:15:01 -0500 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <4f2f6742-b1b2-4605-8004-b6aa431b8513@k5g2000pra.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4f2f6742-b1b2-4605-8004-b6aa431b8513@k5g2000pra.googlegroups.com> Message-ID: On 12/15/2011 6:04 AM, rusi wrote: > On Dec 15, 3:58 pm, Chris Angelico wrote: >> On Thu, Dec 15, 2011 at 9:47 PM, Robert Kern wrote: >>> 42 = 2 mod 5 >>> 2 = 42 mod 5 >> >> It might make more sense to programmers if you think of it as written: >> >> 42 = 2, mod 5 >> 2 = 42, mod 5 Better, using ascii text, would be 42 =mod5 2 where =mod is a parameterized equivalence relation that is coarser than = (which is =mod-infinity). divmod(a,inf) = 0,a. =mod1 is the most coarse relation in that it make every count equivalent. divmod(a,1) = a,1. > For the record I should say that the guy who taught me abstract > algebra, said about as much: > He said that the notation > a == b mod n > should be written as > a ==n b > (read the == as 3 horizontal lines and the n as a subscript) The 3 horizontal line symbol is often used for equivalence relations other than =. -- Terry Jan Reedy From passiday at gmail.com Thu Dec 15 18:20:34 2011 From: passiday at gmail.com (Passiday) Date: Thu, 15 Dec 2011 15:20:34 -0800 (PST) Subject: Can't get Apache2 tp process py (Ubuntu 11.10) In-Reply-To: <1544851.511.1323964197163.JavaMail.geo-discussion-forums@vbxw7> References: <1544851.511.1323964197163.JavaMail.geo-discussion-forums@vbxw7> Message-ID: <22446792.35.1323991234400.JavaMail.geo-discussion-forums@yqkx20> Ok, figured this out by myself. There was an apache config conflict, and the config without mod_python enabled took over the config with mod_python enabled. From jdjodrey at yahoo.com Thu Dec 15 19:20:49 2011 From: jdjodrey at yahoo.com (Joshua Jodrey) Date: Thu, 15 Dec 2011 16:20:49 -0800 (PST) Subject: Python Interpreter Error with PyDev (Eclipse IDE) Message-ID: <1323994849.34934.YahooMailNeo@web120504.mail.ne1.yahoo.com> Hi, This is my first email to this list, so I apologize if there's a better mailing-list for this type of question. I'm looking to get started with python development, so I downloaded Python 3.2.2 and PyDev IDE for Eclipse. ?When I go to configure my interpreter for a new python project, Windows gives me the following error: "python.exe has stopped working" and then Eclipse shows "Error getting info on interpreter "See error log for details. No output was in the standard output when trying to create the interpreter info. The error output contains:>>Fatal Python error: Py_Initialize: unable to load the file system codec ? File "C:\csvn\Python25\\lib\encodings\__init__.py", line 120 ? ? raise CodecRegistryError,\ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^ SyntaxError: invalid syntax This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. <<" Does anybody have any idea what this means or what I'm doing wrong? ?I've tried?re-installing?Python, but still can't get it to work. And again, if I'm in the wrong place, please direct me to the proper mailing-list or forum. Thanks! Josh P.S. I'm not?subscribed?to this mailer, so please include me in your replies! -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Thu Dec 15 19:39:17 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Dec 2011 19:39:17 -0500 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4ee97f20$0$11091$c3e8da3@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: On 12/15/2011 12:01 AM, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: > >> On 12/14/2011 3:01 AM, Steven D'Aprano wrote: >>> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: >>> >>>> To complement what Eric says below: The with statement is looking for >>>> an instance *method*, which by definition, is a function attribute of >>>> a *class* (the class of the context manager) that takes an instance of >>>> the class as its first parameter. Note in the above that I am talking about *instance methods*. >>> I'm not sure that is correct... I don't think that there is anything >>> "by definition" about where methods live. Since you are disagreeing with my statement that *instance methods* are class attributes, you had better be claiming that *instance methods* can live elsewhere, else your statement has no sense. >> From the Python glossary: >> "method: A function which is defined inside a class body." >> >> That is actually a bit too narrow, as a function can be added to the >> class after it is defined. But the point then is that it is treated as >> if defined inside the class body. Actually, I agree with Greg that rewording like he or I have suggested would be a good idea. > So I'm happy for the glossary entry to stay as is, because complicating > it would confuse the average coder more than it would educate them. > But the definition as given is strictly wrong, because it fails to > capture the essence of what distinguishes a method from other objects. It > mistakes *how* you normally create a method for *what* a method is, a > little like defining "a hamburger is a foodstuff you get from McDonalds > by giving them money". > > Here are three ways that the definition fails: > > (1) You can create a function inside a class, and it remains a function, > so long as the class constructor (metaclass) never gets to build a method > object from the function. It is easy to do: just hide it inside a wrapper > object. > >>>> class FunctionInsideClass(object): > ... def func(x, y): # define a function inside a class > ... return x + 2*y > ... print(type(func)) # confirm is actually is a function > ... attr = (func,) # hide it from the metaclass > ... del func The function is not an attribute of the class, so my revised definition does not fail. > >>>> print(type(FunctionInsideClass.attr[0])) > > (2) Instead of hiding the function from the metaclass, you can change the > metaclass to something which doesn't make methods out of functions. I > won't show an example, because it's tricky to get right (or at least *I* > find metaclasses tricky). The default metaclass does not 'make methods out of functions'. Rather, functions that are attributes of an instance of 'type' are treated as methods *when accessed* via an instance of that instance. In Py 2, they were wrapped as unbound methods when accessed via the class, but no longer in Py 3, which simplifies things. Now, perhaps you can define a metaclass that disables method behavior, but practically everything one say about normal Python functioning goes out the window when one invokes 'metaclasses' (which do not even have to be classes!). So I do not consider this relevant to the discussion. > (3) So the definition is too broad: you can have functions defined inside > classes that are not methods. But it is also too narrow: you can have > methods outside of classes. I'm not talking about bound and unbound > methods, but about *creating* the method from scratch outside of a class. > When you call the method constructor directly, you can create a method > from a function defined outside of a class. > >>>> def func(self, a): > ... return self + a > ... >>>> type(func) # Definitely a function. > >>>> obj = types.MethodType(func, 42) As I explained before, the intended input of MethodType is an *instance method* and an instance of the class it is an attribute of. (Or, I suspect, a class method and class, which is why the appropriate check in each case should be outside the call. But I am sticking with instance methods here.) If so, the output is a *bound method*. In your example above, func is not an instance method and obj is not a bound method. It is simply an partially evaluated curried function or if you prefer, a bound function. Take you pick, or make up your own term, but it is NOT an instance method, which is the subject under discussion. So obj has nothing to do with the definition of instance method and whether I had any authority for the definition I gave to the OP to help him solve his problem. >>>> obj(23) # Works as expected. > 65 Yes, and I can think of three other ways to make an 'add42' function. >>>> type(obj) > So what? That means 'bound method', but since your input function to MethodType is not a method, its output is not a bound method. > So there's a method which has never been inside a class, and couldn't > even if you tried: int is a built-in immutable type. Calling it a 'method' when it is not even a bound method does not make it an instance method, which is the subject of discussion. > So what are methods? In Python, methods are wrappers around functions > which automatically pass the instance to the inner function object. These are bound methods. The instance methods are the functions wrapped. >>> Particularly not in Python where >>> instance methods can be attributes of the instance itself. >> >> This is access, not definition or actual location. > > Not so. In the example I gave, the method *really is* inside the > instance, stored in the instance __dict__ and not the class __dict__. Calling the object stored in the instance __dict__ a 'method' does not make it an instance method. >> The glossary entry go >> on to say: "If called as an attribute of an instance of that class, the >> method will get the instance object as its first argument (which is >> usually called self)." This does *not* happen if a callable is found in >> the instance-specific dictionary. > > That's right. Here you agree that instance methods are special because of where they are and how accessed, because that is what the glossary, with my comment, just said. > Methods are special not because of where they are, Here you disagree. >> An instance method is a function >> (callable) attribute of a class that gets special treatment when >> accessed (indirectly) through an instance of that class (or subclass >> thereof). > > Methods aren't functions at all, not in the isinstance sense. Please, I just specifically clarified that I meant function in the generic mathemetical callable sense. There is no single function class for there to be an 'isinstance sense'. 'Callable(f)' means 'hasattr(f, '__call__') [snip] >> 'types.MethodType' is the exposed name of the class the interpreter uses >> to create bound methods from a method and an instance of the class >> containing the method. I believe the interpreter does an isinstance >> check, but it must do that before calling the class, and not in the >> bound method constructor itself. In any case, a bound method is not a >> method. Not an instance method, which is the usual default meaning of 'method' when not qualified. Sorry if you missed that and got confused. >> In this case, the result is not really even a bound method, as the >> function argument is not a method, so we cannot even ask if the second >> arg is an instance of the function class container. MethodType is a >> special case of functools.partial, which was added later. You could have >> used the latter to the same effect. Or you could have used any old >> function that printed the same thing. > > Good grief. Is it really your argument that the types.MethodType isn't > actually the type of methods, Good grief. As I explained, it is the type of *bound methods*. When you feed it an instance method and an object of the method's class, it outputs a bound (instance) method. When you feed it a class method and the corresponding class, I presume it outputs a bound (class) method. These are the two ways the interpreter uses it. If a user such as you feeds it any old function and an object that has no relation to the function (other than its signature), then the result is a generic bound function and not specifically a bound method. > but a fake that lies about returning methods? It returns a bound method when you input a (instance/class) method, as the interpreter does in its routine operation. I am baffled that you are so insistent on confusing instance methods with bound instance methods and bound functions. I told the OP that he needed instance methods and what they are and that indeed is what they are and what he needs. -- Terry Jan Reedy From tjreedy at udel.edu Thu Dec 15 19:57:22 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Dec 2011 19:57:22 -0500 Subject: How to generate "a, b, c, and d"? In-Reply-To: <4EEA2DF2.9080107@mrabarnett.plus.com> References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA2DF2.9080107@mrabarnett.plus.com> Message-ID: On 12/15/2011 12:27 PM, MRAB wrote: > On 15/12/2011 16:48, Roy Smith wrote: >> I've got a list, ['a', 'b', 'c', 'd']. I want to generate the string, >> "a, b, c, and d" (I'll settle for no comma after 'c'). Is there some >> standard way to do this, handling all the special cases? >> >> [] ==> '' >> ['a'] ==> 'a' >> ['a', 'b'] ==> 'a and b' >> ['a', 'b', 'c', 'd'] ==> 'a, b, and c' >> >> It seems like the kind of thing django.contrib.humanize would handle, >> but alas, it doesn't. > > How about this: > > def and_list(items): > if len(items) <= 2: > return " and ".join(items) > > return ", ".join(items[ : -1]) + ", and " + items[-1] To avoid making a slice copy, last = items.pop() return ", ".join(items) + (", and " + last) I parenthesized the last two small items to avoid copying the long string twice with two appends. Even better is items[-1] = "and " + items[-1] return ", ".join(items) so the entire output is created in one operation with no copy. But I would only mutate the list if I started with items = list(iterable) where iterable was the input, so I was mutating a private copy. -- Terry Jan Reedy From tjreedy at udel.edu Thu Dec 15 20:02:05 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 15 Dec 2011 20:02:05 -0500 Subject: test for list equality In-Reply-To: <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> <61edc02c-4f86-45ef-82a1-61c7013003b4@t38g2000yqe.googlegroups.com> <616578.517.1323971973427.JavaMail.geo-discussion-forums@yqba2> Message-ID: On 12/15/2011 12:59 PM, Miki Tebeka wrote: >> My sort issue... as in this doesn't work >>>>> if x.sort == y.sort: > You're missing the () to make it a function call. > Also list.sort() returns none, it mutates the original list. > You can either > sorted(x) == sorted(y) > or > set(x) == set(y) or x.sort(); y.sort(); x == y -- Terry Jan Reedy From wuwei23 at gmail.com Thu Dec 15 21:14:14 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 15 Dec 2011 18:14:14 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: Eelco wrote: > To tie it back in with python language design; all the more reason not > to opt for pseudo-backwards compatibility. If python wants a remainder > function, call it 'remainder'. Not 'rem', not 'mod', and certainly not > '%'. Good luck with the PEP. > Its the more pythonic way; a self-describing name, rather than > poorly defined or poorly understood cryptology. "Although practicality beats purity." I'm still utterly agog that anyone finds the operator % confusing. From rosuav at gmail.com Thu Dec 15 21:42:17 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Dec 2011 13:42:17 +1100 Subject: How to generate "a, b, c, and d"? In-Reply-To: References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA2DF2.9080107@mrabarnett.plus.com> Message-ID: On Fri, Dec 16, 2011 at 11:57 AM, Terry Reedy wrote: > ?items[-1] = "and " + items[-1] > ?return ", ".join(items) This works only if you're sure there are at least two items, and if you don't mind two items coming out as "a, and b". ChrisA From python at mrabarnett.plus.com Thu Dec 15 21:58:40 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 16 Dec 2011 02:58:40 +0000 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EEAB3E0.80809@mrabarnett.plus.com> On 16/12/2011 02:14, alex23 wrote: > Eelco wrote: >> To tie it back in with python language design; all the more reason >> not to opt for pseudo-backwards compatibility. If python wants a >> remainder function, call it 'remainder'. Not 'rem', not 'mod', and >> certainly not '%'. Python has "def", "del", "int", "str", "len", and so on. "rem" or "mod" (Ada has both, I believe) would be in keeping with the language. > > Good luck with the PEP. > >> Its the more pythonic way; a self-describing name, rather than >> poorly defined or poorly understood cryptology. > > "Although practicality beats purity." > > I'm still utterly agog that anyone finds the operator % confusing. In financial circles it could be an operator for calculating percentages, eg. "5 % x" would be 5 percent of x. It's an oddity, but an established one. :-) From steve+comp.lang.python at pearwood.info Thu Dec 15 22:34:10 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Dec 2011 03:34:10 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4eeabc32$0$29979$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Dec 2011 05:35:55 -0800, Steve Howell wrote: > For the special methods like __enter__ and __exit__, the tricky part > isn't understanding what would happen once the methods were called; the > tricky part is getting them to be called in the first place, if they > were not declared inside the class or attached to the class. If you *must* have per-instance special methods, my advice is to use a bit of scaffolding like this: class Whatever: def __enter__(self, *args): try: enter = self.__dict__['__enter__'] except KeyError: do_something_else() # or just let the exception occur else: enter(*args) Otherwise just live with the limitation that you can override all methods per-instance *except* dunders, and design your application accordingly. -- Steven From rz1991 at foxmail.com Thu Dec 15 23:55:13 2011 From: rz1991 at foxmail.com (=?gbk?B?yO7vow==?=) Date: Fri, 16 Dec 2011 12:55:13 +0800 Subject: Help about Xlib Library in Python Message-ID: Hi, A question about Xlib Library in Python troubled me for several days and I finally found this email list. I hope someone could answer my question. I think it is easy for experienced user. I would like to write a small script to response my mouse click in root screen and write something in the terminal. My script is like this, but it does not work in my computer. from Xlib import X import Xlib.display def main(): display = Xlib.display.Display() root = display.screen().root root.change_attributes(event_mask= X.ButtonPressMask | X.ButtonReleaseMask) while True: event = root.display.next_event() print "1" if __name__ == "__main__": main() Any hints are welcome Thank you! Ruan zheng -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Fri Dec 16 00:01:57 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Dec 2011 16:01:57 +1100 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <4EEAB3E0.80809@mrabarnett.plus.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> Message-ID: On Fri, Dec 16, 2011 at 1:58 PM, MRAB wrote: > In financial circles it could be an operator for calculating > percentages, eg. "5 % x" would be 5 percent of x. > > It's an oddity, but an established one. :-) And I would be most sorry to see % renamed to mod in Python. "Hello, %s! My favourite number is %d." mod ("Fred",42) # This just looks wrong. ChrisA From deface.vr at gmail.com Fri Dec 16 00:16:37 2011 From: deface.vr at gmail.com (vengal b) Date: Thu, 15 Dec 2011 21:16:37 -0800 (PST) Subject: online data entry jobs Message-ID: <2c0d2aac-57fa-4a28-ab0c-2a70a7453857@n13g2000prf.googlegroups.com> online data entry jobs http://venuonlinejobs.blogspot.com From tjreedy at udel.edu Fri Dec 16 00:26:10 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 16 Dec 2011 00:26:10 -0500 Subject: How to generate "a, b, c, and d"? In-Reply-To: References: <9393353.282.1323967703697.JavaMail.geo-discussion-forums@vbyc2> <4EEA2DF2.9080107@mrabarnett.plus.com> Message-ID: On 12/15/2011 9:42 PM, Chris Angelico wrote: > On Fri, Dec 16, 2011 at 11:57 AM, Terry Reedy wrote: >> items[-1] = "and " + items[-1] >> return ", ".join(items) > > This works only if you're sure there are at least two items, and if > you don't mind two items coming out as "a, and b". Please read the context that you removed. The original second return line and my replacements come after if len(items) <= 2: return " and ".join(items) -- Terry Jan Reedy From wuwei23 at gmail.com Fri Dec 16 00:30:48 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 15 Dec 2011 21:30:48 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> Message-ID: On Dec 16, 3:01?pm, Chris Angelico wrote: > And I would be most sorry to see % renamed to mod in Python. > > "Hello, %s! My favourite number is %d." mod ("Fred",42) ? # This just > looks wrong. Finally we can give this operator a more fitting name - I propose 'inject' - and put an end to this insane desire to leverage off pre- existing knowledge of other languages. Furthermore, I suggest that no two languages should ever have identical semantics, just to avoid potential confusion. New concepts for all! From ian.g.kelly at gmail.com Fri Dec 16 01:08:02 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 15 Dec 2011 23:08:02 -0700 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <4EEAB3E0.80809@mrabarnett.plus.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> Message-ID: On Dec 15, 2011 8:01 PM, "MRAB" wrote: > Python has "def", "del", "int", "str", "len", and so on. "rem" or "mod" > (Ada has both, I believe) would be in keeping with the language. I think I would have to object to "rem" purely on the basis that it denotes comments in BASIC. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alec.taylor6 at gmail.com Fri Dec 16 01:30:41 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 16 Dec 2011 17:30:41 +1100 Subject: test for list equality In-Reply-To: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> Message-ID: Just for fun, use the Hungarian Algorithm (Python implementation: http://software.clapper.org/munkres/) On Fri, Dec 16, 2011 at 3:36 AM, noydb wrote: > I want to test for equality between two lists. ?For example, if I have > two lists that are equal in content but not in order, I want a return > of 'equal' -- dont care if they are not in the same order. ?In order > to get that equality, would I have to sort both lists regardless? ?if > yes, how (having issues with list.sort)? > > Another way i tried, that I think is kind-of roundabout is like > x = [2, 5, 1, 88, 9] > y = [5, 2, 9, 1, 88] > inBoth = list(set(x) & set(y)) > > and then test that list.count is equal between inBoth and x and/or y. > > > Any better suggestions? > > Thanks for any help! > -- > http://mail.python.org/mailman/listinfo/python-list From greg.ewing at canterbury.ac.nz Fri Dec 16 01:56:19 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 16 Dec 2011 19:56:19 +1300 Subject: =?UTF-8?B?UmU6IEVSUCB3aXRoIFN1cHBseSBjaGFpbiBtYW5hZ2VtZW50IChpbmM=?= =?UTF-8?B?bC4gUE9TKSBhbmQgQ3VzdG9tZXIgcmVsYXRpb25zaGlwIG1hbmFnZW1lbnQg4oCU?= =?UTF-8?B?IFdoYXQncyBhdmFpbGFibGU/?= In-Reply-To: References: Message-ID: <9l08cjFlaoU1@mid.individual.net> > On Thu, Dec 15, 2011 at 5:54 AM, Anurag Chourasia > wrote: > >>I am building a POS/CRM (Loyalty Management) system as well. Is it just me, or does the phrase "Loyalty Management" have a faintly ominous ring to it? -- Greg From ian.g.kelly at gmail.com Fri Dec 16 02:11:10 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Dec 2011 00:11:10 -0700 Subject: test for list equality In-Reply-To: References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> Message-ID: On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor wrote: > Just for fun, use the Hungarian Algorithm > > (Python implementation: http://software.clapper.org/munkres/) That's a pretty silly approach, but okay: def listequals(a, b): if len(a) != len(b): return False matrix = [[int(item_a != item_b) for item_b in b] for item_a in a] path = Munkres().compute(matrix) return sum(matrix[r][c] for (r, c) in path) == 0 From ian.g.kelly at gmail.com Fri Dec 16 02:32:11 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Dec 2011 00:32:11 -0700 Subject: test for list equality In-Reply-To: References: <45d59336-7e75-4865-8573-56742b37097c@s26g2000yqd.googlegroups.com> Message-ID: On Fri, Dec 16, 2011 at 12:11 AM, Ian Kelly wrote: > On Thu, Dec 15, 2011 at 11:30 PM, Alec Taylor wrote: >> Just for fun, use the Hungarian Algorithm >> >> (Python implementation: http://software.clapper.org/munkres/) > > That's a pretty silly approach, but okay: > > def listequals(a, b): > ? ?if len(a) != len(b): > ? ? ? ?return False > ? ?matrix = [[int(item_a != item_b) for item_b in b] for item_a in a] > ? ?path = Munkres().compute(matrix) > ? ?return sum(matrix[r][c] for (r, c) in path) == 0 Amendment -- it seems that Hungarian implementation fails on an empty matrix: def listequals(a, b): if len(a) == len(b) == 0: return True if len(a) != len(b): return False matrix = [[int(item_a != item_b) for item_b in b] for item_a in a] path = Munkres().compute(matrix) return sum(matrix[r][c] for (r, c) in path) == 0 From ulrich at dorda.net Fri Dec 16 03:52:11 2011 From: ulrich at dorda.net (Ulrich) Date: Fri, 16 Dec 2011 00:52:11 -0800 (PST) Subject: Class: @property -> .__dict__ Message-ID: Good morning, I wonder if someone could please help me out with the @property function as illustrated in the following example. class te(): def __init__(self): self.a = 23 @property def b(self): return 2 * self.a t = te() In [4]: t.a Out[4]: 23 In [5]: t.b Out[5]: 46 #works asexpected so far, now let's have a look into t.__dict__ In [6]: t.__dict__ Out[6]: {'a': 23} -> b does not show up. Could anyone please explain me why this does not work / how to get b into .__dict__ / hint me to an explanation? Thanks a lot in advance! Cheers, Ulrich From steve+comp.lang.python at pearwood.info Fri Dec 16 04:03:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Dec 2011 09:03:05 GMT Subject: Class: @property -> .__dict__ References: Message-ID: <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Dec 2011 00:52:11 -0800, Ulrich wrote: > Good morning, > > I wonder if someone could please help me out with the @property function > as illustrated in the following example. > > class te(): > def __init__(self): > self.a = 23 > @property > def b(self): > return 2 * self.a [...] > Could anyone please explain me why this does not work / how to get b > into .__dict__ / hint me to an explanation? b is a property object. Like any other assignment inside the body of the class, it is shared across all instances, so you won't find it in the instance's personal dict. You will find it in the shared class dict. t.__dict__['b'] # not found te.__dict__['b'] # will return the property object (By the way: it is the usual convention to start the name of a class with initial capital, so Te would be a better name.) To get something into the instance dict, you need to assign it onto the instance: t.x = 42 # puts 'x':42 into t.__dict__ -- Steven From ulrich.dorda at gmail.com Fri Dec 16 04:11:49 2011 From: ulrich.dorda at gmail.com (Ulrich) Date: Fri, 16 Dec 2011 01:11:49 -0800 (PST) Subject: Class: @property -> .__dict__ References: <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <3fe1021b-75ae-4e3d-a5b0-175ad00aefa4@i6g2000vbh.googlegroups.com> On Dec 16, 10:03?am, Steven D'Aprano wrote: > On Fri, 16 Dec 2011 00:52:11 -0800, Ulrich wrote: > > Good morning, > > > I wonder if someone could please help me out with the @property function > > as illustrated in the following example. > > > class te(): > > ? ? def __init__(self): > > ? ? ? ? self.a = 23 > > ? ? @property > > ? ? def b(self): > > ? ? ? ? return 2 * self.a > [...] > > Could anyone please explain me why this does not work / how to get b > > into .__dict__ / hint me to an explanation? > > b is a property object. Like any other assignment inside the body of the > class, it is shared across all instances, so you won't find it in the > instance's personal dict. You will find it in the shared class dict. > > t.__dict__['b'] ?# not found > te.__dict__['b'] ?# will return the property object > > (By the way: it is the usual convention to start the name of a class with > initial capital, so Te would be a better name.) > > To get something into the instance dict, you need to assign it onto the > instance: > > t.x = 42 ?# puts 'x':42 into t.__dict__ > > -- > Steven Hi Steven, Thanks a lot for your quick and helpful answer! This would imply that I have to search in the dict of the class and the dict of the instance. - works nicely. I wonder if there is somewhere a "merge of the two" already available. In the meantime, I came across dir() In [7]: dir(t) Out[7]: ['__doc__', '__init__', '__module__', 'a', 'b'] This seems to return 'a' and 'b', but now crashes @property def attributelist(self): # find all attributes to the class that are of type numpy arrays: return [attr for attr in self.__dict__ if isinstance(getattr(self, attr), numpy.ndarray)] From ulrich.dorda at gmail.com Fri Dec 16 04:14:41 2011 From: ulrich.dorda at gmail.com (Ulrich) Date: Fri, 16 Dec 2011 01:14:41 -0800 (PST) Subject: Class: @property -> .__dict__ References: <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> <3fe1021b-75ae-4e3d-a5b0-175ad00aefa4@i6g2000vbh.googlegroups.com> Message-ID: <08b0a976-dd2a-4728-b865-4d04f4e5b7fb@n10g2000vbg.googlegroups.com> On Dec 16, 10:11?am, Ulrich wrote: > On Dec 16, 10:03?am, Steven D'Aprano > > > > > > > > > +comp.lang.pyt... at pearwood.info> wrote: > > On Fri, 16 Dec 2011 00:52:11 -0800, Ulrich wrote: > > > Good morning, > > > > I wonder if someone could please help me out with the @property function > > > as illustrated in the following example. > > > > class te(): > > > ? ? def __init__(self): > > > ? ? ? ? self.a = 23 > > > ? ? @property > > > ? ? def b(self): > > > ? ? ? ? return 2 * self.a > > [...] > > > Could anyone please explain me why this does not work / how to get b > > > into .__dict__ / hint me to an explanation? > > > b is a property object. Like any other assignment inside the body of the > > class, it is shared across all instances, so you won't find it in the > > instance's personal dict. You will find it in the shared class dict. > > > t.__dict__['b'] ?# not found > > te.__dict__['b'] ?# will return the property object > > > (By the way: it is the usual convention to start the name of a class with > > initial capital, so Te would be a better name.) > > > To get something into the instance dict, you need to assign it onto the > > instance: > > > t.x = 42 ?# puts 'x':42 into t.__dict__ > > > -- > > Steven > > Hi Steven, > > Thanks a lot for your quick and helpful answer! > > This would imply that I have to search in the dict of the class and > the dict of the instance. - works nicely. > > I wonder if there is somewhere a "merge of the two" already available. > > In the meantime, I came across dir() > In [7]: dir(t) > Out[7]: ['__doc__', '__init__', '__module__', 'a', 'b'] > > This seems to return 'a' and 'b', but now crashes > > @property > def attributelist(self): > ? ? ? ? # find all attributes to the class that are of type numpy > arrays: > ? ? ? ? return [attr for attr in self.__dict__ if > isinstance(getattr(self, attr), numpy.ndarray)] hi again, I must have hit the send accidently before finishing. This attributelist should return me all attributes of type numpy.ndarry. if I replace it to def attributelist(self): # find all attributes to the class that are of type numpy arrays: return [attr for attr in dir(self) if isinstance(getattr(self, attr), numpy.ndarray)] it crashes going into some kind of endless loop. Do you happen to have any idea? thanks again! cheers, ulrich From steve+comp.lang.python at pearwood.info Fri Dec 16 04:22:35 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Dec 2011 09:22:35 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: [...] After reading your post, I think I have worked out where our disagreement lines: you think that bound methods and instance methods are not the same thing, and that a function defined inside a class is different from a function outside of a class. For example, you say: > If so, the output is a *bound method*. In your example > above, func is not an instance method and obj is not a bound method. It > is simply an partially evaluated curried function or if you prefer, a > bound function. Take you pick, or make up your own term, but it is NOT > an instance method, and later on: >> So what are methods? In Python, methods are wrappers around functions >> which automatically pass the instance to the inner function object. > > These are bound methods. The instance methods are the functions wrapped. I am afraid you are mistaken. What you say may very well apply to other languages, but in Python, def creates functions no matter where you execute it. Always and without exception. I admit to an earlier mistake: I thought that conversion from function to method occurred once, when the class statement was executed, but I was mistaken. Re-reading Raymond Hettinger's excellent article on the descriptor protocol reminded me that methods are created on an as-needed basis, at runtime. Back to methods and def. So let's see what happens in Python 3.1: >>> def spam(self): # Clearly a function. ... pass ... >>> class K(object): ... def ham(self): # Allegedly an instance method ... pass ... According to your various statements, spam is a function and ham is an instance method. Merely storing a outside function inside a class doesn't create an instance method, it creates what you call a bound method (although I'm unclear what you think the difference is). So let's see what happens when we compare an alleged "bound method that is not an instance method" with an actual instance method: >>> K.spam = spam >>> type(K.spam) is type(K.ham) True Their types are identical. We get the same thing when we compare an actual function with a function object created inside a class, which you claim is actually an instance method: >>> type(spam) is type(K.__dict__['ham']) # Bypass descriptor protocol True Nor is there any difference in type between bound and unbound methods: they are both instance methods differing only in whether or not they have the first argument "self" available. Bound is an adjective, not part of the type: small method, obfuscated method, buggy method, or bound method. Python 3 no longer uses unbound methods, since they are functionally identical to the unwrapped function, so this snippet is from Python 2.6: py> k = K() py> type(k.ham) is type(K().ham) # bound vs bound True py> type(k.ham) is type(K.ham) # bound vs unbound True In Python, instance methods are wrappers around function objects; they are created on call, and generally do not exist *anywhere* until needed, or if you store a reference to them. >>> k = K() >>> a = k.ham >>> b = k.ham >>> a is b False Under normal circumstances, the only persistent object is the function, which you can extract from the (class or instance) __dict__ or the method wrapper: >>> a.__func__ is K.__dict__['ham'] True Methods are created by the descriptor protocol: when you use the normal a.b syntax to access K.__dict__['ham'], the metaclass calls the __get__ method of function ham. If you call it from an instance, you get a method object bound to that instance. If you call it from the class, in Python 2 you get a method object not bound to an instance, in Python 3 you get the function without a wrapper. [Aside: class methods and static methods also work the same way, via __get__ and the descriptor protocol, but behave differently. Class methods reuse the instance method type, which is somewhat confusing, and static methods just return the function.] So, what the hell does all this mean in practice? Most of the time, absolutely nothing. That's why I started this discussion with the disclaimer that I didn't think it was worth changing the (not quite right) definition of "method" you originally quoted. Most of the time, we only access methods via the instance.method syntax, which gives us an instance method. By the principle that if it quacks like a duck and swims like a duck, it is good enough to call it a duck, I'm happy to agree that ham here is a method: class K: def ham(self): pass That covers 99% of all use-cases and is good enough for most situations. But the OP ran into one of those edge cases in the 1%, where things are not so simple. He was attempting to create an instance method called "__exit__" and store it in the instance __dict__ instead of the class __dict__. This is a perfectly reasonable thing to do, but there are two gotchas to it: * The descriptor protocol doesn't get used for instance lookups. That's why you can't stick properties in an instance, only in the class. * CPython, and I believe Jython, don't look up special dunder methods like __exit__ on the instance, only on the class. So even if the OP manually created an instance method rather than relying on the descriptor protocol, his per-instance __exit__ would not be called without extra work. -- Steven From __peter__ at web.de Fri Dec 16 04:32:45 2011 From: __peter__ at web.de (Peter Otten) Date: Fri, 16 Dec 2011 10:32:45 +0100 Subject: Class: @property -> .__dict__ References: <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> <3fe1021b-75ae-4e3d-a5b0-175ad00aefa4@i6g2000vbh.googlegroups.com> <08b0a976-dd2a-4728-b865-4d04f4e5b7fb@n10g2000vbg.googlegroups.com> Message-ID: Ulrich wrote: > if I replace it to > def attributelist(self): > # find all attributes to the class that are of type numpy > arrays: > return [attr for attr in dir(self) if > isinstance(getattr(self, attr), numpy.ndarray)] > > it crashes going into some kind of endless loop. > > Do you happen to have any idea? dir(self) finds an attribute named "attributelist", getattr(self, "attributelist") then tries to calculate the value of that attribute, invokes dir(self) which finds an attribute named "attributelist" and so on ad infinitum or the stack overflows. Try (untested) @property def attributelist(self): return [attr for attr in dir(self) if attr != "attributelist" and isinstance(getattr(self, attr), numpy.ndarray)] to avoid the infinite recursion. From ulrich.eckhardt at dominolaser.com Fri Dec 16 04:45:22 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 16 Dec 2011 10:45:22 +0100 Subject: modifying a time.struct_time Message-ID: Hi! I'm trying to create a struct_time that is e.g. one year ahead or a month back in order to test some parsing/formatting code with different dates. Now, the straightforward approach is t = time.localtime() t.tm_year += 1 This fails with "TypeError: readonly attribute". This kind-of makes sense, as an immutable object allows you to use it as key in a dict. The second approach is this: l = list(t) # convert to a sequence l[0] += 1 # increment year t = time.struct_time(l) # convert to a struct_time This works but is ugly, because the code relies on the order inside the list and uses magic numbers to access them. The order is AFAICT not accessible programmatically but only documented, and not even in a way that makes clear that it is part of the API and as such actualy guaranteed. I could try to assert that the indices match using "if l[0] is t.tm_year", but this is still ugly. The next approach I tried was to simply create a derived class: class my_time(time.struct_time): pass This fails again with "TypeError: Error when calling the metaclass bases, type 'time.struct_time' is not an acceptable base type. I could try to encapsulate a struct_time and delegate attribute access to it in order to do this, but it also seems overkill. Also, using an immutable type as a baseclass and delegating access to members seems like hackery to me, prone to fail in situations where it is least expected. Then I tried duck typing. If it quacks like a duck, it better not be a crocodile! This looks like this: struct my_time(object): pass t = my_time() t.tm_year = 2012 t.tm_month = 12 t.tm... # other fields accordingly time.mktime(t) This fails with "TypeError: argument must be 9-item sequence, not my_time". I thought about using a collections.namedtuple, because a namedtuple is a tuple and therefore also a sequence, but that only leads me back to the problem that time.mktime() takes a sequence and the order of the sequence is not accessible programmatically. A last approach was to convert the thing to a dict and back. Alas, there is no conversion to a dict, otherwise d = dict(t) d['tm_year'] += 1 t = time.struct_time(d) would have been a straightforward approach. Does anyone have a suggestion how to solve this elegantly and pythonically? Also, what I'm wondering is if the lack of a clear way should be considered a bug or not. Cheers! Uli From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Fri Dec 16 05:02:51 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Fri, 16 Dec 2011 11:02:51 +0100 Subject: Class: @property -> .__dict__ In-Reply-To: References: Message-ID: Am 16.12.2011 09:52 schrieb Ulrich: > Could anyone please explain me why this does not work / how to get b > into .__dict__ / hint me to an explanation? b is not a data element of the particular instance, but it lives in the class. It is, roughly spoken, a "kind of method", just to be used without calling. These things are called "data descriptors" (http://docs.python.org/reference/datamodel.html#descriptors). t.b is internally reflected to te.__dict__['b'].__get__(t, te) in order to call the __get__ method of the property object. This is done on every call and the value is, in your case, derived from the instance value a. HTH, Thomas From hoogendoorn.eelco at gmail.com Fri Dec 16 05:25:59 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Fri, 16 Dec 2011 02:25:59 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> Message-ID: <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> On Dec 16, 6:30?am, alex23 wrote: > On Dec 16, 3:01?pm, Chris Angelico wrote: > > > And I would be most sorry to see % renamed to mod in Python. > > > "Hello, %s! My favourite number is %d." mod ("Fred",42) ? # This just > > looks wrong. > > Finally we can give this operator a more fitting name - I propose > 'inject' - and put an end to this insane desire to leverage off pre- > existing knowledge of other languages. > > Furthermore, I suggest that no two languages should ever have > identical semantics, just to avoid potential confusion. > > New concepts for all! Dont get me started on that one. Its that I never work with strings... 'leverage of pre-existing knowledge'... I would hardly call the particular names of functions the knowledge about a language. The only argument that bears any weight with me is backwards compatibility with itself. Pseudo-backwards compatibility with other languages, I couldnt not care less for. From mazen.harake at gmail.com Fri Dec 16 05:32:13 2011 From: mazen.harake at gmail.com (Mazen Harake) Date: Fri, 16 Dec 2011 11:32:13 +0100 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: Hi, Easiest way is to change the time to seconds, add as many seconds as a year/month/week/day/hour/minutes represent and then transform it back. E.g. >>> time.time() 1324031491.026137 >>> time.time() + 3600 # Add an hour 1324035105.082003 >>> time.gmtime(time.time() + 3600) time.struct_time(tm_year=2011, tm_mon=12, tm_mday=16, tm_hour=11, tm_min=31, tm_sec=57, tm_wday=4, tm_yday=350, tm_isdst=0) >>> On 16 December 2011 10:45, Ulrich Eckhardt wrote: > Hi! > > I'm trying to create a struct_time that is e.g. one year ahead or a month > back in order to test some parsing/formatting code with different dates. > > Now, the straightforward approach is > > ?t = time.localtime() > ?t.tm_year += 1 > > This fails with "TypeError: readonly attribute". This kind-of makes sense, > as an immutable object allows you to use it as key in a dict. > > > The second approach is this: > > ?l = list(t) # convert to a sequence > ?l[0] += 1 # increment year > ?t = time.struct_time(l) # convert to a struct_time > > This works but is ugly, because the code relies on the order inside the list > and uses magic numbers to access them. The order is AFAICT not accessible > programmatically but only documented, and not even in a way that makes clear > that it is part of the API and as such actualy guaranteed. I could try to > assert that the indices match using "if l[0] is t.tm_year", but this is > still ugly. > > > The next approach I tried was to simply create a derived class: > > ?class my_time(time.struct_time): > ? ? ?pass > > This fails again with "TypeError: Error when calling the metaclass bases, > type 'time.struct_time' is not an acceptable base type. I could try to > encapsulate a struct_time and delegate attribute access to it in order to do > this, but it also seems overkill. Also, using an immutable type as a > baseclass and delegating access to members seems like hackery to me, prone > to fail in situations where it is least expected. > > > Then I tried duck typing. If it quacks like a duck, it better not be a > crocodile! This looks like this: > > ?struct my_time(object): pass > ?t = my_time() > ?t.tm_year = 2012 > ?t.tm_month = 12 > ?t.tm... # other fields accordingly > ?time.mktime(t) > > This fails with "TypeError: argument must be 9-item sequence, not my_time". > I thought about using a collections.namedtuple, because a namedtuple is a > tuple and therefore also a sequence, but that only leads me back to the > problem that time.mktime() takes a sequence and the order of the sequence is > not accessible programmatically. > > > A last approach was to convert the thing to a dict and back. Alas, there is > no conversion to a dict, otherwise > > ?d = dict(t) > ?d['tm_year'] += 1 > ?t = time.struct_time(d) > > would have been a straightforward approach. > > > Does anyone have a suggestion how to solve this elegantly and pythonically? > Also, what I'm wondering is if the lack of a clear way should be considered > a bug or not. > > > Cheers! > > Uli > > -- > http://mail.python.org/mailman/listinfo/python-list From hoogendoorn.eelco at gmail.com Fri Dec 16 05:36:50 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Fri, 16 Dec 2011 02:36:50 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2a747b91-23bd-4d84-87f7-96609ac3c1dd@ct2g2000vbb.googlegroups.com> On Dec 16, 3:58?am, MRAB wrote: > On 16/12/2011 02:14, alex23 wrote: > > > Eelco ?wrote: > >> To tie it back in with python language design; all the more reason > >> not to opt for pseudo-backwards compatibility. If python wants a > >> remainder function, call it 'remainder'. Not 'rem', not 'mod', and > >> certainly not '%'. > > Python has "def", "del", "int", "str", "len", and so on. "rem" or "mod" > (Ada has both, I believe) would be in keeping with the language. def and del are keywords, and thus in another league. Having shorthand notation for types is somewhat defensible, though I believe I would prefer a more verbose form there too; how often to you encounter these in python anyway? len is a bit of an eeysore to me too; I understand having it as a builtin is a matter of optimization or something, but I do wish we would be given the option of just saying list.length > > Good luck with the PEP. > > >> Its the more pythonic way; a self-describing name, rather than > >> poorly defined or poorly understood cryptology. > > > "Although practicality beats purity." > > > I'm still utterly agog that anyone finds the operator % confusing. > > In financial circles it could be an operator for calculating > percentages, eg. "5 % x" would be 5 percent of x. > > It's an oddity, but an established one. :-) Well yes, thats the only argument ive heard so far that resonated with me. These syntax details are not a very big deal, and backwards compatibility with yourself is quite a big deal. Its nice to keep 'what ought to have been done' and 'what ought we to do' seperate in such discussions. Im not sure we ought to change these syntax details (I mean relating to mod and such), but I am quite sure of what I would have done if I could go back in time. From rosuav at gmail.com Fri Dec 16 05:44:42 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 16 Dec 2011 21:44:42 +1100 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: On Fri, Dec 16, 2011 at 8:45 PM, Ulrich Eckhardt wrote: > I'm trying to create a struct_time that is e.g. one year ahead or a month > back in order to test some parsing/formatting code with different dates. Do you need it to be one exact calendar year, or would it make sense to add/subtract integers from a Unix time? t = time.time() + 365*86400 # Not actually a year ahead, it's 365 days ahead t = time.localtime(t) # if you want a struct_time ChrisA From steve+comp.lang.python at pearwood.info Fri Dec 16 05:44:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 16 Dec 2011 10:44:49 GMT Subject: modifying a time.struct_time References: Message-ID: <4eeb2120$0$29979$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Dec 2011 10:45:22 +0100, Ulrich Eckhardt wrote: > Hi! > > I'm trying to create a struct_time that is e.g. one year ahead or a > month back in order to test some parsing/formatting code with different > dates. [...] > The second approach is this: > > l = list(t) # convert to a sequence > l[0] += 1 # increment year > t = time.struct_time(l) # convert to a struct_time > > This works but is ugly, because the code relies on the order inside the > list and uses magic numbers to access them. The order is AFAICT not > accessible programmatically but only documented, and not even in a way > that makes clear that it is part of the API and as such actualy > guaranteed. I could try to assert that the indices match using "if l[0] > is t.tm_year", but this is still ugly. Feel free to propose a feature enhancement to time.struct_time, but the order of the fields is stable and won't change. So ugly or not, that way is guaranteed to work. [...] > Then I tried duck typing. If it quacks like a duck, it better not be a > crocodile! This looks like this: > > struct my_time(object): pass "struct"? [...] > Does anyone have a suggestion how to solve this elegantly and > pythonically? Also, what I'm wondering is if the lack of a clear way > should be considered a bug or not. Not a bug, but it does seem a very old and inelegant API more suited to hairy C programmers gathered around a smokey fire in a cave chewing on old dinosaur bones, and not worthy of space-age Python coders flying around on anti-gravity belts. -- Steven From mail at timgolden.me.uk Fri Dec 16 05:50:19 2011 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 16 Dec 2011 10:50:19 +0000 Subject: modifying a time.struct_time In-Reply-To: <4eeb2120$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4eeb2120$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EEB226B.7080000@timgolden.me.uk> On 16/12/2011 10:44, Steven D'Aprano wrote: [ on time.struct_time ] > Not a bug, but it does seem a very old and inelegant API more suited to > hairy C programmers gathered around a smokey fire in a cave chewing on > old dinosaur bones, and not worthy of space-age Python coders flying > around on anti-gravity belts. +1 QOTW TJG From richa.sinha at globallogic.com Fri Dec 16 05:54:34 2011 From: richa.sinha at globallogic.com (Richa Sinha) Date: Fri, 16 Dec 2011 16:24:34 +0530 Subject: GlobalLogic PYTHON/JAVA positions Message-ID: Hi All , Trust you doing well.. GlobalLogic is looking for highly skilled *Python Developers/Sr.Developers* , *JAVA DEVELOPERS/Sr. Developers* for one of our strategic engagements with * GOOGLE. * Following are the details: > >> *Python Developer(s)/Sr. Developers* >> >> >> >> Experience: 1-5 Yrs >> >> Min Qualification: BS/BE/B.Tech/MCA Regular >> >> Job Location: Client site- Gurgaon (India) Google office >> > Positions: 20 >> >> * * >> >> *Requisites:* >> >> >> - * * OOPS concepts in Python >> - Exposure to python as a language , not only as a script >> - Good knowledge of File handling and XML parsing >> - Good to have experience in Multithreading in python. >> - Usage of inbuilt data types. >> - Implementation Of various design patterns >> - Should have Worked On python specific packages >> - Should have Worked On python specific frameworks like >> django, zope, >> - Exposure to middlewares, decorators, etc. >> - He should have worked on web apps instead of desktop apps. >> >> For Python: Send updated CV to richa.sinha at globallogic.com > * >> * >> >> *JAVA Positions: * >> >> * >> * >> >> *Min Qualification*: BS/BE/B.Tech/MCA or equivalent. >> >> *Location* ? Client site- Gurgaon (India) Google office >> >> *Positions: 10* >> *Experience*: Total work exp should be 2 - 5 yrs >> >> - Work experience and relevant experience in internet architecture >> and software development. >> - Fluency in at least two of the following languages: Java, >> JavaScript. >> - Expertise in object-oriented programming and design patterns >> (primarily MVC). >> - Hands on experience in database design, SQL and any ORM tool like >> Hibernate/JDO etc. >> - Experience in TDD based developmen. >> >> >> *Desired Skills*: >> >> - Innovativeness; ability to think outside the box . >> - Creativity and problem-solving ability . >> - Very good communication skills; must be able to gather requirements >> from potential users and discuss the requirements effectively with a >> technical team of developers . >> - Previous experience in developing web applications will be an added >> advantage. >> Working knowledge of linux environment is a plus. >> - Previous experience of working with Google tools/gadgets is a big >> plus. >> >> For Java : Send updated CV to ankit.bansal1 at globallogic.com * Do visit* www.globallogic.com* **Share this mail with your friends, alumni groups,communities etc., spread the great opportunity :) * -- Regards, Richa Sinha | Talent Acquisition *Leaders in Software R&D Services* *www.globallogic.com* ARGENTINA |CHINA | INDIA | ISRAEL | UKRAINE | UK | USA Office: +91-120-406-2378 http://www.youtube.com/watch?v=V4pwY800TII -------------- next part -------------- An HTML attachment was scrubbed... URL: From mohandayalank at gmail.com Fri Dec 16 06:20:36 2011 From: mohandayalank at gmail.com (D. Mohan M. Dayalan) Date: Fri, 16 Dec 2011 03:20:36 -0800 (PST) Subject: ckjdlkfnl,ndf,nfd,fndfnkdnk mmdlssdlndnll; k; as; lkds sjdsljdlskjdsl; kdslksdl; ddlk Message-ID: <5663ea95-735e-4859-a04f-8fcf3a7deb4d@24g2000prd.googlegroups.com> http;//123maza.com/48/moon670/ From andrea.crotti.0 at gmail.com Fri Dec 16 06:37:54 2011 From: andrea.crotti.0 at gmail.com (Andrea Crotti) Date: Fri, 16 Dec 2011 11:37:54 +0000 Subject: merging argparse parsers Message-ID: <4EEB2D92.10200@gmail.com> I would like to have something like merged_parser = LoggingParser() + OtherParser() Which should create an argument parser with all the options composed. Now for that I think I would need to subclass the argument, and something fancy with the overloading. The problem is that apparently there is no Argument class, but add_argument just change the object internal data structures. So the only alternative I see now is to do something like: def add_project_argument(parser): parser.add_argument('project_path', help='paths of the project to run') which is not very neat and modifies the object in place. Any other ideas? From waitmeforever at hotmail.com Fri Dec 16 07:54:54 2011 From: waitmeforever at hotmail.com (Yang Chun-Kai) Date: Fri, 16 Dec 2011 20:54:54 +0800 Subject: Localhost client-server simple ssl socket test program problems In-Reply-To: References: , Message-ID: > To: python-list at python.org > From: lists at cheimes.de > Subject: Re: Localhost client-server simple ssl socket test program problems > Date: Thu, 15 Dec 2011 20:45:43 +0100 > > Am 15.12.2011 20:09, schrieb Yang Chun-Kai: > > Server side error: > > > > File "views.py", line 17, in > > connstream = ssl.wrap_socket(newsocket, server_side=True, > > certfile="/etc/home/ckyang/PHA/testsslsocket/mypha.crt", > > keyfile="/etc/home/ckyang/PHA/testsslsocket/mypha.key", > > ssl_version=ssl.PROTOCOL_SSLv23) > > File "/usr/lib/python2.7/ssl.py", line 344, in wrap_socket > > ciphers=ciphers) > > File "/usr/lib/python2.7/ssl.py", line 119, in __init__ > > ciphers) > > ssl.SSLError: [Errno 336265218] _ssl..c:347: error:140B0002:SSL > > routines:SSL_CTX_use_PrivateKey_file:system lib > > This error is most likely caused by an encrypted private key. Python's > SSL lib doesn't support encrypted private keys for sockets. You can > encrypt the private key with >>>>>> >>>I generate the server private key with "openssl genrsa -out mypha.key 2048".>>>But this seems the standard command to do it.>>>How do I get the private key without encrypted ?>>>Or should I always do this and encrypt it again to get it decrypted ?>>>If I use the encrypted key and .csr to produce my certificate will that be different from decrypted key?>>>Thanks.>>>Kay>>> > openssl rsa -in /etc/home/ckyang/PHA/testsslsocket/mypha.key -out > /etc/home/ckyang/PHA/testsslsocket/mypha-nopasswd.key > > Christian > > > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From kj4eit at gmail.com Fri Dec 16 08:21:50 2011 From: kj4eit at gmail.com (Brad Tilley) Date: Fri, 16 Dec 2011 05:21:50 -0800 (PST) Subject: Make a small function thread safe Message-ID: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> Hey guys, I have a C++ function that I'd like to replicate (as closely as possible) in Python. Here's an example: 107 void increment_counter( unsigned int& counter ) 108 { 109 boost::mutex::scoped_lock lock( counter_lock ); 110 ++counter; 111 } A thread locks the function on entrance and then releases it on exit. What is the equivalent way to do this in Python? Many thanks! Brad From ulrich.eckhardt at dominolaser.com Fri Dec 16 08:32:03 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Fri, 16 Dec 2011 14:32:03 +0100 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: Am 16.12.2011 10:45, schrieb Ulrich Eckhardt: > I'm trying to create a struct_time that is e.g. one year ahead or a > month back in order to test some parsing/formatting code with different > dates. There is something I stumbled across that helps and that is the datetime module, which seems more reasonably pythonic, and which allows operations on dates like adding a Concerning the idea to use seconds, I'd rather not, because already the number of seconds per minute ranges from 60 to 62, and it doesn't get better with things like months (28...31 days), years (365...366 days) and all other types built upon them. Considering the question if the current state is buggy, I'm definitely +1 on it. I do understand that this API is not going to change, but explicitly documenting in "help(time)" that the order is fixed and possibly making the order programmatically available are not changes but useful additions, IMHO. Also, conversion from/to a dict and perhaps a link to the datetime module would have saved me some futile attempts. Thanks to all responders, I wish you a happy weekend! Uli From tim.wintle at teamrubber.com Fri Dec 16 08:33:14 2011 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Fri, 16 Dec 2011 13:33:14 +0000 Subject: Make a small function thread safe In-Reply-To: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> Message-ID: <1324042394.23801.6.camel@tim-laptop> On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote: > 107 void increment_counter( unsigned int& counter ) > 108 { > 109 boost::mutex::scoped_lock lock( counter_lock ); > 110 ++counter; > 111 } with counter_lock: counter += 1 ... where counter_lock is a threading.Lock instance. (see docs for the threading module) Tim From kj4eit at gmail.com Fri Dec 16 09:24:19 2011 From: kj4eit at gmail.com (Brad Tilley) Date: Fri, 16 Dec 2011 09:24:19 -0500 Subject: Make a small function thread safe In-Reply-To: <1324042394.23801.6.camel@tim-laptop> References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: On Fri, Dec 16, 2011 at 8:33 AM, Tim Wintle wrote: > On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote: > > 107 void increment_counter( unsigned int& counter ) > > 108 { > > 109 boost::mutex::scoped_lock lock( counter_lock ); > > 110 ++counter; > > 111 } > > > with counter_lock: > counter += 1 > > > ... where counter_lock is a threading.Lock instance. > > (see docs for the threading module) So something like this then: import threading shared_container = [] lock = threading.Lock() class thread_example( threading.Thread ): def __init__( self ): threading.Thread.__init__ (self) def run(t): lock shared_container.append(t.name) # main threads = [] for i in xrange(10): thread = thread_example() threads.append(thread) for thread in threads: thread.start() for item in shared_container: print item -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Fri Dec 16 09:24:53 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 17 Dec 2011 01:24:53 +1100 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: On Sat, Dec 17, 2011 at 12:32 AM, Ulrich Eckhardt wrote: > Concerning the idea to use seconds, I'd rather not, because already the > number of seconds per minute ranges from 60 to 62, and it doesn't get better > with things like months (28...31 days), years (365...366 days) and all other > types built upon them. Right, which is why I asked how important the difference between "365 days" and "1 year" is. Obviously if your goal is one entire calendar year, then you don't want to duplicate the work of figuring out how many seconds that is. ChrisA From kj4eit at gmail.com Fri Dec 16 09:30:26 2011 From: kj4eit at gmail.com (Brad Tilley) Date: Fri, 16 Dec 2011 09:30:26 -0500 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: On Fri, Dec 16, 2011 at 9:24 AM, Brad Tilley wrote: > > > On Fri, Dec 16, 2011 at 8:33 AM, Tim Wintle wrote: > >> On Fri, 2011-12-16 at 05:21 -0800, Brad Tilley wrote: >> > 107 void increment_counter( unsigned int& counter ) >> > 108 { >> > 109 boost::mutex::scoped_lock lock( counter_lock ); >> > 110 ++counter; >> > 111 } >> >> >> with counter_lock: >> counter += 1 >> >> >> ... where counter_lock is a threading.Lock instance. >> >> (see docs for the threading module) > > > > > So something like this then: > > import threading > > shared_container = [] > lock = threading.Lock() > > class thread_example( threading.Thread ): > > def __init__( self ): > threading.Thread.__init__ (self) > > def run(t): > lock > shared_container.append(t.name) > > # main > > threads = [] > for i in xrange(10): > thread = thread_example() > threads.append(thread) > > for thread in threads: > thread.start() > > for item in shared_container: > print item > > Or perhaps run should look like this instead: def run(t): lock.acquire() shared_container.append(t.name) lock.release() That seems a bit barbaric to me, not sure. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tim.wintle at teamrubber.com Fri Dec 16 09:36:20 2011 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Fri, 16 Dec 2011 14:36:20 +0000 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: <1324046180.26617.6.camel@tim-laptop> On Fri, 2011-12-16 at 09:24 -0500, Brad Tilley wrote: > So something like this then: > > import threading > > shared_container = [] > lock = threading.Lock() > > class thread_example( threading.Thread ): > > def __init__( self ): > threading.Thread.__init__ (self) > > def run(t): > lock > shared_container.append(t.name) should be: def run(t): with lock: shared_container.append(t.name) (or lock.acquire() and lock.release() as you mentioned) > # main > > threads = [] > for i in xrange(10): > thread = thread_example() > threads.append(thread) > > for thread in threads: > thread.start() you'll either need to lock again here, or join each thread: for thread in threads: thread.join() > for item in shared_container: > print item Tim From kj4eit at gmail.com Fri Dec 16 10:05:32 2011 From: kj4eit at gmail.com (Brad Tilley) Date: Fri, 16 Dec 2011 07:05:32 -0800 (PST) Subject: Make a small function thread safe References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: On Dec 16, 9:36?am, Tim Wintle wrote: > should be: > ? ? ? def run(t): > ? ? ? ? ? with lock: > ? ? ? ? ? ? ? shared_container.append(t.name) > > (or lock.acquire() and lock.release() as you mentioned) Thanks Tim. The with statement is closer to the C++ code (IMO) more so than the explicit acquire() and release() so I'll use that approach. I appreciate your advice. Brad From invalid at invalid.invalid Fri Dec 16 10:12:13 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Fri, 16 Dec 2011 15:12:13 +0000 (UTC) Subject: ERP with Supply chain management (incl. POS) and Customer relationship management ??? What's available? References: <9l08cjFlaoU1@mid.individual.net> Message-ID: On 2011-12-16, Gregory Ewing wrote: >> On Thu, Dec 15, 2011 at 5:54 AM, Anurag Chourasia >> wrote: >> >>>I am building a POS/CRM (Loyalty Management) system as well. > > Is it just me, or does the phrase "Loyalty Management" have > a faintly ominous ring to it? Yea, sounds a bit like Newspeak -- maybe it's what the secret police do under a dictatorship. -- Grant Edwards grant.b.edwards Yow! It's some people at inside the wall! This is gmail.com better than mopping! From emile at fenx.com Fri Dec 16 11:07:42 2011 From: emile at fenx.com (Emile van Sebille) Date: Fri, 16 Dec 2011 08:07:42 -0800 Subject: =?windows-1252?Q?Re=3A_ERP_with_Supply_chain_managemen?= =?windows-1252?Q?t_=28incl=2E_POS=29_and_Customer_relationship?= =?windows-1252?Q?_management_=97_What=27s_available=3F?= In-Reply-To: References: Message-ID: On 12/15/2011 6:43 AM Alec Taylor said... > On Thu, Dec 15, 2011 at 5:54 AM, Anurag Chourasia > wrote: >> Hi Alec, >> >> I am building a POS/CRM (Loyalty Management) system as well. >> >> So far, the best I could find to use as a base OpenERP? Emile From ian.g.kelly at gmail.com Fri Dec 16 11:23:47 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Dec 2011 09:23:47 -0700 Subject: Class: @property -> .__dict__ In-Reply-To: References: <4eeb0949$0$29979$c3e8da3$5496439d@news.astraweb.com> <3fe1021b-75ae-4e3d-a5b0-175ad00aefa4@i6g2000vbh.googlegroups.com> <08b0a976-dd2a-4728-b865-4d04f4e5b7fb@n10g2000vbg.googlegroups.com> Message-ID: On Fri, Dec 16, 2011 at 2:32 AM, Peter Otten <__peter__ at web.de> wrote: > Ulrich wrote: > >> if I replace it to >> def attributelist(self): >> ? ? ? ? ?# find all attributes to the class that are of type numpy >> arrays: >> ? ? ? ? ?return [attr for attr in dir(self) if >> isinstance(getattr(self, attr), numpy.ndarray)] >> >> it crashes going into some kind of endless loop. >> >> Do you happen to have any idea? > > dir(self) finds an attribute named "attributelist", getattr(self, > "attributelist") then tries to calculate the value of that attribute, > invokes dir(self) which finds an attribute named "attributelist" and so on > ad infinitum or the stack overflows. Try (untested) > > @property > def attributelist(self): > ? ?return [attr for attr in dir(self) if attr != "attributelist" and > ? ? ? ? ? ?isinstance(getattr(self, attr), numpy.ndarray)] > > to avoid the infinite recursion. Or remove attributelist from the class (it feels more like a generic function than a class property to me) or make it a method instead of a property. From gordon at panix.com Fri Dec 16 11:49:56 2011 From: gordon at panix.com (John Gordon) Date: Fri, 16 Dec 2011 16:49:56 +0000 (UTC) Subject: re.sub(): replace longest match instead of leftmost match? Message-ID: According to the documentation on re.sub(), it replaces the leftmost matching pattern. However, I want to replace the *longest* matching pattern, which is not necessarily the leftmost match. Any suggestions? I'm working with IPv6 CIDR strings, and I want to replace the longest match of "(0000:|0000$)+" with ":". But when I use re.sub() it replaces the leftmost match, even if there is a longer match later in the string. I'm also looking for a regexp that will remove leading zeroes in each four-digit group, but will leave a single zero if the group was all zeroes. Thanks! -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From jeanpierreda at gmail.com Fri Dec 16 11:56:32 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 16 Dec 2011 11:56:32 -0500 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: Message-ID: You could use re.finditer to find the longest match, and then replace it manually by hand (via string slicing). (a match is the longest if (m.end() - m.start()) is the largest -- so, max(re.finditer(...), key=lambda m: (m.end() = m.start())) -- Devin P.S. does anyone else get bothered by how it's slice.start and slice.stop, but match.start() and match.end() ? On Fri, Dec 16, 2011 at 11:49 AM, John Gordon wrote: > According to the documentation on re.sub(), it replaces the leftmost > matching pattern. > > However, I want to replace the *longest* matching pattern, which is > not necessarily the leftmost match. ?Any suggestions? > > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(0000:|0000$)+" with ":". ?But when I use re.sub() it replaces > the leftmost match, even if there is a longer match later in the string. > > I'm also looking for a regexp that will remove leading zeroes in each > four-digit group, but will leave a single zero if the group was all > zeroes. > > Thanks! > > -- > John Gordon ? ? ? ? ? ? ? ? ? A is for Amy, who fell down the stairs > gordon at panix.com ? ? ? ? ? ? ?B is for Basil, assaulted by bears > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-- Edward Gorey, "The Gashlycrumb Tinies" > > -- > http://mail.python.org/mailman/listinfo/python-list From python at mrabarnett.plus.com Fri Dec 16 12:36:51 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 16 Dec 2011 17:36:51 +0000 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: Message-ID: <4EEB81B3.6020600@mrabarnett.plus.com> On 16/12/2011 16:49, John Gordon wrote: > According to the documentation on re.sub(), it replaces the leftmost > matching pattern. > > However, I want to replace the *longest* matching pattern, which is > not necessarily the leftmost match. Any suggestions? > > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(0000:|0000$)+" with ":". But when I use re.sub() it replaces > the leftmost match, even if there is a longer match later in the string. > > I'm also looking for a regexp that will remove leading zeroes in each > four-digit group, but will leave a single zero if the group was all > zeroes. > How about this: result = re.sub(r"\b0+(\d)\b", r"\1", string) From rustompmody at gmail.com Fri Dec 16 12:38:56 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 16 Dec 2011 09:38:56 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> Message-ID: <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> On Dec 16, 3:25?pm, Eelco wrote: > Pseudo-backwards compatibility with other > languages, I couldnt not care less for. Double negations n Goedelian situations have interesting implications (tho here its triple) From nathan.alexander.rice at gmail.com Fri Dec 16 12:48:41 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Fri, 16 Dec 2011 12:48:41 -0500 Subject: Making the case for "typed" lists/iterators in python Message-ID: I realize this has been discussed in the past, I hope that I am presenting a slightly different take on the subject that will prove interesting. This is primarily motivated by my annoyance with using comprehensions in certain circumstances. Currently, if you want to perform successive transformations on the elements of a list, a couple of options: 1. Successive comprehensions: L2 = [X(e) for e in L1] L3 = [Y(e) for e in L2] L4 = [Z(e) for e in L3] or L2 = [e.X() for e in L1] This gets the job done and gives you access to all the intermediate values, but isn't very succinct, particularly if you are in the habit of using informative identifiers. 2. One comprehension: L2 = [Z(X(Y(e))) for e in L1] or L2 = [e.X().Y().Z() for e in L1] This gets the job done, but doesn't give you access to all the intermediate values, and tends to be pretty awful to read. Having "typed" lists let you take preexisting string/int/etc methods and expose them in a vectorized context and provides an easy way for developers to support both vectors and scalars in a single function (you could easily "fix" other people's functions dynamically to support both). Additionally, "typed" lists/iterators will allow improved code analysis and optimization. The PyPy people have already stated that they are working on implementing different strategies for lists composed of a single type, so clearly there is already community movement in this direction. Just compare the above examples to their type-aware counterparts: L2 = X(L1) L2 = L1.X() L2 = Z(Y(X(L1))) L2 = L1.X().Y().Z() Also, this would provide a way to clean up stuff like: "\n".join(l.capitalize() for l in my_string.split("\n")) into: my_string.split("\n").capitalize().join_this("\n") Before anyone gets up in arms at the idea of statically typed python, what I am suggesting here would be looser than that. Basically, I believe it would be a good idea in instances where it is known that a list of single type is going to be returned, to return a list subclass (for example, StringList, IntegerList, etc). To avoid handcuffing people with types, the standard list modification methods could be hooked so that if an object of an incorrect type is placed in the list, a warning is raised and the list converts to a generic object list. The only stumbling block is that you can't use __class__ to convert from stack types to heap types in CPython. My workaround for this would be to have a factory that creates generic "List" classes, modifying the bases to produce the correct behavior. Then, converting from a typed list to a generic object list would just be a matter of removing a member from the bases for a class. This of course basically kills the ability to perform type specific list optimization in CPython, but that isn't necessarily true for other implementations. The additional type information would be preserved for code analysis in any case. The case would be even simpler for generators and other iterators, as you don't have to worry about mutation. I'd like to hear people's thoughts on the subject. Currently we are throwing away useful information in many cases that could be used for code analysis, optimization and simpler interfaces. I believe that "typed" lists that get "demoted" to normal lists with a warning on out of type operations preserve this information while providing complete backwards compatibility and freedom. Nathan From ian.g.kelly at gmail.com Fri Dec 16 12:57:06 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Dec 2011 10:57:06 -0700 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: <4EEB81B3.6020600@mrabarnett.plus.com> References: <4EEB81B3.6020600@mrabarnett.plus.com> Message-ID: On Fri, Dec 16, 2011 at 10:36 AM, MRAB wrote: > On 16/12/2011 16:49, John Gordon wrote: >> >> According to the documentation on re.sub(), it replaces the leftmost >> matching pattern. >> >> However, I want to replace the *longest* matching pattern, which is >> not necessarily the leftmost match. ?Any suggestions? >> >> I'm working with IPv6 CIDR strings, and I want to replace the longest >> match of "(0000:|0000$)+" with ":". ?But when I use re.sub() it replaces >> the leftmost match, even if there is a longer match later in the string. >> >> I'm also looking for a regexp that will remove leading zeroes in each >> four-digit group, but will leave a single zero if the group was all >> zeroes. >> > How about this: > > result = re.sub(r"\b0+(\d)\b", r"\1", string) Close. pattern = r'\b0+([1-9a-f]+|0)\b' re.sub(pattern, r'\1', string, flags=re.IGNORECASE) Cheers, Ian From fabiofz at gmail.com Fri Dec 16 12:57:33 2011 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Fri, 16 Dec 2011 15:57:33 -0200 Subject: Python Interpreter Error with PyDev (Eclipse IDE) In-Reply-To: <1323994849.34934.YahooMailNeo@web120504.mail.ne1.yahoo.com> References: <1323994849.34934.YahooMailNeo@web120504.mail.ne1.yahoo.com> Message-ID: It seems you have a version of Python 2.5 in your system and when starting up Python 3.2.2 it's getting things from 2.5 (this would probably also happen on the command line). The usual suspect is that you defined a PYTHONROOT variable which is pointing to Python 2.5 and not to the python you're using... Cheers, Fabio On Thu, Dec 15, 2011 at 10:20 PM, Joshua Jodrey wrote: > Hi, > This is my first email to this list, so I apologize if there's a better > mailing-list for this type of question. > > I'm looking to get started with python development, so I downloaded Python > 3.2.2 and PyDev IDE for Eclipse. ?When I go to configure my interpreter for > a new python project, Windows gives me the following error: > > "python.exe has stopped working" > > and then Eclipse shows > > "Error getting info on interpreter > "See error log for details. > No output was in the standard output when trying to create the interpreter > info. > The error output contains:>>Fatal Python error: Py_Initialize: unable to > load the file system codec > ? File "C:\csvn\Python25\\lib\encodings\__init__.py", line 120 > ? ? raise CodecRegistryError,\ > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^ > SyntaxError: invalid syntax > > This application has requested the Runtime to terminate it in an unusual > way. > Please contact the application's support team for more information. > <<" > > Does anybody have any idea what this means or what I'm doing wrong? ?I've > tried?re-installing?Python, but still can't get it to work. > And again, if I'm in the wrong place, please direct me to the proper > mailing-list or forum. > > Thanks! > Josh > > P.S. I'm not?subscribed?to this mailer, so please include me in your > replies! > > -- > http://mail.python.org/mailman/listinfo/python-list > From ian.g.kelly at gmail.com Fri Dec 16 12:59:57 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 16 Dec 2011 10:59:57 -0700 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: <4EEB81B3.6020600@mrabarnett.plus.com> Message-ID: On Fri, Dec 16, 2011 at 10:57 AM, Ian Kelly wrote: > On Fri, Dec 16, 2011 at 10:36 AM, MRAB wrote: >> On 16/12/2011 16:49, John Gordon wrote: >>> >>> According to the documentation on re.sub(), it replaces the leftmost >>> matching pattern. >>> >>> However, I want to replace the *longest* matching pattern, which is >>> not necessarily the leftmost match. ?Any suggestions? >>> >>> I'm working with IPv6 CIDR strings, and I want to replace the longest >>> match of "(0000:|0000$)+" with ":". ?But when I use re.sub() it replaces >>> the leftmost match, even if there is a longer match later in the string. >>> >>> I'm also looking for a regexp that will remove leading zeroes in each >>> four-digit group, but will leave a single zero if the group was all >>> zeroes. >>> >> How about this: >> >> result = re.sub(r"\b0+(\d)\b", r"\1", string) > > Close. > > pattern = r'\b0+([1-9a-f]+|0)\b' > re.sub(pattern, r'\1', string, flags=re.IGNORECASE) Doh, that's still not quite right. pattern = r'\b0{1,3}([1-9a-f][0-9a-f]*|0)\b' re.sub(pattern, r'\1', string, flags=re.IGNORECASE) From roy at panix.com Fri Dec 16 13:05:37 2011 From: roy at panix.com (Roy Smith) Date: Fri, 16 Dec 2011 13:05:37 -0500 Subject: Making the case for "typed" lists/iterators in python References: Message-ID: In article , Nathan Rice wrote: > I'd like to hear people's thoughts on the subject. Currently we are > throwing away useful information in many cases that could be used for > code analysis, optimization and simpler interfaces. Most of this was TL:DNR, but I will admit I often wish for a better way to log intermediate values. For example, a common pattern in the code I'm working with now is functions that end in: return [Foo(x) for x in bunch_of_x_thingies] When something goes amiss and I want to debug the problem, I often transform that into: temp = [Foo(x) for x in bunch_of_x_thingies] logger.debug(temp) return temp It would be convenient to be able to get at and log the intermediate value without having to pull it out to an explicit temporary. From python at mrabarnett.plus.com Fri Dec 16 13:19:52 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 16 Dec 2011 18:19:52 +0000 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: <4EEB81B3.6020600@mrabarnett.plus.com> Message-ID: <4EEB8BC8.6040000@mrabarnett.plus.com> On 16/12/2011 17:57, Ian Kelly wrote: > On Fri, Dec 16, 2011 at 10:36 AM, MRAB wrote: >> On 16/12/2011 16:49, John Gordon wrote: >>> >>> According to the documentation on re.sub(), it replaces the leftmost >>> matching pattern. >>> >>> However, I want to replace the *longest* matching pattern, which is >>> not necessarily the leftmost match. Any suggestions? >>> >>> I'm working with IPv6 CIDR strings, and I want to replace the longest >>> match of "(0000:|0000$)+" with ":". But when I use re.sub() it replaces >>> the leftmost match, even if there is a longer match later in the string. >>> >>> I'm also looking for a regexp that will remove leading zeroes in each >>> four-digit group, but will leave a single zero if the group was all >>> zeroes. >>> >> How about this: >> >> result = re.sub(r"\b0+(\d)\b", r"\1", string) > > Close. > > pattern = r'\b0+([1-9a-f]+|0)\b' > re.sub(pattern, r'\1', string, flags=re.IGNORECASE) > Ah, OK. The OP said "digit" instead of "hex digit". That's my excuse. :-) From stefan_ml at behnel.de Fri Dec 16 13:23:27 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 16 Dec 2011 19:23:27 +0100 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: Nathan Rice, 16.12.2011 18:48: > I realize this has been discussed in the past, I hope that I am > presenting a slightly different take on the subject that will prove > interesting. This is primarily motivated by my annoyance with using > comprehensions in certain circumstances. > > Currently, if you want to perform successive transformations on the > elements of a list, a couple of options: > > 1. Successive comprehensions: > > L2 = [X(e) for e in L1] > L3 = [Y(e) for e in L2] > L4 = [Z(e) for e in L3] > or > L2 = [e.X() for e in L1] > > This gets the job done and gives you access to all the intermediate > values, but isn't very succinct, particularly if you are in the habit > of using informative identifiers. > > 2. One comprehension: > > L2 = [Z(X(Y(e))) for e in L1] > or > L2 = [e.X().Y().Z() for e in L1] > > This gets the job done, but doesn't give you access to all the > intermediate values, and tends to be pretty awful to read. > > Having "typed" lists let you take preexisting string/int/etc methods > and expose them in a vectorized context and provides an easy way for > developers to support both vectors and scalars in a single function > (you could easily "fix" other people's functions dynamically to > support both). Additionally, "typed" lists/iterators will allow > improved code analysis and optimization. The PyPy people have already > stated that they are working on implementing different strategies for > lists composed of a single type, so clearly there is already community > movement in this direction. > > Just compare the above examples to their type-aware counterparts: > > L2 = X(L1) > L2 = L1.X() > > L2 = Z(Y(X(L1))) > L2 = L1.X().Y().Z() What keeps you from implementing this? You don't need to change the language for it, just wrap the list in a class that overrides __getattr__() to return something that does the appropriate transformation for each element. I would be surprised if you needed more than a couple of lines of Python code for that. Stefan From rosuav at gmail.com Fri Dec 16 13:25:12 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 17 Dec 2011 05:25:12 +1100 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: On Sat, Dec 17, 2011 at 5:05 AM, Roy Smith wrote: > Most of this was TL:DNR, but I will admit I often wish for a better way > to log intermediate values. ?For example, a common pattern in the code > I'm working with now is functions that end in: > > ? return [Foo(x) for x in bunch_of_x_thingies] > > When something goes amiss and I want to debug the problem, I often > transform that into: > > ? ?temp = [Foo(x) for x in bunch_of_x_thingies] > ? ?logger.debug(temp) > ? ?return temp > > It would be convenient to be able to get at and log the intermediate > value without having to pull it out to an explicit temporary. tee = lambda func,arg: (func(arg),arg)[1] return tee(logger.debug,[Foo(x) for x in bunch_of_x_thingies]) ChrisA From roy at panix.com Fri Dec 16 13:36:17 2011 From: roy at panix.com (Roy Smith) Date: Fri, 16 Dec 2011 13:36:17 -0500 Subject: re.sub(): replace longest match instead of leftmost match? References: Message-ID: In article , John Gordon wrote: > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(0000:|0000$)+" with ":". But when I use re.sub() it replaces > the leftmost match, even if there is a longer match later in the string. > > I'm also looking for a regexp that will remove leading zeroes in each > four-digit group, but will leave a single zero if the group was all > zeroes. Having done quite a bit of IPv6 work, my opinion here is that you're trying to do The Wrong Thing. What you want is an IPv6 class which represents an address in some canonical form. It would have constructors which accept any of the RFC-2373 defined formats. It would also have string formatting methods to convert the internal form into any of these formats. Then, instead of attempting to regex your way directly from one string representation to another, you would do something like: addr_string = "FEDC:BA98:7654:3210:FEDC:BA98:7654:321" print IPv6(addr_string).to_short_form() From arnodel at gmail.com Fri Dec 16 13:38:05 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Fri, 16 Dec 2011 18:38:05 +0000 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: On 16 December 2011 18:25, Chris Angelico wrote: > tee = lambda func,arg: (func(arg),arg)[1] What a strange way to spell it! def tee(func, arg): func(arg) return arg -- Arnaud From rosuav at gmail.com Fri Dec 16 13:43:17 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 17 Dec 2011 05:43:17 +1100 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: On Sat, Dec 17, 2011 at 5:38 AM, Arnaud Delobelle wrote: > On 16 December 2011 18:25, Chris Angelico wrote: > >> tee = lambda func,arg: (func(arg),arg)[1] > > What a strange way to spell it! > > def tee(func, arg): > ? ?func(arg) > ? ?return arg I started with that version and moved to the lambda for compactness. But either way works. It's no more strange than the way some people omit the u from colour. :) ChrisA From nathan.alexander.rice at gmail.com Fri Dec 16 13:51:08 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Fri, 16 Dec 2011 13:51:08 -0500 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: Nothing stops me from implementing it, in fact it is VERY trivial to wrap member class methods onto a list subclass, and wrap functions to support vectorized behavior. The problem is that as soon as you hit anything outside your code that returns a list or iterator, everything gets blown away unless you explicitly wrap the return value, which entirely defeats the point. On Fri, Dec 16, 2011 at 1:23 PM, Stefan Behnel wrote: > Nathan Rice, 16.12.2011 18:48: > >> I realize this has been discussed in the past, I hope that I am >> presenting a slightly different take on the subject that will prove >> interesting. ?This is primarily motivated by my annoyance with using >> comprehensions in certain circumstances. >> >> Currently, if you want to perform successive transformations on the >> elements of a list, a couple of options: >> >> 1. Successive comprehensions: >> >> L2 = [X(e) for e in L1] >> L3 = [Y(e) for e in L2] >> L4 = [Z(e) for e in L3] >> or >> L2 = [e.X() for e in L1] >> >> This gets the job done and gives you access to all the intermediate >> values, but isn't very succinct, particularly if you are in the habit >> of using informative identifiers. >> >> 2. One comprehension: >> >> L2 = [Z(X(Y(e))) for e in L1] >> or >> L2 = [e.X().Y().Z() for e in L1] >> >> This gets the job done, but doesn't give you access to all the >> intermediate values, and tends to be pretty awful to read. >> >> Having "typed" lists let you take preexisting string/int/etc methods >> and expose them in a vectorized context and provides an easy way for >> developers to support both vectors and scalars in a single function >> (you could easily "fix" other people's functions dynamically to >> support both). ?Additionally, "typed" lists/iterators will allow >> improved code analysis and optimization. ?The PyPy people have already >> stated that they are working on implementing different strategies for >> lists composed of a single type, so clearly there is already community >> movement in this direction. >> >> Just compare the above examples to their type-aware counterparts: >> >> L2 = X(L1) >> L2 = L1.X() >> >> L2 = Z(Y(X(L1))) >> L2 = L1.X().Y().Z() > > > What keeps you from implementing this? You don't need to change the language > for it, just wrap the list in a class that overrides __getattr__() to return > something that does the appropriate transformation for each element. I would > be surprised if you needed more than a couple of lines of Python code for > that. > > Stefan > > -- > http://mail.python.org/mailman/listinfo/python-list From jeanpierreda at gmail.com Fri Dec 16 14:03:44 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 16 Dec 2011 14:03:44 -0500 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: > 2. One comprehension: > > L2 = [Z(X(Y(e))) for e in L1] > or > L2 = [e.X().Y().Z() for e in L1] I want to say that maybe this is better done by functions like map() (although, Python is missing a composition operator which might be handy, and mapping method calls isn't pretty). I don't know that it belongs as a method on lists, especially since that means you can't do the same thing on, for example, generators. Or I guess, the easy answer to that lattermost objection is to make it typed iterators rather than typed lists. -- Devin On Fri, Dec 16, 2011 at 12:48 PM, Nathan Rice wrote: > I realize this has been discussed in the past, I hope that I am > presenting a slightly different take on the subject that will prove > interesting. ?This is primarily motivated by my annoyance with using > comprehensions in certain circumstances. > > Currently, if you want to perform successive transformations on the > elements of a list, a couple of options: > > 1. Successive comprehensions: > > L2 = [X(e) for e in L1] > L3 = [Y(e) for e in L2] > L4 = [Z(e) for e in L3] > or > L2 = [e.X() for e in L1] > > This gets the job done and gives you access to all the intermediate > values, but isn't very succinct, particularly if you are in the habit > of using informative identifiers. > > 2. One comprehension: > > L2 = [Z(X(Y(e))) for e in L1] > or > L2 = [e.X().Y().Z() for e in L1] > > This gets the job done, but doesn't give you access to all the > intermediate values, and tends to be pretty awful to read. > > Having "typed" lists let you take preexisting string/int/etc methods > and expose them in a vectorized context and provides an easy way for > developers to support both vectors and scalars in a single function > (you could easily "fix" other people's functions dynamically to > support both). ?Additionally, "typed" lists/iterators will allow > improved code analysis and optimization. ?The PyPy people have already > stated that they are working on implementing different strategies for > lists composed of a single type, so clearly there is already community > movement in this direction. > > Just compare the above examples to their type-aware counterparts: > > L2 = X(L1) > L2 = L1.X() > > L2 = Z(Y(X(L1))) > L2 = L1.X().Y().Z() > > Also, this would provide a way to clean up stuff like: > > "\n".join(l.capitalize() for l in my_string.split("\n")) > > into: > > my_string.split("\n").capitalize().join_this("\n") > > Before anyone gets up in arms at the idea of statically typed python, > what I am suggesting here would be looser than that. ?Basically, I > believe it would be a good idea in instances where it is known that a > list of single type is going to be returned, to return a list subclass > (for example, StringList, IntegerList, etc). ?To avoid handcuffing > people with types, the standard list modification methods could be > hooked so that if an object of an incorrect type is placed in the > list, a warning is raised and the list converts to a generic object > list. ?The only stumbling block is that you can't use __class__ to > convert from stack types to heap types in CPython. ?My workaround for > this would be to have a factory that creates generic "List" classes, > modifying the bases to produce the correct behavior. ?Then, converting > from a typed list to a generic object list would just be a matter of > removing a member from the bases for a class. ?This of course > basically kills the ability to perform type specific list optimization > in CPython, but that isn't necessarily true for other implementations. > ?The additional type information would be preserved for code analysis > in any case. ?The case would be even simpler for generators and other > iterators, as you don't have to worry about mutation. > > I'd like to hear people's thoughts on the subject. ?Currently we are > throwing away useful information in many cases that could be used for > code analysis, optimization and simpler interfaces. ?I believe that > "typed" lists that get "demoted" to normal lists with a warning on out > of type operations preserve this information while providing complete > backwards compatibility and freedom. > > Nathan > -- > http://mail.python.org/mailman/listinfo/python-list From using4business at gmail.com Fri Dec 16 14:15:59 2011 From: using4business at gmail.com (using business) Date: Fri, 16 Dec 2011 11:15:59 -0800 (PST) Subject: ANGELINA JOLIE New SEX Tape RELEASED ( Must See Very EROTIC ) Message-ID: Angelina Jolie New SEX Tape. Just Download it and open it to see her new video http://www.fileserve.com/file/H6BqSbr/Angelina Jolie.rar From hoogendoorn.eelco at gmail.com Fri Dec 16 14:40:11 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Fri, 16 Dec 2011 11:40:11 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> Message-ID: <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> On 16 dec, 18:38, rusi wrote: > On Dec 16, 3:25?pm, Eelco wrote: > > > Pseudo-backwards compatibility with other > > languages, I couldnt not care less for. > > Double negations n Goedelian situations have interesting implications > (tho here its triple) Heh. Well at least my extra (unintended) negation is semantically consistent with the actual english usage of the phrase, which omits the negation completely :). (I could care less) But ill stick with trying to change one language at a time :). From mwilson at the-wire.com Fri Dec 16 15:14:39 2011 From: mwilson at the-wire.com (Mel Wilson) Date: Fri, 16 Dec 2011 15:14:39 -0500 Subject: Making the case for "typed" lists/iterators in python References: Message-ID: Chris Angelico wrote: > It's no more strange than the way some people omit the u from colour. :) Bonum Petronio Arbiteri, bonum mihi. Mel. From stefan_ml at behnel.de Fri Dec 16 15:16:22 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 16 Dec 2011 21:16:22 +0100 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: Nathan Rice, 16.12.2011 19:51: > Nothing stops me from implementing it, in fact it is VERY trivial to > wrap member class methods onto a list subclass, and wrap functions to > support vectorized behavior. The problem is that as soon as you hit > anything outside your code that returns a list or iterator, everything > gets blown away unless you explicitly wrap the return value, which > entirely defeats the point. The point you are trying to make, maybe. Stefan PS: note that you top-posted in your reply, you should try to avoid that. From ben+python at benfinney.id.au Fri Dec 16 15:17:15 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 17 Dec 2011 07:17:15 +1100 Subject: Making the case for "typed" lists/iterators in python References: Message-ID: <87hb10z2yc.fsf@benfinney.id.au> Roy Smith writes: > When something goes amiss and I want to debug the problem, I often > transform that into: > > temp = [Foo(x) for x in bunch_of_x_thingies] > logger.debug(temp) > return temp > > It would be convenient to be able to get at and log the intermediate > value without having to pull it out to an explicit temporary. What's wrong with that though? You're not ?pulling it out to? anything; you're binding a name to the value in order to do several things with it. Exactly what you say you want to do. It's explicit and clear. -- \ ?What we usually pray to God is not that His will be done, but | `\ that He approve ours.? ?Helga Bergold Gross | _o__) | Ben Finney From timothy.c.delaney at gmail.com Fri Dec 16 15:47:09 2011 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Sat, 17 Dec 2011 07:47:09 +1100 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: On 17 December 2011 02:05, Brad Tilley wrote: > On Dec 16, 9:36 am, Tim Wintle wrote: > > > should be: > > def run(t): > > with lock: > > shared_container.append(t.name) > > > > (or lock.acquire() and lock.release() as you mentioned) > > > Thanks Tim. The with statement is closer to the C++ code (IMO) more so > than the explicit acquire() and release() so I'll use that approach. I > appreciate your advice. Most definitely. The acquire/release code shown was erroneous, since it would not release if an exception was thrown. The with code is effectively equivalent to: lock.acquire() try: ... finally: lock.release() Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From gordon at panix.com Fri Dec 16 16:04:49 2011 From: gordon at panix.com (John Gordon) Date: Fri, 16 Dec 2011 21:04:49 +0000 (UTC) Subject: re.sub(): replace longest match instead of leftmost match? References: Message-ID: In Devin Jeanpierre writes: > You could use re.finditer to find the longest match, and then replace > it manually by hand (via string slicing). > (a match is the longest if (m.end() - m.start()) is the largest -- > so, max(re.finditer(...), key=3Dlambda m: (m.end() =3D m.start())) I ended up doing something similar: # find the longest match longest_match = '' for word in re.findall('((0000:?)+)', ip6): if len(word[0]) > len(longest_match): longest_match = word[0] # if we found a match, replace it with a colon if longest_match: ip6 = re.sub(longest_match, ':', ip6, 1) Thanks! -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From gordon at panix.com Fri Dec 16 16:06:06 2011 From: gordon at panix.com (John Gordon) Date: Fri, 16 Dec 2011 21:06:06 +0000 (UTC) Subject: re.sub(): replace longest match instead of leftmost match? References: <4EEB81B3.6020600@mrabarnett.plus.com> Message-ID: In Ian Kelly writes: > >>> I'm also looking for a regexp that will remove leading zeroes in each > >>> four-digit group, but will leave a single zero if the group was all > >>> zeroes. > pattern = r'\b0{1,3}([1-9a-f][0-9a-f]*|0)\b' > re.sub(pattern, r'\1', string, flags=re.IGNORECASE) Perfect. Thanks Ian! -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From gordon at panix.com Fri Dec 16 16:07:30 2011 From: gordon at panix.com (John Gordon) Date: Fri, 16 Dec 2011 21:07:30 +0000 (UTC) Subject: re.sub(): replace longest match instead of leftmost match? References: Message-ID: In Roy Smith writes: > Having done quite a bit of IPv6 work, my opinion here is that you're > trying to do The Wrong Thing. > What you want is an IPv6 class which represents an address in some > canonical form. It would have constructors which accept any of the > RFC-2373 defined formats. It would also have string formatting methods > to convert the internal form into any of these formats. > Then, instead of attempting to regex your way directly from one string > representation to another, you would do something like: > addr_string = "FEDC:BA98:7654:3210:FEDC:BA98:7654:321" > print IPv6(addr_string).to_short_form() This does sound like a more robust solution. I'll give it some thought. Thanks Roy! -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From python at mrabarnett.plus.com Fri Dec 16 16:36:34 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 16 Dec 2011 21:36:34 +0000 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: Message-ID: <4EEBB9E2.1070301@mrabarnett.plus.com> On 16/12/2011 21:04, John Gordon wrote: > In Devin Jeanpierre writes: > >> You could use re.finditer to find the longest match, and then replace >> it manually by hand (via string slicing). > >> (a match is the longest if (m.end() - m.start()) is the largest -- >> so, max(re.finditer(...), key=3Dlambda m: (m.end() =3D m.start())) > > I ended up doing something similar: > > # find the longest match > longest_match = '' > for word in re.findall('((0000:?)+)', ip6): > if len(word[0])> len(longest_match): > longest_match = word[0] > > # if we found a match, replace it with a colon > if longest_match: > ip6 = re.sub(longest_match, ':', ip6, 1) > For a simple replace, using re is probably overkill. The .replace method is a better solution: ip6 = longest_match.replace(ip6, ':', 1) From tjreedy at udel.edu Fri Dec 16 17:05:57 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 16 Dec 2011 17:05:57 -0500 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/16/2011 4:22 AM, Steven D'Aprano wrote: > On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: > [...] > > After reading your post, I think I have worked out where our disagreement > lines: you think that bound methods and instance methods are not the same > thing, Do you agree that an unbound method and a bound method are different? In Python, as indicated by the glossary entry, an unspecified 'method' is usually meant to be an unbound method. It is am important distinction and I do not see why you insist on confusing the two. > and that a function defined inside a class is different from a > function outside of a class. That, and your repetition of the same claim further on, is a insulting lie. Def statements always create functions. I have known that for 14 years since the first day I started with Python. I have never thought differently. If you actually think that I have, you are wrong. What I have said from my first response is that a function that is an attribute of a class, *whether defined in or outside the class*, gets special treatment when accessed via an instance of the class. And that is the truth. If you are ever interested in learning anything from me on this subject, re=read what I already wrote with a more open mind than you have so far. Otherwise, I am done. -- Terry Jan Reedy From lie.1296 at gmail.com Fri Dec 16 17:08:04 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 17 Dec 2011 09:08:04 +1100 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: On 12/17/2011 01:30 AM, Brad Tilley wrote: > Or perhaps run should look like this instead: > > def run(t): > lock.acquire() > shared_container.append(t.name ) > lock.release() > > That seems a bit barbaric to me, not sure. change that to: def run(t): with lock: shared_container.append(t.name ) the `with-statement` will call lock.acquire() and lock.release(). From tjreedy at udel.edu Fri Dec 16 17:21:14 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 16 Dec 2011 17:21:14 -0500 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: On 12/16/2011 1:05 PM, Roy Smith wrote: > I'm working with now is functions that end in: > > return [Foo(x) for x in bunch_of_x_thingies] > > When something goes amiss and I want to debug the problem, I often > transform that into: > > temp = [Foo(x) for x in bunch_of_x_thingies] > logger.debug(temp) > return temp > > It would be convenient to be able to get at and log the intermediate > value without having to pull it out to an explicit temporary. Decorate the function with @logreturn and you do not have to touch the function code. If the logging module does not now have such a decorator predefined (I simply do not know), perhaps it should. -- Terry Jan Reedy From tjreedy at udel.edu Fri Dec 16 17:26:03 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 16 Dec 2011 17:26:03 -0500 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: Message-ID: On 12/16/2011 1:36 PM, Roy Smith wrote: > What you want is an IPv6 class which represents an address in some > canonical form. It would have constructors which accept any of the > RFC-2373 defined formats. It would also have string formatting methods > to convert the internal form into any of these formats. > > Then, instead of attempting to regex your way directly from one string > representation to another, you would do something like: > > addr_string = "FEDC:BA98:7654:3210:FEDC:BA98:7654:321" > print IPv6(addr_string).to_short_form() There are at least 2 third-party IP classes in use. I would not be surprised if at least one of them does this. -- Terry Jan Reedy From jdjodrey at yahoo.com Fri Dec 16 17:40:04 2011 From: jdjodrey at yahoo.com (Joshua Jodrey) Date: Fri, 16 Dec 2011 14:40:04 -0800 (PST) Subject: Python Interpreter Error with PyDev (Eclipse IDE) In-Reply-To: References: <1323994849.34934.YahooMailNeo@web120504.mail.ne1.yahoo.com> Message-ID: <1324075204.65234.YahooMailNeo@web120506.mail.ne1.yahoo.com> Thanks Fabio! I looked at my environment variables, and the only one python related was PYTHONHOME, and that was set to C:\csvn\Python25. ?So I changed that to C:\Python32\ and I added an environment variable for PYTHONROOT and set that to the same value. ?It now seems to work! I must have had a some python directories laying around from an odd svn installation or something... Thanks again! ________________________________ From: Fabio Zadrozny To: Joshua Jodrey Cc: "python-list at python.org" Sent: Friday, December 16, 2011 12:57 PM Subject: Re: Python Interpreter Error with PyDev (Eclipse IDE) It seems you have a version of Python 2.5 in your system and when starting up Python 3.2.2 it's getting things from 2.5 (this would probably also happen on the command line). The usual suspect is that you defined a PYTHONROOT variable which is pointing to Python 2.5 and not to the python you're using... Cheers, Fabio On Thu, Dec 15, 2011 at 10:20 PM, Joshua Jodrey wrote: > Hi, > This is my first email to this list, so I apologize if there's a better > mailing-list for this type of question. > > I'm looking to get started with python development, so I downloaded Python > 3.2.2 and PyDev IDE for Eclipse. ?When I go to configure my interpreter for > a new python project, Windows gives me the following error: > > "python.exe has stopped working" > > and then Eclipse shows > > "Error getting info on interpreter > "See error log for details. > No output was in the standard output when trying to create the interpreter > info. > The error output contains:>>Fatal Python error: Py_Initialize: unable to > load the file system codec > ? File "C:\csvn\Python25\\lib\encodings\__init__.py", line 120 > ? ? raise CodecRegistryError,\ > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^ > SyntaxError: invalid syntax > > This application has requested the Runtime to terminate it in an unusual > way. > Please contact the application's support team for more information. > <<" > > Does anybody have any idea what this means or what I'm doing wrong? ?I've > tried?re-installing?Python, but still can't get it to work. > And again, if I'm in the wrong place, please direct me to the proper > mailing-list or forum. > > Thanks! > Josh > > P.S. I'm not?subscribed?to this mailer, so please include me in your > replies! > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Fri Dec 16 18:26:30 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 16 Dec 2011 15:26:30 -0800 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EEBD3A6.4030700@stoneleaf.us> Terry Reedy wrote: > On 12/16/2011 4:22 AM, Steven D'Aprano wrote: >> On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: >> [...] >> >> After reading your post, I think I have worked out where our disagreement >> lies: you think that bound methods and instance methods are not the same >> thing, > > Do you agree that an unbound method and a bound method are different? In > Python, as indicated by the glossary entry, an unspecified 'method' is > usually meant to be an unbound method. I think you two are in violent agreement as far as how Python is functioning, and the conflict is in the names given to the various pieces... I think a glossary would help (please correct me): function: callable code suite method: function that lives in a class unbound method: function that lives in a class bound method: callable wrapper around function that has been extracted from class that will supply the instance object to the function (note: Python does not save these, they are recreated at each lookup) and here is where I think you two diverge: instance method (Steven): a bound method that has been saved into the instance __dict__ (no matter how created) instance method (Terry): a function that must be looked up in the class Have I missed anything? Honestly-trying-learn-the-distinctions-ly yours, ~Ethan~ From greg.ewing at canterbury.ac.nz Fri Dec 16 18:49:17 2011 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Sat, 17 Dec 2011 12:49:17 +1300 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> Message-ID: <9l23ntFsibU1@mid.individual.net> Eelco wrote: > the actual english usage of the phrase, which omits > the negation completely :). (I could care less) No, that's the American usage. The English usage is "I couldn't care less", which has the advantage of actually making sense. -- Greg From hoogendoorn.eelco at gmail.com Fri Dec 16 19:00:23 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Fri, 16 Dec 2011 16:00:23 -0800 (PST) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> <9l23ntFsibU1@mid.individual.net> Message-ID: <2420abd7-7d91-4bc9-bb3b-d8ec1680ed0c@u32g2000yqe.googlegroups.com> On Dec 17, 12:49?am, Gregory Ewing wrote: > Eelco wrote: > > the actual english usage of the phrase, which omits > > the negation completely :). (I could care less) > > No, that's the American usage. The English usage is > "I couldn't care less", which has the advantage of > actually making sense. > > -- > Greg Oh thanks for clearing that up, never noticed a division along these lines. And yes, I agree; 'I couldnt care less' makes much more sense. 'I could care less' can only make sense if you interpret it sarcastically, as if omitting an 'oh wait, I cant', but that does not seem congruent with how its typically pronounced. Just another case of suboptimal language design; but where can you submit EEP's? From roy at panix.com Fri Dec 16 19:03:23 2011 From: roy at panix.com (Roy Smith) Date: Fri, 16 Dec 2011 19:03:23 -0500 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> <9l23ntFsibU1@mid.individual.net> <2420abd7-7d91-4bc9-bb3b-d8ec1680ed0c@u32g2000yqe.googlegroups.com> Message-ID: In article <2420abd7-7d91-4bc9-bb3b-d8ec1680ed0c at u32g2000yqe.googlegroups.com>, Eelco wrote: > And yes, I agree; 'I couldnt care less' makes much more sense. 'I > could care less' can only make sense if you interpret it > sarcastically, as if omitting an 'oh wait, I cant', but that does not > seem congruent with how its typically pronounced. "I care so little about the subject that I am unwilling to spend one of my precious apostrophes to properly express the sentiment" From ethan at stoneleaf.us Fri Dec 16 19:34:49 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 16 Dec 2011 16:34:49 -0800 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4EEBD3A6.4030700@stoneleaf.us> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEBD3A6.4030700@stoneleaf.us> Message-ID: <4EEBE3A9.9010706@stoneleaf.us> Ethan Furman wrote: > Terry Reedy wrote: >> On 12/16/2011 4:22 AM, Steven D'Aprano wrote: >>> On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: >>> [...] >>> >>> After reading your post, I think I have worked out where our >>> disagreement >>> lies: you think that bound methods and instance methods are not the same >>> thing, >> >> Do you agree that an unbound method and a bound method are different? >> In Python, as indicated by the glossary entry, an unspecified 'method' >> is usually meant to be an unbound method. > > I think you two are in violent agreement as far as how Python is > functioning, and the conflict is in the names given to the various > pieces... I think a glossary would help (please correct me): > > function: callable code suite > > method: function that lives in a class > > unbound method: function that lives in a class > > bound method: callable wrapper around function that has been extracted > from class that will supply the instance object to the function (note: > Python does not save these, they are recreated at each lookup) I think the above 'bound method' definition should be attributed to Terry, and Steven's follows: bound method: callable wrapper around any function that will accept an instance object as the first parameter, and the wrapper will supply said instance object when calling the function (and where/how function was created is irrelevent, as is where the wrapper is stored) > > > and here is where I think you two diverge: > > instance method (Steven): a bound method that has been saved into the > instance __dict__ (no matter how created) > > instance method (Terry): a function that must be looked up in the class > > > Have I missed anything? > > Honestly-trying-learn-the-distinctions-ly yours, > > ~Ethan~ From steve+comp.lang.python at pearwood.info Fri Dec 16 19:54:23 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2011 00:54:23 GMT Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> Message-ID: <4eebe83f$0$29979$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Dec 2011 11:40:11 -0800, Eelco wrote: > On 16 dec, 18:38, rusi wrote: >> On Dec 16, 3:25?pm, Eelco wrote: >> >> > Pseudo-backwards compatibility with other languages, I couldnt not >> > care less for. >> >> Double negations n Goedelian situations have interesting implications >> (tho here its triple) > > Heh. Well at least my extra (unintended) negation is semantically > consistent with the actual english usage of the phrase, which omits the > negation completely :). (I could care less) Oh please. "I could care less" is not English. That's American. Here in Australia, we follow the English practice of saying that we couldn't care less. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 16 20:26:24 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2011 01:26:24 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eebefc0$0$29979$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Dec 2011 17:05:57 -0500, Terry Reedy wrote: > On 12/16/2011 4:22 AM, Steven D'Aprano wrote: >> On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: [...] >> >> After reading your post, I think I have worked out where our >> disagreement lines: you think that bound methods and instance methods >> are not the same thing, > > Do you agree that an unbound method and a bound method are different? "Are different" or "are different types"? Bound and unbound methods in Python 2 are the same type. They are different in the sense that one is effectively a curry of the other, but they are implemented as a single type. So presumably the difference is a bound method has a slot filled and an unbound method doesn't, or some other implementation detail. In Python 3, unbound methods don't exist. > In > Python, as indicated by the glossary entry, an unspecified 'method' is > usually meant to be an unbound method. I think you are badly confused here. The glossary talks about calling methods as an attribute of an instance, e.g. instance.method(), and the method receiving the instance as the first argument. That's a bound method, not unbound. How do you conclude that this indicates that an unspecified "method" is unbound? Your conclusion is especially nonsensical because unbound methods don't exist in Python 3 -- you're alleging that the usual meaning of "method" is something which no longer exists in the language! method A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self). See function and nested scope. http://docs.python.org/glossary.html > It is am important distinction > and I do not see why you insist on confusing the two. It is not an important distinction, and I am not confusing the two. Bound or unbound, it is still an instance method. >> and that a function defined inside a class is different from a function >> outside of a class. > > That, and your repetition of the same claim further on, is a insulting > lie. If you can't assume I'm carrying on this discussion in good faith, then we have a serious problem. This is not like you Terry -- I've been reading your posts for many years, and you're not usually so obstreperous. > Def statements always create functions. I have known that for 14 years > since the first day I started with Python. I have never thought > differently. If you actually think that I have, you are wrong. I'm glad to hear it. But nevertheless you have made statements (which I quoted, and you deleted from your reply) that suggest the opposite. If I have misinterpreted them, or if you had worded them badly, there's no need to attribute malice to me. Calling me a liar for something said in good faith and which I prefixed with "I think" is simply not cool. -- Steven From drobinow at gmail.com Fri Dec 16 21:11:27 2011 From: drobinow at gmail.com (David Robinow) Date: Fri, 16 Dec 2011 21:11:27 -0500 Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] In-Reply-To: <4eebe83f$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> <4eebe83f$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 16, 2011 at 7:54 PM, Steven D'Aprano wrote: > On Fri, 16 Dec 2011 11:40:11 -0800, Eelco wrote: > >> On 16 dec, 18:38, rusi wrote: >>> On Dec 16, 3:25?pm, Eelco wrote: >>> >>> > Pseudo-backwards compatibility with other languages, I couldnt not >>> > care less for. >>> >>> Double negations n Goedelian situations have interesting implications >>> (tho here its triple) >> >> Heh. Well at least my extra (unintended) negation is semantically >> consistent with the actual english usage of the phrase, which omits the >> negation completely :). (I could care less) > > Oh please. "I could care less" is not English. That's American. > > Here in Australia, we follow the English practice of saying that we > couldn't care less. Well the phrase is still somewhat controversial in the US. I never heard it until age 19 (in 1966) and have always been somewhat disdainful of those using it. But it appears to be hopeless. http://articles.boston.com/2010-10-24/lifestyle/29303907_1_care-peeves-decades From nagle at animats.com Fri Dec 16 21:18:32 2011 From: nagle at animats.com (John Nagle) Date: Fri, 16 Dec 2011 18:18:32 -0800 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> Message-ID: <4eebfbff$0$1679$742ec2ed@news.sonic.net> On 12/16/2011 2:08 PM, Lie Ryan wrote: > On 12/17/2011 01:30 AM, Brad Tilley wrote: >> Or perhaps run should look like this instead: >> >> def run(t): >> lock.acquire() >> shared_container.append(t.name ) >> lock.release() >> >> That seems a bit barbaric to me, not sure. > > change that to: > > def run(t): > with lock: > shared_container.append(t.name ) > > > the `with-statement` will call lock.acquire() and lock.release(). > And, importantly, if you leave the "with" via an exception, the lock will be unlocked. John Nagle From gosefan at gmail.com Fri Dec 16 21:40:19 2011 From: gosefan at gmail.com (YAN HUA) Date: Sat, 17 Dec 2011 10:40:19 +0800 Subject: root[:]=[root,root] Message-ID: Hi,all. Could anybody tell how this code works? >>> root = [None, None] >>> root[:] = [root, root] >>> root [[...], [...]] >>> root[0] [[...], [...]] >>> root[0][0][1][1][0][0][0][1][1] [[...], [...]] >>> Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Fri Dec 16 22:05:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2011 03:05:59 GMT Subject: AttributeError in "with" statement (3.2.2) References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eec0715$0$29979$c3e8da3$5496439d@news.astraweb.com> On Fri, 16 Dec 2011 15:26:30 -0800, Ethan Furman wrote: > Terry Reedy wrote: >> On 12/16/2011 4:22 AM, Steven D'Aprano wrote: [...] > I think you two are in violent agreement as far as how Python is > functioning, and the conflict is in the names given to the various > pieces... I think a glossary would help (please correct me): > > function: callable code suite Sure, why not? Informally: a function is some object which can be called and returns a result. Hence we sometimes call str() and int() functions even though they are actually implemented as types. "Callable" would be better for such informal use, but newbies may not recognise the term. To be more precise: a function is a particular type of callable object created by the def and lambda statements. > method: function that lives in a class Informally, I'm happy with that definition, and use it myself. Under 99% of circumstances, it would be overly pedantic to describe spam in the following as anything other than a method: class C: def spam(self): pass Call it the duck-type principle: since spam quacks like a duck, we might as well call it a duck. If you only care about the easy cases, then spam is a method and we're done. But as a formal definition, it fails on two fronts: functions that live inside classes are not actually methods, they are functions, and while methods are commonly found in classes, they can also be found in instances, or anywhere else for that matter. Functions inside classes are still functions -------------------------------------------- I said that since spam quacks like a method, we might as well call it a method. But it doesn't swim like a method: it's actually still a function with a remarkable power of mimicry. There are places and times where the fact that spam is not actually a method comes through. The three most important examples in my opinion are: * inside the class suite, during class definition time, if you use or inspect spam, you will see it is a function and not a method; * if you bypass the getattr mechanism for looking up attributes, e.g. use C.__dict__['spam'] instead of C().spam, you will see that spam is stored in the dict as a function object, not a method object; * (Python 3 only) if use the standard getattr mechanism, but call it on the class instead of an instance, you will get the function object instead of a method. So, to be precise: spam is actually a function. It is stored as a function inside C.__dict__, but when you retrieve it using C().spam Python automagically builds a method object wrapping spam and gives you that instead. This distinction is important, well, hardly ever, but on the rare occasion it is important, you need to know it. One example of when it may be important: because methods are created as needed, you can't assume that identity checks will necessarily pass for methods: `inst.spam is inst.spam` may not be true. Methods can be created outside of classes ----------------------------------------- The normal way to get a method is by defining it inside a class. In Java, I believe that is the *only* way to get methods, but this is Python, and we have other options. Methods are first class objects, which means they have a type and a constructor, so if you can get hold of their type, you can make them by hand. You can retrieve the method type two ways: * call type on a method you made the usual way: type(C().spam); * or import types and use types.MethodType. Confusingly, the method type calls itself but is exported via the types module as MethodType. Oh well. MethodType requires two arguments, In Python 2, it accepts an optional third argument. Since Python 2 is the past, I'll only talk about the Python 3 version, where the signature might look something like this: MethodType(callable, instance) => instancemethod object This creates a wrapper around callable (usually, but not necessarily a function) and binds instance to that wrapper so the first argument (conventionally called self) can be automatically provided. Another way to create methods is by calling the function object __get__ method by hand. That's what gets used in the normal C().spam case. So you can create methods without putting them inside a class. One trick is that you can customise behaviour on a per-instance basis by overriding the normal method living inside the class with one which lives inside the instance: class C: def spam(self): return "Spam spam spam, lovely SPAM!!!" c = C() c.spam = types.MethodType(lambda self: "Bloody vikings!", c) Instance c now has a custom method which overrides C.spam. [Aside #1: I think this was the original point of contention between Terry and I. For reasons I still don't understand, it seems to me that he denies that the per-instance method spam is actually a method, since it doesn't live inside the class.] [Aside #2: this per-instance method trick may not work for double- underscore methods like __exit__, since CPython and Jython at least skip the instance __dict__ when using dunder methods. In Python 2, it will work for classic classes, but they're gone in Python 3. If you go all the way back to the initial post in this thread, that was the OP's problem.] > unbound method: function that lives in a class An unbound method is merely a method that doesn't know what instance to use, that is, it cannot automatically supply "self". In Python 2, the easiest way to get an unbound method is by retrieving the method from the class instead of an instance: C.spam => returns method which is not bound ("unbound method") C().spam => returns method which is bound ("bound method") Since unbound methods are more or less functionally identical to the underlying function (the only difference is they do a type-check on the argument), Python 3 gets rid of them and and just uses the function object itself. So all the reasons for why methods are not *strictly* functions living inside classes apply equally to unbound methods, since unbound methods are merely a particular form of method. > bound method: callable wrapper around function that has been extracted > from class that will supply the instance object to the function (note: > Python does not save these, they are recreated at each lookup) Bound methods are not *necessarily* extracted from a class, although they *usually* are. But wherever it comes from, a bound method knows which instance to provide as first argument ("self") to the function; otherwise we'd call it an unbound method. > and here is where I think you two diverge: > > instance method (Steven): a bound method that has been saved into the > instance __dict__ (no matter how created) Certainly not! If I gave that impression, I apologize. Every thing I have said above refers to *instance* methods, or just "methods" for lazy people. Instance methods get their name from the fact that they supply the instance as first argument to the function ("self"). If they instead supply the class, we call them a class method; if they don't supply anything, we call them a static method (much to the confusion of Java programmers, who use the same names for different things). You can even create your own method types: http://code.activestate.com/recipes/577030-dualmethod-descriptor/ -- Steven From lie.1296 at gmail.com Fri Dec 16 23:24:50 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 17 Dec 2011 15:24:50 +1100 Subject: root[:]=[root,root] In-Reply-To: References: Message-ID: On 12/17/2011 01:40 PM, YAN HUA wrote: > Hi,all. Could anybody tell how this code works? > >>> root = [None, None] First, you're creating a list of two None, let's say it's list-1. Then you bind the name 'root' to list-1. > >>> root[:] = [root, root] Next, you assign list-1's first member with list-1 and list-1's second member with list-1. > >>> root > [[...], [...]] The result is a recursive list, both list-1's first and second member is list-1 itself. > >>> root[0] > [[...], [...]] > >>> root[0][0][1][1][0][0][0][1][1] > [[...], [...]] > >>> > > Thanks. > > From nukeymusic at gmail.com Sat Dec 17 05:19:44 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sat, 17 Dec 2011 02:19:44 -0800 (PST) Subject: calculate difference between two timestamps [newbie] Message-ID: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> I'm trying to calculate the difference in seconds between two timestamps, but I'm totally stuck: date1="Dec-13-09:47:12" date2="Dec-13-09:47:39" >>> diff=datetime.date(date2)-datetime.date(date1) Traceback (most recent call last): File "", line 1, in TypeError: an integer is required struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S") struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S") >>> diff=datetime.date(struct_date2)-datetime.date(struct_date1) Traceback (most recent call last): File "", line 1, in TypeError: an integer is required thanks in advance nukey From d at davea.name Sat Dec 17 06:09:50 2011 From: d at davea.name (Dave Angel) Date: Sat, 17 Dec 2011 06:09:50 -0500 Subject: calculate difference between two timestamps [newbie] In-Reply-To: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> Message-ID: <4EEC787E.2080703@davea.name> On 12/17/2011 05:19 AM, nukeymusic wrote: > I'm trying to calculate the difference in seconds between two > timestamps, but I'm totally stuck: > date1="Dec-13-09:47:12" > date2="Dec-13-09:47:39" >>>> diff=datetime.date(date2)-datetime.date(date1) > Traceback (most recent call last): > File "", line 1, in > TypeError: an integer is required > > struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S") > struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S") >>>> diff=datetime.date(struct_date2)-datetime.date(struct_date1) > Traceback (most recent call last): > File "", line 1, in > TypeError: an integer is required > > thanks in advance > nukey You should post the full code; you omitted your import line(s) That last approach was closer, try this one: import datetime struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") struct_date2=datetime.datetime.strptime(date2, "%b-%d-%H:%M:%S") diff = struct_date2 - struct_date1 print diff diff is of type datetime.timedelta -- DaveA From vincent.vandevyvre at swing.be Sat Dec 17 06:13:37 2011 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Sat, 17 Dec 2011 12:13:37 +0100 Subject: calculate difference between two timestamps [newbie] In-Reply-To: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> Message-ID: <4EEC7961.7020408@swing.be> An HTML attachment was scrubbed... URL: From vince at vince.ca Sat Dec 17 06:19:09 2011 From: vince at vince.ca (Vince) Date: Sat, 17 Dec 2011 06:19:09 -0500 Subject: calculate difference between two timestamps [newbie] In-Reply-To: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> Message-ID: <20111217111909.GA16443@derelicts.org> On Sat, Dec 17, 2011 at 02:19:44AM -0800, nukeymusic wrote: > I'm trying to calculate the difference in seconds between two > timestamps, but I'm totally stuck: > date1="Dec-13-09:47:12" > date2="Dec-13-09:47:39" > >>> diff=datetime.date(date2)-datetime.date(date1) > Traceback (most recent call last): > File "", line 1, in > TypeError: an integer is required > > struct_date1=time.strptime(date1, "%b-%d-%H:%M:%S") > struct_date2=time.strptime(date2, "%b-%d-%H:%M:%S") > >>> diff=datetime.date(struct_date2)-datetime.date(struct_date1) > Traceback (most recent call last): > File "", line 1, in > TypeError: an integer is required You're trying to compare two time.struct_time types when they need to be datetime.datetime types. This will do the conversion for you: import datetime,time date1="Dec-13-09:47:12" date2="Dec-13-09:47:39" struct_date1=datetime.datetime(*time.strptime(date1, "%b-%d-%H:%M:%S")[:6]) struct_date2=datetime.datetime(*time.strptime(date2, "%b-%d-%H:%M:%S")[:6]) print struct_date2 - struct_date1 From gd.usenet at spamfence.net Sat Dec 17 06:20:55 2011 From: gd.usenet at spamfence.net (=?UTF-8?Q?G=C3=BCnther?= Dietrich) Date: Sat, 17 Dec 2011 12:20:55 +0100 Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> Message-ID: nukeymusic wrote: >I'm trying to calculate the difference in seconds between two [...] >>> import datetime >>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", "%b-%d-%H:%M:%S") >>> date2 = datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") >>> delta = date2 - date1 >>> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000) For very big time differences you should consider to use the Decimal arithmetics (standard module Decimal) instead of integer arithmetics for the last line. If you are sure, that you don't use fractional seconds, you can omit the part with 'delta.microseconds'. Best regards, G?nther From prabhadigi2 at gmail.com Sat Dec 17 07:11:55 2011 From: prabhadigi2 at gmail.com (prabhakaran k) Date: Sat, 17 Dec 2011 04:11:55 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From hoogendoorn.eelco at gmail.com Sat Dec 17 09:38:22 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 17 Dec 2011 06:38:22 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax Message-ID: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> This is a follow-up discussion on my earlier PEP-suggestion. Ive integrated the insights collected during the previous discussion, and tried to regroup my arguments for a second round of feedback. Thanks to everybody who gave useful feedback the last time. PEP Proposal: Pythonification of the asterisk-based collection packing/ unpacking syntax. This proposal intends to expand upon the currently existing collection packing and unpacking syntax. Thereby we mean the following related python constructs: head, *tail = somesequence #pack the remainder of the unpacking of somesequence into a list called tail def foo(*args): pass #pack the unbound positional arguments into a tuple calls args def foo(**kwargs): pass #pack the unbound keyword arguments into a dict calls kwargs foo(*args) #unpack the sequence args into positional arguments foo(**kwargs) #unpack the mapping kwargs into keyword arguments We suggest that these constructs have the following shortcomings that could be remedied. It is unnecessarily cryptic, and out of line with Pythons preference for an explicit syntax. One can not state in a single line what the asterisk operator does; this is highly context dependent, and is devoid of that ?for line in file? pythonic obviousness. From the perspective of a Python outsider, the only hint as to what *args means is by loose analogy with the C-way of handling variable arguments. The current syntax, in its terseness, leaves to be desired in terms of flexibility. While a tuple might be the logical choice to pack positional arguments in the vast majority of cases, it need not be true that a list is always the preferred choice to repack an unpacked sequence, for instance. Type constraints: In case the asterisk is not used to signal unpacking, but rather to signal packing, its semantics is essentially that of a type constraint. The statement: head, tail = sequence Signifies regular unpacking. However, if we add an asterisk, as in: head, *tail = sequence We demand that tail not be just any python object, but rather a list. This changes the semantics from normal unpacking, to unpacking and then repacking all but the head into a list. It may be somewhat counter-intuitive to think of this as a type constraint, since python is after all a weakly-typed language. But the current usage of askeriskes is an exception to that rule. For those who are unconvinced, please consider the analogy to the following simple C# code: var foo = 3; An ?untyped? object foo is created (actually, its type will be inferred from its rhs as an integer). float foo = 3; By giving foo a type-constraint of float instead, the semantics are modified; foo is no longer the integer 3, but gets silently cast to 3.0. This is a simple example, but conceptually entirely analogous to what happens when one places an asterisk before an lvalue in Python. It means ?be a list, and adjust your behavior accordingly?, versus ?be a float, and adjust your behavior accordingly?. The aim of this PEP, is that this type-constraint syntax is expanded upon. We should be careful here to distinguish with providing optional type constraints throughout python as a whole; this is not our aim. This concept has been considered before, but the costs have not been found to out-weight the benefits. http://www.artima.com/weblogs/viewpost.jsp?thread=86641 Our primary aim is the niche of collection packing/unpacking, but if further generalizations can be made without increasing the cost, those are most welcome. To reiterate: what is proposed is nothing radical; merely to replace the asterisk-based type constraints with a more explicit type constraint. Currently favored alternative syntax: Both for the sake of explicitness and flexibility, we consider it desirable that the name of the collection type is used directly in any collection packing statement. Annotating a variable declaration with a collection type name should signal collection packing. This association between a collection type name and a variable declaration can be accomplished in many ways; for now, we suggest collectionname::collectiontype for packing, and ::collectionname for unpacking. Examples of use: head, tail::tuple = ::sequence def foo(args::list, kwargs::dict): pass foo(::args, ::kwargs) The central idea is to replace annotations with asteriskes by annotations with collection type names, but note that we have opted for several other minor alterations of the existing syntax that seem natural given the proposed changes. First of all, explicitly mentioning the type of the collection involved eliminates the need to have two symbols, * and **. Which variable captures the positional arguments and which captures the keyword arguments can be inferred from the collection type they model, mapping or sequence. The rare case of collections that both model a sequence and a mapping can either be excluded or handled by assigning precedence for one type or the other. A double semicolon before a collection type signals unpacking. As with declarations, there is no genuine need to have a different operator for sequence and mapping types, although if such a demand exists, it would not be hard to accommodate. A double semicolon in front of the collection is congruent with the asterisk syntax, and nicely emphasizes this unpacking operation being the symmetric counterpart of the packing operation, which is signalled by the same symbols to the right of the identifier. Since we are going to make the double semicolon (or whatever the symbol) a general collection packing/ unpacking marker, we feel it makes sense to allow it to be used to explicitly signify unpacking, even when as much is implied by the syntax on the left hand side, to preserve symmetry with the syntax inside function calls. Summarizing, what this syntax achieves, in loose order of perceived importance: Simplicity: we have reduced a set of rather arbitrary rules concerning the syntax and semantics of the asterisk (does it construct a list or a tuple?) to a single general symbol: the double semicolon is the collection packing/unpacking annotation symbol, and that is all there is to know about it. Readability: the proposed syntax reads like a book: args-list and kwargs-dict, unlike the more cryptic asterisk syntax. We avoid extra lines of code in the event another sequence or mapping type than the one returned by default is required. Efficiency: by declaring the desired collection type, it can be constructed in the optimal way from the given input, rather than requiring a conversion after the default collection type is constructed. A double semicolon is suggested, since the single colon is already taken by the function annotation syntax in Python 3. This is somewhat unfortunate: programming should come before meta-programming, and it should rather be the other way around. On the one hand having both : and :: as variable declaration annotation symbols is a nice unification, on the other hand, a syntax more easily visually distinguished from function annotations can be defended. For increased backwards compatibility the asterisk could be used, but sandwiched between two identifiers it looks like a multiplication. But many others symbols would do, such as @ or !. From alex.kapps at web.de Sat Dec 17 10:22:32 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sat, 17 Dec 2011 16:22:32 +0100 Subject: Help about Xlib Library in Python In-Reply-To: References: Message-ID: <4EECB3B8.7000808@web.de> On 16.12.2011 05:55, ?? wrote: > Hi, > > A question about Xlib Library in Python troubled me for several days > and I finally found this email list. I hope someone could answer my > question. I think it is easy for experienced user. > > I would like to write a small script to response my mouse click in > root screen and write something in the terminal. My script is like > this, but it does not work in my computer. > > from Xlib import X > import Xlib.display > > def main(): > display = Xlib.display.Display() > root = display.screen().root > root.change_attributes(event_mask= > X.ButtonPressMask | > X.ButtonReleaseMask) > while True: > event = root.display.next_event() > print "1" > if __name__ == "__main__": > main() > > > Any hints are welcome > Thank you! > > Ruan zheng > You haven't said *how* your script is not working and what distro/desktop-environment you use, but I suspect, that your script collides with the window manager. Most window managers occupy the root window for them self, and even display another window above the root window (for the wallpaper, the desktop icons, etc.), so your clicks never reach the root window, but whatever the window manager draws as the background. If you run your script in a plain and naked X "session" with nothing but an xterm *and no window manager*, your script will probably work. From dihedral88888 at googlemail.com Sat Dec 17 11:34:59 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 17 Dec 2011 08:34:59 -0800 (PST) Subject: Help about Xlib Library in Python In-Reply-To: References: Message-ID: <18311531.104.1324139699498.JavaMail.geo-discussion-forums@prdr27> On Saturday, December 17, 2011 11:22:32 PM UTC+8, Alexander Kapps wrote: > On 16.12.2011 05:55, ?? wrote: > > Hi, > > > > A question about Xlib Library in Python troubled me for several days > > and I finally found this email list. I hope someone could answer my > > question. I think it is easy for experienced user. > > > > I would like to write a small script to response my mouse click in > > root screen and write something in the terminal. My script is like > > this, but it does not work in my computer. > > > > from Xlib import X > > import Xlib.display > > > > def main(): > > display = Xlib.display.Display() > > root = display.screen().root > > root.change_attributes(event_mask= > > X.ButtonPressMask | > > X.ButtonReleaseMask) > > while True: > > event = root.display.next_event() > > print "1" > > if __name__ == "__main__": > > main() > > > > > > Any hints are welcome > > Thank you! > > > > Ruan zheng > > > > You haven't said *how* your script is not working and what > distro/desktop-environment you use, but I suspect, that your script > collides with the window manager. > > Most window managers occupy the root window for them self, and even > display another window above the root window (for the wallpaper, the > desktop icons, etc.), so your clicks never reach the root window, > but whatever the window manager draws as the background. > > If you run your script in a plain and naked X "session" with nothing > but an xterm *and no window manager*, your script will probably work. Check co-linux and VNC and X-windows. It's trivial to play with PYTHON under co-linux. NT, WindowsXP, and Windows7 are excellent in the heap manager of a single process with multiple threads. Linux is multiple-process not multiple threads. Also the brain damaged heap walker is inferior to $$$MS. From dihedral88888 at googlemail.com Sat Dec 17 11:34:59 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sat, 17 Dec 2011 08:34:59 -0800 (PST) Subject: Help about Xlib Library in Python In-Reply-To: References: Message-ID: <18311531.104.1324139699498.JavaMail.geo-discussion-forums@prdr27> On Saturday, December 17, 2011 11:22:32 PM UTC+8, Alexander Kapps wrote: > On 16.12.2011 05:55, ?? wrote: > > Hi, > > > > A question about Xlib Library in Python troubled me for several days > > and I finally found this email list. I hope someone could answer my > > question. I think it is easy for experienced user. > > > > I would like to write a small script to response my mouse click in > > root screen and write something in the terminal. My script is like > > this, but it does not work in my computer. > > > > from Xlib import X > > import Xlib.display > > > > def main(): > > display = Xlib.display.Display() > > root = display.screen().root > > root.change_attributes(event_mask= > > X.ButtonPressMask | > > X.ButtonReleaseMask) > > while True: > > event = root.display.next_event() > > print "1" > > if __name__ == "__main__": > > main() > > > > > > Any hints are welcome > > Thank you! > > > > Ruan zheng > > > > You haven't said *how* your script is not working and what > distro/desktop-environment you use, but I suspect, that your script > collides with the window manager. > > Most window managers occupy the root window for them self, and even > display another window above the root window (for the wallpaper, the > desktop icons, etc.), so your clicks never reach the root window, > but whatever the window manager draws as the background. > > If you run your script in a plain and naked X "session" with nothing > but an xterm *and no window manager*, your script will probably work. Check co-linux and VNC and X-windows. It's trivial to play with PYTHON under co-linux. NT, WindowsXP, and Windows7 are excellent in the heap manager of a single process with multiple threads. Linux is multiple-process not multiple threads. Also the brain damaged heap walker is inferior to $$$MS. From steve+comp.lang.python at pearwood.info Sat Dec 17 12:00:46 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2011 17:00:46 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> Message-ID: <4eeccabe$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 06:38:22 -0800, Eelco wrote: > One can not state in a single line what the asterisk > operator does; Multiplication, exponentiation, sequence packing/unpacking, and varargs. -- Steven From roy at panix.com Sat Dec 17 12:14:12 2011 From: roy at panix.com (Roy Smith) Date: Sat, 17 Dec 2011 12:14:12 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eeccabe$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4eeccabe$0$29979$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > On Sat, 17 Dec 2011 06:38:22 -0800, Eelco wrote: > > > One can not state in a single line what the asterisk > > operator does; > > Multiplication, exponentiation, sequence packing/unpacking, and varargs. Import wildcarding? From rosuav at gmail.com Sat Dec 17 12:18:15 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 04:18:15 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eeccabe$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 18, 2011 at 4:14 AM, Roy Smith wrote: > Import wildcarding? That's not an operator, any more than it is when used in filename globbing. The asterisk _character_ has many meanings beyond those of the operators * and **. ChrisA From nagle at animats.com Sat Dec 17 14:24:24 2011 From: nagle at animats.com (John Nagle) Date: Sat, 17 Dec 2011 11:24:24 -0800 Subject: Python install regression test fail Message-ID: <4eecec6e$0$1707$742ec2ed@news.sonic.net> Here's a result from "make", "make test" of a fresh download of Python 2.7.2 on Linux 2.6.18-1.2239.fc5smp: 350 tests OK. 2 tests failed: test_os test_site 35 tests skipped: test_aepack test_al test_applesingle test_bsddb185 test_bsddb3 test_cd test_cl test_codecmaps_cn test_codecmaps_hk test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw test_curses test_gdb test_gl test_imgfile test_kqueue test_linuxaudiodev test_macos test_macostools test_msilib test_ossaudiodev test_scriptpackages test_smtpnet test_socketserver test_startfile test_sunaudiodev test_timeout test_tk test_ttk_guionly test_urllib2net test_urllibnet test_winreg test_winsound test_zipfile64 3 skips unexpected on linux2: test_gdb test_tk test_ttk_guionly make: *** [test] Error 1 -bash-3.1$ Here are the fails: test_os test test_os failed -- Traceback (most recent call last): File "/var/www/vhosts/sitetruth.com/private/downloads/python/Python-2.7.2/Lib/test/test_os.py", line 563, in helper self.check(getattr(os, f)) File "/var/www/vhosts/sitetruth.com/private/downloads/python/Python-2.7.2/Lib/test/test_os.py", line 572, in check self.assertEqual(e.errno, errno.EBADF) AssertionError: 25 != 9 (This test expected EBADF and got ENOTTY on making a bad file descriptor. Not clear why it got ENOTTY, but that's "Inappropriate IOCTL", as if the file descriptor number was in use but not valid for the test.) test_site test test_site crashed -- : [Errno 13] Permission denied: '/var/www/vhosts/sitetruth.com/.local' (That looks like a bug in the test suite. It shouldn't be storing into the user's home directory.) John Nagle From yasar11732 at gmail.com Sat Dec 17 14:33:01 2011 From: yasar11732 at gmail.com (=?ISO-8859-9?Q?Ya=FEar_Arabac=FD?=) Date: Sat, 17 Dec 2011 21:33:01 +0200 Subject: Bug in multiprocessing.reduction? Message-ID: You can see my all code below, theoritically that code should work I guess. But I keep getting this error: [SUBWARNING/MainProcess] thread for sharing handles raised exception : ------------------------------------------------------------------------------- Traceback (most recent call last): File "/usr/lib/python2.7/multiprocessing/reduction.py", line 127, in _serve send_handle(conn, handle_wanted, destination_pid) File "/usr/lib/python2.7/multiprocessing/reduction.py", line 80, in send_handle _multiprocessing.sendfd(conn.fileno(), handle) OSError: [Errno 9] Bad file descriptor ------------------------------------------------------------------------------- Do you see an error in my side, or is this a bug in Python? import multiprocessing as mp import logging import socket import time from random import randint from multiprocessing.reduction import reduce_socket, rebuild_socket logger = mp.log_to_stderr(logging.DEBUG) def worker(queue): while True: reduced_socket = queue.get() # This is the line where I get Bad file descriptor error: print reduced_socket[0](*reduced_socket[1]) if __name__ == '__main__': serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.bind(('',9090)) serversocket.listen(5) num_workers = 5 socket_queue = mp.Queue() worker_processes = [mp.Process(target=worker,args=(socket_queue,)) for i in range(num_workers)] for worker_process in worker_processes: worker_process.daemon = True worker_process.start() while True: client, address = serversocket.accept() client_handle = reduce_socket(client) socket_queue.put(client_handle) -- http://yasar.serveblog.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid.invalid Sat Dec 17 15:02:44 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Sat, 17 Dec 2011 20:02:44 +0000 (UTC) Subject: % is not an operator [was Re: Verbose and flexible args and kwargs syntax] References: <4ee81592$0$11091$c3e8da3@news.astraweb.com> <4ee89762$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EEAB3E0.80809@mrabarnett.plus.com> <6a0161f3-d349-4a28-b003-793f647c10e4@x7g2000vby.googlegroups.com> <7b7e09cf-f618-4d86-8e5e-81ba1904c75c@k5g2000pra.googlegroups.com> <47fda7c8-1c8a-40c4-8c86-251ec2503cb7@m7g2000vbc.googlegroups.com> <9l23ntFsibU1@mid.individual.net> Message-ID: On 2011-12-16, Gregory Ewing wrote: > Eelco wrote: >> the actual english usage of the phrase, which omits >> the negation completely :). (I could care less) > > No, that's the American usage. That's the _ignorant_ American usage. Americans with a clue use the "couldn't" version. I won't comment on the relative sizes of the two groups. > The English usage is "I couldn't care less", which has the advantage > of actually making sense. Indeed. -- Grant Edwards grant.b.edwards Yow! HUGH BEAUMONT died at in 1982!! gmail.com From hoogendoorn.eelco at gmail.com Sat Dec 17 15:11:04 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 17 Dec 2011 12:11:04 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eeccabe$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 17, 6:18?pm, Chris Angelico wrote: > On Sun, Dec 18, 2011 at 4:14 AM, Roy Smith wrote: > > Import wildcarding? > > That's not an operator, any more than it is when used in filename > globbing. The asterisk _character_ has many meanings beyond those of > the operators * and **. > > ChrisA To cut short this line of discussion; I meant the asterisk symbol purely in the context of collection packing/unpacking. Of course it has other uses too. Even that single use requires a whole paragraph to explain completely; when does it result in a tuple or a list, when is unpacking implicit and when not, why * versus **, and so on. From vinay_sajip at yahoo.co.uk Sat Dec 17 15:44:45 2011 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Sat, 17 Dec 2011 12:44:45 -0800 (PST) Subject: logging issues References: <4EE77279.10104@gmail.com> <4EE77933.2060705@sequans.com> <4EE77B7B.9090408@gmail.com> Message-ID: <1f2ca0bf-4c47-416d-b2d4-e526c56db400@f1g2000yqi.googlegroups.com> On Dec 13, 5:03?pm, Andrea Crotti wrote: > The set_verbosity seems to do its job, the second is also > called and the filters are added but I don't see the output > formatted as it should.. Generally speaking, you should only do logging configuration once, as Jean-Michel said - from your main() function or "if __name__ == '__main__'" suite. Until you show more detail about what you're getting and what you're expecting, it's hard to provide more help. Regards, Vinay Sajip From steve+comp.lang.python at pearwood.info Sat Dec 17 18:20:10 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2011 23:20:10 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eeccabe$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eed23a9$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 12:11:04 -0800, Eelco wrote: > > One can not state in a single line what the asterisk > > operator does; ... > To cut short this line of discussion; I meant the asterisk symbol purely > in the context of collection packing/unpacking. Of course it has other > uses too. > > Even that single use requires a whole paragraph to explain completely; > when does it result in a tuple or a list, when is unpacking implicit and > when not, why * versus **, and so on. Do you think that this paragraph will become shorter if you change the spelling * to something else? It takes more than one line to explain list comprehensions, content managers, iterators, range(), and import. Why should we care if * and ** also take more than one paragraph? Even if you could get it down to a single line, what makes you think that such extreme brevity is a good thing? You might not be able to explain them in a single line, but you can explain them pretty succinctly: Varags: Inside a function parameter list, * collects an arbitrary number of positional arguments into a tuple. When calling functions, * expands any iterator into positional arguments. In both cases, ** does the same thing for keyword arguments. Extended iterator unpacking: On the left hand side of an assignment, * collects multiple values from the right hand side into a list. Let's see you do better with your suggested syntax. How concisely can you explain the three functions? Don't forget the new type coercions (not constraints, as you keep calling them) you're introducing. It boggles my mind that you complain about the complexity of existing functionality, and your solution involves *increasing* the complexity with more functionality. -- Steven From rbanffy at gmail.com Sat Dec 17 18:53:43 2011 From: rbanffy at gmail.com (=?ISO-8859-1?Q?Ricardo_B=E1nffy?=) Date: Sat, 17 Dec 2011 21:53:43 -0200 Subject: PyCon US 2012 sprints Message-ID: Hi folks. Next March I'm planning to attend PyCon US (for the first time) and stay for the sprints. I am not sure how they work, however. Are there any "first-timer guide to PyCon sprints"? -- Ricardo B?nffy http://www.dieblinkenlights.com http://twitter.com/rbanffy From steve+comp.lang.python at pearwood.info Sat Dec 17 19:59:45 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 00:59:45 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> Message-ID: <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 06:38:22 -0800, Eelco wrote: > Type constraints: > > In case the asterisk is not used to signal unpacking, but rather to > signal packing, its semantics is essentially that of a type constraint. "Type constraint" normally refers to type restrictions on *input*: it is a restriction on what types are accepted. When it refers to output, it is not normally a restriction, therefore "constraint" is inappropriate. Instead it is normally described as a coercion, cast or conversion. Automatic type conversions are the opposite of a constraint: it is a loosening of restrictions. "I don't have to use a list, I can use any sequence or iterator". In iterator unpacking, it is the *output* which is a list, not a restriction on input: in the statement: head, *tail = sequence tail may not exist before the assignment, and so describing this as a constraint on the type of tail is completely inappropriate. > The statement: > > head, tail = sequence > > Signifies regular unpacking. However, if we add an asterisk, as in: > > head, *tail = sequence > > We demand that tail not be just any python object, but rather a list. We don't demand anything, any more than when we say: for x in range(1, 100): we "demand" that x is not just any python object, but rather an int. Rather, we accept what we're given: in case of range and the for loop, we are given an int. In the case of extended tuple unpacking, we are given a list. > This changes the semantics from normal unpacking, to unpacking and then > repacking all but the head into a list. Aside: iterator unpacking is more general than just head/tail unpacking. >>> a, b, *var, c, d, e = range(10) >>> print(a, b, c, d, e, var) 0 1 7 8 9 [2, 3, 4, 5, 6] You are jumping to conclusions about implementation details which aren't supported by the visible behaviour. What evidence do you have that iterator unpacking creates a tuple first and then converts it to a list? > It may be somewhat counter-intuitive to think of this as a type > constraint, since python is after all a weakly-typed language. The usual test of a weakly-typed language is that "1"+1 succeeds (and usually gives 2), as in Perl but not Python. I believe you are confusing weak typing with dynamic typing, a common mistake. [...] > The aim of this PEP, is that this type-constraint syntax is expanded > upon. We should be careful here to distinguish with providing optional > type constraints throughout python as a whole; this is not our aim. Iterator unpacking is no more about type constraints than is len(). -- Steven From edriscoll at wisc.edu Sat Dec 17 20:29:53 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 19:29:53 -0600 Subject: Parsing stream of JSON objects incrementally Message-ID: <4EED4211.5080308@wisc.edu> I'm interested in writing two programs, A and B, which communicate using JSON. At a high level, A wants to transfer an array to B. However, I would very much like to make it possible for A and B to run in parallel, so my current plan is to have A output and B read a *sequence* of JSON objects. In other words, instead of [ {"a": 0}, {"b":0}, {"c": 0} ] it would just send {"a": 0} {"b": 0} {"c": 0} I know about the raw_decode() object inside the json.JSONParser class, and that gets me most of the way there there. However, what I'm *not* sure about is the best way to get the input to the raw_decode() function, which expects a "string or buffer": >>> d = json.JSONDecoder() >>> d.raw_decode(sys.stdin) Traceback (most recent call last): ... File "json\scanner.py", line 42, in iterscan match = self.scanner.scanner(string, idx).match TypeError: expected string or buffer Now I'm not very familiar with the buffer and how it could be used (and whether a file or stdin could be used as one in an incremental fashion), but the best way I can come up with is the following: 1. Read a line of input 2. Try to decode it 3. If not, read another line, concatenate it to the end, and try again 4. etc. That seems... inelegant at least. Some other information: * I'm looking for a 2.7 solution ideally * I'd prefer not to use a different JSON library entirely * As suggested, I *am* willing to wait for a newline to do processing * However, I don't want to require exactly one object per line (and want to allow both multiple objects on one line and newlines within an object) Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From tjreedy at udel.edu Sat Dec 17 21:09:36 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Dec 2011 21:09:36 -0500 Subject: AttributeError in "with" statement (3.2.2) In-Reply-To: <4eebefc0$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <4ee857d4$0$11091$c3e8da3@news.astraweb.com> <4ee97f20$0$11091$c3e8da3@news.astraweb.com> <4eeb0ddb$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eebefc0$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/16/2011 8:26 PM, Steven D'Aprano wrote: > On Fri, 16 Dec 2011 17:05:57 -0500, Terry Reedy wrote: >> It is am important distinction [unbound versus bound] > It is not an important distinction, and I am not confusing the two. So we agree on the distinction but disagree on its importance. Let us leave it at that. > Bound or unbound, it is still an instance method. OK. So 'instance method' is a bit ambiguous (more than I thought, or would prefer) in that it can refer to unbound methods, bounds methods, or both. So be it. >>> and that a function defined inside a class is different from a function >>> outside of a class. >> >> That, and your repetition of the same claim further on, is a insulting >> lie. > > If you can't assume I'm carrying on this discussion in good faith, If you can't assume that I am intelligent and experienced enough to know the meaning of def, one on the most basic aspects of Python, and you are unwilling to give me the benefit of any doubt you might have on that score, but instead go on to 'correct' me publicly, then no, I can't. >> Def statements always create functions. I have known that for 14 years >> since the first day I started with Python. I have never thought >> differently. If you actually think that I have, you are wrong. > > I'm glad to hear it. But nevertheless you have made statements (which I > quoted, and you deleted from your reply) that suggest the opposite. OK, let us look at the my statement and your 'repetition of the same claim further on' that I previously deleted. I wrote >> These are bound methods. The instance methods are the functions wrapped. As I indicated in response to Ethan, I would now revised the second sentence now to "The unbound methods are the function wrapped" or "The instance-requiring methods are the functions wrapped." But that is not important here. In my opinion, there is no way that anyone reading that in good faith could conclude that I do not know the meaning of def statements. They are not the subject of discussion in that sentence or the rest of this thread. But in response you wrote. > I am afraid you are mistaken. About what? You go on to explain. > What you say may very well apply to other languages, > but in Python, def creates functions no matter where you > execute it. Always and without exception. So that makes twice that you said or implied that I think the location of a def statement changes what it creates, even though I explicitly said the opposite when I suggested that the glossary entry might be revised. What am I to think at such a tactic. You are normally much more careful in what you write. > If I have misinterpreted them, or if you had worded them badly, > there's no need to attribute malice to me. I did not do that. I gave my opinion of your statement, just as you have given your opinions of mine. I really did not and do not know why you misrepresented my knowledge of Python. I actually consider overt intentional malice much less likely than other possibilities. > Calling me a liar I did not do that, any more than you have been calling me things. I believe you are asking for the same 'benefit of the doubt' that I believe you denied to me. -- Terry Jan Reedy From rosuav at gmail.com Sat Dec 17 21:45:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 13:45:35 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 18, 2011 at 11:59 AM, Steven D'Aprano wrote: > The usual test of a weakly-typed language is that "1"+1 succeeds (and > usually gives 2), as in Perl but not Python. I believe you are confusing > weak typing with dynamic typing, a common mistake. I'd go stronger than "usually" there. If "1"+1 results in "11", then that's not weak typing but rather a convenient syntax for stringification - if every object can (or must) provide a to-string method, and concatenating anything to a string causes it to be stringified, then it's still strongly typed. Or is a rich set of automated type-conversion functions evidence of weak typing? And if so, then where is the line drawn - is upcasting of int to float weak? ChrisA From edriscoll at wisc.edu Sat Dec 17 22:03:01 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 21:03:01 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EED57E5.2020900@wisc.edu> On 12/17/2011 20:45, Chris Angelico wrote: > I'd go stronger than "usually" there. If "1"+1 results in "11", then > that's not weak typing but rather a convenient syntax for > stringification - if every object can (or must) provide a to-string > method, and concatenating anything to a string causes it to be > stringified, then it's still strongly typed. > > Or is a rich set of automated type-conversion functions evidence of > weak typing? And if so, then where is the line drawn - is upcasting of > int to float weak? > > ChrisA Sorry, I just subscribed to the list so am stepping in mid-conversation, but "strong" vs "weak" typing does not have a particularly well-defined meaning. There are at least three very different definitions you'll find people use which are almost pairwise orthogonal in theory, if less so in practice. There's a great mail to a Perl mailing list I've seen [1] where someone lists *eight* definitions (albeit with a couple pairs of definitions that are only slightly different). I like to use it in the "automated conversion" sense, because I feel like the other possible definitions are covered by other terms (static/dynamic, and safe/unsafe). And in that sense, I think that thinking of languages as "strong" *or* "weak" is a misnomer; it's a spectrum. (Actually even a spectrum is simplifying things -- it's more like a partial order.) Something like ML or Haskell, which does not even allow integer to double promotions, is very strong typing. Something like Java, which allows some arithmetic conversion and also automatic stringification (a la "1" + 1) is somewhere in the middle of the spectrum. Personally I'd put Python even weaker on account of things such as '[1,2]*2' and '1 < True' being allowed, but on the other hand it doesn't allow "1"+1. Evan [1] http://groups.google.com/group/comp.lang.perl.moderated/msg/89b5f256ea7bfadb (though I don't think I've seen all of those) -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From edriscoll at wisc.edu Sat Dec 17 22:08:04 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 21:08:04 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4EED57E5.2020900@wisc.edu> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EED57E5.2020900@wisc.edu> Message-ID: <4EED5914.6030903@wisc.edu> On 12/17/2011 21:03, Evan Driscoll wrote: > Personally I'd put Python even weaker on account of things such as > '[1,2]*2' and '1 < True' being allowed, but on the other hand it > doesn't allow "1"+1. Not to mention duck typing, which under my definition I'd argue is pretty much the weakest of typing that you can apply to structure-like types which I can think of. Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From tjreedy at udel.edu Sat Dec 17 22:08:24 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 17 Dec 2011 22:08:24 -0500 Subject: root[:]=[root,root] In-Reply-To: References: Message-ID: On 12/16/2011 9:40 PM, YAN HUA wrote: > Hi,all. Could anybody tell how this code works? > >>> root = [None, None] > >>> root[:] = [root, root] > >>> root > [[...], [...]] > >>> root[0] > [[...], [...]] > >>> root[0][0][1][1][0][0][0][1][1] > [[...], [...]] A simpler example: >>> l = [] >>> l.append(l) >>> l [[...]] Python is (now) smart enough to recognize a recursive list and print '...' instead of going into an infinite loop printing '['s (as it once did, I believe). -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Sat Dec 17 22:33:03 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 03:33:03 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eed5eef$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Dec 2011 13:45:35 +1100, Chris Angelico wrote: > On Sun, Dec 18, 2011 at 11:59 AM, Steven D'Aprano > wrote: >> The usual test of a weakly-typed language is that "1"+1 succeeds (and >> usually gives 2), as in Perl but not Python. I believe you are >> confusing weak typing with dynamic typing, a common mistake. > > I'd go stronger than "usually" there. If "1"+1 results in "11", then > that's not weak typing but rather a convenient syntax for > stringification - if every object can (or must) provide a to-string > method, and concatenating anything to a string causes it to be > stringified, then it's still strongly typed. For what it's worth, Wikipedia's article on type systems gives Javascript as an example of weak typing because it converts "1"+1 to "11". I agree with them. http://en.wikipedia.org/wiki/Type_system I think that weak and strong typing aren't dichotomies, but extremes in a continuum. Assembly languages are entirely weak, since everything is bytes and there are no types to check; some academic languages may be entire strong; but most real-world languages include elements of both. Most commonly coercing ints to floats. Chris Smith's influence article "What To Know Before Debating Type Systems" goes further, suggesting that weak and strong typing are meaningless terms. I don't go that far, but you should read his article: http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/ > Or is a rich set of automated type-conversion functions evidence of weak > typing? And if so, then where is the line drawn - is upcasting of int to > float weak? To my mind, the distinction that should be drawn is that if two types are in some sense the same *kind* of thing, then automatic conversions or coercions are weak evidence of weak typing. Since we consider both ints and floats to be kinds of numbers, mixed int/float arithmetic is not a good example of weak typing. But since numbers and strings are quite different kinds of things, mixed str/int operations is a good example of weak typing. But not *entirely* different: numbers can be considered strings of digits; and non-digit strings can have numeric values. I don't know of any language that allows 1 + "one" to return 2, but such a thing wouldn't be impossible. -- Steven From kmshafique at yahoo.com Sat Dec 17 22:35:33 2011 From: kmshafique at yahoo.com (Shafique, M. (UNU-MERIT)) Date: Sat, 17 Dec 2011 19:35:33 -0800 (PST) Subject: Newman's community structure algorithms Message-ID: <1324179333.70741.YahooMailNeo@web65405.mail.ac4.yahoo.com> Hello, I am in need of the python or matlab implementation of following algorithms: Newman, M. E. J. 2006. Modularity and community structure in networks. PNAS 103(23): 8577-8582. Newman, M. E. J. and Leicht, E. A. 2007. Mixture models and exploratory analysis in networks. PNAS 104(23): 9564-9569. If anyone could share that or guide me to a possible source, I shall be immensely grateful. Best regards,? --- Muhammad Shafique? Ph.D. Fellow? United Nations University, UNU-MERIT? Keizer Kareplein 19, 6211 TC Maastricht? The Netherlands http://www.merit.unu.edu/about/profile.php?id=1058 -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sat Dec 17 22:42:05 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 14:42:05 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4EED57E5.2020900@wisc.edu> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EED57E5.2020900@wisc.edu> Message-ID: On Sun, Dec 18, 2011 at 2:03 PM, Evan Driscoll wrote: > Sorry, I just subscribed to the list so am stepping in mid-conversation, Welcome to the list! If you're curious as to what's happened, check the archives: http://mail.python.org/pipermail/python-list/ > Something like ML or Haskell, which does not even allow integer to > double promotions, is very strong typing. Something like Java, which > allows some arithmetic conversion and also automatic stringification (a > la "1" + 1) is somewhere in the middle of the spectrum. Personally I'd > put Python even weaker on account of things such as '[1,2]*2' and '1 < > True' being allowed, but on the other hand it doesn't allow "1"+1. But [1,2]*2 is operator overloading. The language doesn't quietly convert [1,2] into a number and multiply that by 2, it keeps it as a list and multiplies the list by 2. Allowing 1 < True is weaker typing. It should be noted, however, that "1 < True" is False, and "1 > True" is also False. The comparison doesn't make much sense, but it's not an error. ChrisA From roy at panix.com Sat Dec 17 22:59:17 2011 From: roy at panix.com (Roy Smith) Date: Sat, 17 Dec 2011 22:59:17 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eed5eef$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4eed5eef$0$29979$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > some academic languages may be > entire strong; but most real-world languages include elements of both. > Most commonly coercing ints to floats. Early Fortran compilers did not automatically promote ints to floats. > But not *entirely* different: numbers can be considered strings of > digits; and non-digit strings can have numeric values. I don't know of > any language that allows 1 + "one" to return 2, but such a thing wouldn't > be impossible. It is possible for 1 + "one" to be equal to 2 in C or C++. All it takes is for the string literal to be located at memory location 1. Not likely, but nothing in the language prevents it. From rosuav at gmail.com Sat Dec 17 23:05:33 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 15:05:33 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eed5eef$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 18, 2011 at 2:59 PM, Roy Smith wrote: > It is possible for 1 + "one" to be equal to 2 in C or C++. ?All it takes > is for the string literal to be located at memory location 1. ?Not > likely, but nothing in the language prevents it. Not quite; 1 + "one" will be "ne", which might happen to be at memory location 2. The data type is going to be char* (or, in a modern compiler, const char*), not int. That said, though, I think that (in this obscure circumstance) it would compare equal with 2. For what that's worth. :) ChrisA From workitharder at gmail.com Sat Dec 17 23:17:35 2011 From: workitharder at gmail.com (buck) Date: Sat, 17 Dec 2011 20:17:35 -0800 (PST) Subject: Debugging a difficult refcount issue. Message-ID: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. How should I go about debugging this further? I've been completely stuck on this for two days now :( [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload From edriscoll at wisc.edu Sat Dec 17 23:43:16 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 22:43:16 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EED57E5.2020900@wisc.edu> Message-ID: <4EED6F64.5020906@wisc.edu> On 12/17/2011 21:42, Chris Angelico wrote: > Welcome to the list! If you're curious as to what's happened, check > the archives: > http://mail.python.org/pipermail/python-list/ Thanks! Incidentally, is there a good way to respond to the original post in this thread, considering it wasn't delivered to me? > But [1,2]*2 is operator overloading. The language doesn't quietly > convert [1,2] into a number and multiply that by 2, it keeps it as a > list and multiplies the list by 2. > > Allowing 1 < True is weaker typing. It should be noted, however, that > "1 < True" is False, and "1 > True" is also False. The comparison > doesn't make much sense, but it's not an error. I see where you're coming from, especially as I wouldn't consider overloading a function on types (in a language where that phrase makes sense) moving towards weak typing either. Or at least I wouldn't have before this discussion... At the same time, it seems a bit implementationy. I mean, suppose '1' were an object and implemented __lt__. Does it suddenly become not weak typing because of that? (The other thing is that I think strong vs weak is more subjective than many of the other measures. Is '1 < True' or '"1"+1' weaker? I think it has a lot to do with how the operations provided by the language play to your expectations.) On 12/17/2011 22:05, Chris Angelico wrote: > Not quite; 1 + "one" will be "ne", which might happen to be at memory > location 2. The data type is going to be char* (or, in a modern > compiler, const char*), not int. I'm not quite sure I'd say that it could be 2, exactly, but I definitely disagree with this... after running 'int x = 5, *p = &x' would you say that "p is 5"? (Assume &x != 5.) 1+"one" *points to* "ne", but it's still a pointer. Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From workitharder at gmail.com Sat Dec 17 23:52:32 2011 From: workitharder at gmail.com (buck) Date: Sat, 17 Dec 2011 20:52:32 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> Message-ID: <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> I like the spirit of this. Let's look at your examples. > Examples of use: > head, tail::tuple = ::sequence > def foo(args::list, kwargs::dict): pass > foo(::args, ::kwargs) My initial reaction was "nonono!", but this is simply because of the ugliness. The double-colon is very visually busy. I find that your second example is inconsistent with the others. If we say that the variable-name is always on the right-hand-side, we get: > def foo(list::args, dict::kwargs): pass This nicely mirrors other languages (such as in your C# example: "float foo") as well as the old python behavior (prefixing variables with */** to modify the assignment). As for the separator, let's examine the available ascii punctuation. Excluding valid variable characters, whitespace, and operators, we have: ! -- ok. " -- can't use this. Would look like a string. # -- no. Would looks like a comment. $ -- ok. ' -- no. Would look like a string. ( -- no. Would look like a function. ) -- no. Would look like ... bad syntax. , -- no. Would indicate a separate item in the variable list. . -- no. Would look like an attribute. : -- ok, maybe. Seems confusing in a colon-terminated statement. ; -- no, just no. ? -- ok. @ -- ok. [ -- no. Would look like indexing. ] -- no. ` -- no. Would look like a string? { -- too strange } -- too strange ~ -- ok. That leaves these. Which one looks least strange? float ! x = 1 float $ x = 1 float ? x = 1 float @ x = 1 The last one looks decorator-ish, but maybe that's proper. The implementation of this would be quite decorator-like: take the "normal" value of x, pass it through the indicated function, assign that value back to x. Try these on for size. head, @tuple tail = sequence def foo(@list args, @dict kwargs): pass foo(@args, @kwargs) For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. -buck From rosuav at gmail.com Sun Dec 18 00:00:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 16:00:46 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4EED6F64.5020906@wisc.edu> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <4EED57E5.2020900@wisc.edu> <4EED6F64.5020906@wisc.edu> Message-ID: On Sun, Dec 18, 2011 at 3:43 PM, Evan Driscoll wrote: > On 12/17/2011 21:42, Chris Angelico wrote: >> Welcome to the list! If you're curious as to what's happened, check >> the archives: >> http://mail.python.org/pipermail/python-list/ > Thanks! Incidentally, is there a good way to respond to the original > post in this thread, considering it wasn't delivered to me? I don't know of a way, but this works. It's all part of the same thread. > I mean, suppose '1' were an object and implemented > __lt__. Does it suddenly become not weak typing because of that? Is it weak typing to overload a function? //C++ likes this a lot. int foo(int x,int y) {return x*3+y;} double foo(double x,double y) {return x*3+y;} This is definitely making the term "strongly typed language" fairly useless. > (The other thing is that I think strong vs weak is more subjective than > many of the other measures. Is '1 < True' or '"1"+1' weaker? I think it > has a lot to do with how the operations provided by the language play to > your expectations.) +1. My expectations are: 1) The Boolean value "True" might be the same as a nonzero integer, or might not; it would make sense to use inequality comparisons with zero, MAYBE, but not with 1. So I don't particularly care what the language does with "1 < True", because it's not something that I would normally do. 2) "1"+1, in any high level language with an actual string type, I would expect to produce "11". It makes the most sense this way; having it return 2 means there's a special case where the string happens to look like a number - meaning that " 1"+1 is different from "1"+1. That just feels wrong to me... but I'm fully aware that many other people will disagree. Yep, it's pretty subjective. > On 12/17/2011 22:05, Chris Angelico wrote: >> Not quite; 1 + "one" will be "ne", which might happen to be at memory >> location 2. The data type is going to be char* (or, in a modern >> compiler, const char*), not int. > I'm not quite sure I'd say that it could be 2, exactly, but I definitely > disagree with this... after running 'int x = 5, *p = &x' would you say > that "p is 5"? (Assume &x != 5.) 1+"one" *points to* "ne", but it's > still a pointer. Point. I stand corrected. I tend to think of a char* as "being" the string, even though technically it only points to the beginning of it; it's the nearest thing C has to a string type. (To be honest, it's still a lot better than many high level languages' string types for certain common operations - eg trimming leading whitespace is pretty efficient on a PSZ.) In your example, p would be some integer value that is the pointer, but *p is 5. However, there's really no syntax in C to say what the "string value" is. ChrisA From rosuav at gmail.com Sun Dec 18 00:21:00 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 16:21:00 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: On Sun, Dec 18, 2011 at 3:52 PM, buck wrote: > The last one looks decorator-ish, but maybe that's proper. The implementation of this would be quite decorator-like: take the "normal" value of x, pass it through the indicated function, assign that value back to x. > > Try these on for size. > > ? ? head, @tuple tail = sequence > ? ? def foo(@list args, @dict kwargs): pass > ? ? foo(@args, @kwargs) That's reasonably clean as a concept, but it's not really quite the same. None of these examples is the way a decorator works; each of them requires a fundamental change to the way Python handles the rest of the statement. head, @tuple tail = sequence -- Does this mean "take the second element of a two-element sequence, pass it through tuple(), and store the result in tail"? Because, while that might be useful, and would make perfect sense as a decorator, it's insufficient as a replacement for current "*tail" syntax. ChrisA From edriscoll at wisc.edu Sun Dec 18 00:33:27 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 23:33:27 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: <4EED7B27.6000208@wisc.edu> On 12/17/2011 22:52, buck wrote: > Try these on for size. > > head, @tuple tail = sequence > def foo(@list args, @dict kwargs): pass > foo(@args, @kwargs) > > For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. > I like this idea much more than the original one. In addition to the arguments buck puts forth, which I find compelling, I have one more: you go to great length to say "this isn't really type checking in any sense" (which is true)... but then you go forth and pick a syntax that looks almost exactly like how you name types in many languages! (In fact, except for the fact that it's inline, the 'object :: type' syntax is *exactly* how you name types in Haskell.) buck's syntax still has some of the feel of "I wonder if this is type checking" to a newbie, but much much less IMO. I have a bigger objection with the general idea, however.It seems very strange that you should have to specify types to use it. If the */** syntax were removed, that would make the proposed syntax very very unusual for Python. I could be missing something, but I can think of any other place where you have to name a type except where the type is an integral part of what you're trying to do. (I would not say that choosing between tuples and lists are an integral part of dealing with vararg functions.) If */** were to stick around, I could see 99% of users continuing to use them. And then what has the new syntax achieved? You can fix this if you don't require the types and just allow the user to say "def foo(@args)" and "foo(@args)". Except... that's starting to look pretty familiar... (Not to mention if you just omit the type from the examples above you need another way to distinguish between args and kwargs.) I have one more suggestion. I do have one more thing to point out, which is that currently the Python vararg syntax is very difficult to Google for. In the first pages of the four searches matching "python (function)? (star | asterisk)", there was just one relevant hit on python.org which wasn't a bug report. I certainly remember having a small amount of difficulty figuring out what the heck * and ** did the first time I encountered them. This would suggest perhaps some keywords might be called for instead of operators. In the grand scheme of things the argument packing and unpacking are not *all* that common, so I don't think the syntactic burden would be immense. The bigger issue, of course, would be picking good words. This also helps with the issue above. Let's say we'll use 'varargs' and 'kwargs', though the latter too well-ingrained in code to steal. (I don't want to get too much into the debate over *what* word to choose. Also these don't match the 'head, *tail = l' syntax very well.) Then we could say: def foo(varargs l, kwargs d): bar(varargs l, kwargs d) and varargs would be equivalent to * and kwargs would be equivalent to **. But then you could also say def foo(varargs(list) l, kwargs(dict) d) Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From edriscoll at wisc.edu Sun Dec 18 00:54:35 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 17 Dec 2011 23:54:35 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4EED7B27.6000208@wisc.edu> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4EED7B27.6000208@wisc.edu> Message-ID: <4EED801B.2070208@wisc.edu> On 12/17/2011 23:33, Evan Driscoll wrote: > I do have one more thing to point out, which is that currently the > Python vararg syntax is very difficult to Google for. In the first pages > of the four searches matching "python (function)? (star | asterisk)", > there was just one relevant hit on python.org which wasn't a bug report. Though in the interest of full disclosure, and I should have said this before, the first hit for all of those searches except "python star" *is* relevant and reasonably helpful; just from someone's blog post instead of "from the horse's mouth", so to speak. Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From steve+comp.lang.python at pearwood.info Sun Dec 18 02:31:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 07:31:21 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 23:33:27 -0600, Evan Driscoll wrote: > I do have one more thing to point out, which is that currently the > Python vararg syntax is very difficult to Google for. You're coming in late to the conversation, but that was literally the second thing pointed out by the original poster: On Sun, 11 Dec 2011 11:49:23 +0100, Eelco Hoogendoorn wrote: > Throwing an idea for a PEP out there: > > It strikes me that the def func(*args, **kwargs) syntax is rather > unpytonic. It certainly did not have that 'for line in file' > pythonic obviousness for me as a beginner. Plus, asterikses are > impossible to google for, so finding out what exactly they do more > or less forces you to write a forum post about it. And rebutted. Modesty[1] prevents me from quoting myself, but here are some links to searches: http://duckduckgo.com/?q=python+asterisk http://duckduckgo.com/?q=python+* My normal first place to look for something is Wikipedia. Enjoy it before SOPA kills it. http://en.wikipedia.org/wiki/Asterisk#Programming_languages > In the first pages > of the four searches matching "python (function)? (star | asterisk)", Your search looks overly complicated to me. I'm not an expert on Google's syntax, but if you search for "python, optionally with function", isn't that the same as just searching for "python" since it will return hits either with or without "function"? > I certainly remember having a small amount of difficulty figuring out > what the heck * and ** did the first time I encountered them. Answering that sort of question is what the interactive interpreter excels at. Suppose you see a function declared with *args as an argument, and you have no idea what it means. Try it and find out! py> def spam(*args): ... print(args, type(args)) ... py> spam(42) ((42,), ) py> spam(42, 23) ((42, 23), ) Never underestimate the power of Python's introspection tools, especially the two simplest ones: print and type. Often you will learn more in 10 minutes experimentation than in an hour googling. [1] Eh, who am I fooling? -- Steven From no.email at nospam.invalid Sun Dec 18 02:55:13 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Sat, 17 Dec 2011 23:55:13 -0800 Subject: Debugging a difficult refcount issue. References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: <7xpqfm48m6.fsf@ruckus.brouhaha.com> buck writes: > I tried to pinpoint this intermediate allocation with a similar > PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. Did you try a gdb watchpoint? From steve+comp.lang.python at pearwood.info Sun Dec 18 03:36:12 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 08:36:12 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 23:33:27 -0600, Evan Driscoll wrote: > This would suggest perhaps some keywords might be called for instead of > operators. The barrier to new keywords in Python is very high. Not going to happen for something that already has perfectly good syntax already familiar to Python and Ruby programmers. Might else well try to get C and Java to stop using "..." (ellipses). > In the grand scheme of things the argument packing and > unpacking are not *all* that common, so I don't think the syntactic > burden would be immense. The burden is that you complicate the compiler and reduce the pool of useful names available to the programmer. To be useful, the keywords need to be short, meaningful and memorable -- which means they're already used in code, which you will break needlessly. With operators that otherwise would be illegal, the programmer doesn't need to learn about varargs until they need to. With keywords, the programmer needs to learn "you can't use varargs or kwargs as variable names" practically from day 1. > But then you could also say > def foo(varargs(list) l, kwargs(dict) d) So you're not just adding keywords, you're adding new syntax: something which looks like a function call, but isn't a function call. Another feature which the programmer has to learn just to be able to read Python source code -- and one which almost certainly is going to clash with function annotations as people start using them. I'm going to pose the same question to you I have already posed to Eelco: in your proposal, what happens if the caller has shadowed the list built- in? Does argument l become a built-in list, or does it become whatever type list is currently bound to? Some more questions: Can varargs accepted arbitrary callables, or only a fixed set of known types? How does this differ from what can be done with annotations? -- Steven From rosuav at gmail.com Sun Dec 18 03:43:09 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 19:43:09 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 18, 2011 at 6:31 PM, Steven D'Aprano wrote: > My normal first place to look for something is Wikipedia. Enjoy it before > SOPA kills it. > > http://en.wikipedia.org/wiki/Asterisk#Programming_languages I would never expect to find this sort of thing in the article on the asterisk; maybe I'd look on the language's article, but more likely I would be looking for info on the language's own web site. > On Sat, 17 Dec 2011 23:33:27 -0600, Evan Driscoll wrote: >> In the first pages >> of the four searches matching "python (function)? (star | asterisk)", > > Your search looks overly complicated to me. I understand this to mean that he did four searches: * python star * python function star * python asterisk * python function asterisk > Never underestimate the power of Python's introspection tools, especially > the two simplest ones: print and type. Often you will learn more in 10 > minutes experimentation than in an hour googling. +1 QOTW. I refer to this as "IIDPIO debugging" (Eyed Pio) - If In Doubt, Print It Out. ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 18 03:46:57 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 08:46:57 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eeda881$0$29979$c3e8da3$5496439d@news.astraweb.com> On Sat, 17 Dec 2011 21:03:01 -0600, Evan Driscoll wrote: > Something like ML or Haskell, which does not even allow integer to > double promotions, is very strong typing. Something like Java, which > allows some arithmetic conversion and also automatic stringification (a > la "1" + 1) is somewhere in the middle of the spectrum. Personally I'd > put Python even weaker on account of things such as '[1,2]*2' and '1 < > True' being allowed, They are not examples of weak typing. The first is an example of operator overloading, the second is a side-effect of a compromise made for backwards compatibility. If the first example were written: [1, 2].repeat(2) I expect you'd be perfectly happy with it as an unexceptional example of a list method: it takes an integer count, and returns a new list consisting of the elements of the original list repeated twice. If it were written: [1, 2].__mul__(2) you'd probably raise an eyebrow at the ugliness of the method name, but you would be okay with the concept. Well, that's exactly what it is in Python: the list __mul__ method performs sequence multiplication (repeating the contents), which overloads the * operator. No automatic type conversions needed, so not an example of weak typing. As for comparisons between ints and True or False, that's not weak typing either, because True and False *are* ints: bool is a subclass of int. This was done purely for historical reasons: originally Python didn't have a bool type, and you used 0 and 1 by convention for boolean values. When it was decided to add a bool type, the least disruptive way to do this was to define it as a subclass of int. -- Steven From gustavo.cordova at gmail.com Sun Dec 18 03:52:09 2011 From: gustavo.cordova at gmail.com (RangerElf) Date: Sun, 18 Dec 2011 00:52:09 -0800 (PST) Subject: Make a small function thread safe In-Reply-To: <4eebfbff$0$1679$742ec2ed@news.sonic.net> References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> <4eebfbff$0$1679$742ec2ed@news.sonic.net> Message-ID: <17238811.518.1324198329306.JavaMail.geo-discussion-forums@yqio4> Which is why the original .acquire() ... .release() idiom was wrong, this would better express the intent: try: lock.acquire() shared_container.append(...) finally: lock.release() But like everyone mentions, the with statement takes care of all that in a more readable and compact fashion. From kevin.p.dwyer at gmail.com Sun Dec 18 03:55:49 2011 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Sun, 18 Dec 2011 08:55:49 +0000 Subject: Bug in multiprocessing.reduction? References: Message-ID: Ya?ar Arabac? wrote: > You can see my all code below, theoritically that code should work I > guess. But I keep getting this error: > [SUBWARNING/MainProcess] thread for sharing handles raised exception : > ------------------------------------------------------------------------------- > Traceback (most recent call last): > File "/usr/lib/python2.7/multiprocessing/reduction.py", line 127, in > _serve send_handle(conn, handle_wanted, destination_pid) > File "/usr/lib/python2.7/multiprocessing/reduction.py", line 80, in > send_handle > _multiprocessing.sendfd(conn.fileno(), handle) > OSError: [Errno 9] Bad file descriptor > ------------------------------------------------------------------------------- > > Do you see an error in my side, or is this a bug in Python? > Hello, I don't know much about the multiprocessing module, so I won't speculate about bugs ;) however I can tell you that your code works with error on my machine: kev at pluto:~> python mtest.py [DEBUG/MainProcess] created semlock with handle 140252275732480 [DEBUG/MainProcess] created semlock with handle 140252275728384 [DEBUG/MainProcess] created semlock with handle 140252275724288 [DEBUG/MainProcess] Queue._after_fork() [DEBUG/Process-1] Queue._after_fork() [INFO/Process-1] child process calling self.run() [DEBUG/Process-2] Queue._after_fork() [INFO/Process-2] child process calling self.run() [DEBUG/Process-3] Queue._after_fork() [INFO/Process-3] child process calling self.run() [DEBUG/Process-4] Queue._after_fork() [INFO/Process-4] child process calling self.run() [DEBUG/Process-5] Queue._after_fork() [INFO/Process-5] child process calling self.run() [DEBUG/MainProcess] starting listener and thread for sending handles [INFO/MainProcess] created temp directory /tmp/pymp-J3UxCe [DEBUG/MainProcess] Queue._start_thread() [DEBUG/MainProcess] doing self._thread.start() [DEBUG/MainProcess] starting thread to feed data to pipe [DEBUG/MainProcess] ... done self._thread.start() Here "Here" was added to your print line by me. Otherwise I ran your code as is, and sent the string "Hello world" to port 9090 from another console. Hope that helps, Kev From vs at it.uu.se Sun Dec 18 05:31:33 2011 From: vs at it.uu.se (Virgil Stokes) Date: Sun, 18 Dec 2011 11:31:33 +0100 Subject: Module msvcrt for Python Message-ID: <4EEDC105.9010506@it.uu.se> I am running Python 2.6.6 on a Windows Vista platform and for some reason the module msvcrt is not present. How can I install the msvcrt module in my Python 2.6.6? God Jul :-) From nukeymusic at gmail.com Sun Dec 18 05:37:57 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 02:37:57 -0800 (PST) Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> Message-ID: <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> On 17 dec, 12:20, "G?nther Dietrich" wrote: > nukeymusic wrote: > >I'm trying to calculate the difference in seconds between two > > [...] > > >>> import datetime > >>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", "%b-%d-%H:%M:%S") > >>> date2 = datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") > >>> delta = date2 - date1 > >>> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000) > > For very big time differences you should consider to use the Decimal > arithmetics (standard module Decimal) instead of integer arithmetics > for the last line. > If you are sure, that you don't use fractional seconds, you can omit > the part with 'delta.microseconds'. > > Best regards, > > G?nther That can very much G?nther, this helped me a lot further, I'm only struggling with one more problem to finish my first python-program. Could you tell me why I can't write to the outputfile as I do in the code below:? #!/usr/bin/python #version 16/12/2011 #Example of testfile #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 import datetime f = open('testfile','r') g = open('outputfile','w') #get line 1 from input file: line1=f.readline() #get first element in line 1: date1=line1.rsplit()[0] #convert first element tot structured date time struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") for line in f: temp=line.rsplit() delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000) temp[0]=delta_seconds #the following line is wrong, but I don't know how to fix it: g.write(temp) #Close files f.close() g.close() thanks in advance nukey From nukeymusic at gmail.com Sun Dec 18 06:00:26 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 03:00:26 -0800 (PST) Subject: how to run python-script from the python promt? [absolute newbie] Message-ID: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> How can I load a python-script after starting python in the interactive mode? I tried with >>>load 'myscript.py' >>>myscript.py >>>myscript but none of these works, so the only way I could work further until now was copy/paste line per line of my python-script to the interactive mode prompt I do know how to run the script non-interactively, but what I want to do is adding lines to the code I have written thus far in interactive mode. thanks in advance nukey From rosuav at gmail.com Sun Dec 18 06:29:49 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 18 Dec 2011 22:29:49 +1100 Subject: how to run python-script from the python promt? [absolute newbie] In-Reply-To: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: On Sun, Dec 18, 2011 at 10:00 PM, nukeymusic wrote: > How can I load a python-script after starting python in the > interactive mode? > I tried with >>>>load 'myscript.py' >>>>myscript.py >>>>myscript > > but none of these works, so the only way I could work further until > now was copy/paste line per line of my python-script to the > interactive mode prompt The easiest way to load a file is to import it. However, this is not quite identical to loading the script into the current session. If your script mainly defines functions, you can either: import myscript or from myscript import * and it'll do more or less what you expect; however, it will execute in its own module context, so globals from your current session won't be available to it. Tip for pasting: Indent every line of your code at least one space/tab, prefix it with "if True:", and then you can paste it all at once. ChrisA From __peter__ at web.de Sun Dec 18 06:43:44 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 18 Dec 2011 12:43:44 +0100 Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> Message-ID: nukeymusic wrote: > On 17 dec, 12:20, "G?nther Dietrich" wrote: >> nukeymusic wrote: >> >I'm trying to calculate the difference in seconds between two >> >> [...] >> >> >>> import datetime >> >>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", >> >>> "%b-%d-%H:%M:%S") date2 = >> >>> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta >> >>> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + >> >>> delta.seconds + ((delta.microseconds + 500000) / 1000000) >> >> For very big time differences you should consider to use the Decimal >> arithmetics (standard module Decimal) instead of integer arithmetics >> for the last line. >> If you are sure, that you don't use fractional seconds, you can omit >> the part with 'delta.microseconds'. >> >> Best regards, >> >> G?nther > That can very much G?nther, this helped me a lot further, I'm only > struggling with one more problem to finish my first python-program. > Could you > tell me why I can't write to the outputfile as I do in the code > below:? > #!/usr/bin/python > #version 16/12/2011 > #Example of testfile > #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 > #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 > import datetime > f = open('testfile','r') > g = open('outputfile','w') > #get line 1 from input file: > line1=f.readline() > #get first element in line 1: > date1=line1.rsplit()[0] > #convert first element tot structured date time > struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > for line in f: > temp=line.rsplit() > delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- > datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + > ((delta.microseconds + 500000) / 1000000) > temp[0]=delta_seconds > #the following line is wrong, but I don't know how to fix it: > g.write(temp) > #Close files > f.close() > g.close() The write() method only accepts strings; you have to convert the temp list to a string before passing it on. The minimal change: for line in f: temp = line.rsplit() delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") -datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")) delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000)) temp[0] = str(delta_seconds) g.write(" ".join(temp) + "\n") Other observations: - you are repeating calculations in the loop that you can do (and did) outside. - use four-space indent for better readability - there's no need to use rsplit(); use split() After a few other modifications: import datetime def parse_line(line): date, rest = line.split(None, 1) date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") return date, rest with open('testfile','r') as f: with open('outputfile','w') as g: first_date, first_rest = parse_line(next(f)) for line in f: cur_date, rest = parse_line(line) delta = cur_date - first_date delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000)) g.write("%s %s" % (delta_seconds, rest)) From kevin.p.dwyer at gmail.com Sun Dec 18 06:45:01 2011 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Sun, 18 Dec 2011 11:45:01 +0000 Subject: how to run python-script from the python promt? [absolute newbie] References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: nukeymusic wrote: > How can I load a python-script after starting python in the > interactive mode? > I tried with >>>>load 'myscript.py' >>>>myscript.py >>>>myscript > > but none of these works, so the only way I could work further until > now was copy/paste line per line of my python-script to the > interactive mode prompt > I do know how to run the script non-interactively, but what I want to > do is adding lines to the code I have written thus far in interactive > mode. > > thanks in advance > nukey Hello, You can make the code in your script available to the interpreter by typing import myscript (assuming that you are running the interpreter in the same directory that contains myscript.py) You can access functions, classes and other top-level objects in your script by prefixing their names with "myscript" and a dot (".") e.g. myscript.myfunc, myscript.MyClass, myscript.myvar You can't really edit your script in the interpreter, but you can edit and save in a text editor and then type reload(myscript) in the interpreter to refresh its version of the myscript code. N.B. when you import/reload your script the interpreter will immediately execute any code that is not enclosed in a function or class definition. Cheers, Kev From vs at it.uu.se Sun Dec 18 06:52:51 2011 From: vs at it.uu.se (Virgil Stokes) Date: Sun, 18 Dec 2011 12:52:51 +0100 Subject: Module msvcrt for Python In-Reply-To: <4EEDC105.9010506@it.uu.se> References: <4EEDC105.9010506@it.uu.se> Message-ID: <4EEDD413.2040302@it.uu.se> On 18-Dec-2011 11:31, Virgil Stokes wrote: > I am running Python 2.6.6 on a Windows Vista platform and for some reason the > module msvcrt is not present. > > How can I install the msvcrt module in my Python 2.6.6? > > God Jul :-) I found the problem! My code was using Python 2.5 (inside cygwin) by default and there it was unable to import msvcrt. But, when I ran the same code in my Python 2.6 installation it worked as it should (msvcrt module was imported). Sorry for the "noise". Best :-) From lie.1296 at gmail.com Sun Dec 18 07:39:58 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 18 Dec 2011 23:39:58 +1100 Subject: how to run python-script from the python promt? [absolute newbie] In-Reply-To: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: On 12/18/2011 10:00 PM, nukeymusic wrote: > How can I load a python-script after starting python in the > interactive mode? > I tried with >>>> load 'myscript.py' >>>> myscript.py >>>> myscript > > but none of these works, so the only way I could work further until > now was copy/paste line per line of my python-script to the > interactive mode prompt > I do know how to run the script non-interactively, but what I want to > do is adding lines to the code I have written thus far in interactive > mode. > > thanks in advance > nukey The normal python shell doesn't directly support doing that, although there are several workaround with (ab)using the 'import' statement, it had several subtleties with how module are cached. Try the ipython shell; in ipython you can load a file into the current interpreter session using the %run magic command. From lie.1296 at gmail.com Sun Dec 18 07:58:21 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 18 Dec 2011 23:58:21 +1100 Subject: calculate difference between two timestamps [newbie] In-Reply-To: References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> Message-ID: On 12/18/2011 10:43 PM, Peter Otten wrote: > nukeymusic wrote: > >> On 17 dec, 12:20, "G?nther Dietrich" wrote: >>> nukeymusic wrote: >>>> I'm trying to calculate the difference in seconds between two >>> >>> [...] >>> >>>>>> import datetime >>>>>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", >>>>>> "%b-%d-%H:%M:%S") date2 = >>>>>> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta >>>>>> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + >>>>>> delta.seconds + ((delta.microseconds + 500000) / 1000000) >>> >>> For very big time differences you should consider to use the Decimal >>> arithmetics (standard module Decimal) instead of integer arithmetics >>> for the last line. >>> If you are sure, that you don't use fractional seconds, you can omit >>> the part with 'delta.microseconds'. >>> >>> Best regards, >>> >>> G?nther >> That can very much G?nther, this helped me a lot further, I'm only >> struggling with one more problem to finish my first python-program. >> Could you >> tell me why I can't write to the outputfile as I do in the code >> below:? >> #!/usr/bin/python >> #version 16/12/2011 >> #Example of testfile >> #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 >> #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 >> #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 >> import datetime >> f = open('testfile','r') >> g = open('outputfile','w') >> #get line 1 from input file: >> line1=f.readline() >> #get first element in line 1: >> date1=line1.rsplit()[0] >> #convert first element tot structured date time >> struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") >> for line in f: >> temp=line.rsplit() >> delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- >> datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") >> delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + >> ((delta.microseconds + 500000) / 1000000) >> temp[0]=delta_seconds >> #the following line is wrong, but I don't know how to fix it: >> g.write(temp) >> #Close files >> f.close() >> g.close() > > The write() method only accepts strings; you have to convert the temp list > to a string before passing it on. The minimal change: > > for line in f: > temp = line.rsplit() > delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") > -datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S")) > delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > + ((delta.microseconds + 500000) / 1000000)) > temp[0] = str(delta_seconds) > g.write(" ".join(temp) + "\n") > > Other observations: > > - you are repeating calculations in the loop that you can do (and did) > outside. > > - use four-space indent for better readability > > - there's no need to use rsplit(); use split() > > After a few other modifications: > > import datetime > > def parse_line(line): > date, rest = line.split(None, 1) > date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > return date, rest > > with open('testfile','r') as f: > with open('outputfile','w') as g: > first_date, first_rest = parse_line(next(f)) > for line in f: > cur_date, rest = parse_line(line) > delta = cur_date - first_date > delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > + ((delta.microseconds + 500000) / 1000000)) > g.write("%s %s" % (delta_seconds, rest)) > minor improvement, you can do: with open('testfile','r') as f, open('outputfile','w') as g: ... instead of the nested with-block. Also, you can use `delta.total_seconds()` instead of `delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 500000) / 1000000))` Therefore (untested): import datetime def parse_line(line): date, rest = line.split(None, 1) date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") return date, rest with open('testfile','r') as f, open('outputfile','w') as g: first_date, first_rest = parse_line(next(f)) for line in f: cur_date, rest = parse_line(line) delta = cur_date - first_date g.write("%s %s" % (int(round(delta.total_seconds())), rest)) From nukeymusic at gmail.com Sun Dec 18 08:16:49 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 05:16:49 -0800 (PST) Subject: how to run python-script from the python promt? [absolute newbie] References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: On 18 dec, 13:39, Lie Ryan wrote: > On 12/18/2011 10:00 PM, nukeymusic wrote: > > > > > > > > > > > How can I load a python-script after starting python in the > > interactive mode? > > I tried with > >>>> load 'myscript.py' > >>>> myscript.py > >>>> myscript > > > but none of these works, so the only way I could work further until > > now was copy/paste line per line of my python-script to the > > interactive mode prompt > > I do know how to run the script non-interactively, but what I want to > > do is adding lines to the code I have written thus far in interactive > > mode. > > > thanks in advance > > nukey > > The normal python shell doesn't directly support doing that, although > there are several workaround with (ab)using the 'import' statement, it > had several subtleties with how module are cached. Try the ipython > shell; in ipython you can load a file into the current interpreter > session using the %run magic command. I guess you mean the following command? %run 'myscript.py' is this correct? thanks nukey From roy at panix.com Sun Dec 18 08:58:28 2011 From: roy at panix.com (Roy Smith) Date: Sun, 18 Dec 2011 08:58:28 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Chris Angelico wrote: > > Never underestimate the power of Python's introspection tools, especially > > the two simplest ones: print and type. Often you will learn more in 10 > > minutes experimentation than in an hour googling. > > +1 QOTW. I refer to this as "IIDPIO debugging" (Eyed Pio) - If In > Doubt, Print It Out. I always knew it by the name "Ask the computer". As in, "WTF is foo? Let's ask the computer!". In addition to print and type, I'm a big fan of dir(). Often, I know an object has a method to do what I want, but I can't remember the name. For example, the other day, I was using a set (which I don't use very often). I needed the method to remove an item from the set. Faster than finding the right place in the docs, I just fired up an interpreter and typed dir(set()) at it. From nukeymusic at gmail.com Sun Dec 18 09:03:18 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 06:03:18 -0800 (PST) Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> Message-ID: <8d1ec984-9786-4b0c-924b-01f661c1374c@q11g2000vbq.googlegroups.com> On 18 dec, 13:58, Lie Ryan wrote: > On 12/18/2011 10:43 PM, Peter Otten wrote: > > > > > > > > > > > nukeymusic wrote: > > >> On 17 dec, 12:20, "G?nther Dietrich" ?wrote: > >>> nukeymusic ?wrote: > >>>> I'm trying to calculate the difference in seconds between two > > >>> [...] > > >>>>>> import datetime > >>>>>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", > >>>>>> "%b-%d-%H:%M:%S") date2 = > >>>>>> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta > >>>>>> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + > >>>>>> delta.seconds + ((delta.microseconds + 500000) / 1000000) > > >>> For very big time differences you should consider to use the Decimal > >>> arithmetics (standard module Decimal) instead of integer arithmetics > >>> for the last line. > >>> If you are sure, that you don't use fractional seconds, you can omit > >>> the part with 'delta.microseconds'. > > >>> Best regards, > > >>> G?nther > >> That can very much G?nther, this helped me a lot further, I'm only > >> struggling with one more problem to finish my first python-program. > >> Could you > >> tell me why I can't write to the outputfile as I do in the code > >> below:? > >> #!/usr/bin/python > >> #version 16/12/2011 > >> #Example of testfile > >> #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > >> #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 > >> #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 > >> import datetime > >> f = open('testfile','r') > >> g = open('outputfile','w') > >> #get line 1 from input file: > >> line1=f.readline() > >> #get first element in line 1: > >> date1=line1.rsplit()[0] > >> #convert first element tot structured date time > >> struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > >> for line in f: > >> ? temp=line.rsplit() > >> ? delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- > >> datetime.datetime.strptime(date1, ?"%b-%d-%H:%M:%S") > >> ? delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + > >> ((delta.microseconds + 500000) / 1000000) > >> ? temp[0]=delta_seconds > >> #the following line is wrong, but I don't know how to fix it: > >> ? g.write(temp) > >> #Close files > >> f.close() > >> g.close() > > > The write() method only accepts strings; you have to convert the temp list > > to a string before passing it on. The minimal change: > > > for line in f: > > ? ? ? temp = line.rsplit() > > ? ? ? delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") > > ? ? ? ? ? ? ? ?-datetime.datetime.strptime(date1, ?"%b-%d-%H:%M:%S")) > > ? ? ? delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > ? ? ? ? ? ? ? ? ? ? ? ?+ ((delta.microseconds + 500000) / 1000000)) > > ? ? ? temp[0] = str(delta_seconds) > > ? ? ? g.write(" ".join(temp) + "\n") > > > Other observations: > > > - you are repeating calculations in the loop that you can do (and did) > > outside. > > > - use four-space indent for better readability > > > - there's no need to use rsplit(); use split() > > > After a few other modifications: > > > import datetime > > > def parse_line(line): > > ? ? ?date, rest = line.split(None, 1) > > ? ? ?date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > > ? ? ?return date, rest > > > with open('testfile','r') as f: > > ? ? ?with open('outputfile','w') as g: > > ? ? ? ? ?first_date, first_rest = parse_line(next(f)) > > ? ? ? ? ?for line in f: > > ? ? ? ? ? ? ?cur_date, rest = parse_line(line) > > ? ? ? ? ? ? ?delta = cur_date - first_date > > ? ? ? ? ? ? ?delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? + ((delta.microseconds + 500000) / 1000000)) > > ? ? ? ? ? ? ?g.write("%s %s" % (delta_seconds, rest)) > > minor improvement, you can do: > > with open('testfile','r') as f, open('outputfile','w') as g: > ? ? ?... > > instead of the nested with-block. > > Also, you can use `delta.total_seconds()` instead of `delta_seconds = > ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + > 500000) / 1000000))` > > Therefore (untested): > > import datetime > > def parse_line(line): > ? ? ?date, rest = line.split(None, 1) > ? ? ?date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > ? ? ?return date, rest > > with open('testfile','r') as f, open('outputfile','w') as g: > ? ? ?first_date, first_rest = parse_line(next(f)) > ? ? ?for line in f: > ? ? ? ? ?cur_date, rest = parse_line(line) > ? ? ? ? ?delta = cur_date - first_date > ? ? ? ? ?g.write("%s %s" % (int(round(delta.total_seconds())), rest)) thanks and also thanks to all the others who were so kind to help me out with my first python-script. I tested your alternatives and they work, the only a minor inconvenience is that the first line of the inputfile gets lost i.e. the first timestamp should become zero (seconds) best regards, nukey From rosuav at gmail.com Sun Dec 18 09:06:34 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Dec 2011 01:06:34 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 19, 2011 at 12:58 AM, Roy Smith wrote: > In addition to print and type, I'm a big fan of dir(). ?Often, I know an > object has a method to do what I want, but I can't remember the name. > For example, the other day, I was using a set (which I don't use very > often). ?I needed the method to remove an item from the set. ?Faster > than finding the right place in the docs, I just fired up an interpreter > and typed dir(set()) at it. That's excellent when all you need is the name. I usually have IDLE running (all the time - which sometimes produces oddities after I experiment with monkey-patching some module or other, and then oddly enough, things don't work properly!!), and will snap off "help(foo)" for any foo. Unfortunately help() is at times unhelpful, and even at its best it's very spammy. There's no one best solution; the successful programmer will usually have a large toolset at his disposal. ChrisA From hoogendoorn.eelco at gmail.com Sun Dec 18 09:13:37 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 18 Dec 2011 06:13:37 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9c808e14-fde9-49e7-b051-84ff45d663d5@i8g2000vbh.googlegroups.com> On Dec 18, 1:59?am, Steven D'Aprano wrote: > On Sat, 17 Dec 2011 06:38:22 -0800, Eelco wrote: > > Type constraints: > > > In case the asterisk is not used to signal unpacking, but rather to > > signal packing, its semantics is essentially that of a type constraint. > > "Type constraint" normally refers to type restrictions on *input*: it is > a restriction on what types are accepted. When it refers to output, it is > not normally a restriction, therefore "constraint" is inappropriate. > Instead it is normally described as a coercion, cast or conversion. > Automatic type conversions are the opposite of a constraint: it is a > loosening of restrictions. "I don't have to use a list, I can use any > sequence or iterator". Casts or conversions are a runtime concept; im talking about declarations. That seems to be the source of your confusion. > In iterator unpacking, it is the *output* which is a list, not a > restriction on input: in the statement: > > head, *tail = sequence > > tail may not exist before the assignment, and so describing this as a > constraint on the type of tail is completely inappropriate. Yes, the variable tail is being (re)declared here. Thats exactly why I call it a type constraint. Im not sure what the CS books have to say on the matter, I guess this use of the term entered my lexicon through the C# developer team. Either way, you seem to be the only one who does not grok my intended meaning, so I suggest you try reading it again. > > The statement: > > > ? ? head, tail = sequence > > > Signifies regular unpacking. However, if we add an asterisk, as in: > > > ? ? head, *tail = sequence > > > We demand that tail not be just any python object, but rather a list. > > We don't demand anything, any more than when we say: > > for x in range(1, 100): > > we "demand" that x is not just any python object, but rather an int. > > Rather, we accept what we're given: in case of range and the for loop, we > are given an int. In the case of extended tuple unpacking, we are given a > list. for x in range is syntactic sugar for a series of assignments to x; x is an unconstrained variable that will indeed take anything it gets; the semantics of what comes after 'x' does in no way depend on x itself. head, tail = l and head, *tail = l mean something completely different, and the only difference is a constraint placed on tail, which forces the semantics to be different; the righthand side, or what is to be assigned, is identical. Of course one can just regard it as syntactic sugar for head, tail = unpackheadandtailaslist(l); but the syntactic sugar achieves that same end through a type constraint on tail. Really. > You are jumping to conclusions about implementation details which aren't > supported by the visible behaviour. What evidence do you have that > iterator unpacking creates a tuple first and then converts it to a list? You are jumping to conclusions about my opinion which aren't supported by my visible behaviour. What evidence do you have that I ever even said any such thing? > > The aim of this PEP, is that this type-constraint syntax is expanded > > upon. We should be careful here to distinguish with providing optional > > type constraints throughout python as a whole; this is not our aim. > > Iterator unpacking is no more about type constraints than is len(). Because you wish to keep nitpicking about my usage of the term 'type constraint' (even though you have not introduced an alternative term yourself), or because you actually disagree with the content of my message? From hoogendoorn.eelco at gmail.com Sun Dec 18 09:23:51 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 18 Dec 2011 06:23:51 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: On Dec 18, 5:52?am, buck wrote: > I like the spirit of this. Let's look at your examples. Glad to see an actual on-topic reply; thanks. > > Examples of use: > > ? ? head, tail::tuple = ::sequence > > ? ? def foo(args::list, kwargs::dict): pass > > ? ? foo(::args, ::kwargs) > > My initial reaction was "nonono!", but this is simply because of the ugliness. The double-colon is very visually busy. I would have preferred the single colon, but its taken. But exactly this syntax is used in other languages (for what that is worth...) > I find that your second example is inconsistent with the others. If we say that the variable-name is always on the right-hand-side, we get: > > > ? ? def foo(list::args, dict::kwargs): pass > > This nicely mirrors other languages (such as in your C# example: ?"float foo") as well as the old python behavior (prefixing variables with */** to modify the assignment). The link in my OP has the BDFL discussing type constraints; he also prefers identifier:type. Many modern languages have something similar. How is my second example inconsistent? > As for the separator, let's examine the available ascii punctuation. Excluding valid variable characters, whitespace, and operators, we have: > > ! -- ok. > " -- can't use this. Would look like a string. > # -- no. Would looks like a comment. > $ -- ok. > ' -- no. Would look like a string. > ( -- no. Would look like a function. > ) -- no. Would look like ... bad syntax. > , -- no. Would indicate a separate item in the variable list. > . -- no. Would look like an attribute. > : -- ok, maybe. Seems confusing in a colon-terminated statement. > ; -- no, just no. > ? -- ok. > @ -- ok. > [ -- no. Would look like indexing. > ] -- no. > ` -- no. Would look like a string? > { -- too strange} -- too strange > > ~ -- ok. > > That leaves these. Which one looks least strange? > > float ! x = 1 > float $ x = 1 > float ? x = 1 > float @ x = 1 > > The last one looks decorator-ish, but maybe that's proper. The implementation of this would be quite decorator-like: take the "normal" value of x, pass it through the indicated function, assign that value back to x. I dont think thats too proper an analogy. The type constraint does not just coerce the content that is bound to the variable, it influences the semantics of the whole statement; so insofar there is a suggested relation with decorators, id rather not have it. > > Try these on for size. > > ? ? ?head, @tuple tail = sequence > ? ? ?def foo(@list args, @dict kwargs): pass > ? ? ?foo(@args, @kwargs) > > For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. Yes, maximum backwards compatibility would be great. From nukeymusic at gmail.com Sun Dec 18 09:26:25 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 06:26:25 -0800 (PST) Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> Message-ID: <35ff07d2-540c-483e-b66b-edfa66540bc6@z17g2000vbe.googlegroups.com> On 18 dec, 13:58, Lie Ryan wrote: > On 12/18/2011 10:43 PM, Peter Otten wrote: > > > > > > > > > > > nukeymusic wrote: > > >> On 17 dec, 12:20, "G?nther Dietrich" ?wrote: > >>> nukeymusic ?wrote: > >>>> I'm trying to calculate the difference in seconds between two > > >>> [...] > > >>>>>> import datetime > >>>>>> date1 = datetime.datetime.strptime("Dec-13-09:47:12", > >>>>>> "%b-%d-%H:%M:%S") date2 = > >>>>>> datetime.datetime.strptime("Dec-13-09:47:39", "%b-%d-%H:%M:%S") delta > >>>>>> = date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) + > >>>>>> delta.seconds + ((delta.microseconds + 500000) / 1000000) > > >>> For very big time differences you should consider to use the Decimal > >>> arithmetics (standard module Decimal) instead of integer arithmetics > >>> for the last line. > >>> If you are sure, that you don't use fractional seconds, you can omit > >>> the part with 'delta.microseconds'. > > >>> Best regards, > > >>> G?nther > >> That can very much G?nther, this helped me a lot further, I'm only > >> struggling with one more problem to finish my first python-program. > >> Could you > >> tell me why I can't write to the outputfile as I do in the code > >> below:? > >> #!/usr/bin/python > >> #version 16/12/2011 > >> #Example of testfile > >> #Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00 > >> #Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00 > >> #Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00 > >> import datetime > >> f = open('testfile','r') > >> g = open('outputfile','w') > >> #get line 1 from input file: > >> line1=f.readline() > >> #get first element in line 1: > >> date1=line1.rsplit()[0] > >> #convert first element tot structured date time > >> struct_date1=datetime.datetime.strptime(date1, "%b-%d-%H:%M:%S") > >> for line in f: > >> ? temp=line.rsplit() > >> ? delta=datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S")- > >> datetime.datetime.strptime(date1, ?"%b-%d-%H:%M:%S") > >> ? delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds + > >> ((delta.microseconds + 500000) / 1000000) > >> ? temp[0]=delta_seconds > >> #the following line is wrong, but I don't know how to fix it: > >> ? g.write(temp) > >> #Close files > >> f.close() > >> g.close() > > > The write() method only accepts strings; you have to convert the temp list > > to a string before passing it on. The minimal change: > > > for line in f: > > ? ? ? temp = line.rsplit() > > ? ? ? delta = (datetime.datetime.strptime(temp[0], "%b-%d-%H:%M:%S") > > ? ? ? ? ? ? ? ?-datetime.datetime.strptime(date1, ?"%b-%d-%H:%M:%S")) > > ? ? ? delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > ? ? ? ? ? ? ? ? ? ? ? ?+ ((delta.microseconds + 500000) / 1000000)) > > ? ? ? temp[0] = str(delta_seconds) > > ? ? ? g.write(" ".join(temp) + "\n") > > > Other observations: > > > - you are repeating calculations in the loop that you can do (and did) > > outside. > > > - use four-space indent for better readability > > > - there's no need to use rsplit(); use split() > > > After a few other modifications: > > > import datetime > > > def parse_line(line): > > ? ? ?date, rest = line.split(None, 1) > > ? ? ?date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > > ? ? ?return date, rest > > > with open('testfile','r') as f: > > ? ? ?with open('outputfile','w') as g: > > ? ? ? ? ?first_date, first_rest = parse_line(next(f)) > > ? ? ? ? ?for line in f: > > ? ? ? ? ? ? ?cur_date, rest = parse_line(line) > > ? ? ? ? ? ? ?delta = cur_date - first_date > > ? ? ? ? ? ? ?delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? + ((delta.microseconds + 500000) / 1000000)) > > ? ? ? ? ? ? ?g.write("%s %s" % (delta_seconds, rest)) > > minor improvement, you can do: > > with open('testfile','r') as f, open('outputfile','w') as g: > ? ? ?... > > instead of the nested with-block. > > Also, you can use `delta.total_seconds()` instead of `delta_seconds = > ((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + > 500000) / 1000000))` > > Therefore (untested): > > import datetime > > def parse_line(line): > ? ? ?date, rest = line.split(None, 1) > ? ? ?date = datetime.datetime.strptime(date, "%b-%d-%H:%M:%S") > ? ? ?return date, rest > > with open('testfile','r') as f, open('outputfile','w') as g: > ? ? ?first_date, first_rest = parse_line(next(f)) > ? ? ?for line in f: > ? ? ? ? ?cur_date, rest = parse_line(line) > ? ? ? ? ?delta = cur_date - first_date > ? ? ? ? ?g.write("%s %s" % (int(round(delta.total_seconds())), rest)) thanks to you and all the others for helping me out with my first python-script. I tested your alternatives and they do work. The only minor inconvenience with the scripts is that the output-file misses the first line i.e. the first timestamp should be reset to zero (seconds), but as these log-files are very large, missing one measurement is not too bad best regards nukey From hoogendoorn.eelco at gmail.com Sun Dec 18 09:35:36 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 18 Dec 2011 06:35:36 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> On Dec 18, 6:33?am, Evan Driscoll wrote: > On 12/17/2011 22:52, buck wrote:> Try these on for size. > > > ? ? ?head, @tuple tail = sequence > > ? ? ?def foo(@list args, @dict kwargs): pass > > ? ? ?foo(@args, @kwargs) > > > For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. > > I like this idea much more than the original one. In addition to the > arguments buck puts forth, which I find compelling, I have one more: you > go to great length to say "this isn't really type checking in any sense" > (which is true)... but then you go forth and pick a syntax that looks > almost exactly like how you name types in many languages! (In fact, > except for the fact that it's inline, the 'object :: type' syntax is > *exactly* how you name types in Haskell.) No, its not type *checking*, its type *declaration*. I tried to go to great lengths to point that out, but it appears I did not do a very good job :). Type declaration is exactly what I want, and insofar this syntax has already found adoptation elsewhere, ill consider that a plus. > I have a bigger objection with the general idea, however.It seems very > strange that you should have to specify types to use it. If the */** > syntax were removed, that would make the proposed syntax very very > unusual for Python. I could be missing something, but I can think of any > other place where you have to name a type except where the type is an > integral part of what you're trying to do. (I would not say that > choosing between tuples and lists are an integral part of dealing with > vararg functions.) If */** were to stick around, I could see 99% of > users continuing to use them. And then what has the new syntax achieved? Good point; technically the askeriskes could be kept for backwards compatibility, but that would break 'there should only be one way to do it'. Indeed it would be unusual for python to have to explicitly name a type, but implicitly ** does very much the same. Its just a cryptic way of saying 'dict please'. > You can fix this if you don't require the types and just allow the user > to say "def foo(@args)" and "foo(@args)". Except... that's starting to > look pretty familiar... (Not to mention if you just omit the type from > the examples above you need another way to distinguish between args and > kwargs.) Yes, one could opt for a syntax where the collection type is optional and a sensible default is chosen, But to me that would largely defeat the point; I very much like the added verbosity and explicitness. args- tuple and kwargs-dict; that just has a much better ring to it than star-star-kwargs, or whatever other cryptic symbol you use. > I have one more suggestion. > > I do have one more thing to point out, which is that currently the > Python vararg syntax is very difficult to Google for. In the first pages > of the four searches matching "python (function)? (star | asterisk)", > there was just one relevant hit on python.org which wasn't a bug report. > I certainly remember having a small amount of difficulty figuring out > what the heck * and ** did the first time I encountered them. > > This would suggest perhaps some keywords might be called for instead of > operators. In the grand scheme of things the argument packing and > unpacking are not *all* that common, so I don't think the syntactic > burden would be immense. The bigger issue, of course, would be picking > good words. > > This also helps with the issue above. Let's say we'll use 'varargs' and > 'kwargs', though the latter too well-ingrained in code to steal. (I > don't want to get too much into the debate over *what* word to choose. > Also these don't match the 'head, *tail = l' syntax very well.) Then we > could say: > ? def foo(varargs l, kwargs d): > ? ? ? bar(varargs l, kwargs d) > and varargs would be equivalent to * and kwargs would be equivalent to > **. But then you could also say > ? def foo(varargs(list) l, kwargs(dict) d) I agree; I had the same experience. But according to others our opinion is wrong, and we should just become better at using google. From __peter__ at web.de Sun Dec 18 10:01:25 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 18 Dec 2011 16:01:25 +0100 Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> <8d1ec984-9786-4b0c-924b-01f661c1374c@q11g2000vbq.googlegroups.com> Message-ID: nukeymusic wrote: > thanks and also thanks to all the others who were so kind to help me > out with my first python-script. > I tested your alternatives and they work, the only a minor > inconvenience is that the first line of the inputfile gets lost i.e. > the first timestamp should become zero (seconds) That should be easy to fix: >> with open('testfile','r') as f, open('outputfile','w') as g: >> first_date, first_rest = parse_line(next(f)) g.write("0 %s" % first_rest) >> for line in f: >> cur_date, rest = parse_line(line) >> delta = cur_date - first_date >> g.write("%s %s" % (int(round(delta.total_seconds())), rest)) From nukeymusic at gmail.com Sun Dec 18 10:22:23 2011 From: nukeymusic at gmail.com (nukeymusic) Date: Sun, 18 Dec 2011 07:22:23 -0800 (PST) Subject: calculate difference between two timestamps [newbie] References: <36f818ba-b0b0-413f-a3e9-1485a2acb238@u6g2000vbg.googlegroups.com> <4af23f4a-00d8-41a9-a6aa-4f57b60c9c30@y7g2000vbe.googlegroups.com> <8d1ec984-9786-4b0c-924b-01f661c1374c@q11g2000vbq.googlegroups.com> Message-ID: <5b86f74b-df94-462e-b022-3119f022995d@z17g2000vbe.googlegroups.com> On 18 dec, 16:01, Peter Otten <__pete... at web.de> wrote: > nukeymusic wrote: > > thanks and also thanks to all the others who were so kind to help me > > out with my first python-script. > > I tested your alternatives and they work, the only a minor > > inconvenience is that the first line of the inputfile gets lost i.e. > > the first timestamp should become zero (seconds) > > That should be easy to fix: > > >> with open('testfile','r') as f, open('outputfile','w') as g: > >> ? ? ?first_date, first_rest = parse_line(next(f)) > > ? ? ? ? g.write("0 %s" % first_rest) > > > > > > > > >> ? ? ?for line in f: > >> ? ? ? ? ?cur_date, rest = parse_line(line) > >> ? ? ? ? ?delta = cur_date - first_date > >> ? ? ? ? ?g.write("%s %s" % (int(round(delta.total_seconds())), rest)) thanks, that solves it, you're too kind nukey From joshua.landau.ws at gmail.com Sun Dec 18 11:04:58 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Sun, 18 Dec 2011 16:04:58 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> Message-ID: I'm just going to throw myself in the conversation obtusely.I felt we needed some real code. These are literally the first two samples I had on hand. !!! OUT OF CONTEXT REAL CODE ALERT !!! ################################### formatter = formatter.format(**color).replace("(","{").replace(")","}") print("\n\n".join(formatter.format(**d) for d in db)) # vs # formatter = formatter.format(::(), ::color).replace("(","{").replace(")","}") print("\n\n".join(formatter.format(::(), ::d) for d in db)) # What do we do for "format(**kwargs)"? # With the current guess of mine, I'm thinking "what does ::() do?" and "why are there two double-colons?". # Aside from that it looks not much different/worse. # and # def checkLoad(self, *args, **kwargs): if getattr(self, x) == None: getattr(self, loadx)() if fakeself: return function(getattr(self, x), *args, **kwargs) else: return function(self, *args, **kwargs) return checkLoad # vs # def checkLoad(self, tuple::args, dict::kwargs): if getattr(self, x) == None: getattr(self, loadx)() if fakeself: return function(getattr(self, x), ::args, ::kwargs) else: return function(self, ::args, ::kwargs) return checkLoad # It looks much the same, but how does it benefit me? # "*args" is very distinct from "**kwargs" in my opinion. "tuple"/"dict" doesn't have that. # That said, the second works better with my syntax highlighter (although pylint then swears at me). ############################################################# Before I actually put the syntax in context I hated this. Now I just really dislike it. There is one big thing bugging me: How do I do: "foo(**kwargs)"? My solution was to use "foo(::(), ::kwargs)", but it's obviously not a great alternative. There is another, smaller one. Wrapper functions often just want to pass values straight through them. They don't care that it's a tuple. They don't care that it's a dict. They're just going to throw it across. Without "def foo(::args)" syntax, that's just a waste of a lookup. With "def foo(::args)" syntax you lose the discrimination of args and kwargs, resulting in the same problem above. Finally, it's uglier when without. I do have one suggestion: foo(*args, **kwargs): pass foo(tuple(*) args, dict(**) kwargs): pass foo(dict(**) kwargs): pass foo(*, bar="spam"): pass # this is illegal because unbound "catchalls" don't catch anything, as they do today foo(tuple(*), bar="spam"): pass I'll leave you to guess how it works. If you can't it's obviously not obvious enough. Advantages: "*" means a catchall, as it should. "*" is distinct from "**". With syntax highlighting, it's pretty obvious what is going on. It's backwards compatible (the main reason it's not "tuple(*):args"). *My main problem is: use case? I've had none at all.* If only 1% of users want it, it's not right to make the 99% change. Saving *one line and 50 microseconds* per hundred uses of (*/**) is not a valid argument.And don't make us all change all our code just to satisfy that. The only argument you can make is readability, but (*/**) is more explicit than a lookup, just because it's not dependant on position (args/kwargs argument) and it's always a real tuple and dict. def tuple(): 1/0 -------------- next part -------------- An HTML attachment was scrubbed... URL: From workitharder at gmail.com Sun Dec 18 11:06:56 2011 From: workitharder at gmail.com (buck) Date: Sun, 18 Dec 2011 08:06:56 -0800 (PST) Subject: Debugging a difficult refcount issue. In-Reply-To: <7xpqfm48m6.fsf@ruckus.brouhaha.com> References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> <7xpqfm48m6.fsf@ruckus.brouhaha.com> Message-ID: <812938.821.1324224416576.JavaMail.geo-discussion-forums@prfi34> On Saturday, December 17, 2011 11:55:13 PM UTC-8, Paul Rubin wrote: > buck writes: > > I tried to pinpoint this intermediate allocation with a similar > > PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > Did you try a gdb watchpoint? I didn't try that, since that piece of code is run millions of times, and I don't know the dict-id I'm looking for until after the problem has occurred. From steve+comp.lang.python at pearwood.info Sun Dec 18 12:03:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2011 17:03:05 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <9c808e14-fde9-49e7-b051-84ff45d663d5@i8g2000vbh.googlegroups.com> Message-ID: <4eee1cc8$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 18 Dec 2011 06:13:37 -0800, Eelco wrote: > Casts or conversions are a runtime concept; im talking about > declarations. That seems to be the source of your confusion. Everything in Python happens at runtime, apart from compilation of source code into byte code. Python doesn't have declarations. Even class and def are statements which happen at runtime, not declarations which happen at compile time. Your proposal will be no different: if it happens, it will happen at runtime. [...] > I guess this use of the term entered my lexicon through the > C# developer team. Either way, you seem to be the only one who does not > grok my intended meaning, so I suggest you try reading it again. I grok your *intended* meaning. I also grok that you are using the wrong words to explain your intended meaning. "head, *tail = iterable" is not a declaration in Python. It is not a type conversion, or a constraint, or a cast. It is an iterator unpacking operation. It is syntactic sugar for something similar to this: tmp = iter(iterable) head = next(tmp) tail = [] try: while True: tail.append(next(tmp)) except StopIteration: pass del tmp The only constraint is on the *input* to the operation: the right hand side object must obey the iterator protocol. The only conversion (if we want to call it such) is that an iterator object is created by the right hand side object. [...] > head, tail = l and head, *tail = l mean something completely different, They are both iterator unpacking, and therefore by definition not "completely different". The first one is syntactic sugar for something like this: tmp = iter(iterable) head = next(tmp) tail = next(tmp) if tmp is not empty: raise exception del tmp and the second as shown earlier. Clearly they are quite similar: the only difference is that in the first case, the right hand side must have exactly two items, while in the second case, the right hand side can have an arbitrary number of items. > and the only difference is a constraint placed on tail, which forces the > semantics to be different; the righthand side, or what is to be > assigned, is identical. Of course one can just regard it as syntactic > sugar for head, tail = unpackheadandtailaslist(l); but the syntactic > sugar achieves that same end through a type constraint on tail. Really. All the words are in English, but I have no understanding of what you are trying to say here. What is unpackheadandtailaslist and how does it differ from actual unpacking in Python? >> You are jumping to conclusions about implementation details which >> aren't supported by the visible behaviour. What evidence do you have >> that iterator unpacking creates a tuple first and then converts it to a >> list? > > You are jumping to conclusions about my opinion which aren't supported > by my visible behaviour. What evidence do you have that I ever even said > any such thing? My evidence was your actual words, quoted in my post, which you deleted in your response. Here they are again: [quote] This changes the semantics from normal unpacking, to unpacking and then repacking all but the head into a list. [end quote] In context, "this changes..." refers to extended iterator unpacking in the form "head, *tail" contrasted with "head, tail =...", and that it generates a list. It may very well be that I have misunderstood you. If you do not intend the meaning that extended tuple unpacking first unpacks to a tuple and then re-packs to a list, then please explain what you did mean. >> > The aim of this PEP, is that this type-constraint syntax is expanded >> > upon. We should be careful here to distinguish with providing >> > optional type constraints throughout python as a whole; this is not >> > our aim. >> >> Iterator unpacking is no more about type constraints than is len(). > > Because you wish to keep nitpicking about my usage of the term 'type > constraint' (even though you have not introduced an alternative term > yourself), or because you actually disagree with the content of my > message? I don't think much about your proposal. I think it is unnecessary, based on a profound misunderstanding of how Python actually operates, badly explained using inappropriate terms, and the syntax you propose is ugly. -- Steven From hoogendoorn.eelco at gmail.com Sun Dec 18 12:40:06 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 18 Dec 2011 09:40:06 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4eed3b00$0$29979$c3e8da3$5496439d@news.astraweb.com> <9c808e14-fde9-49e7-b051-84ff45d663d5@i8g2000vbh.googlegroups.com> <4eee1cc8$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <600d1e90-c8b5-4dd1-831d-922372bc8808@q16g2000yqn.googlegroups.com> On 18 dec, 18:03, Steven D'Aprano wrote: > On Sun, 18 Dec 2011 06:13:37 -0800, Eelco wrote: > > Casts or conversions are a runtime concept; im talking about > > declarations. That seems to be the source of your confusion. > > Everything in Python happens at runtime, apart from compilation of source > code into byte code. Python doesn't have declarations. Even class and def > are statements which happen at runtime, not declarations which happen at > compile time. Of course it will have runtime effects; and compile/interpret-time effects too. You said as much yourself: 'Everything in Python happens at runtime, apart from compilation of source'. Python *does* have declarations. Even though they may not be called such in the documentation (dunno, havnt checked) the current use of * and ** when not signifying collectiong UNpacking, is an annotation to the declaration of the symbol that comes after it. In this case, a constraint-on / specification-of the type of the object that the variable symbol may bind; a list or tuple, depending on the context. > Your proposal will be no different: if it happens, it will happen at > runtime. At least something we agree upon; all im proposing is an alternative syntax for something thats already there. Lets be pragmatic and agree to disagree on what it is that is already there, or what it ought to be called. Because frankly, this has detracted enough already from what it is I do want to discuss. > [snipped staements of the obvious] > > and the second as shown earlier. Clearly they are quite similar: the only > difference is that in the first case, the right hand side must have > exactly two items, while in the second case, the right hand side can have > an arbitrary number of items. Oh great, you found another semantic hair to split. Yes, they are quite similar. Nonetheless, any python implementation that would confuse the two behaviors would be considered badly bugged. > > and the only difference is a constraint placed on tail, which forces the > > semantics to be different; the righthand side, or what is to be > > assigned, is identical. Of course one can just regard it as syntactic > > sugar for head, tail = unpackheadandtailaslist(l); but the syntactic > > sugar achieves that same end through a type constraint on tail. Really. > > All the words are in English, but I have no understanding of what you are > trying to say here. What is unpackheadandtailaslist and how does it > differ from actual unpacking in Python? I have the impression you are not even trying then. Google 'syntactic sugar'. It means 'semantically identical way of putting things', in short. That tells you everything you need to know about unpackheadandtailaslist: nothing. > >> You are jumping to conclusions about implementation details which > >> aren't supported by the visible behaviour. What evidence do you have > >> that iterator unpacking creates a tuple first and then converts it to a > >> list? > > > You are jumping to conclusions about my opinion which aren't supported > > by my visible behaviour. What evidence do you have that I ever even said > > any such thing? > > My evidence was your actual words, quoted in my post, which you deleted > in your response. > > Here they are again: > > ? ? [quote] > ? ? This changes the semantics from normal unpacking, to unpacking > ? ? and then repacking all but the head into a list. > ? ? [end quote] > > In context, "this changes..." refers to extended iterator unpacking in > the form "head, *tail" contrasted with "head, tail =...", and that it > generates a list. > > It may very well be that I have misunderstood you. If you do not intend > the meaning that extended tuple unpacking first unpacks to a tuple and > then re-packs to a list, then please explain what you did mean. Again, where did I say it first unpacks to a tuple? As far as im aware that only happens in the context of a function call. > >> > The aim of this PEP, is that this type-constraint syntax is expanded > >> > upon. We should be careful here to distinguish with providing > >> > optional type constraints throughout python as a whole; this is not > >> > our aim. > > >> Iterator unpacking is no more about type constraints than is len(). > > > Because you wish to keep nitpicking about my usage of the term 'type > > constraint' (even though you have not introduced an alternative term > > yourself), or because you actually disagree with the content of my > > message? > > I don't think much about your proposal. I think it is unnecessary, based > on a profound misunderstanding of how Python actually operates, badly > explained using inappropriate terms, and the syntax you propose is ugly. Which is not an aswer to the question I posed; just an expression of frustration. Its mutual. From jackdied at gmail.com Sun Dec 18 12:52:46 2011 From: jackdied at gmail.com (Jack Diederich) Date: Sun, 18 Dec 2011 12:52:46 -0500 Subject: Debugging a difficult refcount issue. In-Reply-To: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: I don't have any great advice, that kind of issue is hard to pin down. That said, do try using a python compile with --with-debug enabled, with that you can turn your unit tests on and off to pinpoint where the refcounts are getting messed up. It also causes python to use plain malloc()s so valgrind becomes useful. Worst case add assertions and printf()s in the places you think are most janky. -Jack On Sat, Dec 17, 2011 at 11:17 PM, buck wrote: > I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. > > Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. > > I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. > > I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > How should I go about debugging this further? I've been completely stuck on this for two days now :( > > [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 > [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload > -- > http://mail.python.org/mailman/listinfo/python-list From traveller3141 at gmail.com Sun Dec 18 13:33:32 2011 From: traveller3141 at gmail.com (traveller3141) Date: Sun, 18 Dec 2011 13:33:32 -0500 Subject: Question about Reading from text file with Python's Array class Message-ID: I've been trying to use the Array class to read 32-bit integers from a file. There is one integer per line and the integers are stored as text. For problem specific reasons, I only am allowed to read 2 lines (2 32-bit integers) at a time. To test this, I made a small sample file (sillyNums.txt) as follows; 109 345 2 1234556 To read this file I created the following test script (trying to copy something I saw on Guido's blog - http://neopythonic.blogspot.com/2008/10/sorting-million-32-bit-integers-in-2mb.html ): import array assert array.array('i').itemsize == 4 bufferSize = 2 f=open("sillyNums.txt","r") data = array.array('i') data.fromstring(f.read(data.itemsize* bufferSize)) print data The output was nonsense: array('i', [171520049, 171258931]) I assume this has something to do with my incorrectly specifying how the various bit/bytes line up. Does anyone know if there's a simple explanation of how I can do this correctly, or why I can't do it at all? Thanks, Steven -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sun Dec 18 14:24:28 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 18 Dec 2011 20:24:28 +0100 Subject: Question about Reading from text file with Python's Array class References: Message-ID: traveller3141 wrote: > I've been trying to use the Array class to read 32-bit integers from a > file. There is one integer per line and the integers are stored as text. > For problem specific reasons, I only am allowed to read 2 lines (2 32-bit > integers) at a time. > > To test this, I made a small sample file (sillyNums.txt) as follows; > 109 > 345 > 2 > 1234556 These are numbers in ascii not in binary. To read these you don't have to use the array class: from itertools import islice data = [] with open("sillyNums.txt") as f: for line in islice(f, 2): data.append(int(line)) (If you insist on using an array you of course can) "Binary" denotes the numbers as they are stored in memory and typically used by lowlevel languages like C. A binary 32 bit integer always consists of 4 bytes. With your code > To read this file I created the following test script (trying to copy > something I saw on Guido's blog - > http://neopythonic.blogspot.com/2008/10/sorting-million-32-bit-integers- in-2mb.html > ): > > import array > assert array.array('i').itemsize == 4 > > bufferSize = 2 > > f=open("sillyNums.txt","r") > data = array.array('i') > data.fromstring(f.read(data.itemsize* bufferSize)) > print data > > > The output was nonsense: > array('i', [171520049, 171258931]) you are telling Python to interpret the first 8 bytes in the file as two integers. The first 4 bytes are "1", "0", "9", "\n" (newline) when interpreted as characters or 49, 48, 57, 10 when looking at the bytes' numerical values. A 32 bit integer is calculated with >>> 49+48*2**8+57*2**16+10*2**24 171520049 Looks familiar... From paka-en at tumia.org Sun Dec 18 14:26:33 2011 From: paka-en at tumia.org (Paka Small) Date: Sun, 18 Dec 2011 11:26:33 -0800 (PST) Subject: Tumia the first Object-Oriented Internet Directory Message-ID: <3309e437-d9cd-47dc-b257-a62d30ae3c93@f1g2000yqi.googlegroups.com> Hi, To my knowledge Tumia (http://www.tumia.org) is the first internet directory structured according to the object-oriented paradigm. Currently Tumia is in its baby years. To illustrate the object- oriented concept consider below "Navigate from Earth Global Warming 2000 AD to Mitigating Solar Energy and Back" which is a specific scenario of the use case "Navigate to Related Tumia Instances". Scenario Navigate from Earth Global Warming 2000 AD to Mitigating Solar Energy and Back Precondition: The user has opened http://www.tumia.org . Scenario: 1. The user selects the Tumia Class Climate Change. 2. The system displays http://www.tumia.org/en/directory/en/earth_global_warming_2000_ad_p1.html 3. The user selects the Related Instance Solar Energy. 4. The system displays http://www.tumia.org/en/directory/en/solar_energy_p1.html 5. The user selects the Related Instance Earth Global Warming 2000 AD. 6. The system displays http://www.tumia.org/en/directory/en/earth_global_warming_2000_ad_p1.html I hope as software developers and OO and use case believers you'll find this example to be a promising use of object-orientation and of use cases in the functional analysis discipline of software development. I also hope it shows how important OO is not only in software design but in first instance in functional analysis and thus determining the domain classes of your software design and thus realising low representational gap. Kind Regards, Paka Small From python.list at tim.thechases.com Sun Dec 18 14:33:13 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 18 Dec 2011 13:33:13 -0600 Subject: Question about Reading from text file with Python's Array class In-Reply-To: References: Message-ID: <4EEE3FF9.1010308@tim.thechases.com> On 12/18/11 12:33, traveller3141 wrote: > To test this, I made a small sample file (sillyNums.txt) as follows; > 109 > 345 > 2 > 1234556 > > f=open("sillyNums.txt","r") > data = array.array('i') > data.fromstring(f.read(data.itemsize* bufferSize)) > print data > > The output was nonsense: > array('i', [171520049, 171258931]) > > I assume this has something to do with my incorrectly specifying how the > various bit/bytes line up. Does anyone know if there's a simple explanation > of how I can do this correctly, or why I can't do it at all? It reads the bytes directly as if using struct.unpack() as in >>> data = '109\n345\n2\n123456' >>> print ' '.join(hex(ord(c))[2:] for c in data) 31 30 39 a 33 34 35 a 32 a 31 32 33 34 35 36 >>> 0x0a393031 # 4 bytes 171520049 It sounds like you want something like from itertools import islice a = array.array('i') a.fromlist(map(int, islice(f, bufferSize))) which will read the first 2 lines of the file (using islice() on the file-object and slicing the first 2 items), and map the string representations into integers, then pass the resulting list of integer data to the array.array.fromlist() method. -tkc From someone at someplace.invalid Sun Dec 18 14:41:37 2011 From: someone at someplace.invalid (HoneyMonster) Date: Sun, 18 Dec 2011 19:41:37 +0000 (UTC) Subject: Newbie Question: Obtain element from list of tuples Message-ID: Hi, I'm just starting to learn Python, so please bear with me. I have in my program an object (recs) which is a list of tuples (returned as such by a database query). My question is doubtless a very easy one to answer: Say I want the ninth element in the twentieth tuple put into variable PID, I can do this, bearing in mind that numbering starts at zero: tup = recs[19] PID = tup[8] But there must be an easier way; i.e. to do it in one go without the extra variable. How do I achieve that please? Secondly, I am more than happy to read the fine manuals. If someone could point me in the direction of one or two "for idiots" guides, that would be greatly appreciated. From edriscoll at wisc.edu Sun Dec 18 14:47:46 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sun, 18 Dec 2011 13:47:46 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EEE4362.8060103@wisc.edu> On 12/18/2011 2:36, Steven D'Aprano wrote: > The barrier to new keywords in Python is very high. Not going to > happen for something that already has perfectly good syntax already > familiar to Python and Ruby programmers. Might else well try to get C > and Java to stop using "..." (ellipses). I agree to some extent; I did say my suggestion was somewhat an effort in brainstorming. (Though I would point out that, if you accept the premise of this PEP suggestion, "perfectly good" only sort of applies.) > The burden is that you complicate the compiler and reduce the pool of > useful names available to the programmer. To be useful, the keywords need > to be short, meaningful and memorable -- which means they're already used > in code, which you will break needlessly. OTOH, it wouldn't be the first time it was done. For instance, 'with'. > With operators that otherwise would be illegal, the programmer doesn't > need to learn about varargs until they need to. With keywords, the > programmer needs to learn "you can't use varargs or kwargs as variable > names" practically from day 1. Yes, but it's not exactly a hard lesson. I can't think of any time where you'd have ambiguous syntax, so misuse could always produce a good error message. And keyword highlighting is something that basically all editors can do well... so unless you're opposed to using an editor more advanced than Notepad, you'll type in "varargs", it'll turn purple or whatever, and you'll go "oh that's a keyword." > So you're not just adding keywords, you're adding new syntax: something > which looks like a function call, but isn't a function call. Well, almost by definition these proposals consider adding syntax. > I'm going to pose the same question to you I have already posed to Eelco: > in your proposal, what happens if the caller has shadowed the list built- > in? Does argument l become a built-in list, or does it become whatever > type list is currently bound to? > > Some more questions: > > Can varargs accepted arbitrary callables, or only a fixed set of known > types? > > How does this differ from what can be done with annotations? These are questions I don't feel I can answer very well. I was giving my thoughts on what I would do or consider doing with the proposal assuming the fundamental idea is sound. Whether it should be accepted at all is a matter for someone else. :-) Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From arnodel at gmail.com Sun Dec 18 14:49:34 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sun, 18 Dec 2011 19:49:34 +0000 Subject: Newbie Question: Obtain element from list of tuples In-Reply-To: References: Message-ID: On 18 December 2011 19:41, HoneyMonster wrote: > Hi, > > I'm just starting to learn Python, so please bear with me. I have in my > program an object (recs) which is a list of tuples (returned as such by a > database query). > > My question is doubtless a very easy one to answer: Say I want the ninth > element in the twentieth tuple put into variable PID, I can do this, > bearing in mind that numbering starts at zero: > > tup = recs[19] > PID = tup[8] > > But there must be an easier way; i.e. to do it in one go without the > extra variable. How do I achieve that please? Well, you were almost there: pid = recs[19][8] Note: all caps is usually reserved for constants in Python. -- Arnaud From edriscoll at wisc.edu Sun Dec 18 14:56:17 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sun, 18 Dec 2011 13:56:17 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EEE4561.10705@wisc.edu> On 12/18/2011 1:31, Steven D'Aprano wrote: > And rebutted. Modesty[1] prevents me from quoting myself, but here are > some links to searches: > > http://duckduckgo.com/?q=python+asterisk > http://duckduckgo.com/?q=python+* OK, so if you search using the right search engine, you *might* get a link to the Python docs. (As I pointed out in a reply to myself before, you *do* get relevant hits with my Google searches as well, but virtually nothing to the relevant Python documentation.) My thinking is borne out; I asked a friend who didn't know what it did to find out, and he went to Google with the search "python 'starred argument'", which he said led to moderately helpful results, but again none from the actual Python docs. > My normal first place to look for something is Wikipedia. Enjoy it before > SOPA kills it. > > http://en.wikipedia.org/wiki/Asterisk#Programming_languages I might think to look at the Wikipedia article for *Python* (which, by the way, doesn't help); looking at the wikipedia page for *asterisk* wouldn't even enter my mind. > Your search looks overly complicated to me. > > I'm not an expert on Google's syntax, but if you search for "python, > optionally with function", isn't that the same as just searching for > "python" since it will return hits either with or without "function"? Chris Angelico's interpretation is correct: I did four searches, "python function star", "python function asterisk", "python star", and "python asterisk". Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From roy at panix.com Sun Dec 18 15:04:13 2011 From: roy at panix.com (Roy Smith) Date: Sun, 18 Dec 2011 15:04:13 -0500 Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: In article , HoneyMonster wrote: > Hi, > > I'm just starting to learn Python, so please bear with me. I have in my > program an object (recs) which is a list of tuples (returned as such by a > database query). Sounds like a standard database interface -- each tuple represents one row in the query result. Very common. > My question is doubtless a very easy one to answer: Say I want the ninth > element in the twentieth tuple put into variable PID, I can do this, > bearing in mind that numbering starts at zero: > > tup = recs[19] > PID = tup[8] Sure, you could do: PID = recs[19][8] That's shorter, but it probably not better. Presumably, the tuple elements represent columns in the database. It would make the code easier to read if you unpacked the tuple into something with human-readable names: col_1, col_2, col_3, ...... col_n = recs[19] and then use the name for the column of interest. A common convention is that when you're unpacking a tuple (or a list, I suppose) and are only interested in some of the elements, you unpack the others into "_". Thus: _, _, _, _, pid, _, _, _ = recs[19] Of course, if you're going to do all that, you probably could have written a better query which returned just the column you were interested in. Instead of "select * from foo", you could have done "select pid from foo" (assuming you're using some SQL-based database). > Secondly, I am more than happy to read the fine manuals. If someone could > point me in the direction of one or two "for idiots" guides, that would > be greatly appreciated. The on-line tutorial is pretty good. http://docs.python.org/tutorial/index.html From edriscoll at wisc.edu Sun Dec 18 15:20:20 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sun, 18 Dec 2011 14:20:20 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> Message-ID: <4EEE4B04.1040505@wisc.edu> On 12/18/2011 8:35, Eelco wrote: > No, its not type *checking*, its type *declaration*. I tried to go to > great lengths to point that out, but it appears I did not do a very > good job :). Type declaration is exactly what I want, and insofar this > syntax has already found adoptation elsewhere, ill consider that a > plus. You say it's found adoption elsewhere, but I think it's that adoption which makes it a *bad* idea, because it does something entirely different in those situations. Other languages are using that syntax for something which is statically checked -- you are proposing that syntax for a dynamic conversion. look pretty familiar... (Not to mention if you just omit the type from the examples above you need another way to distinguish between args and kwargs.) > Yes, one could opt for a syntax where the collection type is optional > and a sensible default is chosen, But to me that would largely defeat > the point; I very much like the added verbosity and explicitness. args- > tuple and kwargs-dict; that just has a much better ring to it than > star-star-kwargs, or whatever other cryptic symbol you use. My problem with it is that it in some sense is forcing me to make a decision I don't care about. Yes, what we have now is less flexible, but I have *never* said "man, I wish this *args parameter were a list instead of a tuple". So at least for me, making me say "args::tuple" or "@tuple args" or whatever is just changing the syntax a bit and adding several more characters for me to type. Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From someone at someplace.invalid Sun Dec 18 15:22:20 2011 From: someone at someplace.invalid (HoneyMonster) Date: Sun, 18 Dec 2011 20:22:20 +0000 (UTC) Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: On Sun, 18 Dec 2011 15:04:13 -0500, Roy Smith wrote: > In article , > HoneyMonster wrote: > >> Hi, >> >> I'm just starting to learn Python, so please bear with me. I have in my >> program an object (recs) which is a list of tuples (returned as such by >> a database query). > > Sounds like a standard database interface -- each tuple represents one > row in the query result. Very common. > >> My question is doubtless a very easy one to answer: Say I want the >> ninth element in the twentieth tuple put into variable PID, I can do >> this, bearing in mind that numbering starts at zero: >> >> tup = recs[19] >> PID = tup[8] > > Sure, you could do: > > PID = recs[19][8] > > That's shorter, but it probably not better. Presumably, the tuple > elements represent columns in the database. It would make the code > easier to read if you unpacked the tuple into something with > human-readable names: > > col_1, col_2, col_3, ...... col_n = recs[19] > > and then use the name for the column of interest. A common convention > is that when you're unpacking a tuple (or a list, I suppose) and are > only interested in some of the elements, you unpack the others into "_". > Thus: > > _, _, _, _, pid, _, _, _ = recs[19] > > Of course, if you're going to do all that, you probably could have > written a better query which returned just the column you were > interested in. Instead of "select * from foo", you could have done > "select pid from foo" (assuming you're using some SQL-based database). > >> Secondly, I am more than happy to read the fine manuals. If someone >> could point me in the direction of one or two "for idiots" guides, that >> would be greatly appreciated. > > The on-line tutorial is pretty good. > http://docs.python.org/tutorial/index.html Thanks, Arnaud and Roy. I had no idea it could be so easy. I think I rather like Python! OK, I'll change PID to pid if that's best practice. Yes, it's an SQL-based database, PostgreSQL actually. I'm using psycopg2 to access it and wxGlade to construct the GUI interface. It all seems to be progressing very smoothly. Thanks again. From roy at panix.com Sun Dec 18 15:25:06 2011 From: roy at panix.com (Roy Smith) Date: Sun, 18 Dec 2011 15:25:06 -0500 Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: In article , HoneyMonster wrote: > I think I rather like Python! +1 QOTD? From rosuav at gmail.com Sun Dec 18 15:51:08 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Dec 2011 07:51:08 +1100 Subject: Newbie Question: Obtain element from list of tuples In-Reply-To: References: Message-ID: On Mon, Dec 19, 2011 at 6:41 AM, HoneyMonster wrote: > My question is doubtless a very easy one to answer: Say I want the ninth > element in the twentieth tuple put into variable PID, I can do this, > bearing in mind that numbering starts at zero: > > tup = recs[19] > PID = tup[8] > > But there must be an easier way; i.e. to do it in one go without the > extra variable. How do I achieve that please? The specific answer has already been given, but I'd like to fill in the generality. In Python, everything's an object; if you can do something with a variable, you can do it with an expression too. I don't know what your function call is to obtain your record list, but imagine it's: recs = conn.query("SELECT * FROM some_table") Then you can actually do all of this in a single statement. It's not usually what you'll want to do, but this is legal: pid = conn.query("SELECT * FROM some_table")[19][8] If you're absolutely certain that you'll always get precisely one value from a query, this becomes rather more useful: mode = conn.query("SELECT mode FROM config WHERE id=5")[0][0] In any case: You can work with a function's return value directly, without first storing it in a temporary variable. Hope that helps! Chris Angelico From roy at panix.com Sun Dec 18 16:10:25 2011 From: roy at panix.com (Roy Smith) Date: Sun, 18 Dec 2011 16:10:25 -0500 Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: In article , Chris Angelico wrote: > If you're absolutely certain that you'll always get precisely one > value from a query, this becomes rather more useful: > > mode = conn.query("SELECT mode FROM config WHERE id=5")[0][0] Although, if you're going to do that, you might as well take advantage of the fetchone() method that most Python database APIs have and eliminate one of the indexes. Typical code would be something like: cursor.execute("SELECT mode FROM config WHERE id=5") mode = cursor.fetchone()[0] If you're going to be doing any database work in Python, you should be familiar with the Python Database API Specification, http://www.python.org/dev/peps/pep-0249/. Most of the vendor-specific database modules have interfaces which hold pretty close to the style described there. You might also want to explore SQL_Alchemy. I personally find it difficult and painful to work with, but it does have the powerful advantage that it abstracts away all the vendor-specific differences between databases. From someone at someplace.invalid Sun Dec 18 17:55:38 2011 From: someone at someplace.invalid (HoneyMonster) Date: Sun, 18 Dec 2011 22:55:38 +0000 (UTC) Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: On Mon, 19 Dec 2011 07:51:08 +1100, Chris Angelico wrote: > On Mon, Dec 19, 2011 at 6:41 AM, HoneyMonster > wrote: >> My question is doubtless a very easy one to answer: Say I want the >> ninth element in the twentieth tuple put into variable PID, I can do >> this, bearing in mind that numbering starts at zero: >> >> tup = recs[19] >> PID = tup[8] >> >> But there must be an easier way; i.e. to do it in one go without the >> extra variable. How do I achieve that please? > > The specific answer has already been given, but I'd like to fill in the > generality. In Python, everything's an object; if you can do something > with a variable, you can do it with an expression too. I don't know what > your function call is to obtain your record list, but imagine it's: > > recs = conn.query("SELECT * FROM some_table") > > Then you can actually do all of this in a single statement. It's not > usually what you'll want to do, but this is legal: > > pid = conn.query("SELECT * FROM some_table")[19][8] > > If you're absolutely certain that you'll always get precisely one value > from a query, this becomes rather more useful: > > mode = conn.query("SELECT mode FROM config WHERE id=5")[0][0] > > In any case: You can work with a function's return value directly, > without first storing it in a temporary variable. > > Hope that helps! Thanks, Chris. Actually I had simplified the question somewhat. The actuality is indeed - as you suspected - that recs = conn.query("SELECT * FROM some_table") but I am then using the recs object to populate a (non editable) wxGrid. When the user selects a row and clicks a button, I am using: pos = self.grid_1.GetGridCursorRow() to establish which tuple in recs is involved, and then pid = recs[pos][4] to determine the key value (I suspected that accessing recs directly would be more efficient that trying to determine the wxGrid column value, and in any case the pid is in recs but not in the wxGrid). I trust this all makes sense. Thanks again. From rosuav at gmail.com Sun Dec 18 18:40:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Dec 2011 10:40:06 +1100 Subject: Newbie Question: Obtain element from list of tuples In-Reply-To: References: Message-ID: On Mon, Dec 19, 2011 at 9:55 AM, HoneyMonster wrote: > When the user selects a row and clicks a button, I am using: > pos = self.grid_1.GetGridCursorRow() to establish which tuple in recs is > involved, and then pid = recs[pos][4] to determine the key value (I > suspected that accessing recs directly would be more efficient that > trying to determine the wxGrid column value, and in any case the pid is > in recs but not in the wxGrid). Yep, this looks like a sensible way to do it! I would recommend, though, that you avoid "magic numbers" - the process ID might happen to be in cell 4, but what if your column list changes? This is especially problematic when you use "select * from table", because changes outside your code can break things. There's two solutions. One is to carefully match your SELECT statement to a set of constants: exec("SELECT FOO,BAR,QUUX,ASDF,PID,WHATEVER FROM table") FOO,BAR,QUUX,ASDF,PID,WHATEVER,*_=range(20) # Cool trick to avoid having to count the columns But a better way is to let the database handle it. Check your interface library to see if you can have it return dictionaries or objects instead of tuples - then you could use: pid = recs[pos]["pid"] # or pid = recs[pos]->pid which avoids the need to count columns at all. Other than that, though, yep - your code concept looks fine. ChrisA From rantingrickjohnson at gmail.com Sun Dec 18 19:57:56 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 18 Dec 2011 16:57:56 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: On Dec 17, 10:52?pm, buck wrote: > [...] > As for the separator, let's examine the available ascii punctuation. Excluding valid variable characters, whitespace, and operators, we have: > > ! -- ok. No, there are better uses for that char. > " -- can't use this. Would look like a string. > # -- no. Would looks like a comment. > $ -- ok. No, there are better uses for that char. > : -- ok, maybe. Seems confusing in a colon-terminated statement. dear god no! > ; -- no, just no. > ? -- ok. No, there are better uses for that char. > @ -- ok. YES! From rantingrickjohnson at gmail.com Sun Dec 18 19:59:22 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 18 Dec 2011 16:59:22 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> Message-ID: <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> On Dec 17, 11:33?pm, Evan Driscoll wrote: > On 12/17/2011 22:52, buck wrote:> Try these on for size. > > > ? ? ?head, @tuple tail = sequence > > ? ? ?def foo(@list args, @dict kwargs): pass > > ? ? ?foo(@args, @kwargs) > > > For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. > > I like this idea much more than the original one. +1. I will second that! Eelco has the CORRECT idea, but the WRONG syntax! From rantingrickjohnson at gmail.com Sun Dec 18 20:04:42 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 18 Dec 2011 17:04:42 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> Message-ID: <8bc7f444-24f4-4179-9eaa-8dafbe541c14@f1g2000yqi.googlegroups.com> On Dec 18, 8:35?am, Eelco wrote: > On Dec 18, 6:33?am, Evan Driscoll wrote: > Good point; technically the askeriskes could be kept for backwards > compatibility, but that would break 'there should only be one way to > do it'. I believe it's high time for the upper society of this community to realize that the small breaks in backward compatibility in Python 3000 were only the beginning. Python is evolving past even what Guido could have dreamed. He can not hold the reins of this unwieldy beast any longer. From steve+comp.lang.python at pearwood.info Sun Dec 18 20:26:02 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Dec 2011 01:26:02 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eee92aa$0$11091$c3e8da3@news.astraweb.com> On Sun, 18 Dec 2011 13:47:46 -0600, Evan Driscoll wrote: [...] > so unless you're opposed to using an editor more > advanced than Notepad, you'll type in "varargs", it'll turn purple or > whatever, and you'll go "oh that's a keyword." Not everybody uses editors more advanced than Notepad. Even those who do may not have an editor that understands Python keywords, or has an obsolete list of keywords (my editor of choice doesn't know about NotImplementedError). Or they're ssh'ed into a customer's server half-way across the world and using `ed` because that's the only thing available. The point is not that there can never been more keywords in Python, but that the burden of proof is on those asking for new keywords. By default, the status quo wins: http://www.boredomandlaziness.org/2011/02/status-quo-wins-stalemate.html -- Steven From timothy.c.delaney at gmail.com Sun Dec 18 20:27:24 2011 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Mon, 19 Dec 2011 12:27:24 +1100 Subject: Make a small function thread safe In-Reply-To: <17238811.518.1324198329306.JavaMail.geo-discussion-forums@yqio4> References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> <4eebfbff$0$1679$742ec2ed@news.sonic.net> <17238811.518.1324198329306.JavaMail.geo-discussion-forums@yqio4> Message-ID: On 18 December 2011 19:52, RangerElf wrote: > Which is why the original .acquire() ... .release() idiom was wrong, this > would better express the intent: > > try: > lock.acquire() > shared_container.append(...) > finally: > lock.release() > No - this is very bad. The lock must be acquired outside the try: - otherwise if an exception is thrown while acquiring, you will try to release a lock that you have not acquired. Which again is why using with is a much better option - you can't make this kind of mistake. Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From workitharder at gmail.com Sun Dec 18 21:01:50 2011 From: workitharder at gmail.com (buck) Date: Sun, 18 Dec 2011 18:01:50 -0800 (PST) Subject: Debugging a difficult refcount issue. In-Reply-To: References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: <4953582.389.1324260110614.JavaMail.geo-discussion-forums@pruu23> Thanks Jack. I think printf is what it will come down to. I plan to put a little code into PyDict_New to print the id and the line at which it was allocated. Hopefully this will show me all the possible suspects and I can figure it out from there. I hope figuring out the file and line-number from within that code isn't too hard. On Sunday, December 18, 2011 9:52:46 AM UTC-8, Jack Diederich wrote: > I don't have any great advice, that kind of issue is hard to pin down. > That said, do try using a python compile with --with-debug enabled, > with that you can turn your unit tests on and off to pinpoint where > the refcounts are getting messed up. It also causes python to use > plain malloc()s so valgrind becomes useful. Worst case add assertions > and printf()s in the places you think are most janky. > > -Jack > > On Sat, Dec 17, 2011 at 11:17 PM, buck wrote: > > I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. > > > > Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. > > > > I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. > > > > I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > > > How should I go about debugging this further? I've been completely stuck on this for two days now :( > > > > [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 > > [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload > > -- > > http://mail.python.org/mailman/listinfo/python-list From workitharder at gmail.com Sun Dec 18 21:01:50 2011 From: workitharder at gmail.com (buck) Date: Sun, 18 Dec 2011 18:01:50 -0800 (PST) Subject: Debugging a difficult refcount issue. In-Reply-To: References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: <4953582.389.1324260110614.JavaMail.geo-discussion-forums@pruu23> Thanks Jack. I think printf is what it will come down to. I plan to put a little code into PyDict_New to print the id and the line at which it was allocated. Hopefully this will show me all the possible suspects and I can figure it out from there. I hope figuring out the file and line-number from within that code isn't too hard. On Sunday, December 18, 2011 9:52:46 AM UTC-8, Jack Diederich wrote: > I don't have any great advice, that kind of issue is hard to pin down. > That said, do try using a python compile with --with-debug enabled, > with that you can turn your unit tests on and off to pinpoint where > the refcounts are getting messed up. It also causes python to use > plain malloc()s so valgrind becomes useful. Worst case add assertions > and printf()s in the places you think are most janky. > > -Jack > > On Sat, Dec 17, 2011 at 11:17 PM, buck wrote: > > I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. > > > > Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. > > > > I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. > > > > I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > > > How should I go about debugging this further? I've been completely stuck on this for two days now :( > > > > [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 > > [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload > > -- > > http://mail.python.org/mailman/listinfo/python-list From rantingrickjohnson at gmail.com Sun Dec 18 21:16:00 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 18 Dec 2011 18:16:00 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> Message-ID: <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> On Dec 18, 7:26?pm, Steven D'Aprano wrote: > Not everybody uses editors more advanced than Notepad. And they have no excuse for NOT using a better one. Well, except for a "foolish consistency" that is! > Even those who do > may not have an editor that understands Python keywords, or has an > obsolete list of keywords (my editor of choice doesn't know about > NotImplementedError). Are you kidding me? Any editor that is not smart enough to do... >>> import keyword >>> pyKeyWords = keyword.kwlist ... needs to be thrown out the window. And let's not forget that Python even ships with an IDE called IDLE. As poorly written as IDLE's code may be, at least it is smart enough to find the current keyword list. From wuwei23 at gmail.com Sun Dec 18 21:23:41 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 18 Dec 2011 18:23:41 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> Message-ID: <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> Evan Driscoll wrote: > My problem with it is that it in some sense is forcing me to make a > decision I don't care about. Yes, what we have now is less flexible, but > I have *never* said "man, I wish this *args parameter were a list > instead of a tuple". And if you _did_, then one of the first lines in your function would be: args = list(args) Which is obvious to everyone, doesn't modify existing behaviour, doesn't force everyone without a fetish for change to add unnecessary cruft to their function signature... Except, OMG, list() is RETURNING A LIST, which is an OBVIOUS type constraint. I propose that: args = @set list(args) Will coerce args into a list and then give me a set in return. From wuwei23 at gmail.com Sun Dec 18 21:35:47 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 18 Dec 2011 18:35:47 -0800 (PST) Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: <2c286dea-efba-45d5-85a7-a7503733575a@i6g2000vbe.googlegroups.com> Roy Smith wrote: > A common convention > is that when you're unpacking a tuple (or a list, I suppose) and are > only interested in some of the elements, you unpack the others into "_". > Thus: > > _, _, _, _, pid, _, _, _ = recs[19] Pre-namedtuple, I used to like using named slices for this: cPID = slice(19) pid = recs[cPID] cHostPort = slice(99,100) host, port = recs[cHostPort] etc. The suggestions of having your query return a dictionary where possible are the most ideal, but if it's not possible you can always wrap the result tuple in a namedtuple: from collections import namedtuple Staff = namedtuple('Staff', ['firstname','lastname','age','position']) sql_result = ('John', 'Example', '30', 'Dummy') staff_record = Staff(sql_result) print staff_record.firstname, staff_record.age From cs at zip.com.au Sun Dec 18 21:45:03 2011 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 19 Dec 2011 13:45:03 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4EED6F64.5020906@wisc.edu> References: <4EED6F64.5020906@wisc.edu> Message-ID: <20111219024502.GA10048@cskk.homeip.net> On 17Dec2011 22:43, Evan Driscoll wrote: | On 12/17/2011 21:42, Chris Angelico wrote: | > Welcome to the list! If you're curious as to what's happened, check | > the archives: | > http://mail.python.org/pipermail/python-list/ | Thanks! Incidentally, is there a good way to respond to the original | post in this thread, considering it wasn't delivered to me? Personally, when I join a mailing list I download the archives and unpack them into my mail folder for that list. Then I can reply! (I actually do this for search purposes). Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ You can't have everything... where would you put it? - Charles Robinson, cr0100 at medtronic.com From ian.g.kelly at gmail.com Sun Dec 18 22:00:59 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 18 Dec 2011 20:00:59 -0700 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4eee92aa$0$11091$c3e8da3@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Sun, Dec 18, 2011 at 6:26 PM, Steven D'Aprano wrote: > On Sun, 18 Dec 2011 13:47:46 -0600, Evan Driscoll wrote: > > [...] >> so unless you're opposed to using an editor more >> advanced than Notepad, you'll type in "varargs", it'll turn purple or >> whatever, and you'll go "oh that's a keyword." > > Not everybody uses editors more advanced than Notepad. Even those who do > may not have an editor that understands Python keywords, or has an > obsolete list of keywords (my editor of choice doesn't know about > NotImplementedError). Probably because NotImplementedError is not a keyword. From steve+comp.lang.python at pearwood.info Sun Dec 18 22:00:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Dec 2011 03:00:59 GMT Subject: Newbie Question: Obtain element from list of tuples References: <2c286dea-efba-45d5-85a7-a7503733575a@i6g2000vbe.googlegroups.com> Message-ID: <4eeea8eb$0$11091$c3e8da3@news.astraweb.com> On Sun, 18 Dec 2011 18:35:47 -0800, alex23 wrote: > Pre-namedtuple, I used to like using named slices for this: > > cPID = slice(19) > pid = recs[cPID] You know, that is an incredibly simple thing and yet it never dawned on me before now. Thank you for sharing that. -- Steven From wuwei23 at gmail.com Sun Dec 18 22:15:34 2011 From: wuwei23 at gmail.com (alex23) Date: Sun, 18 Dec 2011 19:15:34 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <4EED6F64.5020906@wisc.edu> Message-ID: <4a93e19b-edf6-479b-af0f-37561e9c46f8@q11g2000vbq.googlegroups.com> Cameron Simpson wrote: > Personally, when I join a mailing list I download the archives and > unpack them into my mail folder for that list. Would it be more useful to you if you could unpack them into a tuple? From steve+comp.lang.python at pearwood.info Sun Dec 18 22:36:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Dec 2011 03:36:05 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4eeeb125$0$11091$c3e8da3@news.astraweb.com> On Sun, 18 Dec 2011 20:00:59 -0700, Ian Kelly wrote: > On Sun, Dec 18, 2011 at 6:26 PM, Steven D'Aprano > wrote: >> On Sun, 18 Dec 2011 13:47:46 -0600, Evan Driscoll wrote: >> >> [...] >>> so unless you're opposed to using an editor more advanced than >>> Notepad, you'll type in "varargs", it'll turn purple or whatever, and >>> you'll go "oh that's a keyword." >> >> Not everybody uses editors more advanced than Notepad. Even those who >> do may not have an editor that understands Python keywords, or has an >> obsolete list of keywords (my editor of choice doesn't know about >> NotImplementedError). > > Probably because NotImplementedError is not a keyword. Poor choice of words on my part. My editor colours built-ins like ValueError, TypeError, len, sum, etc., but not NotImplementedError. -- Steven From rosuav at gmail.com Sun Dec 18 23:35:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 19 Dec 2011 15:35:06 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> Message-ID: On Mon, Dec 19, 2011 at 1:23 PM, alex23 wrote: > Except, OMG, list() is RETURNING A LIST, which is an OBVIOUS type > constraint. I propose that: > > ? ?args = @set list(args) > > Will coerce args into a list and then give me a set in return. Point to note: list,set = set,list # Request a death sentence from the next maintainer is perfectly legal code. Now, what does your "args=" line do? ChrisA From brian at python.org Mon Dec 19 00:05:58 2011 From: brian at python.org (Brian Curtin) Date: Sun, 18 Dec 2011 23:05:58 -0600 Subject: PyCon US 2012 sprints In-Reply-To: References: Message-ID: 2011/12/17 Ricardo B?nffy : > Hi folks. > > Next March I'm planning to attend PyCon US (for the first time) and > stay for the sprints. I am not sure how they work, however. Are there > any "first-timer guide to PyCon sprints"? I don't know of any such guide, but here's what usually happens: 1. Right after the closing of the conference days on Sunday, late in the afternoon, there will be a chance for all projects who plan to host sprints to get on stage and talk for a minute about who they are, what their project is, and what they'll be sprinting on. You might hear something like this: "Hi I'm Brian and I'll be hosting a sprint on foo project. We're going to be working on x, y, and z functionality using a lot of a, b, and c libraries. Come check it out of you're interested. We've got plenty of stuff for first timers" 2. At the entrance to wherever the sprints will be there will be a board listing what projects are sprinting in what areas. The last few years we've taken over numerous rooms, so that board acted as a map to know what was going on in what room. 3. I don't yet know how the sprints will be laid out this year since it's a new hotel and location, but if its anything like last year, each room will have several projects working inside it. It's quite normal to just walk in and say "hey where's the blah blah sprint at?", then see they're over in the corner. Everyone has been really friendly in my experience, and a lot of people bounce back and forth between rooms for a lot of reasons, so ask if you don't know where you're going or who you're looking for. 4. Hack on some fun stuff and meet some new people. It's a bit early for the 2012 sprint page to be up and populated, but it'll happen soon enough. The 2011 page is here (http://us.pycon.org/2011/sprints/) and the list of some (but not all) of the sprints and their attendees here: http://us.pycon.org/2011/sprints/projects/ It's really relaxed and a lot of fun. It's one of my favorite parts of the PyCon experience. From frank at chagford.com Mon Dec 19 01:46:28 2011 From: frank at chagford.com (Frank Millman) Date: Mon, 19 Dec 2011 08:46:28 +0200 Subject: Newbie Question: Obtain element from list of tuples References: <2c286dea-efba-45d5-85a7-a7503733575a@i6g2000vbe.googlegroups.com> <4eeea8eb$0$11091$c3e8da3@news.astraweb.com> Message-ID: "Steven D'Aprano" wrote in message news:4eeea8eb$0$11091$c3e8da3 at news.astraweb.com... > On Sun, 18 Dec 2011 18:35:47 -0800, alex23 wrote: > >> Pre-namedtuple, I used to like using named slices for this: >> >> cPID = slice(19) >> pid = recs[cPID] > > You know, that is an incredibly simple thing and yet it never dawned on > me before now. Thank you for sharing that. > I also like it, but it does not work quite so simply for me. >>> row = tuple(range(20)) >>> cPID = slice(15) >>> pid = row[cPID] >>> pid (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) >>> This works - >>> row = tuple(range(20)) >>> cPID = slice(15, 16) >>> pid, = row[cPID] # note the trailing comma >>> pid 15 >>> Still nice, but not quite so elegant. Am I missing something? Frank Millman From ian.g.kelly at gmail.com Mon Dec 19 01:57:43 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 18 Dec 2011 23:57:43 -0700 Subject: Make a small function thread safe In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> <4eebfbff$0$1679$742ec2ed@news.sonic.net> <17238811.518.1324198329306.JavaMail.geo-discussion-forums@yqio4> Message-ID: On Sun, Dec 18, 2011 at 6:27 PM, Tim Delaney wrote: > On 18 December 2011 19:52, RangerElf wrote: >> >> Which is why the original .acquire() ... .release() idiom was wrong, this >> would better express the intent: >> >> try: >> ?lock.acquire() >> ?shared_container.append(...) >> finally: >> ?lock.release() > > > No - this is very bad. The lock must be acquired outside the try: - > otherwise if an exception is thrown while acquiring, you will try to release > a lock that you have not acquired. > > Which again is why using with is a much better option - you can't make this > kind of mistake. Well, not unless you make the same mistake in the context manager itself. from contextlib import contextmanager @contextmanager def bad_context_manager(lock): try: lock.acquire() yield finally: lock.release() class Lock(object): def __init__(self): self.is_locked = False def acquire(self): assert False def release(self): assert self.is_locked, "Tried to release lock without acquiring it" with bad_context_manager(Lock()): pass Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__ return self.gen.next() File "", line 7, in bad_context_manager File "", line 7, in release AssertionError: Tried to release lock without acquiring it Perhaps a (small) reason to avoid the contextmanager decorator and implement __enter__ and __exit__ instead. From workitharder at gmail.com Mon Dec 19 03:09:19 2011 From: workitharder at gmail.com (buck) Date: Mon, 19 Dec 2011 00:09:19 -0800 (PST) Subject: Debugging a difficult refcount issue. In-Reply-To: References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: <13061655.1156.1324282159733.JavaMail.geo-discussion-forums@prkz16> This is what I came up with: https://gist.github.com/1496028 We'll see if it helps, tomorrow. On Sunday, December 18, 2011 6:01:50 PM UTC-8, buck wrote: > Thanks Jack. I think printf is what it will come down to. I plan to put a little code into PyDict_New to print the id and the line at which it was allocated. Hopefully this will show me all the possible suspects and I can figure it out from there. > > I hope figuring out the file and line-number from within that code isn't too hard. > > > On Sunday, December 18, 2011 9:52:46 AM UTC-8, Jack Diederich wrote: > > I don't have any great advice, that kind of issue is hard to pin down. > > That said, do try using a python compile with --with-debug enabled, > > with that you can turn your unit tests on and off to pinpoint where > > the refcounts are getting messed up. It also causes python to use > > plain malloc()s so valgrind becomes useful. Worst case add assertions > > and printf()s in the places you think are most janky. > > > > -Jack > > > > On Sat, Dec 17, 2011 at 11:17 PM, buck wrote: > > > I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. > > > > > > Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. > > > > > > I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. > > > > > > I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > > > > > How should I go about debugging this further? I've been completely stuck on this for two days now :( > > > > > > [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 > > > [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload > > > -- > > > http://mail.python.org/mailman/listinfo/python-list From workitharder at gmail.com Mon Dec 19 03:09:19 2011 From: workitharder at gmail.com (buck) Date: Mon, 19 Dec 2011 00:09:19 -0800 (PST) Subject: Debugging a difficult refcount issue. In-Reply-To: References: <18365650.792.1324181855929.JavaMail.geo-discussion-forums@prht13> Message-ID: <13061655.1156.1324282159733.JavaMail.geo-discussion-forums@prkz16> This is what I came up with: https://gist.github.com/1496028 We'll see if it helps, tomorrow. On Sunday, December 18, 2011 6:01:50 PM UTC-8, buck wrote: > Thanks Jack. I think printf is what it will come down to. I plan to put a little code into PyDict_New to print the id and the line at which it was allocated. Hopefully this will show me all the possible suspects and I can figure it out from there. > > I hope figuring out the file and line-number from within that code isn't too hard. > > > On Sunday, December 18, 2011 9:52:46 AM UTC-8, Jack Diederich wrote: > > I don't have any great advice, that kind of issue is hard to pin down. > > That said, do try using a python compile with --with-debug enabled, > > with that you can turn your unit tests on and off to pinpoint where > > the refcounts are getting messed up. It also causes python to use > > plain malloc()s so valgrind becomes useful. Worst case add assertions > > and printf()s in the places you think are most janky. > > > > -Jack > > > > On Sat, Dec 17, 2011 at 11:17 PM, buck wrote: > > > I'm getting a fatal python error "Fatal Python error: GC object already tracked"[1]. > > > > > > Using gdb, I've pinpointed the place where the error is detected. It is an empty dictionary which is marked as in-use. This is somewhat helpful since I can reliably find the memory address of the dict, but it does not help me pinpoint the issue. I was able to find the piece of code that allocates the problematic dict via a malloc/LD_PRELOAD interposer, but that code was pure python. I don't think it was the cause. > > > > > > I believe that the dict was deallocated, cached, and re-allocated via PyDict_New to a C routine with bad refcount logic, then the above error manifests when the dict is again deallocated, cached, and re-allocated. > > > > > > I tried to pinpoint this intermediate allocation with a similar PyDict_New/LD_PRELOAD interposer, but that isn't working for me[2]. > > > > > > How should I go about debugging this further? I've been completely stuck on this for two days now :( > > > > > > [1] http://hg.python.org/cpython/file/99af4b44e7e4/Include/objimpl.h#l267 > > > [2] http://stackoverflow.com/questions/8549671/cant-intercept-pydict-new-with-ld-preload > > > -- > > > http://mail.python.org/mailman/listinfo/python-list From franck at ditter.org Mon Dec 19 04:15:52 2011 From: franck at ditter.org (Franck Ditter) Date: Mon, 19 Dec 2011 10:15:52 +0100 Subject: Can't get tilde character with IDLE 3.2.2 on French Mac Lion Message-ID: Hi ! All is in the subject. I'm starting to use Python with Idle 3.2.2 on MacOS-X Lion (French). I can't get "Option-N space" to provide the ~ char. I tried to go into the Keys preferences but I can't find "Option-N space" to modify its meaning. Its actual behavior is to merge lines of a paragraph. Thanks for help ! franck From fperez.net at gmail.com Mon Dec 19 04:49:02 2011 From: fperez.net at gmail.com (Fernando Perez) Date: Mon, 19 Dec 2011 09:49:02 +0000 (UTC) Subject: [ANN] IPython 0.12 is out! Message-ID: Hi all, on behalf of the IPython development team, I'm thrilled to announce, after an intense 4 1/2 months of work, the official release of IPython 0.12. This is a very important release for IPython, for several reasons. First and foremost, we have a major new feature, our interactive web-based notebook, that has been in our sights for a very long time. We tried to build one years ago (with WX) as a Google SoC project in 2005, had other prototypes later on, but things never quite worked. Finally the refactoring effort started two years ago, the communications architecture we built in 2010, and the advances of modern browsers, gave us all the necessary pieces. With this foundation in place, while part of the team worked on the 0.11 release, Brian Granger had already started quietly building the web notebook, which we demoed in early-alpha mode at the SciPy 2011 conference (http://www.archive.org/details/Wednesday-203-6- IpythonANewArchitectureForInteractiveAndParallel). By the EuroScipy conference in August we had merged Brian's amazing effort into our master branch, and after that multiple people (old and new) jumped in to make all kinds of improvements, leaving us today with something that is an excellent foundation. It's still the first release of the notebook, and as such we know it has a number of rough edges, but several of us have been using it as a daily research tool for the last few months. Do not hesitate to file issues for any problems you encounter with it, and we even have an 'open issue' for general discussion of ideas and features for the notebook at: https://github.com/ipython/ipython/issues/977. Furthermore, it is clear that our big refactoring work, combined with the amazing facilities at Github, are paying off. The 0.11 series was a major amount of work, with 511 issues closed over almost two years. But that pales in comparison to this cycle: in only 4 1/2 months we closed 515 issues, with 50% being Pull Requests. And very importantly, our list of contributors includes many new faces (see the credits section in our release notes for full details), which is the best thing that can happen to an open source project. We hope you will find the new features (the notebook isn't the only one! see below) compelling, and that many more will not only use IPython but will join the project; there's plenty to do and now there are tasks for many different skill sets (web, javascript, gui work, low-level networking, parallel machinery, console apps, etc). *Downloads* Download links and instructions are at: http://ipython.org/download.html And IPython is also on PyPI: http://pypi.python.org/pypi/ipython Those contain a built version of the HTML docs; if you want pure source downloads with no docs, those are available on github: Tarball: https://github.com/ipython/ipython/tarball/rel-0.12 Zipball: https://github.com/ipython/ipython/zipball/rel-0.12 * Features * Here is a quick listing of the major new features: - An interactive browser-based Notebook with rich media support - Two-process terminal console - Tabbed QtConsole - Full Python 3 compatibility - Standalone Kernel - PyPy support And many more... We closed over 500 tickets, merged over 200 pull requests, and more than 45 people contributed commits for the final release. Please see our release notes for the full details on everything about this release: http://ipython.org/ipython-doc/stable/whatsnew/version0.12.html * IPython tutorial at PyCon 2012 * Those of you attending (or planning on it) PyCon 2012 in Santa Clara, CA, may be interested in attending a hands-on tutorial we will be presenting on the many faces of IPython. See https://us.pycon.org/2012/schedule/ presentation/121/ for full details. * Errata * This was caught by Matthias Bussionnier's (one of our great new contributors) sharp eyes while I was writing these release notes: In the example notebook called display_protocol, the first cell starts with: from IPython.lib.pylabtools import print_figure which should instead be: from IPython.core.pylabtools import print_figure This has already been fixed on master, but since the final 0.12 files have been uploaded to github and PyPI, we'll let them be. As usual, if you find any other problem, please file a ticket --or even better, a pull request fixing it-- on our github issues site (https:// github.com/ipython/ipython/issues/). Many thanks to all who contributed! Fernando, on behalf of the IPython development team. http://ipython.org From hoogendoorn.eelco at gmail.com Mon Dec 19 05:15:11 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 19 Dec 2011 02:15:11 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> Message-ID: On Dec 19, 3:23?am, alex23 wrote: > Evan Driscoll wrote: > > My problem with it is that it in some sense is forcing me to make a > > decision I don't care about. Yes, what we have now is less flexible, but > > I have *never* said "man, I wish this *args parameter were a list > > instead of a tuple". > > And if you _did_, then one of the first lines in your function would > be: > > ? ? args = list(args) > > Which is obvious to everyone, doesn't modify existing behaviour, > doesn't force everyone without a fetish for change to add unnecessary > cruft to their function signature... Its obvious you end up with a list (assuming args is an iterable); knowing what args was to begin with suffers from the same problems. > Except, OMG, list() is RETURNING A LIST, which is an OBVIOUS type > constraint. I propose that: > > ? ? args = @set list(args) > > Will coerce args into a list and then give me a set in return. ? What does that have to do with collection packing/unpacking? From hoogendoorn.eelco at gmail.com Mon Dec 19 05:20:33 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 19 Dec 2011 02:20:33 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> Message-ID: <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> On Dec 19, 1:59?am, Rick Johnson wrote: > On Dec 17, 11:33?pm, Evan Driscoll wrote: > > > On 12/17/2011 22:52, buck wrote:> Try these on for size. > > > > ? ? ?head, @tuple tail = sequence > > > ? ? ?def foo(@list args, @dict kwargs): pass > > > ? ? ?foo(@args, @kwargs) > > > > For backward compatibility, we could say that the unary * is identical to @list and unary ** is identical to @dict. > > > I like this idea much more than the original one. > > +1. I will second that! Eelco has the CORRECT idea, but the WRONG > syntax! Thanks, your opinion is noted. Personally im impartial between identifier::collectiontype and identifier at collectiontype, but that order is something I think is rather important. Having two seperate symbols seperated by whitespace, as in @list args strikes me as a terrible break of normal python lexical rules. Plus, identifier at collectiontype as a collection-type annotation would be consistent with the existing general function annotation syntax. Many non-C-family languages postfix the type declaration. Also, we want to use the same symbol for collection unpacking as we use for collection packing. Saying foo(@argslist) really does look a tad much like a decorator, even though it can be unambigiously distinguished from it by context. From ralf at systemexit.de Mon Dec 19 05:26:00 2011 From: ralf at systemexit.de (Ralf Schmitt) Date: Mon, 19 Dec 2011 11:26:00 +0100 Subject: [ANNOUNCE] greenlet 0.3.2 Message-ID: <87wr9skgcn.fsf@winserver.brainbot.com> Hi, I have uploaded greenlet 0.3.2 to PyPI: http://pypi.python.org/pypi/greenlet What is it? ----------- The greenlet module provides coroutines for python. coroutines allow suspending and resuming execution at certain locations. concurrence[1], eventlet[2] and gevent[3] use the greenlet module in order to implement concurrent network applications. Documentation can be found here: http://packages.python.org/greenlet/ The code is hosted on github: https://github.com/python-greenlet/greenlet Changes in version 0.3.2 ------------------------ The NEWS file lists these changes for release 0.3.2: * fix various crashes with recent gcc versions and VC90 * Try to fix stack save/restore on arm32 * store and restore the threadstate on exceptions like pypy/stackless do * Switch to using PyCapsule for Python 2.7 and 3.1 * Port for AIX on PowerPC * Fix the sparc/solaris header * Improved build dependencies patch from flub. * #21 - Can't pass parent=None to greenlet.greenlet() [1] http://opensource.hyves.org/concurrence/ [2] http://eventlet.net/ [3] http://www.gevent.org/ -- Cheers Ralf Schmitt From lie.1296 at gmail.com Mon Dec 19 05:54:37 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 19 Dec 2011 21:54:37 +1100 Subject: how to run python-script from the python promt? [absolute newbie] In-Reply-To: References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: On 12/19/2011 12:16 AM, nukeymusic wrote: > On 18 dec, 13:39, Lie Ryan wrote: >> On 12/18/2011 10:00 PM, nukeymusic wrote: >> >> >> >> >> >> >> >> >> >>> How can I load a python-script after starting python in the >>> interactive mode? >>> I tried with >>>>>> load 'myscript.py' >>>>>> myscript.py >>>>>> myscript >> >>> but none of these works, so the only way I could work further until >>> now was copy/paste line per line of my python-script to the >>> interactive mode prompt >>> I do know how to run the script non-interactively, but what I want to >>> do is adding lines to the code I have written thus far in interactive >>> mode. >> >>> thanks in advance >>> nukey >> >> The normal python shell doesn't directly support doing that, although >> there are several workaround with (ab)using the 'import' statement, it >> had several subtleties with how module are cached. Try the ipython >> shell; in ipython you can load a file into the current interpreter >> session using the %run magic command. > > I guess you mean the following command? > %run 'myscript.py' > > is this correct? yes From ulrich.eckhardt at dominolaser.com Mon Dec 19 08:16:42 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Mon, 19 Dec 2011 14:16:42 +0100 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: Am 16.12.2011 18:48, schrieb Nathan Rice: > I realize this has been discussed in the past, I hope that I am > presenting a slightly different take on the subject that will prove > interesting. This is primarily motivated by my annoyance with using > comprehensions in certain circumstances. [...] > Having "typed" lists let you take preexisting string/int/etc methods > and expose them in a vectorized context and provides an easy way for > developers to support both vectors and scalars in a single function > (you could easily "fix" other people's functions dynamically to > support both). Additionally, "typed" lists/iterators will allow > improved code analysis and optimization. The PyPy people have > already stated that they are working on implementing different > strategies for lists composed of a single type, so clearly there is > already community movement in this direction. > > Just compare the above examples to their type-aware counterparts: > > L2 = X(L1) L2 = L1.X() > > L2 = Z(Y(X(L1))) L2 = L1.X().Y().Z() > > Also, this would provide a way to clean up stuff like: > > "\n".join(l.capitalize() for l in my_string.split("\n")) > > into: > > my_string.split("\n").capitalize().join_this("\n") > > Before anyone gets up in arms at the idea of statically typed > python, what I am suggesting here would be looser than that. > Basically, I believe it would be a good idea in instances where it is > known that a list of single type is going to be returned, to return a > list subclass (for example, StringList, IntegerList, etc). To avoid > handcuffing people with types, the standard list modification methods > could be hooked so that if an object of an incorrect type is placed > in the list, a warning is raised and the list converts to a generic > object list. The only stumbling block is that you can't use > __class__ to convert from stack types to heap types in CPython. My > workaround for this would be to have a factory that creates generic > "List" classes, modifying the bases to produce the correct behavior. > Then, converting from a typed list to a generic object list would > just be a matter of removing a member from the bases for a class. > This of course basically kills the ability to perform type specific > list optimization in CPython, but that isn't necessarily true for > other implementations. The additional type information would be > preserved for code analysis in any case. The case would be even > simpler for generators and other iterators, as you don't have to > worry about mutation. > > I'd like to hear people's thoughts on the subject. Currently we are > throwing away useful information in many cases that could be used > for code analysis, optimization and simpler interfaces. I think there are two aspects to your idea: 1. collections that share a single type 2. accessing multiple elements via a common interface Both are things that should be considered and I think both are useful in some contexts. The former would provide additional guarantees, for example, you could savely look up an attribute of the type only once while iterating over the sequence and use it for all elements. Also, I believe you could save some storage. The second aspect would mean that you have a single function call that targets multiple objects, which is syntactic sugar, but that's a good thing. To some extent, this looks like a C++ valarray, see e.g. [1] and [2] (note that I don't trust [1] and that [2] is perhaps a bit outdated), in case you know C++ and want to draw some inspiration from this. Anyway, I believe something like that would already be possible today, which would give people something they could actually try out instead of just musing about: class ValarrayWrapper(object): def __init__(self, elements): self._elements = elements def map(self, function): tmp = [function(x) for x in self._elements] return ValarrayWrapper(tmp) def apply(self, function) self._elements[:] = [function(x) for x in self._elements] I could even imagine this to implement "generic" attribute lookup by looking at the first element. If it contains the according attribute, return a proxy that allows calls to member functions or property access, depending on the type of the attribute. > I believe that "typed" lists that get "demoted" to normal lists with > a warning on out of type operations preserve this information while > providing complete backwards compatibility and freedom. I don't think that a warning helps people write correct code, a meaningful error does. Otherwise, with the same argument you could convert a tuple on the fly to a list when someone tries to change an element of it. Cheers! Uli [1] http://www.cplusplus.com/reference/std/valarray/abs/ [2] http://drdobbs.com/184403620 From lancee.ware at gmail.com Mon Dec 19 09:26:36 2011 From: lancee.ware at gmail.com (Overo) Date: Mon, 19 Dec 2011 06:26:36 -0800 (PST) Subject: Pycrypto AES CTR Message-ID: <58e00ba7-d4cc-4eb3-9325-94256aa37ee0@u5g2000vbd.googlegroups.com> Hello, I am writing an application that will decrypt an AES file with a counter attached at the beginning offset. I see that in the CTR mode. Pycrypto Seems to use the top 16-bytes as random data and perpends zero's at the bottom end. For the sake of more security, I would like to provide a counter with a 32-bits of unique's in order to give it more security. I wanted to know if 1) Will I be able to use the Counter class provided with pycrypto? 2) If not what call does pycrpto use to increment the counter so I can provide my own implementation. Thanks From duncan.booth at invalid.invalid Mon Dec 19 10:23:14 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 19 Dec 2011 15:23:14 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eed96c9$0$29979$c3e8da3$5496439d@news.astraweb.com> Message-ID: Evan Driscoll wrote: >> I'm not an expert on Google's syntax, but if you search for "python, >> optionally with function", isn't that the same as just searching for >> "python" since it will return hits either with or without "function"? > Chris Angelico's interpretation is correct: I did four searches, "python > function star", "python function asterisk", "python star", and "python > asterisk". > Using www.google.com: "python function star": first hit is: http://farmdev.com/thoughts/24/what- does-the-def-star-variable-or-def-asterisk-parameter-syntax-do-in-python-/ "python function asterisk": first hit is: http://www.technovelty.org/code/python/asterisk.html "python asterisk": the first hit I get is http://www.technovelty.org/code/python/asterisk.html "python star" is the only useless one of those giving me two hits for star pathfinding algorithms, one about the BNF used in Python's documentation, and a Daily Mail article about John Cleese and Eric Idle. -- Duncan Booth http://kupuguy.blogspot.com From devplayer at gmail.com Mon Dec 19 10:37:58 2011 From: devplayer at gmail.com (DevPlayer) Date: Mon, 19 Dec 2011 07:37:58 -0800 (PST) Subject: Newbie Question: Obtain element from list of tuples References: <2c286dea-efba-45d5-85a7-a7503733575a@i6g2000vbe.googlegroups.com> <4eeea8eb$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Dec 19, 1:46?am, "Frank Millman" wrote: > "Steven D'Aprano" wrote in message > > news:4eeea8eb$0$11091$c3e8da3 at news.astraweb.com... > > > On Sun, 18 Dec 2011 18:35:47 -0800, alex23 wrote: > > >> Pre-namedtuple, I used to like using named slices for this: > > >> ? ? cPID = slice(19) > >> ? ? pid = recs[cPID] > > > You know, that is an incredibly simple thing and yet it never dawned on > > me before now. Thank you for sharing that. > > I also like it, but it does not work quite so simply for me. > > >>> row = tuple(range(20)) > >>> cPID = slice(15) > >>> pid = row[cPID] > >>> pid > > (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) > > > > This works - > > > > >>> row = tuple(range(20)) > >>> cPID = slice(15, 16) > >>> pid, = row[cPID] ?# note the trailing comma > >>> pid > 15 > > Still nice, but not quite so elegant. > > Am I missing something? > > Frank Millman if you're going to do: > >>> cPID = slice(15, 16) > >>> pid, = row[cPID] # note the trailing comma Why not just: >>>row = tuple(range(20)) >>>cPID = 15 # a proposed constant >>>pid = row[cPID] >>>pid 15 It might be better for other's to debug if you use >>> pid, _ = row[cPID] as a little tiny comma like that is easy to miss in some text editors. Not everyone has large LCD screens. Not that I like the "_" for this purpose. Personally I'd love to see the "_" used instead of "self" as a common practice (useful when you use lots of "self...." on a line). From duncan.booth at invalid.invalid Mon Dec 19 10:46:12 2011 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 19 Dec 2011 15:46:12 GMT Subject: re.sub(): replace longest match instead of leftmost match? References: Message-ID: MRAB wrote: > On 16/12/2011 21:04, John Gordon wrote: >> In Devin >> Jeanpierre writes: >> >>> You could use re.finditer to find the longest match, and then >>> replace it manually by hand (via string slicing). >> >>> (a match is the longest if (m.end() - m.start()) is the largest -- >>> so, max(re.finditer(...), key=3Dlambda m: (m.end() =3D m.start())) >> >> I ended up doing something similar: >> >> # find the longest match >> longest_match = '' >> for word in re.findall('((0000:?)+)', ip6): >> if len(word[0])> len(longest_match): >> longest_match = word[0] >> >> # if we found a match, replace it with a colon >> if longest_match: >> ip6 = re.sub(longest_match, ':', ip6, 1) >> > For a simple replace, using re is probably overkill. The .replace > method is a better solution: > > ip6 = longest_match.replace(ip6, ':', 1) I think you got longest_match/ip6 backwards there. Anyway, for those who like brevity: try: ip6 = ip6.replace(max(re.findall('((?:0000:?)+)', ip6), key=len), ':', 1) except ValueError: pass -- Duncan Booth http://kupuguy.blogspot.com From nathan.alexander.rice at gmail.com Mon Dec 19 10:56:21 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Mon, 19 Dec 2011 10:56:21 -0500 Subject: Making the case for "typed" lists/iterators in python In-Reply-To: References: Message-ID: > I think there are two aspects to your idea: > 1. collections that share a single type > 2. accessing multiple elements via a common interface You are correct, and I now regret posing them in a coupled manner. > Both are things that should be considered and I think both are useful in > some contexts. The former would provide additional guarantees, for example, > you could savely look up an attribute of the type only once while iterating > over the sequence and use it for all elements. Also, I believe you could > save some storage. > > The second aspect would mean that you have a single function call that > targets multiple objects, which is syntactic sugar, but that's a good thing. > To some extent, this looks like a C++ valarray, see e.g. [1] and [2] (note > that I don't trust [1] and that [2] is perhaps a bit outdated), in case you > know C++ and want to draw some inspiration from this. > > Anyway, I believe something like that would already be possible today, which > would give people something they could actually try out instead of just > musing about: > > ? class ValarrayWrapper(object): > ? ? ? def __init__(self, elements): > ? ? ? ? ? self._elements = elements > ? ? ? def map(self, function): > ? ? ? ? ? tmp = [function(x) for x in self._elements] > ? ? ? ? ? return ValarrayWrapper(tmp) > ? ? ? def apply(self, function) > ? ? ? ? ? self._elements[:] = [function(x) for x in self._elements] > > I could even imagine this to implement "generic" attribute lookup by looking > at the first element. If it contains the according attribute, return a proxy > that allows calls to member functions or property access, depending on the > type of the attribute. Thank you for the references, I am always interested to see how other languages solve problems. I have received the "code please" comment repeatedly, I will have to take some time after work today to deliver. >> I believe that "typed" lists that get "demoted" to normal lists with >> a warning on out of type operations preserve this information while >> providing complete backwards compatibility and freedom. > > > I don't think that a warning helps people write correct code, a meaningful > error does. Otherwise, with the same argument you could convert a tuple on > the fly to a list when someone tries to change an element of it. I do agree errors are more normative than warnings. The problem with an error in these circumstances is it will certainly break code somewhere. Perhaps a warning that becomes an error at some point in the future would be the prudent way to go. Thanks! Nathan From wanderer at dialup4less.com Mon Dec 19 12:12:17 2011 From: wanderer at dialup4less.com (Wanderer) Date: Mon, 19 Dec 2011 09:12:17 -0800 (PST) Subject: IPython 0.12 is out! References: Message-ID: <1331f71b-b8c6-475c-af22-13c79cfa0471@o9g2000yqa.googlegroups.com> On Dec 19, 4:49?am, Fernando Perez wrote: > Hi all, > > on behalf of the IPython development team, I'm thrilled to announce, after > an intense 4 1/2 months of work, the official release of IPython 0.12. > > This is a very important release for IPython, for several reasons. First > and foremost, we have a major new feature, our interactive web-based > notebook, that has been in our sights for a very long time. ?We tried to > build one years ago (with WX) as a Google SoC project in 2005, had other > prototypes later on, but things never quite worked. ?Finally the > refactoring effort started two years ago, the communications architecture > we built in 2010, and the advances of modern browsers, gave us all the > necessary pieces. > > With this foundation in place, while part of the team worked on the 0.11 > release, Brian Granger had already started quietly building the web > notebook, which we demoed in early-alpha mode at the SciPy 2011 conference > (http://www.archive.org/details/Wednesday-203-6- > IpythonANewArchitectureForInteractiveAndParallel). ?By the EuroScipy > conference in August we had merged Brian's amazing effort into our master > branch, and after that multiple people (old and new) jumped in to make all > kinds of improvements, leaving us today with something that is an > excellent foundation. ?It's still the first release of the notebook, and > as such we know it has a number of rough edges, but several of us have > been using it as a daily research tool for the last few months. ?Do not > hesitate to file issues for any problems you encounter with it, and we > even have an 'open issue' for general discussion of ideas and features for > the notebook at:https://github.com/ipython/ipython/issues/977. > > Furthermore, it is clear that our big refactoring work, combined with the > amazing facilities at Github, are paying off. ?The 0.11 series was a major > amount of work, with 511 issues closed over almost two years. But that > pales in comparison to this cycle: in only 4 1/2 months we closed 515 > issues, with 50% being Pull Requests. And very importantly, our list of > contributors includes many new faces (see the credits section in our > release notes for full details), which is the best thing that can happen > to an open source project. > > We hope you will find the new features (the notebook isn't the only one! > see below) compelling, and that many more will not only use IPython but > will join the project; there's plenty to do and now there are tasks for > many different skill sets (web, javascript, gui work, low-level > networking, parallel machinery, console apps, etc). > > *Downloads* > > Download links and instructions are at:http://ipython.org/download.html > And IPython is also on PyPI:http://pypi.python.org/pypi/ipython > > Those contain a built version of the HTML docs; if you want pure source > downloads with no docs, those are available on github: > > Tarball:https://github.com/ipython/ipython/tarball/rel-0.12 > Zipball:https://github.com/ipython/ipython/zipball/rel-0.12 > > * Features * > Here is a quick listing of the major new features: > > - An interactive browser-based Notebook with rich media support > - Two-process terminal console > - Tabbed QtConsole > - Full Python 3 compatibility > - Standalone Kernel > - PyPy support > > And many more... > > We closed over 500 tickets, merged over 200 pull requests, and more than > 45 people contributed commits for the final release. > > Please see our release notes for the full details on everything about this > release:http://ipython.org/ipython-doc/stable/whatsnew/version0.12.html > > * IPython tutorial at PyCon 2012 * > > Those of you attending (or planning on it) PyCon 2012 in Santa Clara, CA, > may be interested in attending a hands-on tutorial we will be presenting > on the many faces of IPython. ?Seehttps://us.pycon.org/2012/schedule/ > presentation/121/ for full details. > > * Errata * > This was caught by Matthias Bussionnier's (one of our great new > contributors) sharp eyes while I was writing these release notes: In the > example notebook called display_protocol, the first cell starts with: > > from IPython.lib.pylabtools import print_figure > > which should instead be: > > from IPython.core.pylabtools import print_figure > > This has already been fixed on master, but since the final 0.12 files have > been uploaded to github and PyPI, we'll let them be. > > As usual, if you find any other problem, please file a ticket --or even > better, a pull request fixing it-- on our github issues site (https:// > github.com/ipython/ipython/issues/). > > Many thanks to all who contributed! > > Fernando, on behalf of the IPython development team. > > http://ipython.org Doesn't work for me. I installed it over my IPython 0.11 which did work for me. I get the following complaint. Traceback (most recent call last): File "C:\Python27\scripts\ipython.py", line 26, in (module) import Ipython.Shell File "C:\Python27\lib\site-packages\Ipython\Shell.py", line 38 in (module) from Ipython.Magic import Magic File "C:\Python27\lib\site-packages\Ipython\Magic.py", line 536, in Magic @testdec.skip_doctest AttributeError: 'module' object has no attribute 'skip_doctest' From tycho at tycho.ws Mon Dec 19 12:13:19 2011 From: tycho at tycho.ws (Tycho Andersen) Date: Mon, 19 Dec 2011 11:13:19 -0600 Subject: Getting a patch accepted Message-ID: <20111219171319.GB16551@smitten> Hi all, A couple months ago I found a bug in a corner of the curses library (http://bugs.python.org/issue13051) and filed it. Unfortunately, there was nobody listed to cc on the noisy list, so it probably got lost in the shuffle. (There is even previous mention of this bug elsewhere on the tracker, and on stackoverflow, so it does affect some small number of people.) Is there anyone else I can bother to get this patch applied? Thanks! \t From wanderer at dialup4less.com Mon Dec 19 13:07:21 2011 From: wanderer at dialup4less.com (Wanderer) Date: Mon, 19 Dec 2011 10:07:21 -0800 (PST) Subject: IPython 0.12 is out! References: <1331f71b-b8c6-475c-af22-13c79cfa0471@o9g2000yqa.googlegroups.com> Message-ID: On Dec 19, 12:12?pm, Wanderer wrote: > On Dec 19, 4:49?am, Fernando Perez wrote: > > > > > > > > > > > Hi all, > > > on behalf of the IPython development team, I'm thrilled to announce, after > > an intense 4 1/2 months of work, the official release of IPython 0.12. > > > This is a very important release for IPython, for several reasons. First > > and foremost, we have a major new feature, our interactive web-based > > notebook, that has been in our sights for a very long time. ?We tried to > > build one years ago (with WX) as a Google SoC project in 2005, had other > > prototypes later on, but things never quite worked. ?Finally the > > refactoring effort started two years ago, the communications architecture > > we built in 2010, and the advances of modern browsers, gave us all the > > necessary pieces. > > > With this foundation in place, while part of the team worked on the 0.11 > > release, Brian Granger had already started quietly building the web > > notebook, which we demoed in early-alpha mode at the SciPy 2011 conference > > (http://www.archive.org/details/Wednesday-203-6- > > IpythonANewArchitectureForInteractiveAndParallel). ?By the EuroScipy > > conference in August we had merged Brian's amazing effort into our master > > branch, and after that multiple people (old and new) jumped in to make all > > kinds of improvements, leaving us today with something that is an > > excellent foundation. ?It's still the first release of the notebook, and > > as such we know it has a number of rough edges, but several of us have > > been using it as a daily research tool for the last few months. ?Do not > > hesitate to file issues for any problems you encounter with it, and we > > even have an 'open issue' for general discussion of ideas and features for > > the notebook at:https://github.com/ipython/ipython/issues/977. > > > Furthermore, it is clear that our big refactoring work, combined with the > > amazing facilities at Github, are paying off. ?The 0.11 series was a major > > amount of work, with 511 issues closed over almost two years. But that > > pales in comparison to this cycle: in only 4 1/2 months we closed 515 > > issues, with 50% being Pull Requests. And very importantly, our list of > > contributors includes many new faces (see the credits section in our > > release notes for full details), which is the best thing that can happen > > to an open source project. > > > We hope you will find the new features (the notebook isn't the only one! > > see below) compelling, and that many more will not only use IPython but > > will join the project; there's plenty to do and now there are tasks for > > many different skill sets (web, javascript, gui work, low-level > > networking, parallel machinery, console apps, etc). > > > *Downloads* > > > Download links and instructions are at:http://ipython.org/download.html > > And IPython is also on PyPI:http://pypi.python.org/pypi/ipython > > > Those contain a built version of the HTML docs; if you want pure source > > downloads with no docs, those are available on github: > > > Tarball:https://github.com/ipython/ipython/tarball/rel-0.12 > > Zipball:https://github.com/ipython/ipython/zipball/rel-0.12 > > > * Features * > > Here is a quick listing of the major new features: > > > - An interactive browser-based Notebook with rich media support > > - Two-process terminal console > > - Tabbed QtConsole > > - Full Python 3 compatibility > > - Standalone Kernel > > - PyPy support > > > And many more... > > > We closed over 500 tickets, merged over 200 pull requests, and more than > > 45 people contributed commits for the final release. > > > Please see our release notes for the full details on everything about this > > release:http://ipython.org/ipython-doc/stable/whatsnew/version0.12.html > > > * IPython tutorial at PyCon 2012 * > > > Those of you attending (or planning on it) PyCon 2012 in Santa Clara, CA, > > may be interested in attending a hands-on tutorial we will be presenting > > on the many faces of IPython. ?Seehttps://us.pycon.org/2012/schedule/ > > presentation/121/ for full details. > > > * Errata * > > This was caught by Matthias Bussionnier's (one of our great new > > contributors) sharp eyes while I was writing these release notes: In the > > example notebook called display_protocol, the first cell starts with: > > > from IPython.lib.pylabtools import print_figure > > > which should instead be: > > > from IPython.core.pylabtools import print_figure > > > This has already been fixed on master, but since the final 0.12 files have > > been uploaded to github and PyPI, we'll let them be. > > > As usual, if you find any other problem, please file a ticket --or even > > better, a pull request fixing it-- on our github issues site (https:// > > github.com/ipython/ipython/issues/). > > > Many thanks to all who contributed! > > > Fernando, on behalf of the IPython development team. > > >http://ipython.org > > Doesn't work for me. I installed it over my IPython 0.11 which did > work for me. I get the following complaint. > > Traceback (most recent call last): > ?File "C:\Python27\scripts\ipython.py", line 26, in (module) > ? import Ipython.Shell > ?File "C:\Python27\lib\site-packages\Ipython\Shell.py", line 38 in > (module) > ? from Ipython.Magic import Magic > ?File "C:\Python27\lib\site-packages\Ipython\Magic.py", line 536, in > Magic > ? @testdec.skip_doctest > AttributeError: 'module' object has no attribute 'skip_doctest' The windows installer didn't work but installing from the tar file did. But installing from the tar file doesn't install Ipython in the site-packages directory. It installs it wherever you untar the tar file. I don't remember ever having to deal with this before. Most things just install in the site-packages directory without me having to do anything special. From bex.lewis at gmail.com Mon Dec 19 14:05:16 2011 From: bex.lewis at gmail.com (becky_lewis) Date: Mon, 19 Dec 2011 11:05:16 -0800 (PST) Subject: IPython 0.12 is out! References: Message-ID: Thanks and congratulations! Installed via pip --upgrade and everything seems to be working just fine (python 2.7.0+ on Linux) I look forward to investigating all of the new features! Becky Lewis On Dec 19, 9:49?am, Fernando Perez wrote: > Hi all, > > on behalf of the IPython development team, I'm thrilled to announce, after > an intense 4 1/2 months of work, the official release of IPython 0.12. > ... snip ... From nad at acm.org Mon Dec 19 14:19:02 2011 From: nad at acm.org (Ned Deily) Date: Mon, 19 Dec 2011 14:19:02 -0500 Subject: Can't get tilde character with IDLE 3.2.2 on French Mac Lion References: Message-ID: In article , Franck Ditter wrote: > All is in the subject. I'm starting to use Python with Idle 3.2.2 > on MacOS-X Lion (French). I can't get "Option-N space" to provide > the ~ char. > I tried to go into the Keys preferences but I can't find "Option-N space" > to modify its meaning. Its actual behavior is to merge lines of a > paragraph. You are likely running into a current problem in the OS X Cocoa version of Tcl/Tk 8.5 as included with Lion and as shipped by ActiveState. Previously, if you tried to type composite characters, like Option N, the Cocoa Tcl/Tk would crash. Pending a real fix, a patch was made to Tcl/Tk 8.5 to discard composite characters rather than crash. You should be able to get a tilde by using the post-composite keyboard sequence: try typing "space" followed by "Shift-Option-N". http://sourceforge.net/tracker/index.php?func=detail&aid=2907388&group_id =12997&atid=112997 -- Ned Deily, nad at acm.org From irmen at -NOSPAM-xs4all.nl Mon Dec 19 14:39:25 2011 From: irmen at -NOSPAM-xs4all.nl (Irmen de Jong) Date: Mon, 19 Dec 2011 20:39:25 +0100 Subject: Getting a patch accepted In-Reply-To: References: Message-ID: <4eef92ed$0$6932$e4fe514c@news2.news.xs4all.nl> On 19-12-11 18:13, Tycho Andersen wrote: > Hi all, > > A couple months ago I found a bug in a corner of the curses library > (http://bugs.python.org/issue13051) and filed it. Unfortunately, there > was nobody listed to cc on the noisy list, so it probably got lost in > the shuffle. (There is even previous mention of this bug elsewhere on > the tracker, and on stackoverflow, so it does affect some small number > of people.) > > Is there anyone else I can bother to get this patch applied? > Maybe the folks over at #python-dev on freenode can help you out? Also the dev guide has some hints on the 'lifecycle of a patch' http://docs.python.org/devguide/patch.html Irmen From rantingrickjohnson at gmail.com Mon Dec 19 16:24:11 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Mon, 19 Dec 2011 13:24:11 -0800 (PST) Subject: Getting a patch accepted References: Message-ID: On Dec 19, 11:13?am, Tycho Andersen wrote: > Hi all, > > A couple months ago I found a bug in a corner of the curses library > (http://bugs.python.org/issue13051) and filed it. Unfortunately, there > was nobody listed to cc on the noisy list, so it probably got lost in > the shuffle. (There is even previous mention of this bug elsewhere on > the tracker, and on stackoverflow, so it does affect some small number > of people.) > > Is there anyone else I can bother to get this patch applied? My friend, it seems as though you've exhausted all avenues and explored all options. Now you need to contact upper management. I'd dare say you should go further! Yes. I'd say you should contact the great Guido van Rossum himself and bring this atrocious patching system to his attention. If no one in the community has the time to patch a bug in the source, then by god GvR needs to step forth and earn that "Benevolent" scout patch. I mean, he has the 20% time from Google for which he can do whatever he likes. What did he think, we were going to let him veg out on the couch watching re-runs of Monty Pythons Flying Circus! He'll end up like Shatner for christ sakes! I want to see him mingling with the masses and manning the trenches. I want to see him shaking hands and kissing babies! From brian at python.org Mon Dec 19 16:37:43 2011 From: brian at python.org (Brian Curtin) Date: Mon, 19 Dec 2011 15:37:43 -0600 Subject: Getting a patch accepted In-Reply-To: <4eef92ed$0$6932$e4fe514c@news2.news.xs4all.nl> References: <4eef92ed$0$6932$e4fe514c@news2.news.xs4all.nl> Message-ID: On 19-12-11 18:13, Tycho Andersen wrote: > > Hi all, > > A couple months ago I found a bug in a corner of the curses library > (http://bugs.python.org/issue13051) and filed it. Unfortunately, there > was nobody listed to cc on the noisy list, so it probably got lost in > the shuffle. (There is even previous mention of this bug elsewhere on > the tracker, and on stackoverflow, so it does affect some small number > of people.) > > Is there anyone else I can bother to get this patch applied? Part of the issue with this taking a while is that there's no one on the committer side of things that deals with curses. For one thing, I see that the patch has no tests, so you might want to add unit tests to ensure that your changes don't break anything else. I left a comment saying that on the issue, so I'll get any updates you make and can look at them. Rick Johnson's response is, as always, pure trolling. I'd suggest you ignore him. From bahamutzero8825 at gmail.com Mon Dec 19 16:57:59 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Mon, 19 Dec 2011 15:57:59 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: <4EEFB367.7030408@gmail.com> On 12/18/2011 8:16 PM, Rick Johnson wrote: > On Dec 18, 7:26 pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> Not everybody uses editors more advanced than Notepad. > And they have no excuse for NOT using a better one. Well, except for a > "foolish consistency" that is! But what about the example he gave about being logged into a customer's machine with only ed available? I suppose such fools would not be worthy of your business. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From Juan.Declet-Barreto at MesaAZ.gov Mon Dec 19 17:02:50 2011 From: Juan.Declet-Barreto at MesaAZ.gov (Juan Declet-Barreto) Date: Mon, 19 Dec 2011 15:02:50 -0700 Subject: the slash & Windows paths Message-ID: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> All, I have a Windows-style path that I need to modify so Python functions can find it. For example, the path "C:\Projects\Population_Pyramids\charts\test.xls" is being interpreted as pointing to a file called "est.xls" since the "t" is being escaped by the preceding slash as a tab. I know this is straightforward to handle using raw literals by prefixing the literal with an 'r', but my path is stored in a variable. My questions are: 1. How do I prevent single slashes from being escaped in a string variable (NOT literal)? 2. Is it possible to do a string.replace to substitute all instances of '\' for '\\'? I tried this but it did not give me the desired result. Juan Declet-Barreto [cid:image004.png at 01CCBE5F.46845530] GIS Specialist, Information Technology Dept. City of Mesa Office: 480.644.4751 juan.declet-barreto at mesaaz.gov [cid:image003.png at 01CCBE5E.C7AA1EC0] -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image003.png Type: image/png Size: 11402 bytes Desc: image003.png URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image004.png Type: image/png Size: 1454 bytes Desc: image004.png URL: From bahamutzero8825 at gmail.com Mon Dec 19 17:03:14 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Mon, 19 Dec 2011 16:03:14 -0600 Subject: Getting a patch accepted In-Reply-To: References: Message-ID: <4EEFB4A2.5030205@gmail.com> On 12/19/2011 3:24 PM, Rick Johnson wrote: > ... > I want to see him mingling with > the masses and manning the trenches. I want to see him shaking hands > and kissing babies! I prefer these over-the-top responses over the more subtle ones. Much more entertaining IMO. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From brian at python.org Mon Dec 19 17:26:52 2011 From: brian at python.org (Brian Curtin) Date: Mon, 19 Dec 2011 16:26:52 -0600 Subject: the slash & Windows paths In-Reply-To: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> References: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> Message-ID: On Mon, Dec 19, 2011 at 16:02, Juan Declet-Barreto < Juan.Declet-Barreto at mesaaz.gov> wrote: > All,**** > > ** ** > > I have a Windows-style path that I need to modify so Python functions can > find it. For example, the path > ?C:\Projects\Population_Pyramids\charts\test.xls? is being interpreted as > pointing to a file called ?est.xls? since the ?t? is being escaped by the > preceding slash as a tab. **** > > ** ** > > I know this is straightforward to handle using raw literals by prefixing > the literal with an ?r?, but my path is stored in a variable. **** > > ** ** > > My questions are: 1. How do I prevent single slashes from being escaped in > a string variable (NOT literal)? **** > > 2. Is it possible to do a > string.replace to substitute all instances of ?\? for ?\\?? I tried this > but it did not give me the desired result. > Use os.path.normpath(your_path). -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Mon Dec 19 17:32:13 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Mon, 19 Dec 2011 16:32:13 -0600 Subject: the slash & Windows paths In-Reply-To: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> References: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> Message-ID: <4EEFBB6D.8020900@gmail.com> On 12/19/2011 4:02 PM, Juan Declet-Barreto wrote: > I have a Windows-style path that I need to modify so Python functions > can find it. For example, the path > ?C:\Projects\Population_Pyramids\charts\test.xls? is being interpreted > as pointing to a file called ?est.xls? since the ?t? is being escaped by > the preceding slash as a tab. Whatever code is getting the path and storing it into a variable needs to escape the path. Otherwise, you'll have to replace each tab/newline/etc. with literal \t/\n/\r/etc., and that's ugly and could lead to problems (you may have special characters you want to keep). The reason your replace code didn't work is because the \t is a tab character, not a literal "\t". The replace function doesn't see "\t", but rather a tab. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From ting at thsu.org Mon Dec 19 18:15:06 2011 From: ting at thsu.org (ting at thsu.org) Date: Mon, 19 Dec 2011 15:15:06 -0800 (PST) Subject: re.sub(): replace longest match instead of leftmost match? References: Message-ID: On Dec 16, 11:49?am, John Gordon wrote: > I'm working with IPv6 CIDR strings, and I want to replace the longest > match of "(0000:|0000$)+" with ":". ?But when I use re.sub() it replaces > the leftmost match, even if there is a longer match later in the string. Typically this means that your regular expression is not specific enough. That is, if you get multiple matches, and you need to sort through those matches before performing a replace, it usually means that you should rewrite your expression to get a single match. Invariably this happens when you try to take short cuts. I can't blame you for using a short cut, as sometimes short cuts just work, but once you find that your short cut fails, you need to step back and rethink the problem, rather than try to hack your short cut. I don't know what you are doing, but off the top of my head, I'd check to see if the CIDR string is wrapped in a header message and include the header as part of the search pattern, or if you know the IPv6 strings are interspersed with IPv4 strings, I would rewrite the regex to exclude IPv4 strings. -- // T.Hsu From miki.tebeka at gmail.com Mon Dec 19 18:18:19 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Mon, 19 Dec 2011 15:18:19 -0800 (PST) Subject: Parsing stream of JSON objects incrementally In-Reply-To: References: Message-ID: <22477795.145.1324336699742.JavaMail.geo-discussion-forums@yqir6> You probably need to accumulate a buffer and try to decode it, when succeeded return the object and read more. See example below (note that for sockets select might be a better option for reading data). import json from time import sleep def json_decoder(fo): buff = '' decode = json.JSONDecoder().raw_decode while True: line = fo.readline() if not line: break buff += line print('BUFF: {}'.format(buff)) try: obj, i = decode(buff) buff = buff[i:].lstrip() yield obj except ValueError as e: print('ERR: {}'.format(e)) sleep(0.01) # select will probably be a better option :) def main(): import sys for obj in json_decoder(sys.stdin): print(obj) if __name__ == '__main__': main() From miki.tebeka at gmail.com Mon Dec 19 18:18:19 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Mon, 19 Dec 2011 15:18:19 -0800 (PST) Subject: Parsing stream of JSON objects incrementally In-Reply-To: References: Message-ID: <22477795.145.1324336699742.JavaMail.geo-discussion-forums@yqir6> You probably need to accumulate a buffer and try to decode it, when succeeded return the object and read more. See example below (note that for sockets select might be a better option for reading data). import json from time import sleep def json_decoder(fo): buff = '' decode = json.JSONDecoder().raw_decode while True: line = fo.readline() if not line: break buff += line print('BUFF: {}'.format(buff)) try: obj, i = decode(buff) buff = buff[i:].lstrip() yield obj except ValueError as e: print('ERR: {}'.format(e)) sleep(0.01) # select will probably be a better option :) def main(): import sys for obj in json_decoder(sys.stdin): print(obj) if __name__ == '__main__': main() From jerome at jolimont.fr Mon Dec 19 18:50:17 2011 From: jerome at jolimont.fr (=?UTF-8?B?SsOpcsO0bWU=?=) Date: Tue, 20 Dec 2011 00:50:17 +0100 Subject: Threading issue (using alsaaudio) Message-ID: <20111220005017.7f713e14@bouzin.lan> Hi all. I am currently learning python (and pyGTK) and I'm having a hard time understanding some threading stuff. I'm writing here hoping I can get - pointers to some documentation that could help me - a lead concerning the specific problem described in the message - a hint about which library to use for audio playback As an exercise, I am trying to write an application that plays sounds through the speakers. For that, I use alsaaudio (http://pyalsaaudio.sourceforge.net/). The sound is just a sine wave. I guess using threading is the good way to go. So I create a thread. The run() method calls PCM.write() from alsaudio to send the sound to a playback buffer. Once the thread is started, the button is disabled, and it is enabled again when the thread is dead. This is done via GObject.idle_add(), which triggers a regular call to the thread is_alive() function. There's a design flow, here, as the run() method that calls pcm.write() exits when the sound data is buffered (this is quick, say 0.1s), not when it is actualy finished playing (this is long : 1s). Therefore, the thread dies long before the sound is finished playing and the button is going to be enabled too soon. But this is not what happens, and before finding another way to proceed (this is likely to be a timer), I would like to understand what actually happens (remember, this is just an exercise). What happens is that the idle_add polling occurs as long as the run() method is running, but then, it gets stuck until the sound has actually finished playing. The button is enabled at the end of the playback (which is what I wanted in the first place, except I'd like to understand why...). Besides, a second click (on the then disabled button) during the playback is not ignored, but generates another sound after the first. It seems that while the thread is dead (or should be, as the last print has been reached and executed), as long as the sound is being played, the Gtk.main() is stuck and does not process either gtk events or idle tasks. I don't understand that. I wonder what happens and in which thread lies my program while the sound is playing, between the thread run() end and the Gtk.main(). (There's a note in alsaaudio documentation about pcm.write normal and non-block modes but I don't think it is relevant here because, as far as I understand, the data sent does not fill the buffer so in both modes, pcm.write() should exit without blocking. (I might be wrong about this.)) I tried to remove from the code all that has nothing to do with this issue, and I left the prints that reveal what happens. Here it is : --------------------------------------------------------------------------- #!/usr/bin/env python # -*- coding: UTF8 -*- from gi.repository import Gtk, GObject import alsaaudio from numpy import arange, sin, ceil, pi, float32, hstack import threading import time ######################################################################### # audio functions ######################################################################### class Alsa_play_note(threading.Thread): def run(self): print "Entering thread run() method" length = 1 #?1 second freq = 440 channels = 1 sample_size = 1 # bytes per sample frame_size = channels * sample_size # bytes per frame frame_rate = 44000 # frames per second period_size = int(frame_rate * length) pcm = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, alsaaudio.PCM_NORMAL) #pcm = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, alsaaudio.PCM_NONBLOCK) pcm.setchannels(channels) pcm.setrate(frame_rate) pcm.setformat(alsaaudio.PCM_FORMAT_FLOAT_LE) pcm.setperiodsize(period_size) nb_samp = int(ceil(frame_rate / freq)) sample = arange(0, nb_samp, dtype=float32) sample *= pi * 2 / 44100 * freq sample = sin(sample) (nw, ex) = divmod(period_size * frame_size, nb_samp) wave_data = sample for i in range(nw-2): wave_data = hstack((wave_data, sample)) wave_data = hstack((wave_data, sample[:ex])) print "run() -> entering pcm.write" #time.sleep(1) a = pcm.write(wave_data.tostring()) #print a print "run() -> pcm.write done" print "Leaving thread run() method" def __init__(self): threading.Thread.__init__(self) ######################################################################### # class Keyboard ######################################################################### class Keyboard: ##################################################################### # Play note ##################################################################### def play_note(self, widget): print "Entering play_note() callback" self._button.set_sensitive(False) print "Creating thread" self._alsa_thread = Alsa_play_note() print "Calling thread.start()" self._alsa_thread.start() print "Back from thread.start()" GObject.idle_add(self._poll_alsa_play_in_progress) print "GObject.idle_add() done" ##################################################################### # Poll alsa play in progress ##################################################################### def _poll_alsa_play_in_progress (self): print "." if (self._alsa_thread.is_alive()): return True print "Button enabled" self._button.set_sensitive(True) return False ##################################################################### # Init ##################################################################### def __init__(self): # Declare thread handler self._alsa_thread = 0 # Create window ############### self._window = Gtk.Window() self._window.connect("destroy", Gtk.main_quit) self._window.connect("destroy", Gtk.main_quit) # Key button ############ self._button = Gtk.Button() self._button.set_size_request(400, 100) self._button.set_label("A") self._button.show() # Connect handler self._handler = self._button.connect("clicked", self.play_note) # Show everything ################# self._window.add(self._button) self._window.show() ######################################################################### # main ######################################################################### def main(): GObject.threads_init() Gtk.main() return 0 if __name__ == "__main__": Keyboard() main() --------------------------------------------------------------------------- I'd be thankful for any tip. I've been pulling my hair quite some time with these threading issues. Regarding python and pyGTK, I'm generally refering to : Python 2.7 documentation http://docs.python.org/ python GTK+3 tutorial (not really complete) http://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/index.html python GTK2.0 tutorial http://pygtk.org/pygtk2tutorial/index.html I may be missing some documentation / tutorials. Anything else I should read extensively before asking here ? Regarding the playback library, I've been searching before choosing alsaaudio. I chose ALSA because I understand OSS is deprecated, or on its way to be. And alsaaudio seemed to be the project with the most recent updates. Yet it does not pretend to be (or aim at being) complete. Was that a sensible choice ? Would you recommend something else ? The learning curve is sometimes steep... I would be thankful for any lead. -- J?r?me From ting at thsu.org Mon Dec 19 18:56:43 2011 From: ting at thsu.org (ting at thsu.org) Date: Mon, 19 Dec 2011 15:56:43 -0800 (PST) Subject: Make a small function thread safe References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> Message-ID: <13838f06-58a2-4f77-8279-25e414d9c64c@z19g2000vbe.googlegroups.com> On Dec 16, 8:21?am, Brad Tilley wrote: > A thread locks the function on entrance and then releases it on exit. > What is the equivalent way to do this in Python? I'm not sure if this applies in your case, but much of the time, you can use thread local storage, rather thread locking, in order to make your code thread safe. You tend to run into far less bottleneck and race condition issues by using thread local storage rather than thread locking, whenever possible. -- // T.Hsu From d at davea.name Mon Dec 19 19:15:23 2011 From: d at davea.name (Dave Angel) Date: Mon, 19 Dec 2011 19:15:23 -0500 Subject: the slash & Windows paths In-Reply-To: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> References: <3CB1190388197146B35D522652EAB501016E4044BA@MESAMAIL01.acctcom.mesa> Message-ID: <4EEFD39B.5010705@davea.name> On 12/19/2011 05:02 PM, Juan Declet-Barreto wrote: > All, > > I have a Windows-style path that I need to modify so Python functions can find it. For example, the path "C:\Projects\Population_Pyramids\charts\test.xls" is being interpreted as pointing to a file called "est.xls" since the "t" is being escaped by the preceding slash as a tab. > > I know this is straightforward to handle using raw literals by prefixing the literal with an 'r', but my path is stored in a variable. > > My questions are: 1. How do I prevent single slashes from being escaped in a string variable (NOT literal)? > 2. Is it possible to do a string.replace to substitute all instances of '\' for '\\'? I tried this but it did not give me the desired result. > So far you haven't told us how you got this weird pattern into your variable. For example, if it was typed in by the user, in the form: mypath = raw_input("what's yourpath?") then the question is nonsensical. There's no escaping needed, as it has never been a literal string. if it came from a text file, there's no escaping needed. So give us a real scenario, and we'll probably be able to figure out where you're going wrong. -- DaveA From roy at panix.com Mon Dec 19 19:30:53 2011 From: roy at panix.com (Roy Smith) Date: Mon, 19 Dec 2011 19:30:53 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: In article , Andrew Berg wrote: > But what about the example he gave about being logged into a customer's > machine with only ed available? I suppose such fools would not be worthy > of your business. The customer is always right. Especially when the support contract is big enough to make or break your quarter. From someone at someplace.invalid Mon Dec 19 19:59:32 2011 From: someone at someplace.invalid (HoneyMonster) Date: Tue, 20 Dec 2011 00:59:32 +0000 (UTC) Subject: Newbie Question: Obtain element from list of tuples References: Message-ID: On Mon, 19 Dec 2011 10:40:06 +1100, Chris Angelico wrote: > On Mon, Dec 19, 2011 at 9:55 AM, HoneyMonster > wrote: >> When the user selects a row and clicks a button, I am using: >> pos = self.grid_1.GetGridCursorRow() to establish which tuple in recs >> is involved, and then pid = recs[pos][4] to determine the key value (I >> suspected that accessing recs directly would be more efficient that >> trying to determine the wxGrid column value, and in any case the pid is >> in recs but not in the wxGrid). > > Yep, this looks like a sensible way to do it! > > I would recommend, though, that you avoid "magic numbers" - the process > ID might happen to be in cell 4, but what if your column list changes? > This is especially problematic when you use "select * from table", > because changes outside your code can break things. > > There's two solutions. One is to carefully match your SELECT statement > to a set of constants: > > exec("SELECT FOO,BAR,QUUX,ASDF,PID,WHATEVER FROM table") > FOO,BAR,QUUX,ASDF,PID,WHATEVER,*_=range(20) # Cool trick to avoid having > to count the columns > > But a better way is to let the database handle it. Check your interface > library to see if you can have it return dictionaries or objects instead > of tuples - then you could use: > > pid = recs[pos]["pid"] > # or pid = recs[pos]->pid > > which avoids the need to count columns at all. > > Other than that, though, yep - your code concept looks fine. Thanks again, Chris. I'm completely new to Python, but am an old had at databases. I *never* use "select *" in a production environment; rather an explicit list of columns. Thus it doesn't matter if a column is added to the table, nor if the column order in the table is changed. But I have used this at the start if my code to make it more readable anyway: # Define constants (position in tuple) Title = 0 Episode = 1 Categories = 2 Channel = 3 PID = 4 Index = 5 so that in the bit where I populate the grid, the code is now: cnt = 0 for tup in recs: self.grid_1.SetCellValue(cnt, Title, tup[Title]) self.grid_1.SetCellValue(cnt, Episode, tup[Episode]) self.grid_1.SetCellValue(cnt, Categories, tup[Categories]) self.grid_1.SetCellValue(cnt, Channel, tup[Channel]) cnt = cnt + 1 just so that is more legible. Cheers, HM From joshua.landau.ws at gmail.com Mon Dec 19 20:10:59 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Dec 2011 01:10:59 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: On 20 December 2011 00:30, Roy Smith wrote: > In article , > Andrew Berg wrote: > > > But what about the example he gave about being logged into a customer's > > machine with only ed available? I suppose such fools would not be worthy > > of your business. > > The customer is always right. Especially when the support contract is > big enough to make or break your quarter. > -- > http://mail.python.org/mailman/listinfo/python-list > This whole conversation is ridiculous. Do we really want a syntax that's worse just to satisfy the 1/10,000 cases* where someone will be using ed or notepad? Correct Answer: no. * Probably less, as I use vim many_times_a_day * many_days_a_year - and even less if we consider that no-one'll be doing as much coding in an hour of ed as in an hour of -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Mon Dec 19 20:18:14 2011 From: roy at panix.com (Roy Smith) Date: Mon, 19 Dec 2011 20:18:14 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: Sorry, I wasn't meaning to imply support for the syntax proposal. Just reacting to the (seemingly unrelated) comment that a customer with foolish access policies would not be worthy of your business. Only because I've been in the situation of having to provide remote support to major customers with similar absurd policies :-) On Dec 19, 2011, at 8:10 PM, Joshua Landau wrote: > On 20 December 2011 00:30, Roy Smith wrote: > In article , > Andrew Berg wrote: > > > But what about the example he gave about being logged into a customer's > > machine with only ed available? I suppose such fools would not be worthy > > of your business. > > The customer is always right. Especially when the support contract is > big enough to make or break your quarter. > -- > http://mail.python.org/mailman/listinfo/python-list > > This whole conversation is ridiculous. Do we really want a syntax that's worse just to satisfy the 1/10,000 cases* where someone will be using ed or notepad? > > Correct Answer: no. > > * Probably less, as I use vim many_times_a_day * many_days_a_year - and even less if we consider that no-one'll be doing as much coding in an hour of ed as in an hour of -- Roy Smith roy at panix.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Mon Dec 19 20:22:52 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Dec 2011 01:22:52 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: On 20 December 2011 01:18, Roy Smith wrote: > Sorry, I wasn't meaning to imply support for the syntax proposal. Just > reacting to the (seemingly unrelated) comment that a customer with foolish > access policies would not be worthy of your business. Only because I've > been in the situation of having to provide remote support to major > customers with similar absurd policies :-) > Nononono! I wasn't referring to you! I was referring to the whole topic. You just happened to be that last person to say something. Sorry about that, I should have been clearer... > On Dec 19, 2011, at 8:10 PM, Joshua Landau wrote: > > On 20 December 2011 00:30, Roy Smith wrote: > >> In article , >> Andrew Berg wrote: >> >> > But what about the example he gave about being logged into a customer's >> > machine with only ed available? I suppose such fools would not be worthy >> > of your business. >> >> The customer is always right. Especially when the support contract is >> big enough to make or break your quarter. >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > This whole conversation is ridiculous. Do we really want a syntax that's > worse just to satisfy the 1/10,000 cases* where someone will be using ed or > notepad? > > Correct Answer: no. > > * Probably less, as I use vim many_times_a_day * many_days_a_year - and > even less if we consider that no-one'll be doing as much coding in an hour > of ed as in an hour of > > > > -- > Roy Smith > roy at panix.com > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Mon Dec 19 20:34:25 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Dec 2011 01:34:25 +0000 Subject: Grammar for classes Message-ID: In reading thorough the syntax defined in the reference, the class statement has surprised me. It says that the inheritance part of the class can accept comprehensions. What does this mean? I've tried: "class A(x for x in ()): pass" but this doesn't need the extra clause as "x for x in ()" is an expression, and thus this evaluates: "class A(x for x in (),): pass" although again it won't be a valid class anytime soon. So what is this clause for? -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Mon Dec 19 20:41:42 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Mon, 19 Dec 2011 19:41:42 -0600 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: <4EEFE7D6.2090903@gmail.com> On 12/19/2011 7:18 PM, Roy Smith wrote: > Sorry, I wasn't meaning to imply support for the syntax proposal. Just > reacting to the (seemingly unrelated) comment that a customer with > foolish access policies would not be worthy of your business. It was directed at Rick, and by "your", I was referring to him specifically, as that is the kind of attitude I might expect him to show. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From gnarlodious at gmail.com Mon Dec 19 21:04:15 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Mon, 19 Dec 2011 18:04:15 -0800 (PST) Subject: Transform two tuples item by item Message-ID: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> What is the best way to operate on a tuple of values transforming them against a tuple of operations? Result can be a list or tuple: tup=(35, '34', 0, 1, 31, 0, '既濟') from cgi import escape [tup[0], "{}".format(tup[1]), bool(tup[2]), bool(tup[3]), tup[4], bool(tup[5]), escape(tup[6])] -> [35, "34", False, True, 31, False, '&#26082;&#28639;'] But I want to loop rather than subscripting. -- Gnarlie From python.list at tim.thechases.com Mon Dec 19 21:42:13 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 19 Dec 2011 20:42:13 -0600 Subject: Transform two tuples item by item In-Reply-To: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> References: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> Message-ID: <4EEFF605.8040100@tim.thechases.com> On 12/19/11 20:04, Gnarlodious wrote: > What is the best way to operate on a tuple of values > transforming them against a tuple of operations? Result can be > a list or tuple: > > tup=(35, '34', 0, 1, 31, 0, '既濟') > > from cgi import escape > [tup[0], " class='H'>{}".format(tup[1]), bool(tup[2]), > bool(tup[3]), tup[4], bool(tup[5]), escape(tup[6])] > > -> [35, "34", False, True, 31, False, > '&#26082;&#28639;'] > > But I want to loop rather than subscripting. Well, you can do something like >>> from cgi import escape >>> nop = lambda x: x >>> tup = (35, '34', 0, 1, 31, 0, '既濟') >>> ops = [nop, "{0}".format, bool, bool, nop, bool, escape] >>> [f(x) for f, x in zip(ops,tup)] [35, "34", False, True, 31, False, '&#26082;&#28639;'] Note #1: I had to change your format from "{}" to "{0}", at least in 2.6 I've got here) Note #2: it's spelled ".format" not ".format()" which puts the function reference in the "ops" list, not the results of calling it. -tkc From 1011_wxy at 163.com Mon Dec 19 21:44:07 2011 From: 1011_wxy at 163.com (1011_wxy) Date: Tue, 20 Dec 2011 10:44:07 +0800 (CST) Subject: About pysvn for x64 system In-Reply-To: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> References: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> Message-ID: <4b4ac11a.12356.134595ac321.Coremail.1011_wxy@163.com> Hi: I want to use svn to backup my oracle db object daily,so i want to use pysvn module. server: windows server 2008 64bit python:Python 2.6.5 (r265:79096, Mar 19 2010, 18:02:59) [MSC v.1500 64 bit (AMD64)] on win32 I have downloaded pysvn from "http://pysvn.tigris.org/files/documents/1233/48844/py26-pysvn-svn1615-1.7.5-1360.exe",and It can't be installed unless I copy [HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.6\InstallPath] to [HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\2.6\InstallPath]. After I installed with no error occured >>> import pysvn Traceback (most recent call last): File "", line 1, in import pysvn File "C:\Python26\lib\site-packages\pysvn\__init__.py", line 99, in import _pysvn_2_6 ImportError: DLL load failed: %1 is not valid Win32 application. So, I think there is no pysvn for 64bit windows,is that right? -------------- next part -------------- An HTML attachment was scrubbed... URL: From pytom at yahoo.cn Mon Dec 19 21:53:13 2011 From: pytom at yahoo.cn (Tom Zhou) Date: Tue, 20 Dec 2011 10:53:13 +0800 (CST) Subject: Columnize in module "cmd" Message-ID: <1324349593.51908.YahooMailNeo@web92411.mail.cnh.yahoo.com> Hi~ alls recently, i focus on the module "cmd", and find some confused things-- the function named "columnize". Why we need a multiloop as "for nrows .. ? ? ? ? for col .. ? ? ? ? ? ? ? ? ? ?for row.." ?? i think we can make a easier method, for example, first, find out the maxlen str in list, and use its length as the standard size to format the list. Ok, maybe i ignore something, so please give me some hints. --- thanks tom -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Mon Dec 19 21:58:11 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 19 Dec 2011 19:58:11 -0700 Subject: re.sub(): replace longest match instead of leftmost match? In-Reply-To: References: Message-ID: On Mon, Dec 19, 2011 at 4:15 PM, wrote: > On Dec 16, 11:49?am, John Gordon wrote: >> I'm working with IPv6 CIDR strings, and I want to replace the longest >> match of "(0000:|0000$)+" with ":". ?But when I use re.sub() it replaces >> the leftmost match, even if there is a longer match later in the string. > > Typically this means that your regular expression is not specific > enough. > > That is, if you get multiple matches, and you need to sort through > those matches before performing a replace, it usually means that you > should rewrite your expression to get a single match. > > Invariably this happens when you try to take short cuts. I can't blame > you for using a short cut, as sometimes short cuts just work, but once > you find that your short cut fails, you need to step back and rethink > the problem, rather than try to hack your short cut. The problem isn't short cuts. To narrow down multiple matches to a single longest match here, two additional criteria need to be met: there must be no other match anywhere in the search string that is longer than the match being considered, and there must be no match of equal length preceding the match being considered. Note that in the general case, the language is not regular and it would not even be possible to get a single longest match using a regular expression. For IPv6, it is possible only because IPv6 addresses are bounded in length. In English, that regular expression would be constructed like this: Any block of four or more groups of zeroes OR Zero or more blocks containing (zero to two groups of zero followed by a non-zero group) followed by a block of three or more groups of zeroes followed by zero or more blocks containing (a non-zero group followed by zero to three groups of zeroes) OR Zero or more blocks containing (an optional zero group followed by a non-zero group) followed by a block of two or more groups of zeroes followed by zero or more blocks containing (a non-zero group followed by zero to to two groups of zeroes) OR Zero or more non-zero groups followed by a group of zeroes followed by zero or more blocks containing (a non-zero group followed by an optional zero group). If anyone wants to give a crack at translating that to an actual regular expression, I'd be interested in seeing it. The added complexity is so great, though, that I for one would vastly prefer the simple solution already proposed of getting all the matches and iterating to find the longest. Cheers, Ian From rantingrickjohnson at gmail.com Mon Dec 19 22:15:33 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Mon, 19 Dec 2011 19:15:33 -0800 (PST) Subject: The ever-expanding bubble of GREED Message-ID: <24b782e6-6320-4f97-ad54-a0609450e4d0@x7g2000yqb.googlegroups.com> I just read an article[1] reporting that Apple has won a court case against Andoid (and others) over a patent infringement. Before i was able to see what all the hub-bub was about my mind started to wonder what "advanced technology" Google has stolen from Apple. To my great UNSURPRISE the so-called "advanced tech" the apple-tards managed to patent was... wait for it....HYPERLINKS! WHAT? They got a patent for hyperlinks? When will the REAL inventor get due credit? Are you trying to tell us that YOU invented the hyperlink, Apple? Folks, this whole intellectual property thing has approached a point of madness. I can see two spoiled kids fighting over a toy iterating for eternity ... while throat_not__too_terribly_sore(): kid_1.say( "i had it first") kid_2.say("No, I had it first") ...meanwhile WE suffer the consequences. And the consequences are two fold. 1. Increased cost 2. decreased advancement of tech But is Apple the REAL problem here? No, the *real* problem is GREED. We have corporations who's sole purpose is to make a very few people rich off the backs of the slaves. In this environment, the only way you can prosper is through GREED. I don't blame the corporations, no, i blame the system. Greed is the disease, and Cooperatives[2] are the cure. [1] http://www.nytimes.com/2011/12/20/technology/apple-wins-partial-victory-on-patent-claim-over-android-features.html [2] http://en.wikipedia.org/wiki/Cooperative From ian.g.kelly at gmail.com Mon Dec 19 22:26:30 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 19 Dec 2011 20:26:30 -0700 Subject: The ever-expanding bubble of GREED In-Reply-To: <24b782e6-6320-4f97-ad54-a0609450e4d0@x7g2000yqb.googlegroups.com> References: <24b782e6-6320-4f97-ad54-a0609450e4d0@x7g2000yqb.googlegroups.com> Message-ID: On Mon, Dec 19, 2011 at 8:15 PM, Rick Johnson wrote: > I just read an article[1] reporting that Apple has won a court case > against Andoid (and others) over a patent infringement. Before i was > able to see what all the hub-bub was about my mind started to wonder > what "advanced technology" Google has stolen from Apple. None of which has anything to do with Python. Congratulations, you're back in my message filters again. From steve+comp.lang.python at pearwood.info Mon Dec 19 22:29:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 03:29:00 GMT Subject: the slash & Windows paths References: Message-ID: <4ef000fc$0$11091$c3e8da3@news.astraweb.com> On Mon, 19 Dec 2011 15:02:50 -0700, Juan Declet-Barreto wrote: > xmlns:o="urn:schemas-microsoft-com:office:office" > xmlns:w="urn:schemas-microsoft-com:office:word" > xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" > xmlns="http://www.w3.org/TR/REC-html40"> http-equiv=Content-Type content="text/html; charset=us-ascii"> name=Generator content="Microsoft Word 14 (filtered medium)">> UG everyone! db = %d'%self.db ## TODO: meretezhetoseg def createWidgets(self): top = self.winfo_toplevel() top.rowconfigure(0,weight = 1) top.columnconfigure(0,weight = 1) self.rowconfigure(0,weight = 1) self.columnconfigure(0,weight = 1) self.QUIT = Button(self) self.QUIT["text"] = "QUIT" self.QUIT["fg"] = "red" self.QUIT["command"] = self.quit self.QUIT.pack({"side": "left"}) self.hi_there = Button(self) self.hi_there["text"] = "Hello", self.hi_there["command"] = self.say_hi self.hi_there.pack({"side": "left"}) def __init__(self, master=None): Frame.__init__(self, master) self.pack() self.createWidgets() self.db = 0 app = Application() app.master.title('UG test') app.mainloop() Thx in advance... Gabor -- Linux is like a wigwam: no Gates, no Windows and an Apache inside. From franck at ditter.org Tue Dec 20 07:56:20 2011 From: franck at ditter.org (Franck Ditter) Date: Tue, 20 Dec 2011 13:56:20 +0100 Subject: Can't get tilde character with IDLE 3.2.2 on French Mac Lion References: Message-ID: Nope, "space" followed by "Shift-Option-N" gives a greek iota... I tried other combinations, unsuccessfully. IDLE 3 (French) seems to be unusable as we use many ~ in web applications :-( Should we hope a fix soon, or leave IDLE ? Thanks, franck In article , Ned Deily wrote: > In article , > Franck Ditter wrote: > > All is in the subject. I'm starting to use Python with Idle 3.2.2 > > on MacOS-X Lion (French). I can't get "Option-N space" to provide > > the ~ char. > > I tried to go into the Keys preferences but I can't find "Option-N space" > > to modify its meaning. Its actual behavior is to merge lines of a > > paragraph. > > You are likely running into a current problem in the OS X Cocoa version > of Tcl/Tk 8.5 as included with Lion and as shipped by ActiveState. > Previously, if you tried to type composite characters, like Option N, > the Cocoa Tcl/Tk would crash. Pending a real fix, a patch was made to > Tcl/Tk 8.5 to discard composite characters rather than crash. You > should be able to get a tilde by using the post-composite keyboard > sequence: try typing "space" followed by "Shift-Option-N". > > http://sourceforge.net/tracker/index.php?func=detail&aid=2907388&group_id=12997&atid=112997 From ulrich.eckhardt at dominolaser.com Tue Dec 20 08:33:37 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Tue, 20 Dec 2011 14:33:37 +0100 Subject: check for a certain Python version Message-ID: Hi! I'd like to give my users a meaningful error message when they are using obsolete Python versions. Is there some kind of best practice or recipe for that? Specifically, the case is a bunch of unittests that use features new to Python 2.7. Thank you! Uli From saqib.ali.75 at gmail.com Tue Dec 20 08:52:30 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Tue, 20 Dec 2011 08:52:30 -0500 Subject: Need help with really elementary pexpect fragment In-Reply-To: <11996.1324359117@alphaville.dokosmarshall.org> References: <93ad02cb-68c3-44c4-ac9b-74a6e87d3f70@y7g2000vbe.googlegroups.com> <11996.1324359117@alphaville.dokosmarshall.org> Message-ID: Oops! Good call. Thank you. You pointed out my mistake. - Saqib On Tue, Dec 20, 2011 at 12:31 AM, Nick Dokos wrote: > Saqib Ali wrote: > > > > > I want to write a pexpect script that simply cd's into a directory ("~/ > > install") and then runs a command from there. It should be so easy. > > But even my cd command is failing. Can't figure out what the problem > > is. The command line prompt is "[my machine name here] % " > > > > Here is the code fragment: > > > > print "Spawning Expect" > > p = pexpect.spawn ('/bin/tcsh',) > > > > If you execute /bin/tcsh by hand, do you get a "%" prompt? > > Nick > > > print "Sending cd command" > > i = p.expect([pexpect.TIMEOUT, "%",]) > > assert i != 0, "Time-Out.... exiting" > > p.sendline("cd ~/install") > > > > Here is the output: > > > > Spawning Expect > > Sending cd command > > Time-Out.... exiting > > > > > > How could I be screwing something so simple up?? > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From arnodel at gmail.com Tue Dec 20 09:02:31 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Tue, 20 Dec 2011 14:02:31 +0000 Subject: Performing a number of substitutions on a unicode string Message-ID: Hi all, I've got to escape some unicode text according to the following map: escape_map = { u'\n': u'\\n', u'\t': u'\\t', u'\r': u'\\r', u'\f': u'\\f', u'\\': u'\\\\' } The simplest solution is to use str.replace: def escape_text(text): return text.replace('\\', '\\\\').replace('\n', '\\n').replace('\t', '\\t').replace('\r', '\\r').replace('\f', '\\f') But it creates 4 intermediate strings, which is quite inefficient (I've got 10s of MB's worth of unicode strings to escape) I can think of another way using regular expressions: escape_ptn = re.compile(r"[\n\t\f\r\\]") # escape_map is defined above def escape_match(m, map=escape_map): return map[m.group(0)] def escape_text(text, sub=escape_match): return escape_ptn.sub(sub, text) Is there a better way? Thanks, -- Arnaud From ulrich.eckhardt at dominolaser.com Tue Dec 20 09:15:06 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Tue, 20 Dec 2011 15:15:06 +0100 Subject: nesting context managers Message-ID: Hi! Let us assume I had a class HTTPClient that has a socket for HTTP and a logfile for filing logs. I want to make this HTTPClient a context manager, so that I can write with HTTPClient(url) as client: pass and reliably have both the socket and the logfile closed. The easy way is wrong def __enter__(self): with self._mysock, self._myfile: return self because it closes the file and socket as part of the return statement. Of course I can go the hard way, code everything myself, but I wonder if there isn't some useful tool that would help me achieve this (common?) task. Thanks! Uli From roy at panix.com Tue Dec 20 09:21:41 2011 From: roy at panix.com (Roy Smith) Date: Tue, 20 Dec 2011 09:21:41 -0500 Subject: check for a certain Python version References: Message-ID: In article , Ulrich Eckhardt wrote: > Hi! > > I'd like to give my users a meaningful error message when they are using > obsolete Python versions. Is there some kind of best practice or recipe > for that? Specifically, the case is a bunch of unittests that use > features new to Python 2.7. >>> sys.version_info (2, 6, 1, 'final', 0) major, minor, _, _, _ = sys.version_info self.assertGreaterEqual(minor, 7, "You, dude, fix your python") From rantingrickjohnson at gmail.com Tue Dec 20 09:37:58 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 20 Dec 2011 06:37:58 -0800 (PST) Subject: Set initial size in TKinter References: Message-ID: <1deb41c4-d511-4a54-a34d-90b0566d0f9e@q9g2000yqe.googlegroups.com> On Dec 20, 5:09?am, Gabor Urban wrote: > Hi, > > I am quite newbie with Tkinter and I could not find the way to set the > size of the application. Probably due to this haphazard coding style; why would you name an object "Application" that is an instance of Tkinter.Frame? That is just going to confuse people, including yourself. Choosing proper names is very important. Would you call a cat a hat? Or a dog a frog? Also, why did you post so much non-applicable code? Here is what you should have posted... from Tkinter import * # app = Tk() app.title('This is an App, not a frame or a button') app.config(width=50, height=400) #frame1 = Frame() # Proper Naming Example. #frame2 = Frame() # Proper Naming Example. app.mainloop() YWATF From rantingrickjohnson at gmail.com Tue Dec 20 09:45:47 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 20 Dec 2011 06:45:47 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <0b479489-8dab-488c-81e8-f15ae2804234@z17g2000vbe.googlegroups.com> On Dec 20, 2:14?am, Stefan Behnel wrote: > For teaching, I think it's better to come around with something simpler > than a full-blown IDE, so that you can show off interactive development, > help() and other introspection features. IMHO much better than hiding all > that behind an IDE, That is a damn good point Stefan. Too many noobs step passed the introspection features of Python without knowing what happened. help, dir, type, isinstance, id... to name a few. Heck for most things, considering you have at least basic programming experience, the help function is all you need to learn the language. From python.list at tim.thechases.com Tue Dec 20 09:54:16 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 20 Dec 2011 08:54:16 -0600 Subject: Performing a number of substitutions on a unicode string In-Reply-To: References: Message-ID: <4EF0A198.6020309@tim.thechases.com> On 12/20/11 08:02, Arnaud Delobelle wrote: > Hi all, > > I've got to escape some unicode text according to the following map: > > escape_map = { > u'\n': u'\\n', > u'\t': u'\\t', > u'\r': u'\\r', > u'\f': u'\\f', > u'\\': u'\\\\' > } > > The simplest solution is to use str.replace: > > def escape_text(text): > return text.replace('\\', '\\\\').replace('\n', > '\\n').replace('\t', '\\t').replace('\r', '\\r').replace('\f', '\\f') > > But it creates 4 intermediate strings, which is quite inefficient > (I've got 10s of MB's worth of unicode strings to escape) You might try def escape_text(text): return text.encode("string_escape") as documented at[1] -tkc [1] http://docs.python.org/release/2.5.2/lib/standard-encodings.html#standard-encodings From arnodel at gmail.com Tue Dec 20 10:04:35 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Tue, 20 Dec 2011 15:04:35 +0000 Subject: Performing a number of substitutions on a unicode string In-Reply-To: <4EF0A198.6020309@tim.thechases.com> References: <4EF0A198.6020309@tim.thechases.com> Message-ID: On 20 December 2011 14:54, Tim Chase wrote: > On 12/20/11 08:02, Arnaud Delobelle wrote: >> >> Hi all, >> >> I've got to escape some unicode text according to the following map: >> >> escape_map = { >> ? ? u'\n': u'\\n', >> ? ? u'\t': u'\\t', >> ? ? u'\r': u'\\r', >> ? ? u'\f': u'\\f', >> ? ? u'\\': u'\\\\' >> } >> >> The simplest solution is to use str.replace: >> >> def escape_text(text): >> ? ? return text.replace('\\', '\\\\').replace('\n', >> '\\n').replace('\t', '\\t').replace('\r', '\\r').replace('\f', '\\f') >> >> But it creates 4 intermediate strings, which is quite inefficient >> (I've got 10s of MB's worth of unicode strings to escape) > > > You might try > > ?def escape_text(text): > ? ?return text.encode("string_escape") > I don't think this kind of approach would work as I'm not decoding unicode: both the input and the output of the "escape_text" function are unicode. -- Arnaud From eric.brunel at pragmadev.nospam.com Tue Dec 20 10:20:08 2011 From: eric.brunel at pragmadev.nospam.com (Eric Brunel) Date: Tue, 20 Dec 2011 16:20:08 +0100 Subject: Set initial size in TKinter References: Message-ID: In article , Gabor Urban wrote: > Hi, > > I am quite newbie with Tkinter and I could not find the way to set the > size of the application. (I could find the method to make it > resizeable, though :-)) ) Any ideas, suggestions or links to > references are wellcome. Usually, the best way is to use the geometry method on instances of Tk or Toplevel. For example, if you have a variable named root which is the instance of Tk, you can do: root.geometry('500x400') This will make the window 500 pixels wide and 400 pixels high. > Here is my code: > > from Tkinter import * > > class Application(Frame): > def say_hi(self): > self.db += 1 > print 'hi there, -->> UG everyone! db = %d'%self.db > > ## TODO: meretezhetoseg > def createWidgets(self): > top = self.winfo_toplevel() > top.rowconfigure(0,weight = 1) > top.columnconfigure(0,weight = 1) > self.rowconfigure(0,weight = 1) > self.columnconfigure(0,weight = 1) > self.QUIT = Button(self) > self.QUIT["text"] = "QUIT" > self.QUIT["fg"] = "red" > self.QUIT["command"] = self.quit > > self.QUIT.pack({"side": "left"}) > > self.hi_there = Button(self) > self.hi_there["text"] = "Hello", > self.hi_there["command"] = self.say_hi > > self.hi_there.pack({"side": "left"}) > > def __init__(self, master=None): > Frame.__init__(self, master) > self.pack() > self.createWidgets() > self.db = 0 > > app = Application() > app.master.title('UG test') > app.mainloop() Where did you find an example code looking like this? This looks like veeeeeery old conventions for Tkinter programs? For example, there's no need at all to do: self.QUIT = Button(self) self.QUIT["text"] = "QUIT" self.QUIT["fg"] = "red" self.QUIT["command"] = self.quit This can be done in a single line: self.QUIT = Button(self, text='QUIT', fg='red', command=self.quit) The same goes for self.QUIT.pack({"side": "left"}). Nowadays, this is always written self.QUIT.pack(side="left"). And you should avoid creating only an instance of Frame. This actually creates a window, but it's a side-effect. Windows are created by instantiating Tk for the main one, and Toplevel for all others. Having only a Frame will cause problems later, for example if you want to add a menu to the window: You can do so on instances of Tk or Toplevel, but not on frames? HTH - Eric - From rantingrickjohnson at gmail.com Tue Dec 20 10:30:50 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 20 Dec 2011 07:30:50 -0800 (PST) Subject: Set initial size in TKinter References: Message-ID: <8fd4083e-ae80-40bf-824e-cc38aa5cddde@i8g2000vbh.googlegroups.com> On Dec 20, 9:20?am, Eric Brunel wrote: > Where did you find an example code looking like this? This looks like > veeeeeery old conventions for Tkinter programs? >[...] > And you should avoid creating only an instance of Frame. This actually > creates a window, but it's a side-effect. Two major problems with Tkinter exposed here. 1. The tutorials are VERY old and need to be updated. There needs to be one, and preferably only one site for Tkinter tutorials. One that is through and complete! 2. Tkinter is designed to "auto-magically" create a root window if the user does not do so explicitly. This is a major flaw in the design of Tkinter and needs to be removed yesterday! From __peter__ at web.de Tue Dec 20 10:35:21 2011 From: __peter__ at web.de (Peter Otten) Date: Tue, 20 Dec 2011 16:35:21 +0100 Subject: Performing a number of substitutions on a unicode string References: Message-ID: Arnaud Delobelle wrote: > I've got to escape some unicode text according to the following map: > > escape_map = { > u'\n': u'\\n', > u'\t': u'\\t', > u'\r': u'\\r', > u'\f': u'\\f', > u'\\': u'\\\\' > } > > The simplest solution is to use str.replace: > > def escape_text(text): > return text.replace('\\', '\\\\').replace('\n', > '\\n').replace('\t', '\\t').replace('\r', '\\r').replace('\f', '\\f') > > But it creates 4 intermediate strings, which is quite inefficient > (I've got 10s of MB's worth of unicode strings to escape) > > I can think of another way using regular expressions: > > escape_ptn = re.compile(r"[\n\t\f\r\\]") > > # escape_map is defined above > def escape_match(m, map=escape_map): > return map[m.group(0)] > > def escape_text(text, sub=escape_match): > return escape_ptn.sub(sub, text) > > Is there a better way? >>> escape_map = { ... u'\n': u'\\n', ... u'\t': u'\\t', ... u'\r': u'\\r', ... u'\f': u'\\f', ... u'\\': u'\\\\' ... } >>> escape_map = dict((ord(k), v) for k, v in escape_map.items()) >>> print u"the quick\n brown\tfox jumps\\over\\the\\lazy\\dog".translate(escape_map) the quick\n brown\tfox jumps\\over\\the\\lazy\\dog From rosuav at gmail.com Tue Dec 20 10:51:42 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Dec 2011 02:51:42 +1100 Subject: Python education survey In-Reply-To: <0b479489-8dab-488c-81e8-f15ae2804234@z17g2000vbe.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <0b479489-8dab-488c-81e8-f15ae2804234@z17g2000vbe.googlegroups.com> Message-ID: On Wed, Dec 21, 2011 at 1:45 AM, Rick Johnson wrote: > Heck for most things, > considering you have at least basic programming experience, the help > function is all you need to learn the language. I know I shouldn't feed the troll, but this is more general. You cannot learn the _language_ from help() - it's more like a dictionary. You still need something that explains the grammar, and you also need to have some ideas of what names to look up. The help() function in Python is actually not as helpful as could be desired, in many cases. I think "help()" on its own is probably a good thing, but... I didn't know about it until I tried it right while typing up this email. (That's probably my fault more than Python's, though.) help(functionname) is usually fairly helpful - but most of that is just from the function's docstring. Unfortunately help(classname) and help(modulename) are way too spammy to be much use. Look for instance at help(decimal.Decimal) - quite a few dunder methods are listed, uselessly. What does "__deepcopy__(self, memo)" do? What about "__ge__(self, other, context=None)"? Unless you happen to know that __ge__ is the greater-than-or-equal function, it's not going to do you much good to see the method listed. They're just spam, forcing you to read through more screed to figure out what's going on. Same, and even worse, with modules - help(decimal) is pages and pages of text, detailing the exceptions supported etc. Unlike the class's, though, the module's docstring is actually quite helpful. (It also hints at what the context=None arguments are on a lot of the methods.) Please note that this is not a criticism of the decimal module specifically; and the help() function can't really be written any other way, short of having it emit ONLY docstrings, and then demand that module authors maintain perfect class and module docstrings (like that's gonna happen). Hmm. Another feature I didn't know about help() - instead of passing it an object, you can pass it a string. This gets around the fact that "help(class)" doesn't work - "help('class')" does. I think I should reword this from "possible feature request" to "does this already exist", because it probably does... so... Is there a less spammy documentation utility, in-built into Python? Chris Angelico From wanderer at dialup4less.com Tue Dec 20 11:14:42 2011 From: wanderer at dialup4less.com (Wanderer) Date: Tue, 20 Dec 2011 08:14:42 -0800 (PST) Subject: IPython 0.12 is out! References: <1331f71b-b8c6-475c-af22-13c79cfa0471@o9g2000yqa.googlegroups.com> <56bc8947-781e-41b6-a4aa-cdb1acd929ab@n6g2000vbz.googlegroups.com> Message-ID: <0a968f0a-3940-4d53-b151-7b43f4bc067d@p9g2000vbb.googlegroups.com> On Dec 19, 11:00?pm, alex23 wrote: > On Dec 20, 4:07?am, Wanderer wrote: > > > The windows installer didn't work but installing from the tar file > > did. But installing from the tar file doesn't install Ipython in the > > site-packages directory. It installs it wherever you untar the tar > > file. I don't remember ever having to deal with this before. Most > > things just install in the site-packages directory without me having > > to do anything special. > > You read the installation instructions and did a 'python setup.py > install' as it states, yes? > > Installed that way for Python 2.7.2 under Win64 with no issues > whatsoever. No I scrolled down to the Windows directions and used setuptools,'python setupegg.py install' It works but it is installed in 'C:Python27\Lib\site-packages\ipython-0.12.tar\dist\ipython-0.12' which is weird. I used 7zip to unzip and untar it maybe that is the issue. I'm using winXP. I should be getting my system upgraded to Win7 any day now, so I'm going to leave it where it is for now. Thanks From rami.chowdhury at gmail.com Tue Dec 20 11:25:58 2011 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Tue, 20 Dec 2011 16:25:58 +0000 Subject: nesting context managers In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 14:15, Ulrich Eckhardt wrote: > Hi! > > Let us assume I had a class HTTPClient that has a socket for HTTP and a > logfile for filing logs. I want to make this HTTPClient a context manager, > so that I can write > > ?with HTTPClient(url) as client: > ? ? ?pass > > and reliably have both the socket and the logfile closed. The easy way is > wrong > > ?def __enter__(self): > ? ? ?with self._mysock, self._myfile: > ? ? ? ? ?return self > It seems like some of the functions in the contextlib module might help? You could try and reorganize your code so that you can use the @contextmanager decorator, for instance? That having been said, it doesn't seem that difficult to me to code your own simple __exit__ method if you're already coding up __enter__ ? HTH, Rami -- Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 From ulrich.eckhardt at dominolaser.com Tue Dec 20 11:56:54 2011 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Tue, 20 Dec 2011 17:56:54 +0100 Subject: nesting context managers In-Reply-To: References: Message-ID: Am 20.12.2011 15:15, schrieb Ulrich Eckhardt: > Let us assume I had a class HTTPClient that has a socket for HTTP and a > logfile for filing logs. I want to make this HTTPClient a context > manager, so that I can write > > with HTTPClient(url) as client: > pass Actually, I overestimated the task: class HTTPClient(url): def __enter__(self): return self def __exit__(self, ctx_type, ctx_val, ctx_tb): self.close() def close(self): self._myfile.close() self._mysocket.close() I'll simply ignore the fact that closing a file can fail if you can't flush buffers. Now, my error was that I have to somehow invoke the file's and socket's enter function in my client's enter function. The default for all files and sockets is that they are already open, they are not opened in the enter function! That way, the client remains usable outside a with context but gives me the desired guarantees inside one. For the case of on-demand allocated stuff, I could write the enter function like this: def __enter__(self): # allocate resources f = ... # open file s = ... # connect socket # attach after completion self._myfile = f self._mysocket = s To be extra safe or in more complicated scenarios, I could wrap this in a try-except and explicitly close those that were already created, but normally I'd expect the garbage collector to do that for me ... or am I then implicitly assuming a specific implementation? Uli From arnodel at gmail.com Tue Dec 20 12:04:48 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Tue, 20 Dec 2011 17:04:48 +0000 Subject: Performing a number of substitutions on a unicode string In-Reply-To: References: Message-ID: On 20 December 2011 15:35, Peter Otten <__peter__ at web.de> wrote: >>>> escape_map = { > ... ? ? u'\n': u'\\n', > ... ? ? u'\t': u'\\t', > ... ? ? u'\r': u'\\r', > ... ? ? u'\f': u'\\f', > ... ? ? u'\\': u'\\\\' > ... } >>>> escape_map = dict((ord(k), v) for k, v in escape_map.items()) >>>> print u"the quick\n brown\tfox > jumps\\over\\the\\lazy\\dog".translate(escape_map) > the quick\n brown\tfox jumps\\over\\the\\lazy\\dog Thanks. I think I've got a blind spot about unicode.translate. Actually, I know why: it's not properly documented where I look [1]. -- Arnaud [1] http://docs.python.org/library/stdtypes.html#str.translate From joshua.landau.ws at gmail.com Tue Dec 20 12:05:05 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Dec 2011 17:05:05 +0000 Subject: Grammar for classes In-Reply-To: References: Message-ID: On 20 December 2011 10:55, Robert Kern wrote: > On 12/20/11 1:34 AM, Joshua Landau wrote: > >> In reading thorough the syntax defined in the reference >> > class-definitions >> >, >> >> the class statement has surprised me. >> >> It says that the inheritance part of the class can accept comprehensions. >> What >> does this mean? >> I've tried: >> "class A(x for x in ()): pass" >> but this doesn't need the extra clause as "x for x in ()" is an >> expression, and >> thus this evaluates: >> "class A(x for x in (),): pass" >> although again it won't be a valid class anytime soon. >> >> So what is this clause for? >> > > I suspect that it's harder to make a grammar rule that allows every kind > of expression except for generator expressions than it is just to reuse the > "testlist" rule and let the runtime reject the generator object when it > goes to construct the class. > The thing is, as far as I can tell, the grammar would be the same without the added clause. That is because it's a valid grammar anyway. The only exception I can think of is if generators were treated exceptionally. Thus, while I agree it makes complete sense to make it a runtime error, the clause in the specification seems to be fluff. Also - what is this "testlist" rule thee speaks of? I think I'm just going to ignore it for now, as long as it doesn't mean I ignore valid code. Thanks Ian and Robert for the input. Should I file a documentation bug report? > -- > 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From robert.kern at gmail.com Tue Dec 20 12:20:12 2011 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 20 Dec 2011 17:20:12 +0000 Subject: Grammar for classes In-Reply-To: References: Message-ID: On 12/20/11 5:05 PM, Joshua Landau wrote: > On 20 December 2011 10:55, Robert Kern > wrote: > > On 12/20/11 1:34 AM, Joshua Landau wrote: > > In reading thorough the syntax defined in the reference > >, > > the class statement has surprised me. > > It says that the inheritance part of the class can accept > comprehensions. What > does this mean? > I've tried: > "class A(x for x in ()): pass" > but this doesn't need the extra clause as "x for x in ()" is an > expression, and > thus this evaluates: > "class A(x for x in (),): pass" > although again it won't be a valid class anytime soon. > > So what is this clause for? > > > I suspect that it's harder to make a grammar rule that allows every kind of > expression except for generator expressions than it is just to reuse the > "testlist" rule and let the runtime reject the generator object when it goes > to construct the class. > > > The thing is, as far as I can tell, the grammar would be the same without the > added clause. That is because it's a valid grammar anyway. The only exception I > can think of is if generators were treated exceptionally. Thus, while I agree it > makes complete sense to make it a runtime error, the clause in the specification > seems to be fluff. > > Also - what is this "testlist" rule thee speaks of? Sorry, I was looking at the actual 2.7 Grammar file. "testlist" has been renamed to "arglist" in the 3.2 version of the Grammar, but the substantive point I made remains (to the extent that it is relevant at all). That said, the documentation for both versions differs from a simple restatement of the Grammar rules, so I'm not sure exactly why the documentation is written the way it is. -- 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 From invalid at invalid.invalid Tue Dec 20 12:40:22 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 20 Dec 2011 17:40:22 +0000 (UTC) Subject: [OT] Quick intro to C++ for a Python and C user? Message-ID: Would anybody care to recommend online C++ resources for a long time C and Python user? (I'm also familiar with Smalltalk, Scheme, FORTRAN, bash, Javascript, and a variety of assembly languages.) I have a C++ library to which I need to add a couple minor wrappers/extensions. I've already done the same for the C version of the library. Writing test suites for C libraries using Python/ctypes is pretty cool. :) After googling a bit, I found several recommendations for the book "Thinking in C++" by Bruce Eckel. I've skimmed through it, and it's _way_ too long-winded. It takes him 200+ pages before he gets to data encapsulation and 600+ pages before he introduces inheritence. The writing is also a bit too condesending for my taste: You literally take the form of the existing class and add code to it, without modifying the existing class. This magical act is called _inheritance_, and most of the work is done by the compiler. Magical act? Seriously? -- Grant Edwards grant.b.edwards Yow! Are you the at self-frying president? gmail.com From rami.chowdhury at gmail.com Tue Dec 20 12:46:09 2011 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Tue, 20 Dec 2011 17:46:09 +0000 Subject: nesting context managers In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 16:56, Ulrich Eckhardt wrote: > To be extra safe or in more complicated scenarios, I could wrap this in a > try-except and explicitly close those that were already created, but > normally I'd expect the garbage collector to do that for me ... or am I then > implicitly assuming a specific implementation? I'm no expert but I believe the basic garbage collection behavior is part of the language, and it's only details of breaking cycles that are specific to CPython -- can anyone correct me if I'm wrong? -- Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 From ethan at stoneleaf.us Tue Dec 20 13:02:35 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 20 Dec 2011 10:02:35 -0800 Subject: nesting context managers In-Reply-To: References: Message-ID: <4EF0CDBB.1080904@stoneleaf.us> Rami Chowdhury wrote: > On Tue, Dec 20, 2011 at 16:56, Ulrich Eckhardt > wrote: >> To be extra safe or in more complicated scenarios, I could wrap this in a >> try-except and explicitly close those that were already created, but >> normally I'd expect the garbage collector to do that for me ... or am I then >> implicitly assuming a specific implementation? > > I'm no expert but I believe the basic garbage collection behavior is > part of the language, and it's only details of breaking cycles that > are specific to CPython -- can anyone correct me if I'm wrong? Garbage collection is part of the language, but how, and when, is implementation specific. Resource management (beyond basic memory allocation) should be handled directly. Python 3 encourages this by complaining if there were still open files when it shuts down. ~Ethan~ From miki.tebeka at gmail.com Tue Dec 20 13:07:31 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 20 Dec 2011 10:07:31 -0800 (PST) Subject: Python education survey In-Reply-To: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <21978406.608.1324404451230.JavaMail.geo-discussion-forums@yqgn9> I've tried several things. So far vim (with line numbers) to show the code and then ipython to run it works great. Another option I tried once was Aptana, since most people in my company know eclipse this was good for them. It has most (all?) of the features you mentioned above. From Admin at no.mail.to Tue Dec 20 13:19:14 2011 From: Admin at no.mail.to (Admin) Date: Tue, 20 Dec 2011 19:19:14 +0100 Subject: how to run python-script from the python promt? [absolute newbie] In-Reply-To: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: <4ef0d1a2$0$13515$4d5ecec7@news.xsusenet.com> Am 18.12.2011 12:00, schrieb nukeymusic: > How can I load a python-script after starting python in the > interactive mode? > I tried with >>>> load 'myscript.py' >>>> myscript.py >>>> myscript > > but none of these works, so the only way I could work further until > now was copy/paste line per line of my python-script to the > interactive mode prompt > I do know how to run the script non-interactively, but what I want to > do is adding lines to the code I have written thus far in interactive > mode. > > thanks in advance > nukey just go to the folder there there script is located and then do "scriptname.py" From rzantow at gmail.com Tue Dec 20 13:24:23 2011 From: rzantow at gmail.com (DZantow) Date: Tue, 20 Dec 2011 10:24:23 -0800 (PST) Subject: Test plans and "refined test plans" Message-ID: First, a disclaimer. I know this group is not the appropriate venue for the question I have. However, I am hoping some of you with experience can point me in the right direction, either by addressing the question or by suggesting a more appropriate group. The question is: A contract we are bidding on calls for, among other things, 2 deliverables: an "initial test plan" and a "refined test plan". We are not allowed to speak directly to the agency at this point, so it isn't clear how to accommodate this request. Have any of you had experience with similar requirements? If so, how did you differentiate the two plans? Thank you for any guidance. rzed From kimma77 at gmail.com Tue Dec 20 13:31:56 2011 From: kimma77 at gmail.com (kimma) Date: Tue, 20 Dec 2011 19:31:56 +0100 Subject: Learning Python 2.4 Message-ID: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Hi there, I am about to learn python with "how to think like a computer scientist". This book is just available for python 2.4. Does it matter for programming? Greetz From neilc at norwich.edu Tue Dec 20 13:36:53 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 20 Dec 2011 18:36:53 GMT Subject: [OT] Quick intro to C++ for a Python and C user? References: Message-ID: <9lc2u5FilrU1@mid.individual.net> On 2011-12-20, Grant Edwards wrote: > Would anybody care to recommend online C++ resources for a long > time C and Python user? (I'm also familiar with Smalltalk, > Scheme, FORTRAN, bash, Javascript, and a variety of assembly > languages.) The best book I know of to get you writing useful C++ quickly is Accelerated C++ by Koenig/Moo. It's not free online, though. It starts with an excellent introduction to using the STL and works it's way slowly down the abstraction ladder to using pointers and inheritance last of all. Iterators turn out to be an excellent starting point for learning pointers, though since you already know C that won't do you as much good. -- Neil Cerutti From ian.g.kelly at gmail.com Tue Dec 20 13:46:40 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Dec 2011 11:46:40 -0700 Subject: nesting context managers In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 9:56 AM, Ulrich Eckhardt wrote: > Am 20.12.2011 15:15, schrieb Ulrich Eckhardt: > >> Let us assume I had a class HTTPClient that has a socket for HTTP and a >> logfile for filing logs. I want to make this HTTPClient a context >> manager, so that I can write >> >> with HTTPClient(url) as client: >> ? ?pass > > > Actually, I overestimated the task: > > ?class HTTPClient(url): > ? ? ?def __enter__(self): > ? ? ? ? ?return self > ? ? ?def __exit__(self, ctx_type, ctx_val, ctx_tb): > ? ? ? ? ?self.close() > ? ? ?def close(self): > ? ? ? ? ?self._myfile.close() > ? ? ? ? ?self._mysocket.close() > > I'll simply ignore the fact that closing a file can fail if you can't flush > buffers. Now, my error was that I have to somehow invoke the file's and > socket's enter function in my client's enter function. The default for all > files and sockets is that they are already open, they are not opened in the > enter function! That way, the client remains usable outside a with context > but gives me the desired guarantees inside one. > > For the case of on-demand allocated stuff, I could write the enter function > like this: > > ?def __enter__(self): > ? ? ?# allocate resources > ? ? ?f = ... # open file > ? ? ?s = ... # connect socket > ? ? ?# attach after completion > ? ? ?self._myfile = f > ? ? ?self._mysocket = s > > To be extra safe or in more complicated scenarios, I could wrap this in a > try-except and explicitly close those that were already created, but > normally I'd expect the garbage collector to do that for me ... or am I then > implicitly assuming a specific implementation? For full generality, copy the code for the contextlib.nested function in Python 2.7 or 3.1 and use that. Don't use contextlib.nested itself, though, because it's deprecated and does not exist in Python 3.2. Cheers, Ian From ian.g.kelly at gmail.com Tue Dec 20 13:55:59 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Dec 2011 11:55:59 -0700 Subject: nesting context managers In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 11:46 AM, Ian Kelly wrote: > On Tue, Dec 20, 2011 at 9:56 AM, Ulrich Eckhardt > wrote: >> Am 20.12.2011 15:15, schrieb Ulrich Eckhardt: >> >>> Let us assume I had a class HTTPClient that has a socket for HTTP and a >>> logfile for filing logs. I want to make this HTTPClient a context >>> manager, so that I can write >>> >>> with HTTPClient(url) as client: >>> ? ?pass >> >> >> Actually, I overestimated the task: >> >> ?class HTTPClient(url): >> ? ? ?def __enter__(self): >> ? ? ? ? ?return self >> ? ? ?def __exit__(self, ctx_type, ctx_val, ctx_tb): >> ? ? ? ? ?self.close() >> ? ? ?def close(self): >> ? ? ? ? ?self._myfile.close() >> ? ? ? ? ?self._mysocket.close() >> >> I'll simply ignore the fact that closing a file can fail if you can't flush >> buffers. Now, my error was that I have to somehow invoke the file's and >> socket's enter function in my client's enter function. The default for all >> files and sockets is that they are already open, they are not opened in the >> enter function! That way, the client remains usable outside a with context >> but gives me the desired guarantees inside one. >> >> For the case of on-demand allocated stuff, I could write the enter function >> like this: >> >> ?def __enter__(self): >> ? ? ?# allocate resources >> ? ? ?f = ... # open file >> ? ? ?s = ... # connect socket >> ? ? ?# attach after completion >> ? ? ?self._myfile = f >> ? ? ?self._mysocket = s >> >> To be extra safe or in more complicated scenarios, I could wrap this in a >> try-except and explicitly close those that were already created, but >> normally I'd expect the garbage collector to do that for me ... or am I then >> implicitly assuming a specific implementation? > > For full generality, copy the code for the contextlib.nested function > in Python 2.7 or 3.1 and use that. ?Don't use contextlib.nested > itself, though, because it's deprecated and does not exist in Python > 3.2. Also note that in versions that do support the nested with syntax, you can do something like: class HTTPClient(url): @contextlib.contextmanager def _contextmanager(file, socket): with file, socket: yield def __enter__(self): self._cm = self._contextmanager(self._myfile, self._mysocket) return self._cm.__enter__() def __exit__(self, exc_type, exc_value, traceback): try: return self._cm.__exit__(exc_type, exc_value, traceback) finally: del self._cm Cheers, Ian From y.turgut at gmail.com Tue Dec 20 14:17:15 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Tue, 20 Dec 2011 11:17:15 -0800 (PST) Subject: Text Processing Message-ID: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> Hi all, I have a text file containing such data ; A B C ------------------------------------------------------- -2.0100e-01 8.000e-02 8.000e-05 -2.0000e-01 0.000e+00 4.800e-04 -1.9900e-01 4.000e-02 1.600e-04 But I only need Section B, and I need to change the notation to ; 8.000e-02 = 0.08 0.000e+00 = 0.00 4.000e-02 = 0.04 Text file is approximately 10MB in size. I looked around to see if there is a quick and dirty workaround but there are lots of modules, lots of options.. I am confused. Which module is most suitable for this task ? From no.email at nospam.invalid Tue Dec 20 14:43:09 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 20 Dec 2011 11:43:09 -0800 Subject: [OT] Quick intro to C++ for a Python and C user? References: Message-ID: <7xvcpb9ghe.fsf@ruckus.brouhaha.com> Grant Edwards writes: > Would anybody care to recommend online C++ resources for a long time C > and Python user? (I'm also familiar with Smalltalk, Scheme, FORTRAN, > bash, Javascript, and a variety of assembly languages.) Stroustrup's web site has lots of good stuff on it. I also like his C++ book "The C++ Programming Language" in preference to the many me-too books that are out there. He also has another C++ book that I haven't looked at but which sounded interesting. C++ is quite a bit different from those other languages you mention in that contemporary C++ style revolves a lot more around a reasonably serious static type system with typed generics. By comparison, C and Fortran have minimal static type systems and Smalltalk, Python, etc. have none at all. From nathan.alexander.rice at gmail.com Tue Dec 20 14:45:06 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Tue, 20 Dec 2011 14:45:06 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. Message-ID: Elementwise provides a proxy object for iterables which supports chained method calls, as well as elementwise expressions and some built-in functions. Example: class ExampleList(ElementwiseProxyMixin, list): def __new__(cls, iterable): return list.__new__(cls, iterable) foo = ExampleList([1, 2, 3, 4]) # You could also do: efoo = ElementwiseProxy(foo) efoo = foo.each assert list(efoo.bit_length()) == [1, 2, 2, 3] print "bit length: ", list(efoo.bit_length()) assert list(efoo + 1) == [2, 3, 4, 5] print "with addition of 1: ", list(efoo + 1) assert list(efoo * 2) == [2, 4, 6, 8] print "with multiplication by 2: ", list(efoo * 2) assert list(efoo == 2) == [False, True, False, False] print "testing equality: ", efoo == 2 assert list((efoo + 1) * 2 + 3) == [7, 9, 11, 13] print "chaining addition and multiplication: ", (efoo + 1) * 2 + 3 Each ElementwiseProxy also has a "parent" attribute so you can backtrack in the chain as needed rather than store each intermediate value, if you think you might need them. There are still some issues with proper support of things like bool() and int(), which refuse to return things that are not of the correct type. Get it: PyPi: http://pypi.python.org/pypi/elementwise/0.111220 GitHub: https://github.com/nathan-rice/Elementwise This was developed as a proof of concept for expanding the role of element-wise syntax in python, and to that end I welcome comments. Nathan Rice From ian.g.kelly at gmail.com Tue Dec 20 14:47:29 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Dec 2011 12:47:29 -0700 Subject: Learning Python 2.4 In-Reply-To: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Message-ID: On Tue, Dec 20, 2011 at 11:31 AM, kimma wrote: > Hi there, > > I am about to learn python with "how to think like a computer scientist". > This book is just available for python 2.4. Does it matter for programming? > > Greetz Python 2.4 is pretty old at this point. A lot has been added to the language since then, and Python 3 introduced a number of large, backward-incompatible changes. I don't have any specific book recommendations, but you should look for a book written for Python 3 if possible, since that is the future of Python. If not, then look for something that at least targets Python 2.6, which I would expect to include sections about coding with an eye toward Python 3 compatibility. Cheers, Ian From steve+comp.lang.python at pearwood.info Tue Dec 20 14:53:57 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 19:53:57 GMT Subject: check for a certain Python version References: Message-ID: <4ef0e7d5$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 14:33:37 +0100, Ulrich Eckhardt wrote: > Hi! > > I'd like to give my users a meaningful error message when they are using > obsolete Python versions. Is there some kind of best practice or recipe > for that? Specifically, the case is a bunch of unittests that use > features new to Python 2.7. if sys.version < "2.7": ... Or test for features and either provide your own, or disable functionality not available: class MyTest: try: @unittest.FancyFeature def testFancy(self): self.assertFancy(self, ...) except AttributeError: pass -- Steven From d at davea.name Tue Dec 20 14:57:15 2011 From: d at davea.name (Dave Angel) Date: Tue, 20 Dec 2011 14:57:15 -0500 Subject: Text Processing In-Reply-To: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> Message-ID: <4EF0E89B.4050907@davea.name> On 12/20/2011 02:17 PM, Yigit Turgut wrote: > Hi all, > > I have a text file containing such data ; > > A B C > ------------------------------------------------------- > -2.0100e-01 8.000e-02 8.000e-05 > -2.0000e-01 0.000e+00 4.800e-04 > -1.9900e-01 4.000e-02 1.600e-04 > > But I only need Section B, and I need to change the notation to ; > > 8.000e-02 = 0.08 > 0.000e+00 = 0.00 > 4.000e-02 = 0.04 > > Text file is approximately 10MB in size. I looked around to see if > there is a quick and dirty workaround but there are lots of modules, > lots of options.. I am confused. > > Which module is most suitable for this task ? You probably don't need anything but sys (to parse the command options) and os (maybe). open the file for eachline if one of the header lines, continue separate out the part you want print it, formatted as you like Then just run the script with its stdout redirected, and you've got your new file The details depend on what your experience with Python is, and what version of Python you're running. -- DaveA From jerome at jolimont.fr Tue Dec 20 15:03:21 2011 From: jerome at jolimont.fr (=?UTF-8?B?SsOpcsO0bWU=?=) Date: Tue, 20 Dec 2011 21:03:21 +0100 Subject: Text Processing In-Reply-To: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> Message-ID: <20111220210321.77451e19@bouzin.lan> Tue, 20 Dec 2011 11:17:15 -0800 (PST) Yigit Turgut a ?crit: > Hi all, > > I have a text file containing such data ; > > A B C > ------------------------------------------------------- > -2.0100e-01 8.000e-02 8.000e-05 > -2.0000e-01 0.000e+00 4.800e-04 > -1.9900e-01 4.000e-02 1.600e-04 > > But I only need Section B, and I need to change the notation to ; > > 8.000e-02 = 0.08 > 0.000e+00 = 0.00 > 4.000e-02 = 0.04 > > Text file is approximately 10MB in size. I looked around to see if > there is a quick and dirty workaround but there are lots of modules, > lots of options.. I am confused. > > Which module is most suitable for this task ? You could try to do it yourself. You'd need to know what seperates the datas. Tabulation character ? Spaces ? Exemple : Input file ---------- A B C ------------------------------------------------------- -2.0100e-01 8.000e-02 8.000e-05 -2.0000e-01 0.000e+00 4.800e-04 -1.9900e-01 4.000e-02 1.600e-04 Python code ----------- # Open file with open('test1.plt','r') as f: b_values = [] # skip as many lines as needed line = f.readline() line = f.readline() line = f.readline() while line: #start = line.find(u"\u0009", 0) + 1 #seek Tab start = line.find(" ", 0) + 4 #seek 4 spaces #end = line.find(u"\u0009", start) end = line.find(" ", start) b_values.append(float(line[start:end].strip())) line = f.readline() print b_values It gets trickier if the amount of spaces is not constant. I would then try with regular expressions. Perhaps would regexp be more efficient in any case. -- J?r?me From steve+comp.lang.python at pearwood.info Tue Dec 20 15:21:03 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 20:21:03 GMT Subject: Learning Python 2.4 References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Message-ID: <4ef0ee2f$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 19:31:56 +0100, kimma wrote: > Hi there, > > I am about to learn python with "how to think like a computer > scientist". This book is just available for python 2.4. Does it matter > for programming? Python 2.4 is pretty old, and missing a lot of really cool features that came out in 2.5 and above. Having tried to support 2.4 through 2.7 in one code base, I can tell you that the features missing from 2.4 really hurt. But for just learning the language basics, 2.4 should be fine. Just make sure you use one of the 2.x series Pythons (that is, 2.4 up to 2.7) and NOT Python 3. Python 3 intentionally breaks some backwards compatibility in ways that will confuse a beginner to the language unless you learn from a book designed for Python 3. -- Steven From steve+comp.lang.python at pearwood.info Tue Dec 20 15:29:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 20:29:49 GMT Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: Message-ID: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 14:45:06 -0500, Nathan Rice wrote: > PyPi: http://pypi.python.org/pypi/elementwise/0.111220 Version 0.111220? What do you do, bump the version number after every keystroke? -- Steven From invalid at invalid.invalid Tue Dec 20 15:41:24 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 20 Dec 2011 20:41:24 +0000 (UTC) Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> Message-ID: On 2011-12-20, Neil Cerutti wrote: > On 2011-12-20, Grant Edwards wrote: >> Would anybody care to recommend online C++ resources for a long >> time C and Python user? (I'm also familiar with Smalltalk, >> Scheme, FORTRAN, bash, Javascript, and a variety of assembly >> languages.) > > The best book I know of to get you writing useful C++ quickly is > Accelerated C++ by Koenig/Moo. It's not free online, though. It > starts with an excellent introduction to using the STL and works > it's way slowly down the abstraction ladder to using pointers and > inheritance last of all. Oops. I should have mentioned this is for embedded systems programming so templates in general (and STL in particular) are probably off the table. > Iterators turn out to be an excellent starting point for learning > pointers, though since you already know C that won't do you as > much good. -- Grant Edwards grant.b.edwards Yow! Boy, am I glad it's at only 1971... gmail.com From benjamin.kaplan at case.edu Tue Dec 20 15:42:31 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Tue, 20 Dec 2011 15:42:31 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 20, 2011 3:32 PM, "Steven D'Aprano" < steve+comp.lang.python at pearwood.info> wrote: > > On Tue, 20 Dec 2011 14:45:06 -0500, Nathan Rice wrote: > > > PyPi: http://pypi.python.org/pypi/elementwise/0.111220 > > Version 0.111220? What do you do, bump the version number after every > keystroke? > I would just like to point out that today is 2011-12-20. That may be relevant here. > Steven > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Tue Dec 20 15:45:07 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Tue, 20 Dec 2011 15:45:07 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: If you take a moment and examine the version number, you will notice that it is a date code. In my opinion that is far more informative than an arbitrary number. I use the major version number to signify... Wait for it... Major changes :) From python at mrabarnett.plus.com Tue Dec 20 15:47:09 2011 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 20 Dec 2011 20:47:09 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EF0F44D.2050403@mrabarnett.plus.com> On 20/12/2011 20:29, Steven D'Aprano wrote: > On Tue, 20 Dec 2011 14:45:06 -0500, Nathan Rice wrote: > >> PyPi: http://pypi.python.org/pypi/elementwise/0.111220 > > Version 0.111220? What do you do, bump the version number after every > keystroke? > It looks like it's based on the date. I do something similar with the regex module, except that _my_ version number is Y2K compliant. :-) From kimma77 at gmail.com Tue Dec 20 15:48:09 2011 From: kimma77 at gmail.com (kimma) Date: Tue, 20 Dec 2011 21:48:09 +0100 Subject: how to run python-script from the python promt? [absolute newbie] In-Reply-To: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> References: <1a2351dd-03a3-4579-b0be-2a53af6c04de@p9g2000vbb.googlegroups.com> Message-ID: <4ef0f488$0$10783$4d5ecec7@news.xsusenet.com> Am 18.12.2011 12:00, schrieb nukeymusic: > How can I load a python-script after starting python in the > interactive mode? > I tried with >>>> load 'myscript.py' >>>> myscript.py >>>> myscript > > but none of these works, so the only way I could work further until > now was copy/paste line per line of my python-script to the > interactive mode prompt > I do know how to run the script non-interactively, but what I want to > do is adding lines to the code I have written thus far in interactive > mode. > > thanks in advance > nukey In console, do "python script.py" (after cd'ing into the right directory) From steve+comp.lang.python at pearwood.info Tue Dec 20 15:51:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 20:51:21 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> Message-ID: <4ef0f548$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 19 Dec 2011 19:38:52 -0800, alex23 wrote: > On Dec 20, 7:57?am, Andrew Berg wrote: >> But what about the example he gave about being logged into a customer's >> machine with only ed available? I suppose such fools would not be >> worthy of your business. > > Do you mean directly editing the source code on a production machine? > Because that's pretty much the only scenario I can come up with where > that's plausible. > > If I was doing that, _I_ wouldn't be worth doing business with. > > So you only have ssh & ed: at the very least you should be making > changes against your local copy, TESTING THEM, and then copy&paste > directly onto the remote box. Come on guys, you're treating a throw-away example *way* too seriously. If we're going to hypothesis a customer so insane to only have ed installed on their system[1], chances are they've specified some crazy rule like "You May Not Copy Our Precious, Precious Source Code Onto Another Machine Because That's Copyright Theft". Or whatever. Spend 15 minutes on The Daily WTF and you'll see war stories far more crazy than that. The point I was making is that in the real world, sometimes you don't have the luxury of using the right tool for the job, but have to make do with whatever you have. Languages shouldn't depend on advanced editor features or special keyboards -- that way leads to ColorForth and APL. [1] Possibly because ed is the standard Unix editor. http://www.gnu.org/fun/jokes/ed.msg.html -- Steven From invalid at invalid.invalid Tue Dec 20 15:51:29 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Tue, 20 Dec 2011 20:51:29 +0000 (UTC) Subject: [OT] Quick intro to C++ for a Python and C user? References: Message-ID: On 2011-12-20, Grant Edwards wrote: > Would anybody care to recommend online C++ resources for a long time C > and Python user? (I'm also familiar with Smalltalk, Scheme, FORTRAN, > bash, Javascript, and a variety of assembly languages.) > > I have a C++ library to which I need to add a couple minor > wrappers/extensions. I've already done the same for the C version of > the library. Writing test suites for C libraries using Python/ctypes > is pretty cool. :) I stumbled across an undergrad CSci lesson that showed in a couple pages how to subclass something and add a couple methods. All I needed to do was add four methods containing two lines of code each. Unfortunately, I had to muck about with the original library's code to change a couple things from "private" to "protected" to allow me to extend the class to do what needed to be done. Every time I have to do anything with C++ (once every handfull of years) it feels like swimming against the current... It probably would have been simpler to just add the code to the original library's implementation, but that would have introduced a "backwards" dependency in the system's libraries. -- Grant Edwards grant.b.edwards Yow! A dwarf is passing out at somewhere in Detroit! gmail.com From no.email at nospam.invalid Tue Dec 20 16:00:22 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 20 Dec 2011 13:00:22 -0800 Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> Message-ID: <7xr4zzx8k9.fsf@ruckus.brouhaha.com> Grant Edwards writes: > Oops. I should have mentioned this is for embedded systems programming > so templates in general (and STL in particular) are probably off the > table. Templates are how C++ does generics and I'd expect them to appear in be used in embedded programming as well as elsewhere. They can bloat up the code if you're not careful (if function f has several parameters, you can end up with a separate, specialized copy of f for every combination of types that f is called with in the program), but in the typical monomorphic situation they don't add any overhead. I'm not sure about the situation with STL. Anyway, templates aren't terribly hard to understand. From steve+comp.lang.python at pearwood.info Tue Dec 20 16:00:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 20 Dec 2011 21:00:49 GMT Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 15:45:07 -0500, Nathan Rice wrote: > If you take a moment and examine the version number, you will notice > that it is a date code. Not any date code I'm familiar with. 0.111220 doesn't look anything like a date to me. Possibly if the last release was two thousand years ago. I'd rather stick to actively maintained software, if it's all the same with you. > In my opinion that is far more informative than > an arbitrary number. I use the major version number to signify... Wait > for it... Major changes :) Well, that's one opinion. Another opinion is that nobody cares what specific day you release a new version, and that versions 0.191231 and 0.200101 probably aren't that big a difference. -- Steven From nicholas.dokos at hp.com Tue Dec 20 16:04:45 2011 From: nicholas.dokos at hp.com (Nick Dokos) Date: Tue, 20 Dec 2011 16:04:45 -0500 Subject: Text Processing In-Reply-To: Message from =?us-ascii?Q?=3D=3FUTF-8=3FB=3FSsOpcsO0bWU=3D=3F?= =?us-ascii?Q?=3D?= of "Tue\, 20 Dec 2011 21\:03\:21 +0100." <20111220210321.77451e19@bouzin.lan> References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> <20111220210321.77451e19@bouzin.lan> Message-ID: <7895.1324415085@alphaville.americas.hpqcorp.net> J?r?me wrote: > Tue, 20 Dec 2011 11:17:15 -0800 (PST) > Yigit Turgut a ?crit: > > > Hi all, > > > > I have a text file containing such data ; > > > > A B C > > ------------------------------------------------------- > > -2.0100e-01 8.000e-02 8.000e-05 > > -2.0000e-01 0.000e+00 4.800e-04 > > -1.9900e-01 4.000e-02 1.600e-04 > > > > But I only need Section B, and I need to change the notation to ; > > > > 8.000e-02 = 0.08 > > 0.000e+00 = 0.00 > > 4.000e-02 = 0.04 > > > > Text file is approximately 10MB in size. I looked around to see if > > there is a quick and dirty workaround but there are lots of modules, > > lots of options.. I am confused. > > > > Which module is most suitable for this task ? > > You could try to do it yourself. > Does it have to be python? If not, I'd go with something similar to sed 1,2d foo.data | awk '{printf("%.2f\n", $2);}' Nick From no.email at nospam.invalid Tue Dec 20 16:06:49 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 20 Dec 2011 13:06:49 -0800 Subject: [OT] Quick intro to C++ for a Python and C user? References: Message-ID: <7xmxanx89i.fsf@ruckus.brouhaha.com> Grant Edwards writes: > Unfortunately, I had to muck about with the original library's code to > change a couple things from "private" to "protected" to allow me to > extend the class to do what needed to be done. Every time I have to > do anything with C++ (once every handfull of years) it feels like > swimming against the current... I think this is what's called "the expression problem" and it's a perennial sore point in statically typed PL design. Different languages have different approaches to dealing with it. I'm no C++ expert so I don't know what the gurus would say for your particular situation. But, I do get the impression that PL mavens now tend to think that OO and inheritance was a 1990's thing that didn't work out as well as expected, that Java is a painful wreck because of it, and that C++ culture has moved somewhat away from OO in favor of template-based generics. From no.email at nospam.invalid Tue Dec 20 16:08:09 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 20 Dec 2011 13:08:09 -0800 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <7xiplbx87a.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > Not any date code I'm familiar with. 0.111220 doesn't look anything like > a date to me. 0 is the major version number. 111220 (the minor version number) is the date code, 2011-12-20 being today. From rosuav at gmail.com Tue Dec 20 16:10:26 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Dec 2011 08:10:26 +1100 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4EF0F44D.2050403@mrabarnett.plus.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4EF0F44D.2050403@mrabarnett.plus.com> Message-ID: On Wed, Dec 21, 2011 at 7:47 AM, MRAB wrote: > It looks like it's based on the date. I do something similar with the > regex module, except that _my_ version number is Y2K compliant. :-) The eight-digit date is a recognized entity. The truncated six-digit form, much less so. But either is better known than what PPWizard uses, which involves the day-of-year... ChrisA From nathan.alexander.rice at gmail.com Tue Dec 20 16:20:37 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Tue, 20 Dec 2011 16:20:37 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Tue, Dec 20, 2011 at 4:00 PM, Steven D'Aprano wrote: > On Tue, 20 Dec 2011 15:45:07 -0500, Nathan Rice wrote: > >> If you take a moment and examine the version number, you will notice >> that it is a date code. > > Not any date code I'm familiar with. 0.111220 doesn't look anything like > a date to me. > > Possibly if the last release was two thousand years ago. I'd rather stick > to actively maintained software, if it's all the same with you. Date code != date. >> In my opinion that is far more informative than >> an arbitrary number. ?I use the major version number to signify... Wait >> for it... Major changes :) > > Well, that's one opinion. Another opinion is that nobody cares what > specific day you release a new version, and that versions 0.191231 and > 0.200101 probably aren't that big a difference. Nobody cares about version numbers in general, except as a way to fulfill dependencies. By using a date code, your versions are guaranteed to sort in release order (which is nice, say if someone was to download your software via FTP), you can tell what release has what ticket fixes in an issue tracker, stuff like that. It also gives me an easy way to be nostalgic about releases.... As for the extra "20" that I exclude, if I haven't updated the major version number by the time 2020 rolls around I deserve any trouble it causes :) From wimpybobcat at gmail.com Tue Dec 20 17:15:43 2011 From: wimpybobcat at gmail.com (abhishek bhat) Date: Tue, 20 Dec 2011 14:15:43 -0800 (PST) Subject: Cross compiling Python 3.2 for PPC (MPC 8641D) Message-ID: I am trying to compile Python 3.2 to a PPC platform running a lesser known OS called OSE (by ENEA). The problem is that there is no shell which can be used to run the configure script which means that I need to manually port the pyconfig.h and the makefile for the given platform. Any suggestions on the best way to achieve this is much appreciated? From joshua.landau.ws at gmail.com Tue Dec 20 17:16:30 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Tue, 20 Dec 2011 22:16:30 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4ef0f548$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <4eeda5fc$0$29979$c3e8da3$5496439d@news.astraweb.com> <4eee92aa$0$11091$c3e8da3@news.astraweb.com> <67794961-950a-4689-a7b1-de18eca946e6@p16g2000yqd.googlegroups.com> <4ef0f548$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 20 December 2011 20:51, Steven D'Aprano < steve+comp.lang.python at pearwood.info> wrote: > Languages shouldn't depend on advanced editor > features or special keyboards -- that way leads to ColorForth and APL. > True, but dependency on a tool is not the same as being crafted to work best on the tool that 99.9% of programmers use. It's not like it's an ide-*only *feature we were discussing. It's just an observation that this feature* works well with syntax highlighting. *That's a good thing.* * * *What feature are we discussing anyway? I can't remember... Oh! It's actually that "vargs" will be a highlighted keyword! Well, someone using ed will obviously have to stop using python on the basis that "vargs" isn't highlighted, then, which makes it a bad idea. *No*, what makes it a bad idea is the concept itself. As I've said, the 1/10,000 using ed have bigger problems. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ladasky at my-deja.com Tue Dec 20 17:43:16 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 20 Dec 2011 14:43:16 -0800 (PST) Subject: Upgraded Ubuntu -> 11.10, Python -> 2.7.2. Now psyco doesn't work? Message-ID: <222051f5-35b7-44ae-857f-bb819bae0a3e@z22g2000prd.googlegroups.com> I'm chasing down a bug that I think may be in my own code. But a fellow Pythoneer suspected that I might actually be looking at a bug in Python itself. http://groups.google.com/group/comp.lang.python/browse_thread/thread/f4b5843e2b06d1a6 Because I was using Python 2.6.6, and I was told that only bugs which affect language security will be fixed in older revisions, I was encouraged to try my code on Python 2.7.2. I was running Ubuntu Linux 10.10, on which Python 2.6.6 was the standard distribution. I succeeded in doing an "alternate install" of Python 2.7.2, over Ubuntu 10.10. But I use so many packages in my code. Dealing with the package dependencies, and the intricacies of invoking the right version of Python at the command prompt, were making me dizzy. After a few hours of struggle, I just decided to upgrade my OS to Ubuntu 11.10, on which Python 2.7.2 is the standard distribution. I then installed numpy, scipy, and matplotlib, and confirmed that they work. I needed just one more package to try my code: psyco. According to the Synaptic Package Manager of Ubuntu 11.10, there is a supported version of psyco for this OS revision, python-psyco 1.6-2ubuntu1. I downloaded it. It installed. But an attempt to "import psyco" gives me an ImportError at the python prompt. I'm not sure whether my OS upgrade is responsible for this, or whether the accompanying Python revision bump is responsible. I can remove the psyco dependency for now, as it's not essential for me to test my bug. But I will eventually want to do some JIT compilation on a critical inner loop in my code. Applying psyco to that loop nearly doubled my execution speed. Can I fix my import problem? Is psyco being superseded by PyPy? From what I've seen of PyPy, it's pretty radically different than psyco. Thanks for any help! From alex.kapps at web.de Tue Dec 20 19:01:41 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Wed, 21 Dec 2011 01:01:41 +0100 Subject: Text Processing In-Reply-To: <7895.1324415085@alphaville.americas.hpqcorp.net> References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> <20111220210321.77451e19@bouzin.lan> <7895.1324415085@alphaville.americas.hpqcorp.net> Message-ID: <4EF121E5.9020300@web.de> On 20.12.2011 22:04, Nick Dokos wrote: >>> I have a text file containing such data ; >>> >>> A B C >>> ------------------------------------------------------- >>> -2.0100e-01 8.000e-02 8.000e-05 >>> -2.0000e-01 0.000e+00 4.800e-04 >>> -1.9900e-01 4.000e-02 1.600e-04 >>> >>> But I only need Section B, and I need to change the notation to ; >>> >>> 8.000e-02 = 0.08 >>> 0.000e+00 = 0.00 >>> 4.000e-02 = 0.04 > Does it have to be python? If not, I'd go with something similar to > > sed 1,2d foo.data | awk '{printf("%.2f\n", $2);}' > Why sed and awk: awk 'NR>2 {printf("%.2f\n", $2);}' data.txt And in Python: f = open("data.txt") f.readline() # skip header f.readline() # skip header for line in f: print "%02s" % float(line.split()[1]) From ian.g.kelly at gmail.com Tue Dec 20 19:03:26 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Dec 2011 17:03:26 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 12:45 PM, Nathan Rice wrote: > There are still some issues with proper support of things like bool() > and int(), which refuse to return things that are not of the correct > type. And that's a good thing. As type conversion functions, bool(x) and int(x) should *always* return bools and ints respectively (or raise an exception), no matter what you pass in for x. If I do "list(efoo)", where efoo is an ElementwiseProxy object, should I expect to get the efoo collection converted to a list, or another ElementwiseProxy where each element has been converted to a list? I would hope the former. > This was developed as a proof of concept for expanding the role of > element-wise syntax in python, and to that end I welcome comments. The examples you gave are all numerical in nature. If I might inquire, what might I use this for that I can't already do with numpy? Cheers, Ian From malaclypse2 at gmail.com Tue Dec 20 19:08:13 2011 From: malaclypse2 at gmail.com (Jerry Hill) Date: Tue, 20 Dec 2011 19:08:13 -0500 Subject: Upgraded Ubuntu -> 11.10, Python -> 2.7.2. Now psyco doesn't work? In-Reply-To: <222051f5-35b7-44ae-857f-bb819bae0a3e@z22g2000prd.googlegroups.com> References: <222051f5-35b7-44ae-857f-bb819bae0a3e@z22g2000prd.googlegroups.com> Message-ID: On Tue, Dec 20, 2011 at 5:43 PM, John Ladasky wrote: > I was running Ubuntu Linux 10.10, on which Python 2.6.6 was the > standard distribution. ?I succeeded in doing an "alternate install" of > Python 2.7.2, over Ubuntu 10.10. ?But I use so many packages in my > code. ?Dealing with the package dependencies, and the intricacies of > invoking the right version of Python at the command prompt, were > making me dizzy. ?After a few hours of struggle, I just decided to > upgrade my OS to Ubuntu 11.10, on which Python 2.7.2 is the standard > distribution. > > I then installed numpy, scipy, and matplotlib, and confirmed that they > work. ?I needed just one more package to try my code: psyco. > According to the Synaptic Package Manager of Ubuntu 11.10, there is a > supported version of psyco for this OS revision, python-psyco > 1.6-2ubuntu1. ?I downloaded it. ?It installed. ?But an attempt to > "import psyco" gives me an ImportError at the python prompt. >From the psyco homepage ( http://psyco.sourceforge.net/ ) "Python 2.7 is unsupported so far. Anyone interested in porting Psyco to it is welcome. I started the work in a branch but it needs finishing." That's the most recent news item on the home page, and it's dated July 16, 2010, about a year and a half ago. I don't think psyco supports python 2.7. -- Jerry From steve+comp.lang.python at pearwood.info Tue Dec 20 19:12:08 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Dec 2011 00:12:08 GMT Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> <7xiplbx87a.fsf@ruckus.brouhaha.com> Message-ID: <4ef12458$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 13:08:09 -0800, Paul Rubin wrote: > Steven D'Aprano writes: >> Not any date code I'm familiar with. 0.111220 doesn't look anything >> like a date to me. > > 0 is the major version number. 111220 (the minor version number) is the > date code, 2011-12-20 being today. Not here it isn't. It is yesterday. Writing-from-Down-Under-ly y'rs, -- Steven From nathan.alexander.rice at gmail.com Tue Dec 20 19:24:49 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Tue, 20 Dec 2011 19:24:49 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 7:03 PM, Ian Kelly wrote: > On Tue, Dec 20, 2011 at 12:45 PM, Nathan Rice > wrote: >> There are still some issues with proper support of things like bool() >> and int(), which refuse to return things that are not of the correct >> type. > > And that's a good thing. ?As type conversion functions, bool(x) and > int(x) should *always* return bools and ints respectively (or raise an > exception), no matter what you pass in for x. I was hoping to have the proxy be completely transparent. I don't disagree with your statement though. > If I do "list(efoo)", where efoo is an ElementwiseProxy object, should > I expect to get the efoo collection converted to a list, or another > ElementwiseProxy where each element has been converted to a list? ?I > would hope the former. Iterators are how you go from an ElementwiseProxy back to a regular collection. Thus list/set/etc or anything that takes an iterator will work. >> This was developed as a proof of concept for expanding the role of >> element-wise syntax in python, and to that end I welcome comments. > > The examples you gave are all numerical in nature. ?If I might > inquire, what might I use this for that I can't already do with numpy? efoo2 = ElementwiseProxy(["one", "two", "three", "four"]) efoo_res = ((efoo2.capitalize() + " little indian").split(" ").apply(reversed) * 2).apply("_".join) # note that you could do reversed(...) instead, I just like to read left to right efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " little indian").split(" ") in case you need to debug something and want to look at intermediate values The idea is to provide a syntax that lets you do very complex things on collections in a more readable manner, without having 5 or 6 lines of generator expressions. Nathan From steve+comp.lang.python at pearwood.info Tue Dec 20 19:28:33 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Dec 2011 00:28:33 GMT Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef12831$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 20 Dec 2011 16:20:37 -0500, Nathan Rice wrote: >> Well, that's one opinion. Another opinion is that nobody cares what >> specific day you release a new version, and that versions 0.191231 and >> 0.200101 probably aren't that big a difference. > > Nobody cares about version numbers in general, except as a way to > fulfill dependencies. By using a date code, your versions are > guaranteed to sort in release order (which is nice, say if someone was > to download your software via FTP), you can tell what release has what > ticket fixes in an issue tracker, stuff like that. *blink* It must be nice to have software with so few bugs that you can remember the date you fixed each and every one just from the ticket number. I don't actually care what version numbering scheme you use. I had just never come across using part of a date as the version number before, and I don't think that it is any more recognisable or sensible than any other arbitrary numbering scheme. And it is arbitrary: the fact that you happened to make a release on Tuesday rather than Wednesday doesn't have any meaning (particularly if you're a night owl who is coding at midnight) and yet you'll assign a different version number to it. To my mind, the version number should encode (in some sense) the feature set, not the date you release it. It also has the serious disadvantage that you're limited to a single release per day, unless you add an extra field to the version string. But whatever floats your boat. I'm just glad that you've put your money where your mouth is, and released the package, instead of demanding others do the work. Thank you. -- Steven From nathan.alexander.rice at gmail.com Tue Dec 20 19:31:02 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Tue, 20 Dec 2011 19:31:02 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: > efoo2 = ElementwiseProxy(["one", "two", "three", "four"]) > > efoo_res = ((efoo2.capitalize() + " little indian").split(" > ").apply(reversed) * 2).apply("_".join) # note that you could do > reversed(...) instead, I just like to read left to right > efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " > little indian").split(" ") in case you need to debug something and > want to look at intermediate values Just a quick note, I realized after I sent this that reversed returns an iterator, so * will not work. Otherwise everything is kosher. From rantingrickjohnson at gmail.com Tue Dec 20 19:31:42 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 20 Dec 2011 16:31:42 -0800 (PST) Subject: Learning Python 2.4 References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Message-ID: <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> On Dec 20, 12:31?pm, kimma wrote: > Hi there, > > I am about to learn python with "how to think like a computer > scientist". This book is just available for python 2.4. Does it matter > for programming? > > Greetz Kimma, don't listen to either of these guys. Anything before Python 3.0 is now obsolete. We are currently at 3.2.2 for a stable release. If a tutorial writer is too lazy to update his tutorial to AT LEAST Python 3.0, THEN he is a fool and should be banished from not only this community, but from the Internets also. The ONLY reason to use any Python version LESS than Python 3.0 is if you have NO OTHER CHOICE! You don't have that problem kimma because you are new to Python. You have a choice. And the choice should be the MOST CURRENT VERSION OF PYTHON. If, at a later time, you find that you need to use some module that is not 3.0 compliant, then by all means download the appropriate interpreter. BUT DON"T WRITE CODE THAT IS NOT 3.0 COMPLIANT UNLESS YOU HAVE NO OTHER CHOICE!!! Don't be a part of slowing the transition that is happening with Python. We need to look forward and forget about the past. The past is old and dumpy, however, the future is bright! Good luck! From ian.g.kelly at gmail.com Tue Dec 20 19:47:47 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 20 Dec 2011 17:47:47 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 12:45 PM, Nathan Rice wrote: > Get it: > > ? ?PyPi: http://pypi.python.org/pypi/elementwise/0.111220 > ? ?GitHub: https://github.com/nathan-rice/Elementwise > > This was developed as a proof of concept for expanding the role of > element-wise syntax in python, and to that end I welcome comments. A couple of notes on the code: @property def iterable(self): iterable = object.__getattribute__(self, "_iterable") if isinstance(iterable, types.GeneratorType): # Should probably look into a better way to do this return itertools.tee(iterable, 1)[0] else: return iterable I don't think tee is going to do what you think it does here. The semantics of tee are that once you tee off an iterable, you're no longer supposed to use the original iterable. "itertools.tee(iterable, 1)[0]" is more or less equivalent to "iter(iterable)". A more correct approach might be: @property def iterable(self): iterable = object.__getattribute__(self, "_iterable") if isinstance(iterable, collections.Iterator): iterator, self.iterable = itertools.tee(iterable, 2) return iterator else: return iterable That also checks for iterators specifically rather than just generators. Alternatively, you could just check for a "__next__" or "next" method, depending on your Python version, if you don't want to introduce a dependency on Python 2.6. ---------- def __str__(self): return ", ".join(e.__str__() for e in object.__getattribute__(self, "iterable")) ... def __unicode__(self): return object.__unicode__(str(self)) So "unicode(efoo)" will first construct a str from each object in the iterable, and then attempt to decode that str using the default ASCII codec, which may or may not be correct. The attempt at code reuse is admirable, but really __unicode__ should not rely on __str__ if the encoding is unknown. This ought to be: def __unicode__(self): return u", ".join(unicode(e) for e in object.__getattribute__(self, "iterable")) If the objects in question don't have __unicode__ methods, then Python will fall back on __str__ and the default encoding for you. That's all that jumps out at me. Also, are you aware that your README is empty? Although maybe that's just some weird github thing. Cheers, Ian From ladasky at my-deja.com Tue Dec 20 20:16:53 2011 From: ladasky at my-deja.com (John Ladasky) Date: Tue, 20 Dec 2011 17:16:53 -0800 (PST) Subject: Upgraded Ubuntu -> 11.10, Python -> 2.7.2. Now psyco doesn't work? References: <222051f5-35b7-44ae-857f-bb819bae0a3e@z22g2000prd.googlegroups.com> Message-ID: <0825ebed-b501-42b7-975b-cc0e1aaec195@37g2000prc.googlegroups.com> On Dec 20, 4:08?pm, Jerry Hill wrote: > From the psyco homepage (http://psyco.sourceforge.net/) "Python 2.7 > is unsupported so far. Anyone interested in porting Psyco to it is > welcome. I started the work in a branch but it needs finishing." > That's the most recent news item on the home page, and it's dated July > 16, 2010, about a year and a half ago. > > I don't think psyco supports python 2.7. > > -- > Jerry Thanks, Jerry. It's funny that the package tree of Ubuntu 11.10 doesn't specify a dependency on an older revision of Python, then. From joshua.landau.ws at gmail.com Tue Dec 20 20:37:23 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 01:37:23 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 00:24, Nathan Rice wrote: > efoo_res = ((efoo2.capitalize() + " little indian").split(" > ").apply(reversed) * 2).apply("_".join) # note that you could do > reversed(...) instead, I just like to read left to right > efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " > little indian").split(" ") in case you need to debug something and > want to look at intermediate values > How is any of this better than the elementwise operators ("~")? People should be able to expect len(x) to *always* return a number or raise an error. I know it's not part of the spec, but a lot breaks without these guarantees. When "str(x)" isn't a string, *all the formatting code breaks**. And when the other suggestion ("~str(x)" or "str~(x)" or something similar) has all the benifits and none of the drawbacks, why should I use this? Upon this implementation I take back my comment on the whole typing thing. Your title just really confused me. * You can't just make functions non-elementwise unless called through ".apply" either: def str(x): return x.__str__() Also note that I'm ignoring that "~" has a predefined meaning, making "~func(x)" valid already. That's a minor syntactic point, not a point of the proposal, that i stuck with just 'cause of the PEP. The idea is to provide a syntax that lets you do very complex things > on collections in a more readable manner, without having 5 or 6 lines > of generator expressions. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fperez.net at gmail.com Tue Dec 20 20:40:54 2011 From: fperez.net at gmail.com (Fernando Perez) Date: Wed, 21 Dec 2011 01:40:54 +0000 (UTC) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: On Mon, 19 Dec 2011 19:51:00 -0800, Raymond Hettinger wrote: > Do you use IDLE when teaching Python? If not, what is the tool of > choice? I'm obviously biased (I started IPython years ago), but I've done a lot of teaching and I still do like the combination of IPython plus an editor. Sometimes I use IDLE configured to only open the editor and not the shell, but I recommend that users learn a 'real' editor for the long run (aka emacs/vim), as it's an investment that will pay off many times over. But if nothing else, there's at least an OK free editor for each OS that does work, and I keep a 'starter kit' page with those resources for my students: http://fperez.org/py4science/starter_kit.html Cheers, f From mavjs01 at gmail.com Tue Dec 20 20:42:03 2011 From: mavjs01 at gmail.com (Maverick JS) Date: Tue, 20 Dec 2011 17:42:03 -0800 (PST) Subject: Learning Python 2.4 In-Reply-To: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Message-ID: <612275.52.1324431723390.JavaMail.geo-discussion-forums@pril5> I'd recommend starting with http://learnpythonthehardway.org/book/ by "Zed Shaw" or Beginning Python Using Python 2.6 and Python 3.1 by "James Payne" I'd say learning Python 2.6 would be better than 2.4, since it's really old. From ashton at fagg.id.au Tue Dec 20 21:06:33 2011 From: ashton at fagg.id.au (Ashton Fagg) Date: Wed, 21 Dec 2011 12:06:33 +1000 Subject: Python education survey In-Reply-To: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: On 20 December 2011 13:51, Raymond Hettinger wrote: > Students may not be experienced with the command-line and may be > running Windows, Linux, or Macs. ?Ideally, the tool or IDE will be > easy to install and configure (startup directory, path, associated > with a particular version of Python etc). I tutor people (usually fellow students) in programming occasionally, and I've always recommended a simple text editor and a command line (this correlates with most languages, not just Python). My personal set up (using Linux) is vim (with line numbers and syntax highlighting) + shell, no matter which language I'm working with. However for people I'm tutoring, particularly if they're new to programming in general and would find vim intimidating, I recommend gedit (for Linux) or Notepad++ (for Windows), executing/compiling from the command line. As long as the text editor has line numbers and syntax highlighting it's sufficient in my book. I don't like obfuscating what's going on in the background (i.e. interacting with the Python/C/ interpreter/compiler/whatever) with a fancy IDE. However that is my personal (strong) opinion. Hope that helps. -- Ashton Fagg E-mail: ashton at fagg.id.au Web: http://www.fagg.id.au/~ashton/ Keep calm and call Batman. From ashton at fagg.id.au Tue Dec 20 21:13:07 2011 From: ashton at fagg.id.au (Ashton Fagg) Date: Wed, 21 Dec 2011 12:13:07 +1000 Subject: Learning Python 2.4 In-Reply-To: <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On 21 December 2011 10:31, Rick Johnson wrote: > Kimma, don't listen to either of these guys. Anything before Python > 3.0 is now obsolete. We are currently at 3.2.2 for a stable release. > If a tutorial writer is too lazy to update his tutorial to AT LEAST > Python 3.0, THEN he is a fool and should be banished from not only > this community, but from the Internets also. I got the impression the OP was learning programming in general (i.e. from scratch) and not merely "learning Python". If this is the case it shouldn't matter if they're merely learning the concepts as you can always get up to speed on the differences later on as they get more experienced. If they are "learning Python" (i.e. have programmed previously and are learning this as a new language), learning the basics from the book would be fine and it shouldn't be too hard to get up to speed on the newer stuff later on. Although I'd recommend starting on the latest iteration if possible, as it will alleviate some of the leg work of catching up on the newer features. Cheers. -- Ashton Fagg E-mail: ashton at fagg.id.au Web: http://www.fagg.id.au/~ashton/ Keep calm and call Batman. From roy at panix.com Tue Dec 20 21:34:38 2011 From: roy at panix.com (Roy Smith) Date: Tue, 20 Dec 2011 21:34:38 -0500 Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: In article , Ashton Fagg wrote: > As long as the text editor has line numbers and syntax highlighting > it's sufficient in my book. I agree with the syntax highlighting. I resisted for many years, then somebody turned me on to it a few years ago and I've been addicted ever since. As for line numbers, for working alone, I don't see much point. But for any kind of interaction with other people, it's essential. It's just SO much easier to say, "line 417" as opposed to "OK, scroll up a couple more lines, no, no, not that far, go down a little. There! See that print statement, now go down three lines below that, ..." By the time the two of you are on the same page about which line of code you're talking about, you will have forgotten what you wanted to say. From rosuav at gmail.com Tue Dec 20 21:44:27 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Dec 2011 13:44:27 +1100 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: On Wed, Dec 21, 2011 at 1:34 PM, Roy Smith wrote: > As for line numbers, for working alone, I don't see much point. ?But for > any kind of interaction with other people, it's essential. ?It's just SO > much easier to say, "line 417" as opposed to "OK, scroll up a couple > more lines, no, no, not that far, go down a little. ?There! ?See that > print statement, now go down three lines below that, ..." ?By the time > the two of you are on the same page about which line of code you're > talking about, you will have forgotten what you wanted to say. Ctrl-G in many editors will let you GOTO (sorry, I'll try to keep this PG-13) a specific line by number. Extremely handy, especially when you're debugging across computers - the error message comes up on your test computer and cites the line number, and you jump to it on your dev easily. ChrisA From rantingrickjohnson at gmail.com Tue Dec 20 21:50:29 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 20 Dec 2011 18:50:29 -0800 (PST) Subject: Learning Python 2.4 References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On Dec 20, 8:13?pm, Ashton Fagg wrote: > On 21 December 2011 10:31, Rick Johnson wrote: > I got the impression the OP was learning programming in general (i.e. > from scratch) and not merely "learning Python". If this is the case it > shouldn't matter if they're merely learning the concepts as you can > always get up to speed on the differences later on as they get more > experienced. I say that assumption is wrong; read on for details... > If they are "learning Python" (i.e. have programmed previously and are > learning this as a new language), learning the basics from the book > would be fine and it shouldn't be too hard to get up to speed on the > newer stuff later on. Although I'd recommend starting on the latest > iteration if possible, as it will alleviate some of the leg work of > catching up on the newer features. I don't see how "experienced" or "un-experienced" has anything to do with this. The fact is... Python 2.4 is dead. Python 2.5 is dead. Python 2.6 is dead, and yes, even python 2.7 is bereft of life! The only Python version that matters at this moment in "time space" is Python 3.2.2. The only exception to that rule is if you need an old module that 3.0 does not support. AND THAT IS THE ONLY EXCEPTION! I currently have Python 2.7 and Python 3.2.2 installed on my machine. I don't use Python 2.7 because i am a member of some secret "Python Historical Commission", NO. I use Python 2.7 because i have no other choice. Much of my old code bases requires modules that DO NOT work in Python 3.0. Some 3rd party devs are even suggesting that they will NEVER upgrade...HOWEVER... I write ALL new code with Python 3.0 compliance. Anyone who would refuse to use Python 3.0 for new code is a fool. You may not realize it now, but you will be kicking yourself later. There are individual losses and collective losses from each person who thinks they should keep some old run down building from the wrecking ball. Progress requires tearing down old, worn out structures, and replacing them with cleaner, safer, and shiny-er structures. Structures that are NOT infested with vermin. Structures that are NOT a fire hazard. Structures that are NOT insulated with asbestos! Remember, back in the day, asbestos was the "wonder material". Look, it does not burn! Look, it does not rot! Look, it will even wipe your back side! Oh by the way, you might get lung cancer and die from one breath of it's dust. But no need to worry about that little hiccup for many years to come. Collectively we as a community suffer when even one new lad chooses to use an old version of Python. Why? Because that is one less person pushing the masses of 3rd party developers to get off their lazy bums and upgrade to 3000 compliance. Avoid Python<3.X like the plague -- and stop propagating stagnation! From rosuav at gmail.com Tue Dec 20 22:41:38 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 21 Dec 2011 14:41:38 +1100 Subject: Learning Python 2.4 In-Reply-To: References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On Wed, Dec 21, 2011 at 1:50 PM, Rick Johnson wrote: > I currently have Python 2.7 and Python 3.2.2 installed on my machine. > I don't use Python 2.7 because i am a member of some secret "Python > Historical Commission" Shun 3.2.2, it's way too old. The ONLY version of Python worth using is the bleeding-edge from source control! Otherwise you ARE on the Historical Commission. Seriously... to the OP, ignore Rick's doom-and-gloom about old Pythons. If you have a book that teaches 2.4, you can almost certainly use what it teaches with a more recent 2.X interpreter (eg 2.7.2). You may have problems trying to use that book with a 3.X interpreter, though. ChrisA From wuwei23 at gmail.com Tue Dec 20 23:27:07 2011 From: wuwei23 at gmail.com (alex23) Date: Tue, 20 Dec 2011 20:27:07 -0800 (PST) Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. References: Message-ID: On Dec 21, 10:24?am, Nathan Rice wrote: > The idea is to provide a syntax that lets you do very complex things > on collections in a more readable manner, without having 5 or 6 lines > of generator expressions. Have you seen PyLINQ? It has a similar approach to operating on collections, returning a PyLINQ object after each call to facilitate chaining. https://github.com/kalessin/PyLINQ/blob/master/pylinq/linq.py This is a personal opinion on the code, but I'd move instantiating the new ElementwiseProxy out of each method and into its own decorator: # declare this outside of the class def chainable(fn): def _(self, *args, **kwargs): return ElementwiseProxy(fn(self, *args, **kwargs), self) return _ This way, each method that is chainable is a little more obvious without inspecting the code, and the method body itself is only doing what the method says it does: @chainable def __add__(self, other): return (e + other for e in object.__getattribute__(self, "iterable")) Incidentally, displaying an ElementwiseProxy instance doesn't go down well with iPython: In [1]: from elementwise import * In [2]: e = ElementwiseProxy(['one','two','three']) In [3]: e Out[3]: ERROR: An unexpected error occurred while tokenizing input The following traceback may be corrupted or invalid The error message is: ('EOF in multi-line statement', (6, 0)) From python at mrabarnett.plus.com Tue Dec 20 23:30:51 2011 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 21 Dec 2011 04:30:51 +0000 Subject: [Python-ideas] =?UTF-8?B?5Zue5aSN77yaICBDb2x1bW5pemUgaW4gbW9k?= =?UTF-8?B?dWxlICJjbWQi?= In-Reply-To: <1324438134.11446.YahooMailNeo@web92408.mail.cnh.yahoo.com> References: <1324349593.51908.YahooMailNeo@web92411.mail.cnh.yahoo.com> <1324350440.30993.YahooMailNeo@web92413.mail.cnh.yahoo.com> <1324438134.11446.YahooMailNeo@web92408.mail.cnh.yahoo.com> Message-ID: <4EF160FB.50500@mrabarnett.plus.com> On 21/12/2011 03:28, Tom Zhou wrote: > Thanks a lot for your reply! > I've reservations, maybe code looks like: > """ > colwidth = max(map(len, list)) > ncols = displaywidth/(colwidth+2)+1 > nrows = size/ncols+1 > for nrows > for ncols > texts = list[...] > output texts > """ > is clear, and just waste some space of stdout. > A couple of points: 1. Trying not to use the names of built-in classes or functions for your variable names, for example "list". 2. In Python 2 int/int returns an int, but in Python 3 int/int returns a float. In both cases int//int returns an int (at least for recent versions of Python 2), so it's a good idea to always use // when you want to do integer division. From emekamicro at gmail.com Tue Dec 20 23:39:43 2011 From: emekamicro at gmail.com (Emeka) Date: Wed, 21 Dec 2011 06:39:43 +0200 Subject: Please explain this for me Message-ID: Hello All, v = [] def add_to_list(plist): u = plist.append(90) return u add_to_list(v) # This function call returns nothing Could someone explain why this function call will return nothing? v = [90] # Object values are passed by reference This one is clear to me add_to_list([]) This one returns nothing, why? Regards, Emeka -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From ironfroggy at gmail.com Tue Dec 20 23:54:36 2011 From: ironfroggy at gmail.com (Calvin Spealman) Date: Tue, 20 Dec 2011 23:54:36 -0500 Subject: Please explain this for me In-Reply-To: References: Message-ID: On Dec 20, 2011 11:41 PM, "Emeka" wrote: > > > Hello All, > > v = [] > > def add_to_list(plist): > u = plist.append(90) > return u > > add_to_list(v) # This function call returns nothing > Could someone explain why this function call will return nothing? > > v = [90] # Object values are passed by reference > This one is clear to me > > add_to_list([]) > This one returns nothing, why? Because list.append() returns nothing, it only modifies the list in place. > > Regards, > Emeka > -- > Satajanus Nig. Ltd > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From enalicho at gmail.com Tue Dec 20 23:57:26 2011 From: enalicho at gmail.com (Noah Hall) Date: Wed, 21 Dec 2011 04:57:26 +0000 Subject: Please explain this for me In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 4:39 AM, Emeka wrote: > > Hello All, > v = [] > > def add_to_list(plist): > ? ? u = plist.append(90) > ? ? return u > > add_to_list(v) ?# This function call returns nothing > Could someone explain why this function call will return nothing? It's because add_to_list returns the value returned from plist.append stored in u. append changes a list in place and returns nothing. Functions that return nothing return None. This is why it'll be None - u is None because append returns None. > add_to_list([]) > This one returns nothing, why? It's because the object [] here has no name, so that you have no way to refer to it after the function changes it, since it changes it in place. It gets eaten by Python, never to be seen again. From emekamicro at gmail.com Wed Dec 21 00:12:40 2011 From: emekamicro at gmail.com (Emeka) Date: Wed, 21 Dec 2011 07:12:40 +0200 Subject: Please explain this for me In-Reply-To: References: Message-ID: Noah, Calvin Thanks so much! Regards, Emeka On Wed, Dec 21, 2011 at 6:57 AM, Noah Hall wrote: > On Wed, Dec 21, 2011 at 4:39 AM, Emeka wrote: > > > > Hello All, > > > v = [] > > > > def add_to_list(plist): > > u = plist.append(90) > > return u > > > > add_to_list(v) # This function call returns nothing > > Could someone explain why this function call will return nothing? > > It's because add_to_list returns the value returned from plist.append > stored in u. > append changes a list in place and returns nothing. Functions that > return nothing return None. This is why it'll be None - u is None > because append returns None. > > > > add_to_list([]) > > This one returns nothing, why? > > It's because the object [] here has no name, so that you have no way > to refer to it after the function changes it, since it changes it in > place. It gets eaten by Python, never to be seen again. > -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From nad at acm.org Wed Dec 21 01:42:05 2011 From: nad at acm.org (Ned Deily) Date: Wed, 21 Dec 2011 01:42:05 -0500 Subject: Can't get tilde character with IDLE 3.2.2 on French Mac Lion References: Message-ID: In article , Franck Ditter wrote: > In article , > Ned Deily wrote: > > In article , > > Franck Ditter wrote: > > > All is in the subject. I'm starting to use Python with Idle 3.2.2 > > > on MacOS-X Lion (French). I can't get "Option-N space" to provide > > > the ~ char. > > > I tried to go into the Keys preferences but I can't find "Option-N space" > > > to modify its meaning. Its actual behavior is to merge lines of a > > > paragraph. > > You are likely running into a current problem in the OS X Cocoa version > > of Tcl/Tk 8.5 as included with Lion and as shipped by ActiveState. > > Previously, if you tried to type composite characters, like Option N, > > the Cocoa Tcl/Tk would crash. Pending a real fix, a patch was made to > > Tcl/Tk 8.5 to discard composite characters rather than crash. You > > should be able to get a tilde by using the post-composite keyboard > > sequence: try typing "space" followed by "Shift-Option-N". > > > > http://sourceforge.net/tracker/index.php?func=detail&aid=2907388&group_id=12 > > 997&atid=112997 > Nope, "space" followed by "Shift-Option-N" gives a greek iota... > I tried other combinations, unsuccessfully. > IDLE 3 (French) seems to be unusable as we use many ~ in web applications :-( > Should we hope a fix soon, or leave IDLE ? Yes, I see now that that won't work with the French input method. Unfortunately, there is nothing that IDLE or Python can do to workaround the issue as the problem is in Cocoa Tcl/Tk and I don't know that any fix is being worked on. One approach that *might* work is to create a modified French keyboard layout that substitutes tilde for some other key combination without using a dead key (Option-N is a dead key). Ukelele.app seems to be the most frequently recommended keyboard layout editor for OS X and comes with extensive documentation. I don't have time right now to experiment with it but I would be interested to hear if someone is able to make it work. http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=ukelele -- Ned Deily, nad at acm.org From chris at simplistix.co.uk Wed Dec 21 02:15:46 2011 From: chris at simplistix.co.uk (Chris Withers) Date: Wed, 21 Dec 2011 07:15:46 +0000 Subject: Anyone still using Python 2.5? Message-ID: <4EF187A2.6070909@simplistix.co.uk> Hi All, What's the general consensus on supporting Python 2.5 nowadays? Do people still have to use this in commercial environments or is everyone on 2.6+ nowadays? I'm finally getting some continuous integration set up for my packages and it's highlighting some 2.5 compatibility issues. I'm wondering whether to fix those (lots of ugly "from __future__ import with_statement" everywhere) or just to drop Python 2.5 support. What do people feel? cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk From stefan_ml at behnel.de Wed Dec 21 03:35:35 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 21 Dec 2011 09:35:35 +0100 Subject: Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: Chris Withers, 21.12.2011 08:15: > What's the general consensus on supporting Python 2.5 nowadays? From my own (recent) polls, it appears that people want continued support for Python 2.4 and later for a couple of years to come, mainly because RHEL5 uses that by default and has official support until 2014. Similar considerations apply to many Solaris installations. The general consensus seems to be that support for Python 2.3 can easily be dropped, but that support for Py2.4 and later would be helpful. Stefan From pierre-yves.david at logilab.fr Wed Dec 21 04:21:28 2011 From: pierre-yves.david at logilab.fr (Pierre-Yves David) Date: Wed, 21 Dec 2011 10:21:28 +0100 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: <20111221092128.GA27175@crater1.logilab.fr> On Wed, Dec 21, 2011 at 07:15:46AM +0000, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > > Do people still have to use this in commercial environments or is > everyone on 2.6+ nowadays? > > I'm finally getting some continuous integration set up for my > packages and it's highlighting some 2.5 compatibility issues. I'm > wondering whether to fix those (lots of ugly "from __future__ import > with_statement" everywhere) or just to drop Python 2.5 support. > > What do people feel? Most linux distribution went directly from 2.4 to 2.5 Debian: old stable (lenny) 2.4 stable (squeeze) 2.5 Red Hat REHL5 2.4 REHL6 2.6 The most notable exception is Ubuntu Hardy and LTS release from april 2008 with 2.5. But this LTS is out of support for almost 1 year now and current LTS (Lucid) ship 2.6. If you don't plan to support 2.4, supporting 2.5 does not seems a priority. -- Pierre-Yves David http://www.logilab.fr/ -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 198 bytes Desc: Digital signature URL: From saqib.ali.75 at gmail.com Wed Dec 21 04:23:43 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Wed, 21 Dec 2011 01:23:43 -0800 (PST) Subject: Can I get use pexpect to control this process? Message-ID: <1b3ff657-53d3-40d9-b7a8-6246b4d27f31@n6g2000vbz.googlegroups.com> I have a program X that constantly spews output, hundreds of lines per minute. X is not an interactive program. IE: it doesn't take any user input. It just produces a lot of textual output to STDOUT. I would like to save the output produced by X into a different file every 5 seconds regardless of the actual content. I want one file to contain the output from seconds 0-5, another file should contain 6-10, etc. etc. Can I do this with pexpect? I'm not sure I can because the "before" argument gives me EVERYTHING upto the most recent match. What I really need is something that will give me what was produced between the last 2 calls to pexpect.expect(). From djc at news.invalid Wed Dec 21 05:06:59 2011 From: djc at news.invalid (DJC) Date: Wed, 21 Dec 2011 10:06:59 +0000 Subject: Learning Python 2.4 In-Reply-To: References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On 21/12/11 02:13, Ashton Fagg wrote: > I got the impression the OP was learning programming in general (i.e. > from scratch) and not merely "learning Python". If this is the case it > shouldn't matter if they're merely learning the concepts as you can > always get up to speed on the differences later on as they get more > experienced. In which case the most important thing is the quality of the book as a text on Programming. If you find the the author's style to your taste, then use that book rather than struggle with a text based on a recent version that you personally find unreadable. From DStaple at nds.com Wed Dec 21 05:09:05 2011 From: DStaple at nds.com (Staple, Daniel (BSKYB)) Date: Wed, 21 Dec 2011 10:09:05 +0000 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: We've a bunch of windows servers stuck on python 2.5 since an API we use on them is shipped to us pyc's only - forcing us to stay with that version. Most of our other machines are on 2.6 or 2.7. -----Original Message----- From: testing-in-python-bounces at lists.idyll.org [mailto:testing-in-python-bounces at lists.idyll.org] On Behalf Of Chris Withers Sent: 21 December 2011 07:16 To: Python List; testing-in-python at lists.idyll.org; simplistix at googlegroups.com Subject: [TIP] Anyone still using Python 2.5? Hi All, What's the general consensus on supporting Python 2.5 nowadays? Do people still have to use this in commercial environments or is everyone on 2.6+ nowadays? I'm finally getting some continuous integration set up for my packages and it's highlighting some 2.5 compatibility issues. I'm wondering whether to fix those (lots of ugly "from __future__ import with_statement" everywhere) or just to drop Python 2.5 support. What do people feel? cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk _______________________________________________ testing-in-python mailing list testing-in-python at lists.idyll.org http://lists.idyll.org/listinfo/testing-in-python ************************************************************************************** This message is confidential and intended only for the addressee. If you have received this message in error, please immediately notify the postmaster at nds.com and delete it from your system as well as any copies. The content of e-mails as well as traffic data may be monitored by NDS for employment and security purposes. To protect the environment please do not print this e-mail unless necessary. NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603 8808 40-00 ************************************************************************************** From steve+comp.lang.python at pearwood.info Wed Dec 21 05:50:34 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Dec 2011 10:50:34 GMT Subject: Anyone still using Python 2.5? References: Message-ID: <4ef1b9fa$0$29973$c3e8da3$5496439d@news.astraweb.com> On Wed, 21 Dec 2011 07:15:46 +0000, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > > Do people still have to use this in commercial environments or is > everyone on 2.6+ nowadays? Centos and Red Hat production systems still use Python 2.4, so yes, absolutely, 2.5 and 2.4 still need to be supported. Not necessarily by package authors though -- that's a matter for them to decide. I'm presently writing a small library which will support 2.4 through 3.2, which isn't as hard as it sounds like, but still isn't exactly fun. If the project were much bigger, I'd drop support for 2.4 and only support 2.5. At least then I could use conditional expressions and __future__ imports. > I'm finally getting some continuous integration set up for my packages > and it's highlighting some 2.5 compatibility issues. I'm wondering > whether to fix those (lots of ugly "from __future__ import > with_statement" everywhere) or just to drop Python 2.5 support. > > What do people feel? It really depends on *your* users, not arbitrary developers. How many of your users are using 2.5? -- Steven From jml at mumak.net Wed Dec 21 05:55:29 2011 From: jml at mumak.net (Jonathan Lange) Date: Wed, 21 Dec 2011 10:55:29 +0000 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <20111221092128.GA27175@crater1.logilab.fr> References: <4EF187A2.6070909@simplistix.co.uk> <20111221092128.GA27175@crater1.logilab.fr> Message-ID: On Wed, Dec 21, 2011 at 9:21 AM, Pierre-Yves David wrote: ... > The most notable exception is Ubuntu Hardy and LTS release from april 2008 with > 2.5. But this LTS is out of support for almost 1 year now and current LTS > (Lucid) ship 2.6. Not quite. Ubuntu 8.04 LTS is supported on the server until April 2013. jml From jim at zope.com Wed Dec 21 05:57:26 2011 From: jim at zope.com (Jim Fulton) Date: Wed, 21 Dec 2011 05:57:26 -0500 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: On Wed, Dec 21, 2011 at 2:15 AM, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > > Do people still have to use this in commercial environments or is everyone > on 2.6+ nowadays? > > I'm finally getting some continuous integration set up for my packages and > it's highlighting some 2.5 compatibility issues. I'm wondering whether to > fix those (lots of ugly "from __future__ import with_statement" everywhere) > or just to drop Python 2.5 support. > > What do people feel? Google app engine is still Python 2.5, as is Jython. Jim -- Jim Fulton http://www.linkedin.com/in/jimfulton From ashton at fagg.id.au Wed Dec 21 07:16:42 2011 From: ashton at fagg.id.au (Ashton Fagg) Date: Wed, 21 Dec 2011 22:16:42 +1000 Subject: Learning Python 2.4 In-Reply-To: References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On 21 December 2011 20:06, DJC wrote: > > In which case the most important thing is the quality of the book as a > text on Programming. If you find the the author's style to your taste, > then use that book rather than struggle with a text based on a recent > version that you personally find unreadable. This is very good advice. -- Ashton Fagg E-mail: ashton at fagg.id.au Web: http://www.fagg.id.au/~ashton/ Keep calm and call Batman. From cosmo_general at yahoo.com Wed Dec 21 07:59:59 2011 From: cosmo_general at yahoo.com (Muddy Coder) Date: Wed, 21 Dec 2011 04:59:59 -0800 (PST) Subject: Why widgets become 'NoneType'? Message-ID: <2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com> Hi Folks, I was driven nuts by this thing: widgets lost their attributes, then I can't configure them. Please take a look at the codes below: from Tkinter import * canvas = Canvas(width=300, height=400, bg='white') canvas.pack(expand=NO, fill=BOTH) pic = PhotoImage(file=img) canvas.create_image(0, 0, image=pic) al = PhotoImage(file='a.gif') lbl = Label(canvas, text=x, fg='red').pack(side=LEFT) canvas.create_text(40,20, text='Howdy') the last line of code got error message, says canvas object has no attribute of config. The same problem happened again, the code is below: def make_labels(win, astr): labels = [] for i in range(len(astr)): lbl = Label(win, text=astr[i]).pack(side=LEFT ) labels.append(lbl) return tuple(labels) def config_labels(atuple, func): for lbl in atuple: lbl.config('%s') % func root = Tk() lbls = make_labels(root, 'foobar') config_labels(lbls, "fg='red'") The config for Label was picked up: No attributes of config. I tried to print type(lbls), and found Python interpreter reported as 'NoneType'. I also tried to print out dir(lbls), fount there was no attributes I familiar with such as config and such. What is wrong with them? Both of the codes above WORKED, since I saw my widgets displayed them as I wanted, but I just can't configure them as I usually did. Can somebody help me out? I never experience such a weird thing. Thanks! Cosmo From georger.silva at gmail.com Wed Dec 21 08:04:13 2011 From: georger.silva at gmail.com (George R. C. Silva) Date: Wed, 21 Dec 2011 11:04:13 -0200 Subject: Anyone still using Python 2.5? In-Reply-To: <4ef1b9fa$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef1b9fa$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EF1D94D.4000004@gmail.com> Em quarta-feira, 21 de dezembro de 2011 08:50:34, Steven D'Aprano escreveu: > On Wed, 21 Dec 2011 07:15:46 +0000, Chris Withers wrote: > >> Hi All, >> >> What's the general consensus on supporting Python 2.5 nowadays? >> >> Do people still have to use this in commercial environments or is >> everyone on 2.6+ nowadays? > > Centos and Red Hat production systems still use Python 2.4, so yes, > absolutely, 2.5 and 2.4 still need to be supported. > > Not necessarily by package authors though -- that's a matter for them to > decide. I'm presently writing a small library which will support 2.4 > through 3.2, which isn't as hard as it sounds like, but still isn't > exactly fun. If the project were much bigger, I'd drop support for 2.4 > and only support 2.5. At least then I could use conditional expressions > and __future__ imports. > > >> I'm finally getting some continuous integration set up for my packages >> and it's highlighting some 2.5 compatibility issues. I'm wondering >> whether to fix those (lots of ugly "from __future__ import >> with_statement" everywhere) or just to drop Python 2.5 support. >> >> What do people feel? > > It really depends on *your* users, not arbitrary developers. How many of > your users are using 2.5? > > > There are still people on 2.5. ESRIs customers (www.esri.com) that rely heavily on Python 2.5, because it ships with a popular ArcGIS release (9.31). The new ArcGIS release uses 2.6, but I can see 9.31 lurking around for another year, at least. Cheers. From roy at panix.com Wed Dec 21 08:07:24 2011 From: roy at panix.com (Roy Smith) Date: Wed, 21 Dec 2011 08:07:24 -0500 Subject: Anyone still using Python 2.5? References: <4ef1b9fa$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4ef1b9fa$0$29973$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > Centos and Red Hat production systems still use Python 2.4, so yes, > absolutely, 2.5 and 2.4 still need to be supported. Is Python 2.4 destined to be the next IE-6? From d at davea.name Wed Dec 21 08:22:04 2011 From: d at davea.name (Dave Angel) Date: Wed, 21 Dec 2011 08:22:04 -0500 Subject: Why widgets become 'NoneType'? In-Reply-To: <2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com> References: <2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com> Message-ID: <4EF1DD7C.4020900@davea.name> On 12/21/2011 07:59 AM, Muddy Coder wrote: > Hi Folks, > > I was driven nuts by this thing: widgets lost their attributes, then I > can't configure them. Please take a look at the codes below: > > from Tkinter import * > canvas = Canvas(width=300, height=400, bg='white') > canvas.pack(expand=NO, fill=BOTH) > pic = PhotoImage(file=img) > canvas.create_image(0, 0, image=pic) > al = PhotoImage(file='a.gif') > > lbl = Label(canvas, text=x, fg='red').pack(side=LEFT) > > canvas.create_text(40,20, text='Howdy') > > the last line of code got error message, says canvas object has no > attribute of config. The same problem happened again, the code is > below: > > def make_labels(win, astr): > > labels = [] > for i in range(len(astr)): > lbl = Label(win, text=astr[i]).pack(side=LEFT ) > labels.append(lbl) > return tuple(labels) > def config_labels(atuple, func): > for lbl in atuple: > lbl.config('%s') % func > > root = Tk() > lbls = make_labels(root, 'foobar') > config_labels(lbls, "fg='red'") > > The config for Label was picked up: No attributes of config. I tried > to print type(lbls), and found Python interpreter reported as > 'NoneType'. I also tried to print out dir(lbls), fount there was no > attributes I familiar with such as config and such. What is wrong with > them? Both of the codes above WORKED, since I saw my widgets displayed > them as I wanted, but I just can't configure them as I usually did. > Can somebody help me out? I never experience such a weird thing. > Thanks! > > Cosmo When posting error messages, post it exactly and completely; do not paraphrase. -- DaveA From tom at recursivedream.com Wed Dec 21 08:38:21 2011 From: tom at recursivedream.com (Tom Davis) Date: Wed, 21 Dec 2011 08:38:21 -0500 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: On Dec 21, 2011, at 2:15 AM, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > > Do people still have to use this in commercial environments or is everyone on 2.6+ nowadays? For those of us living the nightmare of AppEngine *and* working on an app old enough to not be using the newer Datastore, we're stuck on 2.5 until we can justify a data migration. Frankly I don't know how many such apps exist these days that are still actively developed, though. > > I'm finally getting some continuous integration set up for my packages and it's highlighting some 2.5 compatibility issues. I'm wondering whether to fix those (lots of ugly "from __future__ import with_statement" everywhere) or just to drop Python 2.5 support. > > What do people feel? > > cheers, > > Chris > > -- > Simplistix - Content Management, Batch Processing & Python Consulting > - http://www.simplistix.co.uk > > _______________________________________________ > testing-in-python mailing list > testing-in-python at lists.idyll.org > http://lists.idyll.org/listinfo/testing-in-python From kw at codebykevin.com Wed Dec 21 08:46:07 2011 From: kw at codebykevin.com (Kevin Walzer) Date: Wed, 21 Dec 2011 08:46:07 -0500 Subject: Learning Python 2.4 In-Reply-To: <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> <92fc42f4-f0c4-499e-8b68-7bb713991d38@l24g2000yqm.googlegroups.com> Message-ID: On 12/20/11 7:31 PM, Rick Johnson wrote: > Anything before Python > 3.0 is now obsolete. We are currently at 3.2.2 for a stable release. 2.7 is still a supported production release of Python. It has not been end-of-lifed. -- Kevin Walzer Code by Kevin http://www.codebykevin.com From roy at panix.com Wed Dec 21 08:51:14 2011 From: roy at panix.com (Roy Smith) Date: Wed, 21 Dec 2011 08:51:14 -0500 Subject: [TIP] Anyone still using Python 2.5? References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: In article , Tom Davis wrote: > For those of us living the nightmare of AppEngine I've never used AppEngine, just read a little about it. Could you explain why it's a nightmare? From neilc at norwich.edu Wed Dec 21 09:04:54 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 21 Dec 2011 14:04:54 GMT Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> Message-ID: <9le7c5F1lsU1@mid.individual.net> On 2011-12-20, Paul Rubin wrote: > Grant Edwards writes: >> Oops. I should have mentioned this is for embedded systems >> programming so templates in general (and STL in particular) >> are probably off the table. > > Templates are how C++ does generics and I'd expect them to > appear in be used in embedded programming as well as elsewhere. > They can bloat up the code if you're not careful (if function f > has several parameters, you can end up with a separate, > specialized copy of f for every combination of types that f is > called with in the program), but in the typical monomorphic > situation they don't add any overhead. I'm not sure about the > situation with STL. Anyway, templates aren't terribly hard to > understand. Moreover, if you don't plan to take advantage of templates or inheritance, then you could as well write C++ compatible C and be pretty happy with the results. -- Neil Cerutti From roy at panix.com Wed Dec 21 09:26:04 2011 From: roy at panix.com (Roy Smith) Date: Wed, 21 Dec 2011 09:26:04 -0500 Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> Message-ID: In article <9le7c5F1lsU1 at mid.individual.net>, Neil Cerutti wrote: > On 2011-12-20, Paul Rubin wrote: > > Grant Edwards writes: > >> Oops. I should have mentioned this is for embedded systems > >> programming so templates in general (and STL in particular) > >> are probably off the table. > > > > Templates are how C++ does generics and I'd expect them to > > appear in be used in embedded programming as well as elsewhere. > > They can bloat up the code if you're not careful (if function f > > has several parameters, you can end up with a separate, > > specialized copy of f for every combination of types that f is > > called with in the program), but in the typical monomorphic > > situation they don't add any overhead. I'm not sure about the > > situation with STL. Anyway, templates aren't terribly hard to > > understand. > > Moreover, if you don't plan to take advantage of templates or > inheritance, then you could as well write C++ compatible C and be > pretty happy with the results. Well, C++ still gives you constructors, destructors, and the ability to write class-specific operators. But, you'd be missing one of C++'s biggest selling points; safe containers. Even if you never explore anything in STL beyond std:string and std:vector, you will have saved yourself a world of buffer overflow pain. From nathan.alexander.rice at gmail.com Wed Dec 21 10:07:31 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 10:07:31 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 8:37 PM, Joshua Landau wrote: > On 21 December 2011 00:24, Nathan Rice > wrote: >> efoo_res = ((efoo2.capitalize() + " little indian").split(" >> ").apply(reversed) * 2).apply("_".join) # note that you could do >> reversed(...) instead, I just like to read left to right >> efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " >> little indian").split(" ") in case you need to debug something and >> want to look at intermediate values > > How is any of this better than the elementwise operators ("~")? People > should be able to expect len(x) to always?return a number or raise an error. > I know it's not part of the spec, but a lot breaks without these guarantees. > When "str(x)" isn't a string, all the formatting code breaks*. And when the > other suggestion ("~str(x)" or "str~(x)" or something similar) has all the > benifits and none of the drawbacks, why should I use this? len() will always return a number or raise an error, just like the type functions (bool/int/etc) return that type or raise an error. The interpreter guarantees that for you. This has a couple of advantages over element-wise operators: 1. Because everything is handled in terms of generator chains, all operations on an ElementwiseProxy are evaluated lazily. With element-wise operator overloading you would need to perform each operation immediately. 2. As a result of #1, you can "undo" operations you perform on an ElementwiseProxy with the parent property. 3. This still works if the person who created the class you're working with doesn't add support for element-wise operators. Sure, you could monkey patch their code, but that can lead to other problems down the line. 4. There isn't an obvious/intuitive character for element-wise versions of operators, and fewer symbols is better than more IMHO (see: Perl). Also, if you use the ElementwiseProxyMixin, you can sprinkle element-wise stuff in neatly just by using "variable.each" where you would use "~" in your examples. > Upon this implementation I take back my comment on the whole typing thing. > Your title just really confused me. > > * You can't just make functions non-elementwise unless called through > ".apply" either: > def str(x): return x.__str__() str is one of the special cases (along with repr, unicode, int, float, long, bool, etc). These can't ever be elementwise in CPython (I don't know that this holds in other interpreters). The idea is that you use ElementwiseProxy(x) or preferably x.each, work with your data in an element-wise capacity as needed, then list() or iter() back out. I think it should be a deliberate change of context. Interleaving the proxy (say via x.each) in mostly scalar expressions works, but wasn't the use case I designed it for, and in those circumstances it doesn't really offer anything above the map function. Nathan From nathan.alexander.rice at gmail.com Wed Dec 21 10:15:38 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 10:15:38 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: > Have you seen PyLINQ? It has a similar approach to operating on > collections, returning a PyLINQ object after each call to facilitate > chaining. https://github.com/kalessin/PyLINQ/blob/master/pylinq/linq.py I hadn't seen it previously. I am a VERY heavy user of SQL Alchemy though, and I am sure chaining generative ClauseElements/Queries/etc, has burned some patterns into my subconscious at this point. > This is a personal opinion on the code, but I'd move instantiating the > new ElementwiseProxy out of each method and into its own decorator: > > ? ?# declare this outside of the class > ? ?def chainable(fn): > ? ? ? ?def _(self, *args, **kwargs): > ? ? ? ? ? ?return ElementwiseProxy(fn(self, *args, **kwargs), self) > ? ? ? ?return _ > > This way, each method that is chainable is a little more obvious > without inspecting the code, and the method body itself is only doing > what the method says it does: > > ? ?@chainable > ? ?def __add__(self, other): > ? ? ? ?return (e + other for e in object.__getattribute__(self, > "iterable")) This is a reasonable suggestion and I will play with something along those lines soon. > Incidentally, displaying an ElementwiseProxy instance doesn't go down > well with iPython: > > In [1]: from elementwise import * > > In [2]: e = ElementwiseProxy(['one','two','three']) > > In [3]: e > Out[3]: ERROR: An unexpected error occurred while tokenizing input > The following traceback may be corrupted or invalid > The error message is: ('EOF in multi-line statement', (6, 0)) I love IPython, but it has had known problems with iterators for years. A long time ago, I agonized over what I thought was a bug in my code where itertools.count would skip numbers in IPython, but my unit tests all passed. Everything should work fine if you tuple() it first. Nathan From invalid at invalid.invalid Wed Dec 21 10:37:02 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 21 Dec 2011 15:37:02 +0000 (UTC) Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> Message-ID: On 2011-12-21, Neil Cerutti wrote: > On 2011-12-20, Paul Rubin wrote: >> Grant Edwards writes: >>> Oops. I should have mentioned this is for embedded systems >>> programming so templates in general (and STL in particular) >>> are probably off the table. >> >> Templates are how C++ does generics and I'd expect them to >> appear in be used in embedded programming as well as elsewhere. I've only worked on the perphery of a couple embedded projects that used C++, but it seems to be farily common practice to prohibit the use of templates in embedded code. The problem seems to be that people writing C++ code don't really understand the language, how templates work, or (in general) the implications of the code they write. So, you have to prohibit anything that can be easily misused or cause non-obvious problems. For exaple (quoting from http://en.wikipedia.org/wiki/Embedded_C%2B%2B): Embedded C++ is a proper subset of C++. The following language features have been removed: Multiple inheritance Virtual base classes Run-time type information (typeid) New style casts (static_cast, dynamic_cast, reinterpret_cast and const_cast) The mutable type qualifier Namespaces Exceptions Templates Some people go further and prohibit any run-time object creation. >> They can bloat up the code if you're not careful Bingo. In my experience, many (if not most) C++ programmers don't understand enough of the language to realize what "being careful" means. >> (if function f has several parameters, you can end up with a >> separate, specialized copy of f for every combination of types that f >> is called with in the program), but in the typical monomorphic >> situation they don't add any overhead. I'm not sure about the >> situation with STL. Anyway, templates aren't terribly hard to >> understand. > > Moreover, if you don't plan to take advantage of templates or > inheritance, then you could as well write C++ compatible C and be > pretty happy with the results. Well, there are other C++ features that C doesn't have (e.g. exceptions -- but they're often prohibitted in embedded programming as well). Personally, I think C++ is an awful language in general and a _really_ awful language for embedded systems work. -- Grant Edwards grant.b.edwards Yow! PEGGY FLEMMING is at stealing BASKET BALLS to gmail.com feed the babies in VERMONT. From ndbecker2 at gmail.com Wed Dec 21 10:48:36 2011 From: ndbecker2 at gmail.com (Neal Becker) Date: Wed, 21 Dec 2011 10:48:36 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> Message-ID: Clarification: where can packing/unpacking syntax be used? It would be great if it were valid essentially anywhere (not limited to parameter passing). What about constructs like: a, @tuple tail, b = sequence? From neilc at norwich.edu Wed Dec 21 11:16:26 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 21 Dec 2011 16:16:26 GMT Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> Message-ID: <9lef2pFgggU1@mid.individual.net> On 2011-12-21, Grant Edwards wrote: > On 2011-12-21, Neil Cerutti wrote: >> On 2011-12-20, Paul Rubin wrote: >>> Grant Edwards writes: >>>> Oops. I should have mentioned this is for embedded systems >>>> programming so templates in general (and STL in particular) >>>> are probably off the table. >>> >>> Templates are how C++ does generics and I'd expect them to >>> appear in be used in embedded programming as well as elsewhere. > > I've only worked on the perphery of a couple embedded projects that > used C++, but it seems to be farily common practice to prohibit the > use of templates in embedded code. The problem seems to be that > people writing C++ code don't really understand the language, how > templates work, or (in general) the implications of the code they > write. So, you have to prohibit anything that can be easily misused > or cause non-obvious problems. I cheerfully agree that programmers ignorant of C++ should not write programs in it. But furthermore, they should also not define a subset of C++ for use in embedded programming. ;) -- Neil Cerutti From robert.kern at gmail.com Wed Dec 21 11:29:33 2011 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 21 Dec 2011 16:29:33 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 12/21/11 3:15 PM, Nathan Rice wrote: >> Incidentally, displaying an ElementwiseProxy instance doesn't go down >> well with iPython: >> >> In [1]: from elementwise import * >> >> In [2]: e = ElementwiseProxy(['one','two','three']) >> >> In [3]: e >> Out[3]: ERROR: An unexpected error occurred while tokenizing input >> The following traceback may be corrupted or invalid >> The error message is: ('EOF in multi-line statement', (6, 0)) > > I love IPython, but it has had known problems with iterators for > years. A long time ago, I agonized over what I thought was a bug in > my code where itertools.count would skip numbers in IPython, but my > unit tests all passed. Everything should work fine if you tuple() it > first. This is a different problem, actually. The problem is that the recently added (by me, actually) pretty-printing system tries to dispatch based on the type. In order to handle old-style classes, it checks for the type using .__class__ first. ElementProxy's __getattribute__() gets in the way here by returning a generator instead of the ElementProxy class. -- 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 From ethan at stoneleaf.us Wed Dec 21 11:33:41 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 21 Dec 2011 08:33:41 -0800 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> Message-ID: <4EF20A65.3050507@stoneleaf.us> Neal Becker wrote: > Clarification: where can packing/unpacking syntax be used? > > It would be great if it were valid essentially anywhere (not limited to > parameter passing). > > What about constructs like: > > a, @tuple tail, b = sequence? You mean like Python 3's: a, *middle, b = sequence ? From nathan.alexander.rice at gmail.com Wed Dec 21 11:43:49 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 11:43:49 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 11:29 AM, Robert Kern wrote: > On 12/21/11 3:15 PM, Nathan Rice wrote: > >>> Incidentally, displaying an ElementwiseProxy instance doesn't go down >>> well with iPython: >>> >>> In [1]: from elementwise import * >>> >>> In [2]: e = ElementwiseProxy(['one','two','three']) >>> >>> In [3]: e >>> Out[3]: ERROR: An unexpected error occurred while tokenizing input >>> The following traceback may be corrupted or invalid >>> The error message is: ('EOF in multi-line statement', (6, 0)) >> >> >> I love IPython, but it has had known problems with iterators for >> years. ?A long time ago, I agonized over what I thought was a bug in >> my code where itertools.count would skip numbers in IPython, but my >> unit tests all passed. ?Everything should work fine if you tuple() it >> first. > > > This is a different problem, actually. The problem is that the recently > added (by me, actually) pretty-printing system tries to dispatch based on > the type. In order to handle old-style classes, it checks for the type using > .__class__ first. ElementProxy's __getattribute__() gets in the way here by > returning a generator instead of the ElementProxy class. Thanks for the heads up Robert. Given that IPython is awesome and people should probably be doing an '.apply(type)' in that instance anyhow, I will corner case that on __getattribute__. Nathan From joshua.landau.ws at gmail.com Wed Dec 21 11:48:44 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 16:48:44 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 15:07, Nathan Rice wrote: > On Tue, Dec 20, 2011 at 8:37 PM, Joshua Landau > wrote: > > On 21 December 2011 00:24, Nathan Rice > > wrote: > >> efoo_res = ((efoo2.capitalize() + " little indian").split(" > >> ").apply(reversed) * 2).apply("_".join) # note that you could do > >> reversed(...) instead, I just like to read left to right > >> efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " > >> little indian").split(" ") in case you need to debug something and > >> want to look at intermediate values > > > > How is any of this better than the elementwise operators ("~")? People > > should be able to expect len(x) to always return a number or raise an > error. > > I know it's not part of the spec, but a lot breaks without these > guarantees. > > When "str(x)" isn't a string, all the formatting code breaks*. And when > the > > other suggestion ("~str(x)" or "str~(x)" or something similar) has all > the > > benifits and none of the drawbacks, why should I use this? > > len() will always return a number or raise an error, just like the > type functions (bool/int/etc) return that type or raise an error. The > interpreter guarantees that for you. The point wasn't that either way was better, but that with this implementation you get neither choice ("len(x)" vs "len~(x)") or reliability. The reliability point works like this: You want to elementwise a few functions, that before you were doing on a single item. BEFORE: item = foreignfunc1(item) item = foreignfunc2(item) item = foreignfunc3(item) NOW (your method): item = ElementwiseProxy(item) item = foreignfunc1(item) item = foreignfunc2(item) item = foreignfunc3(item) item = list(item) NOW (the PEP): item = foreignfunc1~(item) item = foreignfunc2~(item) item = foreignfunc3~(item) NOW (map): # or imap to do it lazily item = map(foreignfunc1, (item)) item = map(foreignfunc2, (item)) item = map(foreignfunc3, (item)) The PEP is short and quaint, and makes lots of things shorter than map ("[1,2,3]~+[5,6,7]"), so you can see the advantages. That's why I like the PEP as "syntatic sugar". The mapping method works fine. *You might think your method works. *But what if foreignfunc is "str"? *And you can't blacklist functions.* You can't say everything works bar A, B and C. What if you get: "lambda x:str(x)"? You can't blacklist that. That makes the ElementwiseProxy version buggy and prone to unstable operation. If it's consistent, fine. But it's not. This has a couple of advantages over element-wise operators: > Couple == 2 :P > 1. Because everything is handled in terms of generator chains, all > operations on an ElementwiseProxy are evaluated lazily. With > element-wise operator overloading you would need to perform each > operation immediately. > I agree this can be a preferred advantage.But you still have to choose one. "~" could be lazy, or it could be eager. But in both implementations you have to choose. That said, you have map and imap, and so you could have ElemetwiseProxy and eElementwiseProxy (eager), and you can have "~" and "i~". Remember that the syntax I'm using is just for PEP consistency. Some more creative people can find a syntax that works. > 2. As a result of #1, you can "undo" operations you perform on an > ElementwiseProxy with the parent property. > Use case? If this is actually a wanted feature, "parent" could be made a general property of iterators. (x for x in foo).parent == foo I think that's a separate proposal that isn't intrinsic to this idea. 3. This still works if the person who created the class you're working > with doesn't add support for element-wise operators. Sure, you could > monkey patch their code, but that can lead to other problems down the > line. > Example? I'm struggling to think of a case where you could do this only with one. > 4. There isn't an obvious/intuitive character for element-wise > versions of operators, and fewer symbols is better than more IMHO > (see: Perl). Also, if you use the ElementwiseProxyMixin, you can > sprinkle element-wise stuff in neatly just by using "variable.each" > where you would use "~" in your examples. Obvious/intuitive is meaningless for something so potentially common. "**" is far more obscure ("*" is obscure enough). Why is a double star the exponent character? Because it works. "~" would be easy to learn and consistent, once a standard was agreed. And again, I'm only using that syntax because it's the one in the PEP. We're not going to get to Perl, I hope, with this addition. Explicit > Implicit. If someone passes you a "variable.each", things go haywire (see: first section of reply). "~" is explicit. It doesn't have these problems. I also think explicit looks nicer. 'Cause it is, y'know :P > > Upon this implementation I take back my comment on the whole typing > thing. > > Your title just really confused me. > > > > * You can't just make functions non-elementwise unless called through > > ".apply" either: > > def str(x): return x.__str__() > > str is one of the special cases (along with repr, unicode, int, float, > long, bool, etc). These can't ever be elementwise in CPython (I don't > know that this holds in other interpreters). > I'll hammer my point in again. "foo~(x)" is elementwise. "typeofx(foo(ElementwiseProxy(x)))" *may* be elementwise, depending on whether foo is a special case, or uses a special case, or is an obscure bug if it uses both a special case and a non special case. "x~.foo" is an elementwise "getattr" on a list of things. "ElementwiseProxy(x).foo" is elementwise or a *ramble ramble* *exceptions* *special cases*. --------------------------------------------------- To wrap this up, one of your points was style and I disagree, two were implementation agnostic and thus not advantages to yours, and one was a plausible but rare use-case where I feel I need an example. Additionally, your implementation technique has several seemingly unavoidable yet inexplicable behaviours. The idea is that you use ElementwiseProxy(x) or preferably x.each, > work with your data in an element-wise capacity as needed, then list() > or iter() back out. I think it should be a deliberate change of > context. Interleaving the proxy (say via x.each) in mostly scalar > expressions works, but wasn't the use case I designed it for, and in > those circumstances it doesn't really offer anything above the map > function. > > Nathan > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Wed Dec 21 11:55:58 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 16:55:58 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> Message-ID: On 21 December 2011 15:48, Neal Becker wrote: > Clarification: where can packing/unpacking syntax be used? > > It would be great if it were valid essentially anywhere (not limited to > parameter passing). > > What about constructs like: > > a, @tuple tail, b = sequence? >>> a, *b, c = range(10) >>> a, b, c (0, [1, 2, 3, 4, 5, 6, 7, 8], 9) It's just a proposed syntax change, effectively, so yes. However, http://bugs.python.org/issue2292 covers the ones we don't have (that we should :P). This discussion doesn't cover that. > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Wed Dec 21 11:57:57 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 11:57:57 -0500 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: +1 for IPython/%edit using the simplest editor that supports syntax highlighting and line numbers. I have found that Exploring/Prototyping in the interpreter has the highest ROI of anything I teach people. Nathan From pfdubois at gmail.com Wed Dec 21 12:07:28 2011 From: pfdubois at gmail.com (Paul Dubois) Date: Wed, 21 Dec 2011 09:07:28 -0800 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: You're reinventing Numeric Python. On Tue, Dec 20, 2011 at 11:45 AM, Nathan Rice < nathan.alexander.rice at gmail.com> wrote: > Elementwise provides a proxy object for iterables which supports > chained method calls, as well as elementwise expressions and some > built-in functions. > > Example: > > class ExampleList(ElementwiseProxyMixin, list): > def __new__(cls, iterable): > return list.__new__(cls, iterable) > foo = ExampleList([1, 2, 3, 4]) > > # You could also do: efoo = ElementwiseProxy(foo) > efoo = foo.each > > assert list(efoo.bit_length()) == [1, 2, 2, 3] > print "bit length: ", list(efoo.bit_length()) > assert list(efoo + 1) == [2, 3, 4, 5] > print "with addition of 1: ", list(efoo + 1) > assert list(efoo * 2) == [2, 4, 6, 8] > print "with multiplication by 2: ", list(efoo * 2) > assert list(efoo == 2) == [False, True, False, False] > print "testing equality: ", efoo == 2 > assert list((efoo + 1) * 2 + 3) == [7, 9, 11, 13] > print "chaining addition and multiplication: ", (efoo + 1) * 2 + 3 > > Each ElementwiseProxy also has a "parent" attribute so you can > backtrack in the chain as needed rather than store each intermediate > value, if you think you might need them. > > There are still some issues with proper support of things like bool() > and int(), which refuse to return things that are not of the correct > type. > > Get it: > > PyPi: http://pypi.python.org/pypi/elementwise/0.111220 > GitHub: https://github.com/nathan-rice/Elementwise > > This was developed as a proof of concept for expanding the role of > element-wise syntax in python, and to that end I welcome comments. > > Nathan Rice > -- > http://mail.python.org/mailman/listinfo/python-announce-list > > Support the Python Software Foundation: > http://www.python.org/psf/donations/ > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Wed Dec 21 12:22:20 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 12:22:20 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 12:07 PM, Paul Dubois wrote: > You're reinventing Numeric Python. I prefer to think of it in terms of paying homage to NumPy (and functional programming). A big part of the motivation for this was bringing the elegance of NumPy to other areas of python besides numerical/scientific programming. Nathan From ethan at stoneleaf.us Wed Dec 21 12:22:28 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 21 Dec 2011 09:22:28 -0800 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: <4ef12831$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef0f03d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef0f781$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef12831$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EF215D4.70309@stoneleaf.us> Steven D'Aprano wrote: > I'm just glad that you've put your money > where your mouth is, and released the package, instead of demanding > others do the work. Thank you. +1000! ~Ethan~ From python.list at tim.thechases.com Wed Dec 21 12:28:46 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 21 Dec 2011 11:28:46 -0600 Subject: Anyone still using Python 2.5? In-Reply-To: References: <4ef1b9fa$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EF2174E.8040900@tim.thechases.com> On 12/21/11 07:07, Roy Smith wrote: > In article<4ef1b9fa$0$29973$c3e8da3$5496439d at news.astraweb.com>, > Steven D'Aprano wrote: > >> Centos and Red Hat production systems still use Python 2.4, so yes, >> absolutely, 2.5 and 2.4 still need to be supported. > > Is Python 2.4 destined to be the next IE-6? No...unlike IE6, 2.4 backwards compatibility has a foreseeable death when RHEL+2.6 eventually fall out of support ;-) -tkc From andreas.perstinger at gmx.net Wed Dec 21 12:42:22 2011 From: andreas.perstinger at gmx.net (Andreas Perstinger) Date: Wed, 21 Dec 2011 18:42:22 +0100 Subject: Learning Python 2.4 In-Reply-To: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> References: <4ef0d49c$0$12187$4d5ecec7@news.xsusenet.com> Message-ID: <4EF21A7E.4010602@gmx.net> On 2011-12-20 19:31, kimma wrote: > I am about to learn python with "how to think like a computer > scientist". This book is just available for python 2.4. Does it matter > for programming? There is also a online-version for Python 3 but there are some differences: http://openbookproject.net/thinkcs/python/english3e/preface3-rle.html Bye, Andreas From woooee at gmail.com Wed Dec 21 12:45:46 2011 From: woooee at gmail.com (woooee) Date: Wed, 21 Dec 2011 09:45:46 -0800 (PST) Subject: Why widgets become 'NoneType'? References: <2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com> Message-ID: The error is with the labels not the canvas. All labels will have an id of "None" as that is what pack() returns. ? ? ? ? lbl = Label(win, text=astr[i]).pack(side=LEFT ) ? ? ? ? labels.append(lbl) The error will come up in the config_labels function when the program tries to config a tuple of "None", if not before. Other than that, the previous suggestion, post the entire error message so we know where an what is happening, is required to debug further. From ian.g.kelly at gmail.com Wed Dec 21 12:51:37 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Dec 2011 10:51:37 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 9:48 AM, Joshua Landau wrote: > functions, that before you were doing on a single item. > BEFORE: > item = foreignfunc1(item) > item = foreignfunc2(item) > item = foreignfunc3(item) > > NOW (your method): > item = ElementwiseProxy(item) > item = foreignfunc1(item) > item = foreignfunc2(item) > item = foreignfunc3(item) > item = list(item) Shouldn't that be: item = ElementwiseProxy(item) item = item.apply(foreignfunc1) item = item.apply(foreignfunc2) item = item.apply(foreignfunc3) item = list(item) > You might think your method works. But what if foreignfunc is "str"? And you > can't blacklist functions.?You can't say everything works bar A, B and C. > What if you get: "lambda x:str(x)"? You can't blacklist that. That makes the > ElementwiseProxy version buggy and prone to unstable operation. If it's > consistent, fine. But it's not. I'm not sure I understand this criticism. There is no "blacklisting" going on that I am aware of. The behavior seems consistent to me: Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from elementwise import * >>> ewp = ElementwiseProxy(range(5)) >>> str(ewp) '0, 1, 2, 3, 4' >>> (lambda x: str(x))(ewp) '0, 1, 2, 3, 4' >>> ewp.apply(str) '0', '1', '2', '3', '4' >>> ewp.apply(lambda x: str(x)) '0', '1', '2', '3', '4' All of which is exactly what I expected to get. Cheers, Ian From arnodel at gmail.com Wed Dec 21 12:53:06 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Wed, 21 Dec 2011 17:53:06 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 00:24, Nathan Rice wrote: > efoo2 = ElementwiseProxy(["one", "two", "three", "four"]) > > efoo_res = ((efoo2.capitalize() + " little indian").split(" > ").apply(reversed) * 2).apply("_".join) # note that you could do > reversed(...) instead, I just like to read left to right > efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " > little indian").split(" ") in case you need to debug something and > want to look at intermediate values You can already do: efoo2 = ["one", "two", "three", "four"] ["_".join(reversed((x.capitalize() + " little indian").split(" ")) * 2) for x in efoo2] Note 1: I've ignored the fact that reversed(...)*2 is erroneous Note 2: I wouldn't such code myself, in either form What's the advantage of your elementwise stuff? -- Arnaud From ian.g.kelly at gmail.com Wed Dec 21 12:54:40 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Dec 2011 10:54:40 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Tue, Dec 20, 2011 at 12:45 PM, Nathan Rice wrote: > Elementwise provides a proxy object for iterables which supports > chained method calls, as well as elementwise expressions and some > built-in functions. It doesn't seem to work correctly when both operands are Elementwise: >>> numerator = ElementwiseProxy(range(5)) >>> denominator = ElementwiseProxy([2, 2, 3, 3, 3]) >>> remainder = numerator % denominator >>> print remainder Traceback (most recent call last): File "", line 1, in File "elementwise.py", line 72, in __repr__ return ", ".join(e.__repr__() for e in object.__getattribute__(self, "iterable")) TypeError: sequence item 0: expected string, ElementwiseProxy found >>> list(remainder) [] Cheers, Ian From nathan.alexander.rice at gmail.com Wed Dec 21 13:09:01 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 13:09:01 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: >> On Tue, Dec 20, 2011 at 8:37 PM, Joshua Landau >> wrote: >> > On 21 December 2011 00:24, Nathan Rice >> > wrote: >> >> efoo_res = ((efoo2.capitalize() + " little indian").split(" >> >> ").apply(reversed) * 2).apply("_".join) # note that you could do >> >> reversed(...) instead, I just like to read left to right >> >> efoo_res.parent.parent.parent # same as ((efoo2.capitalize() + " >> >> little indian").split(" ") in case you need to debug something and >> >> want to look at intermediate values >> > >> > How is any of this better than the elementwise operators ("~")? People >> > should be able to expect len(x) to always?return a number or raise an >> > error. >> > I know it's not part of the spec, but a lot breaks without these >> > guarantees. >> > When "str(x)" isn't a string, all the formatting code breaks*. And when >> > the >> > other suggestion ("~str(x)" or "str~(x)" or something similar) has all >> > the >> > benifits and none of the drawbacks, why should I use this? >> >> len() will always return a number or raise an error, just like the >> type functions (bool/int/etc) return that type or raise an error. ?The >> interpreter guarantees that for you. > > > The point wasn't that either way was better, but that with this > implementation you get neither choice ("len(x)" vs "len~(x)") or > reliability. If len didn't have the hard coded behavior, you would have the choice of len(x) or len(x.each). Since a lot of code relies on len() returning an it, I think it is fine to accept that you have to use x.each.apply(len). I agree that this is a case where some kind of elementwise designation in the syntax would be better; if there was a ? character on the keyboard and more people knew what it meant I would have fewer reservations. > > The?reliability?point works like this:?You want to elementwise a few > functions, that before you were doing on a single item. > BEFORE: > item = foreignfunc1(item) > item = foreignfunc2(item) > item = foreignfunc3(item) > > NOW (your method): > item = ElementwiseProxy(item) > item = foreignfunc1(item) > item = foreignfunc2(item) > item = foreignfunc3(item) > item = list(item) well, I would say it more like: item.each.apply(foreignfunc1).apply(foreignfunc2).apply(foreignfunc3) # I like to read left to right, what can I say? and I wouldn't list() it right away, since it is nice and lazy. > You might think your method works. But what if foreignfunc is "str"? And you > can't blacklist functions.?You can't say everything works bar A, B and C. > What if you get: "lambda x:str(x)"? You can't blacklist that. That makes the > ElementwiseProxy version buggy and prone to unstable operation. If it's > consistent, fine. But it's not. I don't need to blacklist anything. Everything that has funny behavior like str goes through special methods on the class (that I know of), and isn't hooked through __getattribute__, so I just handle it somewhat normally. >> 1. Because everything is handled in terms of generator chains, all >> operations on an ElementwiseProxy are evaluated lazily. ?With >> element-wise operator overloading you would need to perform each >> operation immediately. > > > I agree this can be a preferred advantage.But you still have to choose one. > "~" could be lazy, or it could be eager. But in both implementations you > have to choose. That said, you have map and imap, and so you could have > ElemetwiseProxy and eElementwiseProxy (eager), and you can have "~" and > "i~". Remember that the syntax I'm using is just for PEP consistency. Some > more creative people can find a syntax that works. >From my perspective the strength of operators is that they are intuitive, since we use them constantly in other areas; I can make a good guess about what X * Y or X + Y means in various contexts. When you introduce operators that don't have any well ingrained, standard meaning, you just make the syntax cryptic. >> >> 2. As a result of #1, you can "undo" operations you perform on an >> ElementwiseProxy with the parent property. > > > Use case? If this is actually a wanted feature, "parent" could be made a > general property of iterators. > (x for x in foo).parent == foo > I think that's a?separate?proposal that isn't intrinsic to this idea. One quick use case: I want to debug something big, slow and nasty; . I set breakpoints with conditions where it seems like the issue lies from the stack trace. Unfortunately I missed the root cause, and the variables that would help me debug it have been garbage collected. I can go through the entire process again, try to set better breakpoints and cross my fingers, or iterate over some subset of parent operations right there. I think I'll take the latter. >> 3. This still works if the person who created the class you're working >> with doesn't add support for element-wise operators. ?Sure, you could >> monkey patch their code, but that can lead to other problems down the >> line. As I understood it, the elementwise operators in the PEP weren't language magic, but new hooks to special methods. If it is language magic, that will probably have very far reaching implications in the code. >> >> 4. There isn't an obvious/intuitive character for element-wise >> versions of operators, and fewer symbols is better than more IMHO >> (see: Perl). ?Also, if you use the ElementwiseProxyMixin, you can >> sprinkle element-wise stuff in neatly just by using "variable.each" >> where you would use "~" in your examples. > > > Obvious/intuitive is meaningless for something so potentially common. "**" > is far more obscure ("*" is obscure enough). Why is a double star the > exponent character? Because it works. "~" would be easy to learn and > consistent, once a standard was agreed. And again, I'm only using that > syntax because it's the one in the PEP. We're not going to get to Perl, I > hope, with this addition. I agree ** is pretty bad (xor should not have gotten first dibs on ^). The only saving grace for this scheme is that it is to some degree a convention in programming. > Explicit > Implicit. If someone passes you a "variable.each", things go > haywire (see: first section of reply). "~" is explicit. It doesn't have > these problems. I also think explicit looks nicer. 'Cause it is, y'know :P Sure, I can't prevent people from doing bad things though. People will always find ways to shoot themselves (and potentially others around them) in the foot. > I'll hammer my point in again. > > "foo~(x)" is elementwise. > "typeofx(foo(ElementwiseProxy(x)))" may?be elementwise, depending on whether > foo is a special case, or uses a special case, or is an obscure bug if it > uses both a special case and a non special case. > > "x~.foo" is an elementwise "getattr" on a list of things. > "ElementwiseProxy(x).foo" is elementwise or a *ramble ramble* *exceptions* > *special cases*. I agree that having elementwise functionality at a deeper level in python would avoid some of the inconsistencies (of which there are really not very many!) Nathan From nathan.alexander.rice at gmail.com Wed Dec 21 13:16:28 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 13:16:28 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 12:53 PM, Arnaud Delobelle wrote: > > You can already do: > > efoo2 = ["one", "two", "three", "four"] > ["_".join(reversed((x.capitalize() + " little indian").split(" ")) * 2) > ? ? for x in efoo2] > > Note 1: I've ignored the fact that reversed(...)*2 is erroneous > Note 2: I wouldn't such code myself, in either form > > What's the advantage of your elementwise stuff? Sit two theoretically identical programmers who have a decent grasp of python, understand the basics of ElementwiseProxy and the components of your version down side by side, and see which one can figure out what the output should be first. I will bet real money it is the one working with the ElementwiseProxy code. We have all had a lot of practice reading inside out and backwards in the python community with comprehensions and such, but it is in no way natural or easy. Additionally, everything done via an ElementwiseProxy is lazy, so you only pay for what you eat :) Nathan From joshua.landau.ws at gmail.com Wed Dec 21 13:24:19 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 18:24:19 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 17:51, Ian Kelly wrote: > On Wed, Dec 21, 2011 at 9:48 AM, Joshua Landau > wrote: > > functions, that before you were doing on a single item. > > BEFORE: > > item = foreignfunc1(item) > > item = foreignfunc2(item) > > item = foreignfunc3(item) > > > > NOW (your method): > > item = ElementwiseProxy(item) > > item = foreignfunc1(item) > > item = foreignfunc2(item) > > item = foreignfunc3(item) > > item = list(item) > > Shouldn't that be: > > item = ElementwiseProxy(item) > item = item.apply(foreignfunc1) > item = item.apply(foreignfunc2) > item = item.apply(foreignfunc3) > item = list(item) I was under the impression that these were meant to be interchangeable. This is because functions are just wrappers to non-functions, really. >>> from elementwise import ElementwiseProxy as P >>> (lambda x:x+[1])(P([[0],[0],[0]])) [0, 1], [0, 1], [0, 1] If we have to use .apply, we might as well use map :P. Note that "len" and "dir" crash. *Here is a perfect example:* >>> int(P(["1","2","3"])) Traceback (most recent call last): File "", line 1, in *TypeError: __int__ returned non-int (type ElementwiseProxy)* It's elementwise, *but that breaks it.* An elementwise operator wouldn't have these problems. > You might think your method works. But what if foreignfunc is "str"? And > you > > can't blacklist functions. You can't say everything works bar A, B and C. > > What if you get: "lambda x:str(x)"? You can't blacklist that. That makes > the > > ElementwiseProxy version buggy and prone to unstable operation. If it's > > consistent, fine. But it's not. > > I'm not sure I understand this criticism. There is no "blacklisting" > going on that I am aware of. The behavior seems consistent to me: > > Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> from elementwise import * > >>> ewp = ElementwiseProxy(range(5)) > >>> str(ewp) > '0, 1, 2, 3, 4' > >>> (lambda x: str(x))(ewp) > '0, 1, 2, 3, 4' > >>> ewp.apply(str) > '0', '1', '2', '3', '4' > >>> ewp.apply(lambda x: str(x)) > '0', '1', '2', '3', '4' > > All of which is exactly what I expected to get. > Again, str(x) in this context is an outlier. It's one of the exceptions to the rule of applying functions = elementwise. -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Wed Dec 21 13:27:28 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 13:27:28 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: > It doesn't seem to work correctly when both operands are Elementwise: > >>>> numerator = ElementwiseProxy(range(5)) >>>> denominator = ElementwiseProxy([2, 2, 3, 3, 3]) >>>> remainder = numerator % denominator >>>> print remainder > Traceback (most recent call last): > ?File "", line 1, in > ?File "elementwise.py", line 72, in __repr__ > ? ?return ", ".join(e.__repr__() for e in > object.__getattribute__(self, "iterable")) > TypeError: sequence item 0: expected string, ElementwiseProxy found >>>> list(remainder) > [] > > Cheers, > Ian Ian, can you clarify the expected output in that case? My initial guess would be that you want to do a modulo on the cartesian product of parameters from the proxies, sort of like: import itertools numerator = range(5) denominator = [2, 2, 3, 3, 3] remainder = [n % d for (n, d) in itertools.product(numerator, denominator)] Nathan From nathan.alexander.rice at gmail.com Wed Dec 21 13:32:03 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 13:32:03 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 1:24 PM, Joshua Landau wrote: > I was under the impression that these were meant to be interchangeable. This > is because functions are just wrappers to non-functions, really. > >>>> from elementwise import ElementwiseProxy as P >>>> (lambda x:x+[1])(P([[0],[0],[0]])) > [0, 1], [0, 1], [0, 1] > > If we have to use .apply, we might as well use map :P. Apply is not required, you can use functions. I just hate reading inside out and backwards. compare: func3(func2(func1(x))) # Fine if the chain is short, but quickly gets unreadable x.apply(func1).apply(func2).apply(func3) # More verbose when working with short chains, but infinitely clearer and more readable when things start getting complicated, or for less experienced coders > Note that "len" and "dir" crash. > > Here is a perfect example: >>>> int(P(["1","2","3"])) > Traceback (most recent call last): > ? File "", line 1, in > TypeError: __int__ returned non-int (type ElementwiseProxy) It is alpha software :P Easily fixed though. Nathan From ian.g.kelly at gmail.com Wed Dec 21 13:39:07 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Dec 2011 11:39:07 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 11:24 AM, Joshua Landau wrote: > I was under the impression that these were meant to be interchangeable. This > is because functions are just wrappers to non-functions, really. > >>>> from elementwise import ElementwiseProxy as P >>>> (lambda x:x+[1])(P([[0],[0],[0]])) > [0, 1], [0, 1], [0, 1] > > If we have to use .apply, we might as well use map :P. Agreed. The advantage of .apply here is that when the functions are nested they are written left-to-right. But I wouldn't be using multiple maps or applys on a single line anyway -- my goal is to write clear code, not great one-liners. :-D From ian.g.kelly at gmail.com Wed Dec 21 13:41:37 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Dec 2011 11:41:37 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 11:27 AM, Nathan Rice wrote: > Ian, can you clarify the expected output in that case? ?My initial > guess would be that you want to do a modulo on the cartesian product > of parameters from the proxies, sort of like: > > import itertools > numerator = range(5) > denominator = [2, 2, 3, 3, 3] > > remainder = [n % d for (n, d) in itertools.product(numerator, denominator)] I expected the equivalent of: remainder = [n % d for (n, d) in zip(numerator, denominator)] which is what numpy does. From ethan at stoneleaf.us Wed Dec 21 13:48:41 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 21 Dec 2011 10:48:41 -0800 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: <4EF22A09.2070908@stoneleaf.us> Ian Kelly wrote: > my goal is to write clear code, not great one-liners. :-D +1 QOTW From garrickp at gmail.com Wed Dec 21 13:48:49 2011 From: garrickp at gmail.com (Falcolas) Date: Wed, 21 Dec 2011 10:48:49 -0800 (PST) Subject: Multiprocessing.Pipe in a daemon Message-ID: So, I'm running into a somewhat crazy bug. I am running several workers using multiprocessing to handle gearman requests. I'm using pipes to funnel log messages from each of the workers back to the controlling process, which iterates over the other end of the pipe, looking for messages with poll(), and logging them using the parent's log handler. This works really well when I'm not daemonizing the entire thing. However, when I daemonize the process (which happens well prior to any setup of the pipes & multiprocess.Process'es), a pipe which has nothing in it return True for the poll(), and then blocks on the pipe.recv() command. The gearman workers are still operational and responsive, and only starting one worker resolves the problem. Has anybody seen anything like this? #Trim # Create all of the end points endpoints = [] log_pipes = [] for w_num in range(5): (recv, snd) = multiprocessing.Pipe(False) # Start the worker logger.debug("Creating the worker {0}".format(w_num)) worker = Worker(w_num, job_name, gm_servers, snd) # Add the worker to the list of endpoints so it can be started endpoints.append(worker) log_pipes.append(recv) # Trim starting endpoints try: while True: time.sleep(1) pipe_logger(logger, log_pipes) except (KeyboardInterrupt, SignalQuit): pass # Trim cleanup def pipe_logger(logger_obj, pipes): done = False while not done: done = True for p in pipes: if p.poll(): # <-- Returning true after a previous pipe actually had data try: log_level, log_msg = p.recv() # <-- Hanging here except EOFError: continue done = False From joshua.landau.ws at gmail.com Wed Dec 21 13:52:37 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 18:52:37 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 18:32, Nathan Rice wrote: > On Wed, Dec 21, 2011 at 1:24 PM, Joshua Landau > wrote: > > I was under the impression that these were meant to be interchangeable. > This > > is because functions are just wrappers to non-functions, really. > > > >>>> from elementwise import ElementwiseProxy as P > >>>> (lambda x:x+[1])(P([[0],[0],[0]])) > > [0, 1], [0, 1], [0, 1] > > > > If we have to use .apply, we might as well use map :P. > > Apply is not required, you can use functions. I just hate reading > inside out and backwards. compare: > > func3(func2(func1(x))) # Fine if the chain is short, but quickly gets > unreadable > > x.apply(func1).apply(func2).apply(func3) # More verbose when working > with short chains, but infinitely clearer and more readable when > things start getting complicated, or for less experienced coders I wan't disagreeing with that, hence the word "if". I was just making a counter-counter point to a misunderstanding of my post. > > Note that "len" and "dir" crash. > > > > Here is a perfect example: > >>>> int(P(["1","2","3"])) > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: __int__ returned non-int (type ElementwiseProxy) > > It is alpha software :P Easily fixed though. > If fix means "return number" then no. This inconsistency between elementwise operations and 'normal' ones is the problem. Again, explicit elementwise-ifying through "~" fixes that. You need to tell me why this is worth the confusion over that. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Wed Dec 21 13:57:05 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 21 Dec 2011 11:57:05 -0700 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 9:48 AM, Joshua Landau wrote: > NOW (the PEP): > item = foreignfunc1~(item) > item = foreignfunc2~(item) > item = foreignfunc3~(item) Just a note: PEP 225 does not actually include this syntax for arbitrary function calls. It only proposes to augment the arithmetic and assignment operators. From bahamutzero8825 at gmail.com Wed Dec 21 13:59:45 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 21 Dec 2011 12:59:45 -0600 Subject: Why does this launch an infinite loop of new processes? Message-ID: <4EF22CA1.1070200@gmail.com> I am trying to understand the multiprocessing module, and I tried some simple code: import multiprocessing def f(): print('bla bla') p = multiprocessing.Process(target=f) p.start() p.join() And the result is a new process that spawns a new process that spawns a new process ad infinitum until I log out and the OS forcefully terminates all my user processes. I have no idea what is happening; I expected it to just print the string and terminate. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From joshua.landau.ws at gmail.com Wed Dec 21 14:00:38 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 19:00:38 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 18:57, Ian Kelly wrote: > On Wed, Dec 21, 2011 at 9:48 AM, Joshua Landau > wrote: > > NOW (the PEP): > > item = foreignfunc1~(item) > > item = foreignfunc2~(item) > > item = foreignfunc3~(item) > > Just a note: PEP 225 does not actually include this syntax for > arbitrary function calls. It only proposes to augment the arithmetic > and assignment operators. > Not quite. They never seem to have come to an agreement (the whole thing is deferred) but they do touch upon "~f(x)": 5. Using ~ as generic elementwise meta-character to replace map ~f(a, b) # map(f, a, b) ~~f(a, b) # map(lambda *x:map(f, *x), a, b) More generally, def ~f(*x): return map(f, *x) def ~~f(*x): return map(~f, *x) ... I used "f~(x)" because it's not ambiguous with the current binary not operator. -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Wed Dec 21 14:03:08 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 19:03:08 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 21 December 2011 19:00, Joshua Landau wrote: > On 21 December 2011 18:57, Ian Kelly wrote: > >> On Wed, Dec 21, 2011 at 9:48 AM, Joshua Landau >> wrote: >> > NOW (the PEP): >> > item = foreignfunc1~(item) >> > item = foreignfunc2~(item) >> > item = foreignfunc3~(item) >> >> Just a note: PEP 225 does not actually include this syntax for >> arbitrary function calls. It only proposes to augment the arithmetic >> and assignment operators. >> > > Not quite. They never seem to have come to an agreement (the whole thing > is deferred) but they do touch upon "~f(x)": > > > 5. Using ~ as generic elementwise meta-character to replace map > > ~f(a, b) # map(f, a, b) > ~~f(a, b) # map(lambda *x:map(f, *x), a, b) > > More generally, > > def ~f(*x): return map(f, *x) > def ~~f(*x): return map(~f, *x) > ... > > I used "f~(x)" because it's not ambiguous with the current binary not operator. > > Apologies for the double post but I think it's worth mentioning that I did make up the "list~.method" syntax. -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Wed Dec 21 14:04:16 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 14:04:16 -0500 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 1:41 PM, Ian Kelly wrote: > > I expected the equivalent of: > > remainder = [n % d for (n, d) in zip(numerator, denominator)] > > which is what numpy does. I do want to come up with a nice way to do that... However: if numerator and denominator are plain lists that magically have an each method... each_numerator = numerator.each each_denominator = denominator.each each_numerator % each_denominator see where I'm going with the mismatch on the semantics? I know the example is a bit contrived, sorry. Expanding iterators by default might be a worth a shot though, since passing around iterators like that is pretty rare. Nathan From joshua.landau.ws at gmail.com Wed Dec 21 14:07:31 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 19:07:31 +0000 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: <4EF22CA1.1070200@gmail.com> References: <4EF22CA1.1070200@gmail.com> Message-ID: On 21 December 2011 18:59, Andrew Berg wrote: > I am trying to understand the multiprocessing module, and I tried some > simple code: > > import multiprocessing > def f(): > print('bla bla') > p = multiprocessing.Process(target=f) > p.start() > p.join() > > And the result is a new process that spawns a new process that spawns a > new process ad infinitum until I log out and the OS forcefully > terminates all my user processes. I have no idea what is happening; I > expected it to just print the string and terminate. Eh? It works for me. Python 3.2 + 2.7 Is this the full code? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dean at red-spanner.com Wed Dec 21 14:12:10 2011 From: dean at red-spanner.com (Dean Richardson P.Eng) Date: Wed, 21 Dec 2011 12:12:10 -0700 Subject: Newbie OOP Question & Code Snippet Message-ID: Hi All, I'm a newbie just learning Python, using a couple of books to learn the language. (Books: "Visual Quickstart Guide - Python, 2nd Ed", "Practical Programming - An Intro to Computer Science using Python"). I'm just now learning OOP material as presented in both books -- I'm new to this approach -- the last formal language I learned was Fortran77 -- :o) I am running Python 3.2.1 on Mac OS/X 10.6.8. My problem stems from a simple example in the Visual Quickstart book. The code is: ---------------------------- #person.py class Person: """Class to represent a person""" def __init__(self): self.name=' ' self.age=0 def display(self): print("Person('%s', age)" % (self.name, self.age)) ------------------------- When I run this, the shell presents thus: >>> ================================ RESTART ================================ >>> >>> p=Person() >>> p.name='Bob' >>> p.age=24 >>> p.display() Traceback (most recent call last): File "", line 1, in p.display() File "/Volumes/dean_richardson/GoFlex Home Personal/Dean's Code/Python3.x/Person.py", line 9, in display (self.name, self.age)) TypeError: not all arguments converted during string formatting >>> --------------- I'm sure this is something simple, but I can't see it. Any help appreciated! Regards, Dean -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Wed Dec 21 14:13:53 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 21 Dec 2011 13:13:53 -0600 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: References: <4EF22CA1.1070200@gmail.com> Message-ID: <4EF22FF1.5040709@gmail.com> On 12/21/2011 1:07 PM, Joshua Landau wrote: > Eh? It works for me. Python 3.2 + 2.7 > Is this the full code? That is the full code. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From happybrowndog at hotmail.com Wed Dec 21 14:19:59 2011 From: happybrowndog at hotmail.com (hbd666) Date: Wed, 21 Dec 2011 11:19:59 -0800 Subject: Need advice on the design of my application Message-ID: I am writing a hull-wave simulator for the design of boat hulls as they are affected by waves. This application is composed of 2 main parts, the part that renders the waves and its impact on the hull, and a GUI that controls the hull shape, waves, and other factors. The two parts of the application will need to exchange data, for example, the GUI will set the hull shape and this will be displayed by the renderer. The renderer will show the waves and it will tell the GUI statistics about the waves as they are being animated. This application will run on Windows. The Renderer part of the application must be written in C because it needs to do very fast numerical calculations and requires the use of OpenGL and must render very quickly. The GUI part of the application will be wxPython. The options for constructing the application that I have thought of are: 1) The wxPython part launches the Renderer on a thread, and the Renderer is a DLL (provided a DLL can display graphics). 2) The Renderer starts a thread where the Python interpreter is embedded, which launches wxPython. 3) The Renderer and the GUI are actually two separate applications that communicate with each other over a socket. My last resort is Option 3 because communications can be a hassle. Option 1 seems to be the easiest but will be very difficult to debug the Renderer using my IDE because it is a DLL, if a DLL can display graphics at all. Option 2 seems to be the most feasible both in terms of debugging and displaying graphics. I have concerns about the Python GIL and how that will affect the way this program will work. The Renderer is going to be running a tight loop. The GUI under wxPython also runs a main loop internally, but has the ability to put function calls on a sort of stack to be executed without interrupting its main loop. In my experience implementing Option 1 in another project, I know that Python suspends execution until the DLL function calls return, but I did not launch the DLL on a thread. I expect that if the DLL were launched on a thread, a function call into the DLL will still suspend Python. Maybe someone can tell me if this is true? Option 2 is of most interest to me, but how shall I handle the Python GIL when the Renderer runs its main loop? Will the main loop be unaffected because Python interpreter is embedded in a thread? From ethan at stoneleaf.us Wed Dec 21 14:29:43 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 21 Dec 2011 11:29:43 -0800 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: <4EF22CA1.1070200@gmail.com> References: <4EF22CA1.1070200@gmail.com> Message-ID: <4EF233A7.6020907@stoneleaf.us> Andrew Berg wrote: > I am trying to understand the multiprocessing module, and I tried some > simple code: > > import multiprocessing > def f(): > print('bla bla') > p = multiprocessing.Process(target=f) > p.start() > p.join() > > And the result is a new process that spawns a new process that spawns a > new process ad infinitum until I log out and the OS forcefully > terminates all my user processes. I have no idea what is happening; I > expected it to just print the string and terminate. > Anything that runs at import time should be protected by the `if __name__ == '__main__'` idiom as the children will import the __main__ module. 8<----------------------------------------------- import multiprocessing def f(): print('bla bla') if __name__ == '__main__': p = multiprocessing.Process(target=f) p.start() p.join() 8<----------------------------------------------- ~Ethan~ From python at mrabarnett.plus.com Wed Dec 21 14:31:05 2011 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 21 Dec 2011 19:31:05 +0000 Subject: Newbie OOP Question & Code Snippet In-Reply-To: References: Message-ID: <4EF233F9.8000902@mrabarnett.plus.com> On 21/12/2011 19:12, Dean Richardson P.Eng wrote: > Hi All, > I'm a newbie just learning Python, using a couple of books to learn the > language. (Books: "Visual Quickstart Guide - Python, 2nd Ed", "Practical > Programming - An Intro to Computer Science using Python"). I'm just now > learning OOP material as presented in both books -- I'm new to this > approach -- the last formal language I learned was Fortran77 -- :o) I > am running Python 3.2.1 on Mac OS/X 10.6.8. > > My problem stems from a simple example in the Visual Quickstart book. > The code is: > ---------------------------- > #person.py > class Person: > """Class to represent a person""" > def __init__(self): > self.name=' ' > self.age=0 > def display(self): > print("Person('%s', age)" % > (self.name, self.age)) > ------------------------- > When I run this, the shell presents thus: > >>> ================================ RESTART > ================================ > >>> > >>> p=Person() > >>> p.name ='Bob' > >>> p.age=24 > >>> p.display() > Traceback (most recent call last): > File "", line 1, in > p.display() > File "/Volumes/dean_richardson/GoFlex Home Personal/Dean's > Code/Python3.x/Person.py", line 9, in display > (self.name , self.age)) > TypeError: not all arguments converted during string formatting > >>> > --------------- > I'm sure this is something simple, but I can't see it. Any help appreciated! > It should be: print("Person('%s', %s)" % (self.name, self.age)) As it is, you're giving it 2 values but only %s placeholder. From ckaynor at zindagigames.com Wed Dec 21 14:37:34 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Wed, 21 Dec 2011 11:37:34 -0800 Subject: Need advice on the design of my application In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 11:19 AM, hbd666 wrote: > > > In my experience implementing Option 1 in another project, I know that > Python suspends > execution until the DLL function calls return, but I did not launch the > DLL on a thread. > I expect that if the DLL were launched on a thread, a function call into > the DLL will still > suspend Python. Maybe someone can tell me if this is true? > > Option 2 is of most interest to me, but how shall I handle the Python GIL > when the Renderer > runs its main loop? Will the main loop be unaffected because Python > interpreter is embedded > in a thread? When extending Python (as option 1 would do), the extension would block all Python threads if called from Python (though c-threads created by the module would not block Python). You can explicitly release the GIL during your C code's execution, which would cause only the thread actually making the call to be blocked (as it is actually running the C code), but other Python threads would still be able to run. When embedding Python (as option 2 would do), the embedding program will not be affected by the GIL unless it explicitly acquires the GIL (which is must do before accessing Python). Via the Python API functions you can acquire and release the Python GIL as needed. The only rule is that you must hold the Python GIL should you wish to access any part of the Python interpreter (function calls, variables, etc) [note: technically you don't HAVE to, but you run a high risk of data corruption and segfaults from the race conditions which would exist]. Either option should work fine. In case of option 1: 1. Import the C module. 2. The C module sets up some defaults for the render. 3. The C module creates its render thread (which is unaffected by the GIL, but probably needs other locks around global variables). 4. The C module returns to Python. 5. The Python GUI calls into the C module to change settings (which probably need to acquire short-term locks) An alternative would be: 1. Import the C module. 2. The C module setups up some defaults. 3. Create a Python thread to call the C module's render loop. 4. The render loop thread releases the GIL and continues as above. 5. The main Python thread continues as above. For option 2: 1. Initialize any needed variables for the system. 2. Start a new thread for loading and executing Python. 3. Have the main thread start the render loop. 4. Have the Python thread startup up Python. 5. Continue as in the first case above. > > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joshua.landau.ws at gmail.com Wed Dec 21 14:38:11 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 19:38:11 +0000 Subject: Newbie OOP Question & Code Snippet In-Reply-To: References: Message-ID: On 21 December 2011 19:12, Dean Richardson P.Eng wrote: > Hi All, > I'm a newbie just learning Python, using a couple of books to learn the > language. (Books: "Visual Quickstart Guide - Python, 2nd Ed", "Practical > Programming - An Intro to Computer Science using Python"). I'm just now > learning OOP material as presented in both books -- I'm new to this > approach -- the last formal language I learned was Fortran77 -- :o) I am > running Python 3.2.1 on Mac OS/X 10.6.8. > > My problem stems from a simple example in the Visual Quickstart book. The > code is: > ---------------------------- > #person.py > class Person: > """Class to represent a person""" > def __init__(self): > self.name=' ' > self.age=0 > def display(self): > print("Person('%s', age)" % > (self.name, self.age)) > The "% (self.name, self.age)" means that two arguments are given in. You only have one "%s" in the string (untested, so I may be completely off). Additionally, use str.format now: "Person({}, {})".format(self.name, self.age) > ------------------------- > When I run this, the shell presents thus: > >>> ================================ RESTART > ================================ > >>> > >>> p=Person() > >>> p.name='Bob' > >>> p.age=24 > >>> p.display() > Traceback (most recent call last): > File "", line 1, in > p.display() > File "/Volumes/dean_richardson/GoFlex Home Personal/Dean's > Code/Python3.x/Person.py", line 9, in display > (self.name, self.age)) > TypeError: not all arguments converted during string formatting > >>> > --------------- > I'm sure this is something simple, but I can't see it. Any help > appreciated! > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Wed Dec 21 15:11:03 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 21 Dec 2011 14:11:03 -0600 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: <4EF233A7.6020907@stoneleaf.us> References: <4EF22CA1.1070200@gmail.com> <4EF233A7.6020907@stoneleaf.us> Message-ID: <4EF23D57.6010305@gmail.com> On 12/21/2011 1:29 PM, Ethan Furman wrote: > Anything that runs at import time should be protected by the `if > __name__ == '__main__'` idiom as the children will import the __main__ > module. So the child imports the parent and runs the spawn code again? That makes sense. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0 From Chris.Wesseling at cwi.nl Wed Dec 21 15:28:38 2011 From: Chris.Wesseling at cwi.nl (Chris.Wesseling at cwi.nl) Date: Wed, 21 Dec 2011 21:28:38 +0100 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: <20111221202838.GU532@cwi.nl> On 2011-12-21T07:15:46+0000, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > > Do people still have to use this in commercial environments or is > everyone on 2.6+ nowadays? 2.5, how modern. SUSE Linux Enterprise Server 10 comes with 2.4.2 Will be moving to a RHEL derivative running 2.6, though. The conservative enterprise will probably run py3k by y3k. :-( -- Chris -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 836 bytes Desc: not available URL: From invalid at invalid.invalid Wed Dec 21 16:02:27 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 21 Dec 2011 21:02:27 +0000 (UTC) Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> <9lef2pFgggU1@mid.individual.net> Message-ID: On 2011-12-21, Neil Cerutti wrote: > On 2011-12-21, Grant Edwards wrote: >> >>>> Templates are how C++ does generics and I'd expect them to >>>> appear in be used in embedded programming as well as elsewhere. >> >> I've only worked on the perphery of a couple embedded projects that >> used C++, but it seems to be farily common practice to prohibit the >> use of templates in embedded code. The problem seems to be that >> people writing C++ code don't really understand the language, how >> templates work, or (in general) the implications of the code they >> write. So, you have to prohibit anything that can be easily misused >> or cause non-obvious problems. > > I cheerfully agree that programmers ignorant of C++ should not > write programs in it. But furthermore, they should also not > define a subset of C++ for use in embedded programming. ;) I fully agree that programmers ignorant of C++ should not write programs in it. However C++ is a vast, complex, and dangerous language -- and industry doesn't seem to be willing to limit itself to using the seven people on the planet who understand it. I'm only half joking... :) It seems to me that you need to know far more about C++ to use it safely than you need to know about most other languages to use them safely. -- Grant Edwards grant.b.edwards Yow! Hey, waiter! I want at a NEW SHIRT and a PONY TAIL gmail.com with lemon sauce! From ethan at stoneleaf.us Wed Dec 21 16:15:35 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Wed, 21 Dec 2011 13:15:35 -0800 Subject: [OT] Quick intro to C++ for a Python and C user? In-Reply-To: References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> <9lef2pFgggU1@mid.individual.net> Message-ID: <4EF24C77.2060607@stoneleaf.us> Grant Edwards wrote: > On 2011-12-21, Neil Cerutti wrote: >> I cheerfully agree that programmers ignorant of C++ should not >> write programs in it. But furthermore, they should also not >> define a subset of C++ for use in embedded programming. ;) > > I fully agree that programmers ignorant of C++ should not write > programs in it. However C++ is a vast, complex, and dangerous > language -- and industry doesn't seem to be willing to limit itself to > using the seven people on the planet who understand it. > > I'm only half joking... :) Ah -- so there's actually 14? ;) ~Ethan~ From hansmu at xs4all.nl Wed Dec 21 17:08:19 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Wed, 21 Dec 2011 23:08:19 +0100 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: <4ef258d4$0$6981$e4fe514c@news2.news.xs4all.nl> On 21/12/11 01:03:26, Ian Kelly wrote: > As type conversion functions, bool(x) and > int(x) should *always* return bools and ints respectively > (or raise an exception), no matter what you pass in for x. That doesn't always happen in 2.x: >>> type(int(1e42)) This was fixed in 3.0. -- HansM From brett at yvrsfo.ca Wed Dec 21 17:08:31 2011 From: brett at yvrsfo.ca (Brett Cannon) Date: Wed, 21 Dec 2011 17:08:31 -0500 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: On Wed, Dec 21, 2011 at 05:57, Jim Fulton wrote: > On Wed, Dec 21, 2011 at 2:15 AM, Chris Withers > wrote: > > Hi All, > > > > What's the general consensus on supporting Python 2.5 nowadays? > > > > Do people still have to use this in commercial environments or is > everyone > > on 2.6+ nowadays? > > > > I'm finally getting some continuous integration set up for my packages > and > > it's highlighting some 2.5 compatibility issues. I'm wondering whether to > > fix those (lots of ugly "from __future__ import with_statement" > everywhere) > > or just to drop Python 2.5 support. > > > > What do people feel? > > Google app engine is still Python 2.5, as is Jython. But App Engine also supports Python 2.7: http://code.google.com/appengine/docs/python/python27/ (currently experimental, but then again App Engine itself was in preview mode until just this past month). -------------- next part -------------- An HTML attachment was scrubbed... URL: From miki.tebeka at gmail.com Wed Dec 21 17:10:10 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 21 Dec 2011 14:10:10 -0800 (PST) Subject: Need advice on the design of my application In-Reply-To: References: Message-ID: <32438051.312.1324505410308.JavaMail.geo-discussion-forums@yqdj19> I'd say go with option 1. Pass the window handler (or DC) to the rendered thread. To overcome the GIL problem, you can use Py_BEGIN_ALLOW_THREADS (see http://docs.python.org/release/1.5.2/api/threads.html) From miki.tebeka at gmail.com Wed Dec 21 17:10:10 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 21 Dec 2011 14:10:10 -0800 (PST) Subject: Need advice on the design of my application In-Reply-To: References: Message-ID: <32438051.312.1324505410308.JavaMail.geo-discussion-forums@yqdj19> I'd say go with option 1. Pass the window handler (or DC) to the rendered thread. To overcome the GIL problem, you can use Py_BEGIN_ALLOW_THREADS (see http://docs.python.org/release/1.5.2/api/threads.html) From ckaynor at zindagigames.com Wed Dec 21 17:17:23 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Wed, 21 Dec 2011 14:17:23 -0800 Subject: Need advice on the design of my application In-Reply-To: References: Message-ID: On Wed, Dec 21, 2011 at 2:10 PM, carlos choy wrote: > Thank you for your great advice. It is detailed and tells me what I need > to know, I wasn't expecting such an accurate response from anyone for some > time. > > I think Option 2 is the way I will go. Having never embedded before, I > think it will be interesting, besides then my IDE can help me debug more > easily than Option 1. > > Either way is a bit of a mixed bag. Going with extending, it will (generally) be harder to debug and test the C side of the code, as it is more difficult to get the C debugger attached. Depending on what IDE you are using (I am most used to Visual Studio 2010), you can probably set it up to execute a Python instance with command-line arguments for automatic attachment. You can also manually attach to the debugger that way. If you go with embedding, you will likely have a harder time debugging the Python side, unless you're very careful in your implementation (which you will need to be anyways) of the embedding. It will also, likely, be harder to actually step into and debug the code (I use Wing, and at work we have it setup to be attachable from any process via 2 lines of code). In either case, it should not be too hard to setup the IDEs to make debugging easy, but either way, you do not really get automatic support for one of the two languages. Note that this is generally a problem anytime you mix multiple languages. -------------- next part -------------- An HTML attachment was scrubbed... URL: From einazaki668 at yahoo.com Wed Dec 21 17:25:17 2011 From: einazaki668 at yahoo.com (Eric) Date: Wed, 21 Dec 2011 14:25:17 -0800 (PST) Subject: what does 'a=b=c=[]' do Message-ID: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Is it true that if I want to create an array or arbitrary size such as: for a in range(n): x.append() I must do this instead? x=[] for a in range(n): x.append() Now to my actual question. I need to do the above for multiple arrays (all the same, arbitrary size). So I do this: x=y=z=[] for a in range(n): x.append() y.append() z.append() Except it seems that I didn't create three different arrays, I created one array that goes by three different names (i.e. x[], y[] and z[] all reference the same pile of numbers, no idea which pile). This surprises me, can someone tell me why it shouldn't? I figure if I want to create and initialize three scalars the just do "a=b=c=7", for example, so why not extend it to arrays. Also, is there a more pythonic way to do "x=[], y=[], z=[]"? It's a slick language but I still have trouble wrapping my brain around some of the concepts. TIA, eric From joshua.landau.ws at gmail.com Wed Dec 21 18:06:11 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Wed, 21 Dec 2011 23:06:11 +0000 Subject: what does 'a=b=c=[]' do In-Reply-To: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: On 21 December 2011 22:25, Eric wrote: > Is it true that if I want to create an array or arbitrary size such > as: > for a in range(n): > x.append() > > I must do this instead? > x=[] > for a in range(n): > x.append() > > Now to my actual question. I need to do the above for multiple arrays > (all the same, arbitrary size). So I do this: > x=y=z=[] > for a in range(n): > x.append() > y.append() > z.append() > > Except it seems that I didn't create three different arrays, I created > one array that goes by three different names (i.e. x[], y[] and z[] > all reference the same pile of numbers, no idea which pile). > > This surprises me, can someone tell me why it shouldn't? I figure if > I want to create and initialize three scalars the just do "a=b=c=7", > 7 is 7 => True They're the same "7". You won't notice it though, as numbers are immutable. for example, so why not extend it to arrays. Also, is there a more > pythonic way to do "x=[], y=[], z=[]"? > a, b, c = [], [], [] It's a slick language but I still have trouble wrapping my brain > around some of the concepts. > > TIA, > eric > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ckaynor at zindagigames.com Wed Dec 21 18:12:53 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Wed, 21 Dec 2011 15:12:53 -0800 Subject: what does 'a=b=c=[]' do In-Reply-To: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: On Wed, Dec 21, 2011 at 2:25 PM, Eric wrote: > > Is it true that if I want to create an array or arbitrary size such > as: > for a in range(n): > x.append() > > I must do this instead? > x=[] > for a in range(n): > x.append() You can also use a list comprehension: x = [ for a in range(n)] Or extend and a generator expression: x = [] x.extend( for a in range(n)) Or map and a generator function: map(x.append, ( for a in range(n))) I would recommend either your way, the first of, or the second of my ways, depending on the full context. > > > Now to my actual question. I need to do the above for multiple arrays > (all the same, arbitrary size). So I do this: > x=y=z=[] This creates a new object, then assigns the labels x, y, and z to that object. > for a in range(n): > x.append() > y.append() > z.append() Then this appends the items to each of those labels, which, as they pointing to the same object, appends to all of the labels. The "variables" in Python are merely labels, and assigning to different labels does not automatically copy the object. Consider: a = [] b = a a.append(1) print b [1] > > Except it seems that I didn't create three different arrays, I created > one array that goes by three different names (i.e. x[], y[] and z[] > all reference the same pile of numbers, no idea which pile). > > This surprises me, can someone tell me why it shouldn't? I figure if > I want to create and initialize three scalars the just do "a=b=c=7", > for example, so why not extend it to arrays. Also, is there a more > pythonic way to do "x=[], y=[], z=[]"? The above rules apply in all cases, however are generally invisible on immutable objects (strings, ints, floats, tuples). In the case of a=b=c=7, you will find that all of a, b, and c point to the same object (try the "id" function or "is" operator). Doing the operation a += 1 after a=7 will create a new int* with the value 7+1 and assign it to the label a. * In CPython, there exists an optimization where small ints are cached, namely from -7 to 255 (the lower bound I stated may be wrong). This improved performance in most cases, but is CPython specific - other implementations such as PyPy or IronPython may behave differently. > > It's a slick language but I still have trouble wrapping my brain > around some of the concepts. > > TIA, > eric > -- > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Dec 21 18:44:53 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Dec 2011 23:44:53 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: <4ef26f74$0$29973$c3e8da3$5496439d@news.astraweb.com> On Wed, 21 Dec 2011 14:25:17 -0800, Eric wrote: > Is it true that if I want to create an array or arbitrary size such as: > for a in range(n): > x.append() x is not defined, so you will get a NameError unless by some lucky fluke something else has created x AND it happens to be a list. Either way, it is unlikely to do what you want. > I must do this instead? > x=[] > for a in range(n): > x.append() Yes, you should create your lists before trying to append to them. But you aren't forced to use a for-loop. You can use a list comprehension: x = [some_function(a) for a in range(n)] Notice that here you don't need x to pre-exist, because the list comp creates a brand new list, which then gets assigned directly to x. > Now to my actual question. I need to do the above for multiple arrays > (all the same, arbitrary size). So I do this: > x=y=z=[] This creates one empty list object, and gives it three names, x, y and z. Every time you append to the list, all three names see the same change, because they refer to a single list. [...] > Except it seems that I didn't create three different arrays, I created > one array that goes by three different names (i.e. x[], y[] and z[] all > reference the same pile of numbers, no idea which pile). Exactly. > This surprises me, can someone tell me why it shouldn't? Because that's the way Python works. Python is an object-oriented, name binding language. This is how OO name binding works: you have a single object, with three names bound to it. The above line is short-cut for: a = [] b = a c = a Python does not make a copy of the list unless you specifically instruct it to. > I figure if I > want to create and initialize three scalars the just do "a=b=c=7", That creates a single integer object with value 7, and binds three names to it, *exactly* the same as the above. If you could modify int objects in place, like you can modify lists in place, you would see precisely the same effect. But ints are immutable: all operations on ints create new ints. Lists are mutable, and can be changed in place. > for > example, so why not extend it to arrays. Also, is there a more pythonic > way to do "x=[], y=[], z=[]"? Well that literally won't work, you can't separate them by commas. Newlines or semicolons will work. Or: x, y, z = [], [], [] Either is pretty Pythonic. -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 21 18:48:30 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 21 Dec 2011 23:48:30 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: <4ef2704d$0$29973$c3e8da3$5496439d@news.astraweb.com> On Wed, 21 Dec 2011 18:20:16 -0500, Dennis Lee Bieber wrote: > For the amount of typing, it's easier to just do a straight line > tuple unpack > >>>> a,b,c = ([],[],[]) Note that tuples are created by the comma, not the round brackets (or parentheses for any Americans reading). So the round brackets there are strictly redundant: a, b, c = [], [], [] The only times you need the brackets around a tuple is to control the precedence of operations, or for an empty tuple. -- Steven From wuwei23 at gmail.com Wed Dec 21 19:50:40 2011 From: wuwei23 at gmail.com (alex23) Date: Wed, 21 Dec 2011 16:50:40 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> On Dec 22, 8:25?am, Eric wrote: > This surprises me, can someone tell me why it shouldn't? ?I figure if > I want to create and initialize three scalars the just do "a=b=c=7", > for example, so why not extend it to arrays. The thing to remember is that everything is an object, and that it's better to think of variables as labels on an object. So: a=b=c=7 means that _one_ integer object with the value of 7 can be referenced using any of the labels a, b or c. x=y=z=[] means that _one_ empty list can be referenced using x, y or z. The difference is that the value of a number object _cannot be changed_ ('immutable') while a list can be modified to add or remove items ('mutable'). a=10 just reassigns the label a to an integer object of value 10. x.append("foo") _modifies_ the list referred to by x, which is the same list known as y & z. > Also, is there a more pythonic way to do "x=[], y=[], z=[]"? I'd say that _is_ the most pythonic way, it's very obvious in its intent (or would be with appropriate names). If it bothers you that much: def listgen(count, default=[]): for _ in xrange(count): yield default[:] x, y, z = listgen(3) From roy at panix.com Wed Dec 21 20:02:14 2011 From: roy at panix.com (Roy Smith) Date: Wed, 21 Dec 2011 20:02:14 -0500 Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> <9lef2pFgggU1@mid.individual.net> Message-ID: In article , Grant Edwards wrote: > C++ is a vast, complex, and dangerous language -- and industry > doesn't seem to be willing to limit itself to using the seven people > on the planet who understand it. > I'm only half joking... :) Half joking, indeed. I happen to know for a fact that there are *fourteen* people on the planet who understand it. From greg at krypto.org Wed Dec 21 20:48:37 2011 From: greg at krypto.org (Gregory P. Smith) Date: Wed, 21 Dec 2011 17:48:37 -0800 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: On Wed, Dec 21, 2011 at 2:57 AM, Jim Fulton wrote: > On Wed, Dec 21, 2011 at 2:15 AM, Chris Withers > wrote: > > Hi All, > > > > What's the general consensus on supporting Python 2.5 nowadays? > > > > Do people still have to use this in commercial environments or is > everyone > > on 2.6+ nowadays? > > > > I'm finally getting some continuous integration set up for my packages > and > > it's highlighting some 2.5 compatibility issues. I'm wondering whether to > > fix those (lots of ugly "from __future__ import with_statement" > everywhere) > > or just to drop Python 2.5 support. > > > > What do people feel? > > Google app engine is still Python 2.5, as is Jython. > There's work being done to change that on the app engine front: http://code.google.com/appengine/docs/python/python27/newin27.html -gps -------------- next part -------------- An HTML attachment was scrubbed... URL: From nathan.alexander.rice at gmail.com Wed Dec 21 21:15:31 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 21 Dec 2011 21:15:31 -0500 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: Just because the default python version on a server is 2.4 doesn't mean you can't install 2.7.2... If the admins that run the machine are too lazy/stupid to install a second copy of Python let them rot. Of course, if by some nightmare scenario you have code that can't be upgraded for whatever reason, I'm so sorry. Nathan From rantingrickjohnson at gmail.com Wed Dec 21 22:43:43 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Wed, 21 Dec 2011 19:43:43 -0800 (PST) Subject: Why widgets become 'NoneType'? References: <2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com> Message-ID: <118d4919-6df3-457d-9409-d95a45b989ec@k10g2000yqk.googlegroups.com> On Dec 21, 6:59?am, Muddy Coder wrote: > Hi Folks, > > I was driven nuts by this thing: widgets lost their attributes, then I > can't configure them. Please take a look at the codes below: The problem is here... > ? ? labels = [] > ? ? for i in range(len(astr)): > ? ? ? ? lbl = Label(win, text=astr[i]).pack(side=LEFT ) label.pack() returns None. So you are creating a variable named "lbl" the value of which is None. Don't believe me? Try this... >>> from Tkinter import * >>> root = Tk() >>> label = Label(root).pack() >>> label['bg'] Traceback (most recent call last): File "", line 1, in label['bg'] TypeError: 'NoneType' object is not subscriptable >>> type(label) # # Now we separate the packing from the instancing. # >>> label = Label(root) >>> label.pack() >>> type(label) >>> label['bg'] 'SystemButtonFace' Same with list.sort. >>> lst = range(5) >>> lst [0, 1, 2, 3, 4] >>> var = lst.sort() >>> repr(var) 'None' From steve+comp.lang.python at pearwood.info Thu Dec 22 00:09:27 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 Dec 2011 05:09:27 GMT Subject: [TIP] Anyone still using Python 2.5? References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: <4ef2bb87$0$13942$c3e8da3$76491128@news.astraweb.com> On Wed, 21 Dec 2011 21:15:31 -0500, Nathan Rice wrote: > Just because the default python version on a server is 2.4 doesn't mean > you can't install 2.7.2... If the admins that run the machine are too > lazy/stupid to install a second copy of Python let them rot. If any of my sys admins installed non-supported software on one of my production servers without permission, they'd be looking for a new job. Just because some guy with root privileges can install software doesn't mean that he should. Having vendor support and security patches is far more important than the ability to write one-liner if statements. -- Steven From zyzhu2000 at gmail.com Thu Dec 22 00:45:13 2011 From: zyzhu2000 at gmail.com (GZ) Date: Wed, 21 Dec 2011 21:45:13 -0800 (PST) Subject: Generator Question Message-ID: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> Hi, I am wondering what would be the best way to return an iterator that has zero items. I just noticed the following two are different: def f(): pass def g(): if 0: yield 0 pass for x in f(): print x Traceback (most recent call last): File "", line 1, in TypeError: 'NoneType' object is not iterable for x in g(): print x #loop exits without any errors Now the question here is this: def h(): if condition=true: #I would like to return an itereator with zero length else: for ...: yield x In other words, when certain condition is met, I want to yield nothing. How to do? Thanks, gz From steve+comp.lang.python at pearwood.info Thu Dec 22 00:52:25 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 Dec 2011 05:52:25 GMT Subject: Generator Question References: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> Message-ID: <4ef2c599$0$13942$c3e8da3$76491128@news.astraweb.com> On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: > Hi, > > I am wondering what would be the best way to return an iterator that has > zero items. return iter([]) > I just noticed the following two are different: > > def f(): > pass That creates a function that does nothing, and then returns None (because Python doesn't have Pascal procedures or C void function). > def g(): > if 0: yield 0 > pass The pass is redundant. This creates a generator function which, when called, doesn't yield anything, then raises StopIteration. -- Steven From rosuav at gmail.com Thu Dec 22 01:08:12 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 22 Dec 2011 17:08:12 +1100 Subject: Generator Question In-Reply-To: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> References: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> Message-ID: On Thu, Dec 22, 2011 at 4:45 PM, GZ wrote: > def h(): > ? ?if condition=true: > ? ? ? #I would like to return an itereator with zero length > ? ?else: > ? ? ? for ...: yield x Easy. Just 'return' in the conditional. >>> def h(): if condition: return for i in range(4): yield i >>> condition=False >>> h() >>> for i in h(): print(i) 0 1 2 3 >>> condition=True >>> h() >>> for i in h(): print(i) >>> A generator object is returned since the function contains a 'yield'. On one of the branches, nothing will ever be yielded and StopIteration will be raised immediately. You could probably also raise StopIteration directly, but it's not necessary. ChrisA From steve+comp.lang.python at pearwood.info Thu Dec 22 01:35:30 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 Dec 2011 06:35:30 GMT Subject: Generator Question References: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> Message-ID: <4ef2cfb2$0$13942$c3e8da3$76491128@news.astraweb.com> On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: > Now the question here is this: > > def h(): > if condition=true: > #I would like to return an itereator with zero length > else: > for ...: yield x > > In other words, when certain condition is met, I want to yield nothing. > How to do? Actually, there's an even easier way. >>> def h(): ... if not condition: ... for c in "abc": ... yield c ... >>> >>> condition = False >>> list(h()) ['a', 'b', 'c'] >>> condition = True >>> list(h()) [] -- Steven From stefan_ml at behnel.de Thu Dec 22 02:05:10 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 22 Dec 2011 08:05:10 +0100 Subject: Anyone still using Python 2.5? In-Reply-To: References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: Dennis Lee Bieber, 21.12.2011 17:48: > On Wed, 21 Dec 2011 07:15:46 +0000, Chris Withers wrote: >> What's the general consensus on supporting Python 2.5 nowadays? >> >> Do people still have to use this in commercial environments or is >> everyone on 2.6+ nowadays? > > I was recently laid-off from a program that is still using Python 2.3 [...] That reminds me: we shouldn't forget about embedded Python installations. They are usually somewhere between "very hard" and "impossible" to upgrade, also because they often use vendor supplied binary packages for plugin APIs. I've recently seen that in a FrontArena installation (basically a trading platform) that had an embedded Py2.3 for scripting. It wasn't exactly the cutting-edge release, but the users of these platforms tend to be pretty conservative with their upgrades, and the time it takes the vendor to upgrade to a new embedded Python version can be similarly long. That means it can take several years before an embedded 2.7 hits the end users, during which anything can happen, from "vendor switches from Python to Lua" to "vendor goes bankrupt" (or maybe just one after the other). Stefan From speeze.pearson at gmail.com Thu Dec 22 03:21:41 2011 From: speeze.pearson at gmail.com (Spencer Pearson) Date: Thu, 22 Dec 2011 00:21:41 -0800 (PST) Subject: Adding an interface to existing classes Message-ID: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> I'm writing a geometry package, with Points and Lines and Circles and so on, and eventually I want to be able to draw these things on the screen. I have two options so far for how to accomplish this, but neither of them sits quite right with me, and I'd like the opinion of comp.lang.python's wizened elders. Option 1. Subclassing. The most Pythonic way would seem to be writing subclasses for the things I want to display, adding a ".draw(...)" method to each one, like this: class DrawablePoint( geometry.Point ): class draw( self, ... ): ... When the time comes to draw things, I'll have some list of objects I want drawn, and say for x in to_draw: x.draw(...) I see a problem with this, though. The intersection of two lines is (usually) an object of type Point. Since DrawableLine inherits from Line, this means that unless I redefine the "intersect" method in DrawableLine, the intersection of two DrawableLines will be a Point object, not a DrawablePoint. I don't want to saddle the user with the burden of converting every method output into its corresponding Drawable subclass, and I don't want to redefine every method to return an instance of said subclass. I see no other options if I continue down the subclassing path. Am I missing something? Option 2. A "draw" function, with a function dictionary. This feels weird, but is fairly simple to write, use, and extend. We have a module with a "draw_functions" dictionary that maps types onto functions, and a "draw" function that just looks up the proper type in the dictionary and calls the corresponding function. If you create your own object, you can just add a new entry to the dictionary. The implementation is simple enough to outline here: In file "geometry/gui.py": def draw_point(...): ... def draw_line(...): ... draw_functions = {geometry.Point: draw_point, geometry.Line: draw_line, ...} def draw( x, *args, **kwargs ): for type, callback in draw_functions.iteritems(): if isinstance(x, type): callback(x, *args, **kwargs) else: raise TypeError("don't know how to draw things of type " "{0}".format(type(x))) In the file that uses this: # Drawing a predefined type of object: geometry.gui.draw(some_point, ...) # Here we define a new kind of object and tell the package how to draw it. class MyObject(GeometricObject): ... def draw_my_object(...): ... geometry.gui.draw_functions[MyObject] = draw_my_object # And now we draw it. geometry.gui.draw(MyObject(...), ...) If I feel fancy, I might use a decorator for adding entries to draw_functions, but this is basically how it'd work. The second way feels kludgey to me, but I'm leaning towards it because it seems like so much less work and I'm out of ideas. Can anyone help, explaining either a different way to do it or why one of these isn't as bad as I think? Thanks for your time! -Spencer From rolf at roce.be Thu Dec 22 03:51:54 2011 From: rolf at roce.be (Rolf Camps) Date: Thu, 22 Dec 2011 09:51:54 +0100 Subject: what does 'a=b=c=[]' do In-Reply-To: <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: <1324543914.2075.82.camel@puppy> alex23 schreef op wo 21-12-2011 om 16:50 [-0800]: > On Dec 22, 8:25 am, Eric wrote: > > This surprises me, can someone tell me why it shouldn't? I figure if > > I want to create and initialize three scalars the just do "a=b=c=7", > > for example, so why not extend it to arrays. > > The thing to remember is that everything is an object, and that it's > better to think of variables as labels on an object. > > So: a=b=c=7 means that _one_ integer object with the value of 7 can be > referenced using any of the labels a, b or c. x=y=z=[] means that > _one_ empty list can be referenced using x, y or z. > > The difference is that the value of a number object _cannot be > changed_ ('immutable') while a list can be modified to add or remove > items ('mutable'). a=10 just reassigns the label a to an integer > object of value 10. x.append("foo") _modifies_ the list referred to by > x, which is the same list known as y & z. > > > Also, is there a more pythonic way to do "x=[], y=[], z=[]"? > > I'd say that _is_ the most pythonic way, it's very obvious in its > intent (or would be with appropriate names). If it bothers you that > much: > > def listgen(count, default=[]): > for _ in xrange(count): > yield default[:] > > x, y, z = listgen(3) > I'm afraid it's dangerous to encourage the use of '[]' as assignment to a parameter in a function definition. If you use the function several times 'default' always points to the same list. >>> def return_list(list_ = []): >>> return list_ >>> a_list = return_list() >>> a_list [] >>> a_list.append(3) >>> a_list [3] >>> b_list = return_list() >>> b_list >>> [3] # !!?? >>> def return_list(): >>> return [] >>> a_list = return_list() >>> a_list [] >>> a_list.append(3) >>> a_list [3] >>> b_list = return_list() >>> b_list >>> [] # OK! I only use python3 so I don't know how these things work in other versions. No problem in your function since you yield a copy, but I've already seen long threads about this. I would change your function to (Python3.x): def empty_lists(count): for _ in range(count): yield [] Regards, Rolf From praveen.venkata at gmail.com Thu Dec 22 06:04:02 2011 From: praveen.venkata at gmail.com (Ven) Date: Thu, 22 Dec 2011 03:04:02 -0800 (PST) Subject: Item Checking not possible with UltimateListCtrl in ULC_VIRTUAL mode Message-ID: Following is the system and software info Platforms: Windows XP and OSX Lion Activestate Python 2.7.2 wxPython2.9-osx-cocoa-py2.7 (for OSX) wxPython2.9-win32-py27 (for Windows XP) I am trying to create a UltimateListCtrl using ULC_VIRTUAL and ULC_REPORT mode. I would like to know how can I put a checkbox beside the first column of every row and catch the event when a user checks the box. I was able to do the same using UltimateListCtrl without VIRTUAL mode. But, with the ULC_VIRTUAL flag ON, I don't know how to proceed. Following is the code I created, but this still doesn't allow me to check the boxes associated with the first column. Please help. import wx import images import random import os, sys from wx.lib.agw import ultimatelistctrl as ULC class TestUltimateListCtrl(ULC.UltimateListCtrl): def __init__(self, parent, log): ULC.UltimateListCtrl.__init__(self, parent, -1, agwStyle=ULC.ULC_AUTO_CHECK_CHILD|ULC.ULC_VIRTUAL|ULC.ULC_REPORT| ULC.ULC_SINGLE_SEL|ULC.ULC_VRULES|ULC.ULC_HRULES) self.SetItemCount(1000) self.table_fields=['First','Second','Third'] field_index=0 for field in self.table_fields: info = ULC.UltimateListItem() info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT | ULC.ULC_MASK_CHECK info._image = [] info._format = wx.LIST_FORMAT_CENTER info._kind = 1 info._text = field info._font= wx.Font(13, wx.ROMAN, wx.NORMAL, wx.BOLD) self.InsertColumnInfo(field_index, info) self.SetColumnWidth(field_index,175) field_index += 1 def getColumnText(self, index, col): item = self.GetItem(index, col) return item.GetText() def OnGetItemText(self, item, col): return "Item %d, Column %d" % (item,col) def OnGetItemColumnImage(self, item, col): return [] def OnGetItemImage(self, item): return [] def OnGetItemAttr(self, item): return None def OnGetItemTextColour(self, item, col): return None #def OnGetItemColumnCheck(self, item, col): #return True #def OnGetItemCheck(self, item): #item=self.GetItem(item) #return item.IsChecked() def OnGetItemToolTip(self, item, col): return None def OnGetItemKind(self, item): return 1 def OnGetItemColumnKind(self, item, col): if col==0: return self.OnGetItemKind(item) return 0 class TestFrame(wx.Frame): def __init__(self, parent, log): wx.Frame.__init__(self, parent, -1, "UltimateListCtrl in wx.LC_VIRTUAL mode", size=(700, 600)) panel = wx.Panel(self, -1) sizer = wx.BoxSizer(wx.VERTICAL) listCtrl = TestUltimateListCtrl(panel, log) sizer.Add(listCtrl, 1, wx.EXPAND) panel.SetSizer(sizer) sizer.Layout() self.CenterOnScreen() self.Show() if __name__ == '__main__': import sys app = wx.PySimpleApp() frame = TestFrame(None, sys.stdout) frame.Show(True) app.MainLoop() Btw, following is the code I used to create the same thing without the VIRTUAL mode. And in this case, I can check the boxes beside the first column data in every row. But, I will be working with tens of thousands of items and I cannot rely on loading the items like below because it is very slow. Hence, I want to use the Virtual List, but I don't know how to get the same functionality in it. import wx import images import random import os, sys from wx.lib.agw import ultimatelistctrl as ULC class TestUltimateListCtrl(ULC.UltimateListCtrl): def __init__(self, parent, log): ULC.UltimateListCtrl.__init__(self, parent, -1, agwStyle=ULC.ULC_REPORT|ULC.ULC_SINGLE_SEL|ULC.ULC_VRULES| ULC.ULC_HRULES) self.table_fields=['First','Second','Third'] field_index=0 for field in self.table_fields: info = ULC.UltimateListItem() info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT | ULC.ULC_MASK_CHECK info._image = [] info._format = wx.LIST_FORMAT_CENTER info._kind = 1 info._text = field info._font= wx.Font(13, wx.ROMAN, wx.NORMAL, wx.BOLD) self.InsertColumnInfo(field_index, info) self.SetColumnWidth(field_index,175) field_index += 1 for record_index in range(0,1000): for field in self.table_fields: if self.table_fields.index(field)==0: self.InsertStringItem(record_index, 'Item %d, Column %d' % (record_index,self.table_fields.index(field)),it_kind=1) else: self.SetStringItem(record_index, self.table_fields.index(field), 'Item %d, Column %d' % (record_index,self.table_fields.index(field))) class TestFrame(wx.Frame): def __init__(self, parent, log): wx.Frame.__init__(self, parent, -1, "UltimateListCtrl in wx.LC_VIRTUAL mode", size=(700, 600)) panel = wx.Panel(self, -1) sizer = wx.BoxSizer(wx.VERTICAL) listCtrl = TestUltimateListCtrl(panel, log) sizer.Add(listCtrl, 1, wx.EXPAND) panel.SetSizer(sizer) sizer.Layout() self.CenterOnScreen() self.Show() if __name__ == '__main__': import sys app = wx.PySimpleApp() frame = TestFrame(None, sys.stdout) frame.Show(True) app.MainLoop() From y.turgut at gmail.com Thu Dec 22 06:11:13 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Thu, 22 Dec 2011 03:11:13 -0800 (PST) Subject: Text Processing References: <209c2abf-dd56-4a7f-839b-fad92920d457@m7g2000vbc.googlegroups.com> <20111220210321.77451e19@bouzin.lan> <7895.1324415085@alphaville.americas.hpqcorp.net> Message-ID: <6ff3a578-5a77-4e31-8e54-e29c46299e3c@f1g2000yqi.googlegroups.com> On Dec 21, 2:01?am, Alexander Kapps wrote: > On 20.12.2011 22:04, Nick Dokos wrote: > > > > > > > > > > >>> I have a text file containing such data ; > > >>> ? ? ? ? ?A ? ? ? ? ? ? ? ?B ? ? ? ? ? ? ? ?C > >>> ------------------------------------------------------- > >>> -2.0100e-01 ? ?8.000e-02 ? ?8.000e-05 > >>> -2.0000e-01 ? ?0.000e+00 ? 4.800e-04 > >>> -1.9900e-01 ? ?4.000e-02 ? ?1.600e-04 > > >>> But I only need Section B, and I need to change the notation to ; > > >>> 8.000e-02 = 0.08 > >>> 0.000e+00 = 0.00 > >>> 4.000e-02 = 0.04 > > Does it have to be python? If not, I'd go with something similar to > > > ? ? sed 1,2d foo.data | awk '{printf("%.2f\n", $2);}' > > Why sed and awk: > > awk 'NR>2 {printf("%.2f\n", $2);}' data.txt > > And in Python: > > f = open("data.txt") > f.readline() ? ?# skip header > f.readline() ? ?# skip header > for line in f: > ? ? ?print "%02s" % float(line.split()[1]) @Jerome ; Your suggestion provided floating point error, it might need some slight modificiation. @Nick ; Sorry mate, it needs to be in Python. But I noted solution in case if I need for another case. @Alexander ; Works as expected. Thank you all for the replies. From robert.kern at gmail.com Thu Dec 22 06:15:11 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 22 Dec 2011 11:15:11 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 12/21/11 6:52 PM, Joshua Landau wrote: > If fix means "return number" then no. This inconsistency between elementwise > operations and 'normal' ones is the problem. Again, explicit elementwise-ifying > through "~" fixes that. You need to tell me why this is worth the confusion over > that. I understand that you are a supporter of PEP 225, but please have some courtesy towards other developers. We can explore alternatives in parallel. If we all worked on exactly the same thing (much less the one thing that only you get to choose), we would forgo a lot of valuable experience. You can point out the flaws of Elementwise's implementation and compare it to the virtues of PEP 225 without being so demanding. He doesn't "need to tell" you a damn thing. -- 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 From hansmu at xs4all.nl Thu Dec 22 06:17:17 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 22 Dec 2011 12:17:17 +0100 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: References: <4EF22CA1.1070200@gmail.com> <4EF233A7.6020907@stoneleaf.us> Message-ID: <4ef311bd$0$6870$e4fe514c@news2.news.xs4all.nl> On 21/12/11 21:11:03, Andrew Berg wrote: > On 12/21/2011 1:29 PM, Ethan Furman wrote: >> Anything that runs at import time should be protected by the `if >> __name__ == '__main__'` idiom as the children will import the __main__ >> module. > So the child imports the parent and runs the spawn code again? That > makes sense. It's platform dependent. On Windows, the child imports the parent and you get the phenomenon your ran into. On Posix platforms, multiprocessing uses fork(), and your code runs without problems. It would still be a good idea to use the `if __name__ is '__main__'` idiom, even on Posix platforms. Hope this helps, -- HansM From robert.kern at gmail.com Thu Dec 22 06:18:38 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 22 Dec 2011 11:18:38 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 12/21/11 5:07 PM, Paul Dubois wrote: > You're reinventing Numeric Python. Actually, he's not. The numerical operators certainly overlap, but Elementwise supports quite a bit more generic operations than we ever bothered to implement with object arrays. -- 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 From robert.kern at gmail.com Thu Dec 22 06:24:21 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 22 Dec 2011 11:24:21 +0000 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: <4EF23D57.6010305@gmail.com> References: <4EF22CA1.1070200@gmail.com> <4EF233A7.6020907@stoneleaf.us> <4EF23D57.6010305@gmail.com> Message-ID: On 12/21/11 8:11 PM, Andrew Berg wrote: > On 12/21/2011 1:29 PM, Ethan Furman wrote: >> Anything that runs at import time should be protected by the `if >> __name__ == '__main__'` idiom as the children will import the __main__ >> module. > So the child imports the parent and runs the spawn code again? That > makes sense. This is a problem with multiprocessing on Windows. Your code would work fine on a UNIX system. The problem is that Windows does not have a proper fork() for multiprocessing to use. Consequently, it has to start a new Python process from scratch and then *import* the main module such that it can properly locate the rest of the code to start. If you do not guard the spawning code with a __main__ test, then this import will cause an infinite loop. Just as a further note on these lines, when older versions of setuptools and distribute install scripts, they generate stub scripts that do not use a __main__ guard, so installing a multiprocessing-using application with setuptools can cause this problem. I believe newer versions of distribute has this problem fixed, but I'm not sure. -- 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 From ndbecker2 at gmail.com Thu Dec 22 06:49:16 2011 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 22 Dec 2011 06:49:16 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> Message-ID: I agree with the OP that the current syntax is confusing. The issue is, the meaning of * is context-dependent. Why not this: Func (*args) == Func (unpack (args)) def Func (*args) == Func (pack (args)) That seems very clear IMO From steve+comp.lang.python at pearwood.info Thu Dec 22 08:12:57 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 22 Dec 2011 13:12:57 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> Message-ID: <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Dec 2011 06:49:16 -0500, Neal Becker wrote: > I agree with the OP that the current syntax is confusing. The issue is, > the meaning of * is context-dependent. Here you are complaining about an operator being "confusing" because it is context-dependent, in a post where you strip all context except the subject line and expect us to still understand what you're talking about. There's a lesson there, I'm sure. * is context dependent. You know what else is context dependent? Well, most things. But in particular, parentheses. Clearly they must be "confusing" too. So how about we say: class MyClass superclasslist A, B C: def method argumentlist self, x, y: t = tuple 1, 2 tuple 3, 4 endtuple endtuple return group x + y endgroup * group x - y endgroup Much less confusing! -- Steven From neilc at norwich.edu Thu Dec 22 08:19:21 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 22 Dec 2011 13:19:21 GMT Subject: [OT] Quick intro to C++ for a Python and C user? References: <9lc2u5FilrU1@mid.individual.net> <7xr4zzx8k9.fsf@ruckus.brouhaha.com> <9le7c5F1lsU1@mid.individual.net> <9lef2pFgggU1@mid.individual.net> Message-ID: <9lgp2pFd2sU4@mid.individual.net> On 2011-12-22, Roy Smith wrote: > In article , > Grant Edwards wrote: >> C++ is a vast, complex, and dangerous language -- and industry >> doesn't seem to be willing to limit itself to using the seven >> people on the planet who understand it. > >> I'm only half joking... :) > > Half joking, indeed. I happen to know for a fact that there > are *fourteen* people on the planet who understand it. One of its greatest contributions to computer science were Glassbarrow's C++ puzzles. They likely couldn't have been as challenging in any other language. ;) -- Neil Cerutti From ethan at stoneleaf.us Thu Dec 22 08:20:10 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 22 Dec 2011 05:20:10 -0800 Subject: what does 'a=b=c=[]' do In-Reply-To: <1324543914.2075.82.camel@puppy> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <1324543914.2075.82.camel@puppy> Message-ID: <4EF32E8A.70900@stoneleaf.us> Rolf Camps wrote: > alex23 schreef op wo 21-12-2011 om 16:50 [-0800]: >> I'd say that _is_ the most pythonic way, it's very obvious in its >> intent (or would be with appropriate names). If it bothers you that >> much: >> >> def listgen(count, default=[]): >> for _ in xrange(count): >> yield default[:] >> >> x, y, z = listgen(3) >> > I would change your function to (Python3.x): > > def empty_lists(count): > for _ in range(count): > yield [] While it's good to be careful, default mutable arguments have their place. Alex's versioun allows one to use an already existing list and get shallow copies of it, yours will only create empty lists. a, b, c = listgen([1, 2, 3]) # a, b, & c are bound to different lists ~Ethan~ From rosuav at gmail.com Thu Dec 22 08:30:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 00:30:56 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 23, 2011 at 12:12 AM, Steven D'Aprano wrote: > class MyClass superclasslist A, B C: > ? ?def method argumentlist self, x, y: > ? ? ? ?t = tuple 1, 2 tuple 3, 4 endtuple endtuple > ? ? ? ?return group x + y endgroup * group x - y endgroup > > > Much less confusing! A definite improvement. However, I fear that the numerals "1", "2", etc are far too vague. In some contexts, the abuttal of two such numerals results in a larger number by a factor of 10, while in others, the factor is 8, or 16, or 2. This must not be. We need an unambiguous representation of integers. Also, I think we should adopt an existing standard [1]. This is generally a good idea. class MyClass superclasslist A, B C: def method argumentlist self, x, y: t = tuple summer's day, proud pony tuple sum of honest purse and fellow, large proud town endtuple endtuple return product of group sum of x and y endgroup and group difference between x and y endgroup You will find this a vast improvement. ChrisA [1] http://shakespearelang.sourceforge.net/report/shakespeare/shakespeare.html#SECTION00045000000000000000 or http://tinyurl.com/6sycv From hansmu at xs4all.nl Thu Dec 22 09:13:22 2011 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 22 Dec 2011 15:13:22 +0100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef33b02$0$6952$e4fe514c@news2.news.xs4all.nl> On 22/12/11 14:12:57, Steven D'Aprano wrote: > On Thu, 22 Dec 2011 06:49:16 -0500, Neal Becker wrote: > >> I agree with the OP that the current syntax is confusing. The issue is, >> the meaning of * is context-dependent. > > Here you are complaining about an operator being "confusing" because it > is context-dependent, in a post where you strip all context except the > subject line and expect us to still understand what you're talking about. > There's a lesson there, I'm sure. > > > * is context dependent. You know what else is context dependent? Well, > most things. But in particular, parentheses. Clearly they must be > "confusing" too. So how about we say: > > class MyClass superclasslist A, B C: > def method argumentlist self, x, y: > t = tuple 1, 2 tuple 3, 4 endtuple endtuple > return group x + y endgroup * group x - y endgroup > > > Much less confusing! How about: 1 2 34 xx xx More more readable! And it's a standard! :-) -- HansM From rosuav at gmail.com Thu Dec 22 09:30:29 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 01:30:29 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4ef33b02$0$6952$e4fe514c@news2.news.xs4all.nl> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef33b02$0$6952$e4fe514c@news2.news.xs4all.nl> Message-ID: On Fri, Dec 23, 2011 at 1:13 AM, Hans Mulder wrote: > How about: > > > ... > > > More more readable! ?And it's a standard! Unfortunately it's not Pythonic, because indentation is insignificant. We need to adopt a more appropriate form. Eliminate all the tags and use indentation to mark the ends of elements. ChrisA PS. Brilliant, sir, brilliant! I take off my cap to you. From nikos.kouras at gmail.com Thu Dec 22 09:40:27 2011 From: nikos.kouras at gmail.com (=?ISO-8859-7?B?zenq/Ovh7/Igyu/98eHy?=) Date: Thu, 22 Dec 2011 06:40:27 -0800 (PST) Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error Message-ID: Hello when i try to visit my webpage i get the error it displays. Iam not posting it since you can see it by visiting my webpage at http://superhost.gr Please if you can tell me what might be wrong. From iversonstan at gmail.com Thu Dec 22 09:55:06 2011 From: iversonstan at gmail.com (Stan Iverson) Date: Thu, 22 Dec 2011 10:55:06 -0400 Subject: Spanish Accents Message-ID: Hi; If I write a python page to print to the web with Spanish accents all is well. However, if I read the data from a text file it prints diamonds with question marks wherever there are accented vowels. Please advise. TIA, Stan -------------- next part -------------- An HTML attachment was scrubbed... URL: From gslindstrom at gmail.com Thu Dec 22 09:58:34 2011 From: gslindstrom at gmail.com (Greg Lindstrom) Date: Thu, 22 Dec 2011 08:58:34 -0600 Subject: Overlaying PDF with data Message-ID: Hello, I would like to take an existing pdf form and overlay text "on top" (name, address, etc.). I'm looking at ReportLab v2.5 and see their "PLATYPUS" library might be what I crave, but would like to know if there's a more obvious way to do this? I'm working with health care forms which are quite complex (over 150 fields to potentially populate) and would like to do it as painlessly as possible. I'm running Python 2.7 on Windows. Thanks for any advice you may have, --greg -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu Dec 22 09:58:43 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 01:58:43 +1100 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Fri, Dec 23, 2011 at 1:55 AM, Stan Iverson wrote: > Hi; > If I write a python page to print to the web with Spanish accents all is > well. However, if I read the data from a text file it prints diamonds with > question marks wherever there are accented vowels. Please advise. Sounds like an encoding problem. Firstly, are you using Python 2 or Python 3? Things will be slightly different, since the default 'str' object in Py3 is Unicode. I would guess that your page is being output as UTF-8; you may find that the solution is as easy as declaring the encoding of your text file when you read it in. ChrisA From mwilson at the-wire.com Thu Dec 22 10:06:46 2011 From: mwilson at the-wire.com (Mel Wilson) Date: Thu, 22 Dec 2011 10:06:46 -0500 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef33b02$0$6952$e4fe514c@news2.news.xs4all.nl> Message-ID: Chris Angelico wrote: > On Fri, Dec 23, 2011 at 1:13 AM, Hans Mulder wrote: >> How about: >> >> >> ... >> >> >> More more readable! And it's a standard! > > Unfortunately it's not Pythonic, because indentation is insignificant. Easy-peasy: Mel. > We need to adopt a more appropriate form. Eliminate all the > tags and use indentation to mark the ends of elements. > > ChrisA > PS. Brilliant, sir, brilliant! I take off my cap to you. From iversonstan at gmail.com Thu Dec 22 10:25:46 2011 From: iversonstan at gmail.com (Stan Iverson) Date: Thu, 22 Dec 2011 11:25:46 -0400 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 10:58 AM, Chris Angelico wrote: > Firstly, are you using Python 2 or Python 3? Things will be slightly > different, since the default 'str' object in Py3 is Unicode. > 2 > > I would guess that your page is being output as UTF-8; you may find > that the solution is as easy as declaring the encoding of your text > file when you read it in. > So I tried this: file = open(p + "2.txt") for line in file: print unicode(line, 'utf-8') and got this error: 142 print unicode(line, 'utf-8') 143 144 print '''

*builtin* *unicode* = , *line* = '\r\n' /usr/lib64/python2.4/encodings/utf_8.pyin *decode*(input=, errors='strict') 14 15 def decode(input, errors='strict'): 16 return codecs.utf_16_decode(input, errors, True) 17 18 class StreamWriter(codecs.StreamWriter): *global* *codecs* = , codecs.*utf_16_decode* = , *input* = , *errors* = 'strict', *builtin* *True* = True *UnicodeDecodeError*: 'utf16' codec can't decode byte 0x0a in position 20: truncated data args = ('utf16', '\r\n', 20, 21, 'truncated data') encoding = 'utf16' end = 21 object = '\r\n' reason = 'truncated data' start = 20 Tried it with utf-16 with same results. TIA, Stan -------------- next part -------------- An HTML attachment was scrubbed... URL: From rami.chowdhury at gmail.com Thu Dec 22 10:30:21 2011 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Thu, 22 Dec 2011 15:30:21 +0000 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 15:25, Stan Iverson wrote: > On Thu, Dec 22, 2011 at 10:58 AM, Chris Angelico wrote: > >> Firstly, are you using Python 2 or Python 3? Things will be slightly >> different, since the default 'str' object in Py3 is Unicode. >> > > 2 > >> >> I would guess that your page is being output as UTF-8; you may find >> that the solution is as easy as declaring the encoding of your text >> file when you read it in. >> > > So I tried this: > > file = open(p + "2.txt") > for line in file: > print unicode(line, 'utf-8') > Could you try using the 'open' function from the 'codecs' module? file = codecs.open(p + "2.txt", "utf-8") # or whatever encoding your file is written in for line in file: print line > > and got this error: > > 142 print unicode(line, 'utf-8') > 143 > 144 print '''

> *builtin* *unicode* = , *line* = '\r\n > ' /usr/lib64/python2.4/encodings/utf_8.py in *decode*(input= buffer ptr 0x2b197e378454, size 21>, errors='strict') 14 > 15 def decode(input, errors='strict'): > 16 return codecs.utf_16_decode(input, errors, True) > 17 > 18 class StreamWriter(codecs.StreamWriter): > *global* *codecs* = '/usr/lib64/python2.4/codecs.pyc'>, codecs.*utf_16_decode* = function utf_16_decode>, *input* = size 21>, *errors* = 'strict', *builtin* *True* = True > > *UnicodeDecodeError*: 'utf16' codec can't decode byte 0x0a in position > 20: truncated data > args = ('utf16', '\r\n', 20, 21, 'truncated > data') > encoding = 'utf16' > end = 21 > object = '\r\n' > reason = 'truncated data' > start = 20 > > Tried it with utf-16 with same results. > > TIA, > > Stan > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 -------------- next part -------------- An HTML attachment was scrubbed... URL: From rami.chowdhury at gmail.com Thu Dec 22 10:32:34 2011 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Thu, 22 Dec 2011 15:32:34 +0000 Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error In-Reply-To: References: Message-ID: 2011/12/22 ???????? ?????? : > Hello when i try to visit my webpage i get the error it displays. Iam > not posting it since you can see it by visiting my webpage at > http://superhost.gr > > Please if you can tell me what might be wrong. I can't see any errors on that page -- can you please post the complete traceback so we can all see it? -- Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 From bex.lewis at gmail.com Thu Dec 22 10:52:39 2011 From: bex.lewis at gmail.com (becky_lewis) Date: Thu, 22 Dec 2011 07:52:39 -0800 (PST) Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error References: Message-ID: <21f682a5-fc62-4820-9cc5-f5dfd064ec6a@t16g2000vba.googlegroups.com> On Dec 22, 2:40?pm, ???????? ?????? wrote: > Hello when i try to visit my webpage i get the error it displays. Iam > not posting it since you can see it by visiting my webpage athttp://superhost.gr > > Please if you can tell me what might be wrong. It doesn't seem entirely clear but if I had to guess I'd think that for some reason os.environ['REMOTE_ADDR'] is not returning a good value (os.environ is a dictionary holding all of the os evironment variables). According to the socket docs, this error gets raised for address related errors. From iversonstan at gmail.com Thu Dec 22 11:22:52 2011 From: iversonstan at gmail.com (Stan Iverson) Date: Thu, 22 Dec 2011 12:22:52 -0400 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 11:30 AM, Rami Chowdhury wrote: > Could you try using the 'open' function from the 'codecs' module? > I believe this is what you meant: file = codecs.open(p + "2.txt", "r", "utf-8") for line in file: print line but got this error: 141 file = codecs.open(p + "2.txt", "r", "utf-8") 142 for line in file: 143 print line 144 *line* = '\r\n', *file* = /usr/lib64/python2.4/codecs.py in *next*(self=) 492 493 """ Return the next decoded line from the input stream.""" 494 return self.reader.next() 495 496 def __iter__(self): *self* = , self.*reader* = , self.reader.*next* = > /usr/lib64/python2.4/codecs.py in *next*(self=) 429 430 """ Return the next decoded line from the input stream.""" 431 line = self.readline() 432 if line: 433 return line line *undefined*, *self* = , self.* readline* = > /usr/lib64/python2.4/codecs.py in *readline*(self=, size=None, keepends=True) 344 # If size is given, we call read() only once 345 while True: 346 data = self.read(readsize, firstline=True) 347 if data: 348 # If we're at a "\r" read one extra character (which might data *undefined*, *self* = , self.*read* = >, * readsize* = 72, firstline *undefined*, *builtin* *True* = True /usr/lib64/python2.4/codecs.py in *read*(self=, size=72, chars=-1, firstline=True) 291 data = self.bytebuffer + newdata 292 try: 293 newchars, decodedbytes = self.decode(data, self.errors) 294 except UnicodeDecodeError, exc: 295 if firstline: *newchars* = u'', *decodedbytes* = 0, *self* = , self.*decode* = , *data* = '\xe1intentado para ellos bastante sabios para discernir lo obvio. Tales perso', self.*errors* = 'strict' *UnicodeDecodeError*: 'utf8' codec can't decode bytes in position 0-2: invalid data args = ('utf8', '\xe1 intentado para ellos bastante sabios para discernir lo obvio. Tales perso', 0, 3, 'invalid data') encoding = 'utf8' end = 3 object = '\xe1 intentado para ellos bastante sabios para discernir lo obvio. Tales perso' reason = 'invalid data' start = 0 which is the letter ? (a with accent). So I tried with utf-16 and got this error: 141 file = codecs.open(p + "2.txt", "r", "utf-16") 142 for line in file: 143 print line 144 *line* = '\r\n', *file* = /usr/lib64/python2.4/codecs.py in *next*(self=) 492 493 """ Return the next decoded line from the input stream.""" 494 return self.reader.next() 495 496 def __iter__(self): *self* = , self.*reader* = , self.reader.*next* = > /usr/lib64/python2.4/codecs.py in *next*(self=) 429 430 """ Return the next decoded line from the input stream.""" 431 line = self.readline() 432 if line: 433 return line line *undefined*, *self* = , self.* readline* = > /usr/lib64/python2.4/codecs.py in *readline*(self=, size=None, keepends=True) 344 # If size is given, we call read() only once 345 while True: 346 data = self.read(readsize, firstline=True) 347 if data: 348 # If we're at a "\r" read one extra character (which might data *undefined*, *self* = , self.*read* = >, * readsize* = 72, firstline *undefined*, *builtin* *True* = True /usr/lib64/python2.4/codecs.py in *read*(self=, size=72, chars=-1, firstline=True) 291 data = self.bytebuffer + newdata 292 try: 293 newchars, decodedbytes = self.decode(data, self.errors) 294 except UnicodeDecodeError, exc: 295 if firstline: newchars *undefined*, decodedbytes *undefined*, *self* = , self.*decode* = >, *data* = '\r\nNoticia: Este sitio web entre este portal est\xe1 i', self.*errors* = 'strict' /usr/lib64/python2.4/encodings/utf_16.py in *decode*(self=, input='\r\nNoticia: Este sitio web entre este portal est\xe1 i', errors='strict') 47 self.decode = codecs.utf_16_be_decode 48 elif consumed>=2: 49 raise UnicodeError,"UTF-16 stream does not start with BOM" 50 return (object, consumed) 51 *builtin* *UnicodeError* = *UnicodeError*: UTF-16 stream does not start with BOM args = ('UTF-16 stream does not start with BOM',) -------------- next part -------------- An HTML attachment was scrubbed... URL: From nikos.kouras at gmail.com Thu Dec 22 11:29:37 2011 From: nikos.kouras at gmail.com (=?ISO-8859-7?B?zenq/Ovh7/Igyu/98eHy?=) Date: Thu, 22 Dec 2011 08:29:37 -0800 (PST) Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error References: Message-ID: <920839e5-58fa-4fdf-9c88-7bed9a7448fd@d10g2000vbh.googlegroups.com> On 22 ???, 17:32, Rami Chowdhury wrote: > 2011/12/22 ???????? ?????? : > > > Hello when i try to visit my webpage i get the error it displays. Iam > > not posting it since you can see it by visiting my webpage at > >http://superhost.gr > > > Please if you can tell me what might be wrong. > > I can't see any errors on that page -- can you please post the > complete traceback so we can all see it? Yes of course. Its the following: A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred. /home/nikos/public_html/cgi-bin/counter.py 15 16 hits = 0 17 host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] 18 date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S' ) 19 agent = os.environ['HTTP_USER_AGENT'] host undefined, socket = , socket.gethostbyaddr = , os = , os.environ = {'REDIRECT_QUERY_STRING': 'page=index.html', 'HT...e,sdch', 'UNIQUE_ID': 'TvNasbAJGaoAABtUy at gAAAAI'} herror: (1, '\xb6\xe3\xed\xf9\xf3\xf4\xef \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2') args = (1, '\xb6\xe3\xed\xf9\xf3\xf4\xef \xfc\xed\xef\xec\xe1 \xf3\xf5\xf3\xf4\xde\xec\xe1\xf4\xef\xf2') From rosuav at gmail.com Thu Dec 22 11:35:54 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 03:35:54 +1100 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Fri, Dec 23, 2011 at 3:22 AM, Stan Iverson wrote: > > On Thu, Dec 22, 2011 at 11:30 AM, Rami Chowdhury wrote: >> >> Could you try using the 'open' function from the 'codecs' module? > > I believe this is what you meant: > > file = codecs.open(p + "2.txt", "r", "utf-8") > > but got this error: > > ?/usr/lib64/python2.4/codecs.py in next(self=) Your file almost certainly isn't UTF-16, so don't bother trying that. I think you may have ISO-8859-1 encoding, also called Latin-1, but it's not easy to be sure. Try: file = codecs.open(p + "2.txt", "r", "iso-8859-1") ChrisA From __peter__ at web.de Thu Dec 22 11:42:09 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 22 Dec 2011 17:42:09 +0100 Subject: Spanish Accents References: Message-ID: Stan Iverson wrote: > On Thu, Dec 22, 2011 at 11:30 AM, Rami Chowdhury > wrote: > >> Could you try using the 'open' function from the 'codecs' module? >> > > I believe this is what you meant: > > file = codecs.open(p + "2.txt", "r", "utf-8") > for line in file: > print line > > but got this error: > > *UnicodeDecodeError*: 'utf8' codec can't decode bytes in position 0-2: > invalid data > args = ('utf8', '\xe1 intentado para ellos bastante sabios para > discernir lo obvio. Tales perso', 0, 3, 'invalid data') > which is the letter ? (a with accent). The file is probably encoded in ISO-8859-1, ISO-8859-15, or cp1252 then: >>> print "\xe1".decode("iso-8859-1") ? >>> print "\xe1".decode("iso-8859-15") ? >>> print "\xe1".decode("cp1252") ? Try codecs.open() with one of these encodings. From iversonstan at gmail.com Thu Dec 22 11:54:50 2011 From: iversonstan at gmail.com (Stan Iverson) Date: Thu, 22 Dec 2011 12:54:50 -0400 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 12:42 PM, Peter Otten <__peter__ at web.de> wrote: > The file is probably encoded in ISO-8859-1, ISO-8859-15, or cp1252 then: > > >>> print "\xe1".decode("iso-8859-1") > ? > >>> print "\xe1".decode("iso-8859-15") > ? > >>> print "\xe1".decode("cp1252") > ? > > Try codecs.open() with one of these encodings. > I'm baffled. I duplicated your print statements but when I run this code (or any of the 3 encodings): file = codecs.open(p + "2.txt", "r", "cp1252") #file = codecs.open(p + "2.txt", "r", "utf-8") for line in file: print line I get this error: *UnicodeEncodeError*: 'ascii' codec can't encode character u'\xe1' in position 48: ordinal not in range(128) args = ('ascii', u'Noticia: Este sitio web entre este portal est...r\xe1pidamente va a salir de aqu\xed.

\r\n', 48, 49, 'ordinal not in range(128)') encoding = 'ascii' end = 49 object = u'Noticia: Este sitio web entre este portal est...r\xe1pidamente va a salir de aqu\xed.

\r\n' reason = 'ordinal not in range(128)' start = 48 Please advise. TIA, Stan -------------- next part -------------- An HTML attachment was scrubbed... URL: From robert.kern at gmail.com Thu Dec 22 12:01:14 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 22 Dec 2011 17:01:14 +0000 Subject: Why does this launch an infinite loop of new processes? In-Reply-To: References: <4EF22CA1.1070200@gmail.com> <4EF233A7.6020907@stoneleaf.us> <4EF23D57.6010305@gmail.com> Message-ID: On 12/22/11 11:24 AM, Robert Kern wrote: > Just as a further note on these lines, when older versions of setuptools and > distribute install scripts, they generate stub scripts that do not use a > __main__ guard, so installing a multiprocessing-using application with > setuptools can cause this problem. I believe newer versions of distribute has > this problem fixed, but I'm not sure. Just checked. No, distribute has not fixed this bug. -- 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 From wolftracks at invalid.com Thu Dec 22 12:53:04 2011 From: wolftracks at invalid.com (W. eWatson) Date: Thu, 22 Dec 2011 09:53:04 -0800 Subject: Cannot Remove Python Libs from XP Message-ID: I have three py libs and the python program itself, 2.52, installed on an 6 year old HP Laptop. I decided to remove them, and took removed Python with the Control Panel ?Add/Remove icon. Worked fine. When I try to remove any of the remaining libs, press the add/remove button does nothing but blink. How do I get around this problem? From __peter__ at web.de Thu Dec 22 13:17:30 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 22 Dec 2011 19:17:30 +0100 Subject: Spanish Accents References: Message-ID: Stan Iverson wrote: > On Thu, Dec 22, 2011 at 12:42 PM, Peter Otten <__peter__ at web.de> wrote: > >> The file is probably encoded in ISO-8859-1, ISO-8859-15, or cp1252 then: >> >> >>> print "\xe1".decode("iso-8859-1") >> ? >> >>> print "\xe1".decode("iso-8859-15") >> ? >> >>> print "\xe1".decode("cp1252") >> ? >> >> Try codecs.open() with one of these encodings. >> > > I'm baffled. I duplicated your print statements but when I run this code > (or any of the 3 encodings): > > file = codecs.open(p + "2.txt", "r", "cp1252") > #file = codecs.open(p + "2.txt", "r", "utf-8") > for line in file: > print line > > I get this error: > > *UnicodeEncodeError*: 'ascii' codec can't encode character u'\xe1' in > position 48: ordinal not in range(128) You are now one step further, you have successfully* decoded the file. The remaining step is to encode the resulting unicode lines back into bytes. The encoding implicitly used by the print statement is sys.stdout.encoding which is either "ascii" or None in your case. Try to encode explicitly to UTF-8 with f = codecs.open(p + "2.txt", "r", "iso-8859-1") for line in f: print line.encode("utf-8") (*) This is however no big achievement as two (ISO-8859-1, ISO-8859-15) of the above codecs would not even balk on a binary file, e.g. a jpeg. They offer a character for every possible byte value. From kumar.mcmillan at gmail.com Thu Dec 22 13:24:13 2011 From: kumar.mcmillan at gmail.com (Kumar McMillan) Date: Thu, 22 Dec 2011 12:24:13 -0600 Subject: [TIP] Anyone still using Python 2.5? In-Reply-To: <4EF187A2.6070909@simplistix.co.uk> References: <4EF187A2.6070909@simplistix.co.uk> Message-ID: On Wed, Dec 21, 2011 at 1:15 AM, Chris Withers wrote: > Hi All, > > What's the general consensus on supporting Python 2.5 nowadays? > If you compile mod_wsgi with Apache you are stuck on the version of Python you compiled with. I had an old server stuck on Python 2.5 for this reason but I finally got a new box where I will be stuck on Python 2.7 for a while. There's probably a better way with gunicorn or something but Apache is pretty sweet when you configure it right. btw, tox is great for developing a project that supports multiple Pythons: http://tox.testrun.org/latest/ > > Do people still have to use this in commercial environments or is everyone > on 2.6+ nowadays? > > I'm finally getting some continuous integration set up for my packages and > it's highlighting some 2.5 compatibility issues. I'm wondering whether to > fix those (lots of ugly "from __future__ import with_statement" everywhere) > or just to drop Python 2.5 support. > > What do people feel? > > cheers, > > Chris > > -- > Simplistix - Content Management, Batch Processing & Python Consulting > - http://www.simplistix.co.uk > > ______________________________**_________________ > testing-in-python mailing list > testing-in-python at lists.idyll.**org > http://lists.idyll.org/**listinfo/testing-in-python > -------------- next part -------------- An HTML attachment was scrubbed... URL: From raymond.hettinger at gmail.com Thu Dec 22 15:01:25 2011 From: raymond.hettinger at gmail.com (Raymond Hettinger) Date: Thu, 22 Dec 2011 12:01:25 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <28c19da6-8ad8-42e9-bb67-964a39672c0b@s10g2000prs.googlegroups.com> On Dec 21, 9:57?am, Nathan Rice wrote: > +1 for IPython/%edit using the simplest editor that supports syntax > highlighting and line numbers. ?I have found that > Exploring/Prototyping in the interpreter has the highest ROI of > anything I teach people. Thank you Nathan and all the other respondents for your thoughtful answers. Raymond From bahamutzero8825 at gmail.com Thu Dec 22 15:27:31 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 22 Dec 2011 14:27:31 -0600 Subject: IPC with multiprocessing.connection Message-ID: <4EF392B3.2050005@gmail.com> I'm trying to set up a system where my main program launches external programs and then establishes connections to them via named pipes or Unix domain sockets (depending on platform) with multiprocessing.connection.Listener. The issue I'm having is with the accept() method. If there is nothing on the other side of the pipe/socket, it just hangs. Connection objects have a poll() method that can timeout, but I see no way to continue the program if there's no initial connection. What I'd like to do is let it time out if there's nothing on the other side. I'm pretty new to the multiprocessing module (and IPC in general), so I could've easily missed something. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From saqib.ali.75 at gmail.com Thu Dec 22 15:53:41 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Thu, 22 Dec 2011 12:53:41 -0800 (PST) Subject: Can't I define a decorator in a separate file and import it? Message-ID: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> I'm using this decorator to implement singleton class in python: http://stackoverflow.com/posts/7346105/revisions The strategy described above works if and only if the Singleton is declared and defined in the same file. If it is defined in a different file and I import that file, it doesn't work. Why can't I import this Singleton decorator from a different file? What's the best work around? From ethan at stoneleaf.us Thu Dec 22 16:07:34 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 22 Dec 2011 13:07:34 -0800 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> Message-ID: <4EF39C16.4090803@stoneleaf.us> Saqib Ali wrote: > > I'm using this decorator to implement singleton class in python: > > http://stackoverflow.com/posts/7346105/revisions > > The strategy described above works if and only if the Singleton is > declared and defined in the same file. If it is defined in a different > file and I import that file, it doesn't work. > > Why can't I import this Singleton decorator from a different file? > What's the best work around? Post the code, and the traceback. ~Ethan~ From saqib.ali.75 at gmail.com Thu Dec 22 16:09:20 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Thu, 22 Dec 2011 13:09:20 -0800 (PST) Subject: Can't I define a decorator in a separate file and import it? References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> Message-ID: <7fb2b7f5-12a2-4724-88e2-53991004d079@d8g2000vbb.googlegroups.com> BTW Here is the traceback: >>> import myClass Traceback (most recent call last): File "", line 1, in File "myClass.py", line 6, in @Singleton TypeError: 'module' object is not callable Here is Singleton.py: class Singleton: def __init__(self, decorated): self._decorated = decorated def Instance(self): try: return self._instance except AttributeError: self._instance = self._decorated() return self._instance def __call__(self): raise TypeError( 'Singletons must be accessed through the `Instance` method.') Here is myClass.py: #!/usr/bin/env python import os, sys, string, time, re, subprocess import Singleton @Singleton class myClass: def __init__(self): print 'Constructing myClass' def __del__(self): print 'Destructing myClass' From saqib.ali.75 at gmail.com Thu Dec 22 16:11:11 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Thu, 22 Dec 2011 16:11:11 -0500 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: <4EF39C16.4090803@stoneleaf.us> References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <4EF39C16.4090803@stoneleaf.us> Message-ID: MYCLASS.PY: #!/usr/bin/env python import os, sys, string, time, re, subprocess import Singleton @Singleton class myClass: def __init__(self): print 'Constructing myClass' def __del__(self): print 'Destructing myClass' SINGLETON.PY: #!/usr/bin/env python import os, sys, string, time, re, subprocess import Singleton @Singleton class myClass: def __init__(self): print 'Constructing myClass' def __del__(self): print 'Destructing myClass' TRACEBACK: >>> import myClass Traceback (most recent call last): File "", line 1, in File "myClass.py", line 6, in @Singleton TypeError: 'module' object is not callable - Saqib >> > Post the code, and the traceback. > > ~Ethan~ -------------- next part -------------- An HTML attachment was scrubbed... URL: From ckaynor at zindagigames.com Thu Dec 22 16:15:32 2011 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Thu, 22 Dec 2011 13:15:32 -0800 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <4EF39C16.4090803@stoneleaf.us> Message-ID: On Thu, Dec 22, 2011 at 1:11 PM, Saqib Ali wrote: > MYCLASS.PY: > > #!/usr/bin/env python > import os, sys, string, time, re, subprocess > import Singleton > This imports the module Singleton, not the class or function. There are two options to deal with this: from Singleton import Singleton imports the name Singleton from the module Singleton, which will be what you are probably expecting. If you wish to keep the namespace, you can also change your call from @Singleton to @Singleton.Singleton > > @Singleton > class myClass: > > def __init__(self): > print 'Constructing myClass' > > def __del__(self): > print 'Destructing myClass' > > > SINGLETON.PY: > > > #!/usr/bin/env python > import os, sys, string, time, re, subprocess > import Singleton > > > @Singleton > class myClass: > > def __init__(self): > print 'Constructing myClass' > > def __del__(self): > print 'Destructing myClass' > > TRACEBACK: > > >>> import myClass > Traceback (most recent call last): > File "", line 1, in > File "myClass.py", line 6, in > @Singleton > TypeError: 'module' object is not callable > > > > - Saqib > > > > > >>> >> Post the code, and the traceback. >> >> ~Ethan~ > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bahamutzero8825 at gmail.com Thu Dec 22 16:20:02 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Thu, 22 Dec 2011 15:20:02 -0600 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <4EF39C16.4090803@stoneleaf.us> Message-ID: <4EF39F02.8040201@gmail.com> You have the same code for both files... From joshua.landau.ws at gmail.com Thu Dec 22 16:22:53 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Thu, 22 Dec 2011 21:22:53 +0000 Subject: Elementwise -//- first release -//- Element-wise (vectorized) function, method and operator support for iterables in python. In-Reply-To: References: Message-ID: On 22 December 2011 11:15, Robert Kern wrote: > On 12/21/11 6:52 PM, Joshua Landau wrote: > > If fix means "return number" then no. This inconsistency between >> elementwise >> operations and 'normal' ones is the problem. Again, explicit >> elementwise-ifying >> through "~" fixes that. You need to tell me why this is worth the >> confusion over >> that. >> > > I understand that you are a supporter of PEP 225, but please have some > courtesy towards other developers. We can explore alternatives in parallel. > If we all worked on exactly the same thing (much less the one thing that > only you get to choose), we would forgo a lot of valuable experience. You > can point out the flaws of Elementwise's implementation and compare it to > the virtues of PEP 225 without being so demanding. He doesn't "need to > tell" you a damn thing. OK. Sorry. I had a feeling my language was a bit blunt. -------------- next part -------------- An HTML attachment was scrubbed... URL: From usenet at solar-empire.de Thu Dec 22 16:26:02 2011 From: usenet at solar-empire.de (Marc Christiansen) Date: Thu, 22 Dec 2011 22:26:02 +0100 Subject: Can't I define a decorator in a separate file and import it? References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> Message-ID: Saqib Ali wrote: > I'm using this decorator to implement singleton class in python: > > http://stackoverflow.com/posts/7346105/revisions > > The strategy described above works if and only if the Singleton is > declared and defined in the same file. If it is defined in a different > file and I import that file, it doesn't work. > > Why can't I import this Singleton decorator from a different file? Maybe you're doing something wrong. But since you didn't provide any code, it's impossible to tell what. > What's the best work around? As far as I can tell with the information you have given, none is needed. See below: 0:> python2.7 singleton_test.py # works also with python3.2 Foo created True #singleton_test.py from singleton import Singleton # Singleton is the class from Paul Manta from the SO page # and singleton.py contains nothing else @Singleton class Foo: def __init__(self): print('Foo created') f = Foo.Instance() g = Foo.Instance() print(f is g) Gr??e Marc From saqib.ali.75 at gmail.com Thu Dec 22 16:29:04 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Thu, 22 Dec 2011 16:29:04 -0500 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: <4EF3A1C9.705@stoneleaf.us> References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <4EF39C16.4090803@stoneleaf.us> <4EF3A1C9.705@stoneleaf.us> Message-ID: Thanks for pointing out the mistake! Works. - Saqib On Thu, Dec 22, 2011 at 4:31 PM, Ethan Furman wrote: > Saqib Ali wrote: > >> MYCLASS.PY: >> >> #!/usr/bin/env python >> import os, sys, string, time, re, subprocess >> import Singleton >> > > This should be 'from Singleton import Singleton' > > > > @Singleton >> class myClass: >> def __init__(self): >> print 'Constructing myClass' >> > > At this point, the *instance* of myClass has already been constructed and > it is now being initialized. The __new__ method is where the instance is > actually created. > > > >> def __del__(self): >> print 'Destructing myClass' >> >> >> > class Singleton: > > > > def __init__(self, decorated): > > self._decorated = decorated > > > > def Instance(self): > > try: > > return self._instance > > except AttributeError: > > self._instance = self._decorated() > > return self._instance > > > > def __call__(self): > > raise TypeError( > > 'Singletons must be accessed through the `Instance` > > method.') > > > ~Ethan~ > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ethan at stoneleaf.us Thu Dec 22 16:31:53 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Thu, 22 Dec 2011 13:31:53 -0800 Subject: Can't I define a decorator in a separate file and import it? In-Reply-To: References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <4EF39C16.4090803@stoneleaf.us> Message-ID: <4EF3A1C9.705@stoneleaf.us> Saqib Ali wrote: > MYCLASS.PY: > > #!/usr/bin/env python > import os, sys, string, time, re, subprocess > import Singleton This should be 'from Singleton import Singleton' > @Singleton > class myClass: > > def __init__(self): > print 'Constructing myClass' At this point, the *instance* of myClass has already been constructed and it is now being initialized. The __new__ method is where the instance is actually created. > > def __del__(self): > print 'Destructing myClass' > > > class Singleton: > > def __init__(self, decorated): > self._decorated = decorated > > def Instance(self): > try: > return self._instance > except AttributeError: > self._instance = self._decorated() > return self._instance > > def __call__(self): > raise TypeError( > 'Singletons must be accessed through the `Instance` > method.') ~Ethan~ From ben+python at benfinney.id.au Thu Dec 22 17:33:22 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 23 Dec 2011 09:33:22 +1100 Subject: Can't I define a decorator in a separate file and import it? References: <53a106b8-1a2f-4b9d-9cac-d6f7dab01696@q11g2000vbq.googlegroups.com> <7fb2b7f5-12a2-4724-88e2-53991004d079@d8g2000vbb.googlegroups.com> Message-ID: <87d3bgkzil.fsf@benfinney.id.au> Saqib Ali writes: > BTW Here is the traceback: > > >>> import myClass > Traceback (most recent call last): > File "", line 1, in > File "myClass.py", line 6, in > @Singleton > TypeError: 'module' object is not callable Yes. When you ?import foo?, you have a module bound to the name ?foo?. A module is not callable. > Here is Singleton.py: You should name the file ?singleton.py? instead; modules should be named in all lower-case, as PEP 8 recommends. > Here is myClass.py: Which should be named ?my_class.py?, instead, by the same rationale. Neither of those is necessary, but it will help dispel the confusion you're experiencing, and help your code be more easily comprehensible to other Python programmers. > #!/usr/bin/env python > import os, sys, string, time, re, subprocess > import Singleton So, you should (after the renames suggested) do either:: import singleton @singleton.Singleton class my_class: or:: from singleton import Singleton @Singleton class my_class: Notice how naming the module in lower-case makes it more easily distinguishable from the class name. More generally, remember that Python is not Java , and you should be grouping your classes into logically coherent files, not one-file-per-class. -- \ ?Reality must take precedence over public relations, for nature | `\ cannot be fooled.? ?Richard P. Feynman, _Rogers' Commission | _o__) Report into the Challenger Crash_, 1986-06 | Ben Finney From tjreedy at udel.edu Thu Dec 22 18:13:42 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 22 Dec 2011 18:13:42 -0500 Subject: Adding an interface to existing classes In-Reply-To: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> Message-ID: On 12/22/2011 3:21 AM, Spencer Pearson wrote: > I'm writing a geometry package, with Points and Lines and Circles and > so on, and eventually I want to be able to draw these things on the > screen. I have two options so far for how to accomplish this, but > neither of them sits quite right with me, and I'd like the opinion of > comp.lang.python's wizened elders. > > Option 1. Subclassing. > The most Pythonic way would seem to be writing subclasses for the > things I want to display, adding a ".draw(...)" method to each one, > like this: > Option 2. A "draw" function, with a function dictionary. Option 3? Add a draw method to existing classes, rather than subclassing? -- Terry Jan Reedy From iversonstan at gmail.com Thu Dec 22 18:14:56 2011 From: iversonstan at gmail.com (Stan Iverson) Date: Thu, 22 Dec 2011 19:14:56 -0400 Subject: Spanish Accents In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 2:17 PM, Peter Otten <__peter__ at web.de> wrote: > You are now one step further, you have successfully* decoded the file. > The remaining step is to encode the resulting unicode lines back into > bytes. > The encoding implicitly used by the print statement is sys.stdout.encoding > which is either "ascii" or None in your case. Try to encode explicitly to > UTF-8 with > > f = codecs.open(p + "2.txt", "r", "iso-8859-1") > for line in f: > print line.encode("utf-8") > > > OOEEE! Thanks! Stan -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Thu Dec 22 18:39:34 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 22 Dec 2011 18:39:34 -0500 Subject: Grammar for classes In-Reply-To: References: Message-ID: On 12/20/2011 12:05 PM, Joshua Landau wrote: > On 20 December 2011 10:55, Robert Kern > wrote: > > On 12/20/11 1:34 AM, Joshua Landau wrote: > > In reading thorough the syntax defined in the reference > >, > > the class statement has surprised me. > > It says that the inheritance part of the class can accept > comprehensions. What > does this mean? Beats me. The 2.7 doc says inheritance ::= "(" [expression_list] ")" and I no on no 3.x change/addition. My experiments with both genexp and listcomp versions gave errors. > I ignore valid code. Thanks Ian and Robert for the input. Should I file > a documentation bug report? Please do. It the addition means something, it needs to be explained in the text. If it is wrong, it should go. -- Terry Jan Reedy From macsmith.us at gmail.com Thu Dec 22 19:33:12 2011 From: macsmith.us at gmail.com (Mac Smith) Date: Fri, 23 Dec 2011 06:03:12 +0530 Subject: reading multiline output References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> Message-ID: Hi, I have started HandBrakeCLI using subprocess.popen but the output is multiline and not terminated with \n so i am not able to read it using readline() while the HandBrakeCLI is running. kindly suggest some alternative. i have attached the output in a file. -------------- next part -------------- A non-text attachment was scrubbed... Name: output Type: application/octet-stream Size: 4541 bytes Desc: not available URL: -------------- next part -------------- -- Thanks Mac From python at mrabarnett.plus.com Thu Dec 22 19:47:07 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 23 Dec 2011 00:47:07 +0000 Subject: reading multiline output In-Reply-To: References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> Message-ID: <4EF3CF8B.4060005@mrabarnett.plus.com> On 23/12/2011 00:33, Mac Smith wrote: > Hi, > > > I have started HandBrakeCLI using subprocess.popen but the output is > multiline and not terminated with \n so i am not able to read it > using readline() while the HandBrakeCLI is running. kindly suggest > some alternative. i have attached the output in a file. > The lines are terminated with \r, so read with read() and then split on "\r". From macsmith.us at gmail.com Thu Dec 22 20:07:12 2011 From: macsmith.us at gmail.com (Mac Smith) Date: Fri, 23 Dec 2011 06:37:12 +0530 Subject: reading multiline output In-Reply-To: <4EF3CF8B.4060005@mrabarnett.plus.com> References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> Message-ID: On 23-Dec-2011, at 6:17 AM, MRAB wrote: > On 23/12/2011 00:33, Mac Smith wrote: >> Hi, >> >> >> I have started HandBrakeCLI using subprocess.popen but the output is >> multiline and not terminated with \n so i am not able to read it >> using readline() while the HandBrakeCLI is running. kindly suggest >> some alternative. i have attached the output in a file. >> > The lines are terminated with \r, so read with read() and then split on > "\r". read() will read the complete output and than i will be able to parse it, i want to read the output of the command in realtime. From python at mrabarnett.plus.com Thu Dec 22 20:18:48 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 23 Dec 2011 01:18:48 +0000 Subject: reading multiline output In-Reply-To: References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> Message-ID: <4EF3D6F8.3020203@mrabarnett.plus.com> On 23/12/2011 01:07, Mac Smith wrote: > > On 23-Dec-2011, at 6:17 AM, MRAB wrote: > >> On 23/12/2011 00:33, Mac Smith wrote: >>> Hi, >>> >>> >>> I have started HandBrakeCLI using subprocess.popen but the output >>> is multiline and not terminated with \n so i am not able to read >>> it using readline() while the HandBrakeCLI is running. kindly >>> suggest some alternative. i have attached the output in a file. >>> >> The lines are terminated with \r, so read with read() and then >> split on "\r". > > read() will read the complete output and than i will be able to parse > it, i want to read the output of the command in realtime. > Try telling it how much to read with read(size): def read_lines(output, line_ending="\n"): buffer = "" while True: chunk = output.read(1024) if not chunk: break buffer += chunk while True: pos = buffer.find(line_ending) if pos < 0: break pos += len(line_ending) yield buffer[ : pos] buffer = buffer[pos : ] if buffer: yield buffer From cs at zip.com.au Thu Dec 22 20:21:40 2011 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 23 Dec 2011 12:21:40 +1100 Subject: reading multiline output In-Reply-To: References: Message-ID: <20111223012140.GA29153@cskk.homeip.net> On 23Dec2011 06:37, Mac Smith wrote: | On 23-Dec-2011, at 6:17 AM, MRAB wrote: | > On 23/12/2011 00:33, Mac Smith wrote: | >> I have started HandBrakeCLI using subprocess.popen but the output is | >> multiline and not terminated with \n so i am not able to read it | >> using readline() while the HandBrakeCLI is running. kindly suggest | >> some alternative. i have attached the output in a file. | >> | > The lines are terminated with \r, so read with read() and then split on | > "\r". | | read() will read the complete output and than i will be able to parse | it, i want to read the output of the command in realtime. You can read a limited number of bytes - for your purposes this needs to be 1 byte at a time. Very inefficient, but you can do what you need. Write a little function that gathers single bytes until "\r" and then returns them joined into a string. Traditionally the way to work with "interactive" facilities like this is often the "expect" command, and I gather there's a third party Python module named "pexpect" with that functionality. This: http://www.noah.org/wiki/pexpect And then there's all sorts of read-with-timeout games you can play I guess... Note that all this relies on HandBrakeCLI emitting its progress messages in a timely (== unbuffered) manner when its output is a pipe... Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ It's better to be DoD and cool, than to get a life and be uncool! - _Harley Davidson & The Marlboro Man_ (slightly paraphrased by Roar Larsen, DoD#463 ) From ian.g.kelly at gmail.com Thu Dec 22 20:27:54 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 22 Dec 2011 18:27:54 -0700 Subject: Grammar for classes In-Reply-To: References: Message-ID: On Thu, Dec 22, 2011 at 4:39 PM, Terry Reedy wrote: > Beats me. The 2.7 doc says inheritance ::= ?"(" [expression_list] ")" and I > no on no 3.x change/addition. Well, there is one change in 3.x which is that the inheritance list now accepts keyword parameters (PEP 3115) -- the "metaclass" keyword specifies the metaclass instead of using a bogus __metaclass__ attribute, and any other keywords are passed along to the metaclass. That's why the parsing of the inheritance list was generalized to look more like a function call, but it doesn't explain why the class declaration might wish to receive a comprehension. Cheers, Ian From macsmith.us at gmail.com Thu Dec 22 21:05:23 2011 From: macsmith.us at gmail.com (Mac Smith) Date: Fri, 23 Dec 2011 07:35:23 +0530 Subject: reading multiline output In-Reply-To: <4EF3D6F8.3020203@mrabarnett.plus.com> References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> <4EF3D6F8.3020203@mrabarnett.plus.com> Message-ID: <2D5264F3-A1EB-4C68-85E2-A39C5C67E699@gmail.com> On 23-Dec-2011, at 6:48 AM, MRAB wrote: > On 23/12/2011 01:07, Mac Smith wrote: >> >> On 23-Dec-2011, at 6:17 AM, MRAB wrote: >> >>> On 23/12/2011 00:33, Mac Smith wrote: >>>> Hi, >>>> >>>> >>>> I have started HandBrakeCLI using subprocess.popen but the output >>>> is multiline and not terminated with \n so i am not able to read >>>> it using readline() while the HandBrakeCLI is running. kindly >>>> suggest some alternative. i have attached the output in a file. >>>> >>> The lines are terminated with \r, so read with read() and then >>> split on "\r". >> >> read() will read the complete output and than i will be able to parse >> it, i want to read the output of the command in realtime. >> > Try telling it how much to read with read(size): > > def read_lines(output, line_ending="\n"): > buffer = "" > > while True: > chunk = output.read(1024) > if not chunk: > break > > buffer += chunk > > while True: > pos = buffer.find(line_ending) > if pos < 0: > break > > pos += len(line_ending) > yield buffer[ : pos] > buffer = buffer[pos : ] > > if buffer: > yield buffer > -- > http://mail.python.org/mailman/listinfo/python-list thanks, this helped. just need to correct line_ending="\n" should be line_ending="\r" From wuwei23 at gmail.com Thu Dec 22 21:10:46 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 22 Dec 2011 18:10:46 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: On Dec 22, 6:51?pm, Rolf Camps wrote: > I'm afraid it's dangerous to encourage the use of '[]' as assignment to > a parameter in a function definition. If you use the function several > times 'default' always points to the same list. I appreciate the concern, but adding a default argument guard would not only obscure the code. It's irrelevant, as you recognise, because no matter what, it's going to make copies of the default argument. You know what the say about foolish consistencies :) From python at mrabarnett.plus.com Thu Dec 22 21:14:36 2011 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 23 Dec 2011 02:14:36 +0000 Subject: reading multiline output In-Reply-To: <2D5264F3-A1EB-4C68-85E2-A39C5C67E699@gmail.com> References: <79DF8234-67BF-4297-ACE6-8D091D05B3E9@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> <4EF3D6F8.3020203@mrabarnett.plus.com> <2D5264F3-A1EB-4C68-85E2-A39C5C67E699@gmail.com> Message-ID: <4EF3E40C.6060705@mrabarnett.plus.com> On 23/12/2011 02:05, Mac Smith wrote: > > On 23-Dec-2011, at 6:48 AM, MRAB wrote: > >> On 23/12/2011 01:07, Mac Smith wrote: >>> >>> On 23-Dec-2011, at 6:17 AM, MRAB wrote: >>> >>>> On 23/12/2011 00:33, Mac Smith wrote: >>>>> Hi, >>>>> >>>>> >>>>> I have started HandBrakeCLI using subprocess.popen but the output >>>>> is multiline and not terminated with \n so i am not able to read >>>>> it using readline() while the HandBrakeCLI is running. kindly >>>>> suggest some alternative. i have attached the output in a file. >>>>> >>>> The lines are terminated with \r, so read with read() and then >>>> split on "\r". >>> >>> read() will read the complete output and than i will be able to parse >>> it, i want to read the output of the command in realtime. >>> >> Try telling it how much to read with read(size): >> >> def read_lines(output, line_ending="\n"): >> buffer = "" >> >> while True: >> chunk = output.read(1024) >> if not chunk: >> break >> >> buffer += chunk >> >> while True: >> pos = buffer.find(line_ending) >> if pos< 0: >> break >> >> pos += len(line_ending) >> yield buffer[ : pos] >> buffer = buffer[pos : ] >> >> if buffer: >> yield buffer > > thanks, this helped. just need to correct line_ending="\n" should be line_ending="\r" > I wrote the default as "\n" because that's the normal line ending in Python. If, as in your case, the line ending is different, just pass the appropriate string as the second argument. From ian.g.kelly at gmail.com Thu Dec 22 21:59:06 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 22 Dec 2011 19:59:06 -0700 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: On Thu, Dec 22, 2011 at 7:10 PM, alex23 wrote: > On Dec 22, 6:51?pm, Rolf Camps wrote: >> I'm afraid it's dangerous to encourage the use of '[]' as assignment to >> a parameter in a function definition. If you use the function several >> times 'default' always points to the same list. > > I appreciate the concern, but adding a default argument guard would > not only obscure the code. It's irrelevant, as you recognise, because > no matter what, it's going to make copies of the default argument. It's only irrelevant in the immediate context of the code you posted. But when Joe Novice sees your code and likes it and duplicates it a million times without receiving any warning about it, he's eventually going to write a function that modifies its default list argument, and he'll be in for a nasty surprise when he does. From rustompmody at gmail.com Thu Dec 22 22:05:54 2011 From: rustompmody at gmail.com (rusi) Date: Thu, 22 Dec 2011 19:05:54 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <643c3931-db20-44ce-80b4-5f4677cc8da3@18g2000prn.googlegroups.com> On Dec 21, 9:57?pm, Nathan Rice wrote: > +1 for IPython/%edit using the simplest editor that supports syntax > highlighting and line numbers. ?I have found that > Exploring/Prototyping in the interpreter has the highest ROI of > anything I teach people. > > Nathan It seems to me that we are not distinguishing different 'scales'. For example: In principle one can use any unit to measure length. In practice it is inconvenient to use kilometers when microns or angstroms are more appropriate. Clearly there is a need, when learning a language, to explore the basics with a minimum of interference from the environment ie introspection features of python with as little extra interference as possible. As a teacher I generally demonstrate with python-mode in emacs. I guess IDLE, ipython etc should be equivalent. But then someone in the class comes with a 'question' of this sort: We have this project in eclipse+python. But the path is not being searched properly. etc Now for me eclipse is more of pizazz on the outside and nightmare on the inside. [Or maybe its just that I am particularly bad at solving these kinds of problems]. Nevertheless this scale or granularity of work is also important in python education and is sorely underrepresented. Here are some of my earlier attempts to canvass for this orientation: http://mail.python.org/pipermail/python-list/2011-May/1271749.html http://mail.python.org/pipermail/python-list/2011-November/1283634.html From wuwei23 at gmail.com Thu Dec 22 22:40:11 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 22 Dec 2011 19:40:11 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: On Dec 23, 12:59?pm, Ian Kelly wrote: > It's only irrelevant in the immediate context of the code you posted. > But when Joe Novice sees your code and likes it and duplicates it a > million times I'm sorry, but I'm not going to modify my coding style for the sake of bad programmers. The context is _important_. Why should I guard against default argument mutability when its not going to occur in the function body? From einazaki668 at yahoo.com Thu Dec 22 22:46:09 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 22 Dec 2011 19:46:09 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: <1ec8f662-f4ef-4f99-b476-5e5ae2a3b412@p41g2000yqm.googlegroups.com> On Dec 21, 6:50?pm, alex23 wrote: > On Dec 22, 8:25?am, Eric wrote: > > > This surprises me, can someone tell me why it shouldn't? ?I figure if > > I want to create and initialize three scalars the just do "a=b=c=7", > > for example, so why not extend it to arrays. > > The thing to remember is that everything is an object, and that it's > better to think of variables as labels on an object. > > So: a=b=c=7 means that _one_ integer object with the value of 7 can be > referenced using any of the labels a, b or c. x=y=z=[] means that > _one_ empty list can be referenced using x, y or z. > > The difference is that the value of a number object _cannot be > changed_ ('immutable') while a list can be modified to add or remove > items ('mutable'). a=10 just reassigns the label a to an integer > object of value 10. x.append("foo") _modifies_ the list referred to by > x, which is the same list known as y & z. > > > Also, is there a more pythonic way to do "x=[], y=[], z=[]"? > > I'd say that _is_ the most pythonic way, it's very obvious in its > intent (or would be with appropriate names). If it bothers you that > much: > Thanks for the explanation. I guess from what I've seen of Python so far I was expecting something more, I don't know, compact. Anyway, it doesn't bother me, at least not enough to go and do something like this: > ? ? def listgen(count, default=[]): > ? ? ? ? for _ in xrange(count): > ? ? ? ? ? ? yield default[:] > > ? ? x, y, z = listgen(3) Thanks, eric From python.list at tim.thechases.com Thu Dec 22 23:16:30 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 22 Dec 2011 22:16:30 -0600 Subject: Idiom for shelling out to $EDITOR/$PAGER? Message-ID: <4EF4009E.8010109@tim.thechases.com> After a little searching, I've not been able to come up with what I'd consider canonical examples of consider calling an external editor/pager on a file and reading the results back in. (most of my results are swamped by people asking about editors written in Python, or what the best editors for Python code are) The pseudocode would be something like def edit_text(data): temp_fname = generate_temp_name() try: f = file(temp_fname, 'w') f.write(data) f.close() before = info(temp_fname) # maybe stat+checksum? editor = find_sensible_editor() subprocess.call([editor, temp_fname]) if before == info(temp_fname): return None else: return file(temp_fname).read() finally: delete_if_exists(temp_fname) However there are things to watch out for in this lousy code: -race conditions, unique naming, and permissions on the temp file -proper & efficient detection of file-change, to know whether the user actually did anything -cross-platform determination of a sensible editor (that blocks rather than spawns), using platform conventions like os.environ['EDITOR'] -cleanup deletion of the temp-file I presume the code for spawning $PAGER on some content would look pretty similar. Any good example code (or blog posts, or other links) that has been battle-tested? Thanks, -tkc From rosuav at gmail.com Thu Dec 22 23:25:51 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 15:25:51 +1100 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: On Fri, Dec 23, 2011 at 2:40 PM, alex23 wrote: > I'm sorry, but I'm not going to modify my coding style for the sake of > bad programmers. And there, folks, you have one of the eternal dilemmas. The correct decision depends on myriad factors; if you're writing code to go into the documentation as an example, you want it to be able to handle idiots hacking on it - but on the other hand, that same situation demands simplicity, which is why a lot of examples omit huge slabs of error checking. ChrisA From einazaki668 at yahoo.com Thu Dec 22 23:27:57 2011 From: einazaki668 at yahoo.com (Eric) Date: Thu, 22 Dec 2011 20:27:57 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <4ef26f74$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <845617ed-2e20-410f-90c7-ddafac2867bf@v13g2000yqc.googlegroups.com> On Dec 21, 5:44?pm, Steven D'Aprano wrote: > Yes, you should create your lists before trying to append to them. > > But you aren't forced to use a for-loop. You can use a list comprehension: > > x = [some_function(a) for a in range(n)] > > Notice that here you don't need x to pre-exist, because the list comp > creates a brand new list, which then gets assigned directly to x. > > > Now to my actual question. ?I need to do the above for multiple arrays > > (all the same, arbitrary size). ?So I do this: > > ? ?x=y=z=[] > > This creates one empty list object, and gives it three names, x, y and z. > Every time you append to the list, all three names see the same change, > because they refer to a single list. > > [...] > > > Except it seems that I didn't create three different arrays, I created > > one array that goes by three different names (i.e. x[], y[] and z[] all > > reference the same pile of numbers, no idea which pile). > > Exactly. > > > This surprises me, can someone tell me why it shouldn't? > > Because that's the way Python works. Python is an object-oriented, name > binding language. This is how OO name binding works: you have a single > object, with three names bound to it. The above line is short-cut for: > > a = [] > b = a > c = a > > Python does not make a copy of the list unless you specifically instruct > it to. > > > I figure if I > > want to create and initialize three scalars the just do "a=b=c=7", > > That creates a single integer object with value 7, and binds three names > to it, *exactly* the same as the above. > > If you could modify int objects in place, like you can modify lists in > place, you would see precisely the same effect. But ints are immutable: > all operations on ints create new ints. Lists are mutable, and can be > changed in place. > > > for > > example, so why not extend it to arrays. ?Also, is there a more pythonic > > way to do "x=[], y=[], z=[]"? > > Well that literally won't work, you can't separate them by commas. > Newlines or semicolons will work. > > Or: x, y, z = [], [], [] > > Either is pretty Pythonic. > > -- > Steven Thanks to you and Dennis for the quick lesson and tips. Very helpful and illuminating. From ian.g.kelly at gmail.com Fri Dec 23 00:22:49 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 22 Dec 2011 22:22:49 -0700 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: On Thu, Dec 22, 2011 at 8:40 PM, alex23 wrote: > On Dec 23, 12:59?pm, Ian Kelly wrote: >> It's only irrelevant in the immediate context of the code you posted. >> But when Joe Novice sees your code and likes it and duplicates it a >> million times > > I'm sorry, but I'm not going to modify my coding style for the sake of > bad programmers. Nobody is asking you to modify your coding style. The request is that you not throw it up as an example without mentioning the important caveats. Also, novice programmer == bad programmer? From wuwei23 at gmail.com Fri Dec 23 01:00:51 2011 From: wuwei23 at gmail.com (alex23) Date: Thu, 22 Dec 2011 22:00:51 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: <7d519515-e278-4159-a35e-36bcafa81c73@c42g2000prb.googlegroups.com> On Dec 23, 3:22?pm, Ian Kelly wrote: > Nobody is asking you to modify your coding style. ?The request is that > you not throw it up as an example without mentioning the important > caveats. No, 100% no. It's not my responsibility to mention every potentially relevant gotcha when providing example code. > Also, novice programmer == bad programmer? If they're wholly learning how to code by throwaway examples on mailing lists, then yes. Object mutability is a _major_ aspect of Python; I'm simply not going to inject an essay explaining what that implies every time I choose to use a mutable default argument. From cs at zip.com.au Fri Dec 23 01:02:18 2011 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 23 Dec 2011 17:02:18 +1100 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: <4EF4009E.8010109@tim.thechases.com> References: <4EF4009E.8010109@tim.thechases.com> Message-ID: <20111223060218.GA28472@cskk.homeip.net> On 22Dec2011 22:16, Tim Chase wrote: | After a little searching, I've not been able to come up with what | I'd consider canonical examples of consider calling an external | editor/pager on a file and reading the results back in. (most of my | results are swamped by people asking about editors written in | Python, or what the best editors for Python code are) | | The pseudocode would be something like | | def edit_text(data): | temp_fname = generate_temp_name() | try: | f = file(temp_fname, 'w') | f.write(data) | f.close() | before = info(temp_fname) # maybe stat+checksum? | editor = find_sensible_editor() | subprocess.call([editor, temp_fname]) | if before == info(temp_fname): | return None | else: | return file(temp_fname).read() | finally: | delete_if_exists(temp_fname) | | However there are things to watch out for in this lousy code: | | -race conditions, unique naming, and permissions on the temp file NamedTemporaryFile is your friend. | -proper & efficient detection of file-change, to know whether the | user actually did anything Wait for the editor to exit? In that scenario I go for: - wait for edit to exit - if exit status 0 and file non-empty, trust it (subject to parse issues afterwards of course) | -cross-platform determination of a sensible editor (that blocks | rather than spawns), using platform conventions like | os.environ['EDITOR'] os.environment.get('EDITOR', 'vi')? Some platforms have an executable called "editor" that solves that problem (absent $EDITOR); MacOSX has "open", though it won't be running a blocking editor, alas. You may need some knowledge of the local system. On a terminal? Easy, use $EDITOR. No terminal, but $DISPLAY? xterm -e "$EDITOR temp_file", or the like. No terminal, no $DISPLAY, macosx? I have an incantation somewhere... | -cleanup deletion of the temp-file NamedTemporaryFile is your friend. | I presume the code for spawning $PAGER on some content would look | pretty similar. Yep. | Any good example code (or blog posts, or other links) that has been | battle-tested? Hmm. Nothing directly to had, but regarding the temp file: # write contents of the file `fp` into a temp file with NamedTemporaryFile('w', dir=os.path.join(self.dir, 'tmp')) as T: T.write(fp.read()) ... do stuff with T.name (the temp file name). Anyway, look it up; it has an autodelete mode etc. Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ I have always been a welly man myself. They are superb in wet grass, let alone lagoons of pig shit. - Julian Macassey From ben+python at benfinney.id.au Fri Dec 23 01:12:20 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 23 Dec 2011 17:12:20 +1100 Subject: Idiom for shelling out to $EDITOR/$PAGER? References: <4EF4009E.8010109@tim.thechases.com> Message-ID: <877h1nn7ej.fsf@benfinney.id.au> Cameron Simpson writes: > On 22Dec2011 22:16, Tim Chase wrote: > | -proper & efficient detection of file-change, to know whether the > | user actually did anything > > Wait for the editor to exit? > In that scenario I go for: > - wait for edit to exit > - if exit status 0 and file non-empty, trust it > (subject to parse issues afterwards of course) That doesn't address the concern Tim raised: did the user actually do anything, did the file change? The exit status of text editors are not bound to distinguish ?buffer was modified?, and certainly don't do so in any standard way. My advice: * Compute a before-edit hash of the text (MD5 or SHA-1 would be fine). * Invoke the editor on that text. * Wait for (or detect) the exit of the editor process. * Compute an after-edit hash of the text resulting from the editor. * Compare the hashes to see whether the text changed. -- \ ?Come on Milhouse, there's no such thing as a soul! It's just | `\ something they made up to scare kids, like the Boogie Man or | _o__) Michael Jackson.? ?Bart, _The Simpsons_ | Ben Finney From cs at zip.com.au Fri Dec 23 01:32:16 2011 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 23 Dec 2011 17:32:16 +1100 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: <877h1nn7ej.fsf@benfinney.id.au> References: <877h1nn7ej.fsf@benfinney.id.au> Message-ID: <20111223063216.GA32188@cskk.homeip.net> On 23Dec2011 17:12, Ben Finney wrote: | Cameron Simpson writes: | > On 22Dec2011 22:16, Tim Chase wrote: | > | -proper & efficient detection of file-change, to know whether the | > | user actually did anything | > | > Wait for the editor to exit? | > In that scenario I go for: | > - wait for edit to exit | > - if exit status 0 and file non-empty, trust it | > (subject to parse issues afterwards of course) | | That doesn't address the concern Tim raised: did the user actually do | anything, did the file change? I'm not sure it matters. It's _quicker_ to do nothing if the file is unchanged, but is it bad to act on it anyway? | The exit status of text editors are not bound to distinguish ?buffer was | modified?, and certainly don't do so in any standard way. Indeed not; my 0 above is just "did you user exit the editor or did it crash"? | My advice: | * Compute a before-edit hash of the text (MD5 or SHA-1 would be fine). | * Invoke the editor on that text. | * Wait for (or detect) the exit of the editor process. | * Compute an after-edit hash of the text resulting from the editor. | * Compare the hashes to see whether the text changed. Sure. But still, if you're able to act on a changed file, why not also act on an unchanged file? The user asked to change things; take what you got back and proceed! And personally, as a user, I've often rewritten an unchanged file to force a recompute of something. Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ "Don't you know the speed limit is 55 miles per hour???" "Yeah, but I wasn't going to be out that long." - Steven Wright From jeanpierreda at gmail.com Fri Dec 23 02:21:01 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 23 Dec 2011 02:21:01 -0500 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: <20111223060218.GA28472@cskk.homeip.net> References: <4EF4009E.8010109@tim.thechases.com> <20111223060218.GA28472@cskk.homeip.net> Message-ID: > Anyway, look it up; it has an autodelete mode etc. The autodelete isn't useful in this context. The file needs to be closed and then reopened again; with the autodelete option, the closing would delete the file, preventing it from being opened by the text editor or reopened to check contents. (As far as I know, the race condition inherent in this is unavoidable. At least on windows, but I couldn't figure out any other way on Linux either.) -- Devin On Fri, Dec 23, 2011 at 1:02 AM, Cameron Simpson wrote: > On 22Dec2011 22:16, Tim Chase wrote: > | After a little searching, I've not been able to come up with what > | I'd consider canonical examples of consider calling an external > | editor/pager on a file and reading the results back in. ?(most of my > | results are swamped by people asking about editors written in > | Python, or what the best editors for Python code are) > | > | The pseudocode would be something like > | > | ? def edit_text(data): > | ? ? temp_fname = generate_temp_name() > | ? ? try: > | ? ? ? f = file(temp_fname, 'w') > | ? ? ? f.write(data) > | ? ? ? f.close() > | ? ? ? before = info(temp_fname) # maybe stat+checksum? > | ? ? ? editor = find_sensible_editor() > | ? ? ? subprocess.call([editor, temp_fname]) > | ? ? ? if before == info(temp_fname): > | ? ? ? ? return None > | ? ? ? else: > | ? ? ? ? return file(temp_fname).read() > | ? ? finally: > | ? ? ? delete_if_exists(temp_fname) > | > | However there are things to watch out for in this lousy code: > | > | -race conditions, unique naming, and permissions on the temp file > > NamedTemporaryFile is your friend. > > | -proper & efficient detection of file-change, to know whether the > | user actually did anything > > Wait for the editor to exit? > In that scenario I go for: > ?- wait for edit to exit > ?- if exit status 0 and file non-empty, trust it > ? ?(subject to parse issues afterwards of course) > > | -cross-platform determination of a sensible editor (that blocks > | rather than spawns), using platform conventions like > | os.environ['EDITOR'] > > os.environment.get('EDITOR', 'vi')? > > Some platforms have an executable called "editor" that solves that > problem (absent $EDITOR); MacOSX has "open", though it won't be running > a blocking editor, alas. > > You may need some knowledge of the local system. On a terminal? Easy, > use $EDITOR. No terminal, but $DISPLAY? xterm -e "$EDITOR temp_file", > or the like. No terminal, no $DISPLAY, macosx? I have an incantation > somewhere... > > | -cleanup deletion of the temp-file > > NamedTemporaryFile is your friend. > > | I presume the code for spawning $PAGER on some content would look > | pretty similar. > > Yep. > > | Any good example code (or blog posts, or other links) that has been > | battle-tested? > > Hmm. Nothing directly to had, but regarding the temp file: > > ?# write contents of the file `fp` into a temp file > ?with NamedTemporaryFile('w', dir=os.path.join(self.dir, 'tmp')) as T: > ? ?T.write(fp.read()) > ?... do stuff with T.name (the temp file name). > > Anyway, look it up; it has an autodelete mode etc. > > Cheers, > -- > Cameron Simpson DoD#743 > http://www.cskk.ezoshosting.com/cs/ > > I have always been a welly man myself. They are superb in wet grass, let > alone lagoons of pig shit. ? ? ?- Julian Macassey > -- > http://mail.python.org/mailman/listinfo/python-list From yasar11732 at gmail.com Fri Dec 23 02:34:39 2011 From: yasar11732 at gmail.com (=?ISO-8859-9?Q?Ya=FEar_Arabac=FD?=) Date: Fri, 23 Dec 2011 09:34:39 +0200 Subject: Recommend blogs for me to follow Message-ID: Hi, I am looking for some recommendations about blogs that I may like to follow. I am interested in Python (that goes without saying), django and server side web development in general, linux tools, and git scm. I am looking for blogs which shares short examples of codes, and hints about various topics that I might be interested with. My favorite blog so far is http://www.saltycrane.com/blog/ as it gives quick tips aboug various topics that I am interested in. So can you suggest other blogs like that? I am considering this thread to become a reference point for people like me who wants to extent their knowledge about Python and world of programming in general by reading fellow programmers. -- http://yasar.serveblog.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri Dec 23 03:28:52 2011 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 Dec 2011 09:28:52 +0100 Subject: Idiom for shelling out to $EDITOR/$PAGER? References: <4EF4009E.8010109@tim.thechases.com> Message-ID: Tim Chase wrote: > After a little searching, I've not been able to come up with what > I'd consider canonical examples of consider calling an external > editor/pager on a file and reading the results back in. (most of > my results are swamped by people asking about editors written in > Python, or what the best editors for Python code are) > > The pseudocode would be something like > > def edit_text(data): > temp_fname = generate_temp_name() > try: > f = file(temp_fname, 'w') > f.write(data) > f.close() > before = info(temp_fname) # maybe stat+checksum? > editor = find_sensible_editor() > subprocess.call([editor, temp_fname]) > if before == info(temp_fname): > return None > else: > return file(temp_fname).read() > finally: > delete_if_exists(temp_fname) > > However there are things to watch out for in this lousy code: > > -race conditions, unique naming, and permissions on the temp file > > -proper & efficient detection of file-change, to know whether the > user actually did anything Just read the whole thing back into memory and compare the string to the original data. The file has to be quite long for the checksum calculation to be worthwhile. > -cross-platform determination of a sensible editor (that blocks > rather than spawns), using platform conventions like > os.environ['EDITOR'] > > -cleanup deletion of the temp-file > > I presume the code for spawning $PAGER on some content would look > pretty similar. > > Any good example code (or blog posts, or other links) that has > been battle-tested? You could look into mercurial to see what it does to let you edit its commit messages. A quick look into mercurial/ui.py (http://selenic.com/hg/file/9cf1620e1e75/mercurial/ui.py, start with the ui.edit() method) suggests that it uses the same approach as your pseudocode. From ethan at stoneleaf.us Fri Dec 23 03:38:04 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 23 Dec 2011 00:38:04 -0800 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: <4EF43DEC.6040100@stoneleaf.us> Ian Kelly wrote: > On Thu, Dec 22, 2011 at 7:10 PM, alex23 wrote: >> On Dec 22, 6:51 pm, Rolf Camps wrote: >>> I'm afraid it's dangerous to encourage the use of '[]' as assignment to >>> a parameter in a function definition. If you use the function several >>> times 'default' always points to the same list. >> >> I appreciate the concern, but adding a default argument guard would >> not only obscure the code. It's irrelevant, as you recognise, because >> no matter what, it's going to make copies of the default argument. > > It's only irrelevant in the immediate context of the code you posted. > But when Joe Novice sees your code and likes it and duplicates it a > million times without receiving any warning about it, he's eventually > going to write a function that modifies its default list argument, and > he'll be in for a nasty surprise when he does. And then he will learn about it and not make the mistake again (or if he does, it take much less time to figure it out). ~Ethan~ From rustompmody at gmail.com Fri Dec 23 03:38:07 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 00:38:07 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> Message-ID: <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> On Dec 23, 7:10?am, alex23 wrote: > On Dec 22, 6:51?pm, Rolf Camps wrote: > > > I'm afraid it's dangerous to encourage the use of '[]' as assignment to > > a parameter in a function definition. If you use the function several > > times 'default' always points to the same list. > > I appreciate the concern, but adding a default argument guard would > not only obscure the code. It's irrelevant, as you recognise, because > no matter what, it's going to make copies of the default argument. > > You know what the say about foolish consistencies :) Programming languages can have bugs as much as programs can. A classic example is the precedence table of C which Kernighan or Ritchie (dont remember which) admitted was wrong. Likewise function arguments that default to mutable entities is a known gotcha of python which is best treated as a bug in python. It should be avoided with the suitable additional circumspection that a language bug deserves over a program bug. [Just my rephrasing of what Ian is saying] From bahamutzero8825 at gmail.com Fri Dec 23 03:46:51 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 23 Dec 2011 02:46:51 -0600 Subject: IPC with multiprocessing.connection In-Reply-To: References: <4EF392B3.2050005@gmail.com> Message-ID: <4EF43FFB.8060609@gmail.com> On 12/23/2011 2:32 AM, Deepak Babu wrote: > See if http://www.rsdcbabu.com/2011/02/multiprocessing-with-python.htmlhelps > you. It doesn't. There is not even a mention of the connection submodule (plus it's Python 2, and I'm familiar enough with Python 2 to do a good job of porting to py3k). I don't even want to spawn new processes with the multiprocessing module; I want processes which may not even be written in Python to communicate over a named pipe or Unix domain socket. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From ethan at stoneleaf.us Fri Dec 23 03:49:45 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Fri, 23 Dec 2011 00:49:45 -0800 Subject: what does 'a=b=c=[]' do In-Reply-To: <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> Message-ID: <4EF440A9.7030101@stoneleaf.us> rusi wrote: > On Dec 23, 7:10 am, alex23 wrote: >> On Dec 22, 6:51 pm, Rolf Camps wrote: >> >>> I'm afraid it's dangerous to encourage the use of '[]' as assignment to >>> a parameter in a function definition. If you use the function several >>> times 'default' always points to the same list. >> >> I appreciate the concern, but adding a default argument guard would >> not only obscure the code. It's irrelevant, as you recognise, because >> no matter what, it's going to make copies of the default argument. >> >> You know what the say about foolish consistencies :) > > Programming languages can have bugs as much as programs can. > A classic example is the precedence table of C which Kernighan or > Ritchie (dont remember which) admitted was wrong. > > Likewise function arguments that default to mutable entities is a > known gotcha of python which is best treated as a bug in python. It > should be avoided with the suitable additional circumspection that a > language bug deserves over a program bug. That is the most ridiculous thing I have heard in a while. Mutable default arguments are *not* a bug in Python. Reminds me of a bug report a couple years back claiming multiple inheritence was a bug and asking it to be removed. ~Ethan~ From steve+comp.lang.python at pearwood.info Fri Dec 23 04:39:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 09:39:59 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> Message-ID: <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: > Likewise function arguments that default to mutable entities is a known > gotcha of python which is best treated as a bug in python. Nonsense. It is a feature, not a bug. Some people might argue that it is a mistake, a minor feature which allegedly causes more difficulties than benefits. I do not hold with that idea. But either way, it is not a bug to be fixed, but a deliberate consequence of intended semantics. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 23 04:44:31 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 09:44:31 GMT Subject: Idiom for shelling out to $EDITOR/$PAGER? References: <4EF4009E.8010109@tim.thechases.com> Message-ID: <4ef44d7f$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 09:28:52 +0100, Peter Otten wrote: >> -proper & efficient detection of file-change, to know whether the user >> actually did anything > > Just read the whole thing back into memory and compare the string to the > original data. The file has to be quite long for the checksum > calculation to be worthwhile. I don't think so. No matter how long the file is, the checksum is always going to do far more work than a simple string comparison. A string comparison can short-circuit on the first difference; the checksum must always process both files in their entirety. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 23 04:47:07 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 09:47:07 GMT Subject: Idiom for shelling out to $EDITOR/$PAGER? References: <4EF4009E.8010109@tim.thechases.com> <4ef44d7f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef44e1a$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 09:44:31 +0000, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 09:28:52 +0100, Peter Otten wrote: > >>> -proper & efficient detection of file-change, to know whether the user >>> actually did anything >> >> Just read the whole thing back into memory and compare the string to >> the original data. The file has to be quite long for the checksum >> calculation to be worthwhile. > > I don't think so. No matter how long the file is, the checksum is always > going to do far more work than a simple string comparison. A string > comparison can short-circuit on the first difference; the checksum must > always process both files in their entirety. Wait... the above only holds assuming you keep both the before-editing and after-editing versions. If you don't, then Peter is right, there comes a point where keeping a checksum saves enough memory for the additional effort to be worth while. Sorry for the noise. -- Steven From rosuav at gmail.com Fri Dec 23 04:59:16 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 23 Dec 2011 20:59:16 +1100 Subject: what does 'a=b=c=[]' do In-Reply-To: <4EF440A9.7030101@stoneleaf.us> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> Message-ID: On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman wrote: > That is the most ridiculous thing I have heard in a while. ?Mutable default > arguments are *not* a bug in Python. > > Reminds me of a bug report a couple years back claiming multiple inheritence > was a bug and asking it to be removed. Both of these could arguably be called misfeaures, but not bugs. ChrisA From steve+comp.lang.python at pearwood.info Fri Dec 23 04:59:51 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 09:59:51 GMT Subject: Idiom for shelling out to $EDITOR/$PAGER? References: Message-ID: <4ef45117$0$29973$c3e8da3$5496439d@news.astraweb.com> On Thu, 22 Dec 2011 22:16:30 -0600, Tim Chase wrote: > I presume the code for spawning $PAGER on some content would look pretty > similar. Have a look at pydoc in the standard library, which implements pager-like functionality. -- Steven From rustompmody at gmail.com Fri Dec 23 05:22:33 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 02:22:33 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 23, 2:39?pm, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: > > Likewise function arguments that default to mutable entities is a known > > gotcha of python which is best treated as a bug in python. > > Nonsense. It is a feature, not a bug. Tsk Tsk How can python have a bug? And that too on the python mailing list? Others however feel differently. See Chris Rebert's http://mail.python.org/pipermail/python-ideas/2007-January/000073.html and the links cited there. > > Some people might argue that it is a mistake, a minor feature which > allegedly causes more difficulties than benefits. I do not hold with that > idea. But either way, it is not a bug to be fixed, but a deliberate > consequence of intended semantics. I did not ask or imply that it should be 'fixed', just that language misfeatures should be treated with extra care. Windows uses CRLF where Unix uses LF. Nobody could argue that this discrepancy is of any use and nobody is about to fix it. Of course if "bug" means "must-fix-else-unusable" then sure you are right but then we would not be able to use most of the hardware or software that we do. To restate what (I think) Ian is saying: Exploit language misfeatures cleverly in your code if you want. But please red-flag them in mailing list posts. From rustompmody at gmail.com Fri Dec 23 05:31:46 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 02:31:46 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> Message-ID: <77861e74-c4fa-4f3f-8f28-853a1a319b1d@l16g2000prg.googlegroups.com> On Dec 23, 2:59?pm, Chris Angelico wrote: > On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman wrote: > > That is the most ridiculous thing I have heard in a while. ?Mutable default > > arguments are *not* a bug in Python. > > > Reminds me of a bug report a couple years back claiming multiple inheritence > > was a bug and asking it to be removed. > > Both of these could arguably be called misfeatures, but not bugs. > > ChrisA In Fortran, if the comma in the loop DO 10 I = 1,10 is misspelt as '.' it becomes the assignment DO10I = 1.0 Do you consider it a bug or a feature? Does Fortran consider it a bug or feature? From bex.lewis at gmail.com Fri Dec 23 05:41:48 2011 From: bex.lewis at gmail.com (becky_lewis) Date: Fri, 23 Dec 2011 02:41:48 -0800 (PST) Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error References: <920839e5-58fa-4fdf-9c88-7bed9a7448fd@d10g2000vbh.googlegroups.com> Message-ID: <1f45fa2a-0c4f-4362-bc75-e73ce16595ab@o9g2000vbc.googlegroups.com> Is there any possibility that you can tell us what the script actually is or provide a code listing (use pastebin if it's big)? From r32813 at freescale.com Fri Dec 23 06:20:36 2011 From: r32813 at freescale.com (Wong Wah Meng-R32813) Date: Fri, 23 Dec 2011 11:20:36 +0000 Subject: Timeout when calling COM objects on Windows In-Reply-To: <77861e74-c4fa-4f3f-8f28-853a1a319b1d@l16g2000prg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <77861e74-c4fa-4f3f-8f28-853a1a319b1d@l16g2000prg.googlegroups.com> Message-ID: <78E1273CA6E76A43BB8830A194FF709B01F48E@039-SN2MPN1-013.039d.mgd.msft.net> Hello there, I am converting my VB ActiveX application written sometime back in year 2000 with python 1.5.2 to run in python 2.7.1. My application is using RMI architecture and I am using a pythonOleRmi.py parser to exchange data/objects between the python and the VB exe. I have no issue when my VB EXE is not running as a COM server while making python API calls where my application server is written purely in python and runs on UNIX. However, when my VB EXE is running as a COM server, this code seems hangs when I make the python API same call to the application server. Is there something that I need to change or configure in order to run this successfully? Or to make some security setting on my Windows? (My VB EXE is run on Windows 7 now, will try out if the same issue is encountered on XP next week). Is the issue resided in the calling to the local daemon process (10.228.70.137:26000), or it is some security setting depicted in the policy.py file? Collecting Python Trace Output... Proxy called with: MatlMgr, getCompatibleFTCSVersions(), (), {} Dec 23 18:41:21 OleRmiClient Timeout of 300 seconds occurred on the invocati on of ('getAppAddress', (u'MatlMgr',), {}) to ('10.228.70.137', 26000) pythoncom error: Python error invoking COM method. Traceback (most recent call last): File "C:\genesis\Enablers\Python\lib\site-packages\win32com\server\policy.py", line 277, in _Invoke_ return self._invoke_(dispid, lcid, wFlags, args) File "C:\genesis\Enablers\Python\lib\site-packages\win32com\server\policy.py", line 282, in _invoke_ return S_OK, -1, self._invokeex_(dispid, lcid, wFlags, args, None, None) File "C:\genesis\Enablers\Python\lib\site-packages\win32com\server\policy.py", line 585, in _invokeex_ return func(*args) File "C:\genesis\Product\Lib\PythonOleRmi.py", line 240, in Proxy proxy = self._getProxyObj(pyserverString) File "C:\genesis\Product\Lib\PythonOleRmi.py", line 223, in _getProxyObj None, self._logWriter, rem_admin_addr = remAddr ) File "C:\genesis\Product\Lib\EComponent.py", line 710, in __init__ addr = self._getAppProxyAddress() File "C:\genesis\Product\Lib\EComponent.py", line 742, in _getAppProxyAddress addr = remAdmin.getAppAddress(self.server_name) File "C:\genesis\Product\Lib\EComponent.py", line 611, in __call__ self._method, args, kw ) File "C:\genesis\Product\Lib\RMI.py", line 1779, in _genericInvocation reply = self._requestReply( replyBit, (method_name, args, kw) ) File "C:\genesis\Product\Lib\RMI.py", line 1585, in _requestReply reply = self._receive() File "C:\genesis\Product\Lib\RMI.py", line 1677, in _receive raise ServerReplyTimeout( self.reply_timeout) ServerReplyTimeout: 300 Regards, Wah Meng From icanbob at gmail.com Fri Dec 23 06:50:19 2011 From: icanbob at gmail.com (bobicanprogram) Date: Fri, 23 Dec 2011 03:50:19 -0800 (PST) Subject: IPC with multiprocessing.connection References: Message-ID: On Dec 22, 3:27?pm, Andrew Berg wrote: > I'm trying to set up a system where my main program launches external > programs and then establishes connections to them via named pipes or > Unix domain sockets (depending on platform) with > multiprocessing.connection.Listener. The issue I'm having is with the > accept() method. If there is nothing on the other side of the > pipe/socket, it just hangs. Connection objects have a poll() method that > can timeout, but I see no way to continue the program if there's no > initial connection. What I'd like to do is let it time out if there's > nothing on the other side. > I'm pretty new to the multiprocessing module (and IPC in general), so I > could've easily missed something. > > -- > CPython 3.2.2 | Windows NT 6.1.7601.17640 You might want to check out the code samples at: http://www.icanprogram.com/06py/lesson1/lesson1.html bob From phil at riverbankcomputing.com Fri Dec 23 06:56:48 2011 From: phil at riverbankcomputing.com (Phil Thompson) Date: Fri, 23 Dec 2011 11:56:48 +0000 Subject: ANN: PyQt v4.9 Released (Python bindings for Qt) Message-ID: PyQt v4.9 has been released and is available from http://www.riverbankcomputing.com/software/pyqt/. PyQt is a comprehensive set of bindings for the Qt application and UI framework from Nokia. It supports the same platforms as Qt (Windows, Linux and MacOS/X). PyQt supports Python v3 and Python v2.3 and later. The highlight of this release is full support for Qt v4.8.0 and the inclusion of the QtDBus module (so providing support for DBus with Python v3). Windows installers are provided for the GPL version of PyQt which contains everything needed for PyQt development (including Qt, Qt Designer, QScintilla, and MySQL, PostgreSQL, SQLite and ODBC drivers) except Python itself. Installers are provided for the 32 and 64 bit versions of Python v2.5, v2.6, v2.7, v3.1 and v3.2. PyQt v4 is implemented as a set of 19 extension modules containing over 400 classes and over 6,000 functions and methods. QtCore The non-GUI infrastructure including event loops, threads, i18n, Unicode, signals and slots, user and application settings, mapped files and shared memory. QtDeclarative A set of classes that implement a framework for building highly dynamic user interfaces using the QML declarative language that can be integrated with Python. QtDBus A set of classes for accessing remote applications using the DBus protocol. QtDesigner A set of classes that allow the Qt Designer GUI design tool to be extended with PyQt. QtGui A rich collection of GUI widgets. QtHelp A set of classes for creating and viewing searchable documentation and being able to integrate online help with PyQt applications. It includes the C++ port of the Lucene text search engine. QtNetwork A set of classes to support TCP and UDP socket programming and higher level protocols (eg. HTTP, SSL). QtOpenGL A set of classes that allows PyOpenGL to render onto Qt widgets. QtScript A set of classes that implements a JavaScript interpreter. Python objects may be exposed in the interpreter as JavaScript objects. QtScriptTools A debugger for the JavaScript interpreter. QtSql A set of classes that implement SQL data models and interfaces to industry standard databases. The Windows installers include support for SQLite, MySQL, PostgreSQL and ODBC. QtSvg A set of classes to render SVG files onto Qt widgets. QtTest A set of classes to automate unit testing of PyQt applications and GUIs. QtWebKit This implements a web browser engine based on the WebKit engine used by Apple's Safari browser. It allows the methods and properties of Python objects to be published and appear as JavaScript objects to scripts embedded in HTML pages. QtXML A set of classes that implement DOM and SAX parsers. QtXMLPatterns A set of classes that implement XQuery and XPath support for XML and custom data models. QAxContainer A set of classes for Windows that allows the integration of ActiveX controls and COM objects. phonon A cross-platform multimedia framework that enables the use of audio and video content in PyQt applications. DirectX is used as the Windows backend, QuickTime as the MacOS/X backend, and GStreamer as the Linux backend. QtMultimedia A set of classes that provide low-level multimedia functions. Application developers would normally use the phonon module. PyQt includes the pyuic4 utility which generates Python code to implement user interfaces created with Qt Designer in the same way that the uic utility generates C++ code. It is also able to load Designer XML files dynamically. PyQt is available under the GPL and a commercial license. Unlike Qt, PyQt is not available under the LGPL. The commercial PyQt license allows GPL applications to be relicensed at any time. From ben+python at benfinney.id.au Fri Dec 23 07:06:03 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 23 Dec 2011 23:06:03 +1100 Subject: Idiom for shelling out to $EDITOR/$PAGER? References: <877h1nn7ej.fsf@benfinney.id.au> Message-ID: <87wr9nlcgk.fsf@benfinney.id.au> Cameron Simpson writes: > On 23Dec2011 17:12, Ben Finney wrote: > | That doesn't address the concern Tim raised: did the user actually do > | anything, did the file change? > > I'm not sure it matters. I know of numerous applications where it matters, spcifically ones which decide what to do with the edited text. Including ?submit a bug report? and ?submit a wiki page edit? and ?send an email message?. > Sure. But still, if you're able to act on a changed file, why not also > act on an unchanged file? The user asked to change things; take what > you got back and proceed! If the buffer was not edited, the user has most likely changed their mind about editing at all, and there are many applications where the correct default action differes depending on whether the user actually changed anything in the text. -- \ ?By instructing students how to learn, unlearn, and relearn, a | `\ powerful new dimension can be added to education.? ?Alvin | _o__) Toffler, _Future Shock_, 1970 | Ben Finney From bahamutzero8825 at gmail.com Fri Dec 23 07:27:12 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Fri, 23 Dec 2011 06:27:12 -0600 Subject: IPC with multiprocessing.connection In-Reply-To: References: Message-ID: <4EF473A0.9090209@gmail.com> On 12/23/2011 5:50 AM, bobicanprogram wrote: > You might want to check out the code samples at: > http://www.icanprogram.com/06py/lesson1/lesson1.html Let's see, I asked about a method in the multiprocessing module, and you recommend code snippets that run on Python versions that don't even have the module and that assume Linux when I state in my email signature the version of /Windows/ that I run. On top of all that, it's not even designed to teach Python; it's designed to explain how to use that library! I honestly don't mind someone pushing their website if it actually might help, but you clearly didn't read my post. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From python.list at tim.thechases.com Fri Dec 23 07:28:26 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 23 Dec 2011 06:28:26 -0600 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: <87wr9nlcgk.fsf@benfinney.id.au> References: <877h1nn7ej.fsf@benfinney.id.au> <87wr9nlcgk.fsf@benfinney.id.au> Message-ID: <4EF473EA.2040205@tim.thechases.com> On 12/23/11 06:06, Ben Finney wrote: > Cameron Simpson writes: >> On 23Dec2011 17:12, Ben Finney >> wrote: | That doesn't address the concern Tim raised: did >> the user actually do | anything, did the file change? >> >> I'm not sure it matters. > > I know of numerous applications where it matters, spcifically > ones which decide what to do with the edited text. Including > ?submit a bug report? and ?submit a wiki page edit? and ?send > an email message?. > > If the buffer was not edited, the user has most likely changed > their mind about editing at all, and there are many > applications where the correct default action differes > depending on whether the user actually changed anything in the > text. Yes, Ben's understanding is correct here, that for some of my use-cases, it's helpful to know if the user did anything. The application can then decide whether it should use the original data it passed to the function, or whether it should go some "abort what you're doing" route. -tkc From neilc at norwich.edu Fri Dec 23 08:10:53 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 23 Dec 2011 13:10:53 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> Message-ID: <9ljcutFemiU5@mid.individual.net> On 2011-12-23, Chris Angelico wrote: > On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman wrote: >> That is the most ridiculous thing I have heard in a while. >> ?Mutable default arguments are *not* a bug in Python. >> >> Reminds me of a bug report a couple years back claiming >> multiple inheritence was a bug and asking it to be removed. > > Both of these could arguably be called misfeaures, but not > bugs. Is the misfeature that Python doesn't evaluate the default argument expression every time you call the function? What would be the harm if it did? -- Neil Cerutti From robert.kern at gmail.com Fri Dec 23 08:10:54 2011 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 23 Dec 2011 13:10:54 +0000 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/23/11 10:22 AM, rusi wrote: > On Dec 23, 2:39 pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: >>> Likewise function arguments that default to mutable entities is a known >>> gotcha of python which is best treated as a bug in python. >> >> Nonsense. It is a feature, not a bug. > > Tsk Tsk How can python have a bug? And that too on the python mailing > list? > > Others however feel differently. See Chris Rebert's > http://mail.python.org/pipermail/python-ideas/2007-January/000073.html > and the links cited there. > >> >> Some people might argue that it is a mistake, a minor feature which >> allegedly causes more difficulties than benefits. I do not hold with that >> idea. But either way, it is not a bug to be fixed, but a deliberate >> consequence of intended semantics. > > I did not ask or imply that it should be 'fixed', just that language > misfeatures should be treated with extra care. "Bug" means, roughly, "something that should be fixed" not just any "thing that has some unwanted consequences". So yes, by calling it a bug you are asking and implying just that. If you don't mean that, don't use the word "bug". -- 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 From neilc at norwich.edu Fri Dec 23 08:13:38 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 23 Dec 2011 13:13:38 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> Message-ID: <9ljd41Fp3bU1@mid.individual.net> On 2011-12-23, Neil Cerutti wrote: > Is the misfeature that Python doesn't evaluate the default > argument expression every time you call the function? What > would be the harm if it did? ...you know, assuming it wouldn't break existing code. ;) -- Neil Cerutti From rustompmody at gmail.com Fri Dec 23 08:23:59 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 05:23:59 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> On Dec 23, 6:10?pm, Robert Kern wrote: > On 12/23/11 10:22 AM, rusi wrote: > > > > > > > > > > > On Dec 23, 2:39 pm, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> ?wrote: > >> On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: > >>> Likewise function arguments that default to mutable entities is a known > >>> gotcha of python which is best treated as a bug in python. > > >> Nonsense. It is a feature, not a bug. > > > Tsk Tsk How can python have a bug? And that too on the python mailing > > list? > > > Others however feel differently. See Chris Rebert's > >http://mail.python.org/pipermail/python-ideas/2007-January/000073.html > > and the links cited there. > > >> Some people might argue that it is a mistake, a minor feature which > >> allegedly causes more difficulties than benefits. I do not hold with that > >> idea. But either way, it is not a bug to be fixed, but a deliberate > >> consequence of intended semantics. > > > I did not ask or imply that it should be 'fixed', just that language > > misfeatures should be treated with extra care. > > "Bug" means, roughly, "something that should be fixed" not just any "thing that > has some unwanted consequences". So yes, by calling it a bug you are asking and > implying just that. If you don't mean that, don't use the word "bug". Of course it should be fixed. The repeated recurrence of it as a standard gotcha as well as the python ideas list testifies to that. Its not going to be fixed -- does not mean thats ideal, just that the best proposed solutions cost/benefit equations are not good enough at this point. Case in point: I think it was around python 2.2 that there were discussions for having a conditional operator. GvR did not put it in for a couple of releases not because it was a bad idea but because no syntax was good enough. When he finally did it, he chose a syntax which is arguably not ideal but is the best all-things-considered (eg no unnecessary new keywords, compatibility with existing code etc) From robert.kern at gmail.com Fri Dec 23 08:53:54 2011 From: robert.kern at gmail.com (Robert Kern) Date: Fri, 23 Dec 2011 13:53:54 +0000 Subject: what does 'a=b=c=[]' do In-Reply-To: <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> Message-ID: On 12/23/11 1:23 PM, rusi wrote: > On Dec 23, 6:10 pm, Robert Kern wrote: >> On 12/23/11 10:22 AM, rusi wrote: >>> On Dec 23, 2:39 pm, Steven D'Aprano>> +comp.lang.pyt... at pearwood.info> wrote: >>>> Some people might argue that it is a mistake, a minor feature which >>>> allegedly causes more difficulties than benefits. I do not hold with that >>>> idea. But either way, it is not a bug to be fixed, but a deliberate >>>> consequence of intended semantics. >> >>> I did not ask or imply that it should be 'fixed', just that language >>> misfeatures should be treated with extra care. >> >> "Bug" means, roughly, "something that should be fixed" not just any "thing that >> has some unwanted consequences". So yes, by calling it a bug you are asking and >> implying just that. If you don't mean that, don't use the word "bug". > > Of course it should be fixed. The repeated recurrence of it as a > standard gotcha as well as the python ideas list testifies to that. So you were lying when you said that you did not ask or imply that it should be 'fixed'? Please make up your mind. It's quite rude to "defend" your position by constantly shifting it whenever you get challenged on it. -- 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 From rustompmody at gmail.com Fri Dec 23 09:57:02 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 06:57:02 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> Message-ID: <0d2fa782-21d1-416a-9756-bd833163c36a@t36g2000prj.googlegroups.com> On Dec 23, 6:53?pm, Robert Kern wrote: > On 12/23/11 1:23 PM, rusi wrote: > > > > > > > > > > > On Dec 23, 6:10 pm, Robert Kern ?wrote: > >> On 12/23/11 10:22 AM, rusi wrote: > >>> On Dec 23, 2:39 pm, Steven D'Aprano >>> +comp.lang.pyt... at pearwood.info> ? ?wrote: > >>>> Some people might argue that it is a mistake, a minor feature which > >>>> allegedly causes more difficulties than benefits. I do not hold with that > >>>> idea. But either way, it is not a bug to be fixed, but a deliberate > >>>> consequence of intended semantics. > > >>> I did not ask or imply that it should be 'fixed', just that language > >>> misfeatures should be treated with extra care. > > >> "Bug" means, roughly, "something that should be fixed" not just any "thing that > >> has some unwanted consequences". So yes, by calling it a bug you are asking and > >> implying just that. If you don't mean that, don't use the word "bug". > > > Of course it should be fixed. ?The repeated recurrence of it as a > > standard gotcha as well as the python ideas list testifies to that. > > So you were lying when you said that you did not ask or imply that it should be > 'fixed'? Please make up your mind. It's quite rude to "defend" your position by > constantly shifting it whenever you get challenged on it. Meanings of "should" http://www.englishpage.com/modals/should.html My first should was the 3rd one My second was the 1st one. [And we really should stop this argument (2nd one)] From steve+comp.lang.python at pearwood.info Fri Dec 23 10:33:09 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 15:33:09 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> <0d2fa782-21d1-416a-9756-bd833163c36a@t36g2000prj.googlegroups.com> Message-ID: <4ef49f34$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 06:57:02 -0800, rusi wrote: > On Dec 23, 6:53?pm, Robert Kern wrote: >> On 12/23/11 1:23 PM, rusi wrote: [...] >> > Of course it should be fixed. ?The repeated recurrence of it as a >> > standard gotcha as well as the python ideas list testifies to that. >> >> So you were lying when you said that you did not ask or imply that it >> should be 'fixed'? Please make up your mind. It's quite rude to >> "defend" your position by constantly shifting it whenever you get >> challenged on it. > > Meanings of "should" http://www.englishpage.com/modals/should.html My > first should was the 3rd one > My second was the 1st one. Rusi, are you a native English speaker? Because that excuse/defense does not excuse your comments at all, multiple meanings of "should" or not. Why not just admit to a mistake? After denying you were asking for Python to fix a so-called bug, you then called for Python to fix it. If this were a real argument rather than a friendly debate, people would be crying "Gotcha!" for sure. > [And we really should stop this argument (2nd one)] When the facts are against you, argue semantics; when semantics are against you, argue the facts; when both are against you, claim to be above arguing. -- Steven From joshua.landau.ws at gmail.com Fri Dec 23 10:33:15 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Fri, 23 Dec 2011 15:33:15 +0000 Subject: Grammar for classes In-Reply-To: References: Message-ID: On 22 December 2011 23:39, Terry Reedy wrote: > On 12/20/2011 12:05 PM, Joshua Landau wrote: > >> Should I file a documentation bug report? >> > > Please do. It the addition means something, it needs to be explained in > the text. If it is wrong, it should go. I have filed one at issue13658 . -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Fri Dec 23 10:49:34 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 15:49:34 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> Message-ID: <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 13:13:38 +0000, Neil Cerutti wrote: > On 2011-12-23, Neil Cerutti wrote: >> Is the misfeature that Python doesn't evaluate the default argument >> expression every time you call the function? What would be the harm if >> it did? > > ...you know, assuming it wouldn't break existing code. ;) It will. Python's default argument strategy has been in use for 20 years. Some code will rely on it. I know mine does. There are two strategies for dealing with default arguments that I know of: early binding and late binding. Python has early binding: the default argument is evaluated once, when the function is created. Late binding means the default argument is always re-evaluated each time it is needed. Both strategies are reasonable choices. Both have advantages and disadvantages. Both have use-cases, and both lead to confusion when the user expects one but gets the other. If you think changing from early to late binding will completely eliminate the default argument "gotcha", you haven't thought things through -- at best you might reduce the number of complaints, but only at the cost of shifting them from one set of use- cases to another. Early binding is simple to implement and simple to explain: when you define a function, the default value is evaluated once, and the result stored to be used whenever it is needed. The disadvantage is that it can lead to unexpected results for mutable arguments. Late binding is also simple to explain, but a little harder to implement. The function needs to store the default value as a piece of code (an expression) which can be re-evaluated as often as needed, not an object. The disadvantage of late binding is that since the expression is live, it needs to be calculated each time, even if it turns out to be the same result. But there's no guarantee that it will return the same result each time: consider a default value like x=time.time(), which will return a different value each time it is called; or one like x=a+b, which will vary if either a or b are changed. Or will fail altogether if either a or b are deleted. This will surprise some people some of the time and lead to demands that Python "fix" the "obviously buggy" default argument gotcha. If a language only offers one, I maintain it should offer early binding (the status quo). Why? Because it is more elegant to fake late binding in an early binding language than vice versa. To fake late binding in a language with early binding, use a sentinel value and put the default value inside the body of the function: def func(x, y=None): if y is None: y = [] ... All the important parts of the function are in one place, namely inside the function. To fake early binding when the language provides late binding, you still use a sentinel value, but the initialization code creating the default value is outside the body of the function, usually in a global variable: _DEFAULT_Y = [] # Private constant, don't touch. def func(x, y=None): if y is None: y = _DEFAULT_Y ... This separates parts of the code that should be together, and relies on a global, with all the disadvantages that implies. -- Steven From rosuav at gmail.com Fri Dec 23 10:55:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Dec 2011 02:55:41 +1100 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Dec 24, 2011 at 2:49 AM, Steven D'Aprano wrote: > To fake early binding when the language provides late binding, you still > use a sentinel value, but the initialization code creating the default > value is outside the body of the function, usually in a global variable: > > ? ?_DEFAULT_Y = [] ?# Private constant, don't touch. > > ? ?def func(x, y=None): > ? ? ? ?if y is None: > ? ? ? ? ? ?y = _DEFAULT_Y > ? ? ? ?... > > This separates parts of the code that should be together, and relies on a > global, with all the disadvantages that implies. A static variable (in the C sense) would make this just as clean as the alternative. In Python, that could be implemented as an attribute of the function object. Oh looky here... that's how default arguments are implemented. :) Tim Toady. ChrisA From hoogendoorn.eelco at gmail.com Fri Dec 23 10:56:06 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Fri, 23 Dec 2011 07:56:06 -0800 (PST) Subject: Need advice on the design of my application References: Message-ID: My first suggestion would be to keep the rendering in Python, if at all feasible, and do only the actual simulation/computation in C. Rasterizing a heightfield and rigid body plus some plash effects is nothing that couldnt be done in PyOpenGL, or even something higher- level like visvis or mayavi. (visvis would be my first suggestion) I would run the simulation in a python subprocess that calls into the C dll; that should give you at least one core fully focussed on your computations, without competition/blocking from any other modules. Marshalling the simulation data between subprocesses should be a small performance hurdle relative to getting it on your GPU; either way, the python subprocessing module makes it entirely painless. And I would start with an implementation in numpy; probably, you will be doing some fft's and/or boundary element stuff. This can all be done fairly efficiently in numpy; at minimum it will give you a reference implementation, and its not unlikely it will work well enough that you dont even want to bother with the C implementation anymore. From rustompmody at gmail.com Fri Dec 23 10:59:27 2011 From: rustompmody at gmail.com (rusi) Date: Fri, 23 Dec 2011 07:59:27 -0800 (PST) Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4ef44c6f$0$29973$c3e8da3$5496439d@news.astraweb.com> <91c2884c-8f02-4f57-a240-a5dc50241369@a31g2000pre.googlegroups.com> <0d2fa782-21d1-416a-9756-bd833163c36a@t36g2000prj.googlegroups.com> <4ef49f34$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <17da5f49-5e48-48d5-aa67-026bffc7b805@a31g2000pre.googlegroups.com> On Dec 23, 8:33?pm, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 06:57:02 -0800, rusi wrote: > > On Dec 23, 6:53?pm, Robert Kern wrote: > >> On 12/23/11 1:23 PM, rusi wrote: > [...] > >> > Of course it should be fixed. ?The repeated recurrence of it as a > >> > standard gotcha as well as the python ideas list testifies to that. > > >> So you were lying when you said that you did not ask or imply that it > >> should be 'fixed'? Please make up your mind. It's quite rude to > >> "defend" your position by constantly shifting it whenever you get > >> challenged on it. > > > Meanings of "should"http://www.englishpage.com/modals/should.htmlMy > > first should was the 3rd one > > My second was the 1st one. > > Rusi, are you a native English speaker? Because that excuse/defense does > not excuse your comments at all, multiple meanings of "should" or not. > Why not just admit to a mistake? After denying you were asking for Python > to fix a so-called bug, you then called for Python to fix it. If this > were a real argument rather than a friendly debate, people would be > crying "Gotcha!" for sure. Ok You got me! Does that help the OP's? > It's a slick language but I still have trouble wrapping my brain around some of the concepts. From gnarlodious at gmail.com Fri Dec 23 11:01:54 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 23 Dec 2011 08:01:54 -0800 (PST) Subject: Get named module's file location Message-ID: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> Given a module's name, how do I get the file path without importing it? Searched all over, can't find any such info. Is it possible to ask if a named module exists before attempting an import? Or are we forced to import first and catch any failure? -- Gnarlie From roy at panix.com Fri Dec 23 11:15:33 2011 From: roy at panix.com (Roy Smith) Date: Fri, 23 Dec 2011 11:15:33 -0500 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4ef4a30d$0$29973$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > The disadvantage of late binding is that since the expression is live, it > needs to be calculated each time, even if it turns out to be the same > result. But there's no guarantee that it will return the same result each > time: consider a default value like x=time.time(), which will return a > different value each time it is called I know this is not quite the same thing, but it's interesting to look at what django (and mongoengine) do in their model definitions, prompted by your time.time() example. You can do declare a model field something like: class Foo(models.Model): timestamp = DateTimeField(default=datetime.utcnow) Now, when you create a Foo, if you don't supply a timestamp, it generates one by calling utcnow() for you. Very handy. I'm not arguing that Python should do that, just pointing out an example of where late binding is nice. Technically, that's probably not really late binding. You always get the same function bound, it's just called each time it's used because django notices that you passed a callable. Maybe sort of "early binding, late calling" would be a more apt description, but given that python has descriptors, the line between data and function sometimes gets a little blurry anyway. From yasar11732 at gmail.com Fri Dec 23 11:16:37 2011 From: yasar11732 at gmail.com (=?ISO-8859-9?Q?Ya=FEar_Arabac=FD?=) Date: Fri, 23 Dec 2011 18:16:37 +0200 Subject: Get named module's file location In-Reply-To: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> Message-ID: >>> import imp >>> imp.find_module("os") (, '/usr/lib/python2.7/os.py', ('.py', 'U', 1)) >>> 2011/12/23 Gnarlodious > Given a module's name, how do I get the file path without importing it? > Searched all over, can't find any such info. > > Is it possible to ask if a named module exists before attempting an import? > > Or are we forced to import first and catch any failure? > > -- Gnarlie > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://yasar.serveblog.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Fri Dec 23 11:18:12 2011 From: roy at panix.com (Roy Smith) Date: Fri, 23 Dec 2011 11:18:12 -0500 Subject: Get named module's file location References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> Message-ID: In article <32472953.855.1324656114851.JavaMail.geo-discussion-forums at prix23>, Gnarlodious wrote: > Given a module's name, how do I get the file path without importing it? > Searched all over, can't find any such info. > > Is it possible to ask if a named module exists before attempting an import? > > Or are we forced to import first and catch any failure? > > -- Gnarlie import imp imp.find_module() Why do you want to do this? From mwilson at the-wire.com Fri Dec 23 11:27:01 2011 From: mwilson at the-wire.com (Mel Wilson) Date: Fri, 23 Dec 2011 11:27:01 -0500 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 23 Dec 2011 13:13:38 +0000, Neil Cerutti wrote: >> On 2011-12-23, Neil Cerutti wrote: >> ...you know, assuming it wouldn't break existing code. ;) > > It will. Python's default argument strategy has been in use for 20 years. > Some code will rely on it. I know mine does. In a tool that's meant for other people to use to accomplish work of their own, breaking workflow is a cardinal sin. In a research language that's meant always to be up-to-date with the concept of the week, not so much. Mel. From gnarlodious at gmail.com Fri Dec 23 11:51:13 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 23 Dec 2011 08:51:13 -0800 (PST) Subject: Get named module's file location In-Reply-To: References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> Message-ID: <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> Roy Smith wrote: > import imp > imp.find_module() Oh yeah that works. I am getting a list of modtimes using List Comprehension, from a list of modules, which will be compared to an older list to see if mod_wsgi needs to be restarted. Maybe thee is an easy way to get the modtimes, I'd be grateful. -- Gnarlie From roy at panix.com Fri Dec 23 11:56:47 2011 From: roy at panix.com (Roy Smith) Date: Fri, 23 Dec 2011 11:56:47 -0500 Subject: Get named module's file location References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> Message-ID: In article <4946660.379.1324659073535.JavaMail.geo-discussion-forums at prez5>, Gnarlodious wrote: > Roy Smith wrote: > > > import imp > > imp.find_module() > > Oh yeah that works. I am getting a list of modtimes using List Comprehension, > from a list of modules, which will be compared to an older list to see if > mod_wsgi needs to be restarted. Ah, I see. Django's runserver does this. You might want to look to see how they implement it (https://www.djangoproject.com/download/). From neilc at norwich.edu Fri Dec 23 12:03:11 2011 From: neilc at norwich.edu (Neil Cerutti) Date: 23 Dec 2011 17:03:11 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9ljqifFfj9U3@mid.individual.net> On 2011-12-23, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 13:13:38 +0000, Neil Cerutti wrote: >> On 2011-12-23, Neil Cerutti wrote: >>> Is the misfeature that Python doesn't evaluate the default >>> argument expression every time you call the function? What >>> would be the harm if it did? >> >> ...you know, assuming it wouldn't break existing code. ;) > > It will. Python's default argument strategy has been in use for > 20 years. Some code will rely on it. I know mine does. I'm aware of that. I should have put the question differently, but you did guess what I meant to ask. Thanks for the dicussion. > Early binding is simple to implement and simple to explain: > when you define a function, the default value is evaluated > once, and the result stored to be used whenever it is needed. > The disadvantage is that it can lead to unexpected results for > mutable arguments. > > Late binding is also simple to explain, but a little harder to > implement. The function needs to store the default value as a > piece of code (an expression) which can be re-evaluated as > often as needed, not an object. > > The disadvantage of late binding is that since the expression > is live, it needs to be calculated each time, even if it turns > out to be the same result. But there's no guarantee that it > will return the same result each time: That's its main *advantage*. > consider a default value like x=time.time(), which will return > a different value each time it is called; or one like x=a+b, > which will vary if either a or b are changed. Or will fail > altogether if either a or b are deleted. This will surprise > some people some of the time and lead to demands that Python > "fix" the "obviously buggy" default argument gotcha. It's hard to see anyone being confused by the resultant exception. It's much harder to figure out what's going wrong with an early-bound mutable. > If a language only offers one, I maintain it should offer early > binding (the status quo). Why? Because it is more elegant to > fake late binding in an early binding language than vice versa. > > To fake late binding in a language with early binding, use a > sentinel value and put the default value inside the body of the > function: > > def func(x, y=None): > if y is None: > y = [] > ... > > All the important parts of the function are in one place, > namely inside the function. > > To fake early binding when the language provides late binding, > you still use a sentinel value, but the initialization code > creating the default value is outside the body of the function, > usually in a global variable: > > _DEFAULT_Y = [] # Private constant, don't touch. > > def func(x, y=None): > if y is None: > y = _DEFAULT_Y > ... > > This separates parts of the code that should be together, and > relies on a global, with all the disadvantages that implies. I'd use a function attribute. def func(x, y=None): if y is None: y = func.default_y ... func.default_y = [] That's awkward only if you believe function attributes are awkward. Even if common practice were instead to use a global variable, as you did, it wouldn't be considered bad if it were a common idiom. In case, a global that's not meant to be rebound or mutated is the best possible variety. However, I also wouldn't base the decision of late versus early binding on how confusing it would be if you assume wrongly which one you are getting. Most default function arguments are literals, so in Python the question just doesn't come up until you get mutabled. The greater efficiency was probably what decided this question for Python, right? Since late-binding is so easy to fake, is hardly ever what you want, and would make all code slower, why do it? -- Neil Cerutti From nikos.kouras at gmail.com Fri Dec 23 12:14:14 2011 From: nikos.kouras at gmail.com (=?ISO-8859-7?B?zenq/Ovh7/Igyu/98eHy?=) Date: Fri, 23 Dec 2011 09:14:14 -0800 (PST) Subject: socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error References: <920839e5-58fa-4fdf-9c88-7bed9a7448fd@d10g2000vbh.googlegroups.com> <1f45fa2a-0c4f-4362-bc75-e73ce16595ab@o9g2000vbc.googlegroups.com> Message-ID: <144a4387-e80e-4782-a6eb-aecc1e742dc6@v14g2000yqh.googlegroups.com> On 23 ???, 12:41, becky_lewis wrote: > Is there any possibility that you can tell us what the script actually > is or provide a code listing (use pastebin if it's big)? The script is about retrieving and storing the visitros hostnames to mysql database creating a log file. I dont know why this line host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] fails sometimes and some other times works ok retrieving the hostnames correctly. What do you understand from the traceback?! From torriem at gmail.com Fri Dec 23 12:23:30 2011 From: torriem at gmail.com (Michael Torrie) Date: Fri, 23 Dec 2011 10:23:30 -0700 Subject: what does 'a=b=c=[]' do In-Reply-To: <77861e74-c4fa-4f3f-8f28-853a1a319b1d@l16g2000prg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <77861e74-c4fa-4f3f-8f28-853a1a319b1d@l16g2000prg.googlegroups.com> Message-ID: <4EF4B912.4090702@gmail.com> On 12/23/2011 03:31 AM, rusi wrote: > In Fortran, if the comma in the loop > DO 10 I = 1,10 > is misspelt as '.' it becomes the assignment > DO10I = 1.0 > > Do you consider it a bug or a feature? > Does Fortran consider it a bug or feature? Non sequitor. Nothing at all to do with the issue at hand. Furthermore, you are talking about a parser "feature" not a runtime behavior. From gnarlodious at gmail.com Fri Dec 23 14:40:48 2011 From: gnarlodious at gmail.com (Gnarlodious) Date: Fri, 23 Dec 2011 11:40:48 -0800 (PST) Subject: Get named module's file location In-Reply-To: References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> Message-ID: <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> I am rolling my own, and learning Python at the same time. One more question. Say I want to assemble a list of tuples like this: modules = ['wsgiref', 'http'] import imp [(imp.find_module(module)[1], os.path.getmtime(imp.find_module(module)[1])) for module in modules] Can I in some way assign imp.find_module(module)[1] to a variable and reuse it? Is this a job for lambda? Thanks anyone. -- Gnarlie From cs at zip.com.au Fri Dec 23 14:47:53 2011 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 24 Dec 2011 06:47:53 +1100 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: References: Message-ID: <20111223194753.GA23781@cskk.homeip.net> On 23Dec2011 02:21, Devin Jeanpierre wrote: | > Anyway, look it up; it has an autodelete mode etc. | | The autodelete isn't useful in this context. The file needs to be | closed and then reopened again; with the autodelete option, the | closing would delete the file, preventing it from being opened by the | text editor or reopened to check contents. | | (As far as I know, the race condition inherent in this is unavoidable. | At least on windows, but I couldn't figure out any other way on Linux | either.) You can fire off the editor from inside the with statement. Write contents, flush, edit, open T.name for read, read, close, exit with statement. Windows may hate you for this with its locking behaviour for open files, but it will work for UNIX. Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ The elliptical cam has gradually slid up the beam shaft and caught on the flange rebate dislodging the gripley, with disastrous results. - Death, 'Reaper Man' (Pratchett) From wolftracks at invalid.com Fri Dec 23 14:51:29 2011 From: wolftracks at invalid.com (W. eWatson) Date: Fri, 23 Dec 2011 11:51:29 -0800 Subject: Cannot Remove Python Libs from XP In-Reply-To: References: Message-ID: On 12/22/2011 1:21 PM, Dennis Lee Bieber wrote: > On Thu, 22 Dec 2011 09:53:04 -0800, "W. eWatson" > wrote: > >> I have three py libs and the python program itself, 2.52, installed on >> an 6 year old HP Laptop. I decided to remove them, and took removed >> Python with the Control Panel ?Add/Remove icon. Worked fine. When I try >> to remove any of the remaining libs, press the add/remove button does >> nothing but blink. How do I get around this problem? > > It would probably have been better to remove the libraries first > (especially if their installers /used/ Python to do the install; the > uninstaller would then likely also have used that version of Python). In > short, uninstalling a set of related packages should be done in the > reverse order of how they were installed. > > If you don't have anything private stored in the remains of the > Python2.5.2 install directory... Just delete the entire directory tree > (which should get rid of any left-over files from the other libraries). > > Removing them from the Add/Remove control might be achieved by using > a registry cleaner; if not, manually scanning the registry for those > package names and deleting the registry keys may suffice (back up the > registry first -- export everything from the top down). Then run a > registry cleaner, as there may be stray GUID keys that eventually mapped > to the deleted keys. > > Lets see.... {booting even older WinXP laptop -- I'm using a Win7 > laptop with data-card for current online activity} > > HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\... > > Look for the particular packages (and anything else you may also > have removed just in case something got left behind) and delete the > keys. (Interesting -- I have an entry for wxPython, but no entry for > Python or ActiveState... They may be stuffed elsewhere -- I also seem to > have a corrupted search path; maybe happened last time I tried using the > laptop to debug one of your queries; Pythonwin isn't finding win32ui) > > I think this turns out to be something of a laugher. I did re-install Python 2.5.2; however, the action was similar when I tried to remove the libs. I happened to move the screen slightly, and noticed a small hidden dialog behind the Add/Remove screen with a message in it. "Are you sure you want to remove this program?". Argh. How foolish it was to hide that simple dialog. So hitting the add/remove button repeatedly really was useless. From rosuav at gmail.com Fri Dec 23 14:52:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Dec 2011 06:52:22 +1100 Subject: Get named module's file location In-Reply-To: <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> Message-ID: On Sat, Dec 24, 2011 at 6:40 AM, Gnarlodious wrote: > [(imp.find_module(module)[1], os.path.getmtime(imp.find_module(module)[1])) for module in modules] > > Can I in some way assign imp.find_module(module)[1] to a variable and reuse it? Is this a job for lambda? Well, you can use an additional comprehension to provide a temporary variable, if you really want to do it all as a single expression. [(m, os.path.getmtime(m)) for m in (imp.find_module(module)[1] for module in modules)] ChrisA From roy at panix.com Fri Dec 23 15:00:17 2011 From: roy at panix.com (Roy Smith) Date: Fri, 23 Dec 2011 15:00:17 -0500 Subject: Get named module's file location References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> Message-ID: In article <4652751.858.1324669248908.JavaMail.geo-discussion-forums at prj1>, Gnarlodious wrote: > I am rolling my own, and learning Python at the same time. Hmmm. The imp module is kind of deep magic for a first introduction to the language. But, whatever. > One more question. Say I want to assemble a list of tuples like this: > > modules = ['wsgiref', 'http'] > import imp > [(imp.find_module(module)[1], os.path.getmtime(imp.find_module(module)[1])) > for module in modules] > > Can I in some way assign imp.find_module(module)[1] to a variable and reuse > it? Is this a job for lambda? I think what you want to do is rewrite the list comprehension as a regular loop. my_list = [] for module in modules: m = imp.find_module(module)[1] my_list.append(m, os.path.getmtime(m)) From jeanpierreda at gmail.com Fri Dec 23 16:01:17 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 23 Dec 2011 16:01:17 -0500 Subject: Idiom for shelling out to $EDITOR/$PAGER? In-Reply-To: <20111223194753.GA23781@cskk.homeip.net> References: <20111223194753.GA23781@cskk.homeip.net> Message-ID: > Windows may hate you for this with its locking behaviour for open files, > but it will work for UNIX. Windows won't let you open the file while NamedTemporaryFile has a hold on it, yeah. Your point is legitimate anyway. Even so, there's still the race conditions for "what if the tempfile is moved before the editor can get to it" and "what if the tempfile is moved after the editor can get to it but before I can open it again". -- Devin On Fri, Dec 23, 2011 at 2:47 PM, Cameron Simpson wrote: > On 23Dec2011 02:21, Devin Jeanpierre wrote: > | > Anyway, look it up; it has an autodelete mode etc. > | > | The autodelete isn't useful in this context. The file needs to be > | closed and then reopened again; with the autodelete option, the > | closing would delete the file, preventing it from being opened by the > | text editor or reopened to check contents. > | > | (As far as I know, the race condition inherent in this is unavoidable. > | At least on windows, but I couldn't figure out any other way on Linux > | either.) > > You can fire off the editor from inside the with statement. > Write contents, flush, edit, open T.name for read, read, close, exit > with statement. > > Windows may hate you for this with its locking behaviour for open files, > but it will work for UNIX. > > Cheers, > -- > Cameron Simpson DoD#743 > http://www.cskk.ezoshosting.com/cs/ > > The elliptical cam has gradually slid up the beam shaft and caught on the > flange rebate dislodging the gripley, with disastrous results. > ? ? ? ?- Death, 'Reaper Man' (Pratchett) From rosuav at gmail.com Fri Dec 23 16:21:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Dec 2011 08:21:56 +1100 Subject: Get named module's file location In-Reply-To: <32191702.960.1324675017428.JavaMail.geo-discussion-forums@prmw6> References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> <32191702.960.1324675017428.JavaMail.geo-discussion-forums@prmw6> Message-ID: I'm guessing you meant for this to be on-list, and am hoping you don't mind that I'm replying on-list. On Sat, Dec 24, 2011 at 8:16 AM, Gnarlodious wrote: > Chris Angelico wrote: >> [(m, os.path.getmtime(m)) for m in (imp.find_module(module)[1] for >> module in modules)] >> >> Yeah, a little hard to read. Tell me, does this formulation execute >> imp.find_module(module) once or twice for each modname? What this does is save a temporary list, more or less. (It's actually a generator expression, not a list comprehension, but that's immaterial.) temporary = [imp.find_module(module)[1] for module in modules] [(m, os.path.getmtime(m)) for m in temporary] It iterates over modules, calling find_module for each, and saving the results to a new list. Then separately iterates over the new list, pairing each with the getmtime. Since I used parentheses instead of square brackets in the original expression, Python won't actually build the full list. Other than that, it's equivalent to the two-statement version, and you can try those two in IDLE to see what they do. ChrisA From steve+comp.lang.python at pearwood.info Fri Dec 23 17:32:15 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 22:32:15 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef5016e$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 02:55:41 +1100, Chris Angelico wrote: > On Sat, Dec 24, 2011 at 2:49 AM, Steven D'Aprano > wrote: >> To fake early binding when the language provides late binding, you >> still use a sentinel value, but the initialization code creating the >> default value is outside the body of the function, usually in a global >> variable: >> >> ? ?_DEFAULT_Y = [] ?# Private constant, don't touch. >> >> ? ?def func(x, y=None): >> ? ? ? ?if y is None: >> ? ? ? ? ? ?y = _DEFAULT_Y >> ? ? ? ?... >> >> This separates parts of the code that should be together, and relies on >> a global, with all the disadvantages that implies. > > A static variable (in the C sense) would make this just as clean as the > alternative. In Python, that could be implemented as an attribute of the > function object. Oh looky here... that's how default arguments are > implemented. :) Yes. But having to manage it *by hand* is still unclean: * you still have to assign the default value to the function assignment outside the function, which is inelegant; * if you rename the function, the internal lookup func.default_value will fail. I'm not saying it can't be done. I'm just saying that the status quo is cleaner and more elegant, and if you want late binding, there is a simple, obvious, elegant idiom to get it. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 23 17:39:57 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 23 Dec 2011 22:39:57 GMT Subject: Get named module's file location References: <32472953.855.1324656114851.JavaMail.geo-discussion-forums@prix23> <4946660.379.1324659073535.JavaMail.geo-discussion-forums@prez5> <4652751.858.1324669248908.JavaMail.geo-discussion-forums@prj1> Message-ID: <4ef5033c$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 15:00:17 -0500, Roy Smith wrote: >> Can I in some way assign imp.find_module(module)[1] to a variable and >> reuse it? Is this a job for lambda? > > I think what you want to do is rewrite the list comprehension as a > regular loop. > > my_list = [] > for module in modules: > m = imp.find_module(module)[1] > my_list.append(m, os.path.getmtime(m)) +1 List comprehensions are so cool that sometimes people forget that not every loop has to be a list comp. There is no shortage of newlines in the world, and not everything needs to be on a single line. -- Steven From rosuav at gmail.com Fri Dec 23 17:50:04 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Dec 2011 09:50:04 +1100 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: <4ef5016e$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef5016e$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Dec 24, 2011 at 9:32 AM, Steven D'Aprano wrote: > Yes. But having to manage it *by hand* is still unclean: Well, my point was that Python's current behaviour _is_ that. > * you still have to assign the default value to the function assignment > outside the function, which is inelegant; C's static variables are initialized inside the function. But since Python doesn't have that, it doesn't really work that way. (You might be able to use a decorator to do some cool tricks though.) > * if you rename the function, the internal lookup func.default_value will > fail. Python would benefit some from a "current-function" reference, if tricks like this were to be encouraged. It'd help with recursion too. hmmmm... >>> def Y(func): return lambda *args,**kwargs: func(func,*args,**kwargs) >>> @Y def foo(me,x): if x>5: return x return me(me,x+1),7,x >>> foo(3) (((6, 7, 5), 7, 4), 7, 3) Useful? Not very. Maybe as a language feature, but not as a parameter. But of curiosity value. ChrisA From jeanpierreda at gmail.com Fri Dec 23 19:24:44 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 23 Dec 2011 19:24:44 -0500 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: > To fake early binding when the language provides late binding, you still > use a sentinel value, but the initialization code creating the default > value is outside the body of the function, usually in a global variable: > > _DEFAULT_Y = [] # Private constant, don't touch. > > def func(x, y=None): > if y is None: > y = _DEFAULT_Y > ... > > This separates parts of the code that should be together, and relies on a > global, with all the disadvantages that implies. No, you can just do def func(x, y=_DEFAULT_Y): ... -- Devin On Fri, Dec 23, 2011 at 10:49 AM, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 13:13:38 +0000, Neil Cerutti wrote: > >> On 2011-12-23, Neil Cerutti wrote: >>> Is the misfeature that Python doesn't evaluate the default argument >>> expression every time you call the function? What would be the harm if >>> it did? >> >> ...you know, assuming it wouldn't break existing code. ;) > > It will. Python's default argument strategy has been in use for 20 years. > Some code will rely on it. I know mine does. > > There are two strategies for dealing with default arguments that I know > of: early binding and late binding. Python has early binding: the default > argument is evaluated once, when the function is created. Late binding > means the default argument is always re-evaluated each time it is needed. > > Both strategies are reasonable choices. Both have advantages and > disadvantages. Both have use-cases, and both lead to confusion when the > user expects one but gets the other. If you think changing from early to > late binding will completely eliminate the default argument "gotcha", you > haven't thought things through -- at best you might reduce the number of > complaints, but only at the cost of shifting them from one set of use- > cases to another. > > Early binding is simple to implement and simple to explain: when you > define a function, the default value is evaluated once, and the result > stored to be used whenever it is needed. The disadvantage is that it can > lead to unexpected results for mutable arguments. > > Late binding is also simple to explain, but a little harder to implement. > The function needs to store the default value as a piece of code (an > expression) which can be re-evaluated as often as needed, not an object. > > The disadvantage of late binding is that since the expression is live, it > needs to be calculated each time, even if it turns out to be the same > result. But there's no guarantee that it will return the same result each > time: consider a default value like x=time.time(), which will return a > different value each time it is called; or one like x=a+b, which will > vary if either a or b are changed. Or will fail altogether if either a or > b are deleted. This will surprise some people some of the time and lead > to demands that Python "fix" the "obviously buggy" default argument > gotcha. > > If a language only offers one, I maintain it should offer early binding > (the status quo). Why? Because it is more elegant to fake late binding in > an early binding language than vice versa. > > To fake late binding in a language with early binding, use a sentinel > value and put the default value inside the body of the function: > > ? ?def func(x, y=None): > ? ? ? ?if y is None: > ? ? ? ? ? ?y = [] > ? ? ? ?... > > All the important parts of the function are in one place, namely inside > the function. > > To fake early binding when the language provides late binding, you still > use a sentinel value, but the initialization code creating the default > value is outside the body of the function, usually in a global variable: > > ? ?_DEFAULT_Y = [] ?# Private constant, don't touch. > > ? ?def func(x, y=None): > ? ? ? ?if y is None: > ? ? ? ? ? ?y = _DEFAULT_Y > ? ? ? ?... > > This separates parts of the code that should be together, and relies on a > global, with all the disadvantages that implies. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From angrybaldguy at gmail.com Fri Dec 23 21:41:07 2011 From: angrybaldguy at gmail.com (Owen Jacobson) Date: Fri, 23 Dec 2011 21:41:07 -0500 Subject: Idiom for shelling out to $EDITOR/$PAGER? References: Message-ID: <2011122321410754142-angrybaldguy@gmailcom> On 2011-12-23 06:02:18 +0000, Cameron Simpson said: > MacOSX has "open", though it won't be running a blocking editor, alas. But it can be. From the man page: -t Causes the file to be opened with the default text editor, as deter- mined via LaunchServices and -W Causes open to wait until the applications it opens (or that were already open) have exited. Use with the -n flag to allow open to function as an appropriate app for the $EDITOR environment variable. and finally -n Open a new instance of the application(s) even if one is already run- ning. can be combined to make 'open' (the shell interface to OS X's LaunchServices system) into a workable $EDITOR. -o From jason at powerpull.net Sat Dec 24 00:34:46 2011 From: jason at powerpull.net (Jason Friedman) Date: Sat, 24 Dec 2011 05:34:46 +0000 Subject: merging argparse parsers In-Reply-To: <4EEB2D92.10200@gmail.com> References: <4EEB2D92.10200@gmail.com> Message-ID: > I would like to have something like > > merged_parser = LoggingParser() + OtherParser() > > Which should create an argument parser with all the options composed. > I have used parent parsers. http://docs.python.org/py3k/library/argparse.html#parents I think in your case merged_parser would become more of a child, like this: >>> mom_parser = argparse.ArgumentParser(add_help=False) >>> mom_parser.add_argument('--arg1', type=int) >>> dad_parser = argparse.ArgumentParser(add_help=False) >>> dad_parser.add_argument('--arg2', type=int) >>> child_parser = argparse.ArgumentParser(parents=[mom_parser, dad_parser]) From jason at powerpull.net Sat Dec 24 01:04:19 2011 From: jason at powerpull.net (Jason Friedman) Date: Sat, 24 Dec 2011 06:04:19 +0000 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: On Fri, Dec 16, 2011 at 10:44 AM, Chris Angelico wrote: > On Fri, Dec 16, 2011 at 8:45 PM, Ulrich Eckhardt > wrote: >> I'm trying to create a struct_time that is e.g. one year ahead or a month >> back in order to test some parsing/formatting code with different dates. > > Do you need it to be one exact calendar year, or would it make sense > to add/subtract integers from a Unix time? > > t = time.time() + 365*86400 ? # Not actually a year ahead, it's 365 days ahead > t = time.localtime(t) ?# if you want a struct_time > > ChrisA > -- Not particularly elegant, but I believe accurate and relying only on the stated struct_time contract: #!/usr/bin/env python # 2.7.2 import time, itertools def is_local_time_different_by_one_year(time1, time2): if abs(time1.tm_year - time2.tm_year) != 1: return False if abs(time1.tm_mon - time2.tm_mon ) != 0: return False if abs(time1.tm_mday - time2.tm_mday) != 0: return False if abs(time1.tm_hour - time2.tm_hour) != 0: return False if abs(time1.tm_min - time2.tm_min ) != 0: return False if abs(time1.tm_sec - time2.tm_sec ) != 0: return False return True t = time.time() time1 = time.localtime(t) print("Local time is {}.".format(time1)) for i in itertools.count(0): t += 1 # Add one second until we have reached next year time2 = time.localtime(t) if is_local_time_different_by_one_year(time1, time2): print("One year later is {}".format(time2)) break Not exactly a speed demon, either: $ time python timediff.py Local time is time.struct_time(tm_year=2011, tm_mon=12, tm_mday=24, tm_hour=5, tm_min=57, tm_sec=44, tm_wday=5, tm_yday=358, tm_isdst=0). One year later is time.struct_time(tm_year=2012, tm_mon=12, tm_mday=24, tm_hour=5, tm_min=57, tm_sec=44, tm_wday=0, tm_yday=359, tm_isdst=0) real 3m8.922s user 2m2.470s sys 1m1.760s From rosuav at gmail.com Sat Dec 24 02:13:13 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 24 Dec 2011 18:13:13 +1100 Subject: modifying a time.struct_time In-Reply-To: References: Message-ID: On Sat, Dec 24, 2011 at 5:04 PM, Jason Friedman wrote: > Not particularly elegant, but I believe accurate and relying only on > the stated struct_time contract: Funny! But a binary search would be better, I think. t = time.time() time1 = time.localtime(t) print("Local time is {}.".format(time1)) est1 = t + 365*86400 est2 = est1 + 86400 # Assume that the year is between 365 and 366 days long (widen this if assumption not valid) while True: est = (est1 + est2) / 2 time2 = time.localtime(t) cmp = is_local_time_different_by_one_year(time1, time2): if cmp<0: est1 = est elif cmp>0: est2 = est else: print("One year later is {}".format(time2)) break Could do with a few more improvements. Also, it requires that the comparison function return -1, 0, or 1, in the same way that strcmp() does (not a difficult enhancement, but I'm off to Christmas dinner with the family so I'll leave it as an exercise for the reader). ChrisA From steve+comp.lang.python at pearwood.info Sat Dec 24 03:11:12 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Dec 2011 08:11:12 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef5016e$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef58920$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 09:50:04 +1100, Chris Angelico wrote: > On Sat, Dec 24, 2011 at 9:32 AM, Steven D'Aprano > wrote: >> Yes. But having to manage it *by hand* is still unclean: > > Well, my point was that Python's current behaviour _is_ that. Minus the managing it by hand part. >> * you still have to assign the default value to the function assignment >> outside the function, which is inelegant; > > C's static variables are initialized inside the function. But since > Python doesn't have that, it doesn't really work that way. (You might be > able to use a decorator to do some cool tricks though.) If Python were C, then static variables would be the right solution, but since it isn't, they aren't. -- Steven From steve+comp.lang.python at pearwood.info Sat Dec 24 03:25:05 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Dec 2011 08:25:05 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> Message-ID: <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 17:03:11 +0000, Neil Cerutti wrote: >> The disadvantage of late binding is that since the expression is live, >> it needs to be calculated each time, even if it turns out to be the >> same result. But there's no guarantee that it will return the same >> result each time: > > That's its main *advantage*. Ah yes, sorry, poor wording on my part. Whether calculating the default value *once* or *each time* is an advantage or disadvantage depends on what you're trying to do. Either way, it could be just what you want, or an annoying source of bugs. >> consider a default value like x=time.time(), which will return a >> different value each time it is called; or one like x=a+b, which will >> vary if either a or b are changed. Or will fail altogether if either a >> or b are deleted. This will surprise some people some of the time and >> lead to demands that Python "fix" the "obviously buggy" default >> argument gotcha. > > It's hard to see anyone being confused by the resultant exception. That's because you're coming at it from the perspective of somebody who knows what to expect, in the middle of a discussion about the semantics of late binding. Now imagine you're a newbie who has never thought about the details of when the default value is created, but has a function like "def foo(x, y=a+b)". He calls foo(x) seven times and it works, and on the eighth time it blows up, perhaps with a NameError. It's surprising behaviour, and newbies aren't good at diagnosing surprising bugs. Or worse, it doesn't blow up at all, but gives some invalid value that causes your calculations to be completely wrong. Exceptions are not the worst bug to have -- they are the best. > It's > much harder to figure out what's going wrong with an early-bound > mutable. Only for those who don't understand, or aren't thinking about, Python's object model. The behaviour of early-bound mutables is obvious and clear once you think about it, but it does require you to think about what's going on under the hood, so to speak. [...] >> To fake early binding when the language provides late binding, you >> still use a sentinel value, but the initialization code creating the >> default value is outside the body of the function, usually in a global >> variable: [...] > I'd use a function attribute. > > def func(x, y=None): > if y is None: > y = func.default_y > ... > func.default_y = [] > > That's awkward only if you believe function attributes are awkward. I do. All you've done is move the default from *before* the function is defined to *after* the function is defined, instead of keeping it in the function definition. It's still separate, and if the function is renamed your code stops working. In other words, it violates encapsulation of the function. That's not to say that you shouldn't do this. It's a perfectly reasonable technique, and I've used it myself, but it's not as elegant as the current Python default argument behaviour. [...] > The greater efficiency was probably what decided this question for > Python, right? Since late-binding is so easy to fake, is hardly ever > what you want, and would make all code slower, why do it? Excellent point. -- Steven From steve+comp.lang.python at pearwood.info Sat Dec 24 03:26:59 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 24 Dec 2011 08:26:59 GMT Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef58cd3$0$29973$c3e8da3$5496439d@news.astraweb.com> On Fri, 23 Dec 2011 19:24:44 -0500, Devin Jeanpierre wrote: >> To fake early binding when the language provides late binding, you >> still use a sentinel value, but the initialization code creating the >> default value is outside the body of the function, usually in a global >> variable: >> >> _DEFAULT_Y = [] # Private constant, don't touch. >> >> def func(x, y=None): >> if y is None: >> y = _DEFAULT_Y >> ... >> >> This separates parts of the code that should be together, and relies on >> a global, with all the disadvantages that implies. > > No, you can just do def func(x, y=_DEFAULT_Y): ... Point taken. Nevertheless, the semantics are still not the same as actual early binding: if the global name is deleted or changed, the function stops working. -- Steven From lie.1296 at gmail.com Sat Dec 24 05:30:04 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 24 Dec 2011 21:30:04 +1100 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: On 12/22/2011 10:20 AM, Dennis Lee Bieber wrote: > which is to define the names "a", "b", and "c", and connects the three > names to the single object (integer 7 or new empty list). note that this "connects" and "disconnecting" business is more commonly referred to in python parlance as "binding" a name to an object. From mmanns at gmx.net Sat Dec 24 07:03:36 2011 From: mmanns at gmx.net (Martin Manns) Date: Sat, 24 Dec 2011 13:03:36 +0100 Subject: [ANN] pyspread 0.2.0 Message-ID: ============== pyspread 0.2.0 ============== Pyspread 0.2.0 has been released. The new version is an almost complete rewrite for better stability and maintainability. About pyspread ============== Pyspread is a non-traditional spreadsheet application that is based on and written in Python. Pyspread expects Python expressions in its grid cells, which makes a spreadsheet specific language obsolete. Pyspread is free software. It is released under the GPL v3. The project website has changed to http://manns.github.com/pyspread/ What's new in 0.2.0 =================== + Windows support discontinued + New pys save file format + A lot of bug fixes and small changes to the user interface Enjoy Martin From wuwei23 at gmail.com Sat Dec 24 08:43:20 2011 From: wuwei23 at gmail.com (alex23) Date: Sat, 24 Dec 2011 05:43:20 -0800 (PST) Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 24, 2:15?am, Roy Smith wrote: > I know this is not quite the same thing, but it's interesting to look at > what django (and mongoengine) do in their model definitions, prompted by > your time.time() example. ?You can do declare a model field something > like: > > class Foo(models.Model): > ? ?timestamp = DateTimeField(default=datetime.utcnow) > > Now, when you create a Foo, if you don't supply a timestamp, it > generates one by calling utcnow() for you. ?Very handy. > > I'm not arguing that Python should do that, just pointing out an example > of where late binding is nice. There is absolutely nothing stopping you from writing functions now with that behaviour. All Python functions are "early binding, late calling" with their arguments, if you treat the arguments as callables within the function body. From wuwei23 at gmail.com Sat Dec 24 08:52:24 2011 From: wuwei23 at gmail.com (alex23) Date: Sat, 24 Dec 2011 05:52:24 -0800 (PST) Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <0fea2ea6-80df-42c4-8fbb-5b4314095672@h37g2000pri.googlegroups.com> On Dec 24, 2:27?am, Mel Wilson wrote: > In a tool that's meant for other people to use to accomplish work of their > own, breaking workflow is a cardinal sin. > > In a research language that's meant always to be up-to-date with the concept > of the week, not so much. What on earth gave you the impression Python was bleeding edge? As there's more to the language than its syntax, "breaking workflow" disrupts the core library as much as it does the code of everyone else. More importantly, you're talking pap. Research is as much about communication as programming; if you expect every single researcher in a discipline (or even in the same _building_) to remain in perfect lockstep with the version releases of a domain-relevant language, you're either not a researcher or not a very good one. You should get out to a conference occasionally and see what people think about your "concept of the week" idea. From wuwei23 at gmail.com Sat Dec 24 09:08:54 2011 From: wuwei23 at gmail.com (alex23) Date: Sat, 24 Dec 2011 06:08:54 -0800 (PST) Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 24, 6:25?pm, Steven D'Aprano wrote: > > It's > > much harder to figure out what's going wrong with an early-bound > > mutable. > > Only for those who don't understand, or aren't thinking about, Python's > object model. The behaviour of early-bound mutables is obvious and clear > once you think about it, but it does require you to think about what's > going on under the hood, so to speak. And here we've come full circle to the point of this thread. If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd see an increase in the number of questions like the OP's. From hoogendoorn.eelco at gmail.com Sat Dec 24 09:39:39 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 06:39:39 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> Message-ID: <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> On Dec 20, 4:30?am, alex23 wrote: > On Dec 19, 8:15?pm, Eelco wrote: > > > What does that have to do with collection packing/unpacking? > > It's mocking your insistance that collection unpacking is a type > constraint. This is really going to be the last time I waste any words on this: The sentence 'collection unpacking is a type constraint' is entirely nonsensical. A type constraint is a linguistical construct that can be applied in many ways; typically, to narrow down the semantics of use of the symbol to which the type constraint is applied. In case of python, collection PACKING (not unpacking) is signaled by a construct that can be viewed as a type constraint. But if you dont want to fortify your view of the syntax by looking at what it is actually going on, ill repeat again; lets keep things simple, and not analyze it in detail. So here it is again, in terms every 5 year old can understand. Id like to do the exact same thing python is already doing. Except with a few more, and different symbols, to enable one to express a few different variants of behavior. Clear enough? From hoogendoorn.eelco at gmail.com Sat Dec 24 09:41:46 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 06:41:46 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> Message-ID: <6b44b5b2-7e86-4f54-86bd-77ab4cbda9bd@i6g2000vbe.googlegroups.com> On Dec 20, 4:35?am, alex23 wrote: > Eelco wrote: > > Having two seperate symbols seperated by whitespace, as in @list args > > strikes me as a terrible break of normal python lexical rules. > > You mean like 'is not'? And the upcoming 'yield from'? Im not sure why, but this feels like something entirely different to me. I suppose because these are compositions of keywords. Can you give an example of a whitespaced composition of identifiers being a valid construct anywhere else in Python? From hoogendoorn.eelco at gmail.com Sat Dec 24 09:45:01 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 06:45:01 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Dec 20, 6:47?am, Steven D'Aprano wrote: > On Mon, 19 Dec 2011 19:35:20 -0800, alex23 wrote: > > Eelco wrote: > >> Having two seperate symbols seperated by whitespace, as in @list args > >> strikes me as a terrible break of normal python lexical rules. > > > You mean like 'is not'? And the upcoming 'yield from'? > > Also "not in". > > Space-delimited tokens are hardly rare in Python, e.g.: > > import module as name > for x in sequence > if flag > elif condition > while condition > with obj > del name > > Nevertheless, I think the suggested syntax "@list args" is awful. > > -- > Steven Can you give an example of a construct in python where two whitespace delimited identifiers are legal? From hoogendoorn.eelco at gmail.com Sat Dec 24 09:47:21 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 06:47:21 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> Message-ID: <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> On Dec 21, 4:48?pm, Neal Becker wrote: > Clarification: where can packing/unpacking syntax be used? > > It would be great if it were valid essentially anywhere (not limited to > parameter passing). > > What about constructs like: > > a, @tuple tail, b = sequence? This has come up many times in the thread, including in my OP. More closely unifying collection packing/unpacking is part of my goal, so indeed, I would like to be able to write something like: a, middle::tuple, b = ::sequence Where I would like the extra :: before the sequence to explicitly signal collection unpacking on the rhs, to maintain the symmetry with collection unpacking within a function call. From hoogendoorn.eelco at gmail.com Sat Dec 24 09:54:07 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 06:54:07 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 22, 2:12?pm, Steven D'Aprano wrote: > On Thu, 22 Dec 2011 06:49:16 -0500, Neal Becker wrote: > > I agree with the OP that the current syntax is confusing. ?The issue is, > > the meaning of * is context-dependent. > > Here you are complaining about an operator being "confusing" because it > is context-dependent, in a post where you strip all context except the > subject line and expect us to still understand what you're talking about. > There's a lesson there, I'm sure. > > * is context dependent. You know what else is context dependent? Well, > most things. But in particular, parentheses. Clearly they must be > "confusing" too. So how about we say: > > class MyClass superclasslist A, B C: > ? ? def method argumentlist self, x, y: > ? ? ? ? t = tuple 1, 2 tuple 3, 4 endtuple endtuple > ? ? ? ? return group x + y endgroup * group x - y endgroup > > Much less confusing! > > -- > Steven Context dependence is not something to be avoided at all costs, but all else being equal, less is certainly more. The general concept of grouping thing together which parenthesis is an extremely pervasive one in programming, and thus deserves its own set of context-dependent rules. Packing and unpacking collections is far, far more rare, and thus a form that requires you to write more but remember less is certainly relatively favorable. From rosuav at gmail.com Sat Dec 24 09:57:18 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Dec 2011 01:57:18 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> Message-ID: On Sun, Dec 25, 2011 at 1:47 AM, Eelco wrote: > a, middle::tuple, b = ::sequence Then it ought to be ::(a, middle::tuple, b) = ::sequence or something, because you're doing the same thing on both sides. ChrisA From rosuav at gmail.com Sat Dec 24 10:01:04 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Dec 2011 02:01:04 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Sun, Dec 25, 2011 at 1:45 AM, Eelco wrote: > Can you give an example of a construct in python where two whitespace > delimited identifiers are legal? What do you mean? Two identifiers, separated only by whitespace and no keyword or operator? def foo(): asdf qwer Perfectly legal, two statements that probably don't do anything useful though. ChrisA From tinnews at isbd.co.uk Sat Dec 24 10:26:21 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Sat, 24 Dec 2011 15:26:21 +0000 Subject: How to check for single character change in a string? Message-ID: Can anyone suggest a simple/easy way to count how many characters have changed in a string? E.g. giving results as follows:- abcdefg abcdefh 1 abcdefg abcdekk 2 abcdefg gfedcba 6 Note that position is significant, a character in a different position should not count as a match. Is there any simpler/neater way than just a for loop running through both strings and counting non-matching characters? -- Chris Green From ian.g.kelly at gmail.com Sat Dec 24 10:57:01 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 24 Dec 2011 08:57:01 -0700 Subject: How to check for single character change in a string? In-Reply-To: References: Message-ID: On Sat, Dec 24, 2011 at 8:26 AM, wrote: > Can anyone suggest a simple/easy way to count how many characters have > changed in a string? > > E.g. giving results as follows:- > > ? ?abcdefg ? ? abcdefh ? ? ? ? 1 > ? ?abcdefg ? ? abcdekk ? ? ? ? 2 > ? ?abcdefg ? ? gfedcba ? ? ? ? 6 > > > Note that position is significant, a character in a different position > should not count as a match. > > Is there any simpler/neater way than just a for loop running through > both strings and counting non-matching characters? No, but the loop approach is pretty simple: sum(a == b for a, b in zip(str1, str2)) From roy at panix.com Sat Dec 24 10:57:14 2011 From: roy at panix.com (Roy Smith) Date: Sat, 24 Dec 2011 10:57:14 -0500 Subject: How to check for single character change in a string? References: Message-ID: In article , tinnews at isbd.co.uk wrote: > Can anyone suggest a simple/easy way to count how many characters have > changed in a string? Depending on exactly how you define "changed", you're probably talking about either Hamming Distance or Levenshtein Distance. I would start with the wikipedia articles on both those topics and explore from there. There are python packages for computing many of these metrics. For example, http://pypi.python.org/pypi/python-Levenshtein/ > Is there any simpler/neater way than just a for loop running through > both strings and counting non-matching characters? If you don't care about insertions and deletions (and it sounds like you don't), then this is the way to do it. It's O(n), and you're not going to get any better than that. It's a one-liner in python: >>> s1 = 'abcdefg' >>> s2 = 'abcdekk' >>> len([x for x in zip(s1, s2) if x[0] != x[1]]) 2 But go read the wikipedia articles. Computing distance between sequences is an interesting, important, and well-studied topic. It's worth exploring a bit. From roy at panix.com Sat Dec 24 11:10:41 2011 From: roy at panix.com (Roy Smith) Date: Sat, 24 Dec 2011 11:10:41 -0500 Subject: How to check for single character change in a string? References: Message-ID: In article , Roy Smith wrote: > >>> len([x for x in zip(s1, s2) if x[0] != x[1]]) Heh, Ian Kelly's version: > sum(a == b for a, b in zip(str1, str2)) is cleaner than mine. Except that Ian's counts matches and the OP asked for non-matches, but that's an exercise for the reader :-) From arnodel at gmail.com Sat Dec 24 12:09:57 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sat, 24 Dec 2011 17:09:57 +0000 Subject: How to check for single character change in a string? In-Reply-To: References: Message-ID: On 24 December 2011 16:10, Roy Smith wrote: > In article , > ?Roy Smith wrote: > >> >>> len([x for x in zip(s1, s2) if x[0] != x[1]]) > > Heh, Ian Kelly's version: > >> sum(a == b for a, b in zip(str1, str2)) > > is cleaner than mine. ?Except that Ian's counts matches and the OP asked > for non-matches, but that's an exercise for the reader :-) Here's a variation on the same theme: sum(map(str.__ne__, str1, str2)) -- Arnaud From hoogendoorn.eelco at gmail.com Sat Dec 24 12:21:37 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 09:21:37 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> Message-ID: On Dec 24, 3:57?pm, Chris Angelico wrote: > On Sun, Dec 25, 2011 at 1:47 AM, Eelco wrote: > > a, middle::tuple, b = ::sequence > > Then it ought to be > > ::(a, middle::tuple, b) = ::sequence > > or something, because you're doing the same thing on both sides. > > ChrisA Thats a fair point; something to think about. It all depends on how you define the rules of course; im trying to stay as close as possible to the original python rules, where the (un)packing of comma-seperated identifiers is implicit. Probably best to keep it that way, from the looks of it :). From hoogendoorn.eelco at gmail.com Sat Dec 24 12:23:59 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sat, 24 Dec 2011 09:23:59 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Dec 24, 4:01?pm, Chris Angelico wrote: > On Sun, Dec 25, 2011 at 1:45 AM, Eelco wrote: > > Can you give an example of a construct in python where two whitespace > > delimited identifiers are legal? > > What do you mean? Two identifiers, separated only by whitespace and no > keyword or operator? > > def foo(): > ? ? asdf > ? ? qwer > > Perfectly legal, two statements that probably don't do anything useful though. > > ChrisA Thats not a fair point, but more nitpicking. Yes, I should have been more precise: in python, 'whitespace' is not a single beast like in most languages, but newlines have a special meaning. I was obviously not talking about those. Want to try again? From k.sahithi2862 at gmail.com Sat Dec 24 12:33:28 2011 From: k.sahithi2862 at gmail.com (SAHITHI) Date: Sat, 24 Dec 2011 09:33:28 -0800 (PST) Subject: YOUTH SPECIAL HOT PICS Message-ID: FOR GOOD JOBS SITES TO YOU http://goodjobssites.blogspot.com/ BUSINESSMAN MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/businessman-movie-stills.html HOT ACTRESS BIKINI STILLS IN 2012 CALENDAR http://actressgallery-kalyani.blogspot.com/2011/12/2012-actress-calendar-wallpapers.html SAMANTHA SOUTHSCOPE HOT PHOTOS http://actressgallery-kalyani.blogspot.com/2011/12/samantha-at-south-scope-magazine.html DEEPIKA PADUKONE SPICY WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/deepika-padukone.html KATRINA KAIF LATEST HOT WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/katrina-kaif-wallpapers.html LOVE FAILURE MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/love-failure-movie-stills.html 4FRIENDS MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/4-friends-movie-stills.html NANDEESWARUDU MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/nandeeswarudu-movie-stills.html HARI PRIYA HOT PHOTO STILLS http://actressgallery-kalyani.blogspot.com/2011/12/haripariya-actress.html SHRUTI HASSAN HOT IN 3 MOVIE http://actressgallery-kalyani.blogspot.com/2011/11/shruti-hassan-in-3-movie.html PANJA MOVIE LATEST STILLS http://actressgallery-kalyani.blogspot.com/2011/11/panja-movie-stills.html NIPPU MOVIE WORKING STILLS http://actressgallery-kalyani.blogspot.com/2011/11/nippu-movie-stills.html FOR FAST UPDATES IN FILM INDUSTRY KATRINA KAIF RARE PHOTOS http://allyouwants.blogspot.com/2011/12/katrina-kaif.html KAJAL AGARWAL LATEST STILLS http://allyouwants.blogspot.com/2011/03/kajal-latest-stills.html PRIYANKA CHOPRA LATEST HOT PHOTOSHOOT http://allyouwants.blogspot.com/2011/08/priyanka-chopra.html TAMIL ACTRESS HOT PHOTOS&VIDEOS http://allyouwants.blogspot.com/2011/08/tamil-actress.html FOR ONLY HOT GUYS SEE THIS LATEST HOT KATRINA KAIF PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/katrina-kaif-hot.html ANUSHKA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/anushka-hot.html PRIYANKA TIWARI HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/12/priyanka-tiwari-hot.html TAMANNA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/tamanna-hot.html PARUL HOT PHOTO STILLS http://hotactress-kalyani.blogspot.com/2011/12/parul-hot.html ADITI AGARWAL NEW ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/12/aditi-agarwal-hot.html PAYAL GHOSH HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/payal-ghosh-hot.html RAGINI DWIVEDI HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/12/ragini-dwivedi.html PARVATHI MELTON LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/parvathi-melton-hot.html SARAH JANE DIAS HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/sarah-jane-dias-hot.html KAJAL AGARWAL HOT SAREE STILLS http://hotactress-kalyani.blogspot.com/2011/11/kajal-agarwal-hot-in-saree.html POONAM KAUR HOT ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/11/poonam-kaur-hot.html From rosuav at gmail.com Sat Dec 24 12:51:32 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Dec 2011 04:51:32 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> Message-ID: On Sun, Dec 25, 2011 at 4:23 AM, Eelco wrote: > Thats not a fair point, but more nitpicking. Yes, I should have been > more precise: in python, 'whitespace' is not a single beast like in > most languages, but newlines have a special meaning. I was obviously > not talking about those. Want to try again? In that case I can't think of any, but at least now you've clarified the question (by not denying the rest of it). It was somewhat ambiguous. ChrisA From rantingrickjohnson at gmail.com Sat Dec 24 13:22:52 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sat, 24 Dec 2011 10:22:52 -0800 (PST) Subject: How to check for single character change in a string? References: Message-ID: On Dec 24, 11:09?am, Arnaud Delobelle wrote: > sum(map(str.__ne__, str1, str2)) Mirror, mirror, on the wall. Who's the cleanest of them all? From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Sat Dec 24 13:41:55 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Sat, 24 Dec 2011 19:41:55 +0100 Subject: what does 'a=b=c=[]' do In-Reply-To: <4ef2704d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <4ef2704d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 22.12.2011 00:48 schrieb Steven D'Aprano: > On Wed, 21 Dec 2011 18:20:16 -0500, Dennis Lee Bieber wrote: > >> For the amount of typing, it's easier to just do a straight line >> tuple unpack >> >>>>> a,b,c = ([],[],[]) > > Note that tuples are created by the comma, not the round brackets (or > parentheses for any Americans reading). So the round brackets there are > strictly redundant: > > a, b, c = [], [], [] > > The only times you need the brackets around a tuple is to control the > precedence of operations, or for an empty tuple. IBTD: a=((a, b) for a, b, c in some_iter) b=[(1, c) for ] Without the round brackets, it is a syntax error. Thomas From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Sat Dec 24 13:45:39 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Sat, 24 Dec 2011 19:45:39 +0100 Subject: what does 'a=b=c=[]' do In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: Am 22.12.2011 00:20 schrieb Dennis Lee Bieber: > The key one is that lists ([] defines a list, not an array) are > "mutable". Your "7" is not mutable. Strictly spoken, that's only a "side show" where the effect is visible. The real key concept is that [] creates *one* object which is then assigned to the three names. That's the same for mutable and immutable objects. But only the modification which happens on mutable objects turns it into a problem. The rest o your explanation is 100% correct. Thomas From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Sat Dec 24 13:49:53 2011 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Sat, 24 Dec 2011 19:49:53 +0100 Subject: what does 'a=b=c=[]' do In-Reply-To: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> Message-ID: Am 21.12.2011 23:25 schrieb Eric: > Is it true that if I want to create an array or arbitrary size such > as: > for a in range(n): > x.append() > > I must do this instead? > x=[] > for a in range(n): > x.append() Of course - your x must exist before using it. > Now to my actual question. I need to do the above for multiple arrays > (all the same, arbitrary size). So I do this: > x=y=z=[] > for a in range(n): > x.append() > y.append() > z.append() > Also, is there a more pythonic way to do "x=[], y=[], z=[]"? You could do: def create_xyz(n): for a in range(n): yield , , \ ) x, y, z = zip(*create_xyz(11)) or, if you want x, y, z to be lists, x, y, z = [list(i) for i in zip(*create_xyz(11))] . Thomas From wolftracks at invalid.com Sat Dec 24 13:55:48 2011 From: wolftracks at invalid.com (W. eWatson) Date: Sat, 24 Dec 2011 10:55:48 -0800 Subject: Is my IDLE problem caused by .idlerc? Permissions. Message-ID: I installed 64-bit Python 2.7.2 on Win 7. IDLE does appear as the second choice on right-click of a py file menu. However, nothing happens if I use it. If I try to execute idle from the cmd window, it complains about .idlerc\recent-files.lst IOError: [Errno 13] Permission denied: 'C:\\Users\\Wayne\\.idlerc\\recent-files.lst' These are permissions for it. Permissions as follows: SYSTEM: All. From Full control to write Account Unknown(S-1-5-21...): read&exec, Read Wayne: (normal use) All. From Full control to write Admin: All. From Full control to write WMPNetwork: Read Comments? .idlerc permissions are slightly different for Account Unknown. From zyzhu2000 at gmail.com Sat Dec 24 16:19:44 2011 From: zyzhu2000 at gmail.com (GZ) Date: Sat, 24 Dec 2011 13:19:44 -0800 (PST) Subject: Generator Question References: <7864542c-d96b-48a6-a329-321f3648be6d@32g2000yqp.googlegroups.com> <4ef2cfb2$0$13942$c3e8da3$76491128@news.astraweb.com> Message-ID: <85113506-abcb-4008-877a-c2c2a3fae2e6@k10g2000yqk.googlegroups.com> I see. Thanks for the clarification. On Dec 22, 12:35?am, Steven D'Aprano wrote: > On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: > > Now the question here is this: > > > def h(): > > ? ? if condition=true: > > ? ? ? ?#I would like to return an itereator with zero length > > ? ? else: > > ? ? ? ?for ...:yieldx > > > In other words, when certain condition is met, I want toyieldnothing. > > How to do? > > Actually, there's an even easier way. > > >>> def h(): > > ... ? ? if not condition: > ... ? ? ? ? for c in "abc": > ... ? ? ? ? ? ?yieldc > ... > > >>> condition = False > >>> list(h()) > ['a', 'b', 'c'] > >>> condition = True > >>> list(h()) > > [] > > -- > Steven > > From jeanpierreda at gmail.com Sat Dec 24 18:25:24 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sat, 24 Dec 2011 18:25:24 -0500 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: > If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd > see an increase in the number of questions like the OP's. What makes you so sure? Both models do make sense and are equally valid, it's just that only one of them is true. Is it just because people already used to Python would get confused? -- Devin On Sat, Dec 24, 2011 at 9:08 AM, alex23 wrote: > On Dec 24, 6:25?pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> > It's >> > much harder to figure out what's going wrong with an early-bound >> > mutable. >> >> Only for those who don't understand, or aren't thinking about, Python's >> object model. The behaviour of early-bound mutables is obvious and clear >> once you think about it, but it does require you to think about what's >> going on under the hood, so to speak. > > And here we've come full circle to the point of this thread. > > If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd > see an increase in the number of questions like the OP's. > -- > http://mail.python.org/mailman/listinfo/python-list From speeze.pearson at gmail.com Sat Dec 24 18:49:59 2011 From: speeze.pearson at gmail.com (Spencer Pearson) Date: Sat, 24 Dec 2011 15:49:59 -0800 (PST) Subject: Adding an interface to existing classes References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> Message-ID: <256b9bcf-aa08-4d3e-9928-8fa9d335f1ff@e8g2000prb.googlegroups.com> On Dec 23, 9:13?am, Terry Reedy wrote: > On 12/22/2011 3:21 AM, Spencer Pearson wrote: > > > I'm writing a geometry package, with Points and Lines and Circles and > > so on, and eventually I want to be able to draw these things on the > > screen. I have two options so far for how to accomplish this, but > > neither of them sits quite right with me, and I'd like the opinion of > > comp.lang.python's wizened elders. > > > Option 1. Subclassing. > > The most Pythonic way would seem to be writing subclasses for the > > things I want to display, adding a ".draw(...)" method to each one, > > like this: > > Option 2. A "draw" function, with a function dictionary. > > Option 3? Add a draw method to existing classes, rather than subclassing? > > -- > Terry Jan Reedy Thanks for the response! Do you mean something like this? class Point(GeometricObject): def intersect(self, other): ... def union(self, other): ... def draw(self, ...): ... I'd like to avoid this, because... well, what I want is a geometry package, and it seems to me that whistles and bells like GUI support ought to be confined to subpackages. I'd look at this and think, "the rest of the Point class deals with fundamental geometric reality. What's this GUI method doing mixed in with my beautiful mathematical purity?" Is this the wrong attitude to take? Or did you mean this? In file "geometry/gui.py": def draw_point(point, ...): ... Point.draw = draw_point I've never modified an existing class before, and I fear the unfamiliar. If that's what you meant... it's really an acceptable thing to do? It seems like somebody might see "some_point.draw(...)" and be confused by the method's absence in the Point class definition. (A hybrid approach would be to say "draw = geometry.gui.draw_point" inside the body of the Point class, setting the attribute there instead of in gui.py, but that makes me uneasy for the same reason as putting the function's full definition in the class.) Thanks again, -Spencer From wuwei23 at gmail.com Sat Dec 24 19:10:35 2011 From: wuwei23 at gmail.com (alex23) Date: Sat, 24 Dec 2011 16:10:35 -0800 (PST) Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 25, 9:25?am, Devin Jeanpierre wrote: > > If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd > > see an increase in the number of questions like the OP's. > > What makes you so sure? Both models do make sense and are equally > valid, it's just that only one of them is true. Is it just because > people already used to Python would get confused? Because I believe that the source of confusion has far more to do with mutable/immutable objects than with early/late binding. Masking or 'correcting' an aspect of Python's behaviour because novices make the wrong assumption about it just pushes the problem elsewhere and potentially makes the language inconsistent at the same time. From wuwei23 at gmail.com Sat Dec 24 19:24:07 2011 From: wuwei23 at gmail.com (alex23) Date: Sat, 24 Dec 2011 16:24:07 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> Message-ID: On Dec 25, 12:39?am, Eelco wrote: > This is really going to be the last time I waste any words on this Oh hey, don't feel you actually have to justify the bullshit you're talking for my sake. > In case of python, collection PACKING (not unpacking) is signaled by a > construct that can be viewed as a type constraint. _But no one does view it that way because it isn't_. No more so than [] taking a string separated list of arguments and return a list is a type constraint. _That's it's behaviour_. We have a language construct that returns a tuple, because in the context of what tuples are in Python, that makes sense. There are _plenty_ of such constructs. You have still yet to show what adding all of this ridiculous shit to a function signature provides that coercing the resulting tuple to your own type doesn't. > So here it is again, in terms every 5 year old can understand. Id like > to do the exact same thing python is already doing. Except with a few > more, and different symbols, to enable one to express a few different > variants of behavior. Clear enough? That you're a condescending douchebag with nothing of value to contribute? Crystal. From jeanpierreda at gmail.com Sat Dec 24 19:32:06 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sat, 24 Dec 2011 19:32:06 -0500 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: > Because I believe that the source of confusion has far more to do with > mutable/immutable objects than with early/late binding. Masking or > 'correcting' an aspect of Python's behaviour because novices make the > wrong assumption about it just pushes the problem elsewhere and > potentially makes the language inconsistent at the same time. That seems fairly silly -- foo.append(bar) obviously mutates _something_ . Certainly it wasn't the source of my confusion when I got caught on this. What makes you believe that the fundamental confusion is about mutability? (Also, if the change is applied everywhere, the language would not be inconsistent.) -- Devin On Sat, Dec 24, 2011 at 7:10 PM, alex23 wrote: > On Dec 25, 9:25?am, Devin Jeanpierre wrote: >> > If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd >> > see an increase in the number of questions like the OP's. >> >> What makes you so sure? Both models do make sense and are equally >> valid, it's just that only one of them is true. Is it just because >> people already used to Python would get confused? > > Because I believe that the source of confusion has far more to do with > mutable/immutable objects than with early/late binding. Masking or > 'correcting' an aspect of Python's behaviour because novices make the > wrong assumption about it just pushes the problem elsewhere and > potentially makes the language inconsistent at the same time. > -- > http://mail.python.org/mailman/listinfo/python-list From rantingrickjohnson at gmail.com Sat Dec 24 20:01:27 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sat, 24 Dec 2011 17:01:27 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> Message-ID: On Dec 24, 6:24?pm, alex23 wrote: > That you're a condescending douchebag with nothing of value to > contribute? > > Crystal. Take it from me Eelco. Once Alex drops into your thread and starts name calling, it's over my friend. From ian.g.kelly at gmail.com Sat Dec 24 20:16:42 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 24 Dec 2011 18:16:42 -0700 Subject: Adding an interface to existing classes In-Reply-To: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> Message-ID: On Thu, Dec 22, 2011 at 1:21 AM, Spencer Pearson wrote: > I see a problem with this, though. The intersection of two lines is > (usually) an object of type Point. Since DrawableLine inherits from > Line, this means that unless I redefine the "intersect" method in > DrawableLine, the intersection of two DrawableLines will be a Point > object, not a DrawablePoint. I don't want to saddle the user with the > burden of converting every method output into its corresponding > Drawable subclass, and I don't want to redefine every method to return > an instance of said subclass. I see no other options if I continue > down the subclassing path. Am I missing something? You could solve this with a factory. Instead of having Line.intersection() create a Point directly, have it call self.factory.create_point(). In the drawing module, replace the factory for the subclasses with one that creates drawable classes instead. > Option 2. A "draw" function, with a function dictionary. > This feels weird, but is fairly simple to write, use, and extend. We > have a module with a "draw_functions" dictionary that maps types onto > functions, and a "draw" function that just looks up the proper type in > the dictionary and calls the corresponding function. If you create > your own object, you can just add a new entry to the dictionary. The > implementation is simple enough to outline here: This will also work, but inheritance complicates things a little. Do you do a type equality check or an isinstance() check when looking up the type in the dictionary? If the former, then somebody who subclasses geometry.Line to create a GroovyLine subclass must add an entry for GroovyLine to the dictionary, even if the drawing code is the same. The drawing code is not inherited. But if you do an isinstance() check, then you need to be very careful about how you check it. If somebody creates a WavyLine subclass and registers a different drawing method, then you need to make sure the isinstance() check for the WavyLine implementation happens before the regular Line implementation, or the wrong drawing code may get invoked for WavyLine. Probably the easiest way to do this correctly is to follow the MRO that Python has conveniently already built for you. Something like: def draw(x, *args, **kwargs ): for class_ in type(x).__mro__: if class_ in draw_functions: draw_functions[class_](*args, **kwargs) break else: raise TypeError("don't know how to draw things of type " "{0}".format(type(x))) Cheers, Ian From rantingrickjohnson at gmail.com Sat Dec 24 20:24:14 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sat, 24 Dec 2011 17:24:14 -0800 (PST) Subject: Adding an interface to existing classes References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> Message-ID: <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> On Dec 22, 2:21?am, Spencer Pearson wrote: > I'm writing a geometry package, with Points and Lines and Circles and > so on, and eventually I want to be able to draw these things on the > screen. ...which is the main reason for creating a geometry package in the first place!. I mean, imaginary points and lines are great if you are a theoretical mathematician, but for the rest of us earthlings, plotted pixels are the "in thang" now-a-days. > I have two options so far for how to accomplish this, but > neither of them sits quite right with me, and I'd like the opinion of > comp.lang.python's wizened elders. > > Option 1. Subclassing. > The most Pythonic way would seem to be writing subclasses for the > things I want to display, adding a ".draw(...)" method to each one, > like this: > class DrawablePoint( geometry.Point ): > ? ? class draw( self, ... ): > ? ? ? ? ... "DrawablePoint"? I suppose there is an "ImaginaryPoint" somewhere in this API? Geez > When the time comes to draw things, I'll have some list of objects I > want drawn, and say > for x in to_draw: > ? ? x.draw(...) Seems reasonable. Not the fastest approach, but a good start for a very high level interface. > I see a problem with this, though. The intersection of two lines is > (usually) an object of type Point. Since DrawableLine inherits from > Line, Why the hell is "Drawable" inheriting from Line? I would think that a "Line" would be "Drawable" object and NOT vice-versa? Am i wrong? > this means that unless I redefine the "intersect" method in > DrawableLine, the intersection of two DrawableLines will be a Point > object, not a DrawablePoint. OMFG! > I don't want to saddle the user with the > burden of converting every method output into its corresponding > Drawable subclass, and I don't want to redefine every method to return > an instance of said subclass. I see no other options if I continue > down the subclassing path. Am I missing something? Yes, a proper object hierarchy and API it seems @_ at . Spencer, i would re-think this entire project from the beginning. You are trying to make an object out of everything. You don't need to make an object of EVERYTHING. Ask yourself, what are the most basic objects of a geometry library, and then report back to us your findings. PS: I prefer option1 for these things as the OOP paradigm fits nicely. I just hate to have modules of loose functions just lying about. From tjreedy at udel.edu Sat Dec 24 20:58:24 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 24 Dec 2011 20:58:24 -0500 Subject: Adding an interface to existing classes In-Reply-To: <256b9bcf-aa08-4d3e-9928-8fa9d335f1ff@e8g2000prb.googlegroups.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <256b9bcf-aa08-4d3e-9928-8fa9d335f1ff@e8g2000prb.googlegroups.com> Message-ID: On 12/24/2011 6:49 PM, Spencer Pearson wrote: > On Dec 23, 9:13 am, Terry Reedy wrote: >> On 12/22/2011 3:21 AM, Spencer Pearson wrote: >> >>> I'm writing a geometry package, with Points and Lines and Circles and >>> so on, and eventually I want to be able to draw these things on the >>> screen. I have two options so far for how to accomplish this, but >>> neither of them sits quite right with me, and I'd like the opinion of >>> comp.lang.python's wizened elders. >> >>> Option 1. Subclassing. >>> The most Pythonic way would seem to be writing subclasses for the >>> things I want to display, adding a ".draw(...)" method to each one, >>> like this: There are people who would advocate a Drawable base class with a virtual or abstract .draw method and that DrawablePoint, etc, inherit from Drawable and Point. >>> Option 2. A "draw" function, with a function dictionary. >> >> Option 3? Add a draw method to existing classes, rather than subclassing? > Thanks for the response! Do you mean something like this? > class Point(GeometricObject): > def intersect(self, other): > ... I am interpreting this to mean that you have a world coordinate system for instances that have location and size. > def union(self, other): > ... > def draw(self, ...): > ... Yes. I would consider that Option 0, the default, unless you have good reason to choose another. I would certainly include it on a list of options. > I'd like to avoid this, because... well, what I want is a geometry > package, and it seems to me that whistles and bells like GUI support > ought to be confined to subpackages. I'd look at this and think, "the > rest of the Point class deals with fundamental geometric reality. > What's this GUI method doing mixed in with my beautiful mathematical > purity?" By default, all Python objects have a text representation method. I do not see that giving all concrete geometric objects (with a location and size) a visual representation is much different. I would use drawing functions that accept the coordinates and distances of your geometry world and translate to low-level pixel functions for a particular gui system. I agree that your geometrical objects should not know about pixels, screens, windows, and aspect ratios. > Is this the wrong attitude to take? It depends on *your* goal and values. > Or did you mean this? > In file "geometry/gui.py": > def draw_point(point, ...): > ... > Point.draw = draw_point > > I've never modified an existing class before, and I fear the > unfamiliar. If that's what you meant... it's really an acceptable > thing to do? Yes, in my opinion. The advantage of this is putting all the draw methods together, and possibly having more than one one set. On the other hand, one needs to know the data attributes of each class to understand its draw method. > It seems like somebody might see "some_point.draw(...)" > and be confused by the method's absence in the Point class definition. With either suboption, you should put an abstract .draw method in the GeometricObject base class. I would look at various game, graph, geometry, and gui packages handle drawing for more ideas. -- Terry Jan Reedy From rustompmody at gmail.com Sat Dec 24 22:22:37 2011 From: rustompmody at gmail.com (rusi) Date: Sat, 24 Dec 2011 19:22:37 -0800 (PST) Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <5198e7e2-859b-4894-8170-16ad35d67625@h7g2000prn.googlegroups.com> On Dec 25, 5:32?am, Devin Jeanpierre wrote: > alex23 wrote: > > Because I believe that the source of confusion has far more to do with > > mutable/immutable objects than with early/late binding. Masking or > > 'correcting' an aspect of Python's behaviour because novices make the > > wrong assumption about it just pushes the problem elsewhere and > > potentially makes the language inconsistent at the same time. Thats hitting the nail on the head. > > That seems fairly silly -- foo.append(bar) obviously mutates > _something_ . Certainly it wasn't the source of my confusion when I > got caught on this. What makes you believe that the fundamental > confusion is about mutability? The confusion is not about mutability. Its about mutability and parameter passing being non-orthogonal. > > (Also, if the change is applied everywhere, the language would not be > inconsistent.) Obviously that would depend on what the change is. From lie.1296 at gmail.com Sat Dec 24 23:12:27 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 25 Dec 2011 15:12:27 +1100 Subject: Early and late binding [was Re: what does 'a=b=c=[]' do] In-Reply-To: <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> <9ljqifFfj9U3@mid.individual.net> <4ef58c60$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/24/2011 07:25 PM, Steven D'Aprano wrote: >> I'd use a function attribute. >> >> def func(x, y=None): >> if y is None: >> y = func.default_y >> ... >> func.default_y = [] >> >> That's awkward only if you believe function attributes are awkward. > > I do. All you've done is move the default from *before* the function is > defined to *after* the function is defined, instead of keeping it in the > function definition. It's still separate, and if the function is renamed > your code stops working. In other words, it violates encapsulation of the > function. Although we can solve that (default being after the function is defined) using a simple decorator: def funcargs(**args): def __decorate_with_args(func): for k,v in args.items(): setattr(func, k, v) return func return __decorate_with_args Usage: @funcargs(foo=4) def bar(baz): return baz + bar.foo et voila, we had just reinvented early binding default argument, with a much uglier syntax. From ater1980 at gmail.com Sun Dec 25 01:43:36 2011 From: ater1980 at gmail.com (Alex Ter-Sarkissov) Date: Sun, 25 Dec 2011 19:43:36 +1300 Subject: installing matplotlib in MacOs 10.6.8. Message-ID: hi everyone, I run python 2.7.2. in Eclipse (recently upgraded from 2.6). I have a problem with installing matplotlib (I found the version for python 2.7. MacOs 10.3, no later versions). If I run python in terminal using arch -i386 python, and then from matplotlib.pylab import * and similar stuff, everything works fine. If I run python in eclipse or just without arch -i386, I can import matplotlib as from matplotlib import * but actually nothing gets imported. If I do it in the same way as above, I get the message no matching architecture in universal wrapper which means there's conflict of versions or something like that. I tried reinstalling the interpreter and adding matplotlib to forced built-ins, but nothing helped. For some reason I didn't have this problem with numpy and tkinter. Any suggestions are appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From zyzhu2000 at gmail.com Sun Dec 25 02:09:50 2011 From: zyzhu2000 at gmail.com (GZ) Date: Sat, 24 Dec 2011 23:09:50 -0800 (PST) Subject: Test None for an object that does not implement == Message-ID: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Hi, I run into a weird problem. I have a piece of code that looks like the following: f(...., a=None, c=None): assert (a==None)==(c==None) The problem is that == is not implemented sometimes for values in a and c, causing an exception NotImplementedError. I ended up doing assert (not a)==(not c), but I think this code has other issues, for example, when a=[] and c=['a'], the assertion will fail, although a is not None. So how do I reliably test if a value is None or not? Thanks, gz From no.email at nospam.invalid Sun Dec 25 02:28:00 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Sat, 24 Dec 2011 23:28:00 -0800 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: <7xaa6h6rgf.fsf@ruckus.brouhaha.com> GZ writes: > assert (a==None)==(c==None)... > So how do I reliably test if a value is None or not? Equality is the wrong comparison to start with. Use "a is None". From nobody at nowhere.com Sun Dec 25 04:38:07 2011 From: nobody at nowhere.com (Nobody) Date: Sun, 25 Dec 2011 09:38:07 +0000 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On Sat, 24 Dec 2011 23:09:50 -0800, GZ wrote: > I run into a weird problem. I have a piece of code that looks like the > following: > > f(...., a=None, c=None): > assert (a==None)==(c==None) > > > The problem is that == is not implemented sometimes for values in a > and c, causing an exception NotImplementedError. I have no idea how that can happen. If a.__eq__(None) returns NotImplemented, the interpreter should flip the test and perform the equivalent of None.__eq__(a), which will return False. > So how do I reliably test if a value is None or not? As Paul says, use "is" to check whether a value _is_ None. Checking for equality is almost certainly the wrong thing to do; nothing should compare equal to None except for None itself, so "x is None" and "x == None" shouldn't produce different results unless there's a bug in the comparison method. From lie.1296 at gmail.com Sun Dec 25 06:10:28 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 25 Dec 2011 22:10:28 +1100 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On 12/25/2011 08:38 PM, Nobody wrote: > nothing should compare equal to None except for None itself, so "x is None" > and "x == None" shouldn't produce different results unless there's a > bug in the comparison method. not necessarily, for example: import random class OddClass: def __eq__(self, other): return [True, False][random.randint(0, 1)] x = OddClass() print x == None print x == None print x == None print x == None print x == None Now, whether doing something like that is advisable or not, that's a different question; however nothing in python states that you couldn't have something that compare equal to None whether there is a bug or not in the comparison method. From mrinalini at edss.co.in Sun Dec 25 06:28:55 2011 From: mrinalini at edss.co.in (Mrinalini Kulkarni) Date: Sun, 25 Dec 2011 16:58:55 +0530 Subject: help - obtaining the type of the object using =?UTF-8?Q?tp=5Fname?= Message-ID: <440b1f62ce9f82adf5e6f17d9e520b5b@edss.co.in> Hello, I have embedded python into a vc++ app. I need to obtain type of the variable defined in the python script, in my c++ code. PyObject *pMyObject; -> assume points to a variable defined in the python script Now I want to do something like this const char * typeName; typeName = pMyObject->ob_type->tp_name Is this possible ? I tried doing this but it crashes. What is the alternative way for getting the name of the type from python. Is there a function available for this purpose such that I can obtain the address of that function using GetProcAddress and then use it for determining the type. (I am required to link to python dynamically) thanks, MK From steve+comp.lang.python at pearwood.info Sun Dec 25 07:10:40 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 12:10:40 GMT Subject: Adding an interface to existing classes References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> Message-ID: <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 17:24:14 -0800, Rick Johnson wrote: >> class DrawablePoint( geometry.Point ): >> ? ? class draw( self, ... ): >> ? ? ? ? ... > > "DrawablePoint"? I suppose there is an "ImaginaryPoint" somewhere in > this API? Geez No, but there's an abstract Point, which presumably refers to the geometric concept (hence the module, geometry) without concerning itself with such things as pixels, raster and vector output devices, screen resolutions, and all the other stuff which is needed for drawing points but not needed for working with points. [...] >> I see a problem with this, though. The intersection of two lines is >> (usually) an object of type Point. Since DrawableLine inherits from >> Line, > > Why the hell is "Drawable" inheriting from Line? I would think that a > "Line" would be "Drawable" object and NOT vice-versa? Am i wrong? Probably. I think there's a case for Drawable to be an abstract mixin class, so that DrawableLine inherits from both Line and Drawable. >> this means that unless I redefine the "intersect" method in >> DrawableLine, the intersection of two DrawableLines will be a Point >> object, not a DrawablePoint. Not if you define intersect in Point correctly in the first place. class Point: # An abstract class. def intersect(self, other): blah; blah; blah return Point(x, y) # No, wrong, bad!!! Don't do this. Instead: return self.__class__(x, y) # Better. > Spencer, i would re-think this entire project from the beginning. You > are trying to make an object out of everything. You don't need to make > an object of EVERYTHING. Very true. -- Steven From rosuav at gmail.com Sun Dec 25 07:32:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 25 Dec 2011 23:32:41 +1100 Subject: Adding an interface to existing classes In-Reply-To: <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 25, 2011 at 11:10 PM, Steven D'Aprano wrote: > class Point: ?# An abstract class. > ? ?def intersect(self, other): > ? ? ? ?blah; blah; blah > ? ? ? ?return Point(x, y) ?# No, wrong, bad!!! Don't do this. > > Instead: > > ? ? ? ?return self.__class__(x, y) ?# Better. This would work if you were dealing with the intersection of two points, but how do you use that sort of trick for different classes? ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 25 07:45:22 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 12:45:22 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 06:54:07 -0800, Eelco wrote: > Context dependence is not something to be avoided at all costs, but all > else being equal, less is certainly more. The general concept of > grouping thing together which parenthesis is an extremely pervasive one > in programming, and thus deserves its own set of context-dependent > rules. Packing and unpacking collections is far, far more rare, Not in Python, where it is a very common idiom. > and thus > a form that requires you to write more but remember less is certainly > relatively favorable. Not in Python, where iteration is a fundamental idiom and packing/ unpacking is a basic syntax construct precisely because the designer of the language expects it to be common and encourages its use. There are built-in functions designed to be used with unpacking operations, e.g. enumerate and zip: for i, obj in enumerate(sequence): ... for a, b in zip(obj, range(100)): ... -- Steven From yasar11732 at gmail.com Sun Dec 25 07:45:26 2011 From: yasar11732 at gmail.com (=?utf-8?B?WWHFn2FyIEFyYWJhY8Sx?=) Date: Sun, 25 Dec 2011 14:45:26 +0200 Subject: help - obtaining the type of the object using tp_name In-Reply-To: <440b1f62ce9f82adf5e6f17d9e520b5b@edss.co.in> References: <440b1f62ce9f82adf5e6f17d9e520b5b@edss.co.in> Message-ID: I am not sure if I understood your question correctly, but I would advice checking this: http://docs.python.org/c-api/object.html#PyObject_Type Sun, 25 Dec 2011 13:28:55 +0200 tarihinde Mrinalini Kulkarni ??yle yazm??: > Hello, > > I have embedded python into a vc++ app. I need to obtain type of the > variable defined in the python script, in my c++ code. > > PyObject *pMyObject; -> assume points to a variable defined in the > python script > > Now I want to do something like this > > const char * typeName; > > typeName = pMyObject->ob_type->tp_name > > Is this possible ? I tried doing this but it crashes. > > What is the alternative way for getting the name of the type from > python. Is there a function available for this purpose such that I can > obtain the address of that function using GetProcAddress and then use > it for determining the type. (I am required to link to python > dynamically) > > thanks, > MK -- Opera'n?n e-posta istemcisi ile g?nderildi: http://www.opera.com/mail From steve+comp.lang.python at pearwood.info Sun Dec 25 07:50:21 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 12:50:21 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> Message-ID: <4ef71c0d$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 06:45:01 -0800, Eelco wrote: > Can you give an example of a construct in python where two whitespace > delimited identifiers are legal? Not apart from the trivial case of two identifiers separated by newlines. What's your point? -- Steven From yasar11732 at gmail.com Sun Dec 25 07:53:55 2011 From: yasar11732 at gmail.com (=?ISO-8859-9?Q?Ya=FEar_Arabac=FD?=) Date: Sun, 25 Dec 2011 14:53:55 +0200 Subject: help - obtaining the type of the object using tp_name In-Reply-To: <440b1f62ce9f82adf5e6f17d9e520b5b@edss.co.in> References: <440b1f62ce9f82adf5e6f17d9e520b5b@edss.co.in> Message-ID: And by the way, I would advice asking these kinds of questions in #python-dev IRC channel (Freenode). I believe you can get much better help about your problems regarding C-api there. 2011/12/25 Mrinalini Kulkarni > Hello, > > I have embedded python into a vc++ app. I need to obtain type of the > variable defined in the python script, in my c++ code. > > PyObject *pMyObject; -> assume points to a variable defined in the python > script > > Now I want to do something like this > > const char * typeName; > > typeName = pMyObject->ob_type->tp_name > > Is this possible ? I tried doing this but it crashes. > > What is the alternative way for getting the name of the type from python. > Is there a function available for this purpose such that I can obtain the > address of that function using GetProcAddress and then use it for > determining the type. (I am required to link to python dynamically) > > thanks, > MK > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://yasar.serveblog.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sun Dec 25 08:12:41 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:12:41 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> Message-ID: <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 06:39:39 -0800, Eelco wrote: > On Dec 20, 4:30?am, alex23 wrote: >> On Dec 19, 8:15?pm, Eelco wrote: >> >> > What does that have to do with collection packing/unpacking? >> >> It's mocking your insistance that collection unpacking is a type >> constraint. > > This is really going to be the last time I waste any words on this: If only that were true, but after sending this post, you immediately followed up with FIVE more posts on this subject in less than half an hour. > The sentence 'collection unpacking is a type constraint' is entirely > nonsensical. How true. Given that you have now acknowledged this fact, can you please stop insisting that collection unpacking is a type constraint? > A type constraint is a linguistical construct that can be > applied in many ways; typically, to narrow down the semantics of use of > the symbol to which the type constraint is applied. Traceback (most recent call last): RuntimeError: maximum recursion depth exceeded > In case of python, collection PACKING (not unpacking) is signaled by a > construct that can be viewed as a type constraint. Only by doing sufficient violence to the concept of type constraint that it could mean *anything*. Earlier, I insisted that a constraint is a rule that applies to input, not output. I haven't seen anyone, including yourself, dispute that. Normally we would say that in the statement: y = list(x) there is a constraint on x, namely that it is some sort of iterable object (otherwise an exception will be raised), but it would be an abuse of language to say that there is a type constraint on y. y ends up being a list, true, but that isn't a constraint on y, it is an outcome. In normal usage, "constraint" refers to pre-conditions, not post- conditions. There are no pre-conditions on y in the above. It may not even exist. Contrast it with this example: y += list(x) where there are constraints on y: it must exist, and it must be a list, or something which can be added to a list. -- Steven From steve+comp.lang.python at pearwood.info Sun Dec 25 08:13:36 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:13:36 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> Message-ID: <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 06:47:21 -0800, Eelco wrote: > I would like to be able to write something like: > > a, middle::tuple, b = ::sequence > > Where I would like the extra :: before the sequence to explicitly signal > collection unpacking on the rhs, to maintain the symmetry with > collection unpacking within a function call. The :: on the right-hand side is redundant, because the left-hand side already explicitly signals collection unpacking of the RHS. Requiring :: on the RHS above is as unnecessary as it would be here: n = len(::sequence) -- Steven From steve+comp.lang.python at pearwood.info Sun Dec 25 08:16:00 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:16:00 GMT Subject: what does 'a=b=c=[]' do References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <4ef2704d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef72210$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sat, 24 Dec 2011 19:41:55 +0100, Thomas Rachel wrote: >> The only times you need the brackets around a tuple is to control the >> precedence of operations, or for an empty tuple. > > IBTD: > > a=((a, b) for a, b, c in some_iter) > b=[(1, c) for ] > > Without the round brackets, it is a syntax error. Correction noted. Nevertheless, the parentheses don't create the tuple, the comma operator does. -- Steven From roy at panix.com Sun Dec 25 08:17:28 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 08:17:28 -0500 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: In article , Lie Ryan wrote: > Now, whether doing something like that is advisable or not, that's a > different question; however nothing in python states that you couldn't > have something that compare equal to None whether there is a bug or not > in the comparison method. Just for fun, I tried playing around with subclassing NoneType and writing an __eq__ for my subclass. Turns out, you can't do that: Traceback (most recent call last): File "./none.py", line 5, in class Nihil(NoneType): TypeError: Error when calling the metaclass bases type 'NoneType' is not an acceptable base type From steve+comp.lang.python at pearwood.info Sun Dec 25 08:27:49 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:27:49 GMT Subject: Adding an interface to existing classes References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef724d4$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 23:32:41 +1100, Chris Angelico wrote: > On Sun, Dec 25, 2011 at 11:10 PM, Steven D'Aprano > wrote: >> class Point: ?# An abstract class. >> ? ?def intersect(self, other): >> ? ? ? ?blah; blah; blah >> ? ? ? ?return Point(x, y) ?# No, wrong, bad!!! Don't do this. >> >> Instead: >> >> ? ? ? ?return self.__class__(x, y) ?# Better. > > This would work if you were dealing with the intersection of two points, > but how do you use that sort of trick for different classes? There's nothing in the above that assumes that other has the same type as self. It's just that the type of other is ignored, and the type of self always wins. I find that a nice, clear rule: x.intersection(y) always returns a point with the same type as x. If you want a more complicated rule, you have to code it yourself: def intersection(self, other): if issubclass(type(other), type(self)): kind = type(other) elif issubclass(type(self), type(other)): kind = AbstractPoint elif other.__class__ is UserPoint: kind = UserPoint elif today is Tuesday: kind = BelgiumPoint else: kind = self.__class__ return kind(x, y) -- Steven From roy at panix.com Sun Dec 25 08:30:46 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 08:30:46 -0500 Subject: Random string of digits? Message-ID: I want to create a string of 20 random digits (I'm OK with leading zeros). The best I came up with is: ''.join(str(random.randint(0, 9)) for i in range(20)) Is there something better? From rosuav at gmail.com Sun Dec 25 08:35:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:35:46 +1100 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith wrote: > Just for fun, I tried playing around with subclassing NoneType and > writing an __eq__ for my subclass. ?Turns out, you can't do that: > > Traceback (most recent call last): > ?File "./none.py", line 5, in > ? ?class Nihil(NoneType): > TypeError: Error when calling the metaclass bases > ? ?type 'NoneType' is not an acceptable base type Yes; unfortunately quite a few Python built-in classes can't be subclassed. It's an unfortunate fact of implementation, I think, rather than a deliberate rule. But then, what would you ever need to subclass None for, other than toys and testing? ChrisA From rosuav at gmail.com Sun Dec 25 08:37:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:37:22 +1100 Subject: Adding an interface to existing classes In-Reply-To: <4ef724d4$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef724d4$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 12:27 AM, Steven D'Aprano wrote: > There's nothing in the above that assumes that other has the same type as > self. It's just that the type of other is ignored, and the type of self > always wins. I find that a nice, clear rule: x.intersection(y) always > returns a point with the same type as x. The intersection of DrawableLine and DrawableLine is DrawablePoint. That's not the same type as either of the inputs. Same if you seek the intersection of two planes, which is a line - or two spheres, which is a circle (with possible failure if they don't intersect). ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 25 08:46:15 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:46:15 GMT Subject: Adding an interface to existing classes References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef724d4$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef72927$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 00:37:22 +1100, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 12:27 AM, Steven D'Aprano > wrote: >> There's nothing in the above that assumes that other has the same type >> as self. It's just that the type of other is ignored, and the type of >> self always wins. I find that a nice, clear rule: x.intersection(y) >> always returns a point with the same type as x. > > The intersection of DrawableLine and DrawableLine is DrawablePoint. > That's not the same type as either of the inputs. Same if you seek the > intersection of two planes, which is a line - or two spheres, which is a > circle (with possible failure if they don't intersect). class Line: intersection_kind = Point def intersection(self, other): blah() return self.intersection_kind(a, b) class DrawableLine(Line): intersection_kind = DrawablePoint -- Steven From steve+comp.lang.python at pearwood.info Sun Dec 25 08:48:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:48:16 GMT Subject: Random string of digits? References: Message-ID: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 08:30:46 -0500, Roy Smith wrote: > I want to create a string of 20 random digits (I'm OK with leading > zeros). The best I came up with is: > > ''.join(str(random.randint(0, 9)) for i in range(20)) > > Is there something better? '%20d' % random.randint(0, 10**20-1) -- Steven From steve+comp.lang.python at pearwood.info Sun Dec 25 08:48:27 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 13:48:27 GMT Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: <4ef729aa$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 00:35:46 +1100, Chris Angelico wrote: [...] >> TypeError: Error when calling the metaclass bases >> ? ?type 'NoneType' is not an acceptable base type > > Yes; unfortunately quite a few Python built-in classes can't be > subclassed. I can't think of any other un-subclassable classes other than NoneType. Which ones are you thinking of? -- Steven From rosuav at gmail.com Sun Dec 25 08:50:44 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:50:44 +1100 Subject: Random string of digits? In-Reply-To: References: Message-ID: On Mon, Dec 26, 2011 at 12:30 AM, Roy Smith wrote: > I want to create a string of 20 random digits (I'm OK with leading > zeros). ?The best I came up with is: > > ''.join(str(random.randint(0, 9)) for i in range(20)) > > Is there something better? The simple option is: random.randint(0,99999999999999999999) or "%020d"%random.randint(0,99999999999999999999) (the latter gives you a string, padded with leading zeroes). But I'm assuming that you discarded that option due to lack of entropy (ie you can't trust randint() over that huge a range). The way I'd do it would be in chunks. The simple option is one chunk; your original technique is twenty. We can go somewhere in between. First thing to do though: ascertain how far randint() is properly random. The Python 2 docs [1] say that the underlying random() function uses 53-bit floats, so you can probably rely on about that much randomness; for argument's sake, let's say it's safe for up to 10,000 but no further (although 53 bits give you about 15 decimal digits). ''.join('%04d'%random.randint(0,9999) for i in range(5)) For your actual task, I'd be inclined to take ten digits, twice, and not bother with join(): '%010d%010d'%(random.randint(0,9999999999),random.randint(0,9999999999)) Looks a little ugly, but it works! And only two random number calls (which can be expensive). ChrisA From rosuav at gmail.com Sun Dec 25 08:51:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:51:41 +1100 Subject: Random string of digits? In-Reply-To: References: Message-ID: On Mon, Dec 26, 2011 at 12:50 AM, Chris Angelico wrote: > The Python 2 docs [1] Forgot to provide link. Python 2: http://docs.python.org/library/random.html And same in Python 3: http://docs.python.org/py3k/library/random.html ChrisA From rosuav at gmail.com Sun Dec 25 08:54:40 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:54:40 +1100 Subject: Random string of digits? In-Reply-To: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 12:48 AM, Steven D'Aprano wrote: > On Sun, 25 Dec 2011 08:30:46 -0500, Roy Smith wrote: > >> I want to create a string of 20 random digits (I'm OK with leading >> zeros). ?The best I came up with is: >> >> ''.join(str(random.randint(0, 9)) for i in range(20)) >> >> Is there something better? > > '%20d' % random.randint(0, 10**20-1) I should mention that this sort of thing is absolutely acceptable in situations where you don't actually need that level of randomness; if you want to assign 20-digit numbers to each of ten million objects, you can happily use something that's only really random to fifteen. I use this technique with base 36 numbers to generate arbitrary alphanumeric strings to use as message tags, for instance; fill out a specified field size, even though not every combination will actually be used. ChrisA From rosuav at gmail.com Sun Dec 25 08:55:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 00:55:22 +1100 Subject: Adding an interface to existing classes In-Reply-To: <4ef72927$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <8f345777-1ef7-46dc-b3fb-a0bea5ebf2c3@r16g2000prr.googlegroups.com> <22ea371a-f282-4d81-89f6-bc3229e2e8b1@a17g2000yqj.googlegroups.com> <4ef712c0$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef724d4$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef72927$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 12:46 AM, Steven D'Aprano wrote: > class DrawableLine(Line): > ? ?intersection_kind = DrawablePoint Ha! That works. I was sure there'd be some simple way to do it! ChrisA From rosuav at gmail.com Sun Dec 25 09:04:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 01:04:06 +1100 Subject: Test None for an object that does not implement == In-Reply-To: <4ef729aa$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> <4ef729aa$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 12:48 AM, Steven D'Aprano wrote: > I can't think of any other un-subclassable classes other than NoneType. > Which ones are you thinking of? I don't remember, but it was mentioned in a thread a little while ago. Experimentation shows that 'bool' is one of them, though. This may shed some light: >>> class Foo(type(iter)): pass Traceback (most recent call last): File "", line 1, in class Foo(type(iter)): TypeError: type 'builtin_function_or_method' is not an acceptable base type I think there are certain types that are actually not implemented as classes, and hence cannot be subclassed. This is almost certainly an implementation detail though; my testing was done in Py3.2 (on Windows fwiw). ChrisA From mohandayalank at gmail.com Sun Dec 25 09:10:09 2011 From: mohandayalank at gmail.com (D. Mohan M. Dayalan) Date: Sun, 25 Dec 2011 06:10:09 -0800 (PST) Subject: ckjdlkfnl,ndf,nfd,fndfnkdnk mmdlssdlndnll; k; as; lkds sjdsljdlskjdsl; kdslksdl; ddlk Message-ID: http;//123maza.com/48/moon670/ From roy at panix.com Sun Dec 25 09:13:09 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 09:13:09 -0500 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: In article , Chris Angelico wrote: > On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith wrote: > > Just for fun, I tried playing around with subclassing NoneType and > > writing an __eq__ for my subclass. ?Turns out, you can't do that: > > > > Traceback (most recent call last): > > ?File "./none.py", line 5, in > > ? ?class Nihil(NoneType): > > TypeError: Error when calling the metaclass bases > > ? ?type 'NoneType' is not an acceptable base type > > Yes; unfortunately quite a few Python built-in classes can't be > subclassed. It's an unfortunate fact of implementation, I think, > rather than a deliberate rule. > > But then, what would you ever need to subclass None for, other than > toys and testing? You might be to differentiate between temporary and permanent failures. Let's say you have a WidgetPool, containing Widgets of various classes. class WidgetPool: def get_widget(class_name): """Return a Widget of a given class. If there are no such Widgets available, returns None.""" [...] You might want to return a None subclass to signify, "No such Widgets are currently available, but they might be if you try again in a little while", as opposed to "No such Widgets will ever be available". If you were designing the interface from scratch, you would probably represent that with an exception hierarchy. However, if this was an old interface that you were modifying, this might be a way to return a richer failure indication for new clients without breaking backwards compatibility for existing code. Of course, the existing code would probably be using "is None" tests, and break anyway. But at least that's a plausible scenario for None subclasses. From roy at panix.com Sun Dec 25 09:21:34 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 09:21:34 -0500 Subject: Random string of digits? References: Message-ID: In article , Chris Angelico wrote: > "%020d"%random.randint(0,99999999999999999999) > (the latter gives you a string, padded with leading zeroes). But I'm > assuming that you discarded that option due to lack of entropy (ie you > can't trust randint() over that huge a range). Actually, the only entropy involved here is the ever increasing amount of it between my ears. It never occurred to me to try that :-) > For your actual task, I'd be inclined to take ten digits, twice, and > not bother with join(): > > '%010d%010d'%(random.randint(0,9999999999),random.randint(0,9999999999)) > > Looks a little ugly, but it works! And only two random number calls > (which can be expensive). Hmmm. In my case, I was looking more to optimize clarity of code, not speed. This is being used during account creation on a web site, so it doesn't get run very often. It turns out, I don't really need 20 digits. If I can count on >>> "%020d" % random.randint(0,999999999999999) to give me 15-ish digits, that's good enough for my needs and I'll probably go with that. Thanks. From rosuav at gmail.com Sun Dec 25 09:23:29 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 01:23:29 +1100 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 1:13 AM, Roy Smith wrote: > If you were designing the interface from scratch, you would probably > represent that with an exception hierarchy Or possibly with "returns a False value", giving the option of None for none available, False for none will ever be available. Of course, you then have to guarantee that your live return values will always boolify as True. ChrisA From hoogendoorn.eelco at gmail.com Sun Dec 25 09:45:01 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 25 Dec 2011 06:45:01 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> Message-ID: On Dec 25, 2:01?am, Rick Johnson wrote: > On Dec 24, 6:24?pm, alex23 wrote: > > > That you're a condescending douchebag with nothing of value to > > contribute? > > > Crystal. > > Take it from me Eelco. Once Alex drops into your thread and starts > name calling, it's over my friend. Yes, he has quite worn out my patience; whats over is our (attempts at) two sided communication, but I hope to continue the constructive lines of argument in this thread. From rosuav at gmail.com Sun Dec 25 09:51:30 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 01:51:30 +1100 Subject: Random string of digits? In-Reply-To: References: Message-ID: On Mon, Dec 26, 2011 at 1:21 AM, Roy Smith wrote: > It turns out, I don't really need 20 digits. ?If I can count on > >>>> "%020d" % random.randint(0,999999999999999) > > to give me 15-ish digits, that's good enough for my needs and I'll > probably go with that. ?Thanks. I'd say you can. The information about the Mersenne Twister underlying the module's functions isn't marked as "CPython Implementation Note", so I would expect that every Python implementation will provide a minimum of 53-bit precision. (It does have the feeling of an implementation detail though; is there a guarantee that two Pythons will generate the same sequence of numbers from the same seed?) ChrisA From hoogendoorn.eelco at gmail.com Sun Dec 25 09:55:28 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 25 Dec 2011 06:55:28 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> On Dec 25, 1:45?pm, Steven D'Aprano wrote: > On Sat, 24 Dec 2011 06:54:07 -0800, Eelco wrote: > > Context dependence is not something to be avoided at all costs, but all > > else being equal, less is certainly more. The general concept of > > grouping thing together which parenthesis is an extremely pervasive one > > in programming, and thus deserves its own set of context-dependent > > rules. Packing and unpacking collections is far, far more rare, > > Not in Python, where it is a very common idiom. I know we are talking about python; it was me that put that in the title, after all. I know python makes more use of this than some languages (and less than others; I wouldnt suggest such a verbose syntax for a functional language for instance). Anyway, braces are used at least an order of magnitude more than collection packing/ unpacking in typical code. From hoogendoorn.eelco at gmail.com Sun Dec 25 09:59:44 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 25 Dec 2011 06:59:44 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <4ef0217f$0$11091$c3e8da3@news.astraweb.com> <4ef71c0d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <77ed8395-54df-44a6-92c1-16cd7c8362da@z1g2000vbx.googlegroups.com> On Dec 25, 1:50?pm, Steven D'Aprano wrote: > On Sat, 24 Dec 2011 06:45:01 -0800, Eelco wrote: > > Can you give an example of a construct in python where two whitespace > > delimited identifiers are legal? > > Not apart from the trivial case of two identifiers separated by newlines. > > What's your point? My point is as I originally stated it: that this construct, of two identifiers seperated by non-newline whitespace, as in 'list tail' does not occur anywhere else in python, so introducing that syntax, while i suppose technically possible, would be a break with existing expectations. Normally speaking, if two identifiers interact, they are explicitly 'joined' by an infixed operator of some sort, as in 3*4, rather than * 3 4. That seems a sensible rule to me, and I see no compelling reason to depart from it. From arnodel at gmail.com Sun Dec 25 10:07:21 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Sun, 25 Dec 2011 15:07:21 +0000 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> Message-ID: On Dec 25, 2011 2:55 PM, "Eelco" wrote: > > On Dec 25, 2:01 am, Rick Johnson wrote: > > On Dec 24, 6:24 pm, alex23 wrote: > > > > > That you're a condescending douchebag with nothing of value to > > > contribute? > > > > > Crystal. > > > > Take it from me Eelco. Once Alex drops into your thread and starts > > name calling, it's over my friend. > > Yes, he has quite worn out my patience; whats over is our (attempts > at) two sided communication, but I hope to continue the constructive > lines of argument in this thread. The real kiss of death is when you find yourself on the same side of the "argument" as ranting Rick. -- Arnaud -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sun Dec 25 10:21:49 2011 From: __peter__ at web.de (Peter Otten) Date: Sun, 25 Dec 2011 16:21:49 +0100 Subject: Random string of digits? References: Message-ID: Chris Angelico wrote: > On Mon, Dec 26, 2011 at 12:30 AM, Roy Smith wrote: >> I want to create a string of 20 random digits (I'm OK with leading >> zeros). The best I came up with is: >> >> ''.join(str(random.randint(0, 9)) for i in range(20)) >> >> Is there something better? > > The simple option is: > random.randint(0,99999999999999999999) > or > "%020d"%random.randint(0,99999999999999999999) > (the latter gives you a string, padded with leading zeroes). But I'm > assuming that you discarded that option due to lack of entropy (ie you > can't trust randint() over that huge a range). > > The way I'd do it would be in chunks. The simple option is one chunk; > your original technique is twenty. We can go somewhere in between. > First thing to do though: ascertain how far randint() is properly > random. The Python 2 docs [1] say that the underlying random() > function uses 53-bit floats, so you can probably rely on about that > much randomness; for argument's sake, let's say it's safe for up to > 10,000 but no further (although 53 bits give you about 15 decimal > digits). > > ''.join('%04d'%random.randint(0,9999) for i in range(5)) > > For your actual task, I'd be inclined to take ten digits, twice, and > not bother with join(): > > '%010d%010d'%(random.randint(0,9999999999),random.randint(0,9999999999)) > > Looks a little ugly, but it works! And only two random number calls > (which can be expensive). Judging from a quick look into the code (method Random._randbelow()) I'd say you don't need to do that unless you override Random.random() and not Random.getrandbits(). Even if you roll your own random() you'll get a warning when you run into the limit: >>> import random >>> random.randrange(10**20) 27709407700486201379L >>> class R(random.Random): ... def random(self): return 4 # * ... >>> R().randrange(10**20) /usr/lib/python2.6/random.py:253: UserWarning: Underlying random() generator does not supply enough bits to choose from a population range this large _warn("Underlying random() generator does not supply \n" 400000000000000000000L (*) According to the literature 4 is the best random number, 9 being the runner-up: http://www.googlefight.com/index.php?lang=en_GB&word1=random+number+dilbert&word2=random+number+xkcd A quick sanity check: >>> from collections import Counter >>> import random >>> Counter(str(random.randrange(10**10000))) Counter({'9': 1060, '6': 1039, '3': 1036, '8': 1007, '7': 997, '4': 977, '1': 976, '5': 976, '2': 970, '0': 962}) From steve+comp.lang.python at pearwood.info Sun Dec 25 10:27:37 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 15:27:37 GMT Subject: Random string of digits? References: Message-ID: <4ef740e8$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 01:51:30 +1100, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 1:21 AM, Roy Smith wrote: >> It turns out, I don't really need 20 digits. ?If I can count on >> >>>>> "%020d" % random.randint(0,999999999999999) >> >> to give me 15-ish digits, that's good enough for my needs and I'll >> probably go with that. ?Thanks. > > I'd say you can. The information about the Mersenne Twister underlying > the module's functions isn't marked as "CPython Implementation Note", so > I would expect that every Python implementation will provide a minimum > of 53-bit precision. (It does have the feeling of an implementation > detail though; is there a guarantee that two Pythons will generate the > same sequence of numbers from the same seed?) Yes. http://docs.python.org/dev/library/random.html#notes-on-reproducibility I think that's a new guarantee. Previously, the answer was only yes-ish: in theory, no guarantee was made, but in practice, you could normally rely on it. For example, when the Mersenne Twister became the default random number generator, the old generator, Wichman-Hill, was moved into its own module whrandom (deprecated in 2.4; now gone) for those who needed backwards compatibility. -- Steven From y.turgut at gmail.com Sun Dec 25 10:33:23 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Sun, 25 Dec 2011 07:33:23 -0800 (PST) Subject: Plot seems weird Message-ID: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> Hi all, I have a text file as following; 0.200047 0.000000 0.200053 0.160000 0.200059 0.000000 0.200065 0.080000 0.200072 0.000000 0.200078 0.160000 And I am trying to plot it with ; filenames = sys.argv[1:] if len(filenames) == 0: filenames = [sys.stdin] for filename in filenames: t,y1 = numpy.genfromtxt(filename, unpack=True) pyplot.plot(t,y1) pyplot.show() But graph seems weird, not as it supposed to be. Any ideas ? From hoogendoorn.eelco at gmail.com Sun Dec 25 10:38:17 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 25 Dec 2011 07:38:17 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> On Dec 25, 2:12?pm, Steven D'Aprano wrote: > On Sat, 24 Dec 2011 06:39:39 -0800, Eelco wrote: > > On Dec 20, 4:30?am, alex23 wrote: > >> On Dec 19, 8:15?pm, Eelco wrote: > > >> > What does that have to do with collection packing/unpacking? > > >> It's mocking your insistance that collection unpacking is a type > >> constraint. > > > This is really going to be the last time I waste any words on this: > > If only that were true, but after sending this post, you immediately > followed up with FIVE more posts on this subject in less than half an > hour. Did I waste any more words on collection packing and type constraints? No, I did not. (though I am about to, and am willing to do so for every question that seems genuinely aimed at engaging me on the matter) Did I intend to say that I was going to let a single troll shut down my entire topic? No, I did not. > > In case of python, collection PACKING (not unpacking) is signaled by a > > construct that can be viewed as a type constraint. > > Only by doing sufficient violence to the concept of type constraint that > it could mean *anything*. > > Earlier, I insisted that a constraint is a rule that applies to input, > not output. I haven't seen anyone, including yourself, dispute that. > > Normally we would say that in the statement: > > ? ? y = list(x) > > there is a constraint on x, namely that it is some sort of iterable > object (otherwise an exception will be raised), but it would be an abuse > of language to say that there is a type constraint on y. y ends up being > a list, true, but that isn't a constraint on y, it is an outcome. > > In normal usage, "constraint" refers to pre-conditions, not post- > conditions. There are no pre-conditions on y in the above. It may not > even exist. Contrast it with this example: > > ? ? y += list(x) > > where there are constraints on y: it must exist, and it must be a list, > or something which can be added to a list. Yes, indeed it would be abuse of language to call this a type constraint, since the fact that y is a list is indeed an outcome of whatever happens to pop out at the right hand side. One could redefine the identifier list to return any kind of object. How is 'head, *tail = sequence' or semantically entirely equivalently, 'head, tail::list = sequence' any different then? Of course after interpretation/compilation, what it boils down to is that we are constructing a list and binding it to the identifier tail, but that is not how it is formulated in python as a language (all talk of types is meaningless after compilation; machine code is untyped). We dont have something of the form 'tail = list_tail(sequence)'. Rather, we annotate the identifier 'tail' with an attribute that unquestionably destinates it to become a list*. It is no longer that 'tail' will just take anything that pops out of the expression on the right hand side; rather, the semantics of what will go on at right hand side is coerced by the constraint placed on 'tail'. But again, if you dont wish to view this as a type constraint, I wont lose any sleep over that. In that case this line of argument was simply never directed at you. It was directed at people who would reasonably argue that 'tail::tuple is a type constraint and thats unpythonic / type constraints have been considered and rejected'. If you dont think it looks like a type constraint: fine. The simpler argument is that whatever it is, its just a more verbose and flexible variant of a construct that python already has. *(I call that a 'type constraint', because that is what it literally is; if you can make a case that this term has acquired a different meaning in practice, and that there is another term in common use for this kind of construct; please enlighten me. Until that time, im going to ask you to take 'type constraint' by its literal meaning; a coercion of the type of a symbol, rather than whatever particular meaning it has acquired for you (it might help if you explained that). Im not sure if it was you that brought that up, but let me reiterate that I dont mean a 'type cast', which is a runtime concept. A 'type constraint' is purely a linguistic construct that will be 'compiled out') From steve+comp.lang.python at pearwood.info Sun Dec 25 10:46:58 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 15:46:58 GMT Subject: Random string of digits? References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 00:54:40 +1100, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 12:48 AM, Steven D'Aprano > wrote: >> On Sun, 25 Dec 2011 08:30:46 -0500, Roy Smith wrote: >> >>> I want to create a string of 20 random digits (I'm OK with leading >>> zeros). ?The best I came up with is: >>> >>> ''.join(str(random.randint(0, 9)) for i in range(20)) >>> >>> Is there something better? >> >> '%20d' % random.randint(0, 10**20-1) > > I should mention that this sort of thing is absolutely acceptable in > situations where you don't actually need that level of randomness; Use the Source, Luke, er, Chris :) If I've read the source correctly, randint() will generate sufficient bits of randomness to ensure that the entire int is random. http://hg.python.org/cpython/file/default/Lib/random.py -- Steven From hoogendoorn.eelco at gmail.com Sun Dec 25 10:47:20 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Sun, 25 Dec 2011 07:47:20 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 25, 2:13?pm, Steven D'Aprano wrote: > On Sat, 24 Dec 2011 06:47:21 -0800, Eelco wrote: > > I would like to be able to write something like: > > > a, middle::tuple, b = ::sequence > > > Where I would like the extra :: before the sequence to explicitly signal > > collection unpacking on the rhs, to maintain the symmetry with > > collection unpacking within a function call. > > The :: on the right-hand side is redundant, because the left-hand side > already explicitly signals collection unpacking of the RHS. Requiring :: > on the RHS above is as unnecessary as it would be here: Yes, it is redundant; hence the word 'extra' in my post. Explicit and implicit are not well-defined terms, but I would say that at the moment the signal is implicit, in the sense that one cannot see what is going on by considering the rhs in isolation. Normally in python, an assignment just binds the rhs to the identifiers on the lhs, but in case of collection (un)packing, this rule that holds almost all of the time is broken, and the assignment statement implies a far more complicated construct, with a far more subtle meaning, and non-constant time complexity. Thats not a terrible thing, but a little extra explicitness there would not hurt, and like I argued many times before, it is a nice unification with the situation where the unpacking can not be implicit, like inside a function call rather than assignment. > ? ? n = len(::sequence) Now you are just discrediting yourself in terms of having any idea what you are talking about. From rosuav at gmail.com Sun Dec 25 11:11:56 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 03:11:56 +1100 Subject: Random string of digits? In-Reply-To: <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 2:46 AM, Steven D'Aprano wrote: > Use the Source, Luke, er, Chris :) > > If I've read the source correctly, randint() will generate sufficient > bits of randomness to ensure that the entire int is random. > > http://hg.python.org/cpython/file/default/Lib/random.py I prefer not to rely on the source. That tells me what happens, not what's guaranteed to happen. However... bit of poking around can't hurt. That file doesn't actually justify anything, because random.Random() does not define getrandbits() - that, it seems, comes from _random(); turns out that getrandbits is actually doing pretty much the same thing I suggested: http://hg.python.org/cpython/file/745f9fd9856d/Modules/_randommodule.c#l371 Need a 64-bit random number? Take two 32-bit numbers and concatenate. So, it's going to be easier and clearer to just take the simple option, since it's actually doing the same thing underneath anyway. ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 25 11:15:25 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 16:15:25 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> Message-ID: <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 06:55:28 -0800, Eelco wrote: > Anyway, braces are used at > least an order of magnitude more than collection packing/ unpacking in > typical code. That's a wild and unjustified claim. Here's a quick and dirty test, using the standard library as an example of typical idiomatic code: [steve at orac ~]$ cd /usr/lib/python2.6 [steve at orac python2.6]$ grep "[*]args" *.py | wc -l 270 [steve at orac python2.6]$ grep "{" *.py | wc -l 550 Doesn't look like a factor of 10 difference to me. And from one of my projects: [steve at orac src]$ grep "[*]args" *.py | wc -l 267 [steve at orac src]$ grep "{" *.py | wc -l 8 -- Steven From rosuav at gmail.com Sun Dec 25 11:23:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 03:23:06 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 2:38 AM, Eelco wrote: > Until that time, im going > to ask you to take 'type constraint' by its literal meaning; a > coercion of the type of a symbol, rather than whatever particular > meaning it has acquired for you (it might help if you explained that). > Im not sure if it was you that brought that up, but let me reiterate > that I dont mean a 'type cast', which is a runtime concept. A 'type > constraint' is purely a linguistic construct that will be 'compiled > out') The dictionary definition of constraint is "a limitation or restriction", and you're right that it can be "compiled out". In fact, that is probably the best definition. Assuming everything is written correctly, you should be able to eliminate all constraints and the code will still function correctly*; but having the constrains means that certain illegal operations will throw errors. Here's two examples of tuple unpacking, one with a type constraint, the other without: a, b = ('hello', [1,2,3] ) a, b::list = ('hello', [1,2,3] ) The constraint on the second line means that, if the second element is not a list, the interpreter should throw an error. It does NOT mean to completely change the meaning of the statement to _make_ the last argument into a list. That is not the job of a constraint. ChrisA * In databasing, it's not uncommon to have code depend on error responses for correct operation; for instance, one implementation of UPSERT is to attempt an INSERT, and if it fails due to a unique key constraint, do the UPDATE instead. The same is also done in Python - eg using an exception to terminate a loop - but in the context of this discussion, assume that errors indicate errors. From steve+comp.lang.python at pearwood.info Sun Dec 25 12:05:39 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 17:05:39 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> Message-ID: <4ef757e3$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 07:38:17 -0800, Eelco wrote: > On Dec 25, 2:12?pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Sat, 24 Dec 2011 06:39:39 -0800, Eelco wrote: >> > On Dec 20, 4:30?am, alex23 wrote: >> >> On Dec 19, 8:15?pm, Eelco wrote: >> >> >> > What does that have to do with collection packing/unpacking? >> >> >> It's mocking your insistance that collection unpacking is a type >> >> constraint. >> >> > This is really going to be the last time I waste any words on this: >> >> If only that were true, but after sending this post, you immediately >> followed up with FIVE more posts on this subject in less than half an >> hour. > > Did I waste any more words on collection packing and type constraints? > No, I did not. (though I am about to, and am willing to do so for every > question that seems genuinely aimed at engaging me on the matter) > > Did I intend to say that I was going to let a single troll shut down my > entire topic? No, I did not. Ah, well whatever you *intended* wasn't clear from your comment. At least not clear to *me*. [...] > Yes, indeed it would be abuse of language to call this a type > constraint, since the fact that y is a list is indeed an outcome of > whatever happens to pop out at the right hand side. One could redefine > the identifier list to return any kind of object. So far, we agree on this. > How is 'head, *tail = sequence' or semantically entirely equivalently, > 'head, tail::list = sequence' any different then? Of course after > interpretation/compilation, what it boils down to is that we are > constructing a list and binding it to the identifier tail, but that is > not how it is formulated in python as a language I'm afraid it is. Here's the definition of assignment in Python 3: http://docs.python.org/py3k/reference/simple_stmts.html#assignment- statements > (all talk of types is > meaningless after compilation; machine code is untyped). What does machine code have to do with Python? > We dont have > something of the form 'tail = list_tail(sequence)'. I'm afraid we do. See the definition of assignment again. > Rather, we annotate > the identifier 'tail' with an attribute that unquestionably destinates > it to become a list*. It is no longer that 'tail' will just take > anything that pops out of the expression on the right hand side; Of course it will. Python is a dynamically typed language. It doesn't suddenly develop static types to ensure that 'tail' becomes a list; 'tail' is bound to a list because that's what the assignment statement provides. > rather, > the semantics of what will go on at right hand side is coerced by the > constraint placed on 'tail'. But it isn't a constraint placed on 'tail'. It is a consequence of the definition of assignment in Python 3. 'tail' becomes bound to a list because that is what the assignment statement is defined to do in that circumstance, not because the identifier (symbol) 'tail' is constrained to only accept lists. 'tail' may not even exist before hand, so talking about constraints on 'tail' is an abuse of language, AS YOU AGREED ABOVE. [...] > *(I call that a 'type constraint', because that is what it literally is; No. It is literally a name binding of a dynamically typed, unconstrained name to an object which happens to be a list. > if you can make a case that this term has acquired a different meaning > in practice, and that there is another term in common use for this kind > of construct; please enlighten me. Until that time, im going to ask you > to take 'type constraint' by its literal meaning; a coercion of the type > of a symbol, But THERE IS NO COERCION OF THE TYPE OF THE SYMBOL. I am sorry for shouting, but you seem oblivious to the simple fact that Python is not statically typed, and the symbol 'tail' is NOT coerced to a specific type. 'tail' may not even exist before the assignment is made; if it does exist, it could be *any type at all* -- and after the assignment takes place, there are no restrictions on subsequent assignments. 'head, *tail = sequence' is no more a type constraint than 'x = 1' is. Whatever the virtues of your proposal, you are doing it incalculable harm by your insistence on this incorrect model of Python's behaviour. -- Steven From rantingrickjohnson at gmail.com Sun Dec 25 12:06:57 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 25 Dec 2011 09:06:57 -0800 (PST) Subject: Plot seems weird References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> Message-ID: On Dec 25, 9:33?am, Yigit Turgut wrote: > Hi all, > > I have a text file as following; > > 0.200047 ? ? ? ?0.000000 > 0.200053 ? ? ? ?0.160000 > 0.200059 ? ? ? ?0.000000 > 0.200065 ? ? ? ?0.080000 > 0.200072 ? ? ? ?0.000000 > 0.200078 ? ? ? ?0.160000 > > And I am trying to plot it with ; > > filenames = sys.argv[1:] > if len(filenames) == 0: > ? ? filenames = [sys.stdin] > for filename in filenames: > ? ? t,y1 = numpy.genfromtxt(filename, unpack=True) > ? ? pyplot.plot(t,y1) > ? ? pyplot.show() > > But graph seems weird, not as it supposed to be. Any ideas ? Interesting. Of course "weird" leaves a LOT to be desired. On a scale of 1-10, how "weird" is the result? But seriously. Have you tried debugging yet? If not, test these points: * What is the value of "filenames" BEFORE the loop? * What is the value of "t" and "y1" for each iteration? Also observe this wonderful phenomenon: py> [] or [1,2,3] [1, 2, 3] py> [] or None or '' or () or {} or [1,2,3] or "1,2,3" [1, 2, 3] From lie.1296 at gmail.com Sun Dec 25 12:13:53 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 26 Dec 2011 04:13:53 +1100 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On 12/26/2011 01:13 AM, Roy Smith wrote: > In article, > Chris Angelico wrote: > >> On Mon, Dec 26, 2011 at 12:17 AM, Roy Smith wrote: >>> Just for fun, I tried playing around with subclassing NoneType and >>> writing an __eq__ for my subclass. Turns out, you can't do that: >>> >>> Traceback (most recent call last): >>> File "./none.py", line 5, in >>> class Nihil(NoneType): >>> TypeError: Error when calling the metaclass bases >>> type 'NoneType' is not an acceptable base type >> >> Yes; unfortunately quite a few Python built-in classes can't be >> subclassed. It's an unfortunate fact of implementation, I think, >> rather than a deliberate rule. >> >> But then, what would you ever need to subclass None for, other than >> toys and testing? > > You might be to differentiate between temporary and permanent failures. > Let's say you have a WidgetPool, containing Widgets of various classes. > > class WidgetPool: > def get_widget(class_name): > """Return a Widget of a given class. If there are no such > Widgets available, returns None.""" > [...] > > You might want to return a None subclass to signify, "No such Widgets > are currently available, but they might be if you try again in a little > while", as opposed to "No such Widgets will ever be available". > > If you were designing the interface from scratch, you would probably > represent that with an exception hierarchy. However, if this was an old > interface that you were modifying, this might be a way to return a > richer failure indication for new clients without breaking backwards > compatibility for existing code. > > Of course, the existing code would probably be using "is None" tests, > and break anyway. But at least that's a plausible scenario for None > subclasses. That scenario doesn't actually need subclassing if you duck type. From steve+comp.lang.python at pearwood.info Sun Dec 25 12:18:53 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 25 Dec 2011 17:18:53 GMT Subject: Random string of digits? References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 03:11:56 +1100, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 2:46 AM, Steven D'Aprano > wrote: >> Use the Source, Luke, er, Chris :) >> >> If I've read the source correctly, randint() will generate sufficient >> bits of randomness to ensure that the entire int is random. >> >> http://hg.python.org/cpython/file/default/Lib/random.py > > I prefer not to rely on the source. That tells me what happens, not > what's guaranteed to happen. In this case, the source explicitly tells you that the API includes support for arbitrary large ranges if you include a getrandbits() method: Optionally, implement a getrandbits() method so that randrange() can cover arbitrarily large ranges. I call that a pretty strong guarantee. > However... bit of poking around can't hurt. > That file doesn't actually justify anything, because random.Random() > does not define getrandbits() - that, it seems, comes from _random(); > turns out that getrandbits is actually doing pretty much the same thing > I suggested: > > http://hg.python.org/cpython/file/745f9fd9856d/Modules/ _randommodule.c#l371 > > Need a 64-bit random number? Take two 32-bit numbers and concatenate. > So, it's going to be easier and clearer to just take the simple option, > since it's actually doing the same thing underneath anyway. Um, I'm not sure what you consider "the simple option" in this context. I would hope you mean to use the high level API of randint: # need a random number with exactly 20 decimal digits random.randint(10**20, 10**21-1) rather than manually assembling a 20 digit number from smaller pieces. -- Steven From storchaka at gmail.com Sun Dec 25 12:32:27 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Sun, 25 Dec 2011 19:32:27 +0200 Subject: Random string of digits? In-Reply-To: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: 25.12.11 15:48, Steven D'Aprano ???????(??): > On Sun, 25 Dec 2011 08:30:46 -0500, Roy Smith wrote: >> I want to create a string of 20 random digits (I'm OK with leading >> zeros). The best I came up with is: >> ''.join(str(random.randint(0, 9)) for i in range(20)) >> Is there something better? > '%20d' % random.randint(0, 10**20-1) '%020d' % random.randrange(10**20) From roy at panix.com Sun Dec 25 12:41:29 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 12:41:29 -0500 Subject: Random string of digits? References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, 26 Dec 2011 03:11:56 +1100, Chris Angelico wrote: > > I prefer not to rely on the source. That tells me what happens, not > > what's guaranteed to happen. Steven D'Aprano wrote: > In this case, the source explicitly tells you that the API includes > support for arbitrary large ranges if you include a getrandbits() method: > > Optionally, implement a getrandbits() method so that randrange() > can cover arbitrarily large ranges. > > I call that a pretty strong guarantee. I think you mis-understood Chris's point. The documentation is the specification of how something behaves. If the documentation doesn't say it, you can't rely on it. The user should never have to read the source to know how to use a function, or what they can depend on. Now, I'm not saying that reading the source isn't useful for a deeper understanding, but it should be understood that any insights you glean from doing that are strictly implementation details. If you're saying that there are guarantees made by the implementation of getrandbits() which are not documented, then one of two things are true: 1) It is intended that users can depend on that behavior, in which case it's a bug in the docs, and the docs should be updated. or 2) It is not intended that users can depend on that behavior, in which case they would be foolish to do so. From rantingrickjohnson at gmail.com Sun Dec 25 12:44:33 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Sun, 25 Dec 2011 09:44:33 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> On Dec 19, 9:51?pm, Raymond Hettinger wrote: > Do you use IDLE when teaching Python? > If not, what is the tool of choice? I believe IDLE has the potential to be a very useful teaching tool and even in it's current abysmal state, i find it to be quite useful. > Students may not be experienced with the command-line and may be > running Windows, Linux, or Macs. ?Ideally, the tool or IDE will be > easy to install and configure (startup directory, path, associated > with a particular version of Python etc). Why install an IDE when IDLE is already there? Oh, yes, IDLE SUCKS. I know that already. But this revelation begs the question... Why has this community allowed IDLE to rot? Why has guido NOT started a public discussion on the matter? > Though an Emacs user myself, I've been teaching with IDLE because it's > free; it runs on multiple OSes, it has tooltips and code colorization > and easy indent/dedent/comment/uncomment commands, it has tab > completion; it allows easy editing at the interactive prompt; it has > an easy run-script command (F5); it has direct access to source code > (File OpenModule) and a class browser (Cntl+B). Yes, IDLE has all the basic tools anyone would need. Some people complain about a debugger, but i never use a debugger anyway. I feel debuggers just wreaken your debugging skills. > On the downside, some python distros aren't built with the requisite > Tcl/Tk support; And who's fault is that? > some distros like the Mac OS ship with a broken Tcl/Tk > so users have to install a fix to that as well; and IDLE sometimes > just freezes for no reason. And who's fault is that? > ?[IDLE] also doesn't have an easy way to > specify the startup directory. Are you kidding me? That could be fixed so easily! > If your goal is to quickly get new users up and running in Python, > what IDE or editor do you recommend? IDLE, of course. But NOT in its current state. Why would myself (or anyone) go to the trouble of downloading third party IDEs when IDLE is just waiting there for us to use? I for one, like to use tools that have open source code. And what is a better Python IDE than a Python IDE written in PYTHON? I ask ya? Also, what is the purpose of this thread Raymond? Are you (and others) considering removing IDLE from the source distro? You know. Many folks in this community have known for a long time how much i love IDLE, but at the same time how much i loath it's atrocious code base. I also know for a fact, that many "movers and shakers" within this community simultaneously use IDLE, and want to see IDLE code improved. However. None of these fine folks have taken the time to contact me privately so we can discuss such an evolution. Why is that? It boggles the mind really. From joshua.landau.ws at gmail.com Sun Dec 25 12:45:09 2011 From: joshua.landau.ws at gmail.com (Joshua Landau) Date: Sun, 25 Dec 2011 17:45:09 +0000 Subject: Random string of digits? In-Reply-To: References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 25 December 2011 17:32, Serhiy Storchaka wrote: > 25.12.11 15:48, Steven D'Aprano ???????(??): > > On Sun, 25 Dec 2011 08:30:46 -0500, Roy Smith wrote: >> >>> I want to create a string of 20 random digits (I'm OK with leading >>> zeros). The best I came up with is: >>> ''.join(str(random.randint(0, 9)) for i in range(20)) >>> Is there something better? >>> >> '%20d' % random.randint(0, 10**20-1) >> > > '%020d' % random.randrange(10**20) Do the docs not advise .format now? "{:20}".format(random.randrange(10**20)) -------------- next part -------------- An HTML attachment was scrubbed... URL: From y.turgut at gmail.com Sun Dec 25 13:27:20 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Sun, 25 Dec 2011 10:27:20 -0800 (PST) Subject: Plot seems weird References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> Message-ID: <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> On Dec 25, 7:06?pm, Rick Johnson wrote: > On Dec 25, 9:33?am, Yigit Turgut wrote: > > Hi all, > > > I have a text file as following; > > > 0.200047 ? ? ? ?0.000000 > > 0.200053 ? ? ? ?0.160000 > > 0.200059 ? ? ? ?0.000000 > > 0.200065 ? ? ? ?0.080000 > > 0.200072 ? ? ? ?0.000000 > > 0.200078 ? ? ? ?0.160000 > > > And I am trying to plot it with ; > > > filenames = sys.argv[1:] > > if len(filenames) == 0: > > ? ? filenames = [sys.stdin] > > for filename in filenames: > > ? ? t,y1 = numpy.genfromtxt(filename, unpack=True) > > ? ? pyplot.plot(t,y1) > > ? ? pyplot.show() > > > But graph seems weird, not as it supposed to be. Any ideas ? > > Interesting. Of course "weird" leaves a LOT to be desired. On a scale > of 1-10, how "weird" is the result? I apply a 1Khz test signal just to see if things run smoothly, but I see spikes at lower and higher ends (logic 0,1) where I should see a clean rectangle pwm signal. By the look of it I say weirdness is around 3/10. > > But seriously. Have you tried debugging yet? If not, test these > points: Yes I double checked it, there seems to be nothing wrong in debug. > ?* What is the value of "filenames" BEFORE the loop? Filename is argument 1 of the startup action. > ?* What is the value of "t" and "y1" for each iteration? I test with both low and mid frequency signals (50Hz - 1Khz), same inconsistency. > > Also observe this wonderful phenomenon: > > py> [] or [1,2,3] > [1, 2, 3] > py> [] or None or '' or () or {} or [1,2,3] or "1,2,3" > [1, 2, 3] Beautiful. I convert my arrays to string before writing to file. Original post contains a fragment of the whole file. Data is fluctuating, not a linear behavior. From dihedral88888 at googlemail.com Sun Dec 25 14:50:03 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 25 Dec 2011 11:50:03 -0800 (PST) Subject: Random string of digits? In-Reply-To: References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20158346.705.1324842603361.JavaMail.geo-discussion-forums@prfi34> Roy Smith? 2011?12?26????UTC+8??1?41?29???? > On Mon, 26 Dec 2011 03:11:56 +1100, Chris Angelico wrote: > > > I prefer not to rely on the source. That tells me what happens, not > > > what's guaranteed to happen. > > Steven D'Aprano wrote: > > In this case, the source explicitly tells you that the API includes > > support for arbitrary large ranges if you include a getrandbits() method: > > > > Optionally, implement a getrandbits() method so that randrange() > > can cover arbitrarily large ranges. > > > > I call that a pretty strong guarantee. > > I think you mis-understood Chris's point. The documentation is the > specification of how something behaves. If the documentation doesn't > say it, you can't rely on it. The user should never have to read the > source to know how to use a function, or what they can depend on. Now, > I'm not saying that reading the source isn't useful for a deeper > understanding, but it should be understood that any insights you glean > from doing that are strictly implementation details. > > If you're saying that there are guarantees made by the implementation of > getrandbits() which are not documented, then one of two things are true: > > 1) It is intended that users can depend on that behavior, in which case > it's a bug in the docs, and the docs should be updated. > > or > > 2) It is not intended that users can depend on that behavior, in which > case they would be foolish to do so. Random bit generations for RSA2048 encoding and cryptography applications in python is simple and elegant. From dihedral88888 at googlemail.com Sun Dec 25 14:58:28 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Sun, 25 Dec 2011 11:58:28 -0800 (PST) Subject: RSA and Cryptography in PYTHON Message-ID: <31261167.63.1324843108857.JavaMail.geo-discussion-forums@prdv33> Long integer is really excellent in Python. Encoding RSA 2048bits in a simple and elegant way in Python is almost trivial. How about the nontrivial decoding part ? From orgnut at yahoo.com Sun Dec 25 18:45:10 2011 From: orgnut at yahoo.com (Larry Hudson) Date: Sun, 25 Dec 2011 15:45:10 -0800 Subject: Test None for an object that does not implement == In-Reply-To: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On 12/24/2011 11:09 PM, GZ wrote: > Hi, > > I run into a weird problem. I have a piece of code that looks like the > following: > > f(...., a=None, c=None): > assert (a==None)==(c==None) > <...> At first glance this looked like it should be a simple boolean "and", but then I realized that when a and c are both unequal to None, the result would also be True. This implies the logical approach would be exclusive-or (^). Try this expression: not ((a==None) ^ (c==None)) OTOH, if what you really want is simply to check that both are None (my first impression), then it's simply: (a==None) and (c==None) Most of the replies you're getting here seem unnecessarily complicated. > Thanks, > gz -=- Larry -=- From jeanpierreda at gmail.com Sun Dec 25 19:06:50 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sun, 25 Dec 2011 19:06:50 -0500 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: > At first glance this looked like it should be a simple boolean "and", but > then I realized that when a and c are both unequal to None, the result would > also be True. This implies the logical approach would be exclusive-or (^). Among booleans, "!=" is exclusive or and "==" is its negation. I don't see the point of complicating the formula to use ^. (related: <= is implication. Which is sad, because the arrow points the wrong way!) The issue here is that "== None" is being used instead of "is None", but I believe that's been covered. Your response doesn't include it, so maybe it's worth restating. -- Devin On Sun, Dec 25, 2011 at 6:45 PM, Larry Hudson wrote: > On 12/24/2011 11:09 PM, GZ wrote: >> >> Hi, >> >> I run into a weird problem. I have a piece of code that looks like the >> following: >> >> f(...., a=None, c=None): >> ? ? assert ?(a==None)==(c==None) >> > <...> > > At first glance this looked like it should be a simple boolean "and", but > then I realized that when a and c are both unequal to None, the result would > also be True. ?This implies the logical approach would be exclusive-or (^). > ?Try this expression: > > ? ? not ((a==None) ^ (c==None)) > > OTOH, if what you really want is simply to check that both are None (my > first impression), then it's simply: > > ? ? (a==None) and (c==None) > > Most of the replies you're getting here seem unnecessarily complicated. > >> Thanks, >> gz > > > ? ?-=- Larry -=- > -- > http://mail.python.org/mailman/listinfo/python-list From roy at panix.com Sun Dec 25 19:27:35 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 19:27:35 -0500 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: In article , Devin Jeanpierre wrote: > The issue here is that "== None" is being used instead of "is None", > but I believe that's been covered. Your response doesn't include it, > so maybe it's worth restating. Which of course leads to a SillyPEP for a new keyword, "are", which would allow you to write: >>> a and c are None instead of the much more verbose >>> a is None and c is None From lists at cheimes.de Sun Dec 25 19:37:03 2011 From: lists at cheimes.de (Christian Heimes) Date: Mon, 26 Dec 2011 01:37:03 +0100 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> <4ef729aa$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: Am 25.12.2011 15:04, schrieb Chris Angelico: > I think there are certain types that are actually not implemented as > classes, and hence cannot be subclassed. This is almost certainly an > implementation detail though; my testing was done in Py3.2 (on Windows > fwiw). Some extension types are not subclass-able on purpose. The feature isn't available to heap types. http://docs.python.org/c-api/typeobj.html#Py_TPFLAGS_BASETYPE Most people have never noticed the deliberate limitation because only a few special types are sub-classable. Christian From drsalists at gmail.com Sun Dec 25 19:49:56 2011 From: drsalists at gmail.com (Dan Stromberg) Date: Sun, 25 Dec 2011 16:49:56 -0800 Subject: How safe is this way of getting a default that allows None? Message-ID: How safe is this? I like the idea. #!/usr/bin/python UNSPECIFIED = object() def fn(x, y=UNSPECIFIED): if y is UNSPECIFIED: print x, 'default' else: print x, y fn(0, 1) fn(0, None) fn(0) -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sun Dec 25 19:52:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 Dec 2011 00:52:16 GMT Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: <4ef7c540$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 15:45:10 -0800, Larry Hudson wrote: > On 12/24/2011 11:09 PM, GZ wrote: >> Hi, >> >> I run into a weird problem. I have a piece of code that looks like the >> following: >> >> f(...., a=None, c=None): >> assert (a==None)==(c==None) >> > <...> > > At first glance this looked like it should be a simple boolean "and", > but then I realized that when a and c are both unequal to None, the > result would also be True. This implies the logical approach would be > exclusive-or (^). Try this expression: > > not ((a==None) ^ (c==None)) ^ is *bitwise* xor, not boolean xor. Python doesn't offer boolean xor directly, although != comes close. > OTOH, if what you really want is simply to check that both are None (my > first impression), then it's simply: > > (a==None) and (c==None) Replace == with 'is'. > Most of the replies you're getting here seem unnecessarily complicated. == is a more complicated operator than the 'is' operator. That's why the is operator is to be preferred when testing for None -- it is guaranteed to do the right thing, while == is not. -- Steven From jeanpierreda at gmail.com Sun Dec 25 19:58:14 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sun, 25 Dec 2011 19:58:14 -0500 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: > Which of course leads to a SillyPEP for a new keyword, "are", which > would allow you to write: > >>>> a and c are None > > instead of the much more verbose > >>>> a is None and c is None How about: >>> a is b is None ;) -- Devin On Sun, Dec 25, 2011 at 7:27 PM, Roy Smith wrote: > In article , > ?Devin Jeanpierre wrote: > >> The issue here is that "== None" is being used instead of "is None", >> but I believe that's been covered. Your response doesn't include it, >> so maybe it's worth restating. > > Which of course leads to a SillyPEP for a new keyword, "are", which > would allow you to write: > >>>> a and c are None > > instead of the much more verbose > >>>> a is None and c is None > -- > http://mail.python.org/mailman/listinfo/python-list From pip.261 at gmail.com Sun Dec 25 20:04:54 2011 From: pip.261 at gmail.com (Felipe O) Date: Sun, 25 Dec 2011 20:04:54 -0500 Subject: Backslash Escapes Message-ID: Hi all, Whenever I take any input (raw_input, of course!) or I read from a file, etc., any backslashes get escaped automatically. Is there any elegant way of parsing the backslashes as though they were written in a python string. The best I have so far right now goes like this: def parse_backslash_escapes(input_string): parts = input_string.split("'''") # That's ' " " " ' without the spaces '"""'.join(eval + p + '"""') for p in parts) I'm not entirely convinced that it's safe on two accounts. + Is that eval statement safe? The input could be coming from an unfriendly source. + Are there any obscure backslash escapes or other tricks I should be aware of? I guess the alternative is to make a dictionary of all the escapes I want to support, but that sounds tedious and error-prone. Thanks, Felipe From python.list at tim.thechases.com Sun Dec 25 20:14:29 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sun, 25 Dec 2011 19:14:29 -0600 Subject: How safe is this way of getting a default that allows None? In-Reply-To: References: Message-ID: <4EF7CA75.50400@tim.thechases.com> On 12/25/11 18:49, Dan Stromberg wrote: > How safe is this? I like the idea. > > UNSPECIFIED = object() > > def fn(x, y=UNSPECIFIED): > if y is UNSPECIFIED: > print x, 'default' > else: > print x, y safe? It's idiomatic :) -tkc From jeanpierreda at gmail.com Sun Dec 25 20:26:39 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sun, 25 Dec 2011 20:26:39 -0500 Subject: Backslash Escapes In-Reply-To: References: Message-ID: > Whenever I take any input (raw_input, of course!) or I read from a > file, etc., any backslashes get escaped automatically. They don't get escaped, they get... treated as the bytes that they are. If you want them to mean what they do in Python, use the 'string_escape' codec. >>> x = r'nyan\nyan' >>> print x nyan\nyan >>> print x.decode('string_escape') nyan yan -- Devin On Sun, Dec 25, 2011 at 8:04 PM, Felipe O wrote: > Hi all, > Whenever I take any input (raw_input, of course!) or I read from a > file, etc., any backslashes get escaped automatically. Is there any > elegant way of parsing the backslashes as though they were written in > a python string. The best I have so far right now goes like this: > > def parse_backslash_escapes(input_string): > ? ?parts = input_string.split("'''") ?# That's ' " " " ' without the spaces > ? ?'"""'.join(eval + p + '"""') for p in parts) > > I'm not entirely convinced that it's safe on two accounts. > + Is that eval statement safe? The input could be coming from an > unfriendly source. > + Are there any obscure backslash escapes or other tricks I should be aware of? > > I guess the alternative is to make a dictionary of all the escapes I > want to support, but that sounds tedious and error-prone. > > Thanks, > > Felipe > -- > http://mail.python.org/mailman/listinfo/python-list From python at mrabarnett.plus.com Sun Dec 25 20:26:52 2011 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 26 Dec 2011 01:26:52 +0000 Subject: Backslash Escapes In-Reply-To: References: Message-ID: <4EF7CD5C.2060501@mrabarnett.plus.com> On 26/12/2011 01:04, Felipe O wrote: > Hi all, > Whenever I take any input (raw_input, of course!) or I read from a > file, etc., any backslashes get escaped automatically. Is there any > elegant way of parsing the backslashes as though they were written in > a python string. The best I have so far right now goes like this: > > def parse_backslash_escapes(input_string): > parts = input_string.split("'''") # That's ' " " " ' without the spaces > '"""'.join(eval + p + '"""') for p in parts) > > I'm not entirely convinced that it's safe on two accounts. > + Is that eval statement safe? The input could be coming from an > unfriendly source. > + Are there any obscure backslash escapes or other tricks I should be aware of? > > I guess the alternative is to make a dictionary of all the escapes I > want to support, but that sounds tedious and error-prone. > Try input_string.decode("string-escape") if input_string is a bytestring or input_string.decode("unicode-escape") it's a Unicode string. From ian.g.kelly at gmail.com Sun Dec 25 21:02:16 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 25 Dec 2011 19:02:16 -0700 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: On Sun, Dec 25, 2011 at 2:38 AM, Nobody wrote: > On Sat, 24 Dec 2011 23:09:50 -0800, GZ wrote: > >> I run into a weird problem. I have a piece of code that looks like the >> following: >> >> f(...., a=None, c=None): >> ? ? assert ?(a==None)==(c==None) >> >> >> The problem is that == is not implemented sometimes for values in a >> and c, causing an exception NotImplementedError. > > I have no idea how that can happen. If a.__eq__(None) returns > NotImplemented, the interpreter should flip the test and perform the > equivalent of None.__eq__(a), which will return False. Maybe the class has a misbehaved __eq__ that raises NotImplementedError directly instead of returning NotImplemented. From steve+comp.lang.python at pearwood.info Sun Dec 25 22:00:08 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 Dec 2011 03:00:08 GMT Subject: Random string of digits? References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4ef7e337$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 12:41:29 -0500, Roy Smith wrote: > On Mon, 26 Dec 2011 03:11:56 +1100, Chris Angelico wrote: >> > I prefer not to rely on the source. That tells me what happens, not >> > what's guaranteed to happen. > > Steven D'Aprano wrote: >> In this case, the source explicitly tells you that the API includes >> support for arbitrary large ranges if you include a getrandbits() >> method: >> >> Optionally, implement a getrandbits() method so that randrange() >> can cover arbitrarily large ranges. >> >> I call that a pretty strong guarantee. > > I think you mis-understood Chris's point. And I'm afraid that you have missed my point. The above comment from the source is from a docstring: it *is* public, official documentation. See help(random.Random). > The documentation is the > specification of how something behaves. If the documentation doesn't > say it, you can't rely on it. A nice platitude, but not true. Documentation is often incomplete or even inaccurate. We rely on many things that aren't documented anywhere. For example, we can rely on the fact that x = 3917 print x+1 will print 3918, even though that specific fact isn't documented anywhere. Nevertheless, we can absolutely bank on it -- if it happened to do something else, we would report it as a bug and not expect to be told "implementation detail, will not fix". We make a number of undocumented assumptions: * we assume that when the documentation talks about "adding" two numbers, it means the standard mathematical definition of addition and not some other meaning; * we assume that the result of such addition must be *correct*, without that assumption being guaranteed anywhere; * we assume that addition of two ints will return an int, as opposed to some other numerically equal value such as a float or Fraction; * we assume that not only will it be an int, but it will be *exactly* an int, and not a subclass of int; and no doubt there are others. And by the way, in case you think I'm being ridiculously pedantic, consider the first assumption listed above: the standard mathematical definition of addition. That assumption is violated for floats. py> 0.7 + 0.1 == 0.8 False It is rather amusing the trust we put in documentation, when documentation can be changed just as easily as code. Just because the Fine Manual says that x+1 performs addition today, doesn't mean that it will continue to say the same thing tomorrow. So if you trust the language designers not to arbitrarily change the documentation, why not trust them not to arbitrarily change the code? Hint: as far as I can tell, nowhere does Python promise that printing 3918 will show those exact digits, but it's a pretty safe bet that Python won't suddenly change to displaying integers in balanced-ternary notation instead of decimal. > The user should never have to read the > source to know how to use a function, or what they can depend on. Well, that's one school of thought. Another school of thought is that documentation and comments lie, and the only thing that you can trust is the code itself. At the end of the day, the only thing that matters is the mindset of the developer(s) of the software. You must try to get into their head and determine whether they are the sort of people whose API promises can be believed. > Now, > I'm not saying that reading the source isn't useful for a deeper > understanding, but it should be understood that any insights you glean > from doing that are strictly implementation details. I couldn't care less about the specific details of *how* getrandbits is used to put together an arbitrarily large random number. That "how" is what people mean when they talk about "mere implementation details". But the fact that getrandbits is used by randrange (as opposed to how it is used) is not a mere implementation detail, but a public part of the interface. > If you're saying that there are guarantees made by the implementation of > getrandbits() which are not documented, then one of two things are true: The implementation of getrandbits is not documented at all. You would have to read the C source of the _random module to find out how the default pseudo-random number generator produces random bits. But note the leading underscore: _random is a private implementation detail. However the existence and use of random.Random.getrandbits is public, documented, and guaranteed. -- Steven From rosuav at gmail.com Sun Dec 25 22:27:44 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 14:27:44 +1100 Subject: Python education survey In-Reply-To: <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 4:44 AM, Rick Johnson wrote: > Why install an IDE when IDLE is already there? Oh, yes, IDLE SUCKS. I > know that already. But this revelation begs the question... Why has > this community allowed IDLE to rot? Why has guido NOT started a public > discussion on the matter? Conversely, why write an IDE into IDLE when perfectly-good IDEs already exist? I don't use IDLE for development per se; it's for interactive Python execution, but not editing of source files. ChrisA From rosuav at gmail.com Sun Dec 25 22:43:35 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 14:43:35 +1100 Subject: Random string of digits? In-Reply-To: <4ef7e337$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef7e337$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 2:00 PM, Steven D'Aprano wrote: > The implementation of getrandbits is not documented at all. You would > have to read the C source of the _random module to find out how the > default pseudo-random number generator produces random bits. But note the > leading underscore: _random is a private implementation detail. > > However the existence and use of random.Random.getrandbits is public, > documented, and guaranteed. It's that last line where I find disagreement between documentation and source code. """Class Random can also be subclassed if you want to use a different basic generator of your own devising: in that case, override the random(), seed(), getstate(), and setstate() methods. Optionally, a new generator can supply a getrandbits() method ? this allows randrange() to produce selections over an arbitrarily large range.""" My reading of this is that, if I were to write my own generator, I could provide that method and gain perfection. It's not until you dig somewhat that you find out that the default generator actually does provide getrandbits, meaning that the default randrange can actually be used for arbitrarily large ranges. Yes, it IS documented, but in separate places and somewhat as an afterthought; in the 2.x documentation it's shown as "new in 2.4", which explains this. Unfortunately my brain isn't really running on all cylinders at the moment and I can't come up with a better wording, but is there some way this could be stated up in the same top paragraph that explains about 53-bit precision and 2**19937-1 period? That's where I got my original information from; I didn't go down to the individual function definitions, which is the only place that it's stated that the Mersenne Twister does include getrandbits (and which assumes that you've read the top section that states that Mersenne Twister is the default implementation). ChrisA From rosuav at gmail.com Sun Dec 25 22:58:36 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 14:58:36 +1100 Subject: Backslash Escapes In-Reply-To: References: Message-ID: On Mon, Dec 26, 2011 at 12:04 PM, Felipe O wrote: > Hi all, > Whenever I take any input (raw_input, of course!) or I read from a > file, etc., any backslashes get escaped automatically. Is there any > elegant way of parsing the backslashes as though they were written in > a python string... I guess the alternative is to make a dictionary of all the escapes I > want to support, but that sounds tedious and error-prone. The question really is: Are you looking to support some backslash escapes, or are you looking to imitate Python? If the former, I recommend making a specific list of what you support; every language has its own, usually including some obscurities, and if all you need is a few basics like \n, \t, \\, etc, then you probably don't want to support \123 and \u1234 notations for arbitrary insertion of characters. But if you actually want to imitate a Python string literal, eval is almost certainly the easiest way; and if you want a safer version: http://docs.python.org/library/ast.html#ast.literal_eval I'm not sure how well suited this is for parsing a string for its escapes, but if you're looking to imitate Python, it may be easier to tell people that the value must be enclosed in quotes - thus making it an expression. ChrisA From roy at panix.com Sun Dec 25 23:17:11 2011 From: roy at panix.com (Roy Smith) Date: Sun, 25 Dec 2011 23:17:11 -0500 Subject: Random string of digits? References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef7e337$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <4ef7e337$0$29973$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > And I'm afraid that you have missed my point. The above comment from the > source is from a docstring: it *is* public, official documentation. See > help(random.Random). When you wrote, "the source explicitly tells you..." the natural assumption I made was "something in the source that's not part of the documentation" (i.e. some comment). > > The documentation is the specification of how something behaves. > > If the documentation doesn't say it, you can't rely on it. > A nice platitude, but not true. Documentation is often incomplete or even > inaccurate. Of course it is. Those things constitute doc bugs which need to get fixed. The fact that the specification is flawed does not change the fact that it *is* the specification. From ethan at stoneleaf.us Sun Dec 25 23:26:06 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 25 Dec 2011 20:26:06 -0800 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: <4EF7F75E.1010909@stoneleaf.us> Nobody wrote: > nothing should compare > equal to None except for None itself, so "x is None" and "x == None" > shouldn't produce different results unless there's a bug in the comparison > method. > Why wouldn't you want other types that can compare equal to None? It could be useful for a Null type to == None. ~Ethan~ From rosuav at gmail.com Sun Dec 25 23:26:48 2011 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 26 Dec 2011 15:26:48 +1100 Subject: Random string of digits? In-Reply-To: References: <4ef7299f$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef74572$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef75afc$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef7e337$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 26, 2011 at 3:17 PM, Roy Smith wrote: > Of course it is. ?Those things constitute doc bugs which need to get > fixed. ?The fact that the specification is flawed does not change the > fact that it *is* the specification. Also, the specification is what can be trusted across multiple implementations of Python - the source can't. I don't intend to get the source code to five Pythons to ascertain whether getrandbits() is provided in the default random.Random() of each of them. Now, in this particular instance, it IS stated in the docs - just down in the individual function descriptions, which I hadn't read - so I almost certainly CAN trust that across all platforms. But in the general case, I would not want to make firm statements on the basis of implementation details. ChrisA From ethan at stoneleaf.us Sun Dec 25 23:27:24 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Sun, 25 Dec 2011 20:27:24 -0800 Subject: Test None for an object that does not implement == In-Reply-To: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: <4EF7F7AC.5000500@stoneleaf.us> GZ wrote: > Hi, > > I run into a weird problem. I have a piece of code that looks like the > following: > > f(...., a=None, c=None): > assert (a==None)==(c==None) Um -- if you don't want a and c being passed in, why put them in the function signature? ~Ethan~ From jeanpierreda at gmail.com Mon Dec 26 00:22:38 2011 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Mon, 26 Dec 2011 00:22:38 -0500 Subject: Test None for an object that does not implement == In-Reply-To: <4EF7F7AC.5000500@stoneleaf.us> References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> <4EF7F7AC.5000500@stoneleaf.us> Message-ID: > Um -- if you don't want a and c being passed in, why put them in the > function signature? He wants both or neither to be passed in. -- Devin On Sun, Dec 25, 2011 at 11:27 PM, Ethan Furman wrote: > GZ wrote: >> >> Hi, >> >> I run into a weird problem. I have a piece of code that looks like the >> following: >> >> f(...., a=None, c=None): >> ? ?assert ?(a==None)==(c==None) > > > > Um -- if you don't want a and c being passed in, why put them in the > function signature? > > ~Ethan~ > -- > http://mail.python.org/mailman/listinfo/python-list From ananya14389 at gmail.com Mon Dec 26 00:30:06 2011 From: ananya14389 at gmail.com (ananya 7) Date: Sun, 25 Dec 2011 21:30:06 -0800 (PST) Subject: Best Online Jobs Message-ID: <7e3ab47d-c41b-4057-88ca-ab3c9a30f3de@r13g2000prr.googlegroups.com> Best Online Jobs http://gsonlinejobs.yolasite.com/ From memilanuk at gmail.com Mon Dec 26 01:02:05 2011 From: memilanuk at gmail.com (Monte Milanuk) Date: Sun, 25 Dec 2011 22:02:05 -0800 Subject: Python education survey In-Reply-To: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: Not a teacher here, but I'm curious why Komodo Edit never seems to get any love in the IDE debates... a free version for personal/non-profit use, pro versions for those that need the extra features, seems to work fairly well but then again I'm probably not the best judge... Thanks, Monte From saqib.ali.75 at gmail.com Mon Dec 26 03:03:57 2011 From: saqib.ali.75 at gmail.com (Saqib Ali) Date: Mon, 26 Dec 2011 00:03:57 -0800 (PST) Subject: Changing the system clock with pexpect confuses pexpect! Message-ID: <1f342621-0c96-447c-ad5d-f8c9dc777883@i6g2000vbe.googlegroups.com> See my code below. I'm controlling a shell logged in as root with pexpect. The class below has a method (startProc) which spawns a shell and keeps it alive until told to destroy it (stopProc). The other 2 methods in this class allow me to change the system clock and to get the IP Address of this machine. They all work fine.... except when I advance the system clock, and then try to get the IP Address. In that case, I get an exception because pexpect incorrectly thinks the output it is getting from ifconfig is invalid. But it is not. Pexpect is just confused. This doesn't happen when I move the clock backwards. It only happens when I move the clock forward. I believe what is going on is that internally pexpect uses the system clock to keep track of when it receives data from spawned processes. When I mess with the clock, that messes up the internal workings of pexpect. Any suggestions what I should do? I have numerous concurrent pexpect processes running when I modify the clock. Is there anyway to prevent them all from getting totally screwed up?? ----------------------------------------------------------------------------------------------------- #!/usr/bin/env python import pexpect, os, time, datetime, re def reportPrint(string): print string def reportAssert(condition, string) if condition == False: print string raise Exception class rootManager: rootProc = None rootPrompt = "] % " myPrompt = "] % " def __init__(self): pass def startProc(self): if self.rootProc != None: reportPrint("\t\t- Root Process is already created") else: self.rootProc = pexpect.spawn ('/bin/tcsh',) i = self.rootProc.expect([pexpect.TIMEOUT, self.myPrompt,]) reportAssert(i != 0, "Time-Out.... exiting") reportPrint("\t\t- Sending su") self.rootProc.sendline("su") i = self.rootProc.expect([pexpect.TIMEOUT, "Password: ",]) reportAssert(i != 0, "Time-Out.... exiting") reportPrint("\t\t- Sending Password") self.rootProc.sendline(ROOT_PASSWORD) i = self.rootProc.expect([pexpect.TIMEOUT, self.rootPrompt,]) reportAssert(i != 0, "Time-Out.... exiting") reportPrint("\t\t- Root Process created") def getIPAddr(self): reportAssert(self.rootProc != None, "No active Root Process!") reportPrint("\t\t- Sending ifconfig -a") self.rootProc.sendline("ifconfig -a") i = self.rootProc.expect([pexpect.TIMEOUT, self.rootPrompt,]) reportAssert(i != 0, "Time-Out.... exiting") outputTxt = self.rootProc.before ipList = [i for i in re.compile("(?<=inet )\d{1,3}\.\d{1,3}\. \d{1,3}\.\d{1,3}").findall(outputTxt) if i != "127.0.0.1"] reportAssert(len(ipList) == 1, "Cannot determine IP Address from 'ifconfig -a': \n%s" % outputTxt) return ipList[0] def changeClock(self, secondsDelta): reportAssert(self.rootProc != None, "No active Root Process!") newTime = datetime.datetime.now() + datetime.timedelta(seconds=secondsDelta) dateStr = "%02d%02d%02d%02d%s" % (newTime.month, newTime.day, newTime.hour, newTime.minute, str(newTime.year)[-2:]) reportPrint("\t\t- Sending 'date %s' command" % dateStr) self.rootProc.sendline("date %s" % dateStr) #Remember, by changing the clock, you are confusing pexpect's timeout measurement! # so ignore timeouts in this case i = self.rootProc.expect([pexpect.TIMEOUT, self.rootPrompt,],) def stopProc(self): if self.rootProc == None: reportPrint("\t\t- Root Process is already destroyed") else: reportPrint("\t\t- Sending exit command") rootProc.sendline("exit") i = rootProc.expect([pexpect.TIMEOUT, self.myPrompt]) reportAssert(i != 0, "Time-Out.... exiting") reportPrint("\t\t- Sending exit command") rootProc.sendline("exit") i = rootProc.expect([pexpect.TIMEOUT, pexpect.EOF]) reportAssert(i != 0, "Time-Out.... exiting") self.rootProc.close() self.rootProc = None reportPrint("\t\t- Root Process Destroyed") From vivekimsit at gmail.com Mon Dec 26 03:37:02 2011 From: vivekimsit at gmail.com (vivek poddar) Date: Mon, 26 Dec 2011 00:37:02 -0800 (PST) Subject: Python Openings Message-ID: Hi, I have just completed my B.tech this year and have above six months experience in OpenERP framework as a technical consultant.In these six months I have completed nearly 5 modules and acted as an active member.Now, my company is going under a financial crisis and so, I think I have to leave this job.But I want to continue my career in OpenERP or Python. Thanks and Regards! Vivek Poddar From lie.1296 at gmail.com Mon Dec 26 04:28:14 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 26 Dec 2011 20:28:14 +1100 Subject: Plot seems weird In-Reply-To: <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> Message-ID: On 12/26/2011 05:27 AM, Yigit Turgut wrote: > On Dec 25, 7:06 pm, Rick Johnson wrote: >> On Dec 25, 9:33 am, Yigit Turgut wrote: >>> Hi all, >> >>> I have a text file as following; >> >>> 0.200047 0.000000 >>> 0.200053 0.160000 >>> 0.200059 0.000000 >>> 0.200065 0.080000 >>> 0.200072 0.000000 >>> 0.200078 0.160000 >> >>> And I am trying to plot it with ; >> >>> filenames = sys.argv[1:] >>> if len(filenames) == 0: >>> filenames = [sys.stdin] >>> for filename in filenames: >>> t,y1 = numpy.genfromtxt(filename, unpack=True) >>> pyplot.plot(t,y1) >>> pyplot.show() >> >>> But graph seems weird, not as it supposed to be. Any ideas ? >> >> Interesting. Of course "weird" leaves a LOT to be desired. On a scale >> of 1-10, how "weird" is the result? > > I apply a 1Khz test signal just to see if things run smoothly, but I > see spikes at lower and higher ends (logic 0,1) where I should see a > clean rectangle pwm signal. By the look of it I say weirdness is > around 3/10. What are you expecting? Your data produces something that looks like the plot on the right of this screenshot (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with that; if you are expecting a square-wave-like plot (like on the left), then you should use a square-wave-like data, pyplot wouldn't magically transform a spiked-plot to squared-plot. Here's what I use to convert the data on right plot to data on left plot, I don't know much about numpy so it might be possible to do it more efficiently or numpy might even have something like it already. from itertools import izip_longest def to_square(t, y1): sq_data = [[], []] for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): sq_data[0].append(x) sq_data[1].append(y) sq_data[0].append(xn) sq_data[1].append(y) return numpy.array(sq_data, dtype=float) From lie.1296 at gmail.com Mon Dec 26 04:36:39 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 26 Dec 2011 20:36:39 +1100 Subject: Backslash Escapes In-Reply-To: References: Message-ID: On 12/26/2011 12:04 PM, Felipe O wrote: > Hi all, > Whenever I take any input (raw_input, of course!) or I read from a > file, etc., any backslashes get escaped automatically. Python never escapes backslashes when reading from raw_input or files. Python only ever escapes backslashes when displaying data on the interactive shell since the interactive shell uses __repr__ by default. > Is there any > elegant way of parsing the backslashes as though they were written in > a python string. The best I have so far right now goes like this: You shouldn't ever need to unless your file are backslash-escaped. From briskb5 at gmail.com Mon Dec 26 05:33:56 2011 From: briskb5 at gmail.com (briskb5 briskb5) Date: Mon, 26 Dec 2011 02:33:56 -0800 (PST) Subject: Best Online Jobs Message-ID: <7bdff594-5561-484e-b251-42a89da9d498@e8g2000prb.googlegroups.com> Best Online Jobs http://gsonlinejobs.yolasite.com/ From roy at panix.com Mon Dec 26 08:43:34 2011 From: roy at panix.com (Roy Smith) Date: Mon, 26 Dec 2011 08:43:34 -0500 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> <4EF7F7AC.5000500@stoneleaf.us> Message-ID: In article , Devin Jeanpierre wrote: > > Um -- if you don't want a and c being passed in, why put them in the > > function signature? > > He wants both or neither to be passed in. assert sum(foo is None for foo in [a, c]) % 2 == 0 From roy at panix.com Mon Dec 26 08:51:24 2011 From: roy at panix.com (Roy Smith) Date: Mon, 26 Dec 2011 08:51:24 -0500 Subject: Changing the system clock with pexpect confuses pexpect! References: <1f342621-0c96-447c-ad5d-f8c9dc777883@i6g2000vbe.googlegroups.com> Message-ID: In article <1f342621-0c96-447c-ad5d-f8c9dc777883 at i6g2000vbe.googlegroups.com>, Saqib Ali wrote: > I believe what is going on is that internally pexpect uses the system > clock to keep track of when it receives data from spawned processes. > When I mess with the clock, that messes up the internal workings of > pexpect. > > Any suggestions what I should do? Yeah. Don't mess with the system clock. Seriously. There is pretty much no reason for anybody not to be running NTP these days. Every OS ships with it, and there's gazillions of open NTP servers to use as references. NTP will keep your system clock accurate to within a fraction of a second and avoid all the horrible problems such as the one you've discovered with pexpect when you reset your clock. From paul.nospam at rudin.co.uk Mon Dec 26 09:33:24 2011 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Mon, 26 Dec 2011 14:33:24 +0000 Subject: Test None for an object that does not implement == References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> Message-ID: GZ writes: > I run into a weird problem. I have a piece of code that looks like the > following: > > f(...., a=None, c=None): > assert (a==None)==(c==None) > There is only one 'None' - so use 'a is None' rather than 'a == None'. (In common lisp there is a particular language construct that allows you do determine whether an argument was passed in or was defaulted.) From rantingrickjohnson at gmail.com Mon Dec 26 09:52:03 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Mon, 26 Dec 2011 06:52:03 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> Message-ID: <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> On Dec 25, 9:27?pm, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 4:44 AM, Rick Johnson > > [...] > Conversely, why write an IDE into IDLE when perfectly-good IDEs > already exist? I don't use IDLE for development per se; it's for > interactive Python execution, but not editing of source files. I believe the answer is two fold: 1. Including an IDE like IDLE into the Python distro helps noobs to get started quickly without needing to traverse a gauntlet of unknown IDEs on their own. If later they find something that they feel is more appropriate; so be it. 2. (and most important to me... IDLE is written in Python using the Tkinter GUI (which ships with python also). Therefore, the source code for IDLE can be a GREAT teaching resource for how to write professional Tkinter applications. I KNOW THE CURRENT SOURCE SUCKS! However, we could change that. So, with those points being covered, i believe IDLE is very important to the Python community and could be useful to more people IF we clean it up a bit. It's really a great little IDE with even greater potential. If Guido would just say something (or at least some of the top Pythionistas (Hettinger i am looking at you!)) this community might work together to fix this problem. From nad at acm.org Mon Dec 26 09:54:41 2011 From: nad at acm.org (Ned Deily) Date: Mon, 26 Dec 2011 09:54:41 -0500 Subject: installing matplotlib in MacOs 10.6.8. References: Message-ID: In article , Alex Ter-Sarkissov wrote: > hi everyone, I run python 2.7.2. in Eclipse (recently upgraded from 2.6). I > have a problem with installing matplotlib (I found the version for python > 2.7. MacOs 10.3, no later versions). If I run python in terminal using arch > -i386 python, and then > > from matplotlib.pylab import * > > and similar stuff, everything works fine. If I run python in eclipse or > just without arch -i386, I can import matplotlib as > > from matplotlib import * > > but actually nothing gets imported. If I do it in the same way as above, I > get the message > > no matching architecture in universal wrapper > > which means there's conflict of versions or something like that. I tried > reinstalling the interpreter and adding matplotlib to forced built-ins, but > nothing helped. For some reason I didn't have this problem with numpy and > tkinter. The message means almost certainly means that the Python you are using is a 64-bit/32-bit universal version. When you launch Python with -arch i386, you force the Python to run in 32-bit mode so it is compatible with the 32-bit-only version of matplotlib you've installed in it. When you launch Python by default, it is runnning in 64-bit mode so the 32-bit-only C extension modules in that version of matplotlib cannot be loaded. You might want to ask the matplotlib developers to make a 64-bit version available. In the mean time, you could try to configure Eclipse to invoke the python with `python2.7-32`, which is included with distributions like the python.org one to force Python to run in 32-bit mode on OS X. -- Ned Deily, nad at acm.org From nathan.alexander.rice at gmail.com Mon Dec 26 11:11:32 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Mon, 26 Dec 2011 11:11:32 -0500 Subject: Python education survey In-Reply-To: <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 9:52 AM, Rick Johnson wrote: > On Dec 25, 9:27?pm, Chris Angelico wrote: >> On Mon, Dec 26, 2011 at 4:44 AM, Rick Johnson >> > [...] >> Conversely, why write an IDE into IDLE when perfectly-good IDEs >> already exist? I don't use IDLE for development per se; it's for >> interactive Python execution, but not editing of source files. > > I believe the answer is two fold: > > 1. Including an IDE like IDLE into the Python distro helps noobs to > get started quickly without needing to traverse a gauntlet of unknown > IDEs on their own. If later they find something that they feel is more > appropriate; so be it. > > 2. (and most important to me... IDLE is written in Python using the > Tkinter GUI (which ships with python also). Therefore, the source code > for IDLE can be a GREAT teaching resource for how to write > professional Tkinter applications. I KNOW THE CURRENT SOURCE SUCKS! > However, we could change that. > > So, with those points being covered, i believe IDLE is very important > to the Python community and could be useful to more people IF we clean > it up a bit. It's really a great little IDE with even greater > potential. If Guido would just say something (or at least some of the > top Pythionistas (Hettinger i am looking at you!)) this community > might work together to fix this problem. Not everyone who has Python installed wants to learn the language. I do think that a "learning" distro that has a lot of core tools pre-installed, and ships with some tutorials, would be a decent idea. Sort of like Enthought for new users :) I don't feel IDLE is worth salvaging though. Nathan From y.turgut at gmail.com Mon Dec 26 12:08:03 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Mon, 26 Dec 2011 09:08:03 -0800 (PST) Subject: Plot seems weird References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> Message-ID: <74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com> On Dec 26, 11:28?am, Lie Ryan wrote: > On 12/26/2011 05:27 AM, Yigit Turgut wrote: > > > > > > > > > > > On Dec 25, 7:06 pm, Rick Johnson ?wrote: > >> On Dec 25, 9:33 am, Yigit Turgut ?wrote: > >>> Hi all, > > >>> I have a text file as following; > > >>> 0.200047 ? ? ? ?0.000000 > >>> 0.200053 ? ? ? ?0.160000 > >>> 0.200059 ? ? ? ?0.000000 > >>> 0.200065 ? ? ? ?0.080000 > >>> 0.200072 ? ? ? ?0.000000 > >>> 0.200078 ? ? ? ?0.160000 > > >>> And I am trying to plot it with ; > > >>> filenames = sys.argv[1:] > >>> if len(filenames) == 0: > >>> ? ? ?filenames = [sys.stdin] > >>> for filename in filenames: > >>> ? ? ?t,y1 = numpy.genfromtxt(filename, unpack=True) > >>> ? ? ?pyplot.plot(t,y1) > >>> ? ? ?pyplot.show() > > >>> But graph seems weird, not as it supposed to be. Any ideas ? > > >> Interesting. Of course "weird" leaves a LOT to be desired. On a scale > >> of 1-10, how "weird" is the result? > > > I apply a 1Khz test signal just to see if things run smoothly, but I > > see spikes at lower and higher ends (logic 0,1) where I should see a > > clean rectangle pwm signal. By the look of it I say weirdness is > > around 3/10. > > What are you expecting? Your data produces something that looks like the > plot on the right of this screenshot > (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with > that; if you are expecting a square-wave-like plot (like on the left), > then you should use a square-wave-like data, pyplot wouldn't magically > transform a spiked-plot to squared-plot. > > Here's what I use to convert the data on right plot to data on left > plot, I don't know much about numpy so it might be possible to do it > more efficiently or numpy might even have something like it already. > > from itertools import izip_longest > def to_square(t, y1): > ? ? ?sq_data = [[], []] > ? ? ?for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): > ? ? ? ? ?sq_data[0].append(x) > ? ? ? ? ?sq_data[1].append(y) > ? ? ? ? ?sq_data[0].append(xn) > ? ? ? ? ?sq_data[1].append(y) > ? ? ?return numpy.array(sq_data, dtype=float) Thanks for the tip. I know that I feed a square wave signal and record this data. Thus I believe the situation can be related to sampling frequency. Couldn't get your code working, maybe because I import the data from file. From ethan at stoneleaf.us Mon Dec 26 12:14:31 2011 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 26 Dec 2011 09:14:31 -0800 Subject: Test None for an object that does not implement == In-Reply-To: References: <0d80ce25-9e65-4216-b26c-b8ee40f989a3@q9g2000yqe.googlegroups.com> <4EF7F7AC.5000500@stoneleaf.us> Message-ID: <4EF8AB77.5080605@stoneleaf.us> Devin Jeanpierre wrote: >> Um -- if you don't want a and c being passed in, why put them in the >> function signature? > > He wants both or neither to be passed in. Ah -- right. ~Ethan~ From rantingrickjohnson at gmail.com Mon Dec 26 12:27:35 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Mon, 26 Dec 2011 09:27:35 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> Message-ID: On Dec 26, 10:11?am, Nathan Rice wrote: > On Mon, Dec 26, 2011 at 9:52 AM, Rick Johnson > > > > > > > > > > wrote: > > On Dec 25, 9:27?pm, Chris Angelico wrote: > >> On Mon, Dec 26, 2011 at 4:44 AM, Rick Johnson > >> > [...] > >> Conversely, why write an IDE into IDLE when perfectly-good IDEs > >> already exist? I don't use IDLE for development per se; it's for > >> interactive Python execution, but not editing of source files. > > > I believe the answer is two fold: > > > 1. Including an IDE like IDLE into the Python distro helps noobs to > > get started quickly without needing to traverse a gauntlet of unknown > > IDEs on their own. If later they find something that they feel is more > > appropriate; so be it. > > > 2. (and most important to me... IDLE is written in Python using the > > Tkinter GUI (which ships with python also). Therefore, the source code > > for IDLE can be a GREAT teaching resource for how to write > > professional Tkinter applications. I KNOW THE CURRENT SOURCE SUCKS! > > However, we could change that. > > > So, with those points being covered, i believe IDLE is very important > > to the Python community and could be useful to more people IF we clean > > it up a bit. It's really a great little IDE with even greater > > potential. If Guido would just say something (or at least some of the > > top Pythionistas (Hettinger i am looking at you!)) this community > > might work together to fix this problem. > > Not everyone who has Python installed wants to learn the language. I'll admit you make a very valid point. But since "they" are not familiar with the language, what they don't know cannot hurt them. Although i do see a need to keep the core distro small. If we remove IDLE then we face the next big question... Should we remove Tkinter? >?I > do think that a "learning" distro that has a lot of core tools > pre-installed, and ships with some tutorials, would be a decent idea. > Sort of like Enthought for new users :) This is the kind of inventiveness i have been looking for in this group. I think this is a great idea! > ?I don't feel IDLE is worth salvaging though. Agreed. Not in it's current state anyway. From steve+comp.lang.python at pearwood.info Mon Dec 26 13:04:34 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 26 Dec 2011 18:04:34 GMT Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> Message-ID: <4ef8b732$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 06:52:03 -0800, Rick Johnson wrote: > If Guido would just say something (or at least some of the top > Pythionistas (Hettinger i am looking at you!)) this community might work > together to fix this problem. The sheer cluelessness displayed here about open source is painful. If Guido would "just say something", this community would yawn. He's not the boss of us. Python development is an all-volunteer effort. "The community" works on projects that we find interesting, or that we need ourselves, not just because Guido "says something" about it -- and certainly not projects that you demand we work on. Instead of demanding that GvR order us to work on IDLE, put your money where your mouth is, Rick, and stop being a blow-hard. You've been nagging us about IDLE for long enough -- what is it now, two years? Three? I haven't been counting, but it feels like geological ages. Start a project to fix IDLE. There is plenty of free hosting on the Internet for open source projects, like GitHub and Google Code Hosting. Track some bugs. Plan some concrete enhancements. Write some actual code. Demonstrate actual effort, and you may attract others who care. Otherwise, you're wasting our time. -- Steven From wolftracks at invalid.com Mon Dec 26 13:16:30 2011 From: wolftracks at invalid.com (W. eWatson) Date: Mon, 26 Dec 2011 10:16:30 -0800 Subject: Is my IDLE problem caused by .idlerc? Permissions. In-Reply-To: References: Message-ID: On 12/24/2011 11:35 AM, Dennis Lee Bieber wrote: > On Sat, 24 Dec 2011 10:55:48 -0800, "W. eWatson" > wrote: > >> >> Permissions as follows: >> SYSTEM: All. From Full control to write >> Account Unknown(S-1-5-21...): read&exec, Read >> Wayne: (normal use) All. From Full control to write >> Admin: All. From Full control to write >> WMPNetwork: Read >> >> Comments? >> >> .idlerc permissions are slightly different for Account Unknown. > > "Account Unknown" refers to a user account that has been deleted > (or, via some other system corruption, is no longer associated with a > login name -- happened to me earlier this year: my user account "name" > had been "Wulfraed" but the user account directory was under "Dennis Lee > Bieber"... When the registry crashed it disconnected name "Wulfraed" > from directory "Dennis Lee Bieber"... Lost a month of email once I got > done creating a new user account where directory and name matched and > restored from backup [I failed to back-up the old user directory before > trying a remove/create user]). > > My suggestion would be, first, remove privileges for that remnant > account where-ever found; check higher directory levels too, and try to > propagate the privilege downwards. > > Second -- could the file itself be set for read-only on the simple > properties? I just tested with Win7 -- setting the general "read-only" > did not make any changes to any of the security tab "users". Third, > confirm the owner of the file is your current user account; if not,see > if you can get "take ownership" option for the file to work. > > I wonder if removing the "Account Unknown" account might just do it. Not quite familiar with Owner, but I'll look around. I'll check the file for "read only". From y.turgut at gmail.com Mon Dec 26 13:31:36 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Mon, 26 Dec 2011 10:31:36 -0800 (PST) Subject: Multithreading Message-ID: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> I have a loop as following ; start = time.time() end = time.time() - start while(end <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> <4ef8b732$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <90a19db5-487a-4af6-92ff-8654f1091283@p41g2000yqm.googlegroups.com> On Dec 26, 12:04?pm, Steven D'Aprano wrote: > If Guido would "just say something", this community would yawn. He's not > the boss of us. Python development is an all-volunteer effort. "The > community" works on projects that we find interesting, or that we need > ourselves, not just because Guido "says something" about it Believe it or not Steven, all communities need leadership. Just because you are a rebel and don't like anyone trumping your opinions does mean that the majority of people here don't need (or want for that mater) some direction.Communities work best when leaders lead and workers work. And "leader" does not always mean a single person. NO. In the best communities leadership roles are dynamic. For example: In such a diverse field as programming language design, no ONE person can lead all the time! We need people to step forth who are competent in the various areas of expertise. Someone who is really knowledgeable writing code for Linux may not be as knowledgeable writing code for windows. Some will be better at writing efficient algorithmic code and others may be better at documentation. etc,etc... HOWEVER Even IF we adopt the intelligent paradigm of dynamic leadership, we still need a small group (or at least) one very intelligent person to bind the process together. Guido van Rossum IS that person. That is why i call for him to get more involved. Of course i don't expect him to write code. But at least he could be more involved in this community. AND PYDEV IS NOT THE ONLY MAILING LIST IN THIS FREAKING COMMUNITY! He needs to step out from behind the coattails of his dev group and be a real leader. He needs to make his opinion known about Tkinter, IDLE, and also, the drastic changes that will need to be implemented in Python 4000. How can HE expect a true FLOSS community to move forward when SO many are just blatantly ignored? HOW? > -- and > certainly not projects that you demand we work on. I am NOT demanding that people WORK on anything Steven. I am ASKING for people to get more involved! Just open your mouth and state your opinion (and not just troll all the time!). But as long as Guido and the other members of pydev refuse to acknowledge the existence of c.l.p, we will forever be a stagnate community. > Instead of demanding that GvR order us to work on IDLE, put your money > where your mouth is, Rick, and stop being a blow-hard. You've been > nagging us about IDLE for long enough -- what is it now, two years? > Three? I haven't been counting, but it feels like geological ages. > > Start a project to fix IDLE. There is plenty of free hosting on the > Internet for open source projects, like GitHub and Google Code Hosting. > Track some bugs. Plan some concrete enhancements. Write some actual code. > Demonstrate actual effort, and you may attract others who care. > Otherwise, you're wasting our time. I will do this, as soon as the "leaders" start to lead. Only a fool would invest time to make a module or package "stdlib ready" without getting the input of his fellow brethren first. Sounds like a recipe for disaster to me. Why has Guido not, at the very least, contacted me privately? He could remain anonymous. From lie.1296 at gmail.com Mon Dec 26 13:58:43 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 27 Dec 2011 05:58:43 +1100 Subject: Plot seems weird In-Reply-To: <74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com> References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> <74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com> Message-ID: On 12/27/2011 04:08 AM, Yigit Turgut wrote: > On Dec 26, 11:28 am, Lie Ryan wrote: >> On 12/26/2011 05:27 AM, Yigit Turgut wrote: >> >> >> >> >> >> >> >> >> >>> On Dec 25, 7:06 pm, Rick Johnson wrote: >>>> On Dec 25, 9:33 am, Yigit Turgut wrote: >>>>> Hi all, >> >>>>> I have a text file as following; >> >>>>> 0.200047 0.000000 >>>>> 0.200053 0.160000 >>>>> 0.200059 0.000000 >>>>> 0.200065 0.080000 >>>>> 0.200072 0.000000 >>>>> 0.200078 0.160000 >> >>>>> And I am trying to plot it with ; >> >>>>> filenames = sys.argv[1:] >>>>> if len(filenames) == 0: >>>>> filenames = [sys.stdin] >>>>> for filename in filenames: >>>>> t,y1 = numpy.genfromtxt(filename, unpack=True) >>>>> pyplot.plot(t,y1) >>>>> pyplot.show() >> >>>>> But graph seems weird, not as it supposed to be. Any ideas ? >> >>>> Interesting. Of course "weird" leaves a LOT to be desired. On a scale >>>> of 1-10, how "weird" is the result? >> >>> I apply a 1Khz test signal just to see if things run smoothly, but I >>> see spikes at lower and higher ends (logic 0,1) where I should see a >>> clean rectangle pwm signal. By the look of it I say weirdness is >>> around 3/10. >> >> What are you expecting? Your data produces something that looks like the >> plot on the right of this screenshot >> (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with >> that; if you are expecting a square-wave-like plot (like on the left), >> then you should use a square-wave-like data, pyplot wouldn't magically >> transform a spiked-plot to squared-plot. >> >> Here's what I use to convert the data on right plot to data on left >> plot, I don't know much about numpy so it might be possible to do it >> more efficiently or numpy might even have something like it already. >> >> from itertools import izip_longest >> def to_square(t, y1): >> sq_data = [[], []] >> for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): >> sq_data[0].append(x) >> sq_data[1].append(y) >> sq_data[0].append(xn) >> sq_data[1].append(y) >> return numpy.array(sq_data, dtype=float) > > Thanks for the tip. I know that I feed a square wave signal and record > this data. Thus I believe the situation can be related to sampling > frequency. It is due to sampling frequency, but also because you cannot sample a square wave perfectly because square wave has infinite steepness at the transitions. Although if you know the exact timing of the transitions, it may be possible to reconstruct the transitions perfectly. > Couldn't get your code working, maybe because I import the data from > file. not your fault, I made a mistake when copy-pasteing the code, here's the fixed code: from itertools import izip_longest def to_square(data): sq_data = [[], []] for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): sq_data[0].append(x) sq_data[1].append(y) sq_data[0].append(xn) sq_data[1].append(y) return numpy.array(sq_data, dtype=float) use it like this: t,y1 = to_square(numpy.genfromtxt(filename, unpack=True)) pyplot.plot(t,y1) pyplot.show() From y.turgut at gmail.com Mon Dec 26 14:14:41 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Mon, 26 Dec 2011 11:14:41 -0800 (PST) Subject: Plot seems weird References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> <74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com> Message-ID: <96ac870e-dffe-4556-aa3a-fab84333ceda@o9g2000vbc.googlegroups.com> On Dec 26, 8:58?pm, Lie Ryan wrote: > On 12/27/2011 04:08 AM, Yigit Turgut wrote: > > > > > > > > > > > On Dec 26, 11:28 am, Lie Ryan ?wrote: > >> On 12/26/2011 05:27 AM, Yigit Turgut wrote: > > >>> On Dec 25, 7:06 pm, Rick Johnson ? ?wrote: > >>>> On Dec 25, 9:33 am, Yigit Turgut ? ?wrote: > >>>>> Hi all, > > >>>>> I have a text file as following; > > >>>>> 0.200047 ? ? ? ?0.000000 > >>>>> 0.200053 ? ? ? ?0.160000 > >>>>> 0.200059 ? ? ? ?0.000000 > >>>>> 0.200065 ? ? ? ?0.080000 > >>>>> 0.200072 ? ? ? ?0.000000 > >>>>> 0.200078 ? ? ? ?0.160000 > > >>>>> And I am trying to plot it with ; > > >>>>> filenames = sys.argv[1:] > >>>>> if len(filenames) == 0: > >>>>> ? ? ? filenames = [sys.stdin] > >>>>> for filename in filenames: > >>>>> ? ? ? t,y1 = numpy.genfromtxt(filename, unpack=True) > >>>>> ? ? ? pyplot.plot(t,y1) > >>>>> ? ? ? pyplot.show() > > >>>>> But graph seems weird, not as it supposed to be. Any ideas ? > > >>>> Interesting. Of course "weird" leaves a LOT to be desired. On a scale > >>>> of 1-10, how "weird" is the result? > > >>> I apply a 1Khz test signal just to see if things run smoothly, but I > >>> see spikes at lower and higher ends (logic 0,1) where I should see a > >>> clean rectangle pwm signal. By the look of it I say weirdness is > >>> around 3/10. > > >> What are you expecting? Your data produces something that looks like the > >> plot on the right of this screenshot > >> (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with > >> that; if you are expecting a square-wave-like plot (like on the left), > >> then you should use a square-wave-like data, pyplot wouldn't magically > >> transform a spiked-plot to squared-plot. > > >> Here's what I use to convert the data on right plot to data on left > >> plot, I don't know much about numpy so it might be possible to do it > >> more efficiently or numpy might even have something like it already. > > >> from itertools import izip_longest > >> def to_square(t, y1): > >> ? ? ? sq_data = [[], []] > >> ? ? ? for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): > >> ? ? ? ? ? sq_data[0].append(x) > >> ? ? ? ? ? sq_data[1].append(y) > >> ? ? ? ? ? sq_data[0].append(xn) > >> ? ? ? ? ? sq_data[1].append(y) > >> ? ? ? return numpy.array(sq_data, dtype=float) > > > Thanks for the tip. I know that I feed a square wave signal and record > > this data. Thus I believe the situation can be related to sampling > > frequency. > > It is due to sampling frequency, but also because you cannot sample a > square wave perfectly because square wave has infinite steepness at the > transitions. Although if you know the exact timing of the transitions, > it may be possible to reconstruct the transitions perfectly. > > > Couldn't get your code working, maybe because I import the data from > > file. > > not your fault, I made a mistake when copy-pasteing the code, here's the > fixed code: > > from itertools import izip_longest > def to_square(data): > ? ? ? sq_data = [[], []] > ? ? ? for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): > ? ? ? ? ? sq_data[0].append(x) > ? ? ? ? ? sq_data[1].append(y) > ? ? ? ? ? sq_data[0].append(xn) > ? ? ? ? ? sq_data[1].append(y) > ? ? ? return numpy.array(sq_data, dtype=float) > > use it like this: > > t,y1 = to_square(numpy.genfromtxt(filename, unpack=True)) > pyplot.plot(t,y1) > pyplot.show() Significant improvement on the plot, pretty interesting. It runs ok but I need to know how?! (: From wolftracks at invalid.com Mon Dec 26 14:37:41 2011 From: wolftracks at invalid.com (W. eWatson) Date: Mon, 26 Dec 2011 11:37:41 -0800 Subject: Is my IDLE problem caused by .idlerc? Permissions. In-Reply-To: References: Message-ID: On 12/26/2011 10:16 AM, W. eWatson wrote: > On 12/24/2011 11:35 AM, Dennis Lee Bieber wrote: >> On Sat, 24 Dec 2011 10:55:48 -0800, "W. eWatson" >> wrote: >> >>> >>> Permissions as follows: >>> SYSTEM: All. From Full control to write >>> Account Unknown(S-1-5-21...): read&exec, Read >>> Wayne: (normal use) All. From Full control to write >>> Admin: All. From Full control to write >>> WMPNetwork: Read >>> >>> Comments? >>> >>> .idlerc permissions are slightly different for Account Unknown. >> >> "Account Unknown" refers to a user account that has been deleted >> (or, via some other system corruption, is no longer associated with a >> login name -- happened to me earlier this year: my user account "name" >> had been "Wulfraed" but the user account directory was under "Dennis Lee >> Bieber"... When the registry crashed it disconnected name "Wulfraed" >> from directory "Dennis Lee Bieber"... Lost a month of email once I got >> done creating a new user account where directory and name matched and >> restored from backup [I failed to back-up the old user directory before >> trying a remove/create user]). >> >> My suggestion would be, first, remove privileges for that remnant >> account where-ever found; check higher directory levels too, and try to >> propagate the privilege downwards. >> >> Second -- could the file itself be set for read-only on the simple >> properties? I just tested with Win7 -- setting the general "read-only" >> did not make any changes to any of the security tab "users". Third, >> confirm the owner of the file is your current user account; if not,see >> if you can get "take ownership" option for the file to work. >> >> > I wonder if removing the "Account Unknown" account might just do it. Not > quite familiar with Owner, but I'll look around. > > I'll check the file for "read only". Solved. Moving .idlerc to another folder for safe keeping, and letting IDLE create a new .idlerc folder did it. I don't have the slightest idea why that worked, but the troublesome file was dated 2/2010, probably from installing 2.5.2, and never touched again by attempts either to re-install 2.5.2 or move up to 2.7.2. From rosuav at gmail.com Mon Dec 26 14:56:14 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 06:56:14 +1100 Subject: Python education survey In-Reply-To: <90a19db5-487a-4af6-92ff-8654f1091283@p41g2000yqm.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <25ff322f-1a4e-43f9-beaa-a9caa650ab1e@v13g2000yqc.googlegroups.com> <4ef8b732$0$29973$c3e8da3$5496439d@news.astraweb.com> <90a19db5-487a-4af6-92ff-8654f1091283@p41g2000yqm.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 5:32 AM, Rick Johnson wrote: > Why has Guido not, at the very least, contacted me > privately? He could remain anonymous. And how would you know if he did contact you anonymously? As to your demand that one of the "top Pythionistas" [sic] say something? I declare hereby that I am, in fact, a top Pythonista. [1] Rick, go fork Python onto Bitbucket. Call it Python 4000 if you like, or just "Python with a better IDLE" or whatever. Go! Shoo! Code! There. A top Pythonista has spoken. And if the above satisfy you not, I actually said it out loud too, just to be sure. That's how important you are to The Python Community (capital letters and all). ChrisA [1] Start here and follow the thread. I've done all of them. (Actually I probably haven't, and I haven't even read the whole thread, which just came up when I googled 'pythonista', but I defy you to prove me lying.) http://mail.python.org/pipermail/python-list/2003-June/207114.html From ian.g.kelly at gmail.com Mon Dec 26 15:01:20 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 26 Dec 2011 13:01:20 -0700 Subject: Multithreading In-Reply-To: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 11:31 AM, Yigit Turgut wrote: > I have a loop as following ; > > start = time.time() > end = time.time() - start > ?while(end ? ? ? ? ?data1 = self.chan1.getWaveform() > ? ? ? ? ?end = time.time() - start > ? ? ? ? ?timer.tick(10) ?#FPS > ? ? ? ? ?screen.fill((255,255,255) if white else(0,0,0)) > ? ? ? ? ?white = not white > ? ? ? ? ?pygame.display.update() > ? ? ? ? ?for i in range(self.size): > ? ? ? ? ? ? ?end = time.time() - start > ? ? ? ? ? ? ?f.write("%3.8f\t%f\n"%(end,data1[i])) > > Roughly speaking, this loop displays something at 10 frames per second > and writes data1 to a file with timestamps. > > At first loop data1 is grabbed but to grab the second value (second > loop) it needs to wait for timer.tick to complete. When I change FPS > value [timer.tick()], capturing period (time interval between loops) > of data1 also changes. What I need is to run ; > > ? ? ? ? ?timer.tick(10) ?#FPS > ? ? ? ? ?screen.fill((255,255,255) if white else(0,0,0)) > ? ? ? ? ?white = not white > ? ? ? ? ?pygame.display.update() > > for N seconds but this shouldn't effect the interval between loops > thus I will be able to continuously grab data while displaying > something at X fps. > > What would be an effective workaround for this situation ? You essentially have two completely independent loops that need to run simultaneously with different timings. Sounds like a good case for multiple threads (or processes if you prefer, but these aren: def write_data(self, f, N): start = time.time() while self.has_more_data(): data1 = self.chan1.getWaveform() time.sleep(N) for i in range(self.size): end = time.time() - start f.write("%3.8f\t%f\n" % (end, data[i])) def write_data_with_display(self, f, N, X): thread = threading.Thread(target=self.write_data, args=(f, N)) thread.start() white = False while thread.is_alive(): timer.tick(X) screen.fill((255, 255, 255) if white else (0, 0, 0)) white = not white pygame.display.update() From ian.g.kelly at gmail.com Mon Dec 26 15:03:04 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 26 Dec 2011 13:03:04 -0700 Subject: Multithreading In-Reply-To: References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 1:01 PM, Ian Kelly wrote: > You essentially have two completely independent loops that need to run > simultaneously with different timings. ?Sounds like a good case for > multiple threads (or processes if you prefer, but these aren: I accidentally sent before I was finished. I was saying "or processes if you prefer, but these aren't CPU-bound, so why complicate things?" Cheers, Ian From y.turgut at gmail.com Mon Dec 26 15:13:01 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Mon, 26 Dec 2011 12:13:01 -0800 (PST) Subject: Multithreading References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> Message-ID: On Dec 26, 10:03?pm, Ian Kelly wrote: > On Mon, Dec 26, 2011 at 1:01 PM, Ian Kelly wrote: > > You essentially have two completely independent loops that need to run > > simultaneously with different timings. ?Sounds like a good case for > > multiple threads (or processes if you prefer, but these aren: > > I accidentally sent before I was finished. ?I was saying "or processes > if you prefer, but these aren't CPU-bound, so why complicate things?" > > Cheers, > Ian I had thought the same workaround but unfortunately loop is already under a def ; def writeWaveform(self, fo, header=''): data1 = numpy.zeros(self.size) screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) timer = pygame.time.Clock() white = True fo.write(header) start = time.time() end = time.time() - start while(end<10): data1 = self.chan1.getWaveform() end = time.time() - start timer.tick(10) #FPS screen.fill((255,255,255) if white else(0,0,0)) white = not white pygame.display.update() for i in range(self.size): end = time.time() - start f.write("%3.8f\t%f\n"%(end,data1[i])) From hoogendoorn.eelco at gmail.com Mon Dec 26 15:39:30 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 12:39:30 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> On Dec 25, 5:15?pm, Steven D'Aprano wrote: > On Sun, 25 Dec 2011 06:55:28 -0800, Eelco wrote: > > Anyway, ?braces are used at > > least an order of magnitude more than collection packing/ unpacking in > > typical code. > > That's a wild and unjustified claim. Here's a quick and dirty test, using > the standard library as an example of typical idiomatic code: > > [steve at orac ~]$ cd /usr/lib/python2.6 > [steve at orac python2.6]$ grep "[*]args" *.py | wc -l > 270 > [steve at orac python2.6]$ grep "{" *.py | wc -l > 550 > > Doesn't look like a factor of 10 difference to me. Now try it without changing the subject from round braces to everything but round braces. From ian.g.kelly at gmail.com Mon Dec 26 15:39:48 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 26 Dec 2011 13:39:48 -0700 Subject: Multithreading In-Reply-To: References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> Message-ID: On Mon, Dec 26, 2011 at 1:13 PM, Yigit Turgut wrote: > I had thought the same workaround but unfortunately loop is already > under a def ; So nest the functions, or refactor it. Either way, that shouldn't be a significant obstacle. From hoogendoorn.eelco at gmail.com Mon Dec 26 15:58:36 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 12:58:36 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> Message-ID: <14c228af-0de5-448f-b617-832173aa6cba@p4g2000vbt.googlegroups.com> On Dec 25, 5:23?pm, Chris Angelico wrote: > On Mon, Dec 26, 2011 at 2:38 AM, Eelco wrote: > > Until that time, im going > > to ask you to take 'type constraint' by its literal meaning; a > > coercion of the type of a symbol, rather than whatever particular > > meaning it has acquired for you (it might help if you explained that). > > Im not sure if it was you that brought that up, but let me reiterate > > that I dont mean a 'type cast', which is a runtime concept. A 'type > > constraint' is purely a linguistic construct that will be 'compiled > > out') > > The dictionary definition of constraint is "a limitation or > restriction", and you're right that it can be "compiled out". In fact, > that is probably the best definition. Assuming everything is written > correctly, you should be able to eliminate all constraints and the > code will still function correctly*; but having the constrains means > that certain illegal operations will throw errors. > > Here's two examples of tuple unpacking, one with a type constraint, > the other without: > > a, b = ('hello', [1,2,3] ) > a, b::list = ('hello', [1,2,3] ) > > The constraint on the second line means that, if the second element is > not a list, the interpreter should throw an error. It does NOT mean to > completely change the meaning of the statement to _make_ the last > argument into a list. That is not the job of a constraint. Thank you for providing clarification on what a 'type constraint' means to you. That clears things up a bit. What you are talking about goes by the name of a 'dynamic type CHECK'; some kind of syntactic sugar for something like 'assert(type(obj)==sometype)'. Like a 'type cast', this is also a runtime concept. How you manage to confuse that with what I am talking about, given that ive stated many times I am not talking about a runtime construct but a compile-time construct, is quite beyond me. (not to mention that ive quite explicitly stated what I mean by 'type constraint' many times now). By contrast, here is the first google hit for 'type constraint'. http://msdn.microsoft.com/en-us/library/d5x73970.aspx Note that this is a different application of the concept of a type constraint, but nonetheless, the concept is as I stated it: a constraint to the type of a symbol to modify its compile-time semantics. To cite from its first paragraph: "...you can apply restrictions to the kinds of types ... by using a type that is not allowed by a constraint, the result is a COMPILE-TIME ERROR" (emphasis mine) From rosuav at gmail.com Mon Dec 26 16:01:34 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 08:01:34 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 7:39 AM, Eelco wrote: > Now try it without changing the subject from round braces to > everything but round braces. Around here, the term "braces" means the curly ones - { and } - that delimit blocks of code in C, and dictionaries/sets in Python. "Brackets" may be what you're looking for, if you mean all of ()[]{}. Or if you just mean (), they're called "parentheses". If your point is that parens are used more often than packing/unpacking, that's almost certainly true, since function calls (including method invocations) are so prevalent in pretty much any code. But what does that prove? ChrisA From rosuav at gmail.com Mon Dec 26 16:05:18 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 08:05:18 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <14c228af-0de5-448f-b617-832173aa6cba@p4g2000vbt.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <14c228af-0de5-448f-b617-832173aa6cba@p4g2000vbt.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 7:58 AM, Eelco wrote: > What you are talking about goes by the name of a 'dynamic type CHECK'; > some kind of syntactic sugar for something like > 'assert(type(obj)==sometype)'. Like a 'type cast', this is also a > runtime concept... > > By contrast, here is the first google hit for 'type constraint'. > > http://msdn.microsoft.com/en-us/library/d5x73970.aspx > > "...you can apply restrictions to the kinds of types ... by using a > type that is not allowed by a constraint, the result is a COMPILE-TIME > ERROR" (emphasis mine) A constraint can be applied at compile time or at run time. It'd be valid to apply them at edit time, if you so chose - your editor could refuse to save your file until you fix the problem. Doesn't mean a thing. Python, by its nature, cannot do compile-time type checking. Under no circumstances, however, does this justify the use of the term "constraint" to mean "utterly different semantics of the same code". ChrisA From hoogendoorn.eelco at gmail.com Mon Dec 26 16:41:34 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 13:41:34 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <4ef757e3$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 25, 6:05?pm, Steven D'Aprano wrote: > On Sun, 25 Dec 2011 07:38:17 -0800, Eelco wrote: > > On Dec 25, 2:12?pm, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> wrote: > >> On Sat, 24 Dec 2011 06:39:39 -0800, Eelco wrote: > >> > On Dec 20, 4:30?am, alex23 wrote: > >> >> On Dec 19, 8:15?pm, Eelco wrote: > > >> >> > What does that have to do with collection packing/unpacking? > > >> >> It's mocking your insistance that collection unpacking is a type > >> >> constraint. > > >> > This is really going to be the last time I waste any words on this: > > >> If only that were true, but after sending this post, you immediately > >> followed up with FIVE more posts on this subject in less than half an > >> hour. > > > Did I waste any more words on collection packing and type constraints? > > No, I did not. (though I am about to, and am willing to do so for every > > question that seems genuinely aimed at engaging me on the matter) > > > Did I intend to say that I was going to let a single troll shut down my > > entire topic? No, I did not. > > Ah, well whatever you *intended* wasn't clear from your comment. At least > not clear to *me*. Always glad to help. > > Yes, indeed it would be abuse of language to call this a type > > constraint, since the fact that y is a list is indeed an outcome of > > whatever happens to pop out at the right hand side. One could redefine > > the identifier list to return any kind of object. > > So far, we agree on this. Good. > > How is 'head, *tail = sequence' or semantically entirely equivalently, > > 'head, tail::list = sequence' any different then? Of course after > > interpretation/compilation, what it boils down to is that we are > > constructing a list and binding it to the identifier tail, but that is > > not how it is formulated in python as a language > > I'm afraid it is. > > Here's the definition of assignment in Python 3:http://docs.python.org/py3k/reference/simple_stmts.html#assignment- > statements Que? 'head, *tail = sequence' Is how one currently unpacks a head and tail in idiomatic python This is semantically equivalent to 'head = sequence[0]' 'tail = list(sequence[1:])' But these forms are linguistically different, in too many different ways to mention. > > We dont have > > something of the form 'tail = list_tail(sequence)'. > > I'm afraid we do. See the definition of assignment again. Que? My claim is that the two semantically identical formulations above do not have isomorphic linguistic form. As far as I can make sense of your words, you seem to be disputing this claim, but its a claim as much worth debating as that the sun rises in the east. > > Rather, we annotate > > the identifier 'tail' with an attribute that unquestionably destinates > > it to become a list*. It is no longer that 'tail' will just take > > anything that pops out of the expression on the right hand side; > > Of course it will. Python is a dynamically typed language. It doesn't > suddenly develop static types to ensure that 'tail' becomes a list; > 'tail' is bound to a list because that's what the assignment statement > provides. How python accomplishes any of this under the hood is entirely immaterial. The form is that of a compile-time type constraint, regardless of whether the BDFL ever thought about it in these terms. > > rather, > > the semantics of what will go on at right hand side is coerced by the > > constraint placed on 'tail'. > > But it isn't a constraint placed on 'tail'. It is a consequence of the > definition of assignment in Python 3. 'tail' becomes bound to a list > because that is what the assignment statement is defined to do in that > circumstance, not because the identifier (symbol) 'tail' is constrained > to only accept lists. 'tail' may not even exist before hand, so talking > about constraints on 'tail' is an abuse of language, AS YOU AGREED ABOVE. 'tail' is (re)declared on the spot as a brand-new identifier (type constraint included); whether it exists before has no significance whatsoever, since python allows rebinding of identifiers. > > *(I call that a 'type constraint', because that is what it literally is; > > No. It is literally a name binding of a dynamically typed, unconstrained > name to an object which happens to be a list. Let me take a step back and reflect on the form of the argument we are having. I claim the object in front of us is a 'cube'. You deny this claim, by countering that it is 'just a particular configuration of atoms'. 'look at the definition!', you say; 'its just a list of coordinates, no mention of cubes whatsoever.'. 'Look at the definition of a cube', I counter. 'This particular list of coordinates happens to fit the definition, whether the BDFT intended to or not'. You are correct, in the sense that I do not disagree that it is a particular configuration of atoms. But if you had a better understanding of cubes, youd realize it meets the definition; the fact that people might not make a habit of pausing at this fact (there is generally speaking not much of a point in doing so in this case), does not diminish the validity of this perspective. But again, if this perspective does not offer you anything useful, feel free not to share in it. > > if you can make a case that this term has acquired a different meaning > > in practice, and that there is another term in common use for this kind > > of construct; please enlighten me. Until that time, im going to ask you > > to take 'type constraint' by its literal meaning; a coercion of the type > > of a symbol, > > But THERE IS NO COERCION OF THE TYPE OF THE SYMBOL. > > I am sorry for shouting, but you seem oblivious to the simple fact that > Python is not statically typed, and the symbol 'tail' is NOT coerced to a > specific type. 'tail' may not even exist before the assignment is made; > if it does exist, it could be *any type at all* -- and after the > assignment takes place, there are no restrictions on subsequent > assignments. > > 'head, *tail = sequence' is no more a type constraint than 'x = 1' is. > > Whatever the virtues of your proposal, you are doing it incalculable harm > by your insistence on this incorrect model of Python's behaviour. Whatever the virtues of your critique of my proposal, you might end up wasting less time doing so by reading a book or two on compiler theory. From hoogendoorn.eelco at gmail.com Mon Dec 26 16:51:50 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 13:51:50 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> Message-ID: <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> On Dec 26, 10:01?pm, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 7:39 AM, Eelco wrote: > > Now try it without changing the subject from round braces to > > everything but round braces. > > Around here, the term "braces" means the curly ones - { and } - that > delimit blocks of code in C, and dictionaries/sets in Python. > "Brackets" may be what you're looking for, if you mean all of ()[]{}. > Or if you just mean (), they're called "parentheses". > > If your point is that parens are used more often than > packing/unpacking, that's almost certainly true, since function calls > (including method invocations) are so prevalent in pretty much any > code. But what does that prove? That proves the original point of contention: that the below* is suboptimal language design, not because terseness always trumps verbosity, but because commonly-used constructs (such as parenthesis or round brackets or whatever you wish to call them) are more deserving of the limited space in both the ascii table and your reflexive memory, than uncommonly used ones. *original mock code by steve: class MyClass superclasslist A, B C: def method argumentlist self, x, y: t = tuple 1, 2 tuple 3, 4 endtuple endtuple return group x + y endgroup * group x - y endgroup From hoogendoorn.eelco at gmail.com Mon Dec 26 17:12:23 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 14:12:23 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <14c228af-0de5-448f-b617-832173aa6cba@p4g2000vbt.googlegroups.com> Message-ID: <82fd3bec-5afa-4e87-ae9a-8f93ecb9e366@dp8g2000vbb.googlegroups.com> On Dec 26, 10:05?pm, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 7:58 AM, Eelco wrote: > > What you are talking about goes by the name of a 'dynamic type CHECK'; > > some kind of syntactic sugar for something like > > 'assert(type(obj)==sometype)'. Like a 'type cast', this is also a > > runtime concept... > > > By contrast, here is the first google hit for 'type constraint'. > > >http://msdn.microsoft.com/en-us/library/d5x73970.aspx > > > "...you can apply restrictions to the kinds of types ... by using a > > type that is not allowed by a constraint, the result is a COMPILE-TIME > > ERROR" (emphasis mine) > > A constraint can be applied at compile time or at run time. It'd be > valid to apply them at edit time, if you so chose - your editor could > refuse to save your file until you fix the problem. Doesn't mean a > thing. A constraint in the sense that I have explained many times now, can in no way, shape or form be applied at run time. Youd have better luck applying a consternation to a squirrel. Perhaps you meant 'type check' again? But then again, that makes no sense whatsoever at compile- time... Im starting to doubt if there is any sense to be found here at all. Anyway, ill take your further silence on the matter as a 'sorry I derailed your thread with my confusion of terminology' > Python, by its nature, cannot do compile-time type checking. Python can do whatever its designers have put into it. In this case, that includes the emission of different code based on a (type) annotation at the point of declaration of an identifier (only in the particular circumstance of collection unpacking though, as far as I am aware). > Under no circumstances, however, does this justify the use of the term > "constraint" to mean "utterly different semantics of the same code". Thank you for your theory on justice. Im sure its fascinating, but until you get around to actually explaining it, im going to have to be conservative and stick with the jargon in common use though, sorry. From charleshixsn at earthlink.net Mon Dec 26 17:23:03 2011 From: charleshixsn at earthlink.net (Charles Hixson) Date: Mon, 26 Dec 2011 14:23:03 -0800 Subject: Possible bug in string handling (with kludgy work-around) Message-ID: <4EF8F3C7.3010005@earthlink.net> This doesn't cause a crash, but rather incorrect results. self.wordList = ["The", "quick", "brown", "fox", "carefully", "jumps", "over", "the", "lazy", "dog", "as", "it", "stealthily", "wends", "its", "way", "homewards", '\b.'] for i in range (len (self.wordList) ): if not isinstance(self.wordList[i], str): self.wordList = "" elif self.wordList[i] != "" and self.wordList[i][0] == "\b": print ("0: wordList[", i, "] = \"", self.wordList[i], "\"", sep = "") print ("0a: wordList[", i, "][1] = \"", self.wordList[i][1], "\"", sep = "") tmp = self.wordList[i][1] ## !! Kludge -- remove tmp to see the error self.wordList[i] = tmp + self.wordList[i][1:-1] ## !! Kludge -- remove tmp + to see the error print ("1: wordList[", i, "] = \"", self.wordList[i], "\"", sep = "") print ("len(wordList[", i, "]) = ", len(self.wordList[i]) ) -- Charles Hixson From rosuav at gmail.com Mon Dec 26 17:27:31 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 09:27:31 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 8:51 AM, Eelco wrote: > That proves the original point of contention: that [Steve's demo code] is > suboptimal language design, not because terseness always trumps > verbosity, but because commonly-used constructs (such as parenthesis > or round brackets or whatever you wish to call them) are more > deserving of the limited space in both the ascii table and your > reflexive memory, than uncommonly used ones. In Magic: The Gathering R&D, they have a term (the article reporting which I can't actually find at the moment) called "spread complexity" or "fan complexity" - the idea being that as you fan out a booster pack, you see a certain amount of complexity in front of you. The designers can afford to put more complex cards in as rares than they can as commons, because you see ten commons for every rare - so a common factors ten times as much as a rare in spread complexity. (Mark Rosewater, my apologies if I'm misremembering here!) The same applies here. When you cast your eye over a program, you're going to see certain syntactic elements a lot. Assignment, arithmetic, blocks of code (ie indent/dedent), and function calls are all extremely common; lambdas, the use of decorators, and exception handling are somewhat uncommon; and metaclasses, the writing of decorators, and reloading of modules are all quite rare. The elements that occur frequently should be: a) Readable and grokkable; b) Easily typed on a regular keyboard - no using ASCII character 126 to mean negation, tyvm! c) Make sense. Rarer elements (and I'm not talking about xenon and plutonium here) are allowed to have long names, obscure syntax, or even be shoved away in odd modules (the way module reloading is in Python 3). If 0.1% of your code is suddenly twice as large as it used to be, will you notice? But if a new syntax adds even 5% to the mindspace requirement of basic assignment, your code will majorly suffer. In summary: Terseness trumps verbosity primarily for common operations, and only when doing so does not violate rules a and c above. ChrisA From rosuav at gmail.com Mon Dec 26 17:37:06 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 09:37:06 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <82fd3bec-5afa-4e87-ae9a-8f93ecb9e366@dp8g2000vbb.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <14c228af-0de5-448f-b617-832173aa6cba@p4g2000vbt.googlegroups.com> <82fd3bec-5afa-4e87-ae9a-8f93ecb9e366@dp8g2000vbb.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 9:12 AM, Eelco wrote: > On Dec 26, 10:05?pm, Chris Angelico wrote: >> A constraint can be applied at compile time or at run time. It'd be >> valid to apply them at edit time, if you so chose - your editor could >> refuse to save your file until you fix the problem. Doesn't mean a >> thing. > > A constraint in the sense that I have explained many times now, can in > no way, shape or form be applied at run time. Youd have better luck > applying a consternation to a squirrel. Perhaps you meant 'type check' > again? But then again, that makes no sense whatsoever at compile- > time... Im starting to doubt if there is any sense to be found here at > all. A constraint failure causes an error at the time it's discovered. 1) Your editor pops up a message the instant you type something with such an error, and forces you to correct it before going on. 2) Your compiler refuses to produce byte-code for the module. 3) When the line of code is executed, an exception is thrown. All of these are valid ways of handling a constraint. Here is a Python example of a type constraint: def foo(arg): if not isinstance(arg,list): raise "This won't work." If you call it with something that's not a list, you get an error. Call it with a list, and execution continues normally. That's a constraint. Of course, it might not be a _type_ constraint: def foo(arg): if arg>5: raise "This won't work either." # and yes, that's oddly truer in Python 3 (Aside: In Pike, I can actually put that into a type constraint (declare the argument to be int(5..) for instance). This, however, is not germane to the conversation.) > Anyway, ill take your further silence on the matter as a 'sorry I > derailed your thread with my confusion of terminology' Like Mary Poppins, I never apologize for derailing threads :) >> Python, by its nature, cannot do compile-time type checking. > > Python can do whatever its designers have put into it. In this case, > that includes the emission of different code based on a (type) > annotation at the point of declaration of an identifier (only in the > particular circumstance of collection unpacking though, as far as I am > aware). Of course, but how can Python, without a completely different structure, do compile-time checking of object types? Everything can be monkey-patched at run-time. Anything could be affected by any function call. It's impossible to say for certain, at compile time, what data type anything will have. ChrisA From tinnews at isbd.co.uk Mon Dec 26 17:37:36 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Mon, 26 Dec 2011 22:37:36 +0000 Subject: How to check for single character change in a string? References: Message-ID: Roy Smith wrote: > In article , > Roy Smith wrote: > > > >>> len([x for x in zip(s1, s2) if x[0] != x[1]]) > > Heh, Ian Kelly's version: > > > sum(a == b for a, b in zip(str1, str2)) > > is cleaner than mine. Except that Ian's counts matches and the OP asked > for non-matches, but that's an exercise for the reader :-) :-) I'm actually walking through a directory tree and checking that file characteristics don't change in a sequence of files. What I'm looking for is 'unusual' changes in file characteristics (they're image files with camera information and such in them) in a sequential list of files. Thus if file001, file002, file003, file004 have the same camera type I'm happy, but if file003 appears to have been taken with a different camera something is probably amiss. I realise there will be *two* character changes when going from file009 to file010 but I can cope with that. I can't just extract the sequence number because in some cases they have non-numeric names, etc. -- Chris Green From rosuav at gmail.com Mon Dec 26 17:44:59 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 09:44:59 +1100 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: <4EF8F3C7.3010005@earthlink.net> References: <4EF8F3C7.3010005@earthlink.net> Message-ID: On Tue, Dec 27, 2011 at 9:23 AM, Charles Hixson wrote: > This doesn't cause a crash, but rather incorrect results. You may need to be a bit clearer. What line of code (or what expression)? What did you expect to see, and what did you see? >From examining your code, I've come up with one most-likely scenario. In Python, indexing is zero-based and, in effect, indexes the boundaries between items rather than the items themselves. Referencing string[-1] actually means asking for the boundary between the second-last and last characters; using that as your end marker actually trims off the last character. What you may want is simply: self.wordList[i][1:] which means "from character position 1 to the end of the string". Hope that helps! Chris Angelico From rantingrickjohnson at gmail.com Mon Dec 26 17:48:35 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Mon, 26 Dec 2011 14:48:35 -0800 (PST) Subject: Possible bug in string handling (with kludgy work-around) References: Message-ID: <0063a1a0-0cbc-4352-90ce-13ab2eda6884@v24g2000yqk.googlegroups.com> On Dec 26, 4:23?pm, Charles Hixson wrote: > This doesn't cause a crash, but rather incorrect results. > > self.wordList ? ?= ? ?["The", "quick", "brown", "fox", "carefully", > ? ? ? ? ? ? ? ? ?"jumps", "over", "the", "lazy", "dog", "as", "it", > ? ? ? ? ? ? ? ? ?"stealthily", "wends", "its", "way", "homewards", '\b.'] > for ? ?i ? ?in ? ?range (len (self.wordList) ): > ? ? if ? ?not isinstance(self.wordList[i], str): > ? ? ? ? self.wordList = "" > ? ?elif self.wordList[i] != "" and self.wordList[i][0] == "\b": > ? ? ? ? print ("0: wordList[", i, "] = \"", self.wordList[i], "\"", sep > = "") > ? ? ? ? print ("0a: wordList[", i, "][1] = \"", self.wordList[i][1], > "\"", sep = "") > ? ? ? ? tmp ? ?= ? ?self.wordList[i][1] ? ? ? ? ? ? ## !! Kludge -- > remove tmp to see the error > ? ? ? ? self.wordList[i] ? ?= ? ?tmp + self.wordList[i][1:-1] ?## !! > Kludge -- remove tmp + to see the error > ? ? ? ? print ("1: wordList[", i, "] = \"", self.wordList[i], "\"", sep > = "") > ? ? ? ? print ? ?("len(wordList[", i, "]) = ", len(self.wordList[i]) ) > > -- > Charles Hixson Handy rules for reporting bugs: 1. Always format code properly. 2. Always trim excess fat from code. 3. Always include relative dependencies ("self.wordlist" is only valid inside a class. In this case, change the code to a state that is NOT dependent on a class definition.) Most times after following these simple rules, you'll find egg on your face BEFORE someone else has a chance to see it and ridicule you. From y.turgut at gmail.com Mon Dec 26 18:00:30 2011 From: y.turgut at gmail.com (Yigit Turgut) Date: Mon, 26 Dec 2011 15:00:30 -0800 (PST) Subject: Multithreading References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> Message-ID: <560bea15-6abc-45ed-ac7a-cf23ff44f130@e2g2000vbb.googlegroups.com> On Dec 26, 10:01?pm, Ian Kelly wrote: > On Mon, Dec 26, 2011 at 11:31 AM, Yigit Turgut wrote: > > I have a loop as following ; > > > start = time.time() > > end = time.time() - start > > ?while(end > ? ? ? ? ?data1 = self.chan1.getWaveform() > > ? ? ? ? ?end = time.time() - start > > ? ? ? ? ?timer.tick(10) ?#FPS > > ? ? ? ? ?screen.fill((255,255,255) if white else(0,0,0)) > > ? ? ? ? ?white = not white > > ? ? ? ? ?pygame.display.update() > > ? ? ? ? ?for i in range(self.size): > > ? ? ? ? ? ? ?end = time.time() - start > > ? ? ? ? ? ? ?f.write("%3.8f\t%f\n"%(end,data1[i])) > > > Roughly speaking, this loop displays something at 10 frames per second > > and writes data1 to a file with timestamps. > > > At first loop data1 is grabbed but to grab the second value (second > > loop) it needs to wait for timer.tick to complete. When I change FPS > > value [timer.tick()], capturing period (time interval between loops) > > of data1 also changes. What I need is to run ; > > > ? ? ? ? ?timer.tick(10) ?#FPS > > ? ? ? ? ?screen.fill((255,255,255) if white else(0,0,0)) > > ? ? ? ? ?white = not white > > ? ? ? ? ?pygame.display.update() > > > for N seconds but this shouldn't effect the interval between loops > > thus I will be able to continuously grab data while displaying > > something at X fps. > > > What would be an effective workaround for this situation ? > > You essentially have two completely independent loops that need to run > simultaneously with different timings. ?Sounds like a good case for > multiple threads (or processes if you prefer, but these aren: > > def write_data(self, f, N): > ? ? start = time.time() > ? ? while self.has_more_data(): > ? ? ? ? data1 = self.chan1.getWaveform() > ? ? ? ? time.sleep(N) > ? ? ? ? for i in range(self.size): > ? ? ? ? ? ? end = time.time() - start > ? ? ? ? ? ? f.write("%3.8f\t%f\n" % (end, data[i])) Why is there N variable in write_data function ? N is related to timer.tick(N) which is related to display function ? time.sleep(N) will pause writing to file for specified amount of time which is exactly what I am trying to avoid. From rosuav at gmail.com Mon Dec 26 18:05:22 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 10:05:22 +1100 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: <0063a1a0-0cbc-4352-90ce-13ab2eda6884@v24g2000yqk.googlegroups.com> References: <0063a1a0-0cbc-4352-90ce-13ab2eda6884@v24g2000yqk.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 9:48 AM, Rick Johnson wrote: > Handy rules for reporting bugs: > > 1. Always format code properly. > 2. Always trim excess fat from code. > 3. Always include relative dependencies ("self.wordlist" is only valid > inside a class. In this case, change the code to a state that is NOT > dependent on a class definition.) > > Most times after following these simple rules, you'll find egg on your > face BEFORE someone else has a chance to see it and ridicule you. 4. Don't take it personally when a known troll insults you. His advice, in this case, is valid; but don't feel that you're going to be ridiculed. We don't work that way on this list. ChrisA From hoogendoorn.eelco at gmail.com Mon Dec 26 18:44:51 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Mon, 26 Dec 2011 15:44:51 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> Message-ID: <5aadbf8b-c905-4115-972c-b37d093126b4@p4g2000vbt.googlegroups.com> On Dec 26, 11:27?pm, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 8:51 AM, Eelco wrote: > > That proves the original point of contention: that [Steve's demo code] is > > suboptimal language design, not because terseness always trumps > > verbosity, but because commonly-used constructs (such as parenthesis > > or round brackets or whatever you wish to call them) are more > > deserving of the limited space in both the ascii table and your > > reflexive memory, than uncommonly used ones. > > In Magic: The Gathering R&D, they have a term (the article reporting > which I can't actually find at the moment) called "spread complexity" > or "fan complexity" - the idea being that as you fan out a booster > pack, you see a certain amount of complexity in front of you. The > designers can afford to put more complex cards in as rares than they > can as commons, because you see ten commons for every rare - so a > common factors ten times as much as a rare in spread complexity. (Mark > Rosewater, my apologies if I'm misremembering here!) > > The same applies here. When you cast your eye over a program, you're > going to see certain syntactic elements a lot. Assignment, arithmetic, > blocks of code (ie indent/dedent), and function calls are all > extremely common; lambdas, the use of decorators, and exception > handling are somewhat uncommon; and metaclasses, the writing of > decorators, and reloading of modules are all quite rare. > > The elements that occur frequently should be: > a) Readable and grokkable; > b) Easily typed on a regular keyboard - no using ASCII character 126 > to mean negation, tyvm! > c) Make sense. > > Rarer elements (and I'm not talking about xenon and plutonium here) > are allowed to have long names, obscure syntax, or even be shoved away > in odd modules (the way module reloading is in Python 3). If 0.1% of > your code is suddenly twice as large as it used to be, will you > notice? But if a new syntax adds even 5% to the mindspace requirement > of basic assignment, your code will majorly suffer. > > In summary: Terseness trumps verbosity primarily for common > operations, and only when doing so does not violate rules a and c > above. > > ChrisA Good to see there is something we agree upon completely. Not that I mean to say the question as to how verbose a syntax is appropriate for collection (un)packing is settled; one could reasonably argue they find tail::tuple too verbose. But parenthesis are not a terribly good example to compare to, since they are infact so much more used they are clearly in another category. *args and **kwargs are debateable in the appropriateness of their terseness (but I personally like to err on the side of verbosity), but extended collection unpacking, as in 'head,*tail=sequence', is quite a rare construct indeed, and here I very strongly feel a more explicit syntax is preferrable. That is, as a seasoned python 2 user, I wouldnt have been willing to gamble on what this does when id come across it for the first time in python 3. Could as well be a completely new use of the asterisk. But if collection packing/unpacking would be presented as a more general construct from the start, 'head,tail::tuple=sequence' would be hard to miss. From mauriceling at gmail.com Mon Dec 26 18:45:21 2011 From: mauriceling at gmail.com (mauriceling@acm.org) Date: Mon, 26 Dec 2011 15:45:21 -0800 (PST) Subject: Regular expressions Message-ID: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Hi I am trying to change "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N: 0:" to "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1". Can anyone help me with the regular expressions needed? Thanks in advance. Maurice From ater1980 at gmail.com Mon Dec 26 18:59:23 2011 From: ater1980 at gmail.com (Alex Ter-Sarkissov) Date: Tue, 27 Dec 2011 12:59:23 +1300 Subject: installing matplotlib in MacOs 10.6.8. Message-ID: thanks Ned, that's quite weird: if I run python2.7-32 in terminal, it works like you said, but when I add it as an interpreter in Eclipse, matplotlib.pyplot doesn't get imported for some reason. Even more strange, either way platform.architecture() reports 64-bit. What's wrong here? cheers, Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon Dec 26 19:00:08 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 11:00:08 +1100 Subject: Regular expressions In-Reply-To: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 10:45 AM, mauriceling at acm.org wrote: > Hi > > I am trying to change to . > > Can anyone help me with the regular expressions needed? A regular expression defines a string based on rules. Without seeing a lot more strings, we can't know what possibilities there are for each part of the string. You probably know your data better than we ever will, even eyeballing the entire set of strings; just write down, in order, what the pieces ought to be - for instance, the first token might be a literal @ sign, followed by three upper-case letters, then a hyphen, then any number of alphanumerics followed by a colon, etc. Once you have that, it's fairly straightforward to translate that into regex syntax. ChrisA From ater1980 at gmail.com Mon Dec 26 19:02:10 2011 From: ater1980 at gmail.com (Alex Ter-Sarkissov) Date: Tue, 27 Dec 2011 13:02:10 +1300 Subject: installing matplotlib in MacOs 10.6.8. In-Reply-To: References: Message-ID: thanks Ned, that's quite weird: if I run python2.7-32 in terminal, it works like you said, but when I add it as an interpreter in Eclipse, matplotlib.pyplot doesn't get imported for some reason. Even more strange, either way platform.architecture() reports 64-bit. What's wrong here? cheers, Alex 27 ??????? 2011 ?. 3:54 ???????????? Ned Deily ???????: > In article > , > Alex Ter-Sarkissov wrote: > > hi everyone, I run python 2.7.2. in Eclipse (recently upgraded from > 2.6). I > > have a problem with installing matplotlib (I found the version for python > > 2.7. MacOs 10.3, no later versions). If I run python in terminal using > arch > > -i386 python, and then > > > > from matplotlib.pylab import * > > > > and similar stuff, everything works fine. If I run python in eclipse or > > just without arch -i386, I can import matplotlib as > > > > from matplotlib import * > > > > but actually nothing gets imported. If I do it in the same way as above, > I > > get the message > > > > no matching architecture in universal wrapper > > > > which means there's conflict of versions or something like that. I tried > > reinstalling the interpreter and adding matplotlib to forced built-ins, > but > > nothing helped. For some reason I didn't have this problem with numpy and > > tkinter. > > The message means almost certainly means that the Python you are using > is a 64-bit/32-bit universal version. When you launch Python with -arch > i386, you force the Python to run in 32-bit mode so it is compatible > with the 32-bit-only version of matplotlib you've installed in it. When > you launch Python by default, it is runnning in 64-bit mode so the > 32-bit-only C extension modules in that version of matplotlib cannot be > loaded. You might want to ask the matplotlib developers to make a > 64-bit version available. In the mean time, you could try to configure > Eclipse to invoke the python with `python2.7-32`, which is included with > distributions like the python.org one to force Python to run in 32-bit > mode on OS X. > > -- > Ned Deily, > nad at acm.org > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From roy at panix.com Mon Dec 26 19:07:49 2011 From: roy at panix.com (Roy Smith) Date: Mon, 26 Dec 2011 19:07:49 -0500 Subject: Regular expressions References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Message-ID: In article <495b6fe6-704a-42fc-b10b-484218ad8409 at b20g2000pro.googlegroups.com>, "mauriceling at acm.org" wrote: > Hi > > I am trying to change "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N: > 0:" to "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1". > > Can anyone help me with the regular expressions needed? Easy-peasy: import re input = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N: 0:" output = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1" pattern = re.compile( r'@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N: 0:') out = pattern.sub( r'@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1', input) assert out == output To be honest, I wouldn't do this with a regex. I'm not quite sure what you're trying to do, but I'm guessing it's something like "Get everything after the first space in the string; keep just the integer that's before the first ':' in that and turn the space into a slash". In that case, I'd do something like: head, tail = input.split(' ', 1) number, _ = tail.split(':') print "%s/%s" % (head, number) From mauriceling at gmail.com Mon Dec 26 19:15:30 2011 From: mauriceling at gmail.com (mauriceling@acm.org) Date: Mon, 26 Dec 2011 16:15:30 -0800 (PST) Subject: Regular expressions References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Message-ID: <4be34afe-4291-414b-9212-498074400e39@v24g2000prn.googlegroups.com> On Dec 27, 8:00?am, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 10:45 AM, mauricel... at acm.org > > wrote: > > Hi > > > I am trying to change to . > > > Can anyone help me with the regular expressions needed? > > A regular expression defines a string based on rules. Without seeing a > lot more strings, we can't know what possibilities there are for each > part of the string. You probably know your data better than we ever > will, even eyeballing the entire set of strings; just write down, in > order, what the pieces ought to be - for instance, the first token > might be a literal @ sign, followed by three upper-case letters, then > a hyphen, then any number of alphanumerics followed by a colon, etc. > Once you have that, it's fairly straightforward to translate that into > regex syntax. > > ChrisA I've tried re.sub('@\S\s[1-9]:[A-N]:[0-9]', '@\S\s', '@HWI-ST115:568:B08LLABXX: 1:1105:6465:151103 1:N:0:') but it does not seems to work. From jason at powerpull.net Mon Dec 26 19:16:51 2011 From: jason at powerpull.net (Jason Friedman) Date: Tue, 27 Dec 2011 00:16:51 +0000 Subject: Regular expressions In-Reply-To: References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Message-ID: > On Tue, Dec 27, 2011 at 10:45 AM, mauriceling at acm.org > wrote: >> Hi >> >> I am trying to change to . >> >> Can anyone help me with the regular expressions needed? > > A regular expression defines a string based on rules. Without seeing a > lot more strings, we can't know what possibilities there are for each > part of the string. You probably know your data better than we ever > will, even eyeballing the entire set of strings; just write down, in > order, what the pieces ought to be - for instance, the first token > might be a literal @ sign, followed by three upper-case letters, then > a hyphen, then any number of alphanumerics followed by a colon, etc. > Once you have that, it's fairly straightforward to translate that into > regex syntax. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list The OP told me, off list, that my guess was true: > Can we say that your string: > 1) Contains 7 colon-delimited fields, followed by > 2) whitespace, followed by > 3) 3 colon-delimited fields (A, B, C), followed by > 4) a colon? > The transformation needed is that the whitespace is replaced by a > slash, the "A" characters are taken as is, and the colons and fields > following the "A" characters are eliminated? Doubtful that my guess was 100% accurate, but nevertheless: >>> import re >>> string1 = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N:0:" >>> re.sub(r"(\S+)\s+(\S+?):.+", "\g<1>/\g<2>", string1) '@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1' From mauriceling at gmail.com Mon Dec 26 19:24:35 2011 From: mauriceling at gmail.com (mauriceling@acm.org) Date: Mon, 26 Dec 2011 16:24:35 -0800 (PST) Subject: Regular expressions References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> Message-ID: <34666b77-9e7a-4093-b7c4-74adeceb6555@l18g2000pro.googlegroups.com> On Dec 27, 8:16?am, Jason Friedman wrote: > > On Tue, Dec 27, 2011 at 10:45 AM, mauricel... at acm.org > > wrote: > >> Hi > > >> I am trying to change to . > > >> Can anyone help me with the regular expressions needed? > > > A regular expression defines a string based on rules. Without seeing a > > lot more strings, we can't know what possibilities there are for each > > part of the string. You probably know your data better than we ever > > will, even eyeballing the entire set of strings; just write down, in > > order, what the pieces ought to be - for instance, the first token > > might be a literal @ sign, followed by three upper-case letters, then > > a hyphen, then any number of alphanumerics followed by a colon, etc. > > Once you have that, it's fairly straightforward to translate that into > > regex syntax. > > > ChrisA > > -- > >http://mail.python.org/mailman/listinfo/python-list > > The OP told me, off list, that my guess was true: > > > Can we say that your string: > > 1) Contains 7 colon-delimited fields, followed by > > 2) whitespace, followed by > > 3) 3 colon-delimited fields (A, B, C), followed by > > 4) a colon? > > The transformation needed is that the whitespace is replaced by a > > slash, the "A" characters are taken as is, and the colons and fields > > following the "A" characters are eliminated? > > Doubtful that my guess was 100% accurate, but nevertheless: > > >>> import re > >>> string1 = "@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N:0:" > >>> re.sub(r"(\S+)\s+(\S+?):.+", "\g<1>/\g<2>", string1) > > '@HWI-ST115:568:B08LLABXX:1:1105:6465:151103/1' Thanks a lot everyone. Can anyone suggest a good place to learn REs? ML From rosuav at gmail.com Mon Dec 26 19:52:41 2011 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 27 Dec 2011 11:52:41 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <5aadbf8b-c905-4115-972c-b37d093126b4@p4g2000vbt.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> <5aadbf8b-c905-4115-972c-b37d093126b4@p4g2000vbt.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 10:44 AM, Eelco wrote: > extended collection unpacking, as in 'head,*tail=sequence', is quite a > rare construct indeed, and here I very strongly feel a more explicit > syntax is preferrable. You may be right, but... > ... if collection packing/unpacking would be > presented as a more general construct from the start, > 'head,tail::tuple=sequence' would be hard to miss. ... it doesn't really justify a _change_. When a language is in its infancy and the only code written in it is on the designers' own computers, these sorts of debates can be tipped by relatively small differences - is it more readable, is it quick enough to type, etc. But once a language reaches a level of maturity, changes need to overwhelm the "but it's a change" hurdle - breaking existing code is majorly dangerous, and keeping two distinct ways of doing something means you get the worst of both worlds. We can argue till the cows come home as to which way would be better, _had Python started with it_. I don't think there's anything like enough difference to justify the breakage/duplication. ChrisA From steve+comp.lang.python at pearwood.info Mon Dec 26 20:10:19 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Dec 2011 01:10:19 GMT Subject: Possible bug in string handling (with kludgy work-around) References: Message-ID: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 14:23:03 -0800, Charles Hixson wrote: > This doesn't cause a crash, but rather incorrect results. Charles, your code is badly formatted and virtually unreadable. You have four spaces between some tokens, lines are too long to fit in an email or News post without word-wrapping. It is a mess of unidiomatic code filled with repeated indexing and unnecessary backslash escapes. You also don't tell us what result you expect, or what result you actually get. What is the intention of the code? What are you trying to do, and what happens instead? The code as given doesn't run -- what's self? Despite all these problems, I can see one obvious problem in your code: you test to see if self.wordList[i] is a string, and if not, you replace the *entire* wordList with the empty string. That is unlikely to do what you want, although I admit I'm guessing what you are trying to do (since you don't tell us). Some hints for you: (1) Python has two string delimiters, " and ' and you should use them both. Instead of hard-to-read backslash escapes, just swap delimiters: print "A string including a \" quote mark." # No! print 'A string including a " quote mark.' # Yes, much easier to read. The only time you should backslash-escape a quotation mark is if you need to include both sorts in a single string: print "Python has both single ' and double \" quotation marks." print 'Python has both single \' and double " quotation marks.' (2) Python is not Pascal, or whatever language you seem to be writing in the style of. You almost never should write for-loops like this: for i in range(len(something)): print something[i] Instead, you should just iterate over "something" directly: for obj in something: print obj If you also need the index, use the enumerate function: for i,obj in enumerate(something): print obj, i If you are forced to use an ancient version of Python without enumerate, do yourself a favour and write your loops like this: for i in range(len(something)): obj = something[i] print obj, i instead of repeatedly indexing the list over and over and over and over again, as you do in your own code. The use of a temporary variable makes the code much easier to read and understand. -- Steven From jason at powerpull.net Mon Dec 26 20:26:04 2011 From: jason at powerpull.net (Jason Friedman) Date: Tue, 27 Dec 2011 01:26:04 +0000 Subject: Regular expressions In-Reply-To: <34666b77-9e7a-4093-b7c4-74adeceb6555@l18g2000pro.googlegroups.com> References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> <34666b77-9e7a-4093-b7c4-74adeceb6555@l18g2000pro.googlegroups.com> Message-ID: > Thanks a lot everyone. > > Can anyone suggest a good place to learn REs? Start with the manual: http://docs.python.org/py3k/library/re.html#module-re From fredrik at dolda2000.com Mon Dec 26 20:43:56 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 02:43:56 +0100 (CET) Subject: Daemon management Message-ID: Dear list, Lately, I've had a personal itch to scratch, in that I run a couple of Python programs as daemons, and sometimes want to inspect or alter them in ad-hoc ways, or other times need to do things to them that are less ad-hoc in nature, but nevertheless lack a natural user interface. In order to solve that problem, I wrote a small library to allow the daemon to simply listen to some socket and accept arbitrary, but easily definable, commands. It also provides a "remote REPL" to allow me to run arbitrary Python code interactively in the context of the daemon. I was actually a bit surprised that I couldn't find any obvious existing solution to the problem, so I'm writing this message in order to share mine, just in case anyone else would happen to have the same problem as I had and doesn't want to reinvent the wheel yet again: Humbly, Fredrik Tolf From rich at noir.com Mon Dec 26 23:28:26 2011 From: rich at noir.com (K. Richard Pixley) Date: Mon, 26 Dec 2011 20:28:26 -0800 Subject: confused about __new__ Message-ID: I'm confused about the following. The idea here is that the set of instances of some class are small and finite, so I'd like to create them at class creation time, then hijack __new__ to simply return one of the preexisting classes instead of creating a new one each call. This seems to work in python3, but fails in python2 with: Foo(23) TypeError: unbound method __Foo__new__() must be called with Foo instance as first argument (got type instance instead) I don't understand. Can anyone explain? --rich -------------- next part -------------- A non-text attachment was scrubbed... Name: tst.py Type: text/x-python-script Size: 236 bytes Desc: not available URL: From steve+comp.lang.python at pearwood.info Mon Dec 26 23:53:31 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Dec 2011 04:53:31 GMT Subject: confused about __new__ References: Message-ID: <4ef94f4b$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 20:28:26 -0800, K. Richard Pixley wrote: > I'm confused about the following. The idea here is that the set of > instances of some class are small and finite, so I'd like to create them > at class creation time, then hijack __new__ to simply return one of the > preexisting classes instead of creating a new one each call. I'm surprised it works in Python3. Try this in Python 2 instead: Foo.__new__ = staticmethod(__Foo__new__) -- Steven From fredrik at dolda2000.com Tue Dec 27 00:01:35 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 06:01:35 +0100 (CET) Subject: Regular expressions In-Reply-To: <4be34afe-4291-414b-9212-498074400e39@v24g2000prn.googlegroups.com> References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> <4be34afe-4291-414b-9212-498074400e39@v24g2000prn.googlegroups.com> Message-ID: On Mon, 26 Dec 2011, mauriceling at acm.org wrote: > I've tried > > re.sub('@\S\s[1-9]:[A-N]:[0-9]', '@\S\s', '@HWI-ST115:568:B08LLABXX: > 1:1105:6465:151103 1:N:0:') > > but it does not seems to work. Indeed, for several reasons. First of all, your backslash sequences are interpreted by Python as string escapes. You'll need to write either "\\S" or r"\S" (the r, for raw, turns off backslash escapes). Second, when you use only "\S", that matches a single non-space character, not several; you'll need to quantify them. "\S*" will match zero or more, "\S+" will match one or more, "\S?" will match zero or one, and there are a couple of other possibilities as well (see the manual for details). In this case, you probably want to use "+" for most of those. Third, you're not marking the groups that you want to use in the replacement. Since you want to retain the entire string before the space, and the numeric element, you'll want to enclose them in parentheses to mark them as groups. Fourth, your replacement string is entirely wacky. You don't use sequences such as "\S" and "\s" to refer back to groups in the original text, but numbered references, to refer back to parenthesized groups in the order they appear in the regex. In accordance what you seemed to want, you should probably use "@\1/\2" in your case ("\1" refers back to the first parentesized group, which you be the first "\S+" part, and "\2" to the second group, which should be the "[1-9]+" part; the at-mark and slash are inserted as they are into the result string). Fifth, you'll probably want to match the last colon as well, in order not to retain it into the result string. All in all, you will probably want to use something like this to correct that regex: re.sub(r'@(\S+)\s([1-9]+):[A-N]+:[0-9]+:', r'@\1/\2', '@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N:0:') Also, you may be interested to know that you can use "\d" instead of "[0-9]". -- Fredrik Tolf From briskb5 at gmail.com Tue Dec 27 00:20:37 2011 From: briskb5 at gmail.com (briskb5 briskb5) Date: Mon, 26 Dec 2011 21:20:37 -0800 (PST) Subject: Best Online Jobs Message-ID: <97b3dc84-837b-4db5-8060-24bc981354c9@h37g2000pri.googlegroups.com> Best Online Jobs http://gsonlinejobs.yolasite.com/ From ian.g.kelly at gmail.com Tue Dec 27 00:21:11 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 26 Dec 2011 22:21:11 -0700 Subject: Multithreading In-Reply-To: <560bea15-6abc-45ed-ac7a-cf23ff44f130@e2g2000vbb.googlegroups.com> References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> <560bea15-6abc-45ed-ac7a-cf23ff44f130@e2g2000vbb.googlegroups.com> Message-ID: On Dec 26, 2011 4:13 PM, "Yigit Turgut" wrote: > Why is there N variable in write_data function ? N is related to > timer.tick(N) which is related to display function ? time.sleep(N) > will pause writing to file for specified amount of time which is > exactly what I am trying to avoid. My understanding from your first post was that the pygame loop was supposed to run at X FPS while the other loop was supposed to run every N seconds. If that is not the case and the data loop is supposed to run as fast as possible, then you don't need threads for this. Just stream the data in a tight loop, and on each iteration check the timer for how much time has elapsed to determine whether to run the pygame code without using timer.tick. If not enough time has elapsed for your target framerate, just skip that part. -------------- next part -------------- An HTML attachment was scrubbed... URL: From Rachelhere33 at yahoo.com Tue Dec 27 00:22:18 2011 From: Rachelhere33 at yahoo.com (Rachelhere33 at yahoo.com) Date: Mon, 26 Dec 2011 21:22:18 -0800 (PST) Subject: =?UTF-8?B?V2lsbCBUaGUgRWFydGggRW5kIGJlIGluIDIwMTI/4p2k4p2k4p2k4p2k4p2k4p2k4p2k?= =?UTF-8?B?4p2k?= Message-ID: <4d705d3c-2e70-42c5-be0d-288deaeda833@p13g2000yqd.googlegroups.com> Will The Earth End be in 2012????????? Many experts from across the globe are predicting that the earth is likely to end by the year 2012. The reason could be a human effect or natural disaster while interpretations of the Christian Bible reveal that the date for Armageddon, the final battle between good an evil has been set for 2012. Active Address:http://www.2012ark.net/ Discuss Address:http://www.facebook.com/pages/2012ark-the-last-life- journey/292745100770702 We provide many free seats for you on Ark.Completely free for lives. Please Click "LIKE" and use your eyes to witness what will happen with us in 2012. From rich at noir.com Tue Dec 27 00:27:23 2011 From: rich at noir.com (K Richard Pixley) Date: Mon, 26 Dec 2011 21:27:23 -0800 Subject: confused about __new__ In-Reply-To: <4ef94f4b$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <4ef94f4b$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <%GcKq.36533$JE1.19080@newsfe21.iad> On 12/26/11 20:53 , Steven D'Aprano wrote: > On Mon, 26 Dec 2011 20:28:26 -0800, K. Richard Pixley wrote: > >> I'm confused about the following. The idea here is that the set of >> instances of some class are small and finite, so I'd like to create them >> at class creation time, then hijack __new__ to simply return one of the >> preexisting classes instead of creating a new one each call. > > I'm surprised it works in Python3. Try this in Python 2 instead: > > Foo.__new__ = staticmethod(__Foo__new__) Excellent. Thank you. --rich From fredrik at dolda2000.com Tue Dec 27 00:48:45 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 06:48:45 +0100 (CET) Subject: confused about __new__ In-Reply-To: References: Message-ID: On Mon, 26 Dec 2011, K. Richard Pixley wrote: > I don't understand. Can anyone explain? I'm also a bit confused about __new__. I'd very much appreciate it if someone could explain the following aspects of it: * The manual () says that __new__ is "a static method (special-cased so you need not declare it as such)". What does "special-cased" mean? Apparently, for instance, in OP's case, Python did not automatically detect that it should not be bound as a method. * Is there any part of the manual that explains, holistically, the greater context of object instantiation into which __new__ fits? I can only find small parts of it spread around the documentation for __new__ and __init__, but no complete explanation of it. There are several things I'm wondering about, like what it means to call a type object at all; how methods, properties and the like are bound; how pickle can instantiate a class without calling __init__; when and whether __dict__ is created and a couple of other things. Is there a complete documentation of the process anywhere that I haven't managed to find? -- Fredrik Tolf From briskbb3 at gmail.com Tue Dec 27 00:55:34 2011 From: briskbb3 at gmail.com (briskbb3 briskbb3) Date: Mon, 26 Dec 2011 21:55:34 -0800 (PST) Subject: Best Online Jobs Message-ID: Best Online Jobs http://gsonlinejobs.yolasite.com/ From carl.input at gmail.com Tue Dec 27 00:59:29 2011 From: carl.input at gmail.com (Carl Smith) Date: Mon, 26 Dec 2011 21:59:29 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> On Dec 20, 10:58?am, Andrea Crotti wrote: > On 12/20/2011 03:51 AM, Raymond Hettinger wrote: > > > > > > > > > > > Do you use IDLE when teaching Python? > > If not, what is the tool of choice? > > > Students may not be experienced with the command-line and may be > > running Windows, Linux, or Macs. ?Ideally, the tool or IDE will be > > easy to install and configure (startup directory, path, associated > > with a particular version of Python etc). > > > Though an Emacs user myself, I've been teaching with IDLE because it's > > free; it runs on multiple OSes, it has tooltips and code colorization > > and easy indent/dedent/comment/uncomment commands, it has tab > > completion; it allows easy editing at the interactive prompt; it has > > an easy run-script command (F5); it has direct access to source code > > (File OpenModule) and a class browser (Cntl+B). > > > On the downside, some python distros aren't built with the requisite > > Tcl/Tk support; some distros like the Mac OS ship with a broken Tcl/Tk > > so users have to install a fix to that as well; and IDLE sometimes > > just freezes for no reason. ?It also doesn't have an easy way to > > specify the startup directory. > > > If your goal is to quickly get new users up and running in Python, > > what IDE or editor do you recommend? > > > Raymond > > I think ipython and a good editor gives a much nicer experience > than IDLE, which I actually almost never used, and > for everything else there is python and python-mode. > > New users however can be pointed to something like PyCharm > or Eclipse+PyDev if they are more familiar to IDEs.. I agree; IPython is a excellent choice. You have a much more powerful interactive Python experience, with all the features you need from an IDE. You can use any editor (VIM) and you can also readily hack IPython to death. I think the fact that anyone with basic programming skills can substantially enhance their console is a big winner in CS education. It gives students something they personally value to work on, it's a place to store all their little bits of code and actually benefit from them in real life. I've never met a programmer that got familiar with IPython and then went on to stop using it. It should be included in the standard library and used as the default Python interactive environment. The last line of my .bashrc file: ipython3 From carl.input at gmail.com Tue Dec 27 01:14:42 2011 From: carl.input at gmail.com (Carl Smith) Date: Mon, 26 Dec 2011 22:14:42 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> Message-ID: On Dec 25, 5:44?pm, Rick Johnson wrote: > On Dec 19, 9:51?pm, Raymond Hettinger > wrote: > > > Do you use IDLE when teaching Python? > > If not, what is the tool of choice? > > I believe IDLE has the potential to be a very useful teaching tool and > even in it's current abysmal state, i find it to be quite useful. > > > Students may not be experienced with the command-line and may be > > running Windows, Linux, or Macs. ?Ideally, the tool or IDE will be > > easy to install and configure (startup directory, path, associated > > with a particular version of Python etc). > > Why install an IDE when IDLE is already there? Oh, yes, IDLE SUCKS. I > know that already. But this revelation begs the question... Why has > this community allowed IDLE to rot? Why has guido NOT started a public > discussion on the matter? > > > Though an Emacs user myself, I've been teaching with IDLE because it's > > free; it runs on multiple OSes, it has tooltips and code colorization > > and easy indent/dedent/comment/uncomment commands, it has tab > > completion; it allows easy editing at the interactive prompt; it has > > an easy run-script command (F5); it has direct access to source code > > (File OpenModule) and a class browser (Cntl+B). > > Yes, IDLE has all the basic tools anyone would need. Some people > complain about a debugger, but i never use a debugger anyway. I feel > debuggers just wreaken your debugging skills. > > > On the downside, some python distros aren't built with the requisite > > Tcl/Tk support; > > And who's fault is that? > > > some distros like the Mac OS ship with a broken Tcl/Tk > > so users have to install a fix to that as well; and IDLE sometimes > > just freezes for no reason. > > And who's fault is that? > > > ?[IDLE] also doesn't have an easy way to > > specify the startup directory. > > Are you kidding me? That could be fixed so easily! > > > If your goal is to quickly get new users up and running in Python, > > what IDE or editor do you recommend? > > IDLE, of course. But NOT in its current state. > > Why would myself (or anyone) go to the trouble of downloading third > party IDEs when IDLE is just waiting there for us to use? I for one, > like to use tools that have open source code. ?And what is a better > Python IDE than a Python IDE written in PYTHON? I ask ya? > > Also, what is the purpose of this thread Raymond? Are you (and others) > considering removing IDLE from the source distro? > > You know. Many folks in this community have known for a long time how > much i love IDLE, but at the same time how much i loath it's atrocious > code base. I also know for a fact, that many "movers and shakers" > within this community simultaneously use IDLE, and want to see IDLE > code improved. However. None of these fine folks have taken the time > to contact me privately so we can discuss such an evolution. Why is > that? It boggles the mind really. Do people seriously use IDLE? I thought it was just there for scratchers, like turtle. From tyler at tysdomain.com Tue Dec 27 01:26:39 2011 From: tyler at tysdomain.com (Littlefield, Tyler) Date: Mon, 26 Dec 2011 23:26:39 -0700 Subject: python logging module:a quick question Message-ID: <4EF9651F.7070806@tysdomain.com> Hello all: I have a basic server I am working on, and wanted some input with an error I'm getting. I am initializing the logger like so: if __name__ == "__main__": observer = log.PythonLoggingObserver() observer.start() logging.basicConfig(filename='logs/server.log', level=logging.DEBUG, format='%(asctime)s [%(levelname)s] %(module)s:%(funcname)s:%(lineno)d %(message)s') logger = logging.getLogger() logger.addHandler(logging.handlers.TimedRotatingFileHandler) ... I get the following error: File "/home/gserver/alpine/lib/python2.7/site-packages/twisted/internet/protoc ol.py", line 52, in doStart log.msg("Starting factory %r" % self) --- --- File "/home/gserver/alpine/lib/python2.7/site-packages/twisted/python/log.py", line 284, in msg self.observers[i](actualEventDict) File "/home/gserver/alpine/lib/python2.7/site-packages/twisted/python/log.py", line 532, in emit self.logger.log(level, text) File "/usr/local/lib/python2.7/logging/__init__.py", line 1195, in log self._log(level, msg, args, **kwargs) File "/usr/local/lib/python2.7/logging/__init__.py", line 1250, in _log self.handle(record) File "/usr/local/lib/python2.7/logging/__init__.py", line 1260, in handle self.callHandlers(record) File "/usr/local/lib/python2.7/logging/__init__.py", line 1299, in callHandler s if record.levelno >= hdlr.level: exceptions.AttributeError: type object 'TimedRotatingFileHandler' has no attribute 'level' I'm also curious how this rotating handler works. Will it just log to a file per day for a week, then start rotating those out with newer ones? Can I change the interval? -- Take care, Ty Web: http://tds-solutions.net The Aspen project: a light-weight barebones mud engine http://code.google.com/p/aspenmud Sent from my toaster. From nad at acm.org Tue Dec 27 01:46:57 2011 From: nad at acm.org (Ned Deily) Date: Tue, 27 Dec 2011 01:46:57 -0500 Subject: installing matplotlib in MacOs 10.6.8. References: Message-ID: In article , Alex Ter-Sarkissov wrote: > that's quite weird: if I run python2.7-32 in terminal, it works like you > said, but when I add it as an interpreter in Eclipse, matplotlib.pyplot > doesn't get imported for some reason. Even more strange, either way > platform.architecture() reports 64-bit. What's wrong here? platform.architecture() is not accurate for OS X 64-/32-bit builds. Use sys.maxsize instead. I have no experience with Eclipse so I really can't say how Eclipse might be launching the interpreter. Sorry! -- Ned Deily, nad at acm.org From ian.g.kelly at gmail.com Tue Dec 27 02:28:24 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 00:28:24 -0700 Subject: confused about __new__ In-Reply-To: References: Message-ID: On Mon, Dec 26, 2011 at 10:48 PM, Fredrik Tolf wrote: > I'm also a bit confused about __new__. I'd very much appreciate it if > someone could explain the following aspects of it: > > ?* The manual () says > ? that __new__ is "a static method (special-cased so you need not declare > ? it as such)". What does "special-cased" mean? Apparently, for > ? instance, in OP's case, ?Python did not automatically detect that it > ? should not be bound as a method. It apparently has to do with the class creation. For the special-casing to happen, the __new__ method has to be present when the class is created. If it is, then it automatically gets wrapped in a staticmethod. In the OP's case, he was adding the __new__ method after class creation, so the wrapping did not happen automatically. Compare: >>> def my_new(cls): return object.__new__(cls) ... >>> class Foo(object): ... __new__ = my_new ... >>> class Bar(object): pass ... >>> Bar.__new__ = my_new >>> Foo.__dict__['__new__'] >>> Bar.__dict__['__new__'] > ?* Is there any part of the manual that explains, holistically, the > ? greater context of object instantiation into which __new__ fits? I can > ? only find small parts of it spread around the documentation for __new__ > ? and __init__, but no complete explanation of it. There are several > ? things I'm wondering about, like what it means to call a type object at > ? all; I don't know of anything that organizes it that way specifically, but I believe the Data Model reference pretty much covers what you're looking for. From the type hierarchy, under "Callable Types": http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy """ Class Types Class types, or ?new-style classes,? are callable. These objects normally act as factories for new instances of themselves, but variations are possible for class types that override __new__(). The arguments of the call are passed to __new__() and, in the typical case, to __init__() to initialize the new instance. """ AFAIK, that's pretty much it. When a type is called, __new__ is called to create the new instance, and then __init__ is called to initialize it (if __new__ returned an instance of the type). > how methods, properties and the like are bound; When they're accessed, using the descriptor protocol, not as part of the instantiation process. See: http://docs.python.org/reference/datamodel.html#invoking-descriptors > how pickle can > ? instantiate a class without calling __init__; By calling the __new__ method directly instead of calling the type: http://docs.python.org/library/pickle.html#object.__getnewargs__ > when and whether __dict__ > ? is created and a couple of other things. Under the hood as part of the object creation process, unless the class uses __slots__. From lie.1296 at gmail.com Tue Dec 27 02:47:22 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 27 Dec 2011 18:47:22 +1100 Subject: confused about __new__ In-Reply-To: References: Message-ID: On 12/27/2011 04:48 PM, Fredrik Tolf wrote: > On Mon, 26 Dec 2011, K. Richard Pixley wrote: >> I don't understand. Can anyone explain? > > I'm also a bit confused about __new__. I'd very much appreciate it if > someone could explain the following aspects of it: > > * The manual () says > that __new__ is "a static method (special-cased so you need not declare > it as such)". What does "special-cased" mean? Apparently, for > instance, in OP's case, Python did not automatically detect that it > should not be bound as a method. If you declare new in the regular way: class Foo(object): def __new__(cls): ... Python would create __new__ as a static method even without applying the staticmethod decorator; that python does not detect to special case __new__ when it is added dynamically is probably an oversight in Python 2.x that was fixed in Python 3.x. From jobinchn at gmail.com Tue Dec 27 03:37:56 2011 From: jobinchn at gmail.com (earn 4 revenue) Date: Tue, 27 Dec 2011 00:37:56 -0800 (PST) Subject: Free Winning Forex course. Message-ID: Free Winning Forex course, Learn forex trading from A to Z with complete tutorials. And You Learn How Make Money in Forex. http://tiny.cc/dhuob From hoogendoorn.eelco at gmail.com Tue Dec 27 05:01:34 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 27 Dec 2011 02:01:34 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> <5aadbf8b-c905-4115-972c-b37d093126b4@p4g2000vbt.googlegroups.com> Message-ID: <1f1f2b88-a3f8-4fdb-be71-a3e5597aaf15@p42g2000vbt.googlegroups.com> On Dec 27, 1:52?am, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 10:44 AM, Eelco wrote: > > extended collection unpacking, as in 'head,*tail=sequence', is quite a > > rare construct indeed, and here I very strongly feel a more explicit > > syntax is preferrable. > > You may be right, but... > > > ... if collection packing/unpacking would be > > presented as a more general construct from the start, > > 'head,tail::tuple=sequence' would be hard to miss. > > ... it doesn't really justify a _change_. When a language is in its > infancy and the only code written in it is on the designers' own > computers, these sorts of debates can be tipped by relatively small > differences - is it more readable, is it quick enough to type, etc. > But once a language reaches a level of maturity, changes need to > overwhelm the "but it's a change" hurdle - breaking existing code is > majorly dangerous, and keeping two distinct ways of doing something > means you get the worst of both worlds. > > We can argue till the cows come home as to which way would be better, > _had Python started with it_. I don't think there's anything like > enough difference to justify the breakage/duplication. That I agree with; I think it is a questionable idea to introduce this in a new python 3 version. But I consider it a reasonable change for a 'python 4', or whatever the next major version change will be called. Writing a code-conversion tool to convert from *args to args::tuple would be quite easy indeed. From brisk67 at gmail.com Tue Dec 27 05:45:16 2011 From: brisk67 at gmail.com (brisk67 brisk67) Date: Tue, 27 Dec 2011 02:45:16 -0800 (PST) Subject: Best Online Jobs Message-ID: <0e290862-6fe8-471e-a87c-6411243c7f5c@g19g2000pri.googlegroups.com> Best Online Jobs http://gsonlinejobs.yolasite.com/ From lie.1296 at gmail.com Tue Dec 27 05:48:58 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 27 Dec 2011 21:48:58 +1100 Subject: python logging module:a quick question In-Reply-To: <4EF9651F.7070806@tysdomain.com> References: <4EF9651F.7070806@tysdomain.com> Message-ID: On 12/27/2011 05:26 PM, Littlefield, Tyler wrote: > Hello all: > I have a basic server I am working on, and wanted some input with an > error I'm getting. > I am initializing the logger like so: > if __name__ == "__main__": > observer = log.PythonLoggingObserver() > observer.start() > logging.basicConfig(filename='logs/server.log', level=logging.DEBUG, > format='%(asctime)s [%(levelname)s] %(module)s:%(funcname)s:%(lineno)d > %(message)s') > logger = logging.getLogger() > logger.addHandler(logging.handlers.TimedRotatingFileHandler) You should pass an **instance** of the handler, not the class; so it should look like this: logger.addHandler(logging.handlers.TimedRotatingFileHandler(...)) if you want to configure the handler, e.g. configure how often the log file is rotated, you can do it by passing parameters to the handler's constructure, e.g.: logger.addHandler(logging.handlers.TimedRotatingFileHandler(filename='/foo/mylog.log', when='d', interval=3, backupCount=5)) will create a handler that will log to the file /foo/mylog.log and rotate the log every 3 days and keeps the last 5 logs for backup. the logging handlers are documented over here: http://docs.python.org/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler Note that if you're using a recent enough version of python, it's also possible to configure the logging module using a dictionary-based configuration. Dictionary-based configuration is generally simpler than code-based configuration. From lie.1296 at gmail.com Tue Dec 27 05:54:57 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Tue, 27 Dec 2011 21:54:57 +1100 Subject: Plot seems weird In-Reply-To: <96ac870e-dffe-4556-aa3a-fab84333ceda@o9g2000vbc.googlegroups.com> References: <880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com> <4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com> <74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com> <96ac870e-dffe-4556-aa3a-fab84333ceda@o9g2000vbc.googlegroups.com> Message-ID: On 12/27/2011 06:14 AM, Yigit Turgut wrote: > On Dec 26, 8:58 pm, Lie Ryan wrote: >> On 12/27/2011 04:08 AM, Yigit Turgut wrote: >> >> not your fault, I made a mistake when copy-pasteing the code, here's the >> fixed code: >> >> from itertools import izip_longest >> def to_square(data): >> sq_data = [[], []] >> for x,y, xn in izip_longest(data[0], data[1], data[0][1:]): >> sq_data[0].append(x) >> sq_data[1].append(y) >> sq_data[0].append(xn) >> sq_data[1].append(y) >> return numpy.array(sq_data, dtype=float) >> >> use it like this: >> >> t,y1 = to_square(numpy.genfromtxt(filename, unpack=True)) >> pyplot.plot(t,y1) >> pyplot.show() > > Significant improvement on the plot, pretty interesting. It runs ok > but I need to know how?! (: it's pretty simple, actually; just observe the numbers before and after it's fixed by the function and it should be fairly obvious. From hoogendoorn.eelco at gmail.com Tue Dec 27 06:41:54 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 27 Dec 2011 03:41:54 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> Message-ID: On Dec 27, 6:59?am, Carl Smith wrote: > On Dec 20, 10:58?am, Andrea Crotti wrote: > > > > > > > > > > > On 12/20/2011 03:51 AM, Raymond Hettinger wrote: > > > > Do you use IDLE when teaching Python? > > > If not, what is the tool of choice? > > > > Students may not be experienced with the command-line and may be > > > running Windows, Linux, or Macs. ?Ideally, the tool or IDE will be > > > easy to install and configure (startup directory, path, associated > > > with a particular version of Python etc). > > > > Though an Emacs user myself, I've been teaching with IDLE because it's > > > free; it runs on multiple OSes, it has tooltips and code colorization > > > and easy indent/dedent/comment/uncomment commands, it has tab > > > completion; it allows easy editing at the interactive prompt; it has > > > an easy run-script command (F5); it has direct access to source code > > > (File OpenModule) and a class browser (Cntl+B). > > > > On the downside, some python distros aren't built with the requisite > > > Tcl/Tk support; some distros like the Mac OS ship with a broken Tcl/Tk > > > so users have to install a fix to that as well; and IDLE sometimes > > > just freezes for no reason. ?It also doesn't have an easy way to > > > specify the startup directory. > > > > If your goal is to quickly get new users up and running in Python, > > > what IDE or editor do you recommend? > > > > Raymond > > > I think ipython and a good editor gives a much nicer experience > > than IDLE, which I actually almost never used, and > > for everything else there is python and python-mode. > > > New users however can be pointed to something like PyCharm > > or Eclipse+PyDev if they are more familiar to IDEs.. > > I agree; IPython is a excellent choice. You have a much more powerful > interactive Python experience, with all the features you need from an > IDE. You can use any editor (VIM) and you can also readily hack > IPython to death. > > I think the fact that anyone with basic programming skills can > substantially enhance their console is a big winner in CS education. > It gives students something they personally value to work on, it's a > place to store all their little bits of code and actually benefit from > them in real life. > > I've never met a programmer that got familiar with IPython and then > went on to stop using it. It should be included in the standard > library and used as the default Python interactive environment. > > The last line of my .bashrc file: > > ipython3 Youve got one here. I like IPython a lot, but it quite rarely enters into my workflow. While I agree that a good interactive python console is a good way to get your feet wet with programming, I also strongly feel that a more comprehensive programming environment should be introduced to students. That includes opening and editing files, syntax highlighting, and code completion. And painless installation. There are no lightweight editors that provide all this functionality in conjuction with Ipython*. So I prefer to work the other way around; use something like pycharm, and open an IPython interactive session within it. *Your suggestion of VIM is especially objectionable. Though I am sure it is a great tool to you, the subject here is beginner education. Just because it is a good tool for you, does not make it a good tool for a beginner. IPython bundled with a lightweight but function-rich and non-hacker- but-WYSIWYG editor would be a great choice. But until that comes around, pycharm it is for me. From mjseger at gmail.com Tue Dec 27 08:32:38 2011 From: mjseger at gmail.com (Mark Seger) Date: Tue, 27 Dec 2011 08:32:38 -0500 Subject: Errors installing mod_python with apache Message-ID: I've tried this on both RHEL5.5 and RHEL 6.0, using the default apache that comes with the environment and itself isn't configured with mod_python. The first thing I noticed when mod_python wouldn't install was that apsx wasn't installed either. After a lot of pain, I discovered httpd-devel provided apsx and so installed that. Back to mod_python. After running ./configure --with-apxs=/usr/sbin/apxs, according to the README I need to run "make dso", but that promptly blows up because it's looking for header files in /usr/include/httpd and itlooks like the apxs files are in /usr/include/apr1, so I copied all its contents to /usr/include/httpd. Now when I try to make dso, it successfully gets past finding its header files but now fails with: [root at rhel6 mod_python-2.7.10]# make dso make[1]: Entering directory `/tmp/mod_python-2.7.10/src' gcc -I/tmp/mod_python-2.7.10/src/include -I/usr/include/httpd -I/usr/include/python2.6 -c -o mod_python.o mod_python.c In file included from /usr/include/python2.6/pyconfig.h:6, from /usr/include/python2.6/Python.h:8, from /tmp/mod_python-2.7.10/src/include/mod_python.h:77, from mod_python.c:54: /usr/include/python2.6/pyconfig-64.h:1031:1: warning: "_POSIX_C_SOURCE" redefined In file included from /usr/include/sys/types.h:26, from /usr/include/httpd/apr-x86_64.h:127, from /usr/include/httpd/apr.h:19, from /usr/include/httpd/ap_config.h:25, from /usr/include/httpd/httpd.h:43, from /tmp/mod_python-2.7.10/src/include/mod_python.h:63, from mod_python.c:54: /usr/include/features.h:213:1: warning: this is the location of the previous definition In file included from mod_python.c:54: /tmp/mod_python-2.7.10/src/include/mod_python.h:93: error: expected ?=?, ?,?, ?;?, ?asm? or ?__attribute__? before ?*? token /tmp/mod_python-2.7.10/src/include/mod_python.h:96: error: expected ?=?, ?,?, ?;?, ?asm? or ?__attribute__? before ?python_module? In file included from /tmp/mod_python-2.7.10/src/include/mod_python.h:99, from mod_python.c:54: /tmp/mod_python-2.7.10/src/include/util.h:57: error: expected ?;?, ?,? or ?)? before ?*? token In file included from /tmp/mod_python-2.7.10/src/include/mod_python.h:100, from mod_python.c:54: and a lot more. Can anyone help? -mark -------------- next part -------------- An HTML attachment was scrubbed... URL: From charleshixsn at earthlink.net Tue Dec 27 10:54:57 2011 From: charleshixsn at earthlink.net (Charles Hixson) Date: Tue, 27 Dec 2011 07:54:57 -0800 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: References: <4EF8F3C7.3010005@earthlink.net> Message-ID: <4EF9EA51.5090301@earthlink.net> That was it! Thanks. On 12/26/2011 02:44 PM, Chris Angelico wrote: > On Tue, Dec 27, 2011 at 9:23 AM, Charles Hixson > wrote: > >> This doesn't cause a crash, but rather incorrect results. >> > You may need to be a bit clearer. What line of code (or what > expression)? What did you expect to see, and what did you see? > > > From examining your code, I've come up with one most-likely scenario. > In Python, indexing is zero-based and, in effect, indexes the > boundaries between items rather than the items themselves. Referencing > string[-1] actually means asking for the boundary between the > second-last and last characters; using that as your end marker > actually trims off the last character. What you may want is simply: > > self.wordList[i][1:] > > which means "from character position 1 to the end of the string". > > Hope that helps! > > Chris Angelico > Sorry I didn't specify things more clearly. -- Charles Hixson From wolftracks at invalid.com Tue Dec 27 11:03:13 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 08:03:13 -0800 Subject: Which libraries for Python 2.5.2 Message-ID: I'm trying to restore Python 2.5.2 on an old XP PC for a particular application from 4-5 years ago that uses it . According to the latest manual on it, the following should be installed. python-2.5.2.msi PIL-1.1.6.win32-py2.5.exe numpy-1.1.0-win32-superpack-python2.5.exe matplotlib-0.98.1.win32-py2.5.exe When I install them, and try to run the app program, Sentinel.py, some part of matplotlib complains (error msgs) and the program quits. The program begins with: from Tkinter import * from numpy import * import Image import ImageChops import ImageTk import time import binascii import tkMessageBox import tkSimpleDialog from pylab import plot, xlabel, ylabel, title, show, xticks, bar I tried numpy-1.2.0 and matplotlib-0.98.3 and had the same difficulty. What are wiser choices? Here's the traceback. Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 1.2.2 ==== No Subprocess ==== >>> Traceback (most recent call last): File "C:\Sentinel\Sent_user-20080716.py", line 16, in (SEE THIS from the above list of imports, and the from)<*****-------< from pylab import plot, xlabel, ylabel, title, show, xticks, bar File "C:\Python25\lib\site-packages\pylab.py", line 1, in from matplotlib.pylab import * File "C:\Python25\lib\site-packages\matplotlib\pylab.py", line 206, in from matplotlib import mpl # pulls in most modules File "C:\Python25\lib\site-packages\matplotlib\mpl.py", line 1, in from matplotlib import artist File "C:\Python25\lib\site-packages\matplotlib\artist.py", line 4, in from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath File "C:\Python25\lib\site-packages\matplotlib\transforms.py", line 34, in from matplotlib._path import affine_transform ImportError: DLL load failed: The specified module could not be found. >>> From Boris.Yanovsky at va.gov Tue Dec 27 11:35:42 2011 From: Boris.Yanovsky at va.gov (Yanovsky, Boris, VHACIN) Date: Tue, 27 Dec 2011 11:35:42 -0500 Subject: PDF 508 Compliance with Python? Message-ID: Hello, I am new to Python and I hope my question makes sense. We are trying to make PDFs 508 Compliant, meaning that the objects within them have to be tagged. The problem is that we have hundreds, and potentially thousands of PDFs, since they are automated reports that we create on a regular basis. Is there a code or feature in Python that can read PDFs and tag the objects within them for this kind of compliance? Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rantingrickjohnson at gmail.com Tue Dec 27 11:37:00 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 08:37:00 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> Message-ID: <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> On Dec 27, 12:14?am, Carl Smith wrote: > Do people seriously use IDLE? I thought it was just there for > scratchers, like turtle. I know for a fact that many folks use IDLE, even some rather "well known" folks around here. The fact is, more people use IDLE than admit to using IDLE. Of course, out of respect, i will not mention their names myself. Hopefully they will chime in..? There is a stigma in this community towards IDLE. I am not sure exactly how it started, or on what logic (or lack thereof) it is based, but the stigma exists no doubt. I believe it may be just an extension of the TCL/TK stigma (since IDLE is coded using Tkinter). My logic is this: """ Including an IDE in the stdlib may have been a bad idea (although i understand and support Guido's original vision for IDLE). But since we do have it, we need to either MAINTAIN the package or REMOVE it. We cannot just stick our heads in the sand and ignore the elephant in the chicken coop. It's bad enough to bloat ANY stdlib with seldom used modules, but i dare say, it's far worse to bloat a library with seldom used modules that are poorly maintained! Every module in Python's stdlib is a testament to the skill and professionalism of this community as a whole. When a module looks or works badly, we ALL look and work badly.""" It's no big secret to anyone in this community that both Tkinter and IDLE are the red headed step children of Pythons stdlib. Very few people speak kindly of either package. I believe GvR still believes in the merits of both packages (psst: batteries included!) but he finds himself at odds with the elite at pydev. Hmm, Maybe he DOES want to remove them but fears loosing face...FYI: Guido IS is the original author of both Tkinter and IDLE... it was HIS idea after all. However, it is high time to re-ignite the original vision and form these packages into something we can be proud of -- OR -- cut our losses and remove them forever. This indecisiveness, collective bickering, and lack of true leadership is shameful. From lie.1296 at gmail.com Tue Dec 27 11:42:18 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 03:42:18 +1100 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/28/2011 03:03 AM, W. eWatson wrote: > Here's the traceback. The traceback seems to imply that matplotlib is not being installed properly. Have you tried uninstalling then reinstalling matplotlib? From ian.g.kelly at gmail.com Tue Dec 27 11:53:27 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 09:53:27 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On Tue, Dec 27, 2011 at 9:03 AM, W. eWatson wrote: > ?File "C:\Python25\lib\site-packages\matplotlib\transforms.py", line 34, in > > ? ?from matplotlib._path import affine_transform > ImportError: DLL load failed: The specified module could not be found. Do you not have the file C:\Python25\lib\site-packages\matplotlib\_path.pyd? It's in the installer archive, so you should be able to just open it up with a zip program and extract it manually. If that's missing, then there may be other things wrong with your installation, though, so I would recommend a full reinstall. From rami.chowdhury at gmail.com Tue Dec 27 12:02:06 2011 From: rami.chowdhury at gmail.com (Rami Chowdhury) Date: Tue, 27 Dec 2011 17:02:06 +0000 Subject: Errors installing mod_python with apache In-Reply-To: References: Message-ID: On Tue, Dec 27, 2011 at 13:32, Mark Seger wrote: > I've tried this on both RHEL5.5 and RHEL 6.0, using the default apache that > comes with the environment and itself isn't configured with mod_python. > > The first thing I noticed when mod_python wouldn't install was that apsx > wasn't installed either. ?After a lot of pain, I discovered httpd-devel > provided apsx and so installed that. > > Back to mod_python. ?After running ./configure?--with-apxs=/usr/sbin/apxs, > according to the README I need to run "make dso", but that promptly blows up > because it's looking for header files in /usr/include/httpd and itlooks like > the apxs files are in /usr/include/apr1, so I copied all its contents to > /usr/include/httpd. > > Now when I try to make dso, it successfully gets past finding its header > files but now fails with: > > [root at rhel6 mod_python-2.7.10]# ?make dso > make[1]: Entering directory `/tmp/mod_python-2.7.10/src' > gcc ?-I/tmp/mod_python-2.7.10/src/include -I/usr/include/httpd > -I/usr/include/python2.6 ? ?-c -o mod_python.o mod_python.c > In file included from /usr/include/python2.6/pyconfig.h:6, > ? ? ? ? ? ? ? ? ?from /usr/include/python2.6/Python.h:8, > ? ? ? ? ? ? ? ? ?from /tmp/mod_python-2.7.10/src/include/mod_python.h:77, > ? ? ? ? ? ? ? ? ?from mod_python.c:54: > /usr/include/python2.6/pyconfig-64.h:1031:1: warning: "_POSIX_C_SOURCE" > redefined > In file included from /usr/include/sys/types.h:26, > ? ? ? ? ? ? ? ? ?from /usr/include/httpd/apr-x86_64.h:127, > ? ? ? ? ? ? ? ? ?from /usr/include/httpd/apr.h:19, > ? ? ? ? ? ? ? ? ?from /usr/include/httpd/ap_config.h:25, > ? ? ? ? ? ? ? ? ?from /usr/include/httpd/httpd.h:43, > ? ? ? ? ? ? ? ? ?from /tmp/mod_python-2.7.10/src/include/mod_python.h:63, > ? ? ? ? ? ? ? ? ?from mod_python.c:54: > /usr/include/features.h:213:1: warning: this is the location of the previous > definition > In file included from mod_python.c:54: > /tmp/mod_python-2.7.10/src/include/mod_python.h:93: error: expected ?=?, > ?,?, ?;?, ?asm? or ?__attribute__? before ?*? token > /tmp/mod_python-2.7.10/src/include/mod_python.h:96: error: expected ?=?, > ?,?, ?;?, ?asm? or ?__attribute__? before ?python_module? > In file included from /tmp/mod_python-2.7.10/src/include/mod_python.h:99, > ? ? ? ? ? ? ? ? ?from mod_python.c:54: > /tmp/mod_python-2.7.10/src/include/util.h:57: error: expected ?;?, ?,? or > ?)? before ?*? token > In file included from /tmp/mod_python-2.7.10/src/include/mod_python.h:100, > ? ? ? ? ? ? ? ? ?from mod_python.c:54: > > and a lot more. ?Can anyone help? Is there a reason you're not using the system package manager? Does "yum install mod_python" not find anything? How about "yum provides */mod_python.so"? -- Rami Chowdhury "A mind all logic is like a knife all blade - it makes the hand bleed that uses it." -- Rabindranath Tagore +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 From mjseger at gmail.com Tue Dec 27 12:07:58 2011 From: mjseger at gmail.com (Mark Seger) Date: Tue, 27 Dec 2011 12:07:58 -0500 Subject: Errors installing mod_python with apache In-Reply-To: References: Message-ID: I did try yum and got this: [root at rhel53 tmp]# yum install mod_python Loaded plugins: rhnplugin, security This system is not registered with RHN. RHN support will be disabled. Setting up Install Process Parsing package install arguments No package mod_python available. Nothing to do after further digging around I did find rh's mod_python rpm so I installed than and now seem to be ok. nevertheless it still bothers me the 'standard' tarball install didn't work. but I guess I'll leave that to others to worry about. thanks -mark On Tue, Dec 27, 2011 at 12:02 PM, Rami Chowdhury wrote: > On Tue, Dec 27, 2011 at 13:32, Mark Seger wrote: > > I've tried this on both RHEL5.5 and RHEL 6.0, using the default apache > that > > comes with the environment and itself isn't configured with mod_python. > > > > The first thing I noticed when mod_python wouldn't install was that apsx > > wasn't installed either. After a lot of pain, I discovered httpd-devel > > provided apsx and so installed that. > > > > Back to mod_python. After running > ./configure --with-apxs=/usr/sbin/apxs, > > according to the README I need to run "make dso", but that promptly > blows up > > because it's looking for header files in /usr/include/httpd and itlooks > like > > the apxs files are in /usr/include/apr1, so I copied all its contents to > > /usr/include/httpd. > > > > Now when I try to make dso, it successfully gets past finding its header > > files but now fails with: > > > > [root at rhel6 mod_python-2.7.10]# make dso > > make[1]: Entering directory `/tmp/mod_python-2.7.10/src' > > gcc -I/tmp/mod_python-2.7.10/src/include -I/usr/include/httpd > > -I/usr/include/python2.6 -c -o mod_python.o mod_python.c > > In file included from /usr/include/python2.6/pyconfig.h:6, > > from /usr/include/python2.6/Python.h:8, > > from /tmp/mod_python-2.7.10/src/include/mod_python.h:77, > > from mod_python.c:54: > > /usr/include/python2.6/pyconfig-64.h:1031:1: warning: "_POSIX_C_SOURCE" > > redefined > > In file included from /usr/include/sys/types.h:26, > > from /usr/include/httpd/apr-x86_64.h:127, > > from /usr/include/httpd/apr.h:19, > > from /usr/include/httpd/ap_config.h:25, > > from /usr/include/httpd/httpd.h:43, > > from /tmp/mod_python-2.7.10/src/include/mod_python.h:63, > > from mod_python.c:54: > > /usr/include/features.h:213:1: warning: this is the location of the > previous > > definition > > In file included from mod_python.c:54: > > /tmp/mod_python-2.7.10/src/include/mod_python.h:93: error: expected ?=?, > > ?,?, ?;?, ?asm? or ?__attribute__? before ?*? token > > /tmp/mod_python-2.7.10/src/include/mod_python.h:96: error: expected ?=?, > > ?,?, ?;?, ?asm? or ?__attribute__? before ?python_module? > > In file included from /tmp/mod_python-2.7.10/src/include/mod_python.h:99, > > from mod_python.c:54: > > /tmp/mod_python-2.7.10/src/include/util.h:57: error: expected ?;?, ?,? or > > ?)? before ?*? token > > In file included from > /tmp/mod_python-2.7.10/src/include/mod_python.h:100, > > from mod_python.c:54: > > > > and a lot more. Can anyone help? > > Is there a reason you're not using the system package manager? Does > "yum install mod_python" not find anything? How about "yum provides > */mod_python.so"? > > > -- > Rami Chowdhury > "A mind all logic is like a knife all blade - it makes the hand bleed > that uses it." -- Rabindranath Tagore > +44-7581-430-517 / +1-408-597-7068 / +88-0189-245544 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lie.1296 at gmail.com Tue Dec 27 12:23:05 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 04:23:05 +1100 Subject: Daemon management In-Reply-To: References: Message-ID: On 12/27/2011 12:43 PM, Fredrik Tolf wrote: > Dear list, > > Lately, I've had a personal itch to scratch, in that I run a couple of > Python programs as daemons, and sometimes want to inspect or alter them > in ad-hoc ways, or other times need to do things to them that are less > ad-hoc in nature, but nevertheless lack a natural user interface. > > In order to solve that problem, I wrote a small library to allow the > daemon to simply listen to some socket and accept arbitrary, but easily > definable, commands. It also provides a "remote REPL" to allow me to run > arbitrary Python code interactively in the context of the daemon. > > I was actually a bit surprised that I couldn't find any obvious existing > solution to the problem, so I'm writing this message in order to share > mine, just in case anyone else would happen to have the same problem as > I had and doesn't want to reinvent the wheel yet again: This is possible through the use of a debugger. I've never used it, but I heard good thing of winpdb which has remote debugging. (http://winpdb.org/) Another tool that I've never used is rconsole, part of rfoo library, which appears to be similar to pdm; it is also intended for the same kind of problem, managing long-running-processes/daemons. From rich at noir.com Tue Dec 27 12:41:55 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 09:41:55 -0800 Subject: confused about __new__ In-Reply-To: References: Message-ID: On 12/26/11 21:48 , Fredrik Tolf wrote: > On Mon, 26 Dec 2011, K. Richard Pixley wrote: >> I don't understand. Can anyone explain? > > I'm also a bit confused about __new__. I'd very much appreciate it if > someone could explain the following aspects of it: > > * The manual () says > that __new__ is "a static method (special-cased so you need not declare > it as such)". What does "special-cased" mean? Apparently, for > instance, in OP's case, Python did not automatically detect that it > should not be bound as a method. > > * Is there any part of the manual that explains, holistically, the > greater context of object instantiation into which __new__ fits? I can > only find small parts of it spread around the documentation for __new__ > and __init__, but no complete explanation of it. There are several > things I'm wondering about, like what it means to call a type object at > all; how methods, properties and the like are bound; how pickle can > instantiate a class without calling __init__; when and whether __dict__ > is created and a couple of other things. Is there a complete > documentation of the process anywhere that I haven't managed to find? Everything I know about this stuff I got by reading the manual. Search for __init__ and you'll find a pretty complete description of how objects are created, how to perturb that process, how attributes are looked up, and how to perturb that process. At least, you do in both python 2 and python 3 current manuals, I think. The conceptual leap for me was in recognizing that a class is just an object. The best way, (imo, so far), to create a singleton in python is to use the class itself as the singleton rather than ever instantiating it. With a little work, you can even prevent it from ever being instantiated. The concept of a "factory" gets a little weird then, as a factory might return an instantiation of some class but it might instead return a class proper - perhaps even an automatically constructed class - which is a somewhat different sort of factory. Calling a type is a little bit different. Calling a class is how you initiate an instantiation of that class. Calling an instantiation leads to __call__, (which may or may not have much semantic meaning, depending on your class). It's the difference between C() and C()(). (And the analogy holds for other builtin types, I think). Super() is also fairly well documented. But __metaclass__ could use a few more examples, imo. I'm still not entirely clear on __metaclass__. As I understand it, (which may well be completely wrong), __metaclass_ is the means of perturbing the results of "isinstance". You can create, (or delete), inheritance arbitrarily, and without even requiring real, existing classes to do it. I'm not clear on why you'd want to do this, nor why __metaclass__ is a better mechanism than, say, any of the implementations of "interface" which have been done in various tool kits or "mixins", which all seem to be somewhat overlapping in functionality. --rich From wolftracks at invalid.com Tue Dec 27 12:47:53 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 09:47:53 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 8:42 AM, Lie Ryan wrote: > On 12/28/2011 03:03 AM, W. eWatson wrote: >> Here's the traceback. > > The traceback seems to imply that matplotlib is not being installed > properly. Have you tried uninstalling then reinstalling matplotlib? > I believe I have, but I'll give it another go. From lie.1296 at gmail.com Tue Dec 27 12:50:28 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 04:50:28 +1100 Subject: Python education survey In-Reply-To: <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> Message-ID: On 12/28/2011 03:37 AM, Rick Johnson wrote: > My logic is this: > """ Including an IDE in the stdlib may have been a bad idea (although > i understand and support Guido's original vision for IDLE). But since > we do have it, we need to either MAINTAIN the package or REMOVE it. We > cannot just stick our heads in the sand and ignore the elephant in the > chicken coop. It's bad enough to bloat ANY stdlib with seldom used > modules, but i dare say, it's far worse to bloat a library with seldom > used modules that are poorly maintained! Every module in Python's > stdlib is a testament to the skill and professionalism of this > community as a whole. When a module looks or works badly, we ALL look > and work badly.""" AFAICS, Python has a pretty good reputation even outside Python community. I haven't seen anyone looks at Python badly because of IDLE. > It's no big secret to anyone in this community that both Tkinter and > IDLE are the red headed step children of Pythons stdlib. Very few > people speak kindly of either package. I believe GvR still believes in > the merits of both packages (psst: batteries included!) but he finds > himself at odds with the elite at pydev. Hmm, Maybe he DOES want to > remove them but fears loosing face...FYI: Guido IS is the original > author of both Tkinter and IDLE... it was HIS idea after all. However, > it is high time to re-ignite the original vision and form these > packages into something we can be proud of -- OR -- cut our losses and > remove them forever. I hope you're not attempting to put words into his mouth, what you think of Guido's ideas is not necessarily Guido's ideas. In any case, IDLE is open source -- I don't know exactly what license it's under but I'd assume it's the same as Python -- and anyone in this list and outside this list -- including you -- can freely fork it and work on it to improve it. In case you haven't realised it, it is pretty much impossible for a large open source project to "die"; even if Guido decided to remove IDLE from the standard library, it's not unlikely that someone will fork it and maintain it as a third party application. From lie.1296 at gmail.com Tue Dec 27 12:53:19 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 04:53:19 +1100 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> Message-ID: On 12/27/2011 10:41 PM, Eelco wrote: > > *Your suggestion of VIM is especially objectionable. Though I am sure > it is a great tool to you, the subject here is beginner education. > Just because it is a good tool for you, does not make it a good tool > for a beginner. Before using VIM, I used to use gedit (and still do, though not as often now); I don't think I've ever had any problem with not using a full blown IDE with Python. I generally don't miss not using an IDE since Python doesn't have the tradition of using overly verbose names like in Java. I'm personally of the opinion that beginners generally should start with a simple programmer text editors (gedit is a good example). Firstly, you don't want to confuse beginners with IDE vs language features; secondly, at the size of the problem beginners typically had, they don't **need** 95% of those features; and if you teach beginners powerful IDE features too early, by the time their problem gets big enough that the IDE features would actually help, they'd already forgotten about them. > IPython bundled with a lightweight but function-rich and non-hacker- > but-WYSIWYG editor would be a great choice. But until that comes > around, pycharm it is for me. From dihedral88888 at googlemail.com Tue Dec 27 12:53:55 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 27 Dec 2011 09:53:55 -0800 (PST) Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> Message-ID: <25014920.545.1325008435089.JavaMail.geo-discussion-forums@prlu4> From rich at noir.com Tue Dec 27 12:59:35 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 09:59:35 -0800 Subject: Python education survey In-Reply-To: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: On 12/19/11 19:51 , Raymond Hettinger wrote: > Do you use IDLE when teaching Python? > If not, what is the tool of choice? > If your goal is to quickly get new users up and running in Python, > what IDE or editor do you recommend? I would: a) let the students pick their own editor. b) encourage emacs and use emacs as a reference editor. The problem is that IDLE is hard to set up. (I've never managed it and I'm a well seasoned veteran). And pretty much only good for python, I'd expect. You'd do better to encourage eclipse, but setting that up isn't trivial either. You could create your own distribution of eclipse, but then you have that "only useful for python" problem again. If students are going to go anywhere else after this class, they're going to need to either be able to learn to switch editors or find an editor they can use more generally. Everyone ends up writing some html eventually, for instance. Either way requires climbing a learning curve that would be difficult to justify for a single class. OTOH, there are binary emacs distributions for all systems you've mentioned. And they work. I'm an antimicrosoft bigot, but I think my answer is probably the same regardless of whether we know the OS the students will be using or not. --rich From wolftracks at invalid.com Tue Dec 27 13:04:37 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 10:04:37 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 8:53 AM, Ian Kelly wrote: > On Tue, Dec 27, 2011 at 9:03 AM, W. eWatson wrote: >> File "C:\Python25\lib\site-packages\matplotlib\transforms.py", line 34, in >> >> from matplotlib._path import affine_transform >> ImportError: DLL load failed: The specified module could not be found. > > Do you not have the file > C:\Python25\lib\site-packages\matplotlib\_path.pyd? It's in the > installer archive, so you should be able to just open it up with a zip > program and extract it manually. If that's missing, then there may be > other things wrong with your installation, though, so I would > recommend a full reinstall. The path exits. _path.pyd exists. From rantingrickjohnson at gmail.com Tue Dec 27 13:04:40 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 10:04:40 -0800 (PST) Subject: Possible bug in string handling (with kludgy work-around) References: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: -- Note: superfluous indention removed for clarity! -- On Dec 27, 8:53?am, Dennis Lee Bieber wrote: > You can get by without the backslash in this situation too, by using > triple quoting: I would not do that because: 1. Because Python already has TWO string literal delimiters (' and ") 2. Because triple quote string literals are SPECIFICALLY created to solve the "multi-line issue" 3. Because you can confuse the hell out of someone who is reading Python code and they may miss the true purpose of triple quotes in Python But this brings up a very important topic. Why do we even need triple quote string literals to span multiple lines? Good question, and one i have never really mused on until now. It's amazing how much BS we just accept blindly! WE DON'T NEED TRIPLE QUOTE STRINGS! What we need is single quote strings that span multiple lines and triple quotes then become superfluous! For the problem of embedding quotes in string literals, we should be using markup. A SIMPLISTIC MARKUP! " This is a multi line string with a single quote --> and a double quote --> . Here is an embedded newline --> . And a backspace . Now we can dispense with all the BS! " > I find """ clearer, ''' could be a " and ' > packed tightly in some fonts, "', whereas """ can only be one construct) Another reason to ONLY use fixed width font when viewing code! Why would you use ANY font that would obscure chars SO ubiquitous as " and '? From fredrik at dolda2000.com Tue Dec 27 13:05:04 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 19:05:04 +0100 (CET) Subject: confused about __new__ In-Reply-To: References: Message-ID: On Tue, 27 Dec 2011, Ian Kelly wrote: > On Mon, Dec 26, 2011 at 10:48 PM, Fredrik Tolf wrote: >> I'm also a bit confused about __new__. I'd very much appreciate it if >> someone could explain the following aspects of it: >> >> ?* The manual () says >> ? that __new__ is "a static method (special-cased so you need not declare >> ? it as such)". What does "special-cased" mean? Apparently, for >> ? instance, in OP's case, ?Python did not automatically detect that it >> ? should not be bound as a method. > > It apparently has to do with the class creation. For the > special-casing to happen, the __new__ method has to be present when > the class is created. If it is, then it automatically gets wrapped in > a staticmethod. In the OP's case, he was adding the __new__ method > after class creation, so the wrapping did not happen automatically. Hmm, thank you. After trying a couple of things, it appears that the reason OP's method worked in Python 3 is that __get__ on ordinary functions in Python 3 simply returns the function itself when called on type lookup, rather than Python 2's unbound method objects. > http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy > """ > Class Types > Class types, or ?new-style classes,? are callable. These objects > normally act as factories for new instances of themselves, but > variations are possible for class types that override __new__(). The > arguments of the call are passed to __new__() and, in the typical > case, to __init__() to initialize the new instance. > """ > > AFAIK, that's pretty much it. When a type is called, __new__ is > called to create the new instance, and then __init__ is called to > initialize it (if __new__ returned an instance of the type). Since that description doesn't include when __dict__ is created, it isn't complete, however. I guess it's safe to assume that __dict__ is created inside object.__new__, but that also leaves some things unclear; see below. >> how methods, properties and the like are bound; > > When they're accessed, using the descriptor protocol, not as part of > the instantiation process. See: > http://docs.python.org/reference/datamodel.html#invoking-descriptors Ah, sorry. I had read that, but I had mistakenly thought that the same instance method object was always returned, which would have implied that it had to be stored for the instance somewhere, at some point. I see now that that is not actually the case, however. That clears up a whole lot for me. Thanks! >> when and whether __dict__ >> ? is created and a couple of other things. > > Under the hood as part of the object creation process, unless the > class uses __slots__. And also unless the object created is of type `object' or any other built-in type, which leaves me wondering exactly under what circumstances __dict__ actually is created. Is it some kind of special case in object.__new__ for types created with the `class' statement? There also appear to be other special cases in object.__new__: >>> object.__new__(dict) Traceback (most recent call last): File "", line 1, in TypeError: object.__new__(dict) is not safe, use dict.__new__() I guess my question reduces into the following: What does object.__new__ actually do, and what other special cases does it implement? -- Fredrik Tolf From rantingrickjohnson at gmail.com Tue Dec 27 13:11:48 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 10:11:48 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> Message-ID: <76281802-e45b-4850-82be-06a51cde1b20@g41g2000yqa.googlegroups.com> On Dec 27, 11:50?am, Lie Ryan wrote: > In case you haven't realised it, it is pretty > much impossible for a large open source project to "die"; even if Guido > decided to remove IDLE from the standard library I don't remember stating that Python would die if IDLE was removed (not sure if you misunderstood me or you're just making a general statement???). My belief is that the atrocious state of IDLE and Tkinter's code bases are making us look bad as a community. And i can go either way on the issue; remove them both, or enrich them both. Either way we improve on the current situation. My point is that we CANNOT just ignore the issue. From rantingrickjohnson at gmail.com Tue Dec 27 13:21:54 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 10:21:54 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <54c9d146-eef9-43bb-9e0b-33f51e410954@t8g2000yqg.googlegroups.com> On Dec 27, 11:59?am, K Richard Pixley wrote: > The problem is that IDLE is hard to set up. ?(I've never managed it and > I'm a well seasoned veteran). Can you qualify that statement? Do you mean "difficult to set up on certain OS's"? Because for windows there is no difficulty. > And [IDLE is] pretty much only good for python, Yes, i will agree on that! We could create snap-ins for other languages, but, there are many good multi-language editors out there already. > I'd > expect. ?You'd do better to encourage eclipse, but setting that up isn't > trivial either. ?You could create your own distribution of eclipse, but > then you have that "only useful for python" problem again. Same boat, different lake. > If students are going to go anywhere else after this class, they're > going to need to either be able to learn to switch editors or find an > editor they can use more generally. Agreed! > ?Everyone ends up writing some html > eventually, for instance. most folks write more than just HTML! Ruby, Lisp, Perl, C, Java, etc. > ?Either way requires climbing a learning curve > that would be difficult to justify for a single class. Yes, i must agree that IDLE does not scale. It's true that IDLE is only good for Python. Could we make the IDLE usable for other languages? Yes, but what good is that going to do? IDLE is already slow due to Tkinter being slow, due to Python being slow... etc. I must admit that IDLE is only useful for complete beginners, those who code only in Python, or those who don't care about using a specific Python IDE. But that damn "batteries included" and CPFE keeps creeping up! What to do, what to do? From bahamutzero8825 at gmail.com Tue Dec 27 13:26:23 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 27 Dec 2011 12:26:23 -0600 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <4EFA0DCF.90001@gmail.com> On 12/27/2011 11:59 AM, K Richard Pixley wrote: > You'd do better to encourage eclipse, but setting that up isn't > trivial either. IIRC, all I had to do to set up PyDev was copy a URL to Eclipse's "Install New Software" wizard, and have Eclipse download and install it. Extra steps are needed if a different implementation of Python (e.g. Jython) is needed, but other than that, the user only needs to specify a couple options (e.g. Python grammar version) at project creation time. This assumes that Python is already installed, but why wouldn't it be? > You could create your own distribution of eclipse, but > then you have that "only useful for python" problem again. AFAIK, Eclipse should always be good for Java unless you do some serious hacking. > If students are going to go anywhere else after this class, they're > going to need to either be able to learn to switch editors or find an > editor they can use more generally. There are a ton of editors that have syntax highlighting and other little features for many languages. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From ian.g.kelly at gmail.com Tue Dec 27 13:28:42 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 11:28:42 -0700 Subject: confused about __new__ In-Reply-To: References: Message-ID: On Tue, Dec 27, 2011 at 10:41 AM, K Richard Pixley wrote: > The conceptual leap for me was in recognizing that a class is just an > object. ?The best way, (imo, so far), to create a singleton in python is to > use the class itself as the singleton rather than ever instantiating it. > ?With a little work, you can even prevent it from ever being instantiated. I don't think that's actually possible: Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> class Foo: ... def __new__(cls): ... raise TypeError ... def __init__(self): ... raise TypeError ... >>> type(object.__new__(Foo)) I prefer the Borg pattern for singletons myself. When you need a Borg instance, you just instantiate the class normally, do whatever you need with it, and then discard it normally. It doesn't matter how many instances there are (there can even be zero) because they all share identical state. It's better encapsulation too, since client code does not even need to know that the class is a singleton variant; that becomes an implementation detail. http://www.aleax.it/Python/5ep.html http://code.activestate.com/recipes/66531/ > But __metaclass__ could use a few more examples, imo. ?I'm still not > entirely clear on __metaclass__. ?As I understand it, (which may well be > completely wrong), __metaclass_ is the means of perturbing the results of > "isinstance". ?You can create, (or delete), inheritance arbitrarily, and > without even requiring real, existing classes to do it. A metaclass is the class of another class (which is normally `type` if no metaclass is specified). It's used to customize the way that the class is created, in the same way that the class can be used to customize the way instances are created (e.g. by overriding __new__). You could use it to mess with the class's base classes, although I think that usage is rare. It's more common to use it to automatically add or modify entries in the class dict. For example, Django models use a metaclass to collect all the fields and options declared for the model class and to setup a default manager for the model class. Note that many things that can be done with a metaclass can also be done with a class decorator. The main difference as I see it is that the metaclass is inherited by subclasses, whereas a decorator would need to be applied individually to each subclass. From wolftracks at invalid.com Tue Dec 27 13:29:12 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 10:29:12 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 8:42 AM, Lie Ryan wrote: > On 12/28/2011 03:03 AM, W. eWatson wrote: >> Here's the traceback. > > The traceback seems to imply that matplotlib is not being installed > properly. Have you tried uninstalling then reinstalling matplotlib? > I just did, and the results are this: ================= Traceback (most recent call last): File "C:\Sentinel\Sent_user-20080716.py", line 16, in from pylab import plot, xlabel, ylabel, title, show, xticks, bar File "C:\Python25\lib\site-packages\pylab.py", line 1, in from matplotlib.pylab import * File "C:\Python25\lib\site-packages\matplotlib\pylab.py", line 204, in from matplotlib import mpl # pulls in most modules File "C:\Python25\lib\site-packages\matplotlib\mpl.py", line 1, in from matplotlib import artist File "C:\Python25\lib\site-packages\matplotlib\artist.py", line 4, in from transforms import Bbox, IdentityTransform, TransformedBbox, TransformedPath File "C:\Python25\lib\site-packages\matplotlib\transforms.py", line 34, in from matplotlib._path import affine_transform ImportError: DLL load failed: The specified module could not be found. ===================== I'm suspicious of this line, and maybe even the app program. There may have been a change to the code that required the later two versions of numpy and matplotlib. In fact, I'm using the later version here, so I'll see if I can back up to the first Python app they produced. from pylab import plot, xlabel, ylabel, title, show, xticks, bar From lists at cheimes.de Tue Dec 27 13:36:12 2011 From: lists at cheimes.de (Christian Heimes) Date: Tue, 27 Dec 2011 19:36:12 +0100 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: Am 27.12.2011 17:03, schrieb W. eWatson: > from matplotlib._path import affine_transform > ImportError: DLL load failed: The specified module could not be found. You are missing one or more DLLs that is required to load the _path.pyd module. You can use http://www.dependencywalker.com/ to track down missing DLLs. Christian From miki.tebeka at gmail.com Tue Dec 27 14:01:02 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 27 Dec 2011 11:01:02 -0800 (PST) Subject: Daemon management In-Reply-To: References: Message-ID: <25792721.106.1325012462924.JavaMail.geo-discussion-forums@yqbl25> What was the term you used to search? Since http://pypi.python.org/pypi/ingress/0.1.1 seems to fit your description. From miki.tebeka at gmail.com Tue Dec 27 14:01:02 2011 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 27 Dec 2011 11:01:02 -0800 (PST) Subject: Daemon management In-Reply-To: References: Message-ID: <25792721.106.1325012462924.JavaMail.geo-discussion-forums@yqbl25> What was the term you used to search? Since http://pypi.python.org/pypi/ingress/0.1.1 seems to fit your description. From hoogendoorn.eelco at gmail.com Tue Dec 27 14:45:28 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 27 Dec 2011 11:45:28 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> Message-ID: On Dec 27, 6:53?pm, Lie Ryan wrote: > On 12/27/2011 10:41 PM, Eelco wrote: > > > > > *Your suggestion of VIM is especially objectionable. Though I am sure > > it is a great tool to you, the subject here is beginner education. > > Just because it is a good tool for you, does not make it a good tool > > for a beginner. > > Before using VIM, I used to use gedit (and still do, though not as often > now); I don't think I've ever had any problem with not using a full > blown IDE with Python. I generally don't miss not using an IDE since > Python doesn't have the tradition of using overly verbose names like in > Java. As for my personal use, I very much prefer an IDE. I hate having only crappy code completion, for starters, and I like a good integrated debugger. But then again, im spoiled I suppose coming from C#. On the other hand, ive worked for many years using a very minimal notepad +command line compilation setup as well. But I can very well imagine that people are perfectly happy with more hackerish tools. That is, once they have gotten past the learning curve. > I'm personally of the opinion that beginners generally should start with > a simple programmer text editors (gedit is a good example). Firstly, you > don't want to confuse beginners with IDE vs language features; secondly, > at the size of the problem beginners typically had, they don't **need** > 95% of those features; and if you teach beginners powerful IDE features > too early, by the time their problem gets big enough that the IDE > features would actually help, they'd already forgotten about them. A good IDE should get out of your way if you want it to. I like pycharm in this regard; click an x or two, and you are facing just your text editor and console/output window. Generally, I think a non-cluttered IDE is ideal for a beginner. You dont have to explain to them how to open a file, and if you tell them to hit the 'play' button to start running their code (not a hard concept to grasp or remember either) they are good to start hacking. Also, I think code completion is a blessing to beginning programmers. IPython is good in that regard; until you switch to editing files, where your choice is between notepad, or configuring an other editor you dont want your class to spend any time on, and youll end up with something that still doesnt do much more than syntax highlighting. From rantingrickjohnson at gmail.com Tue Dec 27 15:04:26 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 12:04:26 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> Message-ID: <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> On Dec 27, 1:45?pm, Eelco wrote: > On Dec 27, 6:53?pm, Lie Ryan wrote: > > On 12/27/2011 10:41 PM, Eelco wrote: > > Before using VIM, I used to use gedit Eelco, please don't get offended, but can you (and everyone else) stop using silly verbage like "used to", "use to", "suppose to", "hard" when you "difficult", and "pretty" when you mean "very". I find this verbiage to be quite ridiculous. In this case you could have simply said... """Before using VIM, I USED gedit.""" or if you want to stress that you don't use gedit anymore you could say... """ Previously i used gedit, but have since moved on to VIM.""" Thanks > As for my personal use, I very much prefer an IDE. I hate having only > crappy code completion Syntax highlight is important ESPECIALLY if you're multi-lingual. Sometimes when i switch between the many languages i know, the highlight of the syntax is the only blindingly apparent clue. Take Python and Ruby for example. > A good IDE should get out of your way if you want it to. I like > pycharm in this regard; click an x or two, and you are facing just > your text editor and console/output window. A "good IDE" is nothing more than an extension of a base text editor with options to switch on as little or as much IDE machinery as you like. Why would you have a text editor AND an IDE? When an IDE is just an intelligent texteditor? That is one thing that i find missing in IDLE; the ability to turn off certain things. With IDLE, it's all or nothing. > Generally, I think a non-cluttered IDE is ideal for a beginner. Agreed, see last response ^^^ > You > dont have to explain to them how to open a file, and if you tell them > to hit the 'play' button to start running their code (not a hard > concept to grasp or remember either) they are good to start hacking. I always though "run" was a perfect verb for "running" code... but who knows :) From wolftracks at invalid.com Tue Dec 27 15:27:07 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 12:27:07 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: ... > > I'm suspicious of this line, and maybe even the app program. There may > have been a change to the code that required the later two versions of > numpy and matplotlib. In fact, I'm using the later version here, so I'll > see if I can back up to the first Python app they produced. > > from pylab import plot, xlabel, ylabel, title, show, xticks, bar I found what I thought might be the original, but that got changed in a later version on July 16, 2008, which became what I continued to use. The libs are as in my first post. I looked at the original Python app, and it used Python 2.4.2 but sure doesn't use the same lib versions as the 2008 version. The puzzler for me is why I can find numpy and matplotlib on my PC slightly latter than shown in the first post. It's like another release of the app came later. Unfortunately, I'm stuck on that, since our sponsors seem to be out for the entire holiday season. From rich at noir.com Tue Dec 27 15:31:10 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 12:31:10 -0800 Subject: confused about __new__ In-Reply-To: References: Message-ID: <4EFA2B0E.2000506@noir.com> On 12/27/11 10:28 , Ian Kelly wrote: > On Tue, Dec 27, 2011 at 10:41 AM, K Richard Pixley wrote: >> The conceptual leap for me was in recognizing that a class is just an >> object. The best way, (imo, so far), to create a singleton in python is to >> use the class itself as the singleton rather than ever instantiating it. >> With a little work, you can even prevent it from ever being instantiated. > I don't think that's actually possible: > > Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> class Foo: > ... def __new__(cls): > ... raise TypeError > ... def __init__(self): > ... raise TypeError > ... >>>> type(object.__new__(Foo)) > Try: class Foo(object): def __new__(cls): return cls --rich From ian.g.kelly at gmail.com Tue Dec 27 15:34:08 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 13:34:08 -0700 Subject: confused about __new__ In-Reply-To: <4EFA2B0E.2000506@noir.com> References: <4EFA2B0E.2000506@noir.com> Message-ID: On Tue, Dec 27, 2011 at 1:31 PM, K Richard Pixley wrote: > On 12/27/11 10:28 , Ian Kelly wrote: >> >> On Tue, Dec 27, 2011 at 10:41 AM, K Richard Pixley ?wrote: >>> >>> The conceptual leap for me was in recognizing that a class is just an >>> object. ?The best way, (imo, so far), to create a singleton in python is >>> to >>> use the class itself as the singleton rather than ever instantiating it. >>> ?With a little work, you can even prevent it from ever being >>> instantiated. >> >> I don't think that's actually possible: >> >> Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit >> (Intel)] on win32 >> Type "help", "copyright", "credits" or "license" for more information. >>>>> >>>>> class Foo: >> >> ... ? ? def __new__(cls): >> ... ? ? ? ? raise TypeError >> ... ? ? def __init__(self): >> ... ? ? ? ? raise TypeError >> ... >>>>> >>>>> type(object.__new__(Foo)) >> >> > > > Try: > > class Foo(object): > ? ?def __new__(cls): > ? ? ? ?return cls Okay: Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> class Foo: ... def __new__(cls): ... return cls ... >>> f1 = object.__new__(Foo) >>> f2 = object.__new__(Foo) >>> type(f1), type(f2) (, ) >>> f1 is f2 False From rich at noir.com Tue Dec 27 15:39:13 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 12:39:13 -0800 Subject: Python education survey In-Reply-To: <54c9d146-eef9-43bb-9e0b-33f51e410954@t8g2000yqg.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <54c9d146-eef9-43bb-9e0b-33f51e410954@t8g2000yqg.googlegroups.com> Message-ID: On 12/27/11 10:21 , Rick Johnson wrote: > On Dec 27, 11:59 am, K Richard Pixley wrote: > >> The problem is that IDLE is hard to set up. (I've never managed it and >> I'm a well seasoned veteran). > > Can you qualify that statement? Do you mean "difficult to set up on > certain OS's"? Because for windows there is no difficulty. The distributed binaries haven't worked when I tried on any version of linux or macosx that I used. Attempting to build from source also failed. (I'm pretty much an anti-microsoft bigot). >> Everyone ends up writing some html >> eventually, for instance. > > most folks write more than just HTML! Ruby, Lisp, Perl, C, Java, etc. Dedicated computer folks and experimental tinkerers, yes, I concur. But even casual computer users eventually want to write html, or compose for a wiki and get annoyed when their 3 hours of work in a web browser test field are lost because they changed pages. --rich From lie.1296 at gmail.com Tue Dec 27 16:13:03 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 08:13:03 +1100 Subject: Python education survey In-Reply-To: <76281802-e45b-4850-82be-06a51cde1b20@g41g2000yqa.googlegroups.com> References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> <76281802-e45b-4850-82be-06a51cde1b20@g41g2000yqa.googlegroups.com> Message-ID: On 12/28/2011 05:11 AM, Rick Johnson wrote: > On Dec 27, 11:50 am, Lie Ryan wrote: >> In case you haven't realised it, it is pretty >> much impossible for a large open source project to "die"; even if Guido >> decided to remove IDLE from the standard library > > I don't remember stating that Python would die if IDLE was removed > (not sure if you misunderstood me or you're just making a general > statement???). My belief is that the atrocious state of IDLE and > Tkinter's code bases are making us look bad as a community. And i can > go either way on the issue; remove them both, or enrich them both. > Either way we improve on the current situation. My point is that we > CANNOT just ignore the issue. The point is, I didn't think it's such a pressing issue. I haven't seen anyone in or outside Python communities making their conclusion about Python based on IDLE. In any case, removing IDLE without a much better replacement is pretty much out of the question. If people installed Python in vanilla Windows install, they would only have Notepad to edit their code. From lie.1296 at gmail.com Tue Dec 27 16:23:02 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 28 Dec 2011 08:23:02 +1100 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: References: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/28/2011 05:04 AM, Rick Johnson wrote: > -- > Note: superfluous indention removed for clarity! > -- > > On Dec 27, 8:53 am, Dennis Lee Bieber wrote: >> You can get by without the backslash in this situation too, by using >> triple quoting: > > I would not do that because: > 1. Because Python already has TWO string literal delimiters (' and ") > 2. Because triple quote string literals are SPECIFICALLY created to > solve the "multi-line issue" > 3. Because you can confuse the hell out of someone who is reading > Python code and they may miss the true purpose of triple quotes in > Python > > But this brings up a very important topic. Why do we even need triple > quote string literals to span multiple lines? Good question, and one i > have never really mused on until now. It's amazing how much BS we just > accept blindly! WE DON'T NEED TRIPLE QUOTE STRINGS! What we need is > single quote strings that span multiple lines and triple quotes then > become superfluous! For the problem of embedding quotes in string > literals, we should be using markup. A SIMPLISTIC MARKUP! > > " This is a multi line > string with a single quote --> > and a double quote --> . Here is an > embedded newline --> . And a backspace. > > Now we can dispense with all the BS! > " Ok, you're trolling. From tjreedy at udel.edu Tue Dec 27 16:29:03 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 27 Dec 2011 16:29:03 -0500 Subject: PDF 508 Compliance with Python? In-Reply-To: References: Message-ID: On 12/27/2011 11:35 AM, Yanovsky, Boris, VHACIN wrote: > Hello, > > I am new to Python and I hope my question makes sense. We are trying to > make PDFs 508 Compliant, meaning that the objects within them have to be > tagged. The problem is that we have hundreds, and potentially thousands > of PDFs, since they are automated reports that we create on a regular basis. > > Is there a code or feature in Python that can read PDFs and tag the > objects within them for this kind of compliance? There are 3rd party python packages that can read and write .pdfs. Google 'Python pdf library' or just go to http://pypi.python.org/pypi?%3Aaction=search&term=pdf&submit=search Make sure you check out the one from ReportLab. I suspect at least one will do what you want. -- Terry Jan Reedy From ian.g.kelly at gmail.com Tue Dec 27 16:31:00 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 14:31:00 -0700 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <8bdab804-7fa7-4a2a-b8db-920c7c034752@32g2000yqp.googlegroups.com> <14358576-d102-4b96-aa90-3b63289be45b@h3g2000yqa.googlegroups.com> <76281802-e45b-4850-82be-06a51cde1b20@g41g2000yqa.googlegroups.com> Message-ID: On Tue, Dec 27, 2011 at 2:13 PM, Lie Ryan wrote: > In any case, removing IDLE without a much better replacement is pretty much > out of the question. If people installed Python in vanilla Windows install, > they would only have Notepad to edit their code. No no, Wordpad is much better for editing Python. It even supports (manual) syntax highlighting! And of course for the console lovers there is MS-DOS Edit. From wolftracks at invalid.com Tue Dec 27 16:35:15 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 13:35:15 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: I realized that I had a working copy of the app on another XP PC, so I looked at what I had installed for Python. It was not what I had posted the first time. There must have been some shift after the July 16, 2008 date. One lib that was missing was scipy. I just collected the three libs I saw there. numpy matplotlib scipy Scipy uses as its installer. scipy-0.6.0.win32-py2.5.exe I replaced numpy and matplotlib, and added scipy. I still get errors, but perhaps because the install order is now wrong. It was numpy matplotlib Does anyone know the right order? The complaint msgs were very close to what I posted before, and matplotlib was again the last of the bunch. DLL for it not found. Possibly I need the dependencywalker that Christian mentioned. From wolftracks at invalid.com Tue Dec 27 16:35:31 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 13:35:31 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 10:36 AM, Christian Heimes wrote: > Am 27.12.2011 17:03, schrieb W. eWatson: >> from matplotlib._path import affine_transform >> ImportError: DLL load failed: The specified module could not be found. > > You are missing one or more DLLs that is required to load the _path.pyd > module. You can use http://www.dependencywalker.com/ to track down > missing DLLs. > > Christian > OK, I installed it. Is there an easy way to go to track down this dll. There's a Help, but I can't print it from the PC I have it on. From tjreedy at udel.edu Tue Dec 27 16:38:58 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 27 Dec 2011 16:38:58 -0500 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: References: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/27/2011 1:04 PM, Rick Johnson wrote: > But this brings up a very important topic. Why do we even need triple > quote string literals to span multiple lines? Good question, and one i > have never really mused on until now. I have, and the reason I thought of is that people, including me, too ofter forget or accidentally fail to properly close a string literal, and type something like 'this is a fairly long single line string" and wonder why they get a syntax error lines later, or, in interactive mode, why the interpreter does not respond to a newline. Color coding editors make it easier to catch such errors, but they were less common in 1991. And there is still uncolored interactive mode. There may also be a technical reason as to how the lexer works. -- Terry Jan Reedy From hoogendoorn.eelco at gmail.com Tue Dec 27 16:44:14 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Tue, 27 Dec 2011 13:44:14 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> Message-ID: <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> On Dec 27, 9:04?pm, Rick Johnson wrote: > On Dec 27, 1:45?pm, Eelco wrote: > > > On Dec 27, 6:53?pm, Lie Ryan wrote: > > > On 12/27/2011 10:41 PM, Eelco wrote: > > > Before using VIM, I used to use gedit > > Eelco, please don't get offended, but can you (and everyone else) stop > using silly verbage like "used to", "use to", "suppose to", "hard" > when you "difficult", and "pretty" when you mean "very". I find this > verbiage to be quite ridiculous. In this case you could have simply > said... > > """Before using VIM, I USED gedit.""" > > or if you want to stress that you don't use gedit anymore you could > say... > > """ Previously i used gedit, but have since moved on to VIM.""" > > Thanks Despite the fact that you mis-attributed that quote to me, im going to be a little bit offended in the name of its actual author anyway. Thats a lot of words to waste on your linguistic preferences. Personally, I reserve the right to botch my non-native languages as much as I please. > > You > > dont have to explain to them how to open a file, and if you tell them > > to hit the 'play' button to start running their code (not a hard > > concept to grasp or remember either) they are good to start hacking. > > I always though "run" was a perfect verb for "running" code... but who > knows :) Im assuming the audience is familiar with an ipod, but not an IDE, or programming in general. To their eyes, it looks like a 'play' button; but yes, 'running' is what its called in my mind. From fredrik at dolda2000.com Tue Dec 27 16:46:27 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 22:46:27 +0100 (CET) Subject: Daemon management In-Reply-To: References: Message-ID: On Wed, 28 Dec 2011, Lie Ryan wrote: > On 12/27/2011 12:43 PM, Fredrik Tolf wrote: >> [...] > This is possible through the use of a debugger. I've never used it, but I > heard good thing of winpdb which has remote debugging. (http://winpdb.org/) Thanks, but not as long as the debugger freezes the program it attaches to. > Another tool that I've never used is rconsole, part of rfoo library, which > appears to be similar to pdm; it is also intended for the same kind of > problem, managing long-running-processes/daemons. Interesting. That does sound very similar to what I wanted, so it's kind of a shame that I didn't find it. If anything, it seems that PDM is a bit more flexible in that it can handle limited references to remote objects, event notifications from the server to the client, and has better support for handling both TCP and Unix sockets (the rconsole script, for instance, seems to lack support for connecting to Unix sockets), so I guess my time wasn't wasted. -- Fredrik Tolf From fredrik at dolda2000.com Tue Dec 27 16:52:01 2011 From: fredrik at dolda2000.com (Fredrik Tolf) Date: Tue, 27 Dec 2011 22:52:01 +0100 (CET) Subject: Daemon management In-Reply-To: <25792721.106.1325012462924.JavaMail.geo-discussion-forums@yqbl25> References: <25792721.106.1325012462924.JavaMail.geo-discussion-forums@yqbl25> Message-ID: On Tue, 27 Dec 2011, Miki Tebeka wrote: > What was the term you used to search? Since http://pypi.python.org/pypi/ingress/0.1.1 seems to fit your description. Not quite, though. It (AFAICT, at least) only provides the REPL part, with no way to construct a more program-friendly interface. -- Fredrik Tolf From candide at free.invalid Tue Dec 27 16:56:19 2011 From: candide at free.invalid (candide) Date: Tue, 27 Dec 2011 22:56:19 +0100 Subject: Type object returned by the re.compile function Message-ID: <4efa3f05$0$6546$426a74cc@news.free.fr> The Python 2.7 official documentation here: http://docs.python.org/library/re.html#re.compile doesn't specify the type object returned by the re.compiled function. According to the documentation, re.compile returns a "regular expression object". A regular expression object seems to be an instance of the class RegexObject. The documentation mentions this class here : http://docs.python.org/library/re.html#regular-expression-objects but i don't see any existing class with this name : >>> import re >>> re.RegexObject Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'RegexObject' >>> Actually, a regular expression object doesn't seem to be a class object : >>> import re >>> reo = re.compile('') >>> reo.__class__ Traceback (most recent call last): File "", line 1, in AttributeError: __class__ >>> but has attributes : >>> dir(reo) ['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner', 'search', 'split', 'sub', 'subn'] >>> I have the same problem with the match object : the doc mentions a re.MatchObject class but this class doesn't exist : >>> re.MatchObject Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'MatchObject' >>> Any clarification is welcome. From rich at noir.com Tue Dec 27 17:04:27 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 14:04:27 -0800 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: On 12/27/11 10:26 , Andrew Berg wrote: > On 12/27/2011 11:59 AM, K Richard Pixley wrote: >> You'd do better to encourage eclipse, but setting that up isn't >> trivial either. > IIRC, all I had to do to set up PyDev was copy a URL to Eclipse's > "Install New Software" wizard, and have Eclipse download and install it. > Extra steps are needed if a different implementation of Python (e.g. > Jython) is needed, but other than that, the user only needs to specify a > couple options (e.g. Python grammar version) at project creation time. > This assumes that Python is already installed, but why wouldn't it be? You still need to match versions of PyDev to versions of Eclipse to versions of operating system to versions of other eclipse plugins. I spent a few days trying to get it together once and came to the conclusion that it was a much bigger effort than I was willing to commit to. >> You could create your own distribution of eclipse, but >> then you have that "only useful for python" problem again. > AFAIK, Eclipse should always be good for Java unless you do some serious > hacking. Depends on which versions of eclipse, java, os, other plugins, etc. >> If students are going to go anywhere else after this class, they're >> going to need to either be able to learn to switch editors or find an >> editor they can use more generally. > There are a ton of editors that have syntax highlighting and other > little features for many languages. Exactly. My preference is emacs but I'll admit that the learning curve there is pretty high by today's standards. (Whether it's worth the effort is a debatable point.) There are certainly many others. --rich From rich at noir.com Tue Dec 27 17:19:44 2011 From: rich at noir.com (K Richard Pixley) Date: Tue, 27 Dec 2011 14:19:44 -0800 Subject: confused about __new__ In-Reply-To: References: <4EFA2B0E.2000506@noir.com> Message-ID: <5wrKq.39032$mJ.10777@newsfe10.iad> On 12/27/11 12:34 , Ian Kelly wrote: > On Tue, Dec 27, 2011 at 1:31 PM, K Richard Pixley wrote: >> On 12/27/11 10:28 , Ian Kelly wrote: >>> >>> On Tue, Dec 27, 2011 at 10:41 AM, K Richard Pixley wrote: >>>> >>>> The conceptual leap for me was in recognizing that a class is just an >>>> object. The best way, (imo, so far), to create a singleton in python is >>>> to >>>> use the class itself as the singleton rather than ever instantiating it. >>>> With a little work, you can even prevent it from ever being >>>> instantiated. >>> >>> I don't think that's actually possible: >>> >>> Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit >>> (Intel)] on win32 >>> Type "help", "copyright", "credits" or "license" for more information. >>>>>> >>>>>> class Foo: >>> >>> ... def __new__(cls): >>> ... raise TypeError >>> ... def __init__(self): >>> ... raise TypeError >>> ... >>>>>> >>>>>> type(object.__new__(Foo)) >>> >>> >> >> >> Try: >> >> class Foo(object): >> def __new__(cls): >> return cls > > > Okay: > > Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit > (Intel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> class Foo: > ... def __new__(cls): > ... return cls > ... >>>> f1 = object.__new__(Foo) >>>> f2 = object.__new__(Foo) >>>> type(f1), type(f2) > (,) >>>> f1 is f2 > False I'm not seeing why you're using "object.__new__". With Foo(), it seems fine. rich at fuji-land.noir.com> python Python 2.7.2 (default, Dec 12 2011, 13:05:49) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> class Foo(object): ... def __new__(cls): ... return cls ... >>> f1 = Foo() >>> f2 = Foo() >>> id(Foo) 4298515984 >>> id(f1) 4298515984 >>> id(f2) 4298515984 rich at fuji-land.noir.com> python3 Python 3.2.2 (default, Dec 26 2011, 15:03:08) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> class Foo(object): ... def __new__(cls): ... return cls ... >>> f1 = Foo() >>> f2 = Foo() >>> id(f1) 4298841008 >>> id(f2) 4298841008 >>> f1.stuff = True >>> f2.stuff True >>> id(Foo) 4298841008 Are you trying to demonstrate that I haven't prevented you from instantiating Foo? If so, then I will cede that point. I certainly don't know enough about python internals just now to even claim to be capable of protecting a class from a hostile user. My guess is that short of a privileged/unprivileged split, or castrating the interpreter and locking you into it, such a protection would not be possible. My point was that I can indeed intercept common and convenient usage to create a lovely singleton semantic. I can't force you to use it. (Nor do I have any motivation to so do.) --rich From malaclypse2 at gmail.com Tue Dec 27 17:29:25 2011 From: malaclypse2 at gmail.com (Jerry Hill) Date: Tue, 27 Dec 2011 17:29:25 -0500 Subject: Type object returned by the re.compile function In-Reply-To: <4efa3f05$0$6546$426a74cc@news.free.fr> References: <4efa3f05$0$6546$426a74cc@news.free.fr> Message-ID: On Tue, Dec 27, 2011 at 4:56 PM, candide wrote: >>>> import re >>>> reo = re.compile('') >>>> reo.__class__ > Traceback (most recent call last): > ?File "", line 1, in > AttributeError: __class__ >>>> I'm not going to comment on what type is returned from the various functions in the re module, mostly because all I have installed locally is 2.6, and I'm not sure if anything has changed in 2.7 or 3.2. Instead, I will recommend a different tool for your toolbox. Take a look at the type() builtin function ( http://docs.python.org/library/functions.html#type ). Particularly, instead of inspecting reo.__class__ in your example above, you can print out type(reo). At a guess, those objects are missing the __class__ attribute, possibly because they are old style classes. -- Jerry From ian.g.kelly at gmail.com Tue Dec 27 17:31:30 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 15:31:30 -0700 Subject: Type object returned by the re.compile function In-Reply-To: <4efa3f05$0$6546$426a74cc@news.free.fr> References: <4efa3f05$0$6546$426a74cc@news.free.fr> Message-ID: On Tue, Dec 27, 2011 at 2:56 PM, candide wrote: > The Python 2.7 official documentation here: > > http://docs.python.org/library/re.html#re.compile > > doesn't specify the type object returned by the re.compiled function. > According to the documentation, re.compile returns a "regular expression > object". > > A regular expression object seems to be an instance of the class > RegexObject. The documentation mentions this class here : > > http://docs.python.org/library/re.html#regular-expression-objects > > but i don't see any existing class with this name : Presumably if it's not in the module then it's not meant to be directly invoked. You should always use re.compile(). If you really want to assign the type a name, though: >>> import re >>> RegexObject = type(re.compile('')) >>> RegexObject Cheers, Ian From ian.g.kelly at gmail.com Tue Dec 27 17:43:29 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 15:43:29 -0700 Subject: confused about __new__ In-Reply-To: <5wrKq.39032$mJ.10777@newsfe10.iad> References: <4EFA2B0E.2000506@noir.com> <5wrKq.39032$mJ.10777@newsfe10.iad> Message-ID: On Tue, Dec 27, 2011 at 3:19 PM, K Richard Pixley wrote: > Are you trying to demonstrate that I haven't prevented you from > instantiating Foo? ?If so, then I will cede that point. ?I certainly don't > know enough about python internals just now to even claim to be capable of > protecting a class from a hostile user. ?My guess is that short of a > privileged/unprivileged split, or castrating the interpreter and locking you > into it, such a protection would not be possible. Yes, that was the point. I certainly don't think that anybody should ever be creating class instances by directly invoking object.__new__ unless they really, really know what they are doing. I was just pointing out that I don't think it's actually possible to prevent classes from being instantiated. > My point was that I can indeed intercept common and convenient usage to > create a lovely singleton semantic. ?I can't force you to use it. (Nor do I > have any motivation to so do.) Sure, I don't disagree with that. And the object.__new__ workaround is a general point that doesn't apply only to singletons. Any class that customizes its instantiation process can be messed up by doing that. From steve+comp.lang.python at pearwood.info Tue Dec 27 17:57:26 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Dec 2011 22:57:26 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <4ef757e3$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4efa4d55$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 13:41:34 -0800, Eelco wrote: > On Dec 25, 6:05?pm, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Sun, 25 Dec 2011 07:38:17 -0800, Eelco wrote: [...] >> > How is 'head, *tail = sequence' or semantically entirely >> > equivalently, 'head, tail::list = sequence' any different then? Of >> > course after interpretation/compilation, what it boils down to is >> > that we are constructing a list and binding it to the identifier >> > tail, but that is not how it is formulated in python as a language >> >> I'm afraid it is. >> >> Here's the definition of assignment in Python 3: >> http://docs.python.org/py3k/reference/simple_stmts.html#assignment- >> statements > > Que? You have claimed that "constructing a list and binding it to the identifier" is not how iterator unpacking is formulated in Python the language. But that is wrong. That *is* how iterator unpacking is formulated in Python the language. The reference manual goes into detail on how assignment is defined in Python. You should read it. > 'head, *tail = sequence' > > Is how one currently unpacks a head and tail in idiomatic python > > This is semantically equivalent to > > 'head = sequence[0]' > 'tail = list(sequence[1:])' There's a reason this feature is called *iterable* unpacking: it operates on any iterable object, not just indexable sequences. 'head, *tail = sequence' is not just semantically equivalent to, but *actually is* implemented as something very close to: temp = iter(sequence) head = next(temp) tail = list(temp) del temp Extended iterable unpacking, as in 'head, *middle, tail = sequence' is a little more complex, but otherwise the same: it operates using the iterator protocol, not indexing. I recommend you read the PEP, if you haven't already done so. http://www.python.org/dev/peps/pep-3132/ > But these forms are linguistically different, in too many different ways > to mention. How about mentioning even one way? Because I have no idea what differences you are referring to, or why you think they are important. >> > We dont have >> > something of the form 'tail = list_tail(sequence)'. >> >> I'm afraid we do. See the definition of assignment again. > > Que? Again, you make a claim about Python which is contradicted by the documented behaviour of the language. You claim that we DON'T have something of the form 'tail = list_tail(sequence)', but that is *exactly* what we DO have: extracting the tail from an iterator. Obviously there is no built-in function "list_tail" but we get the same effect by just using list() on an iterator after advancing past the first item. > My claim is that the two semantically identical formulations above do > not have isomorphic linguistic form. As far as I can make sense of your > words, you seem to be disputing this claim, but its a claim as much > worth debating as that the sun rises in the east. "Isomorphic linguistic form"? Are you referring to the idea from linguistics that there are analogies between the structure of phonic and semantic units? E.g. that the structures (phoneme, syllable, word) and (sememe, onomateme, sentence) are analogous. I don't see why this is relevant, or which units you are referring to -- the human-language description of what Python does, high-level Python language features, Python byte-code, or machine code. Not that it matters, but it is unlikely that any of those are isomorphic in the linguistic sense, and I am not making any general claim that they are. If not, I have no idea what you are getting at, except possibly trying to hide behind obfuscation. >> > Rather, we annotate >> > the identifier 'tail' with an attribute that unquestionably >> > destinates it to become a list*. It is no longer that 'tail' will >> > just take anything that pops out of the expression on the right hand >> > side; >> >> Of course it will. Python is a dynamically typed language. It doesn't >> suddenly develop static types to ensure that 'tail' becomes a list; >> 'tail' is bound to a list because that's what the assignment statement >> provides. > > How python accomplishes any of this under the hood is entirely > immaterial. The form is that of a compile-time type constraint, > regardless of whether the BDFL ever thought about it in these terms. Compile-time type constraints only have meaning in statically-typed languages. Otherwise, you are applying an analogy that simply doesn't fit, like claiming that the motion of paper airplanes and birds are equivalent just because in English we use the word "fly" to describe them both. The differences between what Python actually does and compile-time type- constraints are greater than the similarities. Similarities: (1) In both cases, 'tail' ends up as a list. Differences: (1) Compiler enforces the constraint that the identifier 'tail' is always associated with a list and will prevent any attempt to associate 'tail' with some value that is not a list. Python does nothing like this: the identifier 'tail' has no type restrictions at all, and can be used for any object. (2) Errors are detectable at compile-time with an error that halts compilation; Python detects errors at runtime with an exception that can be caught and by-passed without halting execution. (3) "Constraint" has the conventional meaning of a restriction, not a result; a type-constraint refers to a variable being prevented from being set to some other type, not that it merely becomes set to that type. For example, one doesn't refer to 'x = 1' as a type-constraint merely because x gets set to an int value; why do you insist that 'head, *tail = sequence' is a type-constraint merely because tail gets set to a list? >> > rather, >> > the semantics of what will go on at right hand side is coerced by the >> > constraint placed on 'tail'. >> >> But it isn't a constraint placed on 'tail'. It is a consequence of the >> definition of assignment in Python 3. 'tail' becomes bound to a list >> because that is what the assignment statement is defined to do in that >> circumstance, not because the identifier (symbol) 'tail' is constrained >> to only accept lists. 'tail' may not even exist before hand, so talking >> about constraints on 'tail' is an abuse of language, AS YOU AGREED >> ABOVE. > > 'tail' is (re)declared on the spot as a brand-new identifier (type > constraint included); whether it exists before has no significance > whatsoever, since python allows rebinding of identifiers. Given the following: tail = "beautiful red plumage" head, *tail = [1, 2, 3, 4] tail = 42 is it your option that all three instantiations of the *identifier* 'tail' (one in each line) are *different* identifiers? Otherwise, your statement about "brand new identifier" is simply wrong. If you allow that they are the same identifier with three different values (and three different types), then this completely destroys your argument that there is a constraint on the identifier 'tail'. First 'tail' is a string, then it is a list, then it is an int. If that is a type constraint (restriction) on 'tail', then constraint has no meaning. If you state that they are three different identifiers that just happen to be identical in every way that can be detected, then you are engaged in counting angels dancing on the head of pins. Given this idea that the middle 'tail' identifier is a different identifier from the other two, then I might accept that there *could* be a type-constraint on middle 'tail', at least in some implementation of Python that hasn't yet been created. But what a pointless argument to make. >> > *(I call that a 'type constraint', because that is what it literally >> > is; >> >> No. It is literally a name binding of a dynamically typed, >> unconstrained name to an object which happens to be a list. > > Let me take a step back and reflect on the form of the argument we are > having. I claim the object in front of us is a 'cube'. You deny this > claim, by countering that it is 'just a particular configuration of > atoms'. No no no, you have utterly misunderstood my argument. I'm not calling it a configuration of atoms. I'm calling it a pyramid, and pointing out that no matter how many times you state it is a cube, it actually has FOUR sides, not six, and NONE of the sides are square, so it can't possibly be a cube. > 'look at the definition!', you say; 'its just a list of coordinates, no > mention of cubes whatsoever.'. > > 'Look at the definition of a cube', I counter. 'This particular list of > coordinates happens to fit the definition, whether the BDFT intended to > or not'. But you are wrong. It does not fit the definition of a type-constraint, except in the vacuous sense where you declare that there is some invisible distinction between the identifier 'tail' in one statement and the identical identifier 'tail' in another statement. > You are correct, in the sense that I do not disagree that it is a > particular configuration of atoms. But if you had a better understanding > of cubes, youd realize it meets the definition; I might not share your deep and expert understanding of cubes, but I understand what "type-constraint" means, and I know what Python does in iterator unpacking, and I know that there is no sensible definition of type-constraint that applies to iterator unpacking in Python. > the fact that people > might not make a habit of pausing at this fact (there is generally > speaking not much of a point in doing so in this case), does not > diminish the validity of this perspective. But again, if this > perspective does not offer you anything useful, feel free not to share > in it. Quite frankly, I should just let you witter on about type-constraints, because (again, in my opinion) it destroys your credibility and will reduce even further the already minuscule chance that your proposal will be accepted. Since I'm against the idea, I should encourage you to describe Python's behaviour in terms that will all but guarantee others will dismiss you as knowing little or nothing about Python. [...] > Whatever the virtues of your critique of my proposal, you might end up > wasting less time doing so by reading a book or two on compiler theory. Perhaps you should spend more time considering the reality of how Python works and less time trying to force the triangular peg of Python concepts into the square hole of your ideas about theoretical compilers. -- Steven From bahamutzero8825 at gmail.com Tue Dec 27 17:57:41 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Tue, 27 Dec 2011 16:57:41 -0600 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <4EFA4D65.2060400@gmail.com> On 12/27/2011 4:04 PM, K Richard Pixley wrote: > You still need to match versions of PyDev to versions of Eclipse to > versions of operating system to versions of other eclipse plugins. I > spent a few days trying to get it together once and came to the > conclusion that it was a much bigger effort than I was willing to commit to. This is more of a package management issue than an Eclipse one. I had no problems getting the very latest stable versions of everything on Windows. I'm pretty sure most schools will be using Windows for workstations anyway. In any case, if Eclipse is impractical for whatever reason, obviously it would be wise to try something else. >>> You could create your own distribution of eclipse, but >>> then you have that "only useful for python" problem again. >> AFAIK, Eclipse should always be good for Java unless you do some serious >> hacking. > > Depends on which versions of eclipse, java, os, other plugins, etc. How so? AFAIK, Eclipse is almost (if not completely) unusable without Java. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From ian.g.kelly at gmail.com Tue Dec 27 17:58:58 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 15:58:58 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On Tue, Dec 27, 2011 at 2:35 PM, W. eWatson wrote: > I replaced numpy and matplotlib, and added scipy. I still get errors, but > perhaps because the install order is now wrong. It was > numpy > matplotlib > > Does anyone know the right order? The order (numpy, scipy, matplotlib) reflects the dependencies, but since the installers are just fancied-up self-extracting zips, I don't think it should matter. > OK, I installed it. Is there an easy way to go to track down this dll. > There's a Help, but I can't print it from the PC I have it on. It should be reasonably self-explanatory, I think. Just start Dependency Walker, open up the _path.pyd file, and it will show you the full dependency tree. Below that is a list of all the modules, and any missing dependencies should helpfully show up right at the top of the list. Note that missing delay-load dependencies (those with an hourglass icon) are not necessarily problems. Since the error occurs at load-time, you're looking for a missing module that would be loaded immediately. From rozelak at volny.cz Tue Dec 27 17:59:36 2011 From: rozelak at volny.cz (rozelak at volny.cz) Date: Tue, 27 Dec 2011 23:59:36 +0100 (CET) Subject: No subject Message-ID: Hallo, I have kind of special question when extening python with C++ implemented modules. I try to implement a class, behaving also like an array. And I need to implement slice-getters. I implemented PySequenceMethods.sq_slice to get "simple" slices like: myobj[x:y] It works perfectly, without problems. But now I have a problem when need to access the array with "special" slices, like: myobj[x:y:step] myobj[::-1] # to revert the array I would like to ask which method must be implemented to get this "special" slice getters. The "simple" slice PySequenceMethods.sq_slice getter accepts only two indexes - from, to, but not a step (which would solve the issue). Thank you very much for your ansver, best regards Dan T. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rozelak at volny.cz Tue Dec 27 18:02:16 2011 From: rozelak at volny.cz (rozelak at volny.cz) Date: Wed, 28 Dec 2011 00:02:16 +0100 (CET) Subject: =?iso-8859-2?Q?Slices_when_extending_python_with_C=2B=2B_?= Message-ID: Hallo, I have kind of special question when extening python with C++ implemented modules. I try to implement a class, behaving also like an array. And I need to implement slice-getters. I implemented PySequenceMethods.sq_slice to get "simple" slices like: myobj[x:y] It works perfectly, without problems. But now I have a problem when need to access the array with "special" slices, like: myobj[x:y:step] myobj[::-1] # to revert the array I would like to ask which method must be implemented to get this "special" slice getters. The "simple" slice PySequenceMethods.sq_slice getter accepts only two indexes - from, to, but not a step (which would solve the issue). Thank you very much for your ansver, best regards Dan T. -- Tradi?n? i modern? adventn? a novoro?n? zvyky, sv?te?n? j?dlo a pit?, v?zdoba a d?rky... - ?t?te v?no?n? a silvestrovsk? speci?l port?lu VOLN?.cz na http://web.volny.cz/data/click.php?id=1301 -------------- next part -------------- An HTML attachment was scrubbed... URL: From rozelak at volny.cz Tue Dec 27 18:03:08 2011 From: rozelak at volny.cz (rozelak at volny.cz) Date: Wed, 28 Dec 2011 00:03:08 +0100 (CET) Subject: =?iso-8859-2?Q?Slices_when_extending_python_with_C=2B=2B_?= Message-ID: <65f578abac07f7ad0d107c0f6bdcf951@mail1.volny.cz> Hallo, I have kind of special question when extening python with C++ implemented modules. I try to implement a class, behaving also like an array. And I need to implement slice-getters. I implemented PySequenceMethods.sq_slice to get "simple" slices like: myobj[x:y] It works perfectly, without problems. But now I have a problem when need to access the array with "special" slices, like: myobj[x:y:step] myobj[::-1] # to revert the array I would like to ask which method must be implemented to get this "special" slice getters. The "simple" slice PySequenceMethods.sq_slice getter accepts only two indexes - from, to, but not a step (which would solve the issue). Thank you very much for your ansver, best regards Dan T. -- Tradi?n? i modern? adventn? a novoro?n? zvyky, sv?te?n? j?dlo a pit?, v?zdoba a d?rky... - ?t?te v?no?n? a silvestrovsk? speci?l port?lu VOLN?.cz na http://web.volny.cz/data/click.php?id=1301 From steve+comp.lang.python at pearwood.info Tue Dec 27 18:07:23 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Dec 2011 23:07:23 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> Message-ID: <4efa4fab$0$29973$c3e8da3$5496439d@news.astraweb.com> On Mon, 26 Dec 2011 13:51:50 -0800, Eelco wrote: [...] >> If your point is that parens are used more often than >> packing/unpacking, that's almost certainly true, since function calls >> (including method invocations) are so prevalent in pretty much any >> code. But what does that prove? > > That proves the original point of contention: that the below* is > suboptimal language design, Well duh. I was mocking the idea that the meaning of * is context-dependent is a bad thing by pointing out that we accept context-dependent meaning for round brackets () without any difficulties. Of course it is "suboptimal language design" -- it couldn't fail to be. Context-dependency is not necessarily a bad thing. > not because terseness always trumps > verbosity, but because commonly-used constructs (such as parenthesis or > round brackets or whatever you wish to call them) Parentheses are not a construct. They are symbols (punctuation marks) which are applied to at least three different constructs: grouping, function calls, class inheritance lists. > are more deserving of > the limited space in both the ascii table and your reflexive memory, > than uncommonly used ones. Right. And since sequence packing and unpacking is a common idiom, it deserves to be given punctuation. That's my opinion. -- Steven From steve+comp.lang.python at pearwood.info Tue Dec 27 18:10:42 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 27 Dec 2011 23:10:42 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> On Sun, 25 Dec 2011 07:47:20 -0800, Eelco wrote: > Explicit and implicit are not well-defined terms, We can at least agree on that. > but I would say that > at the moment the signal is implicit, in the sense that one cannot see > what is going on by considering the rhs in isolation. That is a ridiculous argument. If you ignore half the statement, of course you can't tell what is going on. > Normally in > python, an assignment just binds the rhs to the identifiers on the lhs, > but in case of collection (un)packing, this rule that holds almost all > of the time is broken, and the assignment statement implies a far more > complicated construct, with a far more subtle meaning, and non-constant > time complexity. Python assignment in general is not constant time. If the left hand side is a single identifier (a name, a dotted attribute, a subscription or slice, etc.) then the assignment is arguably constant time (hand-waving away complications like attribute access, hash table collisions, descriptors, etc.); but if the left hand side is a list of targets, e.g.: a, b, c = sequence then the assignment is O(N). Python has to walk the sequence (actually, any iterator) and assign each of N items to N identifiers, hence O(N) not O(1). The trivial case of a single value on the left hand side: x = 1 is just the degenerate case for N=1. > Thats not a terrible thing, but a little extra explicitness there would > not hurt, I think it will. It makes the assignment more verbose to no benefit. > and like I argued many times before, it is a nice unification > with the situation where the unpacking can not be implicit, like inside > a function call rather than assignment. That's one opinion; but there's no need for any "extra explicitness" since the definition of assignment in Python already explicitly includes iterable unpacking. How much explicitness do you need? >> ? ? n = len(::sequence) > > Now you are just discrediting yourself in terms of having any idea what > you are talking about. ::sequence is redundant and unnecessary whether it is inside a function call to len or on the right hand side of an assignment. In both cases, it adds nothing to the code except noise. Your proposal is surreal. You started off this conversation arguing that the existing idiom for extended iterator unpacking, e.g. head, *tail = sequence is too hard to understand because it uses punctuation, and have ended up recommending that we replace it with: head, tail:: = ::sequence instead (in the generic case where you don't want to use a different type for tail). Your original use-case, where you want to change the type of tail from a list to something else, is simply solved by one extra line of code: head, *tail = sequence tail = tuple(tail) You have written thousands of words to save one line in an idiom which you claim is very uncommon. Perhaps your understanding of "pythonic" is not as good as you imagine. -- Steven From dihedral88888 at googlemail.com Tue Dec 27 18:45:06 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 27 Dec 2011 15:45:06 -0800 (PST) Subject: RSA and Cryptography in PYTHON In-Reply-To: <31261167.63.1324843108857.JavaMail.geo-discussion-forums@prdv33> References: <31261167.63.1324843108857.JavaMail.geo-discussion-forums@prdv33> Message-ID: <24097307.586.1325029506304.JavaMail.geo-discussion-forums@prmw6> 88888 Dihedral? 2011?12?26????UTC+8??3?58?28???? > Long integer is really excellent in Python. > > Encoding RSA 2048bits in a simple and elegant way in Python > is almost trivial. > > How about the nontrivial decoding part ? I am getting lousy in the news group in my writing? I mean the non-trivial decoding of the key decomposition. Of course RSA is symmetric in encoding and decoding of data. From ian.g.kelly at gmail.com Tue Dec 27 18:47:25 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 16:47:25 -0700 Subject: Slices when extending python with C++ In-Reply-To: <65f578abac07f7ad0d107c0f6bdcf951@mail1.volny.cz> References: <65f578abac07f7ad0d107c0f6bdcf951@mail1.volny.cz> Message-ID: 2011/12/27 : > Hallo, > I have kind of special question when extening python with C++ > implemented modules. > > I try to implement a class, behaving also like an array. And I need > to implement slice-getters. I implemented PySequenceMethods.sq_slice > to get "simple" slices like: > > myobj[x:y] > > It works perfectly, without problems. But now I have a problem when > need to access the array with "special" slices, like: > > myobj[x:y:step] > myobj[::-1] ?# to revert the array > > I would like to ask which method must be implemented to get this > "special" slice getters. The "simple" slice > PySequenceMethods.sq_slice getter accepts only two indexes - from, > to, but not a step (which would solve the issue). I'm not entirely certain about the C API, but in Python the __getslice__ and __setslice__ methods do not support extended slicing and have been deprecated and removed in Python 3. The practice instead is to implement __getitem__ and __setitem__ and check the type of the key object passed in. For slicing, the object passed in will be a slice object, which contains start, stop, and step attributes. For example: class MySequence(object): def __getitem__(self, key): if isinstance(key, slice): # return a subsequence using key.start, key.stop, key.step else: # return a single item indexed by key HTH, Ian From rantingrickjohnson at gmail.com Tue Dec 27 19:57:04 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 16:57:04 -0800 (PST) Subject: Possible bug in string handling (with kludgy work-around) References: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 27, 3:38?pm, Terry Reedy wrote: > On 12/27/2011 1:04 PM, Rick Johnson wrote: > > > But this brings up a very important topic. Why do we even need triple > > quote string literals to span multiple lines? Good question, and one i > > have never really mused on until now. > > I have, and the reason I thought of is that people, including me, too > ofter forget or accidentally fail to properly close a string literal, Yes, agreed. > Color coding editors make it easier to catch such errors, but they were > less common in 1991. I would say the need for triple quote strings has passed long ago. Like you say, since color lexers are ubiquitous now we don't need them. > And there is still uncolored interactive mode. I don't see interactive command line programming as a problem. I mean, who drops into a cmd line and starts writing paragraphs of string literals? Typically, one would just make a few one-liner calls here or there. Also, un-terminated string literal errors can be very aggravating. Not because they are difficult to fix, no, but because they are difficult to find! -- and sending me an error message like... "Exception: Un-terminated string literal meets EOF! line: 50,466,638" ... is about as helpful as a bullet in my head! If the interpreter finds itself at EOF BEFORE a string closes, don't you think it would be more helpful to include the currently "opened" strings START POSITION also? Heck, it would be wonderful to only have the start position since the likely-hood of a string ending at EOF is astronomical! As an intelligent lad must know, the odds that the distance from any given string's start position to it's end position is more likely to be shorter than the distance from the string's beginning to the freaking EOF! Ruby and Python are both guilty of this atrocity. From rantingrickjohnson at gmail.com Tue Dec 27 20:11:49 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 17:11:49 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <254cfbe0-19db-4d56-87b2-08f9da19aa61@q17g2000yqh.googlegroups.com> On Dec 27, 5:10?pm, Steven D'Aprano wrote: > On Sun, 25 Dec 2011 07:47:20 -0800, Eelco wrote: > Your original use-case, where you want to change the type of tail from a > list to something else, is simply solved by one extra line of code: > > head, *tail = sequence > tail = tuple(tail) i wonder if we could make this proposal a bit more "Pythonic"? Hmm... head, tuple(tail) = sequence ...YEP! From wolftracks at invalid.com Tue Dec 27 20:21:26 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 17:21:26 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 2:58 PM, Ian Kelly wrote: > On Tue, Dec 27, 2011 at 2:35 PM, W. eWatson wrote: >> I replaced numpy and matplotlib, and added scipy. I still get errors, but >> perhaps because the install order is now wrong. It was >> numpy >> matplotlib >> >> Does anyone know the right order? > > The order (numpy, scipy, matplotlib) reflects the dependencies, but > since the installers are just fancied-up self-extracting zips, I don't > think it should matter. > >> OK, I installed it. Is there an easy way to go to track down this dll. >> There's a Help, but I can't print it from the PC I have it on. > > It should be reasonably self-explanatory, I think. Just start > Dependency Walker, open up the _path.pyd file, and it will show you > the full dependency tree. Below that is a list of all the modules, > and any missing dependencies should helpfully show up right at the top > of the list. > > Note that missing delay-load dependencies (those with an hourglass > icon) are not necessarily problems. Since the error occurs at > load-time, you're looking for a missing module that would be loaded > immediately. I was too timid. When I saw File->Open, I thought I was going to have to open a file, thinking what file could it be? Well, it found several problems. These DLLs MSVCP1 EFSADU MSJAVA. There may be 100 file. Issues found: required dependency not found delay-dependency not found one nodule unresolved ... missing delay-dependency module Perhaps the next step is to download a fresh version of matplotlib? From dihedral88888 at googlemail.com Tue Dec 27 20:21:55 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 27 Dec 2011 17:21:55 -0800 (PST) Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <6529469.1070.1325035315877.JavaMail.geo-discussion-forums@prix23> There are Dr.Python, Pycrust and Notepadplus to support writing python programs. IDLE is OK, but if a program failed inside IDLE, then I might have to kill the old IDLE and restart IDLE again. From dihedral88888 at googlemail.com Tue Dec 27 20:21:55 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Tue, 27 Dec 2011 17:21:55 -0800 (PST) Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <6529469.1070.1325035315877.JavaMail.geo-discussion-forums@prix23> There are Dr.Python, Pycrust and Notepadplus to support writing python programs. IDLE is OK, but if a program failed inside IDLE, then I might have to kill the old IDLE and restart IDLE again. From rantingrickjohnson at gmail.com Tue Dec 27 20:56:33 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 17:56:33 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: On Dec 27, 3:44?pm, Eelco wrote: > Despite the fact that you mis-attributed that quote to me, im going to > be a little bit offended in the name of its actual author anyway. > Thats a lot of words to waste on your linguistic preferences. > Personally, I reserve the right to botch my non-native languages as > much as I please. I hope you're just joking a bit because i have little respect for those who refuse to better themselves. If you are learning English as a second language then you have a legitimacy excuse, but at some point that excuse just becomes a lie. In any case, i apologize for mis- quoting you. > > I always though "run" was a perfect verb for "running" code... but who > > knows :) > > Im assuming the audience is familiar with an ipod, but not an IDE, or > programming in general. To their eyes, it looks like a 'play' button; I would be very careful about introducing new words, or borrowing words, to replace tried and true technical terms that have existed for along time. As a matter of fact, the BDFL make the point better than i ever could: GvR speaking about ABC's design: """ The ABC group assumed that the users had no prior computer experience (or were willing to forget it). Thus, alternative terminology was introduced that was considered more "newbie-friendly" than standard programming terms. For example: procedures were called "how-tos" and variables "locations". """ From rantingrickjohnson at gmail.com Tue Dec 27 21:01:22 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 18:01:22 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <6529469.1070.1325035315877.JavaMail.geo-discussion-forums@prix23> Message-ID: <4a88c970-ade9-4221-85e2-2feee8f6fec3@24g2000yqi.googlegroups.com> On Dec 27, 7:21?pm, 88888 Dihedral wrote: > There are Dr.Python, Pycrust and ?Notepadplus to support writing python programs. I really like Pycrust. It's written in Python, it's code base is structured in a professional manner (IDLE you should be jealous!), and it works well. However, it is dependent on WxPython; so that's right out. > IDLE is OK, but if a program failed inside IDLE, then I ?might have > to kill the old IDLE and restart IDLE again. Yes, i've had to kill many frozen instances of IDLE. It gets aggravating. From python.list at tim.thechases.com Tue Dec 27 21:21:49 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 27 Dec 2011 20:21:49 -0600 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: <4EFA7D3D.2090908@tim.thechases.com> On 12/27/11 19:56, Rick Johnson wrote: > On Dec 27, 3:44 pm, Eelco > wrote: > >> Despite the fact that you mis-attributed that quote to me, >> im going to be a little bit offended in the name of its >> actual author anyway. Thats a lot of words to waste on your >> linguistic preferences. Personally, I reserve the right to >> botch my non-native languages as much as I please. > > I hope you're just joking a bit because i have little respect > for those who refuse to better themselves. If you are learning > English as a second language then you have a legitimacy > excuse, but at some point that excuse just becomes a lie. I'm glad you're open to learning more about English as "used to" is perfectly acceptable according to the World English Dictionary[1] """ 2. (takes an infinitive or implied infinitive) used as an auxiliary to express habitual or accustomed actions, states, etc, taking place in the past but not continuing into the present: I don't drink these days, but I used to; I used to fish here every day """ May you be found better for learning and come to give others the benefit of the doubt. -tkc [1] http://dictionary.reference.com/browse/used%20to From ian.g.kelly at gmail.com Tue Dec 27 21:27:49 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 27 Dec 2011 19:27:49 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On Tue, Dec 27, 2011 at 6:21 PM, W. eWatson wrote: > Well, it found several problems. These DLLs > MSVCP1 > EFSADU > MSJAVA. I'm guessing MSVCP1 is a typo for MSVCP71? If that is missing then that is probably the culprit. That DLL is the C runtime library. It is supposed to be shipped with applications that need it, but it is so ubiquitous that it is often assumed to be present or forgotten. You can download it from http://www.dll-files.com/dllindex/dll-files.shtml?msvcp71 (or just find it on another Windows XP PC) and copy it into C:\Windows\System32. Don't forget to run regsvr32 to register it. HTH, Ian From rantingrickjohnson at gmail.com Tue Dec 27 21:42:05 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 18:42:05 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: On Dec 27, 8:21?pm, Tim Chase wrote: > I'm glad you're open to learning more about English as "used to" > is perfectly acceptable according to the World English Dictionary[1] > [...] > May you be found better for learning and come to give others the > benefit of the doubt. I don't care what ANY dictionary says. Much less a "world" dictionary. I don't validate or invalidate a word based on some phony baloney group of pseudo intellectuals who decided one to day that writing a dictionary "might be cool". I am against these words and phrases because we already have words that work just fine. Why rock the boat? Why would you use a word like "hard" (which describes the physical properties of a tangible object), to describe how "difficult" a task may be? If you insist on this lunacy, then why not use "soft" to describe how easy a task may be? Seems ridiculous now, huh? Garbage Verbiage Translator: Used to -> previously|before Supposed to -> required|expected Use to -> accustomed|acquainted Right (OOC) -> Correct Hard (OOC) -> Difficult Pretty (OOC) -> very This is group has the most dumbest smart people i have ever met! From prabhadigi2 at gmail.com Tue Dec 27 21:47:48 2011 From: prabhadigi2 at gmail.com (prabhakaran k) Date: Tue, 27 Dec 2011 18:47:48 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From wolftracks at invalid.com Tue Dec 27 22:31:08 2011 From: wolftracks at invalid.com (W. eWatson) Date: Tue, 27 Dec 2011 19:31:08 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 6:27 PM, Ian Kelly wrote: > On Tue, Dec 27, 2011 at 6:21 PM, W. eWatson wrote: >> Well, it found several problems. These DLLs >> MSVCP1 >> EFSADU >> MSJAVA. > > I'm guessing MSVCP1 is a typo for MSVCP71? If that is missing then > that is probably the culprit. That DLL is the C runtime library. It > is supposed to be shipped with applications that need it, but it is so > ubiquitous that it is often assumed to be present or forgotten. You > can download it from > http://www.dll-files.com/dllindex/dll-files.shtml?msvcp71 (or just > find it on another Windows XP PC) and copy it into > C:\Windows\System32. Don't forget to run regsvr32 to register it. > > HTH, > Ian You are very likely right about the spelling. I wrote it down, and carried it to this PC. Sometimes I can't read my own writing. I've never used regsvr32, but I would guess it's entered into the Run (simple dialog for MS tools like regedit) selection off the Start menu. From d at davea.name Tue Dec 27 22:47:54 2011 From: d at davea.name (Dave Angel) Date: Tue, 27 Dec 2011 22:47:54 -0500 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: <4EFA916A.7000105@davea.name> On 12/27/2011 10:31 PM, W. eWatson wrote: > On 12/27/2011 6:27 PM, Ian Kelly wrote: >> >> http://www.dll-files.com/dllindex/dll-files.shtml?msvcp71 (or just >> find it on another Windows XP PC) and copy it into >> C:\Windows\System32. Don't forget to run regsvr32 to register it. >> >> HTH, >> Ian > You are very likely right about the spelling. I wrote it down, and > carried it to this PC. Sometimes I can't read my own writing. > > I've never used regsvr32, but I would guess it's entered into the Run > (simple dialog for MS tools like regedit) selection off the Start menu. Of course not. You run it from the same command window (aka DOS box, or cmd window) you've been doing all your other testing from. And like all good commands, it has built-in help so you can see what the argument list looks like. -- DaveA From rantingrickjohnson at gmail.com Tue Dec 27 22:49:12 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 19:49:12 -0800 (PST) Subject: Py-dea: Streamline string literals now! Message-ID: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Hello folks, In a recent thread i stumbled upon an epiphany of sorts concerning Python string literals, with implications that trickle down to all forms of string literals used in general programming, since, for the most part, the syntax is virtually the same! For all our lives we have been excepting a standard for string literals that is, quite literally, overkill. It seems all along the syntax has been flawed, however, we have been totally unaware... until now! *[A brief history lesson]* Python offers two main styles of delimiting string literals: * single "leading and trailing" double, or single quote chars. * triple "leading and trailing" double, or single quote chars. The single leading group is intended for one line literals whereas the triple is intended for multi-line literals. Now, in my initial days of Python-ing, i thought this was a great system. Unlike most languages that require a programmer to concatenate strings over and over again, python "seemed" to solve the multi-line issue -- but there is a greater issue! *[Current Issues]* The fact is...even with the multi-line issue solved, we still have two forms of literal delimiters that encompass two characters resulting in *four* possible legal combinations of the exact same string! I don't know about you guys, but i am not a big fan of Tim Towtdi. But the problems just keep adding up. There is also the issue of escape sequences; which i have always found to be atrociousness noisy. Everyone who has had to deal with double escapes in regexps raise your hand. The frowns are deafening. The simple fact is: The current syntax for string literals is not only deficient, it is bloated, noisy, and confusing! *[Solution]* I believe that with the ubiquitous-ness of syntax highlight, string literals only need one delimiter. In the old days (before syntax highlight was invented) i could understand how a programmer "might" miss a single (or even a triple!) closing delimiter; but those days died with the king. My proposal is to introduce a single delimiter for string literals. A new string literal that is just as good at spanning single lines as it is spanning multiple lines. A new literal that uses widely known markup tag syntax instead of cryptic and noisy escape sequences. And finally, a *literal* that is worthy of the 21st century. Thank You. From rosuav at gmail.com Tue Dec 27 22:54:29 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Dec 2011 14:54:29 +1100 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: On Wed, Dec 28, 2011 at 1:42 PM, Rick Johnson wrote: > I don't care what ANY dictionary says. Much less a "world" dictionary. > I don't validate or invalidate a word based on some phony baloney > group of pseudo intellectuals who decided one to day that writing a > dictionary "might be cool". Finally we know who Ranting Rick is the alt of. He's Humpty Dumpty! http://en.wikisource.org/wiki/Through_the_Looking-Glass,_and_What_Alice_Found_There/Chapter_VI ChrisA From rosuav at gmail.com Tue Dec 27 23:06:37 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Dec 2011 15:06:37 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Dec 28, 2011 at 10:10 AM, Steven D'Aprano wrote: > Your original use-case, where you want to change the type of tail from a > list to something else, is simply solved by one extra line of code: > > head, *tail = sequence > tail = tuple(tail) That achieves the goal of having tail as a different type, but it does have the additional cost of constructing and then discarding a temporary list. I know this is contrived, but suppose you have a huge set/frozenset using tuples as the keys, and one of your operations is to shorten all keys by removing their first elements. Current Python roughly doubles the cost of this operation, since you can't choose what type the tail is made into. But if that's what you're trying to do, it's probably best to slice instead of unpacking. Fortunately, the Zen of Python "one obvious way to do it" doesn't stop there being other ways that work too. ChrisA From rosuav at gmail.com Tue Dec 27 23:17:30 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Dec 2011 15:17:30 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: On Wed, Dec 28, 2011 at 2:49 PM, Rick Johnson wrote: > My proposal is to introduce a single delimiter for string literals. A > new string literal that is just as good at spanning single lines as it > is spanning multiple lines. A new literal that uses widely known > markup tag syntax instead of cryptic and noisy escape sequences. And > finally, a *literal* that is worthy of the 21st century. So all you're doing is introducing a different form of escaping. You can already achieve this with regular expressions in several engines - declare a different delimiter and/or escape character - in order to dodge the issue of multiple escapes. Python already has raw strings for the same reason (although the rules governing raw strings are oddly complex in edge cases). Your proposal sounds like a good idea for a specific-situation config file, but a very bad idea for Python. If you want elaboration on that, ask me for my arguments on CSV vs /etc/passwd. Or search the web for the same topic, I'm sure many others have made the same points. ChrisA From steve+comp.lang.python at pearwood.info Tue Dec 27 23:20:26 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Dec 2011 04:20:26 GMT Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: <4efa990a$0$29973$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Dec 2011 19:49:12 -0800, Rick Johnson wrote: > I believe that with the ubiquitous-ness of syntax highlight, string > literals only need one delimiter. In the old days (before syntax > highlight was invented) i could understand how a programmer "might" miss > a single (or even a triple!) closing delimiter; but those days died with > the king. http://en.wikipedia.org/wiki/List_of_current_monarchs Thank you Rick for another non-solution to a non-problem. How is that fork of Python going? You promised us you would revitalise the community with a new fork of Python that throws out all the accumulated cruft of the language. I'm sure that the silent majority who agree with you are hanging out for your first release. I know I am. -- Steven From no.email at nospam.invalid Tue Dec 27 23:27:11 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Tue, 27 Dec 2011 20:27:11 -0800 Subject: RSA and Cryptography in PYTHON References: <31261167.63.1324843108857.JavaMail.geo-discussion-forums@prdv33> <24097307.586.1325029506304.JavaMail.geo-discussion-forums@prmw6> Message-ID: <7xy5tx2ue8.fsf@ruckus.brouhaha.com> 88888 Dihedral writes: >> How about the nontrivial decoding part ? > I am getting lousy in the news group in my writing? > I mean the non-trivial decoding of the key decomposition. I still don't have the slightest idea what you are asking for. From rantingrickjohnson at gmail.com Tue Dec 27 23:36:35 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 20:36:35 -0800 (PST) Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: On Dec 27, 10:17?pm, Chris Angelico wrote: > On Wed, Dec 28, 2011 at 2:49 PM, Rick Johnson > > wrote: > > My proposal is to introduce a single delimiter for string literals. A > > new string literal that is just as good at spanning single lines as it > > is spanning multiple lines. A new literal that uses widely known > > markup tag syntax instead of cryptic and noisy escape sequences. And > > finally, a *literal* that is worthy of the 21st century. > > So all you're doing is introducing a different form of escaping. Well that is but one of the proposals. The others include reducing four combinations of string literals into one, and by default, spanning multiple lines with one syntax. > You > can already achieve this with regular expressions in several engines - > declare a different delimiter and/or escape character - in order to > dodge the issue of multiple escapes. I am specifically referring to the Python language and how the interpreter "interprets" string literals. Wait, you do share the same definition of "string literal" as i do, i hope? http://en.wikipedia.org/wiki/String_literal > Python already has raw strings > for the same reason (although the rules governing raw strings are > oddly complex in edge cases). Raw strings would be history with my proposal. No more need for raw strings. This is the string literal to rule all string literals! > Your proposal sounds like a good idea for a specific-situation config > file, but a very bad idea for Python. Can you give specific reasons why? From rantingrickjohnson at gmail.com Wed Dec 28 00:34:19 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Tue, 27 Dec 2011 21:34:19 -0800 (PST) Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: On Dec 27, 9:49?pm, Rick Johnson wrote: > The fact is...even with the multi-line issue solved, we still have two > forms of literal delimiters that encompass two characters resulting in > *four* possible legal combinations of the exact same string! I don't > know about you guys, but i am not a big fan of Tim Towtdi. actually i was a bit hasty with that statment and underestimated the actual number of possiblities. 1) "this is a string" 2) 'this is a string' 3) r"this is a string" 4) r'this is a string' 5) '''this is a string''' 6) """this is a string""" 7) r'''this is a string''' 8) r"""this is a string""" A) "this is difficult to \"eyeball parse\"" B) """this is "overkill"""" C) "that's just plain \"nuts\"!" Now. If anyone can look at that mess and not admit it is a disaster, well then... I am also thinking that ANY quote char is a bad choice for string literal delimiters. Why? Well because it is often necessary to embed single or double quotes into a string literal. We need to use a delimiter that is not a current delimiter elsewhere in Python, and also, is a very rare char. I believe Mr Ewing found that perfect char in his "Multi-line uber raw string literals!" (Just scroll down a bit at this link)... http://www.cosc.canterbury.ac.nz/greg.ewing/python/ideas.html ...however, requiring a programmer to start EVERY line with a marker does not seem like fun to me. And just think what a nightmare it will be to modify copy/pasted data with line markers! Although it does solve the "indention" issue with doc-strings! I think just for foreign language compatibility reasons we should stick with one of either " or ' (or maybe both), but allowing [r]""" and [r]''' is just WAY too much! We need to trim this fat. From rustompmody at gmail.com Wed Dec 28 00:50:20 2011 From: rustompmody at gmail.com (rusi) Date: Tue, 27 Dec 2011 21:50:20 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> Message-ID: <59bae356-99a9-4250-9b56-b3f8723d2a2b@r13g2000prr.googlegroups.com> On Dec 27, 11:26?pm, Andrew Berg wrote: > On 12/27/2011 11:59 AM, K Richard Pixley wrote:> You'd do better to encourage eclipse, but setting that up isn't > > trivial either. > > IIRC, all I had to do to set up PyDev was copy a URL to Eclipse's > "Install New Software" wizard, and have Eclipse download and install it. > Extra steps are needed if a different implementation of Python (e.g. > Jython) is needed, but other than that, the user only needs to specify a > couple options (e.g. Python grammar version) at project creation time. > This assumes that Python is already installed, but why wouldn't it be? There was a recent announcement about eclipse for python (pydev) http://www.mail-archive.com/python-list at python.org/msg320434.html It did not work for me -- got a backtrace. I responded with that. There was no further response. Just mentioning that at least in some cases it does not 'just work.' From angle6602 at gmail.com Wed Dec 28 01:01:30 2011 From: angle6602 at gmail.com (angle 9) Date: Tue, 27 Dec 2011 22:01:30 -0800 (PST) Subject: google form filling Message-ID: <98d92b5b-9f83-48f0-ade1-247f22c8201c@k22g2000prb.googlegroups.com> google form filling http://gsonlinejobs.yolasite.com/ From angle6602 at gmail.com Wed Dec 28 01:01:55 2011 From: angle6602 at gmail.com (angle 9) Date: Tue, 27 Dec 2011 22:01:55 -0800 (PST) Subject: google form filling Message-ID: google form filling http://gsonlinejobs.yolasite.com/ From steve+comp.lang.python at pearwood.info Wed Dec 28 01:25:35 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Dec 2011 06:25:35 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Dec 2011 15:06:37 +1100, Chris Angelico wrote: > On Wed, Dec 28, 2011 at 10:10 AM, Steven D'Aprano > wrote: >> Your original use-case, where you want to change the type of tail from >> a list to something else, is simply solved by one extra line of code: >> >> head, *tail = sequence >> tail = tuple(tail) > > That achieves the goal of having tail as a different type, but it does > have the additional cost of constructing and then discarding a temporary > list. I know this is contrived, but suppose you have a huge > set/frozenset using tuples as the keys, and one of your operations is to > shorten all keys by removing their first elements. Current Python > roughly doubles the cost of this operation, since you can't choose what > type the tail is made into. The First Rule of Program Optimization: - Don't do it. The Second Rule of Program Optimization (for experts only): - Don't do it yet. Building syntax to optimize imagined problems is rarely a good idea. The difference between 2 seconds processing your huge set and 4 seconds processing it is unlikely to be significant unless you have dozens of such huge sets and less than a minute to process them all. And your idea of "huge" is probably not that big... it makes me laugh when people ask how to optimize code "because my actual data has HUNDREDS of items!". Whoop-de-doo. Come back when you have a hundred million items, then I'll take your question seriously. (All references to "you" and "your" are generic, and not aimed at Chris personally. Stupid English language.) > But if that's what you're trying to do, it's probably best to slice > instead of unpacking. Assuming the iterable is a sequence. Fortunately, most iterable constructors accept iterators directly, so for the cost of an extra line (three instead of two), you can handle data structures as big as will fit into memory: # I want to keep both the old and the new set it = iter(huge_set_of_tuples) head = next(it) # actually an arbitrary item tail = set(x[1:] for x in it) # and everything else If you don't need both the old and the new: head = huge_set_of_tuples.pop() tail = set() while huge_set_of_tuples: tail.add(huge_set_of_tuples.pop()[1:]) assert huge_set_of_tuples == set([]) If you rely on language features, who knows how efficient the compiler will be? head, tail::tuple = ::sequence may create a temporary list before building the tuple anyway. And why not? That's what this *must* do: head, second, middle::tuple, second_from_last, last = ::iterator because tuples are immutable and can't be grown or shrunk, so why assume the language designers special cased the first form above? > Fortunately, the Zen of Python "one obvious way to > do it" doesn't stop there being other ways that work too. Exactly. It is astonishing how many people think that if there isn't a built-in language feature, with special syntax, to do something, there's a problem that needs to be solved. -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 28 01:58:37 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Dec 2011 06:58:37 GMT Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Dec 2011 21:34:19 -0800, Rick Johnson wrote: > Now. If anyone can look at that mess and not admit it is a disaster, > well then... It isn't a disaster. A disaster is when people die, lose their houses, get tossed out into the street to starve, radioactive contamination everywhere, floods, fire, the first born of every family being struck down suddenly, that sort of thing. Not having eight ways to write string literals. That's merely a convenience. > I am also thinking that ANY quote char is a bad choice for string > literal delimiters. Why? Well because it is often necessary to embed > single or double quotes into a string literal. We need to use a > delimiter that is not a current delimiter elsewhere in Python, and also, > is a very rare char. I believe Mr Ewing found that perfect char in his > "Multi-line uber raw string literals!" (Just scroll down a bit at this > link)... > > http://www.cosc.canterbury.ac.nz/greg.ewing/python/ideas.html Not surprisingly, you haven't thought this through. I'm sure Greg Ewing has, which is why he hasn't proposed *replacing* string delimiters with his multi-line format. That's why he proposed it as a *statement* and not string-builder syntax. Without an end-delimiter, how do you embed string literals in expressions? -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 28 01:59:15 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Dec 2011 06:59:15 GMT Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> On Tue, 27 Dec 2011 18:42:05 -0800, Rick Johnson wrote: > On Dec 27, 8:21?pm, Tim Chase wrote: > >> I'm glad you're open to learning more about English as "used to" is >> perfectly acceptable according to the World English Dictionary[1] [...] >> May you be found better for learning and come to give others the >> benefit of the doubt. > > I don't care what ANY dictionary says. Much less a "world" dictionary. I > don't validate or invalidate a word based on some phony baloney group of > pseudo intellectuals who decided one to day that writing a dictionary > "might be cool". I am against these words and phrases because we already > have words that work just fine. Why rock the boat? Why do you say "rock" when the word "shake" is just as good? Why do you say "boat" when we already have "ship"? Why do you say "pseudo intellectuals" when you could say "fake intellectuals"? Why do I waste my time reading your pretentious self-important nonsense? [...] > This is group has the most dumbest smart people i have ever met! Considering I keep expecting you to stop trolling, I admit this applies to me. -- Steven From rustompmody at gmail.com Wed Dec 28 02:05:36 2011 From: rustompmody at gmail.com (rusi) Date: Tue, 27 Dec 2011 23:05:36 -0800 (PST) Subject: Regular expressions References: <495b6fe6-704a-42fc-b10b-484218ad8409@b20g2000pro.googlegroups.com> <4be34afe-4291-414b-9212-498074400e39@v24g2000prn.googlegroups.com> Message-ID: <675670e4-a954-46f3-a90d-cd4c5934ffec@h7g2000prn.googlegroups.com> On Dec 27, 10:01?am, Fredrik Tolf wrote: > On Mon, 26 Dec 2011, mauricel... at acm.org wrote: > > I've tried > > > re.sub('@\S\s[1-9]:[A-N]:[0-9]', '@\S\s', '@HWI-ST115:568:B08LLABXX: > > 1:1105:6465:151103 1:N:0:') > > > but it does not seems to work. > > Indeed, for several reasons. First of all, your backslash sequences are > interpreted by Python as string escapes. You'll need to write either "\\S" > or r"\S" (the r, for raw, turns off backslash escapes). > > Second, when you use only "\S", that matches a single non-space character, > not several; you'll need to quantify them. "\S*" will match zero or more, > "\S+" will match one or more, "\S?" will match zero or one, and there are > a couple of other possibilities as well (see the manual for details). In > this case, you probably want to use "+" for most of those. > > Third, you're not marking the groups that you want to use in the > replacement. Since you want to retain the entire string before the space, > and the numeric element, you'll want to enclose them in parentheses to > mark them as groups. > > Fourth, your replacement string is entirely wacky. You don't use sequences > such as "\S" and "\s" to refer back to groups in the original text, but > numbered references, to refer back to parenthesized groups in the order > they appear in the regex. In accordance what you seemed to want, you > should probably use "@\1/\2" in your case ("\1" refers back to the first > parentesized group, which you be the first "\S+" part, and "\2" to the > second group, which should be the "[1-9]+" part; the at-mark and slash > are inserted as they are into the result string). > > Fifth, you'll probably want to match the last colon as well, in order not > to retain it into the result string. > > All in all, you will probably want to use something like this to correct > that regex: > > re.sub(r'@(\S+)\s([1-9]+):[A-N]+:[0-9]+:', r'@\1/\2', > ? ? ? ? '@HWI-ST115:568:B08LLABXX:1:1105:6465:151103 1:N:0:') > > Also, you may be interested to know that you can use "\d" instead of > "[0-9]". > > -- > > Fredrik Tolf For practical 'get-the-hands-dirty' experience look at python-specific: http://kodos.sourceforge.net/ Online: http://gskinner.com/RegExr/ emacs-specific: re-builder and regex-tool http://bc.tech.coop/blog/071103.html From rosuav at gmail.com Wed Dec 28 02:08:37 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Dec 2011 18:08:37 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Dec 28, 2011 at 5:25 PM, Steven D'Aprano wrote: > On Wed, 28 Dec 2011 15:06:37 +1100, Chris Angelico wrote: > >> ... suppose you have a huge >> set/frozenset using tuples as the keys, and one of your operations is to >> shorten all keys by removing their first elements. Current Python >> roughly doubles the cost of this operation, since you can't choose what >> type the tail is made into. > > The First Rule of Program Optimization: > - Don't do it. > > The Second Rule of Program Optimization (for experts only): > - Don't do it yet. > > > Building syntax to optimize imagined problems is rarely a good idea. The > difference between 2 seconds processing your huge set and 4 seconds > processing it is unlikely to be significant unless you have dozens of > such huge sets and less than a minute to process them all. > > And your idea of "huge" is probably not that big... it makes me laugh > when people ask how to optimize code "because my actual data has HUNDREDS > of items!". Whoop-de-doo. Come back when you have a hundred million > items, then I'll take your question seriously. > > (All references to "you" and "your" are generic, and not aimed at Chris > personally. Stupid English language.) And what you're seeing there is the _best possible_ situation I could think of, the strongest possible justification for new syntax. Granted, that may say more about me and my imagination than about the problem, but the challenge is open: Come up with something that actually needs this. ChrisA From panupatc at gmail.com Wed Dec 28 02:45:55 2011 From: panupatc at gmail.com (Panupat Chongstitwattana) Date: Wed, 28 Dec 2011 14:45:55 +0700 Subject: user login session for stand alone PyQt Message-ID: For my current project I'm making PyQt GUI to be used inside Maya. One of my requirement is that users need to first login with username and password. After authenticating the account, I'm not sure how I can store the session. I tried Cookie.Simplecookie but I guess it doesn't wok because the GUI is not running through HTTP. Any suggestion appreciate. From rosuav at gmail.com Wed Dec 28 02:47:44 2011 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 28 Dec 2011 18:47:44 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: On Wed, Dec 28, 2011 at 4:34 PM, Rick Johnson wrote: > I am also thinking that ANY quote char is a bad choice for string > literal delimiters. Why? Well because it is often necessary to embed > single or double quotes into a string literal. Postgres allows dollar-delimited strings, which get around this issue somewhat. http://www.postgresql.org/docs/9.1/static/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING But for most strings, it simply makes sense to use a quote character. Most strings don't need both ' and " in them. You cannot pick one character to be your ultimate delimiter, because there will always be occasion to embed it. (If nothing else, what happens when you emit code?) You want the delimiter to be easily typed and recognized, and that guarantees that it'll be something that's going to want to be emitted. It's necessary to have multiple options, or escaping. ChrisA From rustompmody at gmail.com Wed Dec 28 02:49:18 2011 From: rustompmody at gmail.com (rusi) Date: Tue, 27 Dec 2011 23:49:18 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 28, 11:59?am, Steven D'Aprano wrote: > Why do I waste my time reading your pretentious self-important nonsense? > > > This is group has the most dumbest smart people i have ever met! > > Considering I keep expecting you to stop trolling, I admit this applies > to me. http://www.goodreads.com/quotes/show/55234 From bouleetbil at gmail.com Wed Dec 28 02:58:59 2011 From: bouleetbil at gmail.com (bouleetbil (gaetan)) Date: Wed, 28 Dec 2011 08:58:59 +0100 Subject: ctypes argument by reference Message-ID: <20111228085859.1e368ef6@gmail.com> Hi I try to write a python backend for pamcan-g2, for that I should write a callback for *trans_cb_conv In python I've write : trans_cb_event = CFUNCTYPE(ctypes.c_char_p,ctypes.c_void_p,POINTER(ctypes.c_int) ... def fpm_trans_conv(event,pkg,response): foo... response=1 and for call the callback if trans_init(pm_trans,flags, trans_cb_event(fpm_progress_event), trans_cb_conv(fpm_trans_conv), None) == -1 : foo... Into libpacman response is a C int pointer : ... QUESTION(trans, PM_TRANS_CONV_LOCAL_UPTODATE, local, NULL, NULL, &resp); ... but resp=0 howto pass response by reference instead by value ? If you would more informations/code : the .h is here : http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=blob_plain;f=lib/libpacman/pacman.h;hb=HEAD the python code here : http://git.frugalware.org/gitweb/gitweb.cgi?p=frugal-tweak.git;a=blob_plain;f=py-pacman/py-pacman.py;hb=4abed4ee445f009387a3a51957e231c010c123bb trans_cb_event line 315 fpm_trans_conv line 676 pacman_trans_init callback 708 From ladasky at my-deja.com Wed Dec 28 03:51:48 2011 From: ladasky at my-deja.com (John Ladasky) Date: Wed, 28 Dec 2011 00:51:48 -0800 (PST) Subject: Multiprocessing bug, is information ever omitted from a traceback? References: <3d377e6e-3da3-4e94-9e3f-999557488211@c16g2000pre.googlegroups.com> Message-ID: <96329d43-30b4-4c90-a925-d6500e21ad7c@c42g2000prb.googlegroups.com> I hope that the interested parties who offered me help will remember this thread title. It has been a few weeks. After fussing around with an alternate installation of Python 2.7.2 on top of Ubuntu Linux 10.04, I decided it would be easier to upgrade to Ubuntu 11.10, for which Python 2.7.2 is the default Python version. I have reproduced my Python 2.6.6 bug in Python 2.7.2, as you will see. On Dec 9, 3:14?pm, I wrote: > All lines of code referenced in the traceback are in the standard > library code: > > Exception in thread Thread-1: > Traceback (most recent call last): > ? File "/usr/lib/python2.6/threading.py", line 532, in > __bootstrap_inner > ? ? self.run() > ? File "/usr/lib/python2.6/threading.py", line 484, in run > ? ? self.__target(*self.__args, **self.__kwargs) > ? File "/usr/lib/python2.6/multiprocessing/pool.py", line 284, in > _handle_tasks > ? ? put(task) > TypeError: expected string or Unicode object, NoneType found > > Fortunately, I have a working version of my code. ?I was trying to add > new features, and only my new code is causing trouble. ?This has > allowed me to examine the contexts of task when everything works. > > Task is not a string when the program works. ?Task is not None when > the program doesn't work. ?In fact, task is a deeply-nested tuple. ?NO > PART of this tuple ever contains any strings, as far as I can tell. > More details in my original thread. Here's the traceback that I obtained tonight. It's essentially the same, except for the line numbers in the library code and the thread number which yielded the exception: | Exception in thread Thread-2: | Traceback (most recent call last): | File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner | self.run() | File "/usr/lib/python2.7/threading.py", line 505, in run | self.__target(*self.__args, **self.__kwargs) | File "/usr/lib/python2.7/multiprocessing/pool.py", line 315, in _handle_tasks | put(task) | TypeError: expected string or Unicode object, NoneType found I still have more work to do, I know. I will try to pare down my fairly-complex code to a minimal example. Check back again in a few days? Weeks? Thanks! From robert.kern at gmail.com Wed Dec 28 04:24:42 2011 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 28 Dec 2011 09:24:42 +0000 Subject: Slices when extending python with C++ In-Reply-To: References: Message-ID: On 12/27/11 11:02 PM, rozelak at volny.cz wrote: > Hallo, > I have kind of special question when extening python with C++ > implemented modules. > > I try to implement a class, behaving also like an array. And I need > to implement slice-getters. I implemented PySequenceMethods.sq_slice > to get "simple" slices like: > > myobj[x:y] > > It works perfectly, without problems. But now I have a problem when > need to access the array with "special" slices, like: > > myobj[x:y:step] > myobj[::-1] # to revert the array > > I would like to ask which method must be implemented to get this > "special" slice getters. The "simple" slice > PySequenceMethods.sq_slice getter accepts only two indexes - from, > to, but not a step (which would solve the issue). The sq_slice slot is deprecated, just like the __getslice__() method on the Python side. Instead, implement sq_item to accept a slice object in addition to integer indices. http://docs.python.org/c-api/slice.html -- 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 From emekamicro at gmail.com Wed Dec 28 06:12:50 2011 From: emekamicro at gmail.com (Emeka) Date: Wed, 28 Dec 2011 12:12:50 +0100 Subject: globals function Message-ID: Hello All, What is the downside of using globals function Regards, Janus -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Wed Dec 28 06:57:52 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 28 Dec 2011 03:57:52 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <298b28e6-b8c9-43e1-8c90-cf0d7654fe68@y12g2000vba.googlegroups.com> <56f0073e-e259-4a9d-bd3a-1221fd74c5b1@d8g2000vbb.googlegroups.com> <7191d569-5036-4800-b7a3-fcacb000de94@d10g2000vbk.googlegroups.com> <930f748a-db7c-4ecc-9627-a3ab2647ae65@o14g2000vbo.googlegroups.com> <4ef72149$0$29973$c3e8da3$5496439d@news.astraweb.com> <457ebc8f-bb87-4005-b527-2fe6c02250c8@m7g2000vbc.googlegroups.com> <4ef757e3$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa4d55$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <222e6aa9-1fb9-4186-a23d-e9fd2757329e@dp8g2000vbb.googlegroups.com> On Dec 27, 11:57?pm, Steven D'Aprano wrote: > On Mon, 26 Dec 2011 13:41:34 -0800, Eelco wrote: > > On Dec 25, 6:05?pm, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> wrote: > >> On Sun, 25 Dec 2011 07:38:17 -0800, Eelco wrote: > > [...] > > >> > How is 'head, *tail = sequence' or semantically entirely > >> > equivalently, 'head, tail::list = sequence' any different then? Of > >> > course after interpretation/compilation, what it boils down to is > >> > that we are constructing a list and binding it to the identifier > >> > tail, but that is not how it is formulated in python as a language > > >> I'm afraid it is. > > >> Here's the definition of assignment in Python 3: > >>http://docs.python.org/py3k/reference/simple_stmts.html#assignment- > >> statements > > > Que? > > You have claimed that "constructing a list and binding it to the > identifier" is not how iterator unpacking is formulated in Python the > language. But that is wrong. That *is* how iterator unpacking is > formulated in Python the language. The reference manual goes into detail > on how assignment is defined in Python. You should read it. > > > 'head, *tail = sequence' > > > Is how one currently unpacks a head and tail in idiomatic python > > > This is semantically equivalent to > > > 'head = sequence[0]' > > 'tail = list(sequence[1:])' > > There's a reason this feature is called *iterable* unpacking: it operates > on any iterable object, not just indexable sequences. > > 'head, *tail = sequence' is not just semantically equivalent to, but > *actually is* implemented as something very close to: > > temp = iter(sequence) > head = next(temp) > tail = list(temp) > del temp > > Extended iterable unpacking, as in 'head, *middle, tail = sequence' is a > little more complex, but otherwise the same: it operates using the > iterator protocol, not indexing. I recommend you read the PEP, if you > haven't already done so. > > http://www.python.org/dev/peps/pep-3132/ > > > But these forms are linguistically different, in too many different ways > > to mention. > > How about mentioning even one way? Because I have no idea what > differences you are referring to, or why you think they are important. The one spans two lines; the other one. Need I go on? > >> > We dont have > >> > something of the form 'tail = list_tail(sequence)'. > > >> I'm afraid we do. See the definition of assignment again. > > > Que? > > Again, you make a claim about Python which is contradicted by the > documented behaviour of the language. You claim that we DON'T have > something of the form 'tail = list_tail(sequence)', but that is *exactly* > what we DO have: extracting the tail from an iterator. > > Obviously there is no built-in function "list_tail" but we get the same > effect by just using list() on an iterator after advancing past the first > item. > > > My claim is that the two semantically identical formulations above do > > not have isomorphic linguistic form. As far as I can make sense of your > > words, you seem to be disputing this claim, but its a claim as much > > worth debating as that the sun rises in the east. > > "Isomorphic linguistic form"? Are you referring to the idea from > linguistics that there are analogies between the structure of phonic and > semantic units? E.g. that the structures (phoneme, syllable, word) and > (sememe, onomateme, sentence) are analogous. > > I don't see why this is relevant, or which units you are referring to -- > the human-language description of what Python does, high-level Python > language features, Python byte-code, or machine code. Not that it > matters, but it is unlikely that any of those are isomorphic in the > linguistic sense, and I am not making any general claim that they are. > > If not, I have no idea what you are getting at, except possibly trying to > hide behind obfuscation. I wasnt too worried about obfuscation, since I think there is little left to lose on that front. Let me make a last-ditch effort to explain though: When python reads your code, it parses it into a symbolic graph representation. The two snippets under consideration do not parse into the same graph, in the same way that insignificant whitespace or arbitrary identifier names do, for instance. Hence, not linguistically isomorphic. The very function of a programming language is to transform this representation of the code into semantically identical but different code; (machine code, eventually). You fail to grok the difference between semantic equivalence and linguistic equivalence. Yes, there can be a semantic equivalence between iterable unpacking and a syntax of the form tail_list(sequence). And python does indeed probably apply a transformation of this kind under the hood (though we couldnt care less what it does exactly). AND IT PERFORMS THAT TRANSFORMATION USING THE TYPE CONSTRAINT GIVEN. Another example: the C code 'float x = 3', how would you interpret that? Is it 'not a type constraint on x', because eventually your C compiler turns it into machine code that doesnt leave a trace of that type constraint anyway? No, its a type constraint, exactly because C uses it to emit code specific to the type specified. > >> > Rather, we annotate > >> > the identifier 'tail' with an attribute that unquestionably > >> > destinates it to become a list*. It is no longer that 'tail' will > >> > just take anything that pops out of the expression on the right hand > >> > side; > > >> Of course it will. Python is a dynamically typed language. It doesn't > >> suddenly develop static types to ensure that 'tail' becomes a list; > >> 'tail' is bound to a list because that's what the assignment statement > >> provides. > > > How python accomplishes any of this under the hood is entirely > > immaterial. The form is that of a compile-time type constraint, > > regardless of whether the BDFL ever thought about it in these terms. > > Compile-time type constraints only have meaning in statically-typed > languages. Otherwise, you are applying an analogy that simply doesn't > fit, like claiming that the motion of paper airplanes and birds are > equivalent just because in English we use the word "fly" to describe them > both. > > The differences between what Python actually does and compile-time type- > constraints are greater than the similarities. > > Similarities: > > (1) In both cases, 'tail' ends up as a list. > > Differences: > > (1) Compiler enforces the constraint that the identifier 'tail' is always > associated with a list and will prevent any attempt to associate 'tail' > with some value that is not a list. Python does nothing like this: the > identifier 'tail' has no type restrictions at all, and can be used for > any object. After it is rebound, yes. Within the context of the iterable unpacking statement however, IT IS ALWAYS A LIST. Not only is it always a list, PYTHON *NEEDS* THIS KNOWLEDGE ABOUT TAIL TO EMIT THE CORRECT CODE (explicitly or implicitly; thats why I say 'it can be viewed as a type constraint') Just because the type constraint only applies within the statement does not make it any less of a type constraint; just one with different semantics, from say, C. > (3) "Constraint" has the conventional meaning of a restriction, not a > result; a type-constraint refers to a variable being prevented from being > set to some other type, not that it merely becomes set to that type. For > example, one doesn't refer to 'x = 1' as a type-constraint merely because > x gets set to an int value; why do you insist that 'head, *tail = > sequence' is a type-constraint merely because tail gets set to a list? Yes, the constraint is unique in this case; that does not make it any less of a constraint. A formula of the form 'x = 1' is called a constraint in mathematics. Collections with one element are still called collections. Do you have a problem with that too? > >> > rather, > >> > the semantics of what will go on at right hand side is coerced by the > >> > constraint placed on 'tail'. > > >> But it isn't a constraint placed on 'tail'. It is a consequence of the > >> definition of assignment in Python 3. 'tail' becomes bound to a list > >> because that is what the assignment statement is defined to do in that > >> circumstance, not because the identifier (symbol) 'tail' is constrained > >> to only accept lists. 'tail' may not even exist before hand, so talking > >> about constraints on 'tail' is an abuse of language, AS YOU AGREED > >> ABOVE. > > > 'tail' is (re)declared on the spot as a brand-new identifier (type > > constraint included); whether it exists before has no significance > > whatsoever, since python allows rebinding of identifiers. > > Given the following: > > tail = "beautiful red plumage" > head, *tail = [1, 2, 3, 4] > tail = 42 > > is it your option that all three instantiations of the *identifier* > 'tail' (one in each line) are *different* identifiers? Otherwise, your > statement about "brand new identifier" is simply wrong. > > If you allow that they are the same identifier with three different > values (and three different types), then this completely destroys your > argument that there is a constraint on the identifier 'tail'. First > 'tail' is a string, then it is a list, then it is an int. If that is a > type constraint (restriction) on 'tail', then constraint has no meaning. > > If you state that they are three different identifiers that just happen > to be identical in every way that can be detected, then you are engaged > in counting angels dancing on the head of pins. Given this idea that the > middle 'tail' identifier is a different identifier from the other two, > then I might accept that there *could* be a type-constraint on middle > 'tail', at least in some implementation of Python that hasn't yet been > created. But what a pointless argument to make. > > >> > *(I call that a 'type constraint', because that is what it literally > >> > is; > > >> No. It is literally a name binding of a dynamically typed, > >> unconstrained name to an object which happens to be a list. > > > Let me take a step back and reflect on the form of the argument we are > > having. I claim the object in front of us is a 'cube'. You deny this > > claim, by countering that it is 'just a particular configuration of > > atoms'. > > No no no, you have utterly misunderstood my argument. I'm not calling it > a configuration of atoms. I'm calling it a pyramid, and pointing out that > no matter how many times you state it is a cube, it actually has FOUR > sides, not six, and NONE of the sides are square, so it can't possibly be > a cube. For that to be true, you would have had to explain to me somewhere what it is you mean by a type constraint, and how this does not fit the bill. Granted, you did that for the first time in this post; and I added further detail why I think your particular usage of the term is too narrow; C does not have a unique claim to the concept. Or whereever you picked up your use of the term. Like I said, I mean it in its literal sense (and in the sense that happens to match the first hit on google; probably no coincidence). > > 'look at the definition!', you say; 'its just a list of coordinates, no > > mention of cubes whatsoever.'. > > > 'Look at the definition of a cube', I counter. 'This particular list of > > coordinates happens to fit the definition, whether the BDFT intended to > > or not'. > > But you are wrong. It does not fit the definition of a type-constraint, > except in the vacuous sense where you declare that there is some > invisible distinction between the identifier 'tail' in one statement and > the identical identifier 'tail' in another statement. Its not vacuous; its essential to the mechanics behind the transformation of the iterable unpacking statement to its compiled form. But its meaning does not carry beyond that, no. > > You are correct, in the sense that I do not disagree that it is a > > particular configuration of atoms. But if you had a better understanding > > of cubes, youd realize it meets the definition; > > I might not share your deep and expert understanding of cubes, but I > understand what "type-constraint" means, and I know what Python does in > iterator unpacking, and I know that there is no sensible definition of > type-constraint that applies to iterator unpacking in Python. You seem to know what a type constraint is in a language like C, and you seem to model your complete understanding from that particular instance of its use, rather than look at the actual definition of the term, and seeing where it might apply. From adeep.adeep93 at gmail.com Wed Dec 28 07:02:45 2011 From: adeep.adeep93 at gmail.com (adeep 2) Date: Wed, 28 Dec 2011 04:02:45 -0800 (PST) Subject: Google form filling jobs Message-ID: <37c0df0e-50bf-413d-876e-6a86b5fc5197@u10g2000prl.googlegroups.com> Google form filling jobs http://gsonlinejobs.yolasite.com/ From hoogendoorn.eelco at gmail.com Wed Dec 28 07:04:00 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 28 Dec 2011 04:04:00 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <4ef32cd9$0$29973$c3e8da3$5496439d@news.astraweb.com> <4ef71ae2$0$29973$c3e8da3$5496439d@news.astraweb.com> <2a248812-d0ea-424a-93f0-0f496d130414@o14g2000vbo.googlegroups.com> <4ef74c1d$0$29973$c3e8da3$5496439d@news.astraweb.com> <6f5a3dba-bb7e-44df-8c8f-a0d56441d807@i8g2000vbh.googlegroups.com> <21fe3e6f-9e02-43cd-bd4c-cfce1fdaf0d7@d10g2000vbh.googlegroups.com> <4efa4fab$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 28, 12:07?am, Steven D'Aprano wrote: > On Mon, 26 Dec 2011 13:51:50 -0800, Eelco wrote: > > [...] > > >> If your point is that parens are used more often than > >> packing/unpacking, that's almost certainly true, since function calls > >> (including method invocations) are so prevalent in pretty much any > >> code. But what does that prove? > > > That proves the original point of contention: that the below* is > > suboptimal language design, > > Well duh. This is where the referee should interrupt you for snipping someones citation right before a 'but' > I was mocking the idea that the meaning of * is context-dependent is a > bad thing by pointing out that we accept context-dependent meaning for > round brackets () without any difficulties. Of course it is "suboptimal > language design" -- it couldn't fail to be. Context-dependency is not > necessarily a bad thing. You know, so you dont end up simply restating my point while trying to make it seem like you disagree. > > not because terseness always trumps > > verbosity, but because commonly-used constructs (such as parenthesis or > > round brackets or whatever you wish to call them) > > Parentheses are not a construct. They are symbols (punctuation marks) > which are applied to at least three different constructs: grouping, > function calls, class inheritance lists. Parenthesis encompass a class of constructs. Happy now? > > are more deserving of > > the limited space in both the ascii table and your reflexive memory, > > than uncommonly used ones. > > Right. And since sequence packing and unpacking is a common idiom, it > deserves to be given punctuation. That's my opinion. Its a valid opinion. But if we are going to be quantitative about terms such as 'common', you know that there will be at least an order of magnitude difference between these constructs in commonality, if not two. Thats what makes your example a poor one. If you could verbosify a construct of the same commonality and arrive at equally absurd code, you would have a point. From hoogendoorn.eelco at gmail.com Wed Dec 28 07:05:00 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 28 Dec 2011 04:05:00 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <254cfbe0-19db-4d56-87b2-08f9da19aa61@q17g2000yqh.googlegroups.com> Message-ID: On Dec 28, 2:11?am, Rick Johnson wrote: > On Dec 27, 5:10?pm, Steven D'Aprano > +comp.lang.pyt... at pearwood.info> wrote: > > On Sun, 25 Dec 2011 07:47:20 -0800, Eelco wrote: > > Your original use-case, where you want to change the type of tail from a > > list to something else, is simply solved by one extra line of code: > > > head, *tail = sequence > > tail = tuple(tail) > > i wonder if we could make this proposal a bit more "Pythonic"? Hmm... > > head, tuple(tail) = sequence > > ...YEP! That has been considered; it was my first thought too, but this requires one to break the symmetry between collection packing and unpacking. From emekamicro at gmail.com Wed Dec 28 07:05:56 2011 From: emekamicro at gmail.com (Emeka) Date: Wed, 28 Dec 2011 13:05:56 +0100 Subject: Get Class properties Message-ID: Hello All, Say I have a class like below class Town: state = StateClass() cities = CityClass() Is there way to introspect such that one can list the properties keys and their values in such a way that it would look like playing around dictionary ? Regards, Janus -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Wed Dec 28 07:08:09 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 28 Dec 2011 04:08:09 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> On Dec 28, 8:08?am, Chris Angelico wrote: > On Wed, Dec 28, 2011 at 5:25 PM, Steven D'Aprano > > > > > > > > > > wrote: > > On Wed, 28 Dec 2011 15:06:37 +1100, Chris Angelico wrote: > > >> ... suppose you have a huge > >> set/frozenset using tuples as the keys, and one of your operations is to > >> shorten all keys by removing their first elements. Current Python > >> roughly doubles the cost of this operation, since you can't choose what > >> type the tail is made into. > > > The First Rule of Program Optimization: > > - Don't do it. > > > The Second Rule of Program Optimization (for experts only): > > - Don't do it yet. > > > Building syntax to optimize imagined problems is rarely a good idea. The > > difference between 2 seconds processing your huge set and 4 seconds > > processing it is unlikely to be significant unless you have dozens of > > such huge sets and less than a minute to process them all. > > > And your idea of "huge" is probably not that big... it makes me laugh > > when people ask how to optimize code "because my actual data has HUNDREDS > > of items!". Whoop-de-doo. Come back when you have a hundred million > > items, then I'll take your question seriously. > > > (All references to "you" and "your" are generic, and not aimed at Chris > > personally. Stupid English language.) > > And what you're seeing there is the _best possible_ situation I could > think of, the strongest possible justification for new syntax. > Granted, that may say more about me and my imagination than about the > problem, but the challenge is open: Come up with something that > actually needs this. > > ChrisA I personally feel any performance benefits are but a plus; they are not the motivating factor for this idea. I simply like the added verbosity and explicitness, thats the bottom line. From emekamicro at gmail.com Wed Dec 28 07:13:41 2011 From: emekamicro at gmail.com (Emeka) Date: Wed, 28 Dec 2011 13:13:41 +0100 Subject: Get Class properties In-Reply-To: References: Message-ID: Hello All, I have seen what I am looking for.. __dict__. Thanks! Regards, Janus On Wed, Dec 28, 2011 at 1:05 PM, Emeka wrote: > > Hello All, > > Say I have a class like below > > class Town: > state = StateClass() > cities = CityClass() > > > Is there way to introspect such that one can list the properties keys and > their values in such a way that it would look like playing around > dictionary ? > > Regards, > Janus > -- > *Satajanus Nig. Ltd > > > * > -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Wed Dec 28 07:25:29 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Wed, 28 Dec 2011 04:25:29 -0800 (PST) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> Message-ID: <5dd7607f-b5d6-4785-8b59-fd73d8d8c1d9@p4g2000vbt.googlegroups.com> On Dec 28, 2:56?am, Rick Johnson wrote: > On Dec 27, 3:44?pm, Eelco wrote: > > > Despite the fact that you mis-attributed that quote to me, im going to > > be a little bit offended in the name of its actual author anyway. > > Thats a lot of words to waste on your linguistic preferences. > > Personally, I reserve the right to botch my non-native languages as > > much as I please. > > I hope you're just joking a bit because i have little respect for > those who refuse to better themselves. If you are learning English as > a second language then you have a legitimacy excuse, but at some point > that excuse just becomes a lie. In any case, i apologize for mis- > quoting you. Yes, I was joking a bit; I learned my english primarily on programming boards, and im proud to say it rivals that of a majority of native speakers (low bar to beat, true). Furthermore, you are free to direct criticism at my writing or that of anyone else, but I must say I dont much care to hear it. A language is learned by using it, in reading, writing or speech; not by grammar nazis, or style nazis for that matter. Im here to discuss issues related to python, and anyone who manages to make himself understood is welcome to do so, as far as I am concerned. Im much more worried whether they have something interesting to contribute to the actual discussion. Not getting stuck picking nits, fighting personal feuds or getting dragged into the swamp by trolls; those are the real challenges, in my opinion. If you are insistent on bettering yourself; there is half a dozen other languages we could continue the conversation in. Your marginal gain per sentence read and written might be much larger there than in english. From rozelak at volny.cz Wed Dec 28 08:01:49 2011 From: rozelak at volny.cz (rozelak at volny.cz) Date: Wed, 28 Dec 2011 14:01:49 +0100 (CET) Subject: =?iso-8859-2?Q?Re:_Slices_when_extending_python_with_C=2B=2B?= Message-ID: <18d00d51ecfd54dcfeb18e1708f6c77d@mail2.volny.cz> Dear Robert, thank you very much for your answer. I understand what you mean and I have looked at slice object and C-api methods it provides. It should be easy to implement it. The only question is how exactly yo implement the general getter, since sq_item you mention (assume you mean PySequenceMethods.sq_item) has the following signature: PyObject * (* ssizeargfunc)(PyObject *, Py_ssize_t) accepting Py_ssize_t as the index, not a PyObject * which would hold the slice. So, how exactly to implement the getter? As a general method named __getitem__ registered in PyMethodDef? Or in another way? Thank you very much again. Regards, Dan ----- P?VODN? ZPR?VA ----- Od: "Robert Kern" Komu: python-list at python.org P?edm?t: Re: Slices when extending python with C++ Datum: 28.12.2011 - 10:24:42 > On 12/27/11 11:02 PM, rozelak at volny.cz wrote: > > Hallo, > > I have kind of special question when extening > > python with C++ > > > implemented modules. > > > > I try to implement a class, behaving also like > > an array. And I need > > > to implement slice-getters. I implemented > > PySequenceMethods.sq_slice > > > to get "simple" slices like: > > > > myobj[x:y] > > > > It works perfectly, without problems. But now I > > have a problem when > > > need to access the array with "special" slices, > > like: > > > > > myobj[x:y:step] > > myobj[::-1] # to revert the array > > > > I would like to ask which method must be > > implemented to get this > > > "special" slice getters. The "simple" slice > > PySequenceMethods.sq_slice getter accepts only > > two indexes - from, > > > to, but not a step (which would solve the > > issue). > > > The sq_slice slot is deprecated, just like the > __getslice__() method on the > Python side. Instead, implement sq_item to accept > a slice object in addition to > integer indices. > > http://docs.python.org/c-api/slice.html > > -- > 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 > -- Tradi?n? i modern? adventn? a novoro?n? zvyky, sv?te?n? j?dlo a pit?, v?zdoba a d?rky... - ?t?te v?no?n? a silvestrovsk? speci?l port?lu VOLN?.cz na http://web.volny.cz/data/click.php?id=1301 From robert.kern at gmail.com Wed Dec 28 08:18:36 2011 From: robert.kern at gmail.com (Robert Kern) Date: Wed, 28 Dec 2011 13:18:36 +0000 Subject: Slices when extending python with C++ In-Reply-To: <18d00d51ecfd54dcfeb18e1708f6c77d@mail2.volny.cz> References: <18d00d51ecfd54dcfeb18e1708f6c77d@mail2.volny.cz> Message-ID: On 12/28/11 1:01 PM, rozelak at volny.cz wrote: > Dear Robert, > > thank you very much for your answer. I understand what you mean and > I have looked at slice object and C-api methods it provides. It > should be easy to implement it. > > The only question is how exactly yo implement the general getter, > since sq_item you mention (assume you mean > PySequenceMethods.sq_item) has the following signature: > > PyObject * (* ssizeargfunc)(PyObject *, Py_ssize_t) > > accepting Py_ssize_t as the index, not a PyObject * which would hold > the slice. > > So, how exactly to implement the getter? As a general method named > __getitem__ registered in PyMethodDef? Or in another way? Sorry, PyMappingMethods.mp_subscript is the general function that you need to implement. -- 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 From prabhap2w at gmail.com Wed Dec 28 09:46:19 2011 From: prabhap2w at gmail.com (PRABHAKARAN K) Date: Wed, 28 Dec 2011 06:46:19 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From prabhap2w at gmail.com Wed Dec 28 09:47:07 2011 From: prabhap2w at gmail.com (PRABHAKARAN K) Date: Wed, 28 Dec 2011 06:47:07 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From k.sahithi2862 at gmail.com Wed Dec 28 10:11:30 2011 From: k.sahithi2862 at gmail.com (SAHITHI) Date: Wed, 28 Dec 2011 07:11:30 -0800 (PST) Subject: NEW HOT PHOTOS & VIDEOS Message-ID: <02fd5c97-0ebb-4c5c-8411-554638b8eb0c@37g2000prc.googlegroups.com> FOR GOOD JOBS SITES TO YOU http://goodjobssites.blogspot.com/ SRIKANTH DEVARA MOVIE GALLERY http://actressgallery-kalyani.blogspot.com/2011/12/devaraya-movie-stills.html O MANASA MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/o-manasa-movie-stills.html KULLUMANALI MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/kullu-manali-movie-stills.html BUSINESSMAN MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/businessman-movie-stills.html HOT ACTRESS BIKINI STILLS IN 2012 CALENDAR http://actressgallery-kalyani.blogspot.com/2011/12/2012-actress-calendar-wallpapers.html SAMANTHA SOUTHSCOPE HOT PHOTOS http://actressgallery-kalyani.blogspot.com/2011/12/samantha-at-south-scope-magazine.html DEEPIKA PADUKONE SPICY WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/deepika-padukone.html KATRINA KAIF LATEST HOT WALLPAPERS http://actressgallery-kalyani.blogspot.com/2011/12/katrina-kaif-wallpapers.html LOVE FAILURE MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/love-failure-movie-stills.html 4FRIENDS MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/4-friends-movie-stills.html NANDEESWARUDU MOVIE STILLS http://actressgallery-kalyani.blogspot.com/2011/12/nandeeswarudu-movie-stills.html HARI PRIYA HOT PHOTO STILLS http://actressgallery-kalyani.blogspot.com/2011/12/haripariya-actress.html SHRUTI HASSAN HOT IN 3 MOVIE http://actressgallery-kalyani.blogspot.com/2011/11/shruti-hassan-in-3-movie.html PANJA MOVIE LATEST STILLS http://actressgallery-kalyani.blogspot.com/2011/11/panja-movie-stills.html NIPPU MOVIE WORKING STILLS http://actressgallery-kalyani.blogspot.com/2011/11/nippu-movie-stills.html FOR FAST UPDATES IN FILM INDUSTRY KATRINA KAIF RARE PHOTOS http://allyouwants.blogspot.com/2011/12/katrina-kaif.html KAJAL AGARWAL LATEST STILLS http://allyouwants.blogspot.com/2011/03/kajal-latest-stills.html PRIYANKA CHOPRA LATEST HOT PHOTOSHOOT http://allyouwants.blogspot.com/2011/08/priyanka-chopra.html TAMIL ACTRESS HOT PHOTOS&VIDEOS http://allyouwants.blogspot.com/2011/08/tamil-actress.html FOR ONLY HOT GUYS SEE THIS LATEST HOT KATRINA KAIF PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/katrina-kaif-hot.html ANUSHKA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/anushka-hot.html PRIYANKA TIWARI HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/12/priyanka-tiwari-hot.html TAMANNA LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/08/tamanna-hot.html PARUL HOT PHOTO STILLS http://hotactress-kalyani.blogspot.com/2011/12/parul-hot.html ADITI AGARWAL NEW ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/12/aditi-agarwal-hot.html PAYAL GHOSH HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/payal-ghosh-hot.html RAGINI DWIVEDI HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/12/ragini-dwivedi.html PARVATHI MELTON LATEST HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/parvathi-melton-hot.html SARAH JANE DIAS HOT PHOTOS http://hotactress-kalyani.blogspot.com/2011/11/sarah-jane-dias-hot.html KAJAL AGARWAL HOT SAREE STILLS http://hotactress-kalyani.blogspot.com/2011/11/kajal-agarwal-hot-in-saree.html POONAM KAUR HOT ROMANTIC STILLS http://hotactress-kalyani.blogspot.com/2011/11/poonam-kaur-hot.html From bahamutzero8825 at gmail.com Wed Dec 28 10:59:00 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Wed, 28 Dec 2011 09:59:00 -0600 Subject: RSA and Cryptography in PYTHON In-Reply-To: <24097307.586.1325029506304.JavaMail.geo-discussion-forums@prmw6> References: <31261167.63.1324843108857.JavaMail.geo-discussion-forums@prdv33> <24097307.586.1325029506304.JavaMail.geo-discussion-forums@prmw6> Message-ID: <4EFB3CC4.6080409@gmail.com> On 12/27/2011 5:45 PM, 88888 Dihedral wrote: > I am getting lousy in the news group in my writing? > > I mean the non-trivial decoding of the key decomposition. > Of course RSA is symmetric in encoding and decoding of data. You really are a markov chain bot. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From lorenzo.digregorio at gmail.com Wed Dec 28 11:18:10 2011 From: lorenzo.digregorio at gmail.com (Lorenzo Di Gregorio) Date: Wed, 28 Dec 2011 08:18:10 -0800 (PST) Subject: Global variables in a C extension for Python Message-ID: Hello, I've written a C extension for Python which works so far, but now I've stumbled onto a simple problem for which I just can't find any example on the web, so here I am crying for help ;-) I'll trying to reduce the problem to a minimal example. Let's say I need to call from Python functions of a C program like: static int counter = 0; void do_something(...) { ... counter++; ... } void do_something_else(...) { ... counter++; ... } So they access a common global variable. I've written the wrappers for the functions, but I'd like to place "counter" in the module's space and have wrappers accessing it like self->counter. I do not need to make "counter" visible to Python, I just need the global static variable available for C. I've got somehow a clue of how this should work, but not much more than a clue, and I'd appreciate to see a simple example. Best Regards, Lorenzo From wolftracks at invalid.com Wed Dec 28 11:33:15 2011 From: wolftracks at invalid.com (W. eWatson) Date: Wed, 28 Dec 2011 08:33:15 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/27/2011 7:31 PM, W. eWatson wrote: > On 12/27/2011 6:27 PM, Ian Kelly wrote: >> On Tue, Dec 27, 2011 at 6:21 PM, W. eWatson >> wrote: >>> Well, it found several problems. These DLLs >>> MSVCP1 >>> EFSADU >>> MSJAVA. >> >> I'm guessing MSVCP1 is a typo for MSVCP71? If that is missing then >> that is probably the culprit. That DLL is the C runtime library. It >> is supposed to be shipped with applications that need it, but it is so >> ubiquitous that it is often assumed to be present or forgotten. You >> can download it from >> http://www.dll-files.com/dllindex/dll-files.shtml?msvcp71 (or just >> find it on another Windows XP PC) and copy it into >> C:\Windows\System32. Don't forget to run regsvr32 to register it. >> >> HTH, >> Ian > You are very likely right about the spelling. I wrote it down, and > carried it to this PC. Sometimes I can't read my own writing. > ... Well, thing went slightly awry. The link gave me two choices. Download msv...dll fixer, and download fixer. I took the latter. However, just checking on the other one, found that I got the same exe file. I installed it on the XP PC, and pressed what looked like a reasonable place to start. It would download the dll, and register it. Well, it seemed more interested in the registry. It scanned four areas of the registry, and found 123 problems in total. To fix them would require buying something. I noticed a large button near the top that said download dll. It found one the internet, and guess what? More purchase for the download. It seems like dll-fixer has a corner on the market. I found this , but he almost has too much to say. He issues a warning about getting a dll off the web. He does offer this. Run the sfc /scannow System File Checker command to replace a missing or corrupt copy of the msvcp71.dll file. If this DLL file is provided my Microsoft, the System File Checker tool should restore it. I guess I'm missing something here. OK, I'm borrowing one from my XP laptop. Back later. From wolftracks at invalid.com Wed Dec 28 11:46:48 2011 From: wolftracks at invalid.com (W. eWatson) Date: Wed, 28 Dec 2011 08:46:48 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: A new dilemma. The PC XP in question with Python has the the msvcp71.dll file in System32. The one I took off my other laptop has a slightly newer one. Feb 2003 vs Aug 2003. Perhaps the (python PC) has a corrupt one? From affdfsdfdsfsd at b.com Wed Dec 28 12:16:48 2011 From: affdfsdfdsfsd at b.com (Tracubik) Date: 28 Dec 2011 17:16:48 GMT Subject: pls explain this flags use Message-ID: <4efb4f00$0$1393$4fafbaef@reader1.news.tin.it> Hi all, i've found here (http://python-gtk-3-tutorial.readthedocs.org/en/latest/ layout.html#table) this code: [quote] If omitted, xoptions and yoptions defaults to Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL. [end quote] xoptions have 3 flags: EXPAND, FILL, SHRINK so, how it is supposed to work? it means that by default xoptions is set to EXPAND true, FILL true, SHRINK false? how it work? "|" is used with bits afaik, so i suppouse the xoptions is 3 bits? and the default is EXPAND FILL SHRINK 1 1 0 (or something similar) so i have AttachOptions.EXPAND = 100 AttachOptions.FILL = 010 EXPAND|FILL = 100|010 = 110 is that right? so if i want EXPAND *and* FILL i have to make EXPAND *binary or* FILL? i'm new at this sintassi so i'm trying to guess on my own, pls feel free to tell me if i'm wrong and where thanks and best wishes Medeo From ian.g.kelly at gmail.com Wed Dec 28 12:37:14 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 28 Dec 2011 10:37:14 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On Wed, Dec 28, 2011 at 9:33 AM, W. eWatson wrote: > Well, thing went slightly awry. The link gave me two choices. Download > msv...dll fixer, and download fixer. I took the latter. However, just > checking on the other one, found that I got the same exe file. I installed > it on the XP PC, and pressed what looked like a reasonable place to start. > It would download the dll, and register it. > > Well, it seemed more interested in the registry. It scanned four areas of > the registry, and found 123 problems in total. To fix them would require > buying something. > > I noticed a large button near the top that said download dll. It found one > the internet, and guess what? More purchase for the download. > > It seems like dll-fixer has a corner on the market. Definitely don't download that spamware fixer program -- who knows what that does? You want the grey "Download zip-file" link, not the not the flashy "Download fixer" ad links. Anyway, it sounds like you've found another copy of the DLL. If I were you, I would uninstall that fixer ASAP and then run an anti-malware and anti-virus tool or two, just in case. > Run the sfc /scannow System File Checker command to replace a missing or > corrupt copy of the msvcp71.dll file. If this DLL file is provided my > Microsoft, the System File Checker tool should restore it. I don't believe it is provided by Microsoft, but it wouldn't hurt to try. > A new dilemma. The PC XP in question with Python has the the msvcp71.dll > file in System32. The one I took off my other laptop has a slightly newer > one. Feb 2003 vs Aug 2003. Weird. Try registering the existing dll, try replacing it with the other one (be sure to back up the original first), etc. From tinnews at isbd.co.uk Wed Dec 28 12:38:33 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Wed, 28 Dec 2011 17:38:33 +0000 Subject: Python app installs itself in two places - why? Message-ID: I have installed an application called pycocuma on my xubuntu 11.10 system. It works OK and I'm aiming to develop it a little as its 'owner' has long since stopped work on it. However I'm a little puzzled by the way it has installed itself (it's a standard package from the Ubuntu repositories), all the python source is installed in two places:- /usr/lib/python2.7/dist-packages/pycocumalib /usr/share/pyshared/pycocumalib All the files in /usr/lib/python2.7/dist-packages/pycocumalib are actually symbolic links to the ones in /usr/share/pyshared/pycocumalib but I just wondered why they're in both places. Is it just 'historic' or is there a good sound reason for it? -- Chris Green From lie.1296 at gmail.com Wed Dec 28 12:54:05 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 29 Dec 2011 04:54:05 +1100 Subject: Possible bug in string handling (with kludgy work-around) In-Reply-To: References: <4ef91afb$0$29973$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/28/2011 11:57 AM, Rick Johnson wrote: > On Dec 27, 3:38 pm, Terry Reedy wrote: >> On 12/27/2011 1:04 PM, Rick Johnson wrote: >> >>> But this brings up a very important topic. Why do we even need triple >>> quote string literals to span multiple lines? Good question, and one i >>> have never really mused on until now. >> >> I have, and the reason I thought of is that people, including me, too >> ofter forget or accidentally fail to properly close a string literal, > > Yes, agreed. > >> Color coding editors make it easier to catch such errors, but they were >> less common in 1991. > > I would say the need for triple quote strings has passed long ago. > Like you say, since color lexers are ubiquitous now we don't need > them. > >> And there is still uncolored interactive mode. > > I don't see interactive command line programming as a problem. I mean, > who drops into a cmd line and starts writing paragraphs of string > literals? Typically, one would just make a few one-liner calls here or > there. Also, un-terminated string literal errors can be very > aggravating. Not because they are difficult to fix, no, but because > they are difficult to find! -- and sending me an error message > like... > > "Exception: Un-terminated string literal meets EOF! line: 50,466,638" > > ... is about as helpful as a bullet in my head! > > If the interpreter finds itself at EOF BEFORE a string closes, don't > you think it would be more helpful to include the currently "opened" > strings START POSITION also? No it wouldn't. Once you get an unterminated string literal, the string would terminate at the next string opening. Then it would fuck the parser since it will try to parse what was supposed to be a string literal as a code. For example: hello = 'bar' s = "boo, I missed a quote here print 'hello = ', hello, "; s = ", s the parser would misleadingly show that you have an unclosed string literal here: vvv print 'hello = ', hello, "; s = ", s ^^^ instead of on line 2. While an experienced programmer should be able to figure out what's wrong, I can see a beginner programmer trying to "fix" the problem like this: print 'hello = ', hello, "; s = ", s" and then complaining that print doesn't print. Limiting string literals to one line limits the possibility of damage to a single line. You will still have the same problem if you missed to close triple-quoted string, but since triple-quoted string are much rarer and they're pretty eye-catching, this sort of error harder are much harder. From ian.g.kelly at gmail.com Wed Dec 28 12:56:58 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 28 Dec 2011 10:56:58 -0700 Subject: pls explain this flags use In-Reply-To: <4efb4f00$0$1393$4fafbaef@reader1.news.tin.it> References: <4efb4f00$0$1393$4fafbaef@reader1.news.tin.it> Message-ID: On Wed, Dec 28, 2011 at 10:16 AM, Tracubik wrote: > Hi all, > i've found here (http://python-gtk-3-tutorial.readthedocs.org/en/latest/ > layout.html#table) this code: > > [quote] > If omitted, xoptions and yoptions defaults to > Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL. > [end quote] > > xoptions have 3 flags: EXPAND, FILL, SHRINK > > so, how it is supposed to work? > it means that by default xoptions is set to EXPAND true, FILL true, > SHRINK false? Yes. > how it work? "|" is used with bits afaik, so i suppouse the xoptions is 3 > bits? Yes. > and the default is > EXPAND FILL SHRINK > ?1 ? ? ?1 ? ? 0 > > (or something similar) > > so i have > > AttachOptions.EXPAND = 100 > AttachOptions.FILL ? = 010 > > EXPAND|FILL = 100|010 = 110 > > is that right? > so if i want EXPAND *and* FILL i have to make EXPAND *binary or* FILL? Yes. Binary or is used to set flags, binary and is used to test whether specific flags are set, e.g.: if options & AttachOptions.EXPAND: # do something expandy else: # do something not expandy From nirmaltech28 at gmail.com Wed Dec 28 13:02:13 2011 From: nirmaltech28 at gmail.com (Nirmal Kumar) Date: Wed, 28 Dec 2011 10:02:13 -0800 (PST) Subject: reverse() is not working Message-ID: <20618381.643.1325095333260.JavaMail.geo-discussion-forums@prez5> I am trying to pass the id to thanks view through reverse. But it's not working. I'm getting this error Reverse for 'reg.views.thanks' with arguments '(20,)' and keyword arguments '{}' not found. I posted the question with the code in stackoverflow: http://stackoverflow.com/questions/8648196/reverse-is-not-working How can I transfer arguments from one function to another in views?. Is it a normal thing or I have to use any built in functions? Thanks. From lie.1296 at gmail.com Wed Dec 28 13:37:08 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 29 Dec 2011 05:37:08 +1100 Subject: reverse() is not working In-Reply-To: <20618381.643.1325095333260.JavaMail.geo-discussion-forums@prez5> References: <20618381.643.1325095333260.JavaMail.geo-discussion-forums@prez5> Message-ID: On 12/29/2011 05:02 AM, Nirmal Kumar wrote: > I am trying to pass the id to thanks view through reverse. But it's not working. I'm getting this error > > Reverse for 'reg.views.thanks' with arguments '(20,)' and keyword arguments '{}' not found. > > I posted the question with the code in stackoverflow: > > http://stackoverflow.com/questions/8648196/reverse-is-not-working > > How can I transfer arguments from one function to another in views?. Is it a normal thing or I have to use any built in functions? > > Thanks. Welcome to Python Mailing List; your question is about django, django have their own mailing list, your question is probably better asked there. From tinnews at isbd.co.uk Wed Dec 28 14:04:59 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Wed, 28 Dec 2011 19:04:59 +0000 Subject: Where does this readOne() method come from? Message-ID: In the (rather sparse) documentation for the vobject package it has, in the section about parsing iCalendar objects, the following:- Parsing iCalendar objects ========================= To parse one top level component from an existing iCalendar stream or string, use the readOne function: >>> parsedCal = vobject.readOne(icalstream) >>> parsedCal.vevent.dtstart.value datetime.datetime(2006, 2, 16, 0, 0, tzinfo=tzutc()) Similarly, readComponents is a generator yielding one top level component at a time from a stream or string. >>> vobject.readComponents(icalstream).next().vevent.dtstart.value datetime.datetime(2006, 2, 16, 0, 0, tzinfo=tzutc()) More examples can be found in source code doctests. However *nowhere* can I find anything that tells me what or where the readOne() is. It's not to be found in the full epydoc API documentation for vobject (or, at least, I can't find it). All I want to do is read a .ics file and parse it. I used to use the icalendar package but that seems less well supported than vobject so I'm trying to use vobject instead but I'm not getting far at present. It sort of feels like "everyone knows what readOne() is", but I don't! :-) -- Chris Green From rantingrickjohnson at gmail.com Wed Dec 28 14:36:17 2011 From: rantingrickjohnson at gmail.com (Rick Johnson) Date: Wed, 28 Dec 2011 11:36:17 -0800 (PST) Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 28, 12:58?am, Steven D'Aprano wrote: > On Tue, 27 Dec 2011 21:34:19 -0800, Rick Johnson wrote: > > I am also thinking that ANY quote char is a bad choice for string > > literal delimiters. Why? Well because it is often necessary to embed > > single or double quotes into a string literal. We need to use a > > delimiter that is not a current delimiter elsewhere in Python, and also, > > is a very rare char. I believe Mr Ewing found that perfect char in his > > "Multi-line uber raw string literals!" (Just scroll down a bit at this > > link)... > > > ? ?http://www.cosc.canterbury.ac.nz/greg.ewing/python/ideas.html > > Not surprisingly, you haven't thought this through. I'm sure Greg Ewing > has, which is why he hasn't proposed *replacing* string delimiters with > his multi-line format. That's why he proposed it as a *statement* and not > string-builder syntax. > > Without an end-delimiter, how do you embed string literals in expressions? Did you even read what i wrote? And if you did, you missed the point! My point was... while Greg's idea is nice, it is not the answer. HOWEVER, he did find the perfect char, and that char is the pipe! --> | mlstr = ||| this is a multi line sting that is delimited by "triple pipes". Or we could just 'single pipes' if we like, however, i think the "triple pipe' is easier to see. Since the pipe char is so rare in Python source, it becomes the obvious choice. And, best of all, no more worries about "embedded quotes". YAY! ||| slstr = |this is a single line string| The point is people, we should be using string delimiters that are ANYTHING besides " and '. Stop being a sheep and use your brain! From dbinks at codeaurora.org Wed Dec 28 15:10:12 2011 From: dbinks at codeaurora.org (Dominic Binks) Date: Wed, 28 Dec 2011 12:10:12 -0800 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EFB77A4.4010605@codeaurora.org> On 12/28/2011 11:36 AM, Rick Johnson wrote: > On Dec 28, 12:58 am, Steven D'Aprano +comp.lang.pyt... at pearwood.info> wrote: >> On Tue, 27 Dec 2011 21:34:19 -0800, Rick Johnson wrote: > >>> I am also thinking that ANY quote char is a bad choice for string >>> literal delimiters. Why? Well because it is often necessary to embed >>> single or double quotes into a string literal. We need to use a >>> delimiter that is not a current delimiter elsewhere in Python, and also, >>> is a very rare char. I believe Mr Ewing found that perfect char in his >>> "Multi-line uber raw string literals!" (Just scroll down a bit at this >>> link)... >> >>> http://www.cosc.canterbury.ac.nz/greg.ewing/python/ideas.html >> >> Not surprisingly, you haven't thought this through. I'm sure Greg Ewing >> has, which is why he hasn't proposed *replacing* string delimiters with >> his multi-line format. That's why he proposed it as a *statement* and not >> string-builder syntax. >> >> Without an end-delimiter, how do you embed string literals in expressions? > > Did you even read what i wrote? And if you did, you missed the point! > > My point was... while Greg's idea is nice, it is not the answer. > HOWEVER, he did find the perfect char, and that char is the pipe! --> > | > > mlstr = ||| > this is a > multi line sting that is > delimited by "triple pipes". Or we > could just 'single pipes' if we like, however, i think > the "triple pipe' is easier to see. Since the pipe char > is so rare in Python source, it becomes the obvious > choice. And, best of all, no more worries about > "embedded quotes". YAY! > ||| > > slstr = |this is a single line string| > > The point is people, we should be using string delimiters that are > ANYTHING besides " and '. Stop being a sheep and use your brain! I disagree that quotes are a bad thing. Most programming languages use +, -, / and * for arithmentic operations (* is closest character on a keyboard to x that is not the letter ex). Why do they choose to do this? It's actually a lot more work for the language implementer(s) to do this rather than simply providing functions add(x,y), subtract(x,y), divide(x,y) and multiply(x,y). The answer is very simple - convention. We use these symbols in mathematics and so it's much more convenient to follow through the mathematical convention. The learning curve is much lower (and it's less typing). After all experienced programmers know this is what is going on under the hood in pretty much all programming languages. Compilers are just making it easy for us. While it may indeed make sense to use a different character for delimiting strings, in English we use " to delimit spoken words in a narrative. Since strings most closely resemble spoken words from the point of view a programming language, using double quotes is a sensible choice since it eases learning. Convention is a very strong thing to argue against (not to mention huge code breakage as a result of such a change). Personally, I try to ensure I use consistency in the way I use the different quoting mechanisms, but that's just a personal choice. Sometimes that leads me to less pleasant looking strings, but I believe the consistency of style makes it easier to read. I think this proposal falls in the, let's make python case-insensitive kind of idea - it's never going to happen cause it offers very little benefit at huge cost. And I'm not going to contribute to this thread any further cause it's a pointless waste of my time to write it and others time to read it. -- Dominic Binks: dbinks at codeaurora.org Employee of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum From lie.1296 at gmail.com Wed Dec 28 15:13:17 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 29 Dec 2011 07:13:17 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/29/2011 06:36 AM, Rick Johnson wrote: > > mlstr = ||| > this is a > multi line sting that is > delimited by "triple pipes". Or we > could just 'single pipes' if we like, however, i think > the "triple pipe' is easier to see. Since the pipe char > is so rare in Python source, it becomes the obvious > choice. And, best of all, no more worries about > "embedded quotes". YAY! > ||| > > slstr = |this is a single line string| > > The point is people, we should be using string delimiters that are > ANYTHING besides " and '. Stop being a sheep and use your brain! This will incur the wrath of all linux/unix sysadmins all over the world. You are just replacing one problem with another; in your obliviousness, you had just missed the very obvious fact that once you replace quotes with pipes, quotes is extremely rare in Python (in fact you won't be seeing any quotes except inside string literals). From tjreedy at udel.edu Wed Dec 28 15:41:38 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 28 Dec 2011 15:41:38 -0500 Subject: Where does this readOne() method come from? In-Reply-To: References: Message-ID: On 12/28/2011 2:04 PM, tinnews at isbd.co.uk wrote: > In the (rather sparse) documentation for the vobject package it has, > in the section about parsing iCalendar objects, the following:- > > Parsing iCalendar objects > ========================= > > To parse one top level component from an existing iCalendar stream or > string, use the readOne function: > > >>> parsedCal = vobject.readOne(icalstream) It is obviously supposed to come from the 'vobject' package, whatever that is. I have never heard of vobject before, though. Try reading the source if the doc is limited. Or ask the author or mailing list if there is one. > It sort of feels like "everyone knows what readOne() is", Nope PS You already know the answer to the question you asked in the subject line. Better would have been "What is the vobject.readOne function?" to catch the eye of someone who *does* know something about vobject. -- Terry Jan Reedy From rosuav at gmail.com Wed Dec 28 15:45:31 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 07:45:31 +1100 Subject: Where does this readOne() method come from? In-Reply-To: References: Message-ID: On Thu, Dec 29, 2011 at 6:04 AM, wrote: > In the (rather sparse) documentation for the vobject package it has, > in the section about parsing iCalendar objects, the following:- > > ? ?>>> parsedCal = vobject.readOne(icalstream) Presumably you have this vobject package. Assuming it's installed correctly, all you need to do is: import vobject and then vobject.readOne should be available. Chris Angelico From ian.g.kelly at gmail.com Wed Dec 28 15:52:31 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 28 Dec 2011 13:52:31 -0700 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Dec 28, 2011 at 12:36 PM, Rick Johnson wrote: > My point was... while Greg's idea is nice, it is not the answer. > HOWEVER, he did find the perfect char, and that char is the pipe! --> > | > > mlstr = ||| > this is a > multi line sting that is > delimited by "triple pipes". Or we > could just 'single pipes' if we like, however, i think > the "triple pipe' is easier to see. Since the pipe char > is so rare in Python source, it becomes the obvious > choice. And, best of all, no more worries about > "embedded quotes". YAY! > ||| > > slstr = |this is a single line string| > > The point is people, we should be using string delimiters that are > ANYTHING besides " and '. Stop being a sheep and use your brain! So those who do shell scripting from Python might replace this: subprocess.call("cat {0} | awk '/foo|bar/ {print $3;}' | sort | uniq >{1}".format(infile, outfile), shell=True) with this: subprocess.call(|cat {0} \| awk '/foo\|bar/ {print $3;}' \| sort \| uniq >{1}|.format(infile, outfile), shell=True) or if we combine Rick's string escaping proposal: subprocess.call(|cat {0} awk '/foobar/ {print $3;}' sort uniq {1}|.format(infile, outfile), shell=True) Yeah, that's so much better. I especially like how nice and readable the regex is now. From lie.1296 at gmail.com Wed Dec 28 15:53:39 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 29 Dec 2011 07:53:39 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: On 12/28/2011 04:34 PM, Rick Johnson wrote: > On Dec 27, 9:49 pm, Rick Johnson wrote: > >> The fact is...even with the multi-line issue solved, we still have two >> forms of literal delimiters that encompass two characters resulting in >> *four* possible legal combinations of the exact same string! I don't >> know about you guys, but i am not a big fan of Tim Towtdi. > > actually i was a bit hasty with that statment and underestimated the > actual number of possiblities. > > 1) "this is a string" > 2) 'this is a string' > 3) r"this is a string" > 4) r'this is a string' > 5) '''this is a string''' > 6) """this is a string""" > 7) r'''this is a string''' > 8) r"""this is a string""" you missed u"nicode" string and b"yte" string, each of them available in both single and double quote flavor and single and triple quote flavor. Also, it's possible to mix them together ur"unicode raw string" or br"byte raw string", they are also in single and double quote flavor and single and triple quote flavor. And of course, I can't believe you forget Guido's favourite version, g"", available in musical and sirloin cloth flavor. From wolftracks at invalid.com Wed Dec 28 15:55:34 2011 From: wolftracks at invalid.com (W. eWatson) Date: Wed, 28 Dec 2011 12:55:34 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/28/2011 9:37 AM, Ian Kelly wrote: > On Wed, Dec 28, 2011 at 9:33 AM, W. eWatson wrote: >> Well, thing went slightly awry. The link gave me two choices. Download >> msv...dll fixer, and download fixer. I took the latter. However, just >> checking on the other one, found that I got the same exe file. I installed >> it on the XP PC, and pressed what looked like a reasonable place to start. >> It would download the dll, and register it. >> >> Well, it seemed more interested in the registry. It scanned four areas of >> the registry, and found 123 problems in total. To fix them would require >> buying something. >> >> I noticed a large button near the top that said download dll. It found one >> the internet, and guess what? More purchase for the download. >> >> It seems like dll-fixer has a corner on the market. > > Definitely don't download that spamware fixer program -- who knows > what that does? You want the grey "Download zip-file" link, not the > not the flashy "Download fixer" ad links. Anyway, it sounds like > you've found another copy of the DLL. > > If I were you, I would uninstall that fixer ASAP and then run an > anti-malware and anti-virus tool or two, just in case. > >> Run the sfc /scannow System File Checker command to replace a missing or >> corrupt copy of the msvcp71.dll file. If this DLL file is provided my >> Microsoft, the System File Checker tool should restore it. > > I don't believe it is provided by Microsoft, but it wouldn't hurt to try. > >> A new dilemma. The PC XP in question with Python has the the msvcp71.dll >> file in System32. The one I took off my other laptop has a slightly newer >> one. Feb 2003 vs Aug 2003. > > Weird. Try registering the existing dll, try replacing it with the > other one (be sure to back up the original first), etc. I haven't installed the newer version yet, Aug 2003. I thought I'd see what happened if I entered sfc /scannow. Whatever, happened the black window flashed by in a split second. Somehow this doesn't seem helpful . Although, 20 lines down or so it says: If sfc discovers that a protected file has been overwritten, it retrieves the correct version of the file from the %systemroot%\system32\dllcache folder, and then replaces the incorrect file. I'm going to run this by a XP NG. From python at bdurham.com Wed Dec 28 16:03:34 2011 From: python at bdurham.com (python at bdurham.com) Date: Wed, 28 Dec 2011 16:03:34 -0500 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> Message-ID: <1325106214.5274.140661016836917@webmail.messagingengine.com> Lie, > And of course, I can't believe you forget Guido's favourite version, g"", available in musical and sirloin cloth flavor. LMAO! That was brilliant! :) Cheers! Malcolm From nathan.alexander.rice at gmail.com Wed Dec 28 16:24:50 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 28 Dec 2011 16:24:50 -0500 Subject: Py-dea: Streamline string literals now! In-Reply-To: <1325106214.5274.140661016836917@webmail.messagingengine.com> References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <1325106214.5274.140661016836917@webmail.messagingengine.com> Message-ID: Quotes are obnoxious in the nesting sense because everyone uses quotes for string delimiters. By the same token, quotes are wonderful because not only are they intuitive to programmers, but they are intuitive in general. Parenthesis are pretty much in the same boat... I *HATE* them nested, but they are so intuitive that replacing them is a non starter; Just write code that doesn't nest parenthesis. Nathan From rosuav at gmail.com Wed Dec 28 16:42:46 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 08:42:46 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <1325106214.5274.140661016836917@webmail.messagingengine.com> Message-ID: On Thu, Dec 29, 2011 at 8:24 AM, Nathan Rice wrote: > Quotes are obnoxious in the nesting sense because everyone uses quotes > for string delimiters. ?By the same token, quotes are wonderful > because not only are they intuitive to programmers, but they are > intuitive in general. ?Parenthesis are pretty much in the same boat... > I *HATE* them nested, but they are so intuitive that replacing them is > a non starter; ?Just write code that doesn't nest parenthesis. Parentheses have different starting and ending delimiters and must be 'properly nested' (ie there must be exactly-matching inner parens inside any given set of outer parens (note that English has similar rules - you can't mis-nest parentheses (at any depth) in either language)). You can't guarantee the same about quoted strings - suppose the starting delimiter were ' and the ending " (or vice versa), it still wouldn't deal with the issue of coming across an apostrophe inside a quoted string. In actual fact, the real problem is that quoted strings need to be able to contain _anything_. The only true solution to that is length-provided strings: s = "4spam q = "14Hello, world!\n This works beautifully in interchange formats, but rather poorly in source code (or, for that matter, anything editable). ChrisA From nathan.alexander.rice at gmail.com Wed Dec 28 17:24:47 2011 From: nathan.alexander.rice at gmail.com (Nathan Rice) Date: Wed, 28 Dec 2011 17:24:47 -0500 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <1325106214.5274.140661016836917@webmail.messagingengine.com> Message-ID: On Wed, Dec 28, 2011 at 4:42 PM, Chris Angelico wrote: > On Thu, Dec 29, 2011 at 8:24 AM, Nathan Rice > wrote: >> Quotes are obnoxious in the nesting sense because everyone uses quotes >> for string delimiters. ?By the same token, quotes are wonderful >> because not only are they intuitive to programmers, but they are >> intuitive in general. ?Parenthesis are pretty much in the same boat... >> I *HATE* them nested, but they are so intuitive that replacing them is >> a non starter; ?Just write code that doesn't nest parenthesis. > > Parentheses have different starting and ending delimiters and must be > 'properly nested' (ie there must be exactly-matching inner parens > inside any given set of outer parens (note that English has similar > rules - you can't mis-nest parentheses (at any depth) in either > language)). You can't guarantee the same about quoted strings - > suppose the starting delimiter were ' and the ending " (or vice > versa), it still wouldn't deal with the issue of coming across an > apostrophe inside a quoted string. I think you read more into my statement than was intended. Parens are bad like nested quotes are bad in the sense that they made statements difficult to read and confusing. While it is entirely possible to parse nested strings automatically in a probabilistic manner with nearly flawless accuracy by examining everything between the start and end of the line, generally I feel that people are uncomfortable with probabilistic techniques in the realm of programming :) Best just to make the user be explicit. Nathan From lie.1296 at gmail.com Wed Dec 28 17:29:24 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 29 Dec 2011 09:29:24 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <15424060.724.1324183952802.JavaMail.geo-discussion-forums@prix23> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> Message-ID: On 12/28/2011 11:08 PM, Eelco wrote: > I personally feel any performance benefits are but a plus; they are > not the motivating factor for this idea. I simply like the added > verbosity and explicitness, thats the bottom line. Any performance benefits are a plus, I agree, as long as it doesn't make my language looks like Perl. Now get off my lawn! From dilara.ally at gmail.com Wed Dec 28 17:51:25 2011 From: dilara.ally at gmail.com (Dilara Ally) Date: Wed, 28 Dec 2011 14:51:25 -0800 Subject: importing MySQLdb Message-ID: <4EFB9D6D.3080506@gmail.com> Hi All I'm trying to import the MySQLdb for python. I downloaded the proper setuptools egg (ver2.7) for a Mac with OSX10.6 I then downloaded the MySQL-python-1.2.3 and ran the following commands python setup.py build sudo python setup.py install Then to test that the module was properly loaded I used the interactive python prompt ad ran the following command: import MySQLdb The error message read: Traceback (most recent call last): File "", line 1, in File "MySQLdb/__init__.py", line 19, in import _mysql File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 7, in File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 6, in __bootstrap__ ImportError: dlopen(/Users/dally/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Users/dally/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so Reason: image not found Does anyone have any ideas on how to fix this problem? Any help will be greatly appreciated! Dilara -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Wed Dec 28 17:54:16 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 28 Dec 2011 22:54:16 GMT Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> On Wed, 28 Dec 2011 11:36:17 -0800, Rick Johnson wrote: > The point is people, we should be using string delimiters that are > ANYTHING besides " and '. Stop being a sheep and use your brain! "ANYTHING", hey? I propose we use ? and ? as the opening and closing string delimiters. Problem solved! Thank you Rick for yet another brilliant, well-thought-out idea. I look forward to seeing your fork of Python with this change. How is it going? I hope you aren't going to disappoint the legions of your fans who are relying on you to save the Python community from neglect. -- Steven From rosuav at gmail.com Wed Dec 28 18:08:07 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 10:08:07 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 29, 2011 at 9:54 AM, Steven D'Aprano wrote: > Thank you Rick for yet another brilliant, well-thought-out idea. I look > forward to seeing your fork of Python with this change. How is it going? > I hope you aren't going to disappoint the legions of your fans who are > relying on you to save the Python community from neglect. But Steven, the shocking state of IDLE has utterly destroyed any reputation Python may have had. There's not going to be any community left to neglect, soon! That said, though, the new string delimiters will solve many problems. We should adopt a strict policy: all syntactic elements MUST be comprised of non-ASCII characters, thus allowing all ASCII characters to represent themselves literally. I'm not sure what "representing themselves literally" would mean, since strings already have perfect delimiters, but it seems like a good policy. By the way, doubling the delimiter works for nesting strings. ?? and ?? look just fine. ChrisA From rozelak at volny.cz Wed Dec 28 18:32:27 2011 From: rozelak at volny.cz (rozelak at volny.cz) Date: Thu, 29 Dec 2011 00:32:27 +0100 (CET) Subject: =?iso-8859-2?Q?Re:_Slices_when_extending_python_with_C=2B=2B?= In-Reply-To: References: <18d00d51ecfd54dcfeb18e1708f6c77d@mail2.volny.cz> Message-ID: <0c601b98d7f6993c45d7cf533f1f8b43@mail2.volny.cz> Great, it's working! Thank you very much, Robert! Dan T. ----- P?VODN? ZPR?VA ----- Od: "Robert Kern" Komu: python-list at python.org P?edm?t: Re: Slices when extending python with C++ Datum: 28.12.2011 - 14:18:36 > On 12/28/11 1:01 PM, rozelak at volny.cz wrote: > > Dear Robert, > > > > thank you very much for your answer. I > > understand what you mean and > > > I have looked at slice object and C-api methods > > it provides. It > > > should be easy to implement it. > > > > The only question is how exactly yo implement > > the general getter, > > > since sq_item you mention (assume you mean > > PySequenceMethods.sq_item) has the following > > signature: > > > > > PyObject * (* ssizeargfunc)(PyObject *, > > Py_ssize_t) > > > > > accepting Py_ssize_t as the index, not a > > PyObject * which would hold > > > the slice. > > > > So, how exactly to implement the getter? As a > > general method named > > > __getitem__ registered in PyMethodDef? Or in > > another way? > > > Sorry, PyMappingMethods.mp_subscript is the > general function that you need to > implement. > > -- > 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 > -- Tradi?n? i modern? adventn? a novoro?n? zvyky, sv?te?n? j?dlo a pit?, v?zdoba a d?rky... - ?t?te v?no?n? a silvestrovsk? speci?l port?lu VOLN?.cz na http://web.volny.cz/data/click.php?id=1301 From rosuav at gmail.com Wed Dec 28 19:27:21 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 11:27:21 +1100 Subject: Get Class properties In-Reply-To: References: Message-ID: On Wed, Dec 28, 2011 at 11:13 PM, Emeka wrote: > Hello All, > > I have seen what I am looking for.. __dict__. > Yep! You may also want to look at the dir() function. Chris Angelico From dan at tombstonezero.net Wed Dec 28 20:44:18 2011 From: dan at tombstonezero.net (Dan Sommers) Date: Thu, 29 Dec 2011 01:44:18 +0000 (UTC) Subject: Py-dea: Streamline string literals now! References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, 28 Dec 2011 22:54:16 +0000, Steven D'Aprano wrote: > On Wed, 28 Dec 2011 11:36:17 -0800, Rick Johnson wrote: > >> The point is people, we should be using string delimiters that are >> ANYTHING besides " and '. Stop being a sheep and use your brain! > > "ANYTHING", hey? > > I propose we use ? and ? as the opening and closing string delimiters. > Problem solved! Why stop at pre-determined, literal delimiters? That's way too easy on the parser, whether that parser is a computer or a person. __string_delimiter__ = ? # magic syntax; no quotes needed x = ?hello? __string_delimiter__ = # # that first # isn't a comment marker x = # this isn't a comment; it's part of the string this is a multi-line string # __string_delimiter__ = '' # the delimiter is delimited by whitespace # now I can have comments, again, too x = ''"hello"'' # now x contains two double-quote characters I'm sure that the implementation is trivial, and it's so much easier to write strings that contain quotes (not to mention how easy it is to read those strings back later). -- Dan From mobilebackup77 at gmail.com Wed Dec 28 21:35:39 2011 From: mobilebackup77 at gmail.com (Ram) Date: Wed, 28 Dec 2011 18:35:39 -0800 (PST) Subject: logging.getLogger( __name__ ) Message-ID: <2ccfda7d-ac94-4c51-bf64-513bc69f04d6@f1g2000yqi.googlegroups.com> How does this line work? How do I get my logger to point to a file to be named as /tmp/modulename.log : I can do this using inspect, but there probably is a better way? Thanks, --Ram From wolftracks at invalid.com Wed Dec 28 22:04:13 2011 From: wolftracks at invalid.com (W. eWatson) Date: Wed, 28 Dec 2011 19:04:13 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/28/2011 12:55 PM, W. eWatson wrote: > On 12/28/2011 9:37 AM, Ian Kelly wrote: >> On Wed, Dec 28, 2011 at 9:33 AM, W. eWatson >> wrote: >>> Well, thing went slightly awry. The link gave me two choices. Download >>> msv...dll fixer, and download fixer. I took the latter. However, just >>> checking on the other one, found that I got the same exe file. I >>> installed >>> it on the XP PC, and pressed what looked like a reasonable place to >>> start. >>> It would download the dll, and register it. >>> >>> Well, it seemed more interested in the registry. It scanned four >>> areas of >>> the registry, and found 123 problems in total. To fix them would require >>> buying something. >>> >>> I noticed a large button near the top that said download dll. It >>> found one >>> the internet, and guess what? More purchase for the download. >>> >>> It seems like dll-fixer has a corner on the market. >> >> Definitely don't download that spamware fixer program -- who knows >> what that does? You want the grey "Download zip-file" link, not the >> not the flashy "Download fixer" ad links. Anyway, it sounds like >> you've found another copy of the DLL. >> >> If I were you, I would uninstall that fixer ASAP and then run an >> anti-malware and anti-virus tool or two, just in case. >> >>> Run the sfc /scannow System File Checker command to replace a missing or >>> corrupt copy of the msvcp71.dll file. If this DLL file is provided my >>> Microsoft, the System File Checker tool should restore it. >> >> I don't believe it is provided by Microsoft, but it wouldn't hurt to try. >> >>> A new dilemma. The PC XP in question with Python has the the msvcp71.dll >>> file in System32. The one I took off my other laptop has a slightly >>> newer >>> one. Feb 2003 vs Aug 2003. >> >> Weird. Try registering the existing dll, try replacing it with the >> other one (be sure to back up the original first), etc. > > I haven't installed the newer version yet, Aug 2003. I thought I'd see > what happened if I entered sfc /scannow. Whatever, happened the black > window flashed by in a split second. > > Somehow this doesn't seem helpful > . > Although, 20 lines down or so it says: > If sfc discovers that a protected file has been overwritten, it > retrieves the correct version of the file from the > %systemroot%\system32\dllcache folder, and then replaces the incorrect > file. > > I'm going to run this by a XP NG. > > That certainly didn't help. I'm going to save the msvcp71.dll from the Python laptop somewhere, then insert the one from my other XP laptop. Finally, I'll register it. regsvr32. Just to review, I presume not from cmd, but Run? From benjamin.kaplan at case.edu Wed Dec 28 23:02:21 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 28 Dec 2011 23:02:21 -0500 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On Wed, Dec 28, 2011 at 10:04 PM, W. eWatson wrote: > On 12/28/2011 12:55 PM, W. eWatson wrote: >> >> On 12/28/2011 9:37 AM, Ian Kelly wrote: >>> >>> On Wed, Dec 28, 2011 at 9:33 AM, W. eWatson >>> wrote: >>>> >>>> Well, thing went slightly awry. The link gave me two choices. Download >>>> msv...dll fixer, and download fixer. I took the latter. However, just >>>> checking on the other one, found that I got the same exe file. I >>>> installed >>>> it on the XP PC, and pressed what looked like a reasonable place to >>>> start. >>>> It would download the dll, and register it. >>>> >>>> Well, it seemed more interested in the registry. It scanned four >>>> areas of >>>> the registry, and found 123 problems in total. To fix them would require >>>> buying something. >>>> >>>> I noticed a large button near the top that said download dll. It >>>> found one >>>> the internet, and guess what? More purchase for the download. >>>> >>>> It seems like dll-fixer has a corner on the market. >>> >>> >>> Definitely don't download that spamware fixer program -- who knows >>> what that does? You want the grey "Download zip-file" link, not the >>> not the flashy "Download fixer" ad links. Anyway, it sounds like >>> you've found another copy of the DLL. >>> >>> If I were you, I would uninstall that fixer ASAP and then run an >>> anti-malware and anti-virus tool or two, just in case. >>> >>>> Run the sfc /scannow System File Checker command to replace a missing or >>>> corrupt copy of the msvcp71.dll file. If this DLL file is provided my >>>> Microsoft, the System File Checker tool should restore it. >>> >>> >>> I don't believe it is provided by Microsoft, but it wouldn't hurt to try. >>> >>>> A new dilemma. The PC XP in question with Python has the the msvcp71.dll >>>> file in System32. The one I took off my other laptop has a slightly >>>> newer >>>> one. Feb 2003 vs Aug 2003. >>> >>> >>> Weird. Try registering the existing dll, try replacing it with the >>> other one (be sure to back up the original first), etc. >> >> >> I haven't installed the newer version yet, Aug 2003. I thought I'd see >> what happened if I entered sfc /scannow. Whatever, happened the black >> window flashed by in a split second. >> >> Somehow this doesn't seem helpful >> >> . >> Although, 20 lines down or so it says: >> If sfc discovers that a protected file has been overwritten, it >> retrieves the correct version of the file from the >> %systemroot%\system32\dllcache folder, and then replaces the incorrect >> file. >> >> I'm going to run this by a XP NG. >> >> > That certainly didn't help. > > I'm going to save the msvcp71.dll from the Python laptop somewhere, then > insert the one from my other XP laptop. Finally, I'll register it. regsvr32. > Just to review, I presume not from cmd, but Run? It should work from either but I prefer to use cmd in case there's an error message. From torriem at gmail.com Thu Dec 29 00:09:59 2011 From: torriem at gmail.com (Michael Torrie) Date: Wed, 28 Dec 2011 22:09:59 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: <4EFBF627.6090909@gmail.com> On 12/28/2011 08:04 PM, W. eWatson wrote: > I'm going to save the msvcp71.dll from the Python laptop somewhere, then > insert the one from my other XP laptop. Finally, I'll register it. > regsvr32. Just to review, I presume not from cmd, but Run? Seems like the bulk of your problems are coming from not using cmd (flashing black error windows). Anything non-GUI that could return an error message should be done from cmd. The Run dialog is mainly to be used to launch a cmd window! From wolftracks at invalid.com Thu Dec 29 00:56:59 2011 From: wolftracks at invalid.com (W. eWatson) Date: Wed, 28 Dec 2011 21:56:59 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/28/2011 9:09 PM, Michael Torrie wrote: > On 12/28/2011 08:04 PM, W. eWatson wrote: >> I'm going to save the msvcp71.dll from the Python laptop somewhere, then >> insert the one from my other XP laptop. Finally, I'll register it. >> regsvr32. Just to review, I presume not from cmd, but Run? > > Seems like the bulk of your problems are coming from not using cmd > (flashing black error windows). Anything non-GUI that could return an > error message should be done from cmd. The Run dialog is mainly to be > used to launch a cmd window! > it appears more than just regsvr32 is need. Alone it fails with a msg. It needs an argument. I tried regsvr32 msvcp71.dll It didn't like that either. Got "msvcp71.dll was located but the dll server entry was not found. File could not be registered." Arguments are /u,/s/i/n. From prabhadigi2 at gmail.com Thu Dec 29 01:47:09 2011 From: prabhadigi2 at gmail.com (prabhakaran k) Date: Wed, 28 Dec 2011 22:47:09 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: <62cb12ae-98a1-41cb-9b1b-01d19ef305b6@g19g2000pri.googlegroups.com> Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From prabhadigi2 at gmail.com Thu Dec 29 01:47:14 2011 From: prabhadigi2 at gmail.com (prabhakaran k) Date: Wed, 28 Dec 2011 22:47:14 -0800 (PST) Subject: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ Message-ID: Online Data Entry Jobs Without Investment http://ponlinejobs.yolasite.com/ From rosuav at gmail.com Thu Dec 29 02:37:25 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 18:37:25 +1100 Subject: Which libraries for Python 2.5.2 In-Reply-To: <4EFBF627.6090909@gmail.com> References: <4EFBF627.6090909@gmail.com> Message-ID: On Thu, Dec 29, 2011 at 4:09 PM, Michael Torrie wrote: > The Run dialog is mainly to be > used to launch a cmd window! On any Windows computer that I use, that's strictly true. The only program I ever Start|Run is cmd. ChrisA From lars at rational-it.com Thu Dec 29 04:55:02 2011 From: lars at rational-it.com (lars van gemerden) Date: Thu, 29 Dec 2011 01:55:02 -0800 (PST) Subject: pickling instances of metaclass generated classes Message-ID: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> Hello, Can someone help me with the following: I am using metaclasses to make classes and these classes to make instances. Now I want to use multiprocessing, which needs to pickle these instances. Pickle cannot find the class definitions of the instances. I am trying to add a line to the __new__ of the metaclass to add the new class under the right name in the right module/place, so pickle can find it. Is this the right approach? Can anyone explain to me where/how to add these classes for pickle to find and maybe why? Thanks in advance, Lars From robert.kern at gmail.com Thu Dec 29 06:08:29 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 29 Dec 2011 11:08:29 +0000 Subject: pickling instances of metaclass generated classes In-Reply-To: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> Message-ID: On 12/29/11 9:55 AM, lars van gemerden wrote: > Hello, > > Can someone help me with the following: > > I am using metaclasses to make classes and these classes to make > instances. Now I want to use multiprocessing, which needs to pickle > these instances. > > Pickle cannot find the class definitions of the instances. I am trying > to add a line to the __new__ of the metaclass to add the new class > under the right name in the right module/place, so pickle can find > it. > > Is this the right approach? Can anyone explain to me where/how to add > these classes for pickle to find and maybe why? Can you post some code (preferably pared down to a minimal example that fails)? I'm not really clear on what you are doing. I would expect that a class defined by a class statement would usually work fine unless if the metaclass is doing something particularly weird to it. In any case, you can probably just explicitly register a reduction function for each type using copy_reg.pickle(): http://docs.python.org/library/copy_reg -- 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 From kenzo.zombie at gmail.com Thu Dec 29 06:48:28 2011 From: kenzo.zombie at gmail.com (Sayantan Datta) Date: Thu, 29 Dec 2011 17:18:28 +0530 Subject: Help in rotate 13 program Message-ID: please help me, this code doesn't work, the output file comes out to be empty. How do i fix it? and where is it going wrong? ==============rot13.py============== #!/usr/bin/env python import sys import string CHAR_MAP=dict(zip(string.ascii_lowercase, string.ascii_lowercase[13:26] + string.ascii_lowercase[0:13])) def rotate13_letter(letter) : """ Return the 13-char rotation of a letter """ do_upper = False if letter.isupper() : do_upper = True letter = letter.lower() if letter not in CHAR_MAP : return letter else : letter = CHAR_MAP[letter] if do_upper : letter=letter.upper() return letter if __name__ == '__main__' : for line in sys.stdin : for char in line : sys.stdout.write(rotate13_letter(char)) ===========sample.html=========== Hello, World!

Hi there, all of you earthlings

Take us to your leader.

===============TERMINAL============ this is what i run in the terminal, cat sample.html | python rot13.py rot13.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From hoogendoorn.eelco at gmail.com Thu Dec 29 06:55:14 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Thu, 29 Dec 2011 03:55:14 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> Message-ID: On Dec 28, 11:29?pm, Lie Ryan wrote: > On 12/28/2011 11:08 PM, Eelco wrote: > > > I personally feel any performance benefits are but a plus; they are > > not the motivating factor for this idea. I simply like the added > > verbosity and explicitness, thats the bottom line. > > Any performance benefits are a plus, I agree, as long as it doesn't make > my language looks like Perl. Now get off my lawn! Im no perl expert, but it says on the wikipedia page a common criticism is its overuse of otherwise meaningless special characters; and I would agree; I puked a little in my mouth looking at the code samples. I would argue that the use of single special characters to signal a relatively complex and uncommon construct is exactly what I am trying to avoid with this proposal. From robert.kern at gmail.com Thu Dec 29 07:04:21 2011 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 29 Dec 2011 12:04:21 +0000 Subject: Help in rotate 13 program In-Reply-To: References: Message-ID: On 12/29/11 11:48 AM, Sayantan Datta wrote: > cat sample.html | python rot13.py rot13.html cat sample.html | python rot13.py > rot13.html -- 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 From rosuav at gmail.com Thu Dec 29 07:04:31 2011 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 29 Dec 2011 23:04:31 +1100 Subject: Help in rotate 13 program In-Reply-To: References: Message-ID: On Thu, Dec 29, 2011 at 10:48 PM, Sayantan Datta wrote: > ? ? for line in sys.stdin : > ? ? ? for char in line : > sys.stdout.write(rotate13_letter(char)) > > cat sample.html | python rot13.py rot13.html You're reading from stdin, which is correct, but you're writing to stdout and not redirecting it. You need to put an arrow before rot13.html to indicate redirection: cat sample.html | python rot13.py >rot13.html Note though that 'cat' is superfluous here; all you need to do is redirect input: python rot13.py rot13.html Hope that helps! Chris Angelico From ben+python at benfinney.id.au Thu Dec 29 07:53:21 2011 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 29 Dec 2011 23:53:21 +1100 Subject: logging.getLogger( __name__ ) References: <2ccfda7d-ac94-4c51-bf64-513bc69f04d6@f1g2000yqi.googlegroups.com> Message-ID: <87zkeb35fi.fsf@benfinney.id.au> Ram writes: > How does this line work? What line? Please present some code in an example that we can run. Make the example as small as possible so it's clear what you are asking about. -- \ ?See, in my line of work you gotta keep repeating things over | `\ and over and over again, for the truth to sink in; to kinda | _o__) catapult the propaganda.? ?George W. Bush, 2005-05 | Ben Finney From __peter__ at web.de Thu Dec 29 08:10:16 2011 From: __peter__ at web.de (Peter Otten) Date: Thu, 29 Dec 2011 14:10:16 +0100 Subject: Help in rotate 13 program References: Message-ID: Sayantan Datta wrote: > please help me, this code doesn't work, > the output file comes out to be empty. How do i fix it? and where is it > going wrong? I don't know if that's a copy-n-paste error: the indentation of > if __name__ == '__main__' : > for line in sys.stdin : > for char in line : > sys.stdout.write(rotate13_letter(char)) should be if __name__ == '__main__': for line in sys.stdin: for char in line: sys.stdout.write(rotate13_letter(char)) From steve+comp.lang.python at pearwood.info Thu Dec 29 08:23:24 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 29 Dec 2011 13:23:24 GMT Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> Message-ID: <4efc69cc$0$29966$c3e8da3$5496439d@news.astraweb.com> On Thu, 29 Dec 2011 03:55:14 -0800, Eelco wrote: > I would argue that the use of single special characters to signal a > relatively complex and uncommon construct is exactly what I am trying to > avoid with this proposal. This would be the proposal to change the existing head, *tail = sequence to your proposed: head, tail:: = ::sequence (when happy with the default list for tail), or head, tail::tuple = ::sequence to avoid an explicit call to "tail = tuple(tail)" after the unpacking. Either way, with or without an explicit type declaration on the left hand side, you are increasing the number of punctuation characters from one to four. If your aim is to minimize the number of punctuation characters, you're doing it wrong. -- Steven From jerome at jolimont.fr Thu Dec 29 08:55:33 2011 From: jerome at jolimont.fr (=?UTF-8?B?SsOpcsO0bWU=?=) Date: Thu, 29 Dec 2011 14:55:33 +0100 Subject: Which library for audio playback ? Message-ID: <20111229145533.23a472bc@bouzin.lan> I'm writing a small application that plays sound through the speakers. The sounds are juste sine waves of arbitrary frequency I create in the code, not sample .wav files. I didn't expect the choice for an audio library to be that complicated. There are several libraries, and none of them seems to be *the* reference. Searching the web, I found these resources : http://wiki.python.org/moin/Audio http://wiki.python.org/moin/PythonInMusic * I privileged ALSA to OSS as I read elsewhere that OSS is deprecated, or on its way to be. (And JACK is not widely installed, apart from specific applications (audio work).) * Then, I picked the alsaaudio library (http://pyalsaaudio.sourceforge.net/). I don't remember exactly why. I think the project had the most recent updates. I don't think any project claims to be (let alone aims at being) complete. I'm wondering if I made a sensible choice. There are other libraries, including the following two that are platform independent : * PyAudiere (http://pyaudiere.org/), OSS , not packaged for debian * PyAudio (http://people.csail.mit.edu/hubert/pyaudio/) What solution would you recommend ? Are there other criterions I should take into account ? Thanks. -- J?r?me From alec.taylor6 at gmail.com Thu Dec 29 09:03:53 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 30 Dec 2011 01:03:53 +1100 Subject: Good notation for showing MVC interactions (i.e. Django) Message-ID: Good morning, I'm developing various websites and functionality to cater to various different use-cases up in Django. Is there a good notation for showing what behaviour is at each stage, i.e. using swimlanes? I use BPMN 2 notation for everything, but I feel I am overusing this, and that there would be a more suitable one for this. Thanks for all suggestions, Alec Taylor From hoogendoorn.eelco at gmail.com Thu Dec 29 09:20:11 2011 From: hoogendoorn.eelco at gmail.com (Eelco) Date: Thu, 29 Dec 2011 06:20:11 -0800 (PST) Subject: Pythonification of the asterisk-based collection packing/unpacking syntax References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> <4efc69cc$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <1673c1c1-b9ed-4860-9c7d-108554a03ec9@d10g2000vbk.googlegroups.com> On Dec 29, 2:23?pm, Steven D'Aprano wrote: > On Thu, 29 Dec 2011 03:55:14 -0800, Eelco wrote: > > I would argue that the use of single special characters to signal a > > relatively complex and uncommon construct is exactly what I am trying to > > avoid with this proposal. > > This would be the proposal to change the existing > > ? ? head, *tail = sequence > > to your proposed: > > ? ? head, tail:: = ::sequence > > (when happy with the default list for tail), or > > ? ? head, tail::tuple = ::sequence > > to avoid an explicit call to "tail = tuple(tail)" after the unpacking. > > Either way, with or without an explicit type declaration on the left hand > side, you are increasing the number of punctuation characters from one to > four. If your aim is to minimize the number of punctuation characters, > you're doing it wrong. > > -- > Steven The goal is not to minimize the number of (special) characters to type. To goal is to minimize the number of special characters which are hard to interpret at a glance. I would prefer : over ::, but both are a single special character construct. Adding those characters once more on the rhs is similarly, not an increase in the number of concepts employed; merely a more explicit form of the same construct. And besides, I dont much like 'head, tail:: = ::sequence'. I threw that out there to appease the terseness advocates, but to me it largely defeats the purpose, because indeed it is hardly any different from the original. I like the explicit mentioning of the collection type to be constructed; that is what really brings it more towards 'for line in file' explicit obviousness to my mind. From tinnews at isbd.co.uk Thu Dec 29 09:36:05 2011 From: tinnews at isbd.co.uk (tinnews at isbd.co.uk) Date: Thu, 29 Dec 2011 14:36:05 +0000 Subject: Where does this readOne() method come from? References: Message-ID: Chris Angelico wrote: > On Thu, Dec 29, 2011 at 6:04 AM, wrote: > > In the (rather sparse) documentation for the vobject package it has, > > in the section about parsing iCalendar objects, the following:- > > > > ? ?>>> parsedCal = vobject.readOne(icalstream) > > Presumably you have this vobject package. Assuming it's installed > correctly, all you need to do is: > > import vobject > > and then vobject.readOne should be available. > Digging harder into the epydoc documentation I finally found vobject.vobject.readOne(). However having twice been befuddled by vobject, once when using it for vCard and this time using it for vCal I've decided to move back to the python-icalendar I was using before. The only reason I tried to use vobject is that it's in the Ubuntu repositories (I doing this on an xubuntu system), but it doesn't appear to have a current maintainer and python-icalendar does so even though it's a 'manual' install I'm sticking with python-icalendar. -- Chris Green From roy at panix.com Thu Dec 29 09:44:04 2011 From: roy at panix.com (Roy Smith) Date: Thu, 29 Dec 2011 09:44:04 -0500 Subject: Good notation for showing MVC interactions (i.e. Django) References: Message-ID: In article , Alec Taylor wrote: > Is there a good notation for showing what behaviour is at each stage, > i.e. using swimlanes? > > I use BPMN 2 notation for everything, but I feel I am overusing this, > and that there would be a more suitable one for this. Swimlanes? You mean like http://tinyurl.com/caqf3h5 ? Never used them. Never used BPMN either. I've found most of these heavyweight modeling tools to be more of a hinderance than a help. People get all wrapped up in making pretty diagrams when they should be spending more time writing code. I find that when thinking something through at the whiteboard with another developer, the most common diagram I'll use is a UML sequence diagram. I drew one on the wall just yesterday. Here's one (http://www.panix.com/~roy/SongzaSessionHandoff.pdf) I did up a bit fancier a while ago while working through a gnarly design problem. As with most things in UML, I find the basic concept useful and don't sweat the details of what different shaped arrowheads mean or what flavor of rectangle I'm supposed to be drawing. From ukim86 at gmail.com Thu Dec 29 11:07:11 2011 From: ukim86 at gmail.com (Prim) Date: Thu, 29 Dec 2011 08:07:11 -0800 (PST) Subject: About instance.name look up order Message-ID: First, sorry about my poor english. Put these in IPython under ubuntu. ------------------------------------------------------------------------------------- class C: def __init__(self): self.x = 1 def print(self): print self.x c = C() c.x --> 1, so c.x mean a attr of c named 'x' c.print() --> pirnt 1, so c.print mean a method of c named 'print' ------------------------------------------------------------------------------------- class C: def __init__(self): self.x = 1 def x(self): print 'x method' def y(self): print 'y method' c = C() c.x --> 1 c.x() --> TypeError: 'int' object is not callable c.y --> bound method C.y #Q1: instance.name will get the attr first, than method? ------------------------------------------------------------------------------------- class C: def x(self): print 'x method' def __getattr__(self, attr): print 'in __getattr__ method' return attr c = C() c.x --> print in __getattr__ method, then throw TypeError: 'str' object is not callable c.x() --> print in __getattr__ method, x method 2 lines #Q2: why c.x would get a exception? t = c.x t --> print in __getattr__ method, then throw TypeError: 'str' object is not callable t() --> print x method t = c.x() --> print x method, t == None #Q3 why t=c.x() and c.x() output different? #Q4, if when I define the class use property too, then instance.name look up order would be? Thanks for your reply. From alec.taylor6 at gmail.com Thu Dec 29 11:07:17 2011 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 30 Dec 2011 03:07:17 +1100 Subject: Good notation for showing MVC interactions (i.e. Django) In-Reply-To: References: Message-ID: An example of a BPMN2 diagram with swimlanes, which I created for a project: http://i40.tinypic.com/262r6nr.jpg What I am looking for is something suited towards showing architecture bounds and the interactions between each section of MVC. > I find that when thinking something through at the whiteboard with > another developer, the most common diagram I'll use is a UML sequence > diagram. I drew one on the wall just yesterday. Here's one > (http://www.panix.com/~roy/SongzaSessionHandoff.pdf) I did up a bit > fancier a while ago while working through a gnarly design problem. > That's a good notation, and one which I will use for RESTful API modelling. I sometimes use use-case diagrams and quite rarely also use class diagrams and ER Diagrams. I use BPMN2 to show logic to both technical and non-technical team-members. From emekamicro at gmail.com Thu Dec 29 11:09:45 2011 From: emekamicro at gmail.com (Emeka) Date: Thu, 29 Dec 2011 17:09:45 +0100 Subject: Get Class properties In-Reply-To: References: Message-ID: Chris, Thanks a million! Regards, Emeka On Thu, Dec 29, 2011 at 1:27 AM, Chris Angelico wrote: > On Wed, Dec 28, 2011 at 11:13 PM, Emeka wrote: > > Hello All, > > > > I have seen what I am looking for.. __dict__. > > > > Yep! You may also want to look at the dir() function. > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list > -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From kenzo.zombie at gmail.com Thu Dec 29 11:25:50 2011 From: kenzo.zombie at gmail.com (Sayantan Datta) Date: Thu, 29 Dec 2011 21:55:50 +0530 Subject: Help in rotate 13 program In-Reply-To: References: Message-ID: On Thu, Dec 29, 2011 at 5:34 PM, Chris Angelico wrote: > On Thu, Dec 29, 2011 at 10:48 PM, Sayantan Datta > wrote: > > for line in sys.stdin : > > for char in line : > > sys.stdout.write(rotate13_letter(char)) > > > > cat sample.html | python rot13.py rot13.html > > You're reading from stdin, which is correct, but you're writing to > stdout and not redirecting it. You need to put an arrow before > rot13.html to indicate redirection: > > cat sample.html | python rot13.py >rot13.html > > Note though that 'cat' is superfluous here; all you need to do is > redirect input: > python rot13.py rot13.html > > Hope that helps! > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list > did that, but the output file is still empty? Does the fault lie somewhere else? -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu Dec 29 11:32:55 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Dec 2011 03:32:55 +1100 Subject: Help in rotate 13 program In-Reply-To: References: Message-ID: On Fri, Dec 30, 2011 at 3:25 AM, Sayantan Datta wrote: > did that, but the output file is still empty? Does the fault lie somewhere > else? I'm not sure if it's a copy-paste problem, but check your indentation: your "if __name__" line seems to be indented, it should be flush left. Indentation makes it part of the function, so your script will define a function and never call it. ChrisA From d at davea.name Thu Dec 29 11:39:29 2011 From: d at davea.name (Dave Angel) Date: Thu, 29 Dec 2011 11:39:29 -0500 Subject: Help in rotate 13 program In-Reply-To: References: Message-ID: <4EFC97C1.2010908@davea.name> On 12/29/2011 11:25 AM, Sayantan Datta wrote: > On Thu, Dec 29, 2011 at 5:34 PM, Chris Angelico wrote: > >> On Thu, Dec 29, 2011 at 10:48 PM, Sayantan Datta >> wrote: >>> for line in sys.stdin : >>> for char in line : >>> sys.stdout.write(rotate13_letter(char)) >>> >>> cat sample.html | python rot13.py rot13.html >> You're reading from stdin, which is correct, but you're writing to >> stdout and not redirecting it. You need to put an arrow before >> rot13.html to indicate redirection: >> >> cat sample.html | python rot13.py>rot13.html >> >> Note though that 'cat' is superfluous here; all you need to do is >> redirect input: >> python rot13.pyrot13.html >> >> Hope that helps! >> >> Chris Angelico >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > did that, but the output file is still empty? Does the fault lie somewhere > else? > Both Peter and Chris pointed out that you have the if __name__ == "__main__" line indented. If that's true in your actual file, then the program does nothing useful. Why not run it without output redirection, and see what it displays? And stick an unindented print line in there, just to see it do something. -- DaveA From wolftracks at invalid.com Thu Dec 29 12:23:49 2011 From: wolftracks at invalid.com (W. eWatson) Date: Thu, 29 Dec 2011 09:23:49 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/29/2011 6:54 AM, Dennis Lee Bieber wrote: > On Wed, 28 Dec 2011 21:56:59 -0800, "W. eWatson" > wrote: > > >> It didn't like that either. Got "msvcp71.dll was located but the dll >> server entry was not found. File could not be registered." >> >> Arguments are /u,/s/i/n. > > According to the pop-up I get (Win7) when supplying an invalid > argument, > > /n - do not call DllRegisterServer; this option must be used with /i > and that reads > /i - Call DllInstall passing it an optional [cmdline]; when used > with /u calls dll uninstall > > So.. If the register server entry point was not found, maybe you > should try with /i/n to make it call the /other/ registration entry > point. Well, it's worth a try. However, what "other" entry point. > > > > {And, in line with the other commentators; I don't even use start/run > for the command shell -- I use the shell often enough to have put a > dedicated shortcut on the start menu itself (and have since added > PowerShell shortcuts). Only thing I use start/run with is the registry > editor (regedt32 or regedit) and to start the despised Internet Explorer > (iexplore, which has otherwise been removed from direct mouse access) -- > in short; only programs with GUI interfaces get started from start/run} To keep it simple, I now just enter cmd, and work there. I'm not usually working with these low level operations. Occasionally, I use some of the network commands. From wolftracks at invalid.com Thu Dec 29 12:26:59 2011 From: wolftracks at invalid.com (W. eWatson) Date: Thu, 29 Dec 2011 09:26:59 -0800 Subject: Which libraries for Python 2.5.2 In-Reply-To: References: Message-ID: On 12/29/2011 9:04 AM, Dennis Lee Bieber wrote: > On Thu, 29 Dec 2011 09:54:48 -0500, Dennis Lee Bieber > wrote: > > Talking to myself.. It's the best way to get people to listen to you. :-) > >> >> According to the pop-up I get (Win7) when supplying an invalid >> argument, >> > Maybe regsrv32 was rewritten for Win7 64bit -- and assumed folks > would run it from the start dialog (or file search in Win7) since the > error messages do pop-up in windows, even when running in a command > shell. I'm installing Python on an XP PC that's 5 years old. This PC I'm writing from is Win7. ... From ian.g.kelly at gmail.com Thu Dec 29 12:44:54 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 29 Dec 2011 10:44:54 -0700 Subject: Which libraries for Python 2.5.2 In-Reply-To: <8u6pf7hj0g0vt6k8n0clj3h59e42qjhbqq@4ax.com> References: <8u6pf7hj0g0vt6k8n0clj3h59e42qjhbqq@4ax.com> Message-ID: On Thu, Dec 29, 2011 at 10:04 AM, Dennis Lee Bieber wrote: > On Thu, 29 Dec 2011 09:54:48 -0500, Dennis Lee Bieber > wrote: > > ? ? ? ?Talking to myself.. > >> >> ? ? ? According to the pop-up I get (Win7) when supplying an invalid >>argument, >> > ? ? ? ?Maybe regsrv32 was rewritten for Win7 64bit -- and assumed folks > would run it from the start dialog (or file search in Win7) since the > error messages do pop-up in windows, even when running in a command > shell. As far as I know, it's always been like that. From kevin.p.dwyer at gmail.com Thu Dec 29 13:03:13 2011 From: kevin.p.dwyer at gmail.com (Kev Dwyer) Date: Thu, 29 Dec 2011 18:03:13 +0000 Subject: About instance.name look up order References: Message-ID: Prim wrote: > First, sorry about my poor english. > Put these in IPython under ubuntu. > ------------------------------------------------------------------------------------- > class C: > def __init__(self): > self.x = 1 > def print(self): > print self.x > c = C() > c.x --> 1, so c.x mean a attr of c named 'x' > c.print() --> pirnt 1, so c.print mean a method of c named 'print' > ------------------------------------------------------------------------------------- > class C: > def __init__(self): > self.x = 1 > def x(self): > print 'x method' > def y(self): > print 'y method' > c = C() > c.x --> 1 > c.x() --> TypeError: 'int' object is not callable > c.y --> bound method C.y > #Q1: instance.name will get the attr first, than method? > ------------------------------------------------------------------------------------- > class C: > def x(self): > print 'x method' > def __getattr__(self, attr): > print 'in __getattr__ method' > return attr > c = C() > c.x --> print in __getattr__ method, then throw TypeError: 'str' > object is not callable > c.x() --> print in __getattr__ method, x method 2 lines > #Q2: why c.x would get a exception? > > t = c.x > t --> print in __getattr__ method, then throw TypeError: 'str' object > is not callable > t() --> print x method > t = c.x() --> print x method, t == None > #Q3 why t=c.x() and c.x() output different? > > #Q4, if when I define the class use property too, then instance.name > look up order would be? > > Thanks for your reply. Hello, Python always looks for attributes in the instance first, then in the class, and then in the class's superclasses. In your first example, by defining "x" in C.__init__ you are creating an instance attribute named "x". When the attribute c.x is requested, Python finds an attribute "x" in the instance and returns it; the method "x" is found in the class, but the attribute lookup does not proceed this far. Try looking at C.__dict__ and c.__dict__ in the interpreter to see how the attributes are stored. See also http://docs.python.org/reference/datamodel.html#customizing-attribute-access Cheers Kev From ian.g.kelly at gmail.com Thu Dec 29 13:35:49 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 29 Dec 2011 11:35:49 -0700 Subject: About instance.name look up order In-Reply-To: References: Message-ID: On Thu, Dec 29, 2011 at 9:07 AM, Prim wrote: > class C: > ? ?def x(self): > ? ? ? ?print 'x method' > ? ?def __getattr__(self, attr): > ? ? ? ?print 'in __getattr__ method' > ? ? ? ?return attr > c = C() > c.x --> print in __getattr__ method, then throw TypeError: 'str' > object is not callable > c.x() --> print in __getattr__ method, x method 2 lines > #Q2: why c.x would get a exception? I'm not sure exactly what's going on there, but as a general rule you should use new-style classes, not classic classes, which are deprecated and have been removed in Python 3. In Python 2 if you define a class without any base classes, you get a classic class. To define C as a new-style class in Python 2, derive it from object (or another new-style class): >>> class C(object): ... def x(self): ... print 'x method' ... def __getattr__(self, attr): ... print 'in __getattr__ method' ... return attr ... >>> c = C() >>> c.x > >>> c.x() x method Note that in this case __getattr__ never gets called because the name was found using the normal lookup mechanism. You would need to use __getattribute__ if you want it to be called unconditionally. > #Q4, if when I define the class use property too, then instance.name > look up order would be? You can't use properties in classic classes. In a new-style class, if you access a property, then any instance attribute with the same name is ignored and can't be set in the first place. From wolftracks at invalid.com Thu Dec 29 14:31:17 2011 From: wolftracks at invalid.com (W. eWatson) Date: Thu, 29 Dec 2011 11:31:17 -0800 Subject: Which libraries for Python 2.5.2 [SOLVED] In-Reply-To: References: <8u6pf7hj0g0vt6k8n0clj3h59e42qjhbqq@4ax.com> Message-ID: On 12/29/2011 9:44 AM, Ian Kelly wrote: > On Thu, Dec 29, 2011 at 10:04 AM, Dennis Lee Bieber > wrote: >> On Thu, 29 Dec 2011 09:54:48 -0500, Dennis Lee Bieber >> wrote: >> >> Talking to myself.. >> >>> >>> According to the pop-up I get (Win7) when supplying an invalid >>> argument, >>> >> Maybe regsrv32 was rewritten for Win7 64bit -- and assumed folks >> would run it from the start dialog (or file search in Win7) since the >> error messages do pop-up in windows, even when running in a command >> shell. > > As far as I know, it's always been like that. Well, I thought I'd take the PC to a computer shop to see why when I move windows they shake a bit. The technician took about 2 minutes to show me uninstalled devices under System Devices. He thought they should be fixed. The system was showing lots of device errors. They wanted $75 to repair it, but I told them a friend who will use the PC will have to decide that. This PC belonged to my wife at one time for 5 years. She had put Linux on it, but she no longer needed it. I restored it to XP Home. When I told her where the problems were she found a driver DVD, so I'm using it now to fill in the gaps. Maybe it'll get by the dll problem. It certainly is adding a VGA driver, so I think the window wobbles may get fixed. Well, it now works w/o any difficulty. It was the incomplete install that did it. It now has acrobat, wallpaper, etc, like most XP systems. No more wobbles! Yea! Thanks to all that helped. From ian.g.kelly at gmail.com Thu Dec 29 14:55:04 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 29 Dec 2011 12:55:04 -0700 Subject: pickling instances of metaclass generated classes In-Reply-To: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> Message-ID: On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden wrote: > Hello, > > Can someone help me with the following: > > I am using metaclasses to make classes and these classes to make > instances. Now I want to use multiprocessing, which needs to pickle > these instances. > > Pickle cannot find the class definitions of the instances. I am trying > to add a line to the __new__ of the metaclass to add the new class > under the right name in the right module/place, so pickle can find > it. > > Is this the right approach? Can anyone explain to me where/how to add > these classes for pickle to find and maybe why? It sounds like you're trying to do something like this? >>> class MetaClass(type): ... pass ... >>> instance = MetaClass('', (object,), {})() >>> instance <__main__. object at 0x00CC00F0> >>> import pickle >>> pickle.dumps(instance) Traceback (most recent call last): File "", line 1, in File "c:\python27\lib\pickle.py", line 1374, in dumps Pickler(file, protocol).dump(obj) File "c:\python27\lib\pickle.py", line 224, in dump self.save(obj) File "c:\python27\lib\pickle.py", line 331, in save self.save_reduce(obj=obj, *rv) File "c:\python27\lib\pickle.py", line 401, in save_reduce save(args) File "c:\python27\lib\pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "c:\python27\lib\pickle.py", line 562, in save_tuple save(element) File "c:\python27\lib\pickle.py", line 295, in save self.save_global(obj) File "c:\python27\lib\pickle.py", line 748, in save_global (obj, module, name)) pickle.PicklingError: Can't pickle '>: it's not found as __main__. Yeah, pickle's not going to work with anonymous classes. As you suggest, you could dynamically add the classes to the module namespace so that pickle.dumps will find them, but bear in mind that they will also have to exist when calling pickle.loads, so you will need to be able to reconstruct the same anonymous classes before unpickling later on. Cheers, Ian From merwin.irc at gmail.com Thu Dec 29 15:22:55 2011 From: merwin.irc at gmail.com (Merwin) Date: Thu, 29 Dec 2011 21:22:55 +0100 Subject: PySide / PyQt autocompletion in IDEs Message-ID: <4EFCCC1F.7000605@gmail.com> Hi, I would like to work with PyQt / PySide, but there is a small problem : methods arguments are not completed by IDE's autocompletion. When, typing "PySide.", I correctly get the module's attributes, but when I want to see what arguments are expected to a constructor / method, all I get is *args, **kwargs. I perfectly understand why this happen, but do you know a way to get around this ? I spend more time looking for arguments in Qt's documentation than effectively programming, which is a problem :-/ Thanks for reading, From vim at aryehleib.com Thu Dec 29 15:40:37 2011 From: vim at aryehleib.com (Aryeh Leib Taurog) Date: Thu, 29 Dec 2011 12:40:37 -0800 (PST) Subject: Augmented online documentation Message-ID: <82741962-c597-42fb-9300-e37103bcf63a@d8g2000yqk.googlegroups.com> I have created for myself a small google chrome extension which generates a treeview toc pane of the online python docs as well as the matplotlib docs. This is intended to facilitate doc navigation, much the same way the navigation pane in a chm viewer does. Read more and install it at https://bitbucket.org/altaurog/sphdoctoc/overview enjoy, Aryeh Leib Taurog From lie.1296 at gmail.com Thu Dec 29 18:19:10 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 30 Dec 2011 10:19:10 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/29/2011 12:44 PM, Dan Sommers wrote: > On Wed, 28 Dec 2011 22:54:16 +0000, Steven D'Aprano wrote: > >> On Wed, 28 Dec 2011 11:36:17 -0800, Rick Johnson wrote: >> >>> The point is people, we should be using string delimiters that are >>> ANYTHING besides " and '. Stop being a sheep and use your brain! >> >> "ANYTHING", hey? >> >> I propose we use ? and ? as the opening and closing string delimiters. >> Problem solved! > > Why stop at pre-determined, literal delimiters? That's way too easy on > the parser, whether that parser is a computer or a person. > > __string_delimiter__ = ? # magic syntax; no quotes needed > > x = ?hello? > > __string_delimiter__ = # # that first # isn't a comment marker > > x = # this isn't a comment; it's part of the string > this is a multi-line string > # > > __string_delimiter__ = '' # the delimiter is delimited by whitespace > # now I can have comments, again, too > > x = ''"hello"'' # now x contains two double-quote characters > > I'm sure that the implementation is trivial, and it's so much easier to > write strings that contain quotes (not to mention how easy it is to read > those strings back later). right and you can generalize this idea even further __comment_marker__ = $ __string_delimiter__ = [#, %] $ also, even further __comment_marker_keyword = #__my_own_comment_marker__% __my_own_comment_marker__ = [&, *] __assignment_symbol__ = <- __attribute_delimiter__ <- -> & for attributes * __call_delimiter__ = [!,@] l = [] l->append!5@ you're a genius!! From rosuav at gmail.com Thu Dec 29 18:24:14 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Dec 2011 10:24:14 +1100 Subject: Py-dea: Streamline string literals now! In-Reply-To: References: <280841b8-8674-4c1d-9a82-28982340a4b8@v24g2000yqk.googlegroups.com> <4efabe1d$0$29966$c3e8da3$5496439d@news.astraweb.com> <4efb9e18$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 30, 2011 at 10:19 AM, Lie Ryan wrote: > right and you can generalize this idea even further Don't stop generalizing. Everything should be custom. http://rosuav.com/1/?id=683 ChrisA From lie.1296 at gmail.com Thu Dec 29 18:24:57 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 30 Dec 2011 10:24:57 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: <4efc69cc$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> <4efc69cc$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/30/2011 12:23 AM, Steven D'Aprano wrote: > On Thu, 29 Dec 2011 03:55:14 -0800, Eelco wrote: > >> I would argue that the use of single special characters to signal a >> relatively complex and uncommon construct is exactly what I am trying to >> avoid with this proposal. > > This would be the proposal to change the existing > > head, *tail = sequence > > to your proposed: > > head, tail:: = ::sequence > > (when happy with the default list for tail), or > > head, tail::tuple = ::sequence > > to avoid an explicit call to "tail = tuple(tail)" after the unpacking. > > Either way, with or without an explicit type declaration on the left hand > side, you are increasing the number of punctuation characters from one to > four. If your aim is to minimize the number of punctuation characters, > you're doing it wrong. Another drawback of it is that it looks misleadingly similar to C++ namespace notation. From rosuav at gmail.com Thu Dec 29 18:30:54 2011 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 30 Dec 2011 10:30:54 +1100 Subject: Pythonification of the asterisk-based collection packing/unpacking syntax In-Reply-To: References: <841f4d29-f50b-4b0b-912b-b497fb6e60ec@t16g2000vba.googlegroups.com> <55f2aab5-be87-460e-8576-645ac225c63d@l19g2000yqc.googlegroups.com> <272d2d13-3168-4991-84ed-57a255a98c10@p9g2000vbb.googlegroups.com> <99ae9ba0-4488-4b77-8e56-cf5af0d594af@cs7g2000vbb.googlegroups.com> <4ef72180$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efa5072$0$29973$c3e8da3$5496439d@news.astraweb.com> <4efab65f$0$29973$c3e8da3$5496439d@news.astraweb.com> <954ec0e8-327b-45c4-b49e-9c69c2e463c7@j10g2000vbe.googlegroups.com> <4efc69cc$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Fri, Dec 30, 2011 at 10:24 AM, Lie Ryan wrote: > Another drawback of it is that it looks misleadingly similar to C++ > namespace notation. Granted, but I don't see that as a drawback. The current notation is just as similar to C's pointer-dereference notation, but that hasn't led people to think that tail holds a pointer to the location where something should be stored. This would be a serious concern with notations that are common across many languages (eg "x*y" to mean multiplication, which isn't strictly what mathematics uses), but with language-specific notations, especially such brief ones, it's understood that there'll be duplication. ChrisA From umchaselee at gmail.com Thu Dec 29 20:04:12 2011 From: umchaselee at gmail.com (umchaselee at gmail.com) Date: Thu, 29 Dec 2011 17:04:12 -0800 (PST) Subject: db agnostic admin for Flask? In-Reply-To: <21fa456b-67ef-4c00-af59-e3d238302b2e@t36g2000prj.googlegroups.com> References: <21fa456b-67ef-4c00-af59-e3d238302b2e@t36g2000prj.googlegroups.com> Message-ID: <11135484.2432.1325207052620.JavaMail.geo-discussion-forums@yqmw40> I was considering building an admin that would consist of two components, a totally separate HTML/CSS/JS frontend that presumed API endpoints, and a backend written in Tornado (similar to Flask in a lot of ways) that caught all of the presumed endpoints and performed the db operations. The idea of separating them is to make the frontend portable across backends so that a backend could be written in Tornado/Flask/Django/RoR(*shudder*)/whatever you want as long as it followed the conventions leading to the API endpoints. I've seen a couple backends out there but haven't been too happy with any of them, and I haven't found any doing something like this. From kenzo.zombie at gmail.com Thu Dec 29 23:31:05 2011 From: kenzo.zombie at gmail.com (Sayantan Datta) Date: Fri, 30 Dec 2011 10:01:05 +0530 Subject: Help in rotate 13 program In-Reply-To: <4EFC97C1.2010908@davea.name> References: <4EFC97C1.2010908@davea.name> Message-ID: On Thu, Dec 29, 2011 at 10:09 PM, Dave Angel wrote: > On 12/29/2011 11:25 AM, Sayantan Datta wrote: > >> On Thu, Dec 29, 2011 at 5:34 PM, Chris Angelico wrote: >> >> On Thu, Dec 29, 2011 at 10:48 PM, Sayantan Datta >>> wrote: >>> >>>> for line in sys.stdin : >>>> for char in line : >>>> sys.stdout.write(rotate13_**letter(char)) >>>> >>>> cat sample.html | python rot13.py rot13.html >>>> >>> You're reading from stdin, which is correct, but you're writing to >>> stdout and not redirecting it. You need to put an arrow before >>> rot13.html to indicate redirection: >>> >>> cat sample.html | python rot13.py>rot13.html >>> >>> Note though that 'cat' is superfluous here; all you need to do is >>> redirect input: >>> python rot13.pyrot13.**html >>> >>> Hope that helps! >>> >>> Chris Angelico >>> -- >>> http://mail.python.org/**mailman/listinfo/python-list >>> >>> did that, but the output file is still empty? Does the fault lie >> somewhere >> else? >> >> Both Peter and Chris pointed out that you have the if __name__ == > "__main__" line indented. If that's true in your actual file, then the > program does nothing useful. > > Why not run it without output redirection, and see what it displays? And > stick an unindented print line in there, just to see it do something. > > > > -- > > DaveA > > hmm, yes, it was, noticed it right now. Huh, i made such a silly mistake.. Anyway, thanks a lot. :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From p_s_d_a_s_i_l_v_a at netcabo.pt Fri Dec 30 02:17:13 2011 From: p_s_d_a_s_i_l_v_a at netcabo.pt (Paulo da Silva) Date: Fri, 30 Dec 2011 07:17:13 +0000 Subject: Generating sin/square waves sound Message-ID: Hi, Sorry if this is a FAQ, but I have googled and didn't find any satisfatory answer. Is there a simple way, preferably multiplataform (or linux), of generating sinusoidal/square waves sound in python? Thanks for any answers/suggestions. From lars at rational-it.com Fri Dec 30 04:50:26 2011 From: lars at rational-it.com (lars van gemerden) Date: Fri, 30 Dec 2011 01:50:26 -0800 (PST) Subject: pickling instances of metaclass generated classes References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> Message-ID: On Dec 29, 8:55?pm, Ian Kelly wrote: > On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden wrote: > > > Hello, > > > Can someone help me with the following: > > > I am using metaclasses to make classes and these classes to make > > instances. Now I want to use multiprocessing, which needs to pickle > > these instances. > > > Pickle cannot find the class definitions of the instances. I am trying > > to add a line to the __new__ of the metaclass to add the new class > > under the right name in the right module/place, so pickle can find > > it. > > > Is this the right approach? Can anyone explain to me where/how to add > > these classes for pickle to find and maybe why? > > It sounds like you're trying to do something like this? > > >>> class MetaClass(type): > > ... ? ? pass > ...>>> instance = MetaClass('', (object,), {})() > >>> instance > > <__main__. object at 0x00CC00F0>>>> import pickle > >>> pickle.dumps(instance) > > Traceback (most recent call last): > ? File "", line 1, in > ? File "c:\python27\lib\pickle.py", line 1374, in dumps > ? ? Pickler(file, protocol).dump(obj) > ? File "c:\python27\lib\pickle.py", line 224, in dump > ? ? self.save(obj) > ? File "c:\python27\lib\pickle.py", line 331, in save > ? ? self.save_reduce(obj=obj, *rv) > ? File "c:\python27\lib\pickle.py", line 401, in save_reduce > ? ? save(args) > ? File "c:\python27\lib\pickle.py", line 286, in save > ? ? f(self, obj) # Call unbound method with explicit self > ? File "c:\python27\lib\pickle.py", line 562, in save_tuple > ? ? save(element) > ? File "c:\python27\lib\pickle.py", line 295, in save > ? ? self.save_global(obj) > ? File "c:\python27\lib\pickle.py", line 748, in save_global > ? ? (obj, module, name)) > pickle.PicklingError: Can't pickle '>: > it's not found as __main__. > > Yeah, pickle's not going to work with anonymous classes. ?As you > suggest, you could dynamically add the classes to the module namespace > so that pickle.dumps will find them, but bear in mind that they will > also have to exist when calling pickle.loads, so you will need to be > able to reconstruct the same anonymous classes before unpickling later > on. > > Cheers, > Ian Thank you Ian for the minimal example. This is almost the case i was trying to discribe, however the classes will be named at runtime and the class definitions will be persisted in a database. Can you help me with how to add the classes to the correct namespace? The application spans multiple modules (or compared to the example, the metaclass definition will be in another module then one where the class and instance will be generated). Cheers, Lars From dihedral88888 at googlemail.com Fri Dec 30 04:51:06 2011 From: dihedral88888 at googlemail.com (88888 Dihedral) Date: Fri, 30 Dec 2011 01:51:06 -0800 (PST) Subject: Generating sin/square waves sound In-Reply-To: References: Message-ID: <15235180.160.1325238666252.JavaMail.geo-discussion-forums@prlk31> Please check PYGAME and Simple Directmedia library. Python is used as the director like role and functions in SDL do most of the jobs in Pygame. From d at davea.name Fri Dec 30 05:05:01 2011 From: d at davea.name (Dave Angel) Date: Fri, 30 Dec 2011 05:05:01 -0500 Subject: Generating sin/square waves sound In-Reply-To: References: Message-ID: <4EFD8CCD.9080908@davea.name> On 12/30/2011 02:17 AM, Paulo da Silva wrote: > Hi, > Sorry if this is a FAQ, but I have googled and didn't find any > satisfatory answer. > > Is there a simple way, preferably multiplataform (or linux), of > generating sinusoidal/square waves sound in python? > > Thanks for any answers/suggestions. If you're willing to be Linux-only, then I believe you can do it without any extra libraries. You build up a string (8 bit char, on Python 2.x) of samples, and write it to "/dev/audio". When i experimented, I was only interested in a few seconds, so a single write was all I needed. Note that the samples are 8 bits, and they are offset by 128. So a zero signal would be a string of 128 values. A very quiet square wave might be a bunch of 126, followed by a bunch of 130. and so on. And the loudest might be a bunch of 2's followed by a bunch of 253's. You'll have to experiment with data rate; The data is sent out at a constant rate from your string, but I don't know what that rate is. -- DaveA From nad at acm.org Fri Dec 30 05:45:02 2011 From: nad at acm.org (Ned Deily) Date: Fri, 30 Dec 2011 02:45:02 -0800 Subject: importing MySQLdb References: <4EFB9D6D.3080506@gmail.com> Message-ID: In article <4EFB9D6D.3080506 at gmail.com>, Dilara Ally wrote: > I'm trying to import the MySQLdb for python. I downloaded the proper > setuptools egg (ver2.7) for a Mac with OSX10.6 > > I then downloaded the MySQL-python-1.2.3 and ran the following commands > > python setup.py build > sudo python setup.py install > > Then to test that the module was properly loaded I used the interactive > python prompt ad ran the following command: > > import MySQLdb > > The error message read: > > Traceback (most recent call last): > File "", line 1, in > File "MySQLdb/__init__.py", line 19, in > import _mysql > File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 7, in > File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 6, in > __bootstrap__ > ImportError: > dlopen(/Users/dally/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.eg > g-tmp/_mysql.so, > 2): Library not loaded: libmysqlclient.18.dylib > Referenced from: > /Users/dally/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_ > mysql.so > Reason: image not found > > Does anyone have any ideas on how to fix this problem? Any help will be > greatly appreciated! If you installed the MySQL client libraries from a MySQL binary installer, chances are that the library name in the client library does not contain the full path. The MySQL project has a pretty dismal record for releasing faulty OS X binaries. There are a few workarounds, the best being either to use install_name_tool to fix the path or to set the DYLD_LIBRARY_PATH environment variable. See, for example, this blog post: http://www.blog.bridgeutopiaweb.com/post/how-to-fix-mysql-load-issues-on- mac-os-x/ -- Ned Deily, nad at acm.org From lars at rational-it.com Fri Dec 30 06:16:10 2011 From: lars at rational-it.com (lars van gemerden) Date: Fri, 30 Dec 2011 03:16:10 -0800 (PST) Subject: pickling instances of metaclass generated classes References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> Message-ID: <208b4f8b-c347-44c6-ac9d-73662cc36dbd@o9g2000yqa.googlegroups.com> On Dec 29, 8:55?pm, Ian Kelly wrote: > On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden wrote: > > > Hello, > > > Can someone help me with the following: > > > I am using metaclasses to make classes and these classes to make > > instances. Now I want to use multiprocessing, which needs to pickle > > these instances. > > > Pickle cannot find the class definitions of the instances. I am trying > > to add a line to the __new__ of the metaclass to add the new class > > under the right name in the right module/place, so pickle can find > > it. > > > Is this the right approach? Can anyone explain to me where/how to add > > these classes for pickle to find and maybe why? > > It sounds like you're trying to do something like this? > > >>> class MetaClass(type): > > ... ? ? pass > ...>>> instance = MetaClass('', (object,), {})() > >>> instance > > <__main__. object at 0x00CC00F0>>>> import pickle > >>> pickle.dumps(instance) > > Traceback (most recent call last): > ? File "", line 1, in > ? File "c:\python27\lib\pickle.py", line 1374, in dumps > ? ? Pickler(file, protocol).dump(obj) > ? File "c:\python27\lib\pickle.py", line 224, in dump > ? ? self.save(obj) > ? File "c:\python27\lib\pickle.py", line 331, in save > ? ? self.save_reduce(obj=obj, *rv) > ? File "c:\python27\lib\pickle.py", line 401, in save_reduce > ? ? save(args) > ? File "c:\python27\lib\pickle.py", line 286, in save > ? ? f(self, obj) # Call unbound method with explicit self > ? File "c:\python27\lib\pickle.py", line 562, in save_tuple > ? ? save(element) > ? File "c:\python27\lib\pickle.py", line 295, in save > ? ? self.save_global(obj) > ? File "c:\python27\lib\pickle.py", line 748, in save_global > ? ? (obj, module, name)) > pickle.PicklingError: Can't pickle '>: > it's not found as __main__. > > Yeah, pickle's not going to work with anonymous classes. ?As you > suggest, you could dynamically add the classes to the module namespace > so that pickle.dumps will find them, but bear in mind that they will > also have to exist when calling pickle.loads, so you will need to be > able to reconstruct the same anonymous classes before unpickling later > on. > > Cheers, > Ian Ian also wrote: ''' Actually, I was wrong, you probably don't need to do that. I suggest going with Robert Kern's suggestion to either register the class with the copy_reg module, or (perhaps better since it won't leak registrations) implement a __reduce__ method on the class. For example, this seems to work: >>> def reconstructor(*metaclass_args): ... cls = MetaClass.build_class(*metaclass_args) ... self = cls.__new__(cls) ... return self ... >>> class MetaClass(type): ... @classmethod ... def build_class(mcs, arg1, arg2, arg3): ... # Do something useful with the args... ... class _AnonymousClass(object): ... __metaclass__ = mcs ... def __reduce__(self): ... return (reconstructor, ('foo', 'bar', 'baz'), self.__dict__) ... return _AnonymousClass ... >>> instance = MetaClass.build_class('foo', 'bar', 'baz')() >>> instance <__main__._AnonymousClass object at 0x011DB410> >>> instance.banana = 42 >>> import pickle >>> s = pickle.dumps(instance) >>> s "c__main__\nreconstructor \np0\n(S'foo'\np1\nS'bar'\np2\nS'baz'\np3\ntp4\nRp5\n(dp6\nS'banana'\np7\nI42\nsb." >>> inst2 = pickle.loads(s) >>> inst2 <__main__._AnonymousClass object at 0x011DBE90> >>> inst2.banana 42 >>> inst2.__class__ is instance.__class__ False Cheers, Ian ''' From foobar at invalid.invalid Fri Dec 30 06:23:50 2011 From: foobar at invalid.invalid (mblume) Date: Fri, 30 Dec 2011 11:23:50 +0000 (UTC) Subject: Generating sin/square waves sound References: Message-ID: Am Fri, 30 Dec 2011 07:17:13 +0000 schrieb Paulo da Silva: > Hi, > Sorry if this is a FAQ, but I have googled and didn't find any > satisfatory answer. > > Is there a simple way, preferably multiplataform (or linux), of > generating sinusoidal/square waves sound in python? > > Thanks for any answers/suggestions. Have a look at the wave module, available under Windows and Linux, which operates on .WAV files. The following snippet might get you going: #!/usr/bin/python import math, wave, struct def signal(t, freq): return math.sin(2.0*math.pi*freq*t) wout = wave.open("sample.wav", "wb") nchan = 1 sampwidth = 2 framerate = 8000 nframes = 7 * framerate comptype = "NONE" compname = "no compression" wout.setparams((nchan, sampwidth, framerate, nframes, comptype, compname)) ts = 1.0 / framerate t = 0.0 n = 0 data = [] vals = [] while n < nframes: vals.append(signal(t, 517.0)) n = n + 1 t = t + ts mx = max((abs(x) for x in vals)) vals = [ x/mx for x in vals ] data = "" for v in vals: data = data + struct.pack(" Message-ID: lars van gemerden wrote: > On Dec 29, 8:55 pm, Ian Kelly wrote: >> On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden >> wrote: >> >> > Hello, >> >> > Can someone help me with the following: >> >> > I am using metaclasses to make classes and these classes to make >> > instances. Now I want to use multiprocessing, which needs to pickle >> > these instances. >> >> > Pickle cannot find the class definitions of the instances. I am trying >> > to add a line to the __new__ of the metaclass to add the new class >> > under the right name in the right module/place, so pickle can find >> > it. >> >> > Is this the right approach? Can anyone explain to me where/how to add >> > these classes for pickle to find and maybe why? >> >> It sounds like you're trying to do something like this? >> >> >>> class MetaClass(type): >> >> ... pass >> ...>>> instance = MetaClass('', (object,), {})() >> >>> instance >> >> <__main__. object at 0x00CC00F0>>>> import pickle >> >>> pickle.dumps(instance) >> >> Traceback (most recent call last): >> File "", line 1, in >> File "c:\python27\lib\pickle.py", line 1374, in dumps >> Pickler(file, protocol).dump(obj) >> File "c:\python27\lib\pickle.py", line 224, in dump >> self.save(obj) >> File "c:\python27\lib\pickle.py", line 331, in save >> self.save_reduce(obj=obj, *rv) >> File "c:\python27\lib\pickle.py", line 401, in save_reduce >> save(args) >> File "c:\python27\lib\pickle.py", line 286, in save >> f(self, obj) # Call unbound method with explicit self >> File "c:\python27\lib\pickle.py", line 562, in save_tuple >> save(element) >> File "c:\python27\lib\pickle.py", line 295, in save >> self.save_global(obj) >> File "c:\python27\lib\pickle.py", line 748, in save_global >> (obj, module, name)) >> pickle.PicklingError: Can't pickle '>: >> it's not found as __main__. >> >> Yeah, pickle's not going to work with anonymous classes. As you >> suggest, you could dynamically add the classes to the module namespace >> so that pickle.dumps will find them, but bear in mind that they will >> also have to exist when calling pickle.loads, so you will need to be >> able to reconstruct the same anonymous classes before unpickling later >> on. >> >> Cheers, >> Ian > > Thank you Ian for the minimal example. This is almost the case i was > trying to discribe, however the classes will be named at runtime and > the class definitions will be persisted in a database. > > Can you help me with how to add the classes to the correct namespace? > The application spans multiple modules (or compared to the example, > the metaclass definition will be in another module then one where the > class and instance will be generated). If the metaclass is global in whatever module you don't need to bother about that. The problem is that you cannot access the classes (metaclass instances) under a dotted name. One workaround is a pseudo-module that does whatever is needed to recreate the class: import pickle import sys class MetaClass(type): pass class M(object): def __init__(self, module): self.__module = module def __getattr__(self, name): print "creating class", name class_ = MetaClass(name, (), {"__module__": self.__module}) setattr(self, name, class_) return class_ sys.modules["m"] = M("m") import m c = m.x s = pickle.dumps(c) print repr(s) d = pickle.loads(s) assert c is d sys.modules["m"] = M("m") e = pickle.loads(s) assert c is not e The official way is probably what Robert mentioned, via the copy_reg module, but I didn't get it to work. From mo.menezes at gmail.com Fri Dec 30 08:21:57 2011 From: mo.menezes at gmail.com (Mario Menezes) Date: Fri, 30 Dec 2011 05:21:57 -0800 (PST) Subject: Is python.org down? Message-ID: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Hi, I?m trying to access pypi.python.org as well as www.python.org and none of them is up? Anybody else having problems with python.org today? Is there a scheduled downtime? Thanks Mario Menezes Brazil From tist.verdonck at gmail.com Fri Dec 30 08:39:37 2011 From: tist.verdonck at gmail.com (tistje) Date: Fri, 30 Dec 2011 05:39:37 -0800 (PST) Subject: Is python.org down? References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: <2f72f9e7-8045-455c-ade7-506c378fe733@b32g2000yqn.googlegroups.com> On 30 dec, 14:21, Mario Menezes wrote: > Hi, > > ?I?m trying to access pypi.python.org as well as www.python.org and none of them is up? > > ? Anybody else having problems with python.org today? Is there a scheduled downtime? > > ? Thanks > > Mario Menezes > Brazil Seems like it is down ... I downloaded the 2.7.2 Windows installer a couple of hours ago, but now even the download page isn't accessible. Tist Verdonck Belgium From rosuav at gmail.com Fri Dec 30 08:48:47 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 31 Dec 2011 00:48:47 +1100 Subject: Is python.org down? In-Reply-To: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: On Sat, Dec 31, 2011 at 12:21 AM, Mario Menezes wrote: > Hi, > > ?I?m trying to access pypi.python.org as well as www.python.org and none of them is up? > > ?Anybody else having problems with python.org today? Is there a scheduled downtime? > > ?Thanks Appears to be up now, for me at least. No reported troubles with DNS. If you're still unable to get to it, try doing a DNS lookup, or try pinging 82.94.164.162 (which is www.python.org according to my lookup). Which one's out? ChrisA From jerome at jolimont.fr Fri Dec 30 08:50:56 2011 From: jerome at jolimont.fr (=?UTF-8?B?SsOpcsO0bWU=?=) Date: Fri, 30 Dec 2011 14:50:56 +0100 Subject: Is python.org down? In-Reply-To: <2f72f9e7-8045-455c-ade7-506c378fe733@b32g2000yqn.googlegroups.com> References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> <2f72f9e7-8045-455c-ade7-506c378fe733@b32g2000yqn.googlegroups.com> Message-ID: <20111230145056.40205fd6@bouzin.lan> Fri, 30 Dec 2011 05:39:37 -0800 (PST) tistje a ?crit: > On 30 dec, 14:21, Mario Menezes wrote: > > Hi, > > > > ?I?m trying to access pypi.python.org as well as www.python.org and none > > of them is up? > > > > ? Anybody else having problems with python.org today? Is there a > > scheduled downtime? > Seems like it is down ... I downloaded the 2.7.2 Windows installer a > couple of hours ago, but now even the download page isn't accessible. http://www.python.org seems to be working, now. -- J?r?me France From tist.verdonck at gmail.com Fri Dec 30 08:51:35 2011 From: tist.verdonck at gmail.com (tistje) Date: Fri, 30 Dec 2011 05:51:35 -0800 (PST) Subject: Is python.org down? References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: <7be1cf8a-677d-492e-8252-f4b68dcb9189@u6g2000vbg.googlegroups.com> On 30 dec, 14:48, Chris Angelico wrote: > On Sat, Dec 31, 2011 at 12:21 AM, Mario Menezes wrote: > > Hi, > > > ?I?m trying to access pypi.python.org as well aswww.python.organd none of them is up? > > > ?Anybody else having problems with python.org today? Is there a scheduled downtime? > > > ?Thanks > > Appears to be up now, for me at least. No reported troubles with DNS. > If you're still unable to get to it, try doing a DNS lookup, or try > pinging 82.94.164.162 (which iswww.python.orgaccording to my > lookup). Which one's out? > > ChrisA only pypi is still unaccessible (502 Bad Gateway). Tist From mo.menezes at gmail.com Fri Dec 30 08:52:32 2011 From: mo.menezes at gmail.com (Mario Menezes) Date: Fri, 30 Dec 2011 05:52:32 -0800 (PST) Subject: Is python.org down? In-Reply-To: References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: <22152854.1188.1325253153021.JavaMail.geo-discussion-forums@yqbu38> Yeah! www.python.org is up now. pypi.python.org still with problems (502 Bad Gateway). will wait couple time. somebody seems to be working on this. thanks. From mo.menezes at gmail.com Fri Dec 30 08:52:32 2011 From: mo.menezes at gmail.com (Mario Menezes) Date: Fri, 30 Dec 2011 05:52:32 -0800 (PST) Subject: Is python.org down? In-Reply-To: References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: <22152854.1188.1325253153021.JavaMail.geo-discussion-forums@yqbu38> Yeah! www.python.org is up now. pypi.python.org still with problems (502 Bad Gateway). will wait couple time. somebody seems to be working on this. thanks. From python at bdurham.com Fri Dec 30 09:11:51 2011 From: python at bdurham.com (python at bdurham.com) Date: Fri, 30 Dec 2011 09:11:51 -0500 Subject: Is python.org down? In-Reply-To: References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> Message-ID: <1325254311.9097.140661017425721@webmail.messagingengine.com> A good tool for determining whether a site is down or just unavailable to you: http://downorisitjustme.com Malcolm From vikashagrawal1990 at gmail.com Fri Dec 30 09:51:50 2011 From: vikashagrawal1990 at gmail.com (vikash agrawal) Date: Fri, 30 Dec 2011 20:21:50 +0530 Subject: Is python.org down? In-Reply-To: <7be1cf8a-677d-492e-8252-f4b68dcb9189@u6g2000vbg.googlegroups.com> References: <29615866.12.1325251317664.JavaMail.geo-discussion-forums@yqgn9> <7be1cf8a-677d-492e-8252-f4b68dcb9189@u6g2000vbg.googlegroups.com> Message-ID: hi Everyone, it seems http://in.pycon.org/2011/ is also down :( Regards Vikash Agrawal -- sent via HTC Sensation -------------- next part -------------- An HTML attachment was scrubbed... URL: From lars at rational-it.com Fri Dec 30 10:56:11 2011 From: lars at rational-it.com (lars van gemerden) Date: Fri, 30 Dec 2011 07:56:11 -0800 (PST) Subject: pickling instances of metaclass generated classes References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> <208b4f8b-c347-44c6-ac9d-73662cc36dbd@o9g2000yqa.googlegroups.com> Message-ID: On Dec 30, 12:16?pm, lars van gemerden wrote: > On Dec 29, 8:55?pm, Ian Kelly wrote: > > > > > > > > > > > On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden wrote: > > > > Hello, > > > > Can someone help me with the following: > > > > I am using metaclasses to make classes and these classes to make > > > instances. Now I want to use multiprocessing, which needs to pickle > > > these instances. > > > > Pickle cannot find the class definitions of the instances. I am trying > > > to add a line to the __new__ of the metaclass to add the new class > > > under the right name in the right module/place, so pickle can find > > > it. > > > > Is this the right approach? Can anyone explain to me where/how to add > > > these classes for pickle to find and maybe why? > > > It sounds like you're trying to do something like this? > > > >>> class MetaClass(type): > > > ... ? ? pass > > ...>>> instance = MetaClass('', (object,), {})() > > >>> instance > > > <__main__. object at 0x00CC00F0>>>> import pickle > > >>> pickle.dumps(instance) > > > Traceback (most recent call last): > > ? File "", line 1, in > > ? File "c:\python27\lib\pickle.py", line 1374, in dumps > > ? ? Pickler(file, protocol).dump(obj) > > ? File "c:\python27\lib\pickle.py", line 224, in dump > > ? ? self.save(obj) > > ? File "c:\python27\lib\pickle.py", line 331, in save > > ? ? self.save_reduce(obj=obj, *rv) > > ? File "c:\python27\lib\pickle.py", line 401, in save_reduce > > ? ? save(args) > > ? File "c:\python27\lib\pickle.py", line 286, in save > > ? ? f(self, obj) # Call unbound method with explicit self > > ? File "c:\python27\lib\pickle.py", line 562, in save_tuple > > ? ? save(element) > > ? File "c:\python27\lib\pickle.py", line 295, in save > > ? ? self.save_global(obj) > > ? File "c:\python27\lib\pickle.py", line 748, in save_global > > ? ? (obj, module, name)) > > pickle.PicklingError: Can't pickle '>: > > it's not found as __main__. > > > Yeah, pickle's not going to work with anonymous classes. ?As you > > suggest, you could dynamically add the classes to the module namespace > > so that pickle.dumps will find them, but bear in mind that they will > > also have to exist when calling pickle.loads, so you will need to be > > able to reconstruct the same anonymous classes before unpickling later > > on. > > > Cheers, > > Ian > Ian also wrote: > > ''' > Actually, I was wrong, you probably don't need to do that. ?I suggest > going with Robert Kern's suggestion to either register the class with > the copy_reg module, or (perhaps better since it won't leak > registrations) implement a __reduce__ method on the class. ?For > example, this seems to work: > > >>> def reconstructor(*metaclass_args): > > ... ? ? cls = MetaClass.build_class(*metaclass_args) > ... ? ? self = cls.__new__(cls) > ... ? ? return self > ...>>> class MetaClass(type): > > ... ? ? @classmethod > ... ? ? def build_class(mcs, arg1, arg2, arg3): > ... ? ? ? ? # Do something useful with the args... > ... ? ? ? ? class _AnonymousClass(object): > ... ? ? ? ? ? ? __metaclass__ = mcs > ... ? ? ? ? ? ? def __reduce__(self): > ... ? ? ? ? ? ? ? ? return (reconstructor, ('foo', 'bar', 'baz'), > self.__dict__) > ... ? ? ? ? return _AnonymousClass > ...>>> instance = MetaClass.build_class('foo', 'bar', 'baz')() > >>> instance > > <__main__._AnonymousClass object at 0x011DB410>>>> instance.banana = 42 > >>> import pickle > >>> s = pickle.dumps(instance) > >>> s > > "c__main__\nreconstructor > \np0\n(S'foo'\np1\nS'bar'\np2\nS'baz'\np3\ntp4\nRp5\n(dp6\nS'banana'\np7\nI 42\nsb.">>> inst2 = pickle.loads(s) > >>> inst2 > > <__main__._AnonymousClass object at 0x011DBE90>>>> inst2.banana > 42 > >>> inst2.__class__ is instance.__class__ > > False > > Cheers, > Ian > > ''' Interesting, though I cannot say I completely understand this solution (looked up __reduce__, but still). I am trying to adapt this example to a situation where the metaclass generated classes are named at runtime (not anonymous), but cannot figure it out. Cheers, Lars From lars at rational-it.com Fri Dec 30 11:51:38 2011 From: lars at rational-it.com (lars van gemerden) Date: Fri, 30 Dec 2011 08:51:38 -0800 (PST) Subject: pickling instances of metaclass generated classes References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> <208b4f8b-c347-44c6-ac9d-73662cc36dbd@o9g2000yqa.googlegroups.com> Message-ID: On Dec 30, 4:56?pm, lars van gemerden wrote: > On Dec 30, 12:16?pm, lars van gemerden wrote: > > > > > > > > > > > On Dec 29, 8:55?pm, Ian Kelly wrote: > > > > On Thu, Dec 29, 2011 at 2:55 AM, lars van gemerden wrote: > > > > > Hello, > > > > > Can someone help me with the following: > > > > > I am using metaclasses to make classes and these classes to make > > > > instances. Now I want to use multiprocessing, which needs to pickle > > > > these instances. > > > > > Pickle cannot find the class definitions of the instances. I am trying > > > > to add a line to the __new__ of the metaclass to add the new class > > > > under the right name in the right module/place, so pickle can find > > > > it. > > > > > Is this the right approach? Can anyone explain to me where/how to add > > > > these classes for pickle to find and maybe why? > > > > It sounds like you're trying to do something like this? > > > > >>> class MetaClass(type): > > > > ... ? ? pass > > > ...>>> instance = MetaClass('', (object,), {})() > > > >>> instance > > > > <__main__. object at 0x00CC00F0>>>> import pickle > > > >>> pickle.dumps(instance) > > > > Traceback (most recent call last): > > > ? File "", line 1, in > > > ? File "c:\python27\lib\pickle.py", line 1374, in dumps > > > ? ? Pickler(file, protocol).dump(obj) > > > ? File "c:\python27\lib\pickle.py", line 224, in dump > > > ? ? self.save(obj) > > > ? File "c:\python27\lib\pickle.py", line 331, in save > > > ? ? self.save_reduce(obj=obj, *rv) > > > ? File "c:\python27\lib\pickle.py", line 401, in save_reduce > > > ? ? save(args) > > > ? File "c:\python27\lib\pickle.py", line 286, in save > > > ? ? f(self, obj) # Call unbound method with explicit self > > > ? File "c:\python27\lib\pickle.py", line 562, in save_tuple > > > ? ? save(element) > > > ? File "c:\python27\lib\pickle.py", line 295, in save > > > ? ? self.save_global(obj) > > > ? File "c:\python27\lib\pickle.py", line 748, in save_global > > > ? ? (obj, module, name)) > > > pickle.PicklingError: Can't pickle '>: > > > it's not found as __main__. > > > > Yeah, pickle's not going to work with anonymous classes. ?As you > > > suggest, you could dynamically add the classes to the module namespace > > > so that pickle.dumps will find them, but bear in mind that they will > > > also have to exist when calling pickle.loads, so you will need to be > > > able to reconstruct the same anonymous classes before unpickling later > > > on. > > > > Cheers, > > > Ian > > Ian also wrote: > > > ''' > > Actually, I was wrong, you probably don't need to do that. ?I suggest > > going with Robert Kern's suggestion to either register the class with > > the copy_reg module, or (perhaps better since it won't leak > > registrations) implement a __reduce__ method on the class. ?For > > example, this seems to work: > > > >>> def reconstructor(*metaclass_args): > > > ... ? ? cls = MetaClass.build_class(*metaclass_args) > > ... ? ? self = cls.__new__(cls) > > ... ? ? return self > > ...>>> class MetaClass(type): > > > ... ? ? @classmethod > > ... ? ? def build_class(mcs, arg1, arg2, arg3): > > ... ? ? ? ? # Do something useful with the args... > > ... ? ? ? ? class _AnonymousClass(object): > > ... ? ? ? ? ? ? __metaclass__ = mcs > > ... ? ? ? ? ? ? def __reduce__(self): > > ... ? ? ? ? ? ? ? ? return (reconstructor, ('foo', 'bar', 'baz'), > > self.__dict__) > > ... ? ? ? ? return _AnonymousClass > > ...>>> instance = MetaClass.build_class('foo', 'bar', 'baz')() > > >>> instance > > > <__main__._AnonymousClass object at 0x011DB410>>>> instance.banana = 42 > > >>> import pickle > > >>> s = pickle.dumps(instance) > > >>> s > > > "c__main__\nreconstructor > > \np0\n(S'foo'\np1\nS'bar'\np2\nS'baz'\np3\ntp4\nRp5\n(dp6\nS'banana'\np7\nI 42\nsb.">>> inst2 = pickle.loads(s) > > >>> inst2 > > > <__main__._AnonymousClass object at 0x011DBE90>>>> inst2.banana > > 42 > > >>> inst2.__class__ is instance.__class__ > > > False > > > Cheers, > > Ian > > > ''' > > Interesting, though I cannot say I completely understand this solution > (looked up __reduce__, but still). I am trying to adapt this example > to a situation where the metaclass generated classes are named at > runtime (not anonymous), but cannot figure it out. > > Cheers, Lars Found a way to name the classes: def reconstructor(*metaclass_args): cls = MetaClass2.build_class(*metaclass_args) self = cls.__new__(cls) return self class MetaClass(type): @classmethod def build_class(mcs, name, arg1, arg2, arg3): return mcs(name, (object,), {"__reduce__": lambda e: (reconstructor2, (name, arg1, arg2, arg3), e.__dict__)}) I still wonder whether it might be easier to add the class to the namespace. Can anyone help me with that? Regards, Lars From roy at panix.com Fri Dec 30 12:01:58 2011 From: roy at panix.com (Roy Smith) Date: Fri, 30 Dec 2011 09:01:58 -0800 (PST) Subject: Doing a HTTP DELETE operation with urllib2? Message-ID: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> Is there some way to make urllib2.urlopen() perform a DELETE instead of a GET or POST? I'm hoping I don't have to dip way down into httplib. I've got an application test framework built on top of urllib2. It makes heavy use of HTTPCookieProcessor. If I need to use the httplib calls directly, I'll have to re-implement a lot of that machinery. From ian.g.kelly at gmail.com Fri Dec 30 12:09:35 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 30 Dec 2011 10:09:35 -0700 Subject: pickling instances of metaclass generated classes In-Reply-To: References: <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> <208b4f8b-c347-44c6-ac9d-73662cc36dbd@o9g2000yqa.googlegroups.com> Message-ID: On Fri, Dec 30, 2011 at 9:51 AM, lars van gemerden wrote: > I still wonder whether it might be easier to add the class to the > namespace. Can anyone help me with that? from mypackage import mymodule setattr(mymodule, myclass.__name__, myclass) From akhi3030 at gmail.com Fri Dec 30 12:27:44 2011 From: akhi3030 at gmail.com (akhilesh singhania) Date: Fri, 30 Dec 2011 17:27:44 +0000 Subject: Creating a binary only python distribution of a C extension module and including some additional python and c files in it Message-ID: Hi, I have a extension module in C which I want to distribute in binary format, ideally an rpm. Additionally, I want to include some python files (examples on how to use the extension module) and source for a library the module dynamically links to (c,h, and make files). How do I specify the example python file in setup.py so that it will be included in the rpm? If I specify it as scripts, I get the following error: $ python setup.py bdist --format=rpm running build_scripts creating build/scripts-2.6 error: file 'foo.py' does not exist error: Bad exit status from /var/tmp/rpm-tmp.yjws9x (%build) If I specify it as data_files, I get the following error: $ python setup.py bdist --format=rpm error: Installed (but unpackaged) file(s) found: /usr/foo.pyc /usr/foo.pyo If I specify it as py_modules, I do not get errors but it is not included in the resulting rpm. Specifying it as a script works if I use $ python setup.py bdist --format=gztar Additionally, can I control the hierarchy of how the files are laid out in the rpm? Currently, I am specifying the c,h,make files as data_files and they get placed in /usr, which is not desirable. Regards, akhi From dmitrey15 at gmail.com Fri Dec 30 12:51:52 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Fri, 30 Dec 2011 09:51:52 -0800 (PST) Subject: How to get function string name from i-th stack position? Message-ID: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> hi all, how to get string name of a function that is n levels above the current Python interpreter position? Thank you in advance, D. From python.list at tim.thechases.com Fri Dec 30 13:35:53 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Fri, 30 Dec 2011 12:35:53 -0600 Subject: How to get function string name from i-th stack position? In-Reply-To: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: <4EFE0489.7060607@tim.thechases.com> On 12/30/11 11:51, dmitrey wrote: > how to get string name of a function that is n levels above > the current Python interpreter position? Use the results of traceback.extract_stack() from traceback import extract_stack def one(x): print "one", x stk = extract_stack() for mod, lineno, fun_name, call_code_text in stk: print "[%s:%i] in %s" % (mod, lineno, fun_name) def two(x): print "two", x one(x) def three(x): print "three", x two(x) three("Hi") -tkc From dmitrey15 at gmail.com Fri Dec 30 13:43:50 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Fri, 30 Dec 2011 10:43:50 -0800 (PST) Subject: How to get function string name from i-th stack position? References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: On Dec 30, 8:35?pm, Tim Chase wrote: > On 12/30/11 11:51, dmitrey wrote: > > > how to get string name of a function that is n levels above > > the current Python interpreter position? > > Use the results of traceback.extract_stack() > > ? ?from traceback import extract_stack > ? ?def one(x): > ? ? ?print "one", x > ? ? ?stk = extract_stack() > ? ? ?for mod, lineno, fun_name, call_code_text in stk: > ? ? ? ?print "[%s:%i] in %s" % (mod, lineno, fun_name) > ? ?def two(x): > ? ? ?print "two", x > ? ? ?one(x) > ? ?def three(x): > ? ? ?print "three", x > ? ? ?two(x) > ? ?three("Hi") > > -tkc Thank you. And what should I do to get function by itself instead of its string name, e.g. I want to know does this function is my_func or any other? For example, I would like to check is this function Python sum(), or maybe numpy.sum(), or anything else? Regards, D. From roy at panix.com Fri Dec 30 13:57:06 2011 From: roy at panix.com (Roy Smith) Date: Fri, 30 Dec 2011 10:57:06 -0800 (PST) Subject: Doing a HTTP DELETE operation with urllib2? In-Reply-To: References: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> Message-ID: <20419811.1501.1325271426676.JavaMail.geo-discussion-forums@vbmq3> Ah, cool. I didn't know you could do that. Thanks. From roy at panix.com Fri Dec 30 13:57:06 2011 From: roy at panix.com (Roy Smith) Date: Fri, 30 Dec 2011 10:57:06 -0800 (PST) Subject: Doing a HTTP DELETE operation with urllib2? In-Reply-To: References: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> Message-ID: <20419811.1501.1325271426676.JavaMail.geo-discussion-forums@vbmq3> Ah, cool. I didn't know you could do that. Thanks. From dougsland at redhat.com Fri Dec 30 15:14:56 2011 From: dougsland at redhat.com (Douglas Landgraf) Date: Fri, 30 Dec 2011 15:14:56 -0500 Subject: Doing a HTTP DELETE operation with urllib2? In-Reply-To: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> References: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> Message-ID: <4EFE1BC0.4060506@redhat.com> Hi, On 12/30/2011 12:01 PM, Roy Smith wrote: > Is there some way to make urllib2.urlopen() perform a DELETE instead of a GET or POST? > > I'm hoping I don't have to dip way down into httplib. I've got an application test framework built on top of urllib2. It makes heavy use of HTTPCookieProcessor. If I need to use the httplib calls directly, I'll have to re-implement a lot of that machinery. You might want to look: https://github.com/dougsland/rhev3-restapi-scripts/blob/master/sample-delete.py -- Cheers Douglas From steve+comp.lang.python at pearwood.info Fri Dec 30 15:37:41 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Dec 2011 20:37:41 GMT Subject: Doing a HTTP DELETE operation with urllib2? References: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> <20419811.1501.1325271426676.JavaMail.geo-discussion-forums@vbmq3> Message-ID: <4efe2115$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Dec 2011 10:57:06 -0800, Roy Smith wrote: > Ah, cool. I didn't know you could do that. Thanks. Who are you talking to, and what is "that"? Replies with no context are somewhat less than useful. It might have made sense in your head when you wrote the reply, but to those reading, it is rather cryptic. -- Steven From a24061 at ducksburg.com Fri Dec 30 15:40:16 2011 From: a24061 at ducksburg.com (Adam Funk) Date: Fri, 30 Dec 2011 20:40:16 +0000 Subject: mutually exclusive arguments to a constructor Message-ID: (Warning: this question obviously reflects the fact that I am more accustomed to using Java than Python.) Suppose I'm creating a class that represents a bearing or azimuth, created either from a string of traditional bearing notation ("N24d30mE") or from a number indicating the angle in degrees as usually measured in trigonometry (65.5, measured counter-clockwise from the x-axis). The class will have methods to return the same bearing in various formats. In Java, I would write two constructors, one taking a single String argument and one taking a single Double argument. But in Python, a class can have only one __init__ method, although it can have a lot of optional arguments with default values. What's the correct way to deal with a situation like the one I've outlined above? -- Unix is a user-friendly operating system. It's just very choosy about its friends. From d at davea.name Fri Dec 30 15:46:55 2011 From: d at davea.name (Dave Angel) Date: Fri, 30 Dec 2011 15:46:55 -0500 Subject: Doing a HTTP DELETE operation with urllib2? In-Reply-To: <4efe2115$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <19590178.802.1325264518483.JavaMail.geo-discussion-forums@vbdz6> <20419811.1501.1325271426676.JavaMail.geo-discussion-forums@vbmq3> <4efe2115$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EFE233F.4030607@davea.name> On 12/30/2011 03:37 PM, Steven D'Aprano wrote: > On Fri, 30 Dec 2011 10:57:06 -0800, Roy Smith wrote: > >> Ah, cool. I didn't know you could do that. Thanks. > Who are you talking to, and what is "that"? > > Replies with no context are somewhat less than useful. It might have made > sense in your head when you wrote the reply, but to those reading, it is > rather cryptic. > > Actually I think he was replying to Roy Smith's message, which has a timestamp of 77 minutes later. Interesting the effects you get when some clocks are allowed to drift. -- DaveA From gd.usenet at spamfence.net Fri Dec 30 16:00:58 2011 From: gd.usenet at spamfence.net (=?ISO-8859-1?Q?G=FCnther?= Dietrich) Date: Fri, 30 Dec 2011 22:00:58 +0100 Subject: mutually exclusive arguments to a constructor References: Message-ID: Adam Funk wrote: >Suppose I'm creating a class that represents a bearing or azimuth, >created either from a string of traditional bearing notation >("N24d30mE") or from a number indicating the angle in degrees as >usually measured in trigonometry (65.5, measured counter-clockwise >from the x-axis). The class will have methods to return the same >bearing in various formats. > >In Java, I would write two constructors, one taking a single String >argument and one taking a single Double argument. But in Python, a >class can have only one __init__ method, although it can have a lot of >optional arguments with default values. What's the correct way to >deal with a situation like the one I've outlined above? You can determine the type of the input data by using isinstance() and take the appropriate actions depending on this decision: >>> class MyClass(object): ... def __init__(self, input_data): ... if isinstance(input_data, basestring): ... print "Do actions for string type input" ... elif isinstance(input_data, float): ... print "Do actions for float type input" ... def get_output_data(self): ... return "output data" ... >>> a = MyClass("String") Do actions for string type input >>> b = MyClass(15.9) Do actions for float type input Best regards, G?nther From mwilson at the-wire.com Fri Dec 30 16:08:51 2011 From: mwilson at the-wire.com (Mel Wilson) Date: Fri, 30 Dec 2011 16:08:51 -0500 Subject: mutually exclusive arguments to a constructor References: Message-ID: Adam Funk wrote: > (Warning: this question obviously reflects the fact that I am more > accustomed to using Java than Python.) > > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as > usually measured in trigonometry (65.5, measured counter-clockwise > from the x-axis). The class will have methods to return the same > bearing in various formats. > > In Java, I would write two constructors, one taking a single String > argument and one taking a single Double argument. But in Python, a > class can have only one __init__ method, although it can have a lot of > optional arguments with default values. What's the correct way to > deal with a situation like the one I've outlined above? Cleanest from the point of view of the class source code would be factory functions at the module level, or special classmethods to deal with the less common cases. You see this a lot in wxPython when they have to deal with overloaded C++ constructors. Most like the Java would be to check within __init__ for a string argument that could be parsed as a bearing, and failing that fall back to treating the argument as a numeric angle. Neither fish nor fowl would be to accept named arguments for the different kinds of values. Mel. From arnodel at gmail.com Fri Dec 30 16:14:05 2011 From: arnodel at gmail.com (Arnaud Delobelle) Date: Fri, 30 Dec 2011 21:14:05 +0000 Subject: mutually exclusive arguments to a constructor In-Reply-To: References: Message-ID: On 30 December 2011 20:40, Adam Funk wrote: > (Warning: this question obviously reflects the fact that I am more > accustomed to using Java than Python.) > > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as > usually measured in trigonometry (65.5, measured counter-clockwise > from the x-axis). ?The class will have methods to return the same > bearing in various formats. > > In Java, I would write two constructors, one taking a single String > argument and one taking a single Double argument. ?But in Python, a > class can have only one __init__ method, although it can have a lot of > optional arguments with default values. ?What's the correct way to > deal with a situation like the one I've outlined above? (Using Python 3 below) Method 1 ---------- Your __init__ method could take the angle as an argument (which seems the most natural to me). Then you could have a class method that takes the string i.e. class Bearing: def __init__(self, angle): self.angle = angle # or whatever your internal reprsentation is @classmethod def fromstring(cls, string): # Here, work out the angle from the string return cls(angle) So you can do: b = Bearing(65.5) or b = Bearing.fromstring("N24d30mE") Method 2 ---------- You can test the type of the argument of the __init__ method class Bearing: def __init__(self, arg): if isinstance(arg, str): # here calculate the value of angle else: angle = float(angle) self.angle = angle Now you can do: b = Bearing(65.5) or b = Bearing("N24d30mE") Both methods are used for builtin types: >>> int('12') 12 >>> int(12.5) 12 >>> dict([(1, 2), (3, 4)]) {1: 2, 3: 4} >>> dict.fromkeys([1, 2]) {1: None, 2: None} HTH -- Arnaud From jason at powerpull.net Fri Dec 30 16:18:29 2011 From: jason at powerpull.net (Jason Friedman) Date: Fri, 30 Dec 2011 21:18:29 +0000 Subject: mutually exclusive arguments to a constructor In-Reply-To: References: Message-ID: > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as > usually measured in trigonometry (65.5, measured counter-clockwise > from the x-axis). ?The class will have methods to return the same > bearing in various formats. > > In Java, I would write two constructors, one taking a single String > argument and one taking a single Double argument. ?But in Python, a > class can have only one __init__ method, although it can have a lot of > optional arguments with default values. ?What's the correct way to > deal with a situation like the one I've outlined above? Similar to other answers already posted: #!/usr/bin/env python class azimuth: def __init__(self, bearing, heading): self.bearing = bearing self.heading = heading if not bearing: self.bearing = 30.5 # or, realistically, a calculation based on the heading if not heading: self.heading = "N..." # or, realistically, a calculation based on the bearing @staticmethod def getBearingInstance(bearing): return azimuth(bearing, None) @staticmethod def getHeadingInstance(heading): return azimuth(None, heading) azimuth1 = azimuth.getBearingInstance("N24d30mE") print azimuth1.heading azimuth2 = azimuth.getHeadingInstance(30) print azimuth2.bearing From ian.g.kelly at gmail.com Fri Dec 30 16:48:49 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 30 Dec 2011 14:48:49 -0700 Subject: How to get function string name from i-th stack position? In-Reply-To: References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: On Fri, Dec 30, 2011 at 11:43 AM, dmitrey wrote: > Thank you. And what should I do to get function by itself instead of > its string name, e.g. I want to know does this function is my_func or > any other? For example, I would like to check is this function Python > sum(), or maybe numpy.sum(), or anything else? The Python stack only includes Python code objects. Built-ins like sum won't appear in it because they're basically C functions and don't have associated code objects. If you really want to see them, you could probably do something with ctypes to inspect the C stack, but I don't recommend it. You can get the Python code objects from the stack by calling inspect.stack(), which includes each frame object currently on the stack as the first member of each tuple. E.g.: frames = map(operator.itemgetter(0), inspect.stack()) Each frame has an f_code attribute that stores the code object associated with that frame. Getting the actual function from the code object is tricky, for two reasons. One, not all code objects represent functions. There are also code objects for modules, class definitions, and probably other thing as well. Two, code objects don't have associated functions. The relationship is the reverse: functions have associated code objects. You would have to iterate over each function that you're interested in, looking for one with a func_code attribute that "is" the frame's f_code attribute. In any case, testing function identity is a rather large rabbit hole that is best avoided. These are mathematically the same function: def plus1(value): return value + 1 plus_one = lambda x: x + 1 But they are two distinct function objects, and there is no way programmatically to determine that they are the same function except by comparing the bytecode (which won't work generally because of the halting problem). What is it that you're trying to do? Perhaps the helpful folks on the list will be able to suggest a better solution if you can provide more details. Cheers, Ian From steve+comp.lang.python at pearwood.info Fri Dec 30 17:13:15 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Dec 2011 22:13:15 GMT Subject: mutually exclusive arguments to a constructor References: Message-ID: <4efe377b$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Dec 2011 20:40:16 +0000, Adam Funk wrote: > (Warning: this question obviously reflects the fact that I am more > accustomed to using Java than Python.) > > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as usually > measured in trigonometry (65.5, measured counter-clockwise from the > x-axis). The class will have methods to return the same bearing in > various formats. > > In Java, I would write two constructors, one taking a single String > argument and one taking a single Double argument. But in Python, a > class can have only one __init__ method, although it can have a lot of > optional arguments with default values. What's the correct way to deal > with a situation like the one I've outlined above? The most idiomatic way to do this would be with named constructor functions, or by testing the argument type in __init__. For example: # Method 1 class Azimuth(object): def __init__(self, angle): # Initialise an azimuth object from an angle (float) self._angle = float(angle) @classmethod def from_bearing(cls, bearing): # Create an azimuth object from a bearing (string). angle = cls.bearing_to_angle(bearing) return cls(angle) @staticmethod def bearing_to_angle(bearing): # Convert a bearing (string) into a float. return 0.0 # whatever... Note some features of this version: * Normal methods receive the instance as first argument, "self". * We use the classmethod and staticmethod decorators to create class and static methods. Be warned that the meaning of these are NOT the same as in Java! * Class methods receive the class object as first argument, "cls". Hence the name. Note that in Python, classes are objects too. * We make from_bearing a class method, so we can call it from either the class itself: ang = Azimuth.from_bearing("25N14E") or from an existing instance: ang2 = ang.from_bearing("17N31W") * Static methods don't receive either the class or the instance. They are equivalent to a top level function, except encapsulated inside a class. # Method 2 class Azimuth(object): def __init__(self, arg): # Initialise an azimuth object from arg, either an angle (float) # or a bearing (string). if isinstance(arg, str): angle = bearing_to_angle(arg) else: angle = float(arg) self._angle = float(angle) def bearing_to_angle(bearing): # Convert a bearing (string) into a float. return 0.0 # whatever... Note that in this example, I've turned bearing_to_angle into a regular function outside of the class instead of a static method. Just because I can. This is probably slightly more idiomatic than the use of static methods. Either method is acceptable, although the first is slightly more "pure" because it doesn't use isinstance. The second may fail if the user passes a string-like object which behaves identically to strings, but doesn't inherit from str. If you care about that, you should prefer the first way with an explicit from_bearing method. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 30 17:21:09 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 30 Dec 2011 22:21:09 GMT Subject: mutually exclusive arguments to a constructor References: Message-ID: <4efe3955$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Dec 2011 21:18:29 +0000, Jason Friedman wrote: > class azimuth: > def __init__(self, bearing, heading): It is conventional, and recommended, to use an initial capital letter for classes. (Yes, Python built-ins violate that rule, and indeed so do some non-built-ins.) See PEP 8 for the recommended style guide. [...] > @staticmethod > def getBearingInstance(bearing): > return azimuth(bearing, None) > @staticmethod > def getHeadingInstance(heading): > return azimuth(None, heading) In this case, you should use classmethod rather than staticmethod and avoid hard-coding the class: @classmethod def getBearingInstance(cls, bearing): return cls(bearing, None) That way subclassing will work correctly: class MyAzimuth(azimuth): pass angle = MyAzimuth.getBearingInstance("N24d30mE") will return a MyAzimuth instance instead of an azimuth instance. -- Steven From roy at panix.com Fri Dec 30 18:24:23 2011 From: roy at panix.com (Roy Smith) Date: Fri, 30 Dec 2011 18:24:23 -0500 Subject: mutually exclusive arguments to a constructor References: Message-ID: In article , Adam Funk wrote: > (Warning: this question obviously reflects the fact that I am more > accustomed to using Java than Python.) > > Suppose I'm creating a class that represents a bearing or azimuth, > created either from a string of traditional bearing notation > ("N24d30mE") or from a number indicating the angle in degrees as > usually measured in trigonometry (65.5, measured counter-clockwise > from the x-axis). There's two ways to do this. One would be to have the __init__ method switch on the type of its argument: def __init__(self, bearing_or_azimuth): if isinstance(bearing_or_azimuth, basestring): # do the bearing thing else: # do the azimuth thing I suspect many people would consider that unpythonic. The other way would be what, in the C++/Java world, would be called the "named constructor idiom". Just write two factory functions: class DirectionIndicatingThingie: @staticmethod def from_bearing(cls, bearing): dit = DirectionIndicatingThingie() dit.direction = whatever return dit and likewise for from_azimuth() "But!", some C++/Java type bondage addicts might cry, "there's nothing to prevent somebody from creating a DirectionIndicatingThingie directly, bypassing the factory functions. There's no way to make the constructor private!". To which the free-willed pythonistas would respond, "If it hurts when you do that, don't do that". From rosuav at gmail.com Fri Dec 30 18:36:18 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 31 Dec 2011 10:36:18 +1100 Subject: mutually exclusive arguments to a constructor In-Reply-To: References: Message-ID: On Sat, Dec 31, 2011 at 10:24 AM, Roy Smith wrote: > "But!", some C++/Java type bondage addicts might cry, "there's nothing > to prevent somebody from creating a DirectionIndicatingThingie directly, > bypassing the factory functions. ?There's no way to make the constructor > private!". ?To which the free-willed pythonistas would respond, "If it > hurts when you do that, don't do that". You know a Python programmer's been at your C++ code when it opens: #define class struct ChrisA From roy at panix.com Fri Dec 30 18:39:16 2011 From: roy at panix.com (Roy Smith) Date: Fri, 30 Dec 2011 18:39:16 -0500 Subject: mutually exclusive arguments to a constructor References: Message-ID: In article , Chris Angelico wrote: > On Sat, Dec 31, 2011 at 10:24 AM, Roy Smith wrote: > > "But!", some C++/Java type bondage addicts might cry, "there's nothing > > to prevent somebody from creating a DirectionIndicatingThingie directly, > > bypassing the factory functions. ?There's no way to make the constructor > > private!". ?To which the free-willed pythonistas would respond, "If it > > hurts when you do that, don't do that". > > You know a Python programmer's been at your C++ code when it opens: > #define class struct Why stop there? #define private public From rosuav at gmail.com Fri Dec 30 18:47:20 2011 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 31 Dec 2011 10:47:20 +1100 Subject: mutually exclusive arguments to a constructor In-Reply-To: References: Message-ID: On Sat, Dec 31, 2011 at 10:39 AM, Roy Smith wrote: > In article , > ?Chris Angelico wrote: > >> You know a Python programmer's been at your C++ code when it opens: >> #define class struct > > Why stop there? > > #define private public Probably yeah, do both. Anyway, life's so much easier when you don't have to write trivial getter/setter methods (and then maintain them). I've never had a situation where I've changed a private member while keeping the getters and setters unchanged; the ONLY benefit accessor methods have ever given to me personally has been logging (and granted, that is hard to do without them - since you can't override __getitem__ in C++ - but how often do you really need that facility?). I used to believe in the separation of interface from implementation. Then I realised that most of the separation was transparent anyway, and gave up on it. And then realised why the separation is a good idea after all. :) ChrisA From rich at noir.com Fri Dec 30 20:17:51 2011 From: rich at noir.com (K Richard Pixley) Date: Fri, 30 Dec 2011 17:17:51 -0800 Subject: Which library for audio playback ? In-Reply-To: References: Message-ID: <3ptLq.44453$8O1.22863@newsfe07.iad> On 12/29/11 05:55 , J?r?me wrote: > I'm writing a small application that plays sound through the speakers. The > sounds are juste sine waves of arbitrary frequency I create in the code, not > sample .wav files. > > I didn't expect the choice for an audio library to be that complicated. There > are several libraries, and none of them seems to be *the* reference. > > Searching the web, I found these resources : > > http://wiki.python.org/moin/Audio > http://wiki.python.org/moin/PythonInMusic > > * I privileged ALSA to OSS as I read elsewhere that OSS is deprecated, or on > its way to be. (And JACK is not widely installed, apart from specific > applications (audio work).) > > * Then, I picked the alsaaudio library (http://pyalsaaudio.sourceforge.net/). > I don't remember exactly why. I think the project had the most recent > updates. I don't think any project claims to be (let alone aims at being) > complete. > > I'm wondering if I made a sensible choice. > > There are other libraries, including the following two that are platform > independent : > > * PyAudiere (http://pyaudiere.org/), OSS , not packaged for debian > * PyAudio (http://people.csail.mit.edu/hubert/pyaudio/) > > What solution would you recommend ? > > Are there other criterions I should take into account ? > > Thanks. > I made a similar survey of available libraries recently. I'm interested in MIDI also, though, primarily on mac. There doesn't seem to be any definitive audio library for linux, although several, (jack, oss, alsa), exist. (My impression is similar to yours, OSS is obsolete. Jack may be the better library technically, but is not as widely ported or available as ALSA.) There is a definitive library for both audio and MIDI on mac - core audio. There really aren't any competitors. There is also a definitive library for windows, although I don't use windows. It's the open source one with low latency that everyone in the professional world uses to replace windows because, (surprise!), windows isn't capable of coping. I have found python libraries for each of these, although some are very low level libraries, basically just wrapping something even lower. If anyone has done an integrated "full solution" for linux or mac, I didn't find it. The closest I found was PortMIDI and PortAudio which appear to have ports for all three platforms as well as one or two sets of python bindings and seem to be high enough level to be both useful and productive. The hard part there is that PortMIDI and PortAudio come in source, which requires a bit of hunting to track down prerequisites, etc. Please keep us posted as I'm chasing a similar problem. --rich From rich at noir.com Fri Dec 30 20:19:07 2011 From: rich at noir.com (K Richard Pixley) Date: Fri, 30 Dec 2011 17:19:07 -0800 Subject: Generating sin/square waves sound In-Reply-To: References: Message-ID: On 12/29/11 23:17 , Paulo da Silva wrote: > Hi, > Sorry if this is a FAQ, but I have googled and didn't find any > satisfatory answer. > > Is there a simple way, preferably multiplataform (or linux), of > generating sinusoidal/square waves sound in python? > > Thanks for any answers/suggestions. I just posted on this elsewhere. Look for a thread titled: "Which library for audio playback ?" --rich From pavanbond0001 at gmail.com Fri Dec 30 22:31:58 2011 From: pavanbond0001 at gmail.com (pavanm k) Date: Fri, 30 Dec 2011 19:31:58 -0800 (PST) Subject: ONLINE DATA ENTRY JOBS Message-ID: <7a86d02c-b365-4827-a2f0-b4f3d21a11d3@c42g2000prb.googlegroups.com> ONLINE DATA ENTRY JOBS http://pavankumaronlinejobs.blogsspot.com From niklasro at gmail.com Fri Dec 30 23:58:10 2011 From: niklasro at gmail.com (Niklas Rosencrantz) Date: Fri, 30 Dec 2011 20:58:10 -0800 (PST) Subject: InvalidResponseError: headers must be str Message-ID: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> I'm upgrading from python 2.5 to python 2.7 and then I'm starting to get this error: InvalidResponseError: headers must be str I think it is because somewhere my HTTP headers are cast to unicode instead of string but I can't find where in the code? The example code I'm trying to upgrade to python 2.7 is https://github.com/supernifty/gae-paypal-online-market-example class Pay( object ): def __init__( self, amount, return_url, cancel_url, remote_address, secondary_receiver=None, ipn_url=None, shipping=False ): headers = { 'X-PAYPAL-SECURITY-USERID': str(settings.PAYPAL_USERID), 'X-PAYPAL-SECURITY-PASSWORD': str(settings.PAYPAL_PASSWORD), 'X-PAYPAL-SECURITY-SIGNATURE': str(settings.PAYPAL_SIGNATURE), 'X-PAYPAL-REQUEST-DATA-FORMAT': str('JSON'), 'X-PAYPAL-RESPONSE-DATA-FORMAT': str('JSON'), 'X-PAYPAL-APPLICATION-ID': str(settings.PAYPAL_APPLICATION_ID), 'X-PAYPAL-DEVICE-IPADDRESS': str(remote_address), } data = { 'currencyCode': 'USD', 'returnUrl': return_url, 'cancelUrl': cancel_url, 'requestEnvelope': { 'errorLanguage': 'en_US' }, } if shipping: data['actionType'] = 'CREATE' else: data['actionType'] = 'PAY' if secondary_receiver == None: # simple payment data['receiverList'] = { 'receiver': [ { 'email': settings.PAYPAL_EMAIL, 'amount': '%f' % amount } ] } else: # chained commission = amount * settings.PAYPAL_COMMISSION data['receiverList'] = { 'receiver': [ { 'email': settings.PAYPAL_EMAIL, 'amount': '%0.2f' % amount, 'primary': 'true' }, { 'email': secondary_receiver, 'amount': '%0.2f' % ( amount - commission ), 'primary': 'false' }, ] } if ipn_url != None: data['ipnNotificationUrl'] = str(ipn_url) self.raw_request = json.dumps(data) #request = urllib2.Request( "%s%s" % ( settings.PAYPAL_ENDPOINT, "Pay" ), data=self.raw_request, headers=headers ) #self.raw_response = urllib2.urlopen( request ).read() self.raw_response = url_request( "%s%s" % ( str(settings.PAYPAL_ENDPOINT), str("Pay") ), data=self.raw_request, headers=headers ).content() logging.info( "response was: %s" % self.raw_response ) self.response = json.loads( str(self.raw_response) ) From emekamicro at gmail.com Sat Dec 31 00:48:29 2011 From: emekamicro at gmail.com (Emeka) Date: Sat, 31 Dec 2011 07:48:29 +0200 Subject: dict_to_xml Message-ID: Hello All, I have a dictionary object I would like to convert to xml. Could some assist with the link to libs to use? Or good examples. Regards, Janus -- *Satajanus Nig. Ltd * -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Sat Dec 31 01:40:42 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Dec 2011 06:40:42 GMT Subject: InvalidResponseError: headers must be str References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> Message-ID: <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> On Fri, 30 Dec 2011 20:58:10 -0800, Niklas Rosencrantz wrote: > I'm upgrading from python 2.5 to python 2.7 and then I'm starting to get > this error: > > InvalidResponseError: headers must be str Please show the ENTIRE traceback, not just the error message. The traceback is very informative: it shows the actual line of code that causes the problem, plus the entire sequence of calls that lead to it. The error message on its own is almost useless. Please COPY AND PASTE the traceback, do not re-type it from memory, summarize it, simplify it, or otherwise change it in any way. > I think it is because somewhere my HTTP headers are cast to unicode Why do you think that? > instead of string but I can't find where in the code? Have you tried searching for any of these? - direct calls to unicode() - unicode literals u"..." - calls to the decode method some_string.decode( ... ) > The example code > I'm trying to upgrade to python 2.7 is > https://github.com/supernifty/gae-paypal-online-market-example > > class Pay( object ): > def __init__( self, amount, return_url, cancel_url, remote_address, > secondary_receiver=None, ipn_url=None, shipping=False ): > headers = { > 'X-PAYPAL-SECURITY-USERID': str(settings.PAYPAL_USERID), > 'X-PAYPAL-SECURITY-PASSWORD': str(settings.PAYPAL_PASSWORD), > 'X-PAYPAL-SECURITY-SIGNATURE': str(settings.PAYPAL_SIGNATURE), > 'X-PAYPAL-REQUEST-DATA-FORMAT': str('JSON'), > 'X-PAYPAL-RESPONSE-DATA-FORMAT': str('JSON'), > 'X-PAYPAL-APPLICATION-ID': str(settings.PAYPAL_APPLICATION_ID), > 'X-PAYPAL-DEVICE-IPADDRESS': str(remote_address), > } 'JSON' is already a string. Calling str() on it is a waste of time. What values do the various settings.* have? If they are already strings, calling str() again is a waste of time. I see you do this all through your class, needlessly calling str() on strings. -- Steven From vojtech.rylko at gmail.com Sat Dec 31 03:08:24 2011 From: vojtech.rylko at gmail.com (=?UTF-8?B?Vm9qdMSbY2ggUnlsa28=?=) Date: Sat, 31 Dec 2011 09:08:24 +0100 Subject: dict_to_xml In-Reply-To: References: Message-ID: <4EFEC2F8.9080006@gmail.com> Hello, there is one simple example which I made for my needs - https://github.com/vojtarylko/dict2xml Regards, Vojt?ch Dne 31.12.2011 6:48, Emeka napsal(a): > Hello All, > > > I have a dictionary object I would like to convert to xml. > > Could some assist with the link to libs to use? Or good examples. > > Regards, > Janus > > -- > /Satajanus Nig. Ltd > > > / > > From niklasro at gmail.com Sat Dec 31 03:32:54 2011 From: niklasro at gmail.com (Niklas Rosencrantz) Date: Sat, 31 Dec 2011 00:32:54 -0800 (PST) Subject: InvalidResponseError: headers must be str In-Reply-To: <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <3262240.2559.1325320374936.JavaMail.geo-discussion-forums@yqdj19> Thank you for the reply. I had the same error message before and it was resolved when I removed a casting of a header value from unicode to str. Now in this case I can't see where that happens or what causes the error. The full trace I'm experiencing now is File "/media/Lexar/montao/google/appengine/runtime/wsgi.py", line 129, in _StartResponse raise InvalidResponseError('headers must be str') InvalidResponseError: headers must be str INFO 2011-12-31 04:55:36,484 recording.py:372] Saved; key: __appstats__:034800, part: 137 bytes, full: 27325 bytes, overhead: 0.001 + 0.008; link: http://localhost:8080/_ah/stats/details?time=1325307334890 ERROR 2011-12-31 04:55:36,484 wsgi.py:186] Traceback (most recent call last): File "/media/Lexar/montao/google/appengine/runtime/wsgi.py", line 175, in Handle for chunk in result: File "/media/Lexar/montao/google/appengine/ext/appstats/recording.py", line 926, in appstats_wsgi_wrapper result = app(environ, appstats_start_response) File "/media/Lexar/montao/lib/webapp2/webapp2.py", line 1524, in __call__ return self._internal_error(e)(environ, start_response) File "/media/Lexar/montao/lib/webapp2/webapp2.py", line 1522, in __call__ return response(environ, start_response) File "/media/Lexar/montao/lib/webob/webob/__init__.py", line 2000, in __call__ start_response(self.status, self.headerlist) File "/media/Lexar/montao/google/appengine/ext/appstats/recording.py", line 923, in appstats_start_response return start_response(status, headers, exc_info) File "/media/Lexar/montao/google/appengine/runtime/wsgi.py", line 129, in _StartResponse raise InvalidResponseError('headers must be str') InvalidResponseError: headers must be str From dmitrey15 at gmail.com Sat Dec 31 03:44:50 2011 From: dmitrey15 at gmail.com (dmitrey) Date: Sat, 31 Dec 2011 00:44:50 -0800 (PST) Subject: How to get function string name from i-th stack position? References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: <4b0f3a04-3b29-4cb7-af52-2001b25d560a@t30g2000vbx.googlegroups.com> On Dec 30, 11:48?pm, Ian Kelly wrote: > On Fri, Dec 30, 2011 at 11:43 AM, dmitrey wrote: > > Thank you. And what should I do to get function by itself instead of > > its string name, e.g. I want to know does this function is my_func or > > any other? For example, I would like to check is this function Python > > sum(), or maybe numpy.sum(), or anything else? > > The Python stack only includes Python code objects. ?Built-ins like > sum won't appear in it because they're basically C functions and don't > have associated code objects. ?If you really want to see them, you > could probably do something with ctypes to inspect the C stack, but I > don't recommend it. > > You can get the Python code objects from the stack by calling > inspect.stack(), which includes each frame object currently on the > stack as the first member of each tuple. ?E.g.: > > frames = map(operator.itemgetter(0), inspect.stack()) > > Each frame has an f_code attribute that stores the code object > associated with that frame. ?Getting the actual function from the code > object is tricky, for two reasons. ?One, not all code objects > represent functions. ?There are also code objects for modules, class > definitions, and probably other thing as well. ?Two, code objects > don't have associated functions. The relationship is the reverse: > functions have associated code objects. ?You would have to iterate > over each function that you're interested in, looking for one with a > func_code attribute that "is" the frame's f_code attribute. > > In any case, testing function identity is a rather large rabbit hole > that is best avoided. ?These are mathematically the same function: > > def plus1(value): > ? ? return value + 1 > > plus_one = lambda x: x + 1 > > But they are two distinct function objects, and there is no way > programmatically to determine that they are the same function except > by comparing the bytecode (which won't work generally because of the > halting problem). > > What is it that you're trying to do? ?Perhaps the helpful folks on the > list will be able to suggest a better solution if you can provide more > details. > > Cheers, > Ian Maybe it is somehow possible to compare function id with my candidates id, e.g. PythonSumID = id(sum) import numpy NumpySumID = id(numpy.sum) func = getting_function_from_Nth_stack_level_above if id(func) == PythonSumID: .... elif id(func) == NumpySumID: .... else: .... I need it due to the following reason: FuncDesigner users often use Python or numpy sum on FuncDesigner objects, while FuncDesigner.sum() is optimized for this case, works faster and doesn't lead to "Max recursion dept exceeded", that sometimes trigger for numpy or Python sum() when number of summarized elements is more than several hundreds. I would like to print warning "you'd better use FuncDesigner sum" if this case has been identified. Regards, D. From niklasro at gmail.com Sat Dec 31 03:44:50 2011 From: niklasro at gmail.com (Niklas Rosencrantz) Date: Sat, 31 Dec 2011 00:44:50 -0800 (PST) Subject: InvalidResponseError: headers must be str In-Reply-To: <3262240.2559.1325320374936.JavaMail.geo-discussion-forums@yqdj19> References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> <3262240.2559.1325320374936.JavaMail.geo-discussion-forums@yqdj19> Message-ID: <29532651.581.1325321090077.JavaMail.geo-discussion-forums@yqlp13> I can log the headers and it seems that they are strings: INFO 2011-12-31 08:43:03,286 paypal.py:143] headers: {'X-PAYPAL-REQUEST-DATA-FORMAT': 'JSON', 'X-PAYPAL-SECURITY-PASSWORD': '1324348659', 'X-PAYPAL-RESPONSE-DATA-FORMAT': 'JSON', 'X-PAYPAL-SECURITY-SIGNATURE': 'Al6H7Bq3kp4bKFht2fR-p2FlAbp3AJFKl5pFMzHpo.QKYe4Uob5YPIm.', 'X-PAYPAL-SECURITY-USERID': 'niklas_1354859649_biz_api1.eddaconsult.se', 'X-PAYPAL-DEVICE-IPADDRESS': '127.0.0.1', 'X-PAYPAL-APPLICATION-ID': 'APP-80W284485P519543T'} From storchaka at gmail.com Sat Dec 31 05:04:13 2011 From: storchaka at gmail.com (Serhiy Storchaka) Date: Sat, 31 Dec 2011 12:04:13 +0200 Subject: InvalidResponseError: headers must be str In-Reply-To: <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: 31.12.11 08:40, Steven D'Aprano ???????(??): > 'JSON' is already a string. Calling str() on it is a waste of time. from __future__ import unicode_literals From jerome at jolimont.fr Sat Dec 31 06:02:31 2011 From: jerome at jolimont.fr (=?UTF-8?B?SsOpcsO0bWU=?=) Date: Sat, 31 Dec 2011 12:02:31 +0100 Subject: Which library for audio playback ? In-Reply-To: <3ptLq.44453$8O1.22863@newsfe07.iad> References: <3ptLq.44453$8O1.22863@newsfe07.iad> Message-ID: <20111231120231.36165856@bouzin.lan> Fri, 30 Dec 2011 17:17:51 -0800 K Richard Pixley a ?crit: > I made a similar survey of available libraries recently. I'm interested > in MIDI also, though, primarily on mac. > > There doesn't seem to be any definitive audio library for linux, > although several, (jack, oss, alsa), exist. (My impression is similar > to yours, OSS is obsolete. Jack may be the better library technically, > but is not as widely ported or available as ALSA.) > > There is a definitive library for both audio and MIDI on mac - core > audio. There really aren't any competitors. > > There is also a definitive library for windows, although I don't use > windows. It's the open source one with low latency that everyone in the > professional world uses to replace windows because, (surprise!), windows > isn't capable of coping. > > I have found python libraries for each of these, although some are very > low level libraries, basically just wrapping something even lower. If > anyone has done an integrated "full solution" for linux or mac, I didn't > find it. The closest I found was PortMIDI and PortAudio which appear to > have ports for all three platforms as well as one or two sets of python > bindings and seem to be high enough level to be both useful and > productive. The hard part there is that PortMIDI and PortAudio come in > source, which requires a bit of hunting to track down prerequisites, etc. > > Please keep us posted as I'm chasing a similar problem. Hi. Looking at it again, I've narrowed down to the following few : ossaudiodev http://docs.python.org/library/ossaudiodev.html Linux only but included in python pygame http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound Most occurring answer on forums and mailing-lists on the net for this question. Might be a little overkill... PortAudio http://people.csail.mit.edu/hubert/pyaudio/ http://www.portaudio.com/ Cross-platform, packaged for debian. Could fit my needs as well. Generally, the examples I find are about opening and playing a wavefile. I guess I'm gonna have to work a little to feed them a homegrown sinewave of arbitrary frequency. Regarding PortAudio, python /usr/share/doc/python-pyaudio/examples/system_info.py (and other examples) yield complaints about jack not being running. I assume they could be tweaked to use ALSA instead. I haven't had time to dig any further. If I come up with something, I'll keep you posted. Thanks for your answer. -- J?r?me From lie.1296 at gmail.com Sat Dec 31 06:41:46 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 31 Dec 2011 22:41:46 +1100 Subject: How to get function string name from i-th stack position? In-Reply-To: References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: On 12/31/2011 08:48 AM, Ian Kelly wrote: > > But they are two distinct function objects, and there is no way > programmatically to determine that they are the same function except > by comparing the bytecode (which won't work generally because of the > halting problem). Actually, it is often possible to determine that two functions are the same function, you simply need a to compare whether the function object lives in the same memory address. It is also possible to determine if two functions are different, if the function object are in different memory address than the function is different function. What is difficult to do due to the Halting problem is comparing whether two different functions are "equivalent" (and therefore interchangeable). I think the OP wants to find the former, not the latter. The former is trivial, the latter impossible. From steve+comp.lang.python at pearwood.info Sat Dec 31 07:52:13 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Dec 2011 12:52:13 GMT Subject: InvalidResponseError: headers must be str References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com> On Sat, 31 Dec 2011 12:04:13 +0200, Serhiy Storchaka wrote: > 31.12.11 08:40, Steven D'Aprano ???????(??): >> 'JSON' is already a string. Calling str() on it is a waste of time. > > from __future__ import unicode_literals Fair point. Your correction is noted. -- Steven From niklasro at gmail.com Sat Dec 31 08:31:14 2011 From: niklasro at gmail.com (Niklas Rosencrantz) Date: Sat, 31 Dec 2011 05:31:14 -0800 (PST) Subject: InvalidResponseError: headers must be str In-Reply-To: <4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> <4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <9589290.553.1325338274423.JavaMail.geo-discussion-forums@yqcb25> I'm still no further to reaching a solution and my efforts of logging the headers didn't produce anything. I'm certain that it's upgrading from ptyhon 2.5 to python 2.7 that causes this since the GAE SDK uses WSGI instead of CGI now. Any idea about my problem? Thank you From steve+comp.lang.python at pearwood.info Sat Dec 31 09:31:56 2011 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 31 Dec 2011 14:31:56 GMT Subject: InvalidResponseError: headers must be str References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> <4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com> <9589290.553.1325338274423.JavaMail.geo-discussion-forums@yqcb25> Message-ID: <4eff1cdb$0$29966$c3e8da3$5496439d@news.astraweb.com> On Sat, 31 Dec 2011 05:31:14 -0800, Niklas Rosencrantz wrote: > I'm still no further to reaching a solution and my efforts of logging > the headers didn't produce anything. I'm certain that it's upgrading > from ptyhon 2.5 to python 2.7 that causes this since the GAE SDK uses > WSGI instead of CGI now. Any idea about my problem? > > Thank you Have patience. It has been less than a day since you first asked, and it is New Years Day or New Years Eve (depending on where you are). Most people will be away from their computers or busy celebrating. -- Steven From nvidhya01 at gmail.com Sat Dec 31 10:22:51 2011 From: nvidhya01 at gmail.com (n v) Date: Sat, 31 Dec 2011 07:22:51 -0800 (PST) Subject: funny minny year@@@@@@@@@@@@@@@@@@@@ Message-ID: <4cc4cf06-0a55-4a07-9127-c8f38c4bf1fa@n13g2000prf.googlegroups.com> http://123maza.com/48/silver424/ From invalid at invalid.invalid Sat Dec 31 11:27:11 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Sat, 31 Dec 2011 16:27:11 +0000 (UTC) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-12-28, Steven D'Aprano wrote: > On Tue, 27 Dec 2011 18:42:05 -0800, Rick Johnson wrote: > >> I don't care what ANY dictionary says. Much less a "world" >> dictionary. I don't validate or invalidate a word based on some phony >> baloney group of pseudo intellectuals who decided one to day that >> writing a dictionary "might be cool". I am against these words and >> phrases because we already have words that work just fine. Why rock >> the boat? > > Why do you say "rock" when the word "shake" is just as good? > > Why do you say "boat" when we already have "ship"? > > Why do you say "pseudo intellectuals" when you could say "fake > intellectuals"? > > Why do I waste my time reading your pretentious self-important nonsense? http://xkcd.com/386/ ;) -- Grant Edwards grant.b.edwards Yow! Not SENSUOUS ... only at "FROLICSOME" ... and in gmail.com need of DENTAL WORK ... in PAIN!!! From ian.g.kelly at gmail.com Sat Dec 31 12:05:38 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 31 Dec 2011 10:05:38 -0700 Subject: How to get function string name from i-th stack position? In-Reply-To: References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> Message-ID: On Sat, Dec 31, 2011 at 4:41 AM, Lie Ryan wrote: > On 12/31/2011 08:48 AM, Ian Kelly wrote: >> >> >> But they are two distinct function objects, and there is no way >> programmatically to determine that they are the same function except >> by comparing the bytecode (which won't work generally because of the >> halting problem). > > > Actually, it is often possible to determine that two functions are the same > function, you simply need a to compare whether the function object lives in > the same memory address. It is also possible to determine if two functions > are different, if the function object are in different memory address than > the function is different function. > > What is difficult to do due to the Halting problem is comparing whether two > different functions are "equivalent" (and therefore interchangeable). Yes, that is what I said. You can determine whether two function objects are the same, but not whether they are *mathematically* the same function. > I think the OP wants to find the former, not the latter. The former is > trivial, the latter impossible. Based on his subsequent clarification, I agree, although from his original post it sounded like he was just trying to identify summing functions in general. However, finding the built-in sum function on the stack is not trivial at all, perhaps not even possible. From ian.g.kelly at gmail.com Sat Dec 31 12:13:08 2011 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 31 Dec 2011 10:13:08 -0700 Subject: How to get function string name from i-th stack position? In-Reply-To: <4b0f3a04-3b29-4cb7-af52-2001b25d560a@t30g2000vbx.googlegroups.com> References: <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> <4b0f3a04-3b29-4cb7-af52-2001b25d560a@t30g2000vbx.googlegroups.com> Message-ID: On Sat, Dec 31, 2011 at 1:44 AM, dmitrey wrote: > On Dec 30, 11:48?pm, Ian Kelly wrote: >> On Fri, Dec 30, 2011 at 11:43 AM, dmitrey wrote: >> > Thank you. And what should I do to get function by itself instead of >> > its string name, e.g. I want to know does this function is my_func or >> > any other? For example, I would like to check is this function Python >> > sum(), or maybe numpy.sum(), or anything else? >> >> The Python stack only includes Python code objects. ?Built-ins like >> sum won't appear in it because they're basically C functions and don't >> have associated code objects. ?If you really want to see them, you >> could probably do something with ctypes to inspect the C stack, but I >> don't recommend it. >> >> You can get the Python code objects from the stack by calling >> inspect.stack(), which includes each frame object currently on the >> stack as the first member of each tuple. ?E.g.: >> >> frames = map(operator.itemgetter(0), inspect.stack()) >> >> Each frame has an f_code attribute that stores the code object >> associated with that frame. ?Getting the actual function from the code >> object is tricky, for two reasons. ?One, not all code objects >> represent functions. ?There are also code objects for modules, class >> definitions, and probably other thing as well. ?Two, code objects >> don't have associated functions. The relationship is the reverse: >> functions have associated code objects. ?You would have to iterate >> over each function that you're interested in, looking for one with a >> func_code attribute that "is" the frame's f_code attribute. >> >> In any case, testing function identity is a rather large rabbit hole >> that is best avoided. ?These are mathematically the same function: >> >> def plus1(value): >> ? ? return value + 1 >> >> plus_one = lambda x: x + 1 >> >> But they are two distinct function objects, and there is no way >> programmatically to determine that they are the same function except >> by comparing the bytecode (which won't work generally because of the >> halting problem). >> >> What is it that you're trying to do? ?Perhaps the helpful folks on the >> list will be able to suggest a better solution if you can provide more >> details. >> >> Cheers, >> Ian > > Maybe it is somehow possible to compare function id with my candidates > id, e.g. > PythonSumID = id(sum) > import numpy > NumpySumID = id(numpy.sum) > func = getting_function_from_Nth_stack_level_above > if id(func) == PythonSumID: > ?.... > elif id(func) == NumpySumID: > ?.... > else: > ?.... > I need it due to the following reason: FuncDesigner users often use > Python or numpy sum on FuncDesigner objects, while FuncDesigner.sum() > is optimized for this case, works faster and doesn't lead to "Max > recursion dept exceeded", that sometimes trigger for numpy or Python > sum() when number of summarized elements is more than several > hundreds. I would like to print warning "you'd better use FuncDesigner > sum" if this case has been identified. The only think I can think of would be to replace the sum built-in with a wrapper that checks whether it's being called on FuncDesigner objects and issues a warning. Users might not like you messing with their built-ins in that way, though. From dgeorge29ca at gmail.com Sat Dec 31 13:19:34 2011 From: dgeorge29ca at gmail.com (davidfx) Date: Sat, 31 Dec 2011 10:19:34 -0800 (PST) Subject: .format vs. % Message-ID: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> Hello everyone, I just have a quick question about .format and %r %s %d. Should we always be using .format() for formatting strings or %? Example a = 'apples' print "I love {0}.".format(a) If I wanted to put .format into a variable, how would I do that. Thanks for your information in advance. David From roy at panix.com Sat Dec 31 13:23:35 2011 From: roy at panix.com (Roy Smith) Date: Sat, 31 Dec 2011 13:23:35 -0500 Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article , Grant Edwards wrote: > On 2011-12-28, Steven D'Aprano wrote: > > On Tue, 27 Dec 2011 18:42:05 -0800, Rick Johnson wrote: > > > >> I don't care what ANY dictionary says. Much less a "world" > >> dictionary. I don't validate or invalidate a word based on some phony > >> baloney group of pseudo intellectuals who decided one to day that > >> writing a dictionary "might be cool". I am against these words and > >> phrases because we already have words that work just fine. Why rock > >> the boat? > > > > Why do you say "rock" when the word "shake" is just as good? > > > > Why do you say "boat" when we already have "ship"? > > > > Why do you say "pseudo intellectuals" when you could say "fake > > intellectuals"? > > > > Why do I waste my time reading your pretentious self-important nonsense? > > http://xkcd.com/386/ > > ;) Why ROFLMAO when double-plus funny works just as well? From yasar11732 at gmail.com Sat Dec 31 13:34:01 2011 From: yasar11732 at gmail.com (=?utf-8?B?WWHFn2FyIEFyYWJhY8Sx?=) Date: Sat, 31 Dec 2011 20:34:01 +0200 Subject: .format vs. % In-Reply-To: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> Message-ID: What exactly do you mean by putting .format into a variable? You mean like this: "{name} is very {adj} {gender}".format(name="sandy",adj="diligent",gender="female") Sat, 31 Dec 2011 20:19:34 +0200 tarihinde davidfx ??yle yazm??: > Hello everyone, > I just have a quick question about .format and %r %s %d. > > Should we always be using .format() for formatting strings or %? > > Example a = 'apples' > print "I love {0}.".format(a) > > If I wanted to put .format into a variable, how would I do that. > > Thanks for your information in advance. > > David -- Opera'n?n e-posta istemcisi ile g?nderildi: http://www.opera.com/mail From bahamutzero8825 at gmail.com Sat Dec 31 13:34:06 2011 From: bahamutzero8825 at gmail.com (Andrew Berg) Date: Sat, 31 Dec 2011 12:34:06 -0600 Subject: .format vs. % In-Reply-To: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> Message-ID: <4EFF559E.1050408@gmail.com> On 12/31/2011 12:19 PM, davidfx wrote: > Should we always be using .format() for formatting strings or %? In new code, yes. %-style formatting will eventually go away, but probably not for a long time. > If I wanted to put .format into a variable, how would I do that. What do you mean? -- CPython 3.2.2 | Windows NT 6.1.7601.17640 From dgeorge29ca at gmail.com Sat Dec 31 13:44:08 2011 From: dgeorge29ca at gmail.com (davidfx) Date: Sat, 31 Dec 2011 10:44:08 -0800 (PST) Subject: .format vs. % In-Reply-To: References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> Message-ID: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) What is the .format version of this concept? From dgeorge29ca at gmail.com Sat Dec 31 13:44:08 2011 From: dgeorge29ca at gmail.com (davidfx) Date: Sat, 31 Dec 2011 10:44:08 -0800 (PST) Subject: .format vs. % In-Reply-To: References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> Message-ID: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) What is the .format version of this concept? From edriscoll at wisc.edu Sat Dec 31 13:47:02 2011 From: edriscoll at wisc.edu (Evan Driscoll) Date: Sat, 31 Dec 2011 13:47:02 -0500 Subject: .format vs. % In-Reply-To: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: <4EFF58A6.4070605@wisc.edu> How 'bout just: >>> s = "{0} {1} {2} {3}" >>> s.format(1, 2, 3, 4) '1 2 3 4' Evan On 12/31/2011 13:44, davidfx wrote: > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 552 bytes Desc: OpenPGP digital signature URL: From yasar11732 at gmail.com Sat Dec 31 13:51:53 2011 From: yasar11732 at gmail.com (=?utf-8?B?WWHFn2FyIEFyYWJhY8Sx?=) Date: Sat, 31 Dec 2011 20:51:53 +0200 Subject: .format vs. % In-Reply-To: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: You mean like this? =========================== >>> a = "I like {name}" >>> a.format(name="myself") 'I like myself' ============================ Sat, 31 Dec 2011 20:44:08 +0200 tarihinde davidfx ??yle yazm??: > Thanks for your response. I know the following code is not going to be > correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > -- Opera'n?n e-posta istemcisi ile g?nderildi: http://www.opera.com/mail From benjamin.kaplan at case.edu Sat Dec 31 13:57:13 2011 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 31 Dec 2011 13:57:13 -0500 Subject: .format vs. % In-Reply-To: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: On Dec 31, 2011 1:46 PM, "davidfx" wrote: > > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > format is a method of the string class. You store the string the same way you would any other. formatter = "Hello, {}" print(formatter.format("world")) -------------- next part -------------- An HTML attachment was scrubbed... URL: From alex.kapps at web.de Sat Dec 31 13:59:19 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sat, 31 Dec 2011 19:59:19 +0100 Subject: .format vs. % In-Reply-To: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: <4EFF5B87.9060805@web.de> On 31.12.2011 19:44, davidfx wrote: > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > formatter = "{0} {1} {2} {3}" formatter.format(1,2,3,4) I have to say, I simply hate .format(), and I will be very, very upset if classic formatting will really be removed. I understand that .format() has a lot of advantages for more complex formatting, but for the simple stuff I usually do, it's just terribly inconvenient (try {} on a German keyboard). From jason at feedmagnet.com Sat Dec 31 14:05:17 2011 From: jason at feedmagnet.com (Jason Ford) Date: Sat, 31 Dec 2011 11:05:17 -0800 (PST) Subject: Python powering giant screen in Times Square Message-ID: Verizon has a giant HD screen in Times Square today showing a live feed of social media activity ? and it's built entirely in Python. Our app, FeedMagnet, aggregates and curates social content and is powering the screen. It has a Django front-end and pure Python backend to talk to Facebook, Twitter, Instagram, Google+, Foursquare, and other sources. So if you happen to be in Times Square for the crazy party today, tell the people around you that big social media screen is powered by Python! - Jason (founder of FeedMagnet) From alex.kapps at web.de Sat Dec 31 14:06:05 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sat, 31 Dec 2011 20:06:05 +0100 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EFF5D1D.4000905@web.de> On 31.12.2011 19:23, Roy Smith wrote: >>> Why do I waste my time reading your pretentious self-important nonsense? >> >> http://xkcd.com/386/ >> >> ;) > > Why ROFLMAO when double-plus funny works just as well? xkcd/386 has been the excuse for replying to RR for ages and I still don't understand why he gets that much advertence. Charity? Sympathy for the lone and broken? FWIW, it undermines all my attempts to block him. Sigh. From python.list at tim.thechases.com Sat Dec 31 14:24:10 2011 From: python.list at tim.thechases.com (Tim Chase) Date: Sat, 31 Dec 2011 13:24:10 -0600 Subject: .format vs. % In-Reply-To: References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: <4EFF615A.1050008@tim.thechases.com> On 12/31/11 12:57, Benjamin Kaplan wrote: > format is a method of the string class. You store the string the same way > you would any other. > > formatter = "Hello, {}" > print(formatter.format("world")) Just to note that this syntax doesn't quite work in some earlier versions (tested below in 2.6, which came stock in Debian Stable) >>> formatter = "Hello, {}" >>> print(formatter.format("world")) Traceback (most recent call last): File "", line 1, in ValueError: zero length field name in format It needs to be spelled "{0}" -tkc From magawake at gmail.com Sat Dec 31 14:24:37 2011 From: magawake at gmail.com (Mag Gam) Date: Sat, 31 Dec 2011 14:24:37 -0500 Subject: python curses wrapper Message-ID: Hello, I have been struggling reseting the terminal when I try to do KeyboardInterrupt exception therefore I read the documentation for curses.wrapper and it seems to take care of it for me, http://docs.python.org/library/curses.html#curses.wrapper. Can someone please provide a Hello World example with python curses wrapper? tia From lie.1296 at gmail.com Sat Dec 31 14:34:45 2011 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 01 Jan 2012 06:34:45 +1100 Subject: .format vs. % In-Reply-To: <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: On 01/01/2012 05:44 AM, davidfx wrote: > Thanks for your response. I know the following code is not going to be correct but I want to show you what I was thinking. > > formatter = "%r %r %r %r" > > print formatter % (1, 2, 3, 4) > > What is the .format version of this concept? > I don't think the (%r)epr-formatting exist anymore, so if you want to do that you'll need to call repr manually. From alex.kapps at web.de Sat Dec 31 14:34:49 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sat, 31 Dec 2011 20:34:49 +0100 Subject: python curses wrapper In-Reply-To: References: Message-ID: <4EFF63D9.5010100@web.de> On 31.12.2011 20:24, Mag Gam wrote: > Hello, > > I have been struggling reseting the terminal when I try to do > KeyboardInterrupt exception therefore I read the documentation for > curses.wrapper and it seems to take care of it for me, > http://docs.python.org/library/curses.html#curses.wrapper. > > Can someone please provide a Hello World example with python curses wrapper? > > tia Use atexit.register() to register a cleanup function which is called when the program exits: import atexit import curses def cleanup(): curses.nocbreak() stdscr.keypad(0) curses.echo() curses.endwin() atexit.register(cleanup) stdscr = curses.initscr() curses.noecho() curses.cbreak() stdscr.keypad(1) curses.start_color() curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLUE) curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK) stdscr.bkgd(curses.color_pair(1)) stdscr.refresh() win = curses.newwin(5, 20, 5, 5) win.bkgd(curses.color_pair(2)) win.box() win.addstr(2, 2, "Hallo, Welt!") win.refresh() c = stdscr.getch() From robert.kern at gmail.com Sat Dec 31 14:43:50 2011 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 31 Dec 2011 19:43:50 +0000 Subject: .format vs. % In-Reply-To: References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> Message-ID: On 12/31/11 7:34 PM, Lie Ryan wrote: > On 01/01/2012 05:44 AM, davidfx wrote: >> Thanks for your response. I know the following code is not going to be correct >> but I want to show you what I was thinking. >> >> formatter = "%r %r %r %r" >> >> print formatter % (1, 2, 3, 4) >> >> What is the .format version of this concept? >> > > I don't think the (%r)epr-formatting exist anymore, so if you want to do that > you'll need to call repr manually. Yes, it does. formatter = '{!r} {!r} {!r} {!r}' print formatter.format(1,2,3,4) -- 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 From alex.kapps at web.de Sat Dec 31 14:49:55 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sat, 31 Dec 2011 20:49:55 +0100 Subject: python curses wrapper In-Reply-To: <4EFF63D9.5010100@web.de> References: <4EFF63D9.5010100@web.de> Message-ID: <4EFF6763.7080209@web.de> On 31.12.2011 20:34, Alexander Kapps wrote: > On 31.12.2011 20:24, Mag Gam wrote: >> Hello, >> >> I have been struggling reseting the terminal when I try to do >> KeyboardInterrupt exception therefore I read the documentation for >> curses.wrapper and it seems to take care of it for me, >> http://docs.python.org/library/curses.html#curses.wrapper. >> >> Can someone please provide a Hello World example with python >> curses wrapper? >> >> tia > > Use atexit.register() to register a cleanup function which is called > when the program exits: Oh, sorry, I missed the curses.wrapper() part. Here is an example: import curses def main(screen): curses.start_color() curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLUE) curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK) screen.bkgd(curses.color_pair(1)) screen.refresh() win = curses.newwin(5, 20, 5, 5) win.bkgd(curses.color_pair(2)) win.box() win.addstr(2, 2, "Hallo, Welt!") win.refresh() c = screen.getch() try: curses.wrapper(main) except KeyboardInterrupt: print "Got KeyboardInterrupt exception. Exiting..." exit() From magawake at gmail.com Sat Dec 31 14:52:10 2011 From: magawake at gmail.com (Mag Gam) Date: Sat, 31 Dec 2011 14:52:10 -0500 Subject: python curses wrapper In-Reply-To: <4EFF63D9.5010100@web.de> References: <4EFF63D9.5010100@web.de> Message-ID: thanks for your prompt reply. Why would I have to use atexeit? According to the documentation, curses.wrapper should handle what cleanup() should be doing. Neverthless, good to know it exists :p On Sat, Dec 31, 2011 at 2:34 PM, Alexander Kapps wrote: > On 31.12.2011 20:24, Mag Gam wrote: >> >> Hello, >> >> I have been struggling reseting the terminal when I try to do >> KeyboardInterrupt exception therefore I read the documentation for >> curses.wrapper and it seems to take care of it for me, >> http://docs.python.org/library/curses.html#curses.wrapper. >> >> Can someone please provide a Hello World example with python curses >> wrapper? >> >> tia > > > Use atexit.register() to register a cleanup function which is called when > the program exits: > > import atexit > import curses > > def cleanup(): > ? ?curses.nocbreak() > ? ?stdscr.keypad(0) > ? ?curses.echo() > ? ?curses.endwin() > > atexit.register(cleanup) > > stdscr = curses.initscr() > curses.noecho() > curses.cbreak() > stdscr.keypad(1) > > curses.start_color() > curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLUE) > curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK) > > stdscr.bkgd(curses.color_pair(1)) > stdscr.refresh() > > win = curses.newwin(5, 20, 5, 5) > win.bkgd(curses.color_pair(2)) > win.box() > win.addstr(2, 2, "Hallo, Welt!") > win.refresh() > > c = stdscr.getch() > > -- > http://mail.python.org/mailman/listinfo/python-list From tundra at tundraware.com Sat Dec 31 15:26:27 2011 From: tundra at tundraware.com (Tim Daneliuk) Date: Sat, 31 Dec 2011 14:26:27 -0600 Subject: Best Way To Bound Function Execution Time Message-ID: I am writing some paramiko-based ssh routines. One of them logs into a remote server and then does a sudo command. The problem is that if the user provides the incorrect sudo password, the call hangs the other end is waiting for the correct password to be entered. Is there some standard Pythonic way to bound how long a function call can run, after which time it is forcefully terminated? TIA, -- ---------------------------------------------------------------------------- Tim Daneliuk tundra at tundraware.com PGP Key: http://www.tundraware.com/PGP/ From ericsnowcurrently at gmail.com Sat Dec 31 15:47:57 2011 From: ericsnowcurrently at gmail.com (Eric Snow) Date: Sat, 31 Dec 2011 13:47:57 -0700 Subject: Python powering giant screen in Times Square In-Reply-To: References: Message-ID: On Sat, Dec 31, 2011 at 12:05 PM, Jason Ford wrote: > Verizon has a giant HD screen in Times Square today showing a live > feed of social media activity ? and it's built entirely in Python. > > Our app, FeedMagnet, aggregates and curates social content and is > powering the screen. It has a Django front-end and pure Python backend > to talk to Facebook, Twitter, Instagram, Google+, Foursquare, and > other sources. > > So if you happen to be in Times Square for the crazy party today, tell > the people around you that big social media screen is powered by > Python! Wow! That's awesome. Thanks for sharing this! -eric > > - Jason (founder of FeedMagnet) > -- > http://mail.python.org/mailman/listinfo/python-list From a24061 at ducksburg.com Sat Dec 31 15:55:09 2011 From: a24061 at ducksburg.com (Adam Funk) Date: Sat, 31 Dec 2011 20:55:09 +0000 Subject: mutually exclusive arguments to a constructor References: Message-ID: On 2011-12-30, G?nther Dietrich wrote: > Adam Funk wrote: > >>Suppose I'm creating a class that represents a bearing or azimuth, >>created either from a string of traditional bearing notation >>("N24d30mE") or from a number indicating the angle in degrees as >>usually measured in trigonometry (65.5, measured counter-clockwise >>from the x-axis). The class will have methods to return the same >>bearing in various formats. ... > You can determine the type of the input data by using isinstance() and > take the appropriate actions depending on this decision: > >>>> class MyClass(object): > ... def __init__(self, input_data): > ... if isinstance(input_data, basestring): > ... print "Do actions for string type input" > ... elif isinstance(input_data, float): > ... print "Do actions for float type input" > ... def get_output_data(self): > ... return "output data" Aha, I think I like this approach best, partly because I realized after writing my post that it might also be good to accept strings representing "pure" angles (e.g., "65d30m"). So I think I'll use isinstance *and then* check the input string against some regexes to determine whether it's in traditional surveying notation or trig notation in DMS. -- The generation of random numbers is too important to be left to chance. [Robert R. Coveyou] From a24061 at ducksburg.com Sat Dec 31 15:59:17 2011 From: a24061 at ducksburg.com (Adam Funk) Date: Sat, 31 Dec 2011 20:59:17 +0000 Subject: mutually exclusive arguments to a constructor References: Message-ID: <5m94t8x025.ln2@news.ducksburg.com> On 2011-12-30, Roy Smith wrote: > "But!", some C++/Java type bondage addicts might cry, "there's nothing > to prevent somebody from creating a DirectionIndicatingThingie directly, > bypassing the factory functions. There's no way to make the constructor > private!". To which the free-willed pythonistas would respond, "If it > hurts when you do that, don't do that". Actually one problem that can occur in large Java projects is that the package structure requires some things to have public constructors (even when you'd rather not do that) so the Factory class in the main package has access to them. -- English has perfect phonetic spelling. It just doesn't have phonetic pronunciation. [Peter Moylan] From tjreedy at udel.edu Sat Dec 31 16:09:09 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 31 Dec 2011 16:09:09 -0500 Subject: .format vs. % In-Reply-To: <4EFF615A.1050008@tim.thechases.com> References: <5642862.375.1325355574622.JavaMail.geo-discussion-forums@vbbhx10> <3779937.289.1325357048110.JavaMail.geo-discussion-forums@vbtv37> <4EFF615A.1050008@tim.thechases.com> Message-ID: On 12/31/2011 2:24 PM, Tim Chase wrote: > On 12/31/11 12:57, Benjamin Kaplan wrote: >> format is a method of the string class. You store the string the same way >> you would any other. >> >> formatter = "Hello, {}" >> print(formatter.format("world")) > > Just to note that this syntax doesn't quite work in some earlier > versions (tested below in 2.6, which came stock in Debian Stable) Autonumbering of {} was introduced, at my suggestion, in 3.1 and included with 2.7 (I presume). > > >>> formatter = "Hello, {}" > >>> print(formatter.format("world")) > Traceback (most recent call last): > File "", line 1, in > ValueError: zero length field name in format > > > It needs to be spelled "{0}" > > -tkc > > -- Terry Jan Reedy From tjreedy at udel.edu Sat Dec 31 16:27:50 2011 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 31 Dec 2011 16:27:50 -0500 Subject: Python powering giant screen in Times Square In-Reply-To: References: Message-ID: On 12/31/2011 2:05 PM, Jason Ford wrote: > Verizon has a giant HD screen in Times Square today showing a live > feed of social media activity ? and it's built entirely in Python. > > Our app, FeedMagnet, aggregates and curates social content and is > powering the screen. It has a Django front-end and pure Python backend > to talk to Facebook, Twitter, Instagram, Google+, Foursquare, and > other sources. > > So if you happen to be in Times Square for the crazy party today, tell > the people around you that big social media screen is powered by > Python! Great! I will look for it on the TV broadcasts. -- Terry Jan Reedy From d.l.goldsmith at gmail.com Sat Dec 31 17:13:01 2011 From: d.l.goldsmith at gmail.com (OlyDLG) Date: Sat, 31 Dec 2011 14:13:01 -0800 (PST) Subject: [Windows 7, Python 2.6] Can't write to a directory made w/ os.makedirs Message-ID: <50693cc2-93c9-481d-9fb1-42312e69b2d9@c42g2000prb.googlegroups.com> Hi! I'm working on a script utilizing os.makedirs to make directories to which I'm then trying to write files created by exe's spawned w/ subprocess.call; I'm developing in Stani's Python Editor, debugging using Winpdb. I've gotten to the point where subprocess.Popen._execute_child is raising a WindowsError(5,'Access is denied') exception. I've tried: setting the mode in the makedirs call; using os.chmod(, stat.S_IWRITE); and even setting a breakpoint before the subprocess.call and unsetting the read-only attribute of the pertinent directory tree using the Windows directory Properties dialog--which doesn't "take," if that's a clue--all to no avail. I suspect that maybe it has to do w/ not running Winpdb as administrator, but I'm not sure how to do that when starting Winpdb from w/in SPE, and in any event, it doesn't seem reasonable that there's no way to get this to work w/in the script itself. Please help! From fabiofz at gmail.com Sat Dec 31 18:20:50 2011 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Sat, 31 Dec 2011 21:20:50 -0200 Subject: PySide / PyQt autocompletion in IDEs In-Reply-To: <4EFCCC1F.7000605@gmail.com> References: <4EFCCC1F.7000605@gmail.com> Message-ID: On Thu, Dec 29, 2011 at 6:22 PM, Merwin wrote: > Hi, > > I would like to work with PyQt / PySide, but there is a small problem : > methods arguments are not completed by IDE's autocompletion. > > When, typing "PySide.", I correctly get the module's attributes, but when I > want to see what arguments are expected to a constructor / method, all I get > is *args, **kwargs. > > I perfectly understand why this happen, but do you know a way to get around > this ? > > I spend more time looking for arguments in Qt's documentation than > effectively programming, which is a problem :-/ In PyDev you can use a .api file (the same from QScintilla) to be used for the code-completion. Take a look at: http://pydev.org/manual_101_interpreter.html for details (I know it's available for PyQt, but I'm not sure about PySide, but if it's not, maybe it can be requested for the PySide guys). Cheers, Fabio From no.email at nospam.invalid Sat Dec 31 18:47:36 2011 From: no.email at nospam.invalid (Paul Rubin) Date: Sat, 31 Dec 2011 15:47:36 -0800 Subject: Best Way To Bound Function Execution Time References: Message-ID: <7xpqf4e21z.fsf@ruckus.brouhaha.com> Tim Daneliuk writes: > Is there some standard Pythonic way to bound how long a function > call can run, after which time it is forcefully terminated? Basically, run it in a separate process and use os.kill to kill it. From niklasro at gmail.com Sat Dec 31 20:40:15 2011 From: niklasro at gmail.com (Niklas Rosencrantz) Date: Sat, 31 Dec 2011 17:40:15 -0800 (PST) Subject: InvalidResponseError: headers must be str In-Reply-To: <4eff1cdb$0$29966$c3e8da3$5496439d@news.astraweb.com> References: <18871670.1096.1325307490244.JavaMail.geo-discussion-forums@yqdx38> <4efeae6a$0$29966$c3e8da3$5496439d@news.astraweb.com> <4eff057d$0$29966$c3e8da3$5496439d@news.astraweb.com> <9589290.553.1325338274423.JavaMail.geo-discussion-forums@yqcb25> <4eff1cdb$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <19725208.758.1325382015274.JavaMail.geo-discussion-forums@yqja5> Thanks for the replies here. I will have patience but this bug is blocking my integration efforts. I tried logging the TCP packets with tcpdump and nothing special appeared. Niklas From invalid at invalid.invalid Sat Dec 31 21:36:47 2011 From: invalid at invalid.invalid (Grant Edwards) Date: Sun, 1 Jan 2012 02:36:47 +0000 (UTC) Subject: Python education survey References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2011-12-31, Alexander Kapps wrote: > On 31.12.2011 19:23, Roy Smith wrote: > >>>> Why do I waste my time reading your pretentious self-important nonsense? >>> >>> http://xkcd.com/386/ >>> >>> ;) >> >> Why ROFLMAO when double-plus funny works just as well? > > xkcd/386 has been the excuse for replying to RR for ages and I still > don't understand why he gets that much advertence. Charity? Sympathy > for the lone and broken? Sadly, RR's post are often (in the supposed words of Wolfgang Pauli) "not even wrong". -- Grant From alex.kapps at web.de Sat Dec 31 23:53:06 2011 From: alex.kapps at web.de (Alexander Kapps) Date: Sun, 01 Jan 2012 05:53:06 +0100 Subject: Python education survey In-Reply-To: References: <06423dd7-4fbb-4e7a-b529-e697ea862b05@f11g2000yql.googlegroups.com> <36065956-359d-42cf-a5fc-75fdea830737@y7g2000vbe.googlegroups.com> <470c9b9b-780f-4b00-8fef-adba8b036c2b@32g2000yqp.googlegroups.com> <74b1ed63-f755-49e3-a275-92b2658ef991@o14g2000vbo.googlegroups.com> <4efabe42$0$29966$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4EFFE6B2.7040000@web.de> On 01.01.2012 03:36, Grant Edwards wrote: > On 2011-12-31, Alexander Kapps wrote: >> On 31.12.2011 19:23, Roy Smith wrote: >> >>>>> Why do I waste my time reading your pretentious self-important nonsense? >>>> >>>> http://xkcd.com/386/ >>>> >>>> ;) >>> >>> Why ROFLMAO when double-plus funny works just as well? >> >> xkcd/386 has been the excuse for replying to RR for ages and I still >> don't understand why he gets that much advertence. Charity? Sympathy >> for the lone and broken? > > Sadly, RR's post are often (in the supposed words of Wolfgang Pauli) > "not even wrong". > I'm sure, RR is now jumping up high in rapture for being compared to high-profile scientist geniuses. Move fuel for his self-affirmation. I'll give my entire kingdom (or important body-parts, in case my kingdom isn't enough) if people would just understand that perfection(ists) is/are *the most* dangerous thing possible.